-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
159 lines (137 loc) · 3.49 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bytes"
"encoding/base64"
"flag"
"fmt"
gosecret "github.com/cimpress-mcp/gosecret/api"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)
func main() {
os.Exit(realMain())
}
func realMain() int {
var mode string
var value string
var keystore string
var keyname string
var rotate bool
var fileName string
flag.Usage = usage
flag.StringVar(
&mode, "mode", "encrypt",
"mode of operation, either keygen, encrypt, or decrypt; defaults to encrypt")
flag.StringVar(
&value, "value", "",
"value to encrypt/decrypt in lieu of file")
flag.StringVar(
&keystore, "keystore", "/keys/",
"directory in which keys are stored")
flag.StringVar(
&keyname, "key", "",
"name of a key file to use for encryption")
flag.BoolVar(
&rotate, "rotate", true,
"if encrypting, whether to rotate any already-encrypted tags to the new key")
flag.Parse()
if value == "" {
if flag.NArg() != 1 {
flag.Usage()
return 1
} else {
fileName = flag.Args()[0]
}
}
if mode == "encrypt" {
if (keyname == "") {
fmt.Println("A -key must be provided for encryption")
return 2
}
rawBytes := getBytes(value, fileName)
fileContents, err := gosecret.EncryptTags(rawBytes, keyname, keystore, rotate)
if (err != nil) {
fmt.Println("encryption failed", err)
return 4
}
data := string(fileContents)
// Create a template, add the function map, and parse the text.
// FuncMap maps the goEncrypt (and goDecrypt below) names to functions so that the
// template recognizes and knows what to do when it encounters such tags during parsing
funcs := template.FuncMap{
// Template functions
"goEncrypt": goEncryptFunc(keystore),
}
tmpl, err := template.New("encryption").Funcs(funcs).Parse(data)
if err != nil {
fmt.Println("Could not parse template", err)
return 99
}
// Run the template to verify the output.
buff := new(bytes.Buffer)
err = tmpl.Execute(buff, nil)
if err != nil {
fmt.Println("Could not execute template", err)
return 98
}
fmt.Printf(string(buff.Bytes()))
} else if mode == "decrypt" {
rawBytes := getBytes(value, fileName)
fileContents, err := gosecret.DecryptTags(rawBytes, keystore)
if (err != nil) {
fmt.Println("err", err)
return 8
}
data := string(fileContents)
funcs := template.FuncMap{
// Template functions
"goDecrypt": goDecryptFunc(keystore),
}
tmpl, err := template.New("decryption").Funcs(funcs).Parse(data)
if err != nil {
fmt.Println("Could not parse template", err)
return 99
}
// Run the template to verify the output.
buff := new(bytes.Buffer)
err = tmpl.Execute(buff, nil)
if err != nil {
fmt.Println("Could not execute template", err)
return 98
}
fmt.Printf(string(buff.Bytes()))
} else if mode == "keygen" {
key := gosecret.CreateKey()
encodedKey := make([]byte, base64.StdEncoding.EncodedLen(len(key)))
base64.StdEncoding.Encode(encodedKey, key)
ioutil.WriteFile(fileName, encodedKey, 0666)
} else {
fmt.Println("Unknown mode", mode)
return 16
}
return 0
}
func getBytes(value string, fileName string) []byte {
if value != "" {
return []byte(value)
}
file, err := ioutil.ReadFile(fileName)
if err != nil {
fmt.Println("Unable to read file for encryption", err)
return nil
}
return file
}
func usage() {
cmd := filepath.Base(os.Args[0])
fmt.Fprintf(os.Stderr, strings.TrimSpace(helpText)+"\n\n", cmd)
flag.PrintDefaults()
}
const helpText = `
Usage: %s [options] file
Encrypt or decrypt file using gosecret.
Options:
`