-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
267 lines (240 loc) · 6.61 KB
/
config.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package certs
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"html/template"
"io/ioutil"
"os"
"reflect"
"strings"
"golang.org/x/crypto/ssh/terminal"
"github.com/taybart/certs/scheme"
"github.com/taybart/log"
)
type CAConfig struct {
Name string `json:"name,omitempty" label:"CA name"`
Key string `json:"key,omitempty" label:"CA key file name"`
Crt string `json:"crt,omitempty" label:"CA certificate file name"`
Scheme string `json:"scheme,omitempty" label:"Cryptographic scheme"`
}
type Config struct {
Dir string `json:"-"`
CA CAConfig `json:"ca" label:""`
DefaultSubject scheme.Subject `json:"default_subject"`
}
var DefaultConfig = Config{
Dir: fmt.Sprintf("%s/.config/certs", os.Getenv("HOME")),
CA: CAConfig{
Name: "My CA",
Key: fmt.Sprintf("%s/.config/certs/profiles/default/ca.key", os.Getenv("HOME")),
Crt: fmt.Sprintf("%s/.config/certs/profiles/default/ca.crt", os.Getenv("HOME")),
Scheme: "ecdsa256",
},
DefaultSubject: scheme.Subject{
CommonName: "taybart",
OrganizationalUnit: []string{"Engineering"},
Organization: []string{"taybart"},
StreetAddress: []string{""},
PostalCode: []string{""},
Locality: []string{""},
Province: []string{""},
Country: []string{""},
},
}
var config = DefaultConfig
func (c *Config) FirstRun() (err error) {
env := make(map[string]string)
for _, v := range os.Environ() {
split := strings.Split(v, "=")
env[split[0]] = split[1]
}
fmt.Printf("%sError:%s Could not load config, let's set up.\n", log.Red, log.Rtd)
fmt.Printf("%sNote:%s You can use ENV vars like this -> {{ .HOME }} => %s\n", log.Yellow, log.Rtd, env["HOME"])
fmt.Printf(" Hit enter to use default values, labeled with (%svalue%s)\n\n", log.Blue, log.Rtd)
rt := reflect.TypeOf(c.CA)
if rt.Kind() != reflect.Struct {
err = fmt.Errorf("issue getting config fields")
return
}
fmt.Println("Certificate authority details...")
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
defaultValue := reflect.ValueOf(DefaultConfig.CA).Field(i).String()
var res string
res, err = readConfigVar(formatVarPrompt(f.Tag.Get("label"), defaultValue))
if err != nil {
return
}
if res == "" {
reflect.ValueOf(&c.CA).Elem().Field(i).SetString(defaultValue)
continue
}
var buf bytes.Buffer
t := template.Must(template.New("conf").Parse(res))
err = t.Execute(&buf, env)
if err != nil {
return
}
reflect.ValueOf(&c.CA).Elem().Field(i).SetString(buf.String())
}
rt = reflect.TypeOf(c.DefaultSubject)
if rt.Kind() != reflect.Struct {
err = fmt.Errorf("issue getting config fields")
return
}
fmt.Println("Default CSR Subject...")
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
if f.Tag.Get("label") == "" {
continue
}
var res string
switch f.Type.Kind() {
case reflect.String:
d := reflect.ValueOf(DefaultConfig.DefaultSubject).Field(i).String()
res, err = readConfigVar(formatVarPrompt(f.Tag.Get("label"), d))
if err != nil {
return
}
if res == "" {
reflect.ValueOf(&c.DefaultSubject).Elem().Field(i).SetString(d)
continue
}
var buf bytes.Buffer
t := template.Must(template.New("conf").Parse(res))
err = t.Execute(&buf, env)
if err != nil {
return
}
reflect.ValueOf(&c.CA).Elem().Field(i).SetString(buf.String())
case reflect.Slice:
sf := reflect.ValueOf(DefaultConfig.DefaultSubject).Field(i)
res, err = readConfigVar(formatVarPrompt(f.Tag.Get("label"), sf))
if err != nil {
return
}
if res == "" {
reflect.ValueOf(&c.DefaultSubject).Elem().Field(i).Set(sf)
continue
}
var buf bytes.Buffer
t := template.Must(template.New("conf").Parse(res))
err = t.Execute(&buf, env)
if err != nil {
return
}
arr := strings.Split(buf.String(), ",")
reflect.ValueOf(&c.DefaultSubject).Elem().Field(i).Set(reflect.ValueOf(arr))
default:
err = errors.New("Unkown field in config")
return
}
}
return
}
func (c Config) Save() (err error) {
var file []byte
file, err = json.MarshalIndent(c, "", " ")
if err != nil {
err = fmt.Errorf("issue marshalling config file %w", err)
return
}
if _, err = os.Stat(config.Dir); os.IsNotExist(err) {
err = os.MkdirAll(config.Dir, 0755)
if err != nil {
err = fmt.Errorf("issue creating config folder %w", err)
return
}
}
err = ioutil.WriteFile(fmt.Sprintf("%s/config.json", c.Dir), file, 0600)
if err != nil {
err = fmt.Errorf("issue writing config file %w", err)
return
}
return
}
func LoadConfig(configLocation string) (err error) {
config.Dir = configLocation
if _, err = os.Stat(config.Dir); os.IsNotExist(err) {
err = os.MkdirAll(config.Dir, 0755)
if err != nil {
err = fmt.Errorf("issue creating config folder %w", err)
return
}
}
if _, err = os.Stat(fmt.Sprintf("%s/config.json", config.Dir)); os.IsNotExist(err) {
err = nil
err = config.FirstRun()
if err != nil {
panic(fmt.Errorf("Could not set up config %w", err))
}
err = config.Save()
if err != nil {
panic(fmt.Errorf("Could not save new config %w", err))
}
} else if err != nil {
return err
}
c, err := ioutil.ReadFile(fmt.Sprintf("%s/config.json", config.Dir))
if err != nil {
err = fmt.Errorf("issue reading config %w", err)
return
}
err = json.Unmarshal(c, &config)
if err != nil {
err = fmt.Errorf("issue reading config %w", err)
return
}
// Set default Subject
scheme.SetDefaultSubject(config.DefaultSubject)
return
}
func LoadConfigFromFile(location string) (err error) {
c, err := ioutil.ReadFile(location)
if err != nil {
err = fmt.Errorf("issue reading config %w", err)
return
}
err = json.Unmarshal(c, &config)
if err != nil {
err = fmt.Errorf("issue marshalling config %w", err)
return
}
return
}
func GetDefaultSubject() scheme.Subject {
return config.DefaultSubject
}
func GetDefaultScheme() string {
return config.CA.Scheme
}
func (c *Config) GetCAPassword() []byte {
fmt.Printf("Enter CA Password -> ")
tty, err := os.Open("/dev/tty") // Use tty just in case stdin is pipe
if err != nil {
panic(fmt.Errorf("can't open /dev/tty: %w", err))
}
bytePassword, err := terminal.ReadPassword(int(tty.Fd()))
if err != nil {
panic(err)
}
return bytePassword
}
func formatVarPrompt(label string, defaultValue interface{}) string {
return fmt.Sprintf("%s%s%s (%s%s%s) -> ",
log.BoldGreen, label, log.Rtd,
log.Blue, defaultValue, log.Rtd)
}
func readConfigVar(prompt string) (val string, err error) {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s", prompt)
val, err = reader.ReadString('\n')
if err != nil {
return
}
val = strings.TrimSuffix(val, "\n")
return
}