/
edit.go
352 lines (307 loc) · 9.63 KB
/
edit.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
Copyright 2019 Ridecell, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"bytes"
"fmt"
"html/template"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/Ridecell/ridectl/pkg/cmd/edit"
"github.com/Ridecell/ridectl/pkg/kubernetes"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/manifoldco/promptui"
"github.com/pkg/errors"
"github.com/shurcooL/httpfs/vfsutil"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(editCmd)
}
var filenameFlag string
var keyIdFlag string
var recrypt bool
var whitespaceRegexp *regexp.Regexp
func init() {
editCmd.Flags().BoolVarP(&recrypt, "recrypt", "r", false, "(optional) re-encrypts all secrets in file")
editCmd.Flags().StringVarP(&filenameFlag, "file", "f", "", "(optional) Path to the file to edit")
editCmd.Flags().StringVarP(&keyIdFlag, "key", "k", "", "(optional) KMS key ID to use for encrypting")
whitespaceRegexp = regexp.MustCompile(`\s+`)
}
/*
An explanation of the overall edit process:
1. The existing file is loaded and parsed.
2. That parsed data is decrypted using KMS.
3. A new YAML document is written to a tempfile with the decrypted data.
4. The tempfile is opened in $EDITOR.
5. The tempfile is re-read and parsed.
6. The old and new data is correlated to match up any objects that exist in both.
7. The parsed data is encrypted using KMS if the value changed.
8. A new YAML document is written to the original file.
*/
var editCmd = &cobra.Command{
Use: "edit [flags] <cluster_name>",
Short: "Edit an instance manifest",
Long: `Edit a Summon instance manifest file that contains encrypted secret values`,
Args: func(_ *cobra.Command, args []string) error {
if filenameFlag == "" {
if len(args) == 0 {
return fmt.Errorf("Cluster name argument is required")
}
if len(args) > 1 {
return fmt.Errorf("Too many arguments")
}
} else {
if len(args) > 0 {
return fmt.Errorf("Too many arguments")
}
}
return nil
},
RunE: func(_ *cobra.Command, args []string) error {
// Work out which file we are editing.
filename := filenameFlag
if filename == "" {
match := regexp.MustCompile(`^([a-z0-9]+)-([a-z]+)$`).FindStringSubmatch(args[0])
if match == nil {
return errors.Errorf("unable to parse instance name %s", args[0])
}
filenames, err := filepath.Glob(fmt.Sprintf(`*%s/%s.yml`, match[2], match[1]))
if len(filenames) > 1 {
return errors.New("found multiple matches for filepath")
}
if err != nil {
return err
}
if filenames != nil {
filename = filenames[0]
} else {
// Prompt user for region when creating new file
regionPrompt := promptui.Prompt{
Label: "Enter region (eu, us, in, etc.)",
}
fileRegion, err := regionPrompt.Run()
if err != nil {
return err
}
filename = fmt.Sprintf("%s-%s/%s.yml", fileRegion, match[2], match[1])
}
}
// Read the file in.
var inStream io.Reader
inFile, err := os.Open(filename)
if err != nil {
if os.IsNotExist(err) && filenameFlag == "" {
// No file, render the template with the default content.
buffer, err := createDefaultData(args[0])
if err != nil {
return errors.Wrap(err, "error creating default data")
}
inStream = buffer
} else {
return errors.Wrapf(err, "error reading input file %s", filename)
}
} else {
defer inFile.Close()
inStream = inFile
}
// Parse the input file to objects.
inManifest, err := edit.NewManifest(inStream)
if err != nil {
return errors.Wrap(err, "error decoding input YAML")
}
// Create a KMS session
// TODO error handling for AWS creds
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Config: aws.Config{
Region: aws.String("us-west-1"),
},
}))
kmsService := kms.New(sess)
// Decrypt all the encrypted secrets.
err = inManifest.Decrypt(kmsService)
if err != nil {
return errors.Wrap(err, "error decrypting input manifest")
}
// Edit!
afterManifest, err := editObjects(inManifest, "")
if err != nil {
return errors.Wrap(err, "error editing objects")
}
// Match up the new objects with the old.
afterManifest.CorrelateWith(inManifest)
// Re-encrypt anything that needs it.
keyId := keyIdFlag
if keyId == "" {
keyId, err = edit.FindKeyId(filename)
if err != nil {
return errors.Wrap(err, "error finding key ID")
}
}
err = afterManifest.Encrypt(kmsService, keyId, keyIdFlag != "" || recrypt, recrypt)
if err != nil {
return errors.Wrap(err, "error encrypting after manifest")
}
// Write out the file again.
// TODO make sure the file is writable before doing all this.
outFile, err := os.Create(filename)
if err != nil {
return errors.Wrapf(err, "error opening %s for writing", filename)
}
defer outFile.Close()
afterManifest.Serialize(outFile)
return nil
},
}
func runEditor(filename string) error {
editor := os.Getenv("EDITOR")
if editor == "" {
return errors.New("No $EDITOR set")
}
// Deal with an editor that has options.
editorParts := whitespaceRegexp.Split(editor, -1)
executable := editorParts[0]
executable, err := exec.LookPath(executable)
editorParts = append(editorParts, filename)
cmd := exec.Command(executable, editorParts[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return errors.Wrap(err, "error running editor")
}
return nil
}
func editObjects(manifest edit.Manifest, comment string) (edit.Manifest, error) {
manifestBuf := bytes.Buffer{}
err := manifest.Serialize(&manifestBuf)
if err != nil {
return nil, errors.Wrap(err, "error encoding objects to YAML")
}
for {
// Format the comment.
commentBuf := bytes.Buffer{}
if comment != "" {
for _, line := range strings.Split(comment, "\n") {
commentBuf.WriteString("# ")
commentBuf.WriteString(line)
commentBuf.WriteString("\n")
}
commentBuf.WriteString("#\n")
}
commentReader := bytes.NewReader(commentBuf.Bytes())
// Make the YAML to show in the editor.
editorBuf := bytes.Buffer{}
commentReader.WriteTo(&editorBuf)
manifestBuf.WriteTo(&editorBuf)
editorReader := bytes.NewReader(editorBuf.Bytes())
// Open a temporary file.
tmpfile, err := ioutil.TempFile("", ".*.yml")
if err != nil {
return nil, errors.Wrap(err, "error making tempfile")
}
defer tmpfile.Close()
defer os.Remove(tmpfile.Name())
editorReader.WriteTo(tmpfile)
tmpfile.Sync()
// Show the editor.
err = runEditor(tmpfile.Name())
if err != nil {
return nil, errors.Wrap(err, "error running editor")
}
// Re-read the edited file.
afterTmpfile, err := os.Open(tmpfile.Name())
if err != nil {
return nil, errors.Wrapf(err, "error re-opening tempfile %s", tmpfile.Name())
}
defer afterTmpfile.Close()
afterBuf := bytes.Buffer{}
_, err = afterBuf.ReadFrom(afterTmpfile)
if err != nil {
return nil, errors.Wrapf(err, "error reading tempfile %s", tmpfile.Name())
}
// If we're reencrypting ignore this equality check.
// Check if the file was edited at all.
if bytes.Equal(editorBuf.Bytes(), afterBuf.Bytes()) && !recrypt {
return nil, errors.New("tempfile not edited, aborting")
}
// Try strip off the comment.
afterReader := bytes.NewReader(afterBuf.Bytes())
seekPos := int64(0)
if bytes.Equal(commentBuf.Bytes(), afterBuf.Bytes()[:commentBuf.Len()]) {
seekPos = int64(commentBuf.Len())
}
afterReader.Seek(seekPos, 0)
outManifest, err := edit.NewManifest(afterReader)
if err == nil {
// Decode success, we're done!
return outManifest, nil
}
// Some kind decoding error, probably bad syntax, show the editor again.
comment = fmt.Sprintf("Error parsing file:\n%s", err)
manifestBuf.Reset()
afterReader.Seek(seekPos, 0)
afterReader.WriteTo(&manifestBuf)
}
}
func createDefaultData(instance string) (io.Reader, error) {
templateData, err := vfsutil.ReadFile(Templates, "new_instance.yml.tpl")
if err != nil {
return nil, errors.Wrap(err, "error reading new instance template")
}
template, err := template.New("new_instance.yml.tpl").Parse(string(templateData))
if err != nil {
return nil, errors.Wrap(err, "error parsing new instance template")
}
match := regexp.MustCompile(`^([a-z0-9]+)-([a-z]+)$`).FindStringSubmatch(instance)
if match == nil {
return nil, errors.Errorf("unable to parse instance name %s", instance)
}
// Prompt user for a slack channel to alert to
slackChannelPrompt := promptui.Prompt{
Label: "Enter a slack channel name (#channel-name, blank to skip)",
Validate: func(input string) error {
if !strings.HasPrefix(input, "#") && input != "" {
return errors.New(`Channel name must have prefix "#"`)
}
return nil
},
}
slackChannelName, err := slackChannelPrompt.Run()
if err != nil {
return nil, err
}
buffer := &bytes.Buffer{}
err = template.Execute(buffer, struct {
Name string
Namespace string
SlackChannel string
}{
Name: instance,
Namespace: kubernetes.ParseNamespace(instance),
SlackChannel: slackChannelName,
})
if err != nil {
return nil, errors.Wrap(err, "error rendering new instance template")
}
return buffer, nil
}