-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_write.go
51 lines (43 loc) · 1.25 KB
/
cmd_write.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
package main
import (
"io/ioutil"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssm"
"gopkg.in/alecthomas/kingpin.v2"
)
type writeCommand struct {
Name string
Overwrite bool
}
func configureWriteCommand(app *kingpin.Application) {
wc := &writeCommand{}
write := app.Command("write", "Write secret to parameter store").Action(wc.runWrite)
write.Arg("name", "Secret name").StringVar(&wc.Name)
write.Flag("overwrite", "Overwrite the existing secret").Default("false").BoolVar(&wc.Overwrite)
}
func (wc *writeCommand) runWrite(ctx *kingpin.ParseContext) error {
config := aws.NewConfig().WithRegion(*region)
sess, err := newSession(config, mfaSerial, roleArn)
if err != nil {
return err
}
ssmClient := ssm.New(sess, config)
// read secret value from stdin and convert to a string
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
val := strings.TrimSpace(string(data))
// write the secret to the parameter store
ppInput := &ssm.PutParameterInput{
KeyId: kmsAlias,
Name: aws.String(wc.Name),
Type: aws.String(ssm.ParameterTypeSecureString),
Value: aws.String(val),
Overwrite: aws.Bool(wc.Overwrite),
}
_, err = ssmClient.PutParameter(ppInput)
return err
}