/
editstruct.go
208 lines (177 loc) · 4.69 KB
/
editstruct.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
package frontend
import (
"bytes"
"crypto/x509"
"fmt"
"log"
"strings"
"text/template"
"github.com/IPA-CyberLab/kmgm/keyusage"
"gopkg.in/yaml.v2"
)
type templateContext struct {
ErrorString string `yaml:"-"`
Config interface{} `yaml:"config"`
}
const stripBeforeLine = "# *** LINES ABOVE WILL BE AUTOMATICALLY DELETED ***"
const configTemplateTextPrologue = `
{{- define "subject" -}}
# The subject explains name, affiliation, and location of the target computer,
# user, or service the cert is issued against.
subject:
commonName: {{ .CommonName | YamlEscapeString }}
organization: {{ .Organization | YamlEscapeString }}
organizationalUnit: {{ .OrganizationalUnit | YamlEscapeString }}
country: {{ .Country | YamlEscapeString }}
locality: {{ .Locality | YamlEscapeString }}
province: {{ .Province | YamlEscapeString }}
streetAddress: {{ .StreetAddress | YamlEscapeString }}
postalCode: {{ .PostalCode | YamlEscapeString }}
{{- end -}}
{{- with .ErrorString -}}
# Please address the following error:
{{ PrependYamlCommentLiteral . -}}
{{ StripBeforeLine }}
{{- end }}
{{- with .Config -}}
`
const configTemplateTextEpilogue = `{{- end }}`
func PrependYamlCommentLiteral(s string) string {
var b strings.Builder
for {
ss := strings.SplitN(s, "\n", 2)
b.WriteString("# ")
b.WriteString(ss[0])
b.WriteRune('\n')
if len(ss) < 2 {
break
}
s = ss[1]
}
return b.String()
}
func StripErrorText(s string) string {
lines := strings.Split(s, "\n")
for i, l := range lines {
if l == stripBeforeLine {
return strings.Join(lines[i+1:], "\n")
}
}
return s
}
func YamlEscapeString(s string) string {
bs, err := yaml.Marshal(s)
if err != nil {
log.Panicf("Failed to yaml.Marshal string %q: %v", s, err)
}
// omit last \n
bs = bs[:len(bs)-1]
return string(bs)
}
func CallVerifyMethod(cfgI interface{}) error {
cfg := cfgI.(interface {
Verify() error
})
if err := cfg.Verify(); err != nil {
return err
}
return nil
}
func makeTemplate(tmplstr string) (*template.Template, error) {
tmplstrFull := configTemplateTextPrologue + tmplstr + configTemplateTextEpilogue
tmpl, err :=
template.New("setupconfig").
Funcs(template.FuncMap{
"PrependYamlCommentLiteral": PrependYamlCommentLiteral,
"YamlEscapeString": YamlEscapeString,
"StripBeforeLine": func() string { return stripBeforeLine },
"CommentOutIfFalse": func(e bool) string {
if e {
return ""
}
return "# "
},
"TestKeyUsageBit": func(bitName string, ku x509.KeyUsage) bool {
bit, err := keyusage.KeyUsageFromString(bitName)
if err != nil {
panic(err)
}
return (ku & bit) != 0
},
"HasExtKeyUsage": func(ekuName string, ekus []x509.ExtKeyUsage) bool {
eku, err := keyusage.ExtKeyUsageFromString(ekuName)
if err != nil {
panic(err)
}
for _, e := range ekus {
if e == eku {
return true
}
}
return false
},
}).
Parse(tmplstrFull)
if err != nil {
return nil, err
}
return tmpl, nil
}
func DumpTemplate(tmplstr string, cfg interface{}) error {
tmpl, err := makeTemplate(tmplstr)
if err != nil {
return err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, templateContext{Config: cfg}); err != nil {
return err
}
buf.WriteString(`
# noDefault prevents kmgm from assigning default values to unspecified fields.
# Setting "noDefault: true" is recommended for non-interactive invocations to
# avoid unintended behavior.
noDefault: true
`)
if _, err := fmt.Print(buf.String()); err != nil {
return err
}
return nil
}
func EditStructWithVerifier(fe Frontend, tmplstr string, cfg interface{}, VerifyCfg func(cfg interface{}) error) error {
tmpl, err := makeTemplate(tmplstr)
if err != nil {
return err
}
tctx := templateContext{Config: cfg}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, tctx); err != nil {
return err
}
cfgtxt := buf.String()
VerifyText := func(src string) (string, error) {
if err := yaml.UnmarshalStrict([]byte(src), tctx.Config); err != nil {
// yaml error means that we can't use the template (we will lose the full data)
txtwerr := strings.Join([]string{
"# Please correct syntax error:\n",
PrependYamlCommentLiteral(err.Error()),
stripBeforeLine, "\n",
StripErrorText(src)},
"")
return txtwerr, err
}
if err := VerifyCfg(tctx.Config); err != nil {
tctx.ErrorString = err.Error()
var buf bytes.Buffer
if err := tmpl.Execute(&buf, tctx); err != nil {
// FIXME[P3]: How should we handle template exec error?
panic(err)
}
return buf.String(), err
}
return src, nil
}
if _, err := fe.EditText(cfgtxt, VerifyText); err != nil {
return err
}
return nil
}