/
main.go
111 lines (100 loc) · 2.24 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"bufio"
"crypto/rand"
"flag"
"fmt"
"github.com/cloudfoundry-incubator/bbs/encryption"
"github.com/cloudfoundry-incubator/bbs/format"
"github.com/cloudfoundry-incubator/bbs/models"
"github.com/codegangsta/cli"
"github.com/kr/pretty"
"io"
"os"
)
func main() {
enc_flags := []cli.Flag{
cli.StringFlag{
Name: "encryptionKey",
Value: "key1:a secure passphrase",
},
cli.StringFlag{
Name: "activeKeyLabel",
Value: "key1",
},
}
app := cli.NewApp()
app.Commands = []cli.Command{
{
Name: "desiredlrpinfo",
Aliases: []string{"di"},
Usage: "decode for DesiredLRPInfo",
Flags: enc_flags,
Action: func(c *cli.Context) {
var model models.DesiredLRPRunInfo
print(c, &model)
},
},
{
Name: "desiredlrpschedulinginfo",
Aliases: []string{"dsi"},
Usage: "decode for DesiredLRPSchedulingInfo",
Flags: enc_flags,
Action: func(c *cli.Context) {
var model models.DesiredLRPSchedulingInfo
print(c, &model)
},
},
{
Name: "actuallrp",
Aliases: []string{"a"},
Usage: "decode for ActualLRP",
Flags: enc_flags,
Action: func(c *cli.Context) {
var model models.ActualLRP
print(c, &model)
},
},
}
app.Run(os.Args)
}
type Model interface {
Unmarshal([]byte) error
}
func print(c *cli.Context, model Model) {
var err error
var value []byte
key := c.String("encryptionKey")
label := c.String("activeKeyLabel")
if len(c.Args()) > 0 {
value = []byte(c.Args()[0])
} else {
bio := bufio.NewReader(os.Stdin)
value, err = bio.ReadBytes(0)
if err != io.EOF {
panic(err)
os.Exit(1)
}
}
err = model.Unmarshal(decrypt([]byte(value), key, label))
if err != nil {
fmt.Println(err)
} else {
pretty.Print(model)
}
}
func decrypt(value []byte, key string, label string) []byte {
args := []string{}
flagSet := flag.NewFlagSet("", flag.PanicOnError)
eflags := encryption.AddEncryptionFlags(flagSet)
args = append(args, "-encryptionKey="+key)
args = append(args, "-activeKeyLabel="+label)
flagSet.Parse(args)
keyManager, err := eflags.Validate()
encoder := format.NewEncoder(encryption.NewCryptor(keyManager, rand.Reader))
payload, err := encoder.Decode([]byte(value))
if err != nil {
panic(err)
}
return payload
}