/
secrets.go
180 lines (155 loc) · 4.43 KB
/
secrets.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
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
//
// 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 (
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/naveego/bosun/pkg/cli"
"github.com/naveego/bosun/pkg/environment"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"log"
)
var secretsCmd = addCommand(rootCmd, &cobra.Command{
Use: "secrets",
Aliases: []string{"secret"},
Short: "Root command for managing secrets commands.",
})
var secretsListCmd = addCommand(secretsCmd, &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "Lists secrets in current environment.",
RunE: func(cmd *cobra.Command, args []string) error {
b := MustGetBosun()
e := b.GetCurrentEnvironment()
secretGroups, err := e.GetSecretGroupConfigs()
if err != nil {
return err
}
for _, group := range secretGroups {
fmt.Printf("%s:\n", group.Name)
for _, secret := range group.Secrets {
fmt.Printf("\t%s\n", secret.Name)
}
}
return nil
},
})
var secretsAddGroupCmd = addCommand(secretsCmd, &cobra.Command{
Use: "add-group {name}",
Args: cobra.ExactArgs(1),
Short: "Adds a secret group to the current environment.",
RunE: func(cmd *cobra.Command, args []string) error {
return addSecretGroup(args[0])
},
})
func addSecretGroup(groupName string) error {
b := MustGetBosun()
e := b.GetCurrentEnvironment()
var passwordStrategyIndex int
prompt := &survey.Select{
Message: "How do you want to provide the password?",
Options: []string{
"Enter password",
"Use lastpass",
"Store insecurely in group (only for dev environments)",
},
}
check(survey.AskOne(prompt, &passwordStrategyIndex))
keyConfig := &environment.SecretKeyConfig{}
switch passwordStrategyIndex {
case 0:
keyConfig.Prompt = true
case 1:
var lastpassPath, lastpassField string
check(survey.AskOne(&survey.Input{
Message: "What is the path in lastpass where the password is stored?",
}, &lastpassPath))
check(survey.AskOne(&survey.Input{
Message: "What is the field containing the password?",
Default: "password",
}, &lastpassField))
keyConfig.Lastpass = &environment.LastpassKeyConfig{
Path: lastpassPath,
Field: lastpassField,
}
case 2:
if !e.IsLocal {
return errors.New("Insecure storage of the key only allowed in local environments.")
}
var password string
check(survey.AskOne(&survey.Input{
Message: "Enter the password to save in the file",
}, &password))
keyConfig.UnsafeStoredPassphrase = password
default:
panic("invalid response")
}
err := e.AddSecretGroup(groupName, keyConfig)
return err
}
var secretsSetCmd = addCommand(secretsCmd, &cobra.Command{
Use: "add {group} {name} [value]",
Args: cobra.RangeArgs(2, 3),
Short: "Sets a secret value. You will be prompted to set the secret if not provided.",
RunE: func(cmd *cobra.Command, args []string) error {
b := MustGetBosun()
e := b.GetCurrentEnvironment()
var secret string
if len(args) == 3 {
secret = args[2]
} else {
secret = cli.RequestSecretFromUser("Secret")
}
groupName := args[0]
secretName := args[1]
_, err := e.GetSecretGroupConfig(groupName)
if err != nil {
err = addSecretGroup(groupName)
if err != nil {
return err
}
}
err = e.AddOrUpdateSecretValue(groupName, secretName, secret)
if err != nil {
return err
}
log.Printf("Added or updated secret %s in group %s.", args[1], args[0])
return nil
},
})
var secretsShowCmd = addCommand(secretsCmd, &cobra.Command{
Use: "show { secretPath | {group} {secret} }",
Args: cobra.RangeArgs(1, 2),
Short: "Shows a secret",
RunE: func(cmd *cobra.Command, args []string) error {
b := MustGetBosun()
e := b.GetCurrentEnvironment()
switch len(args){
case 1:
secret, err := e.ResolveSecretPath(args[0])
if err != nil {
return err
}
fmt.Println(secret)
case 2:
secret, err := e.GetSecretValue(args[0], args[1])
if err != nil {
return err
}
fmt.Println(secret)
}
return nil
},
})