generated from brevdev/seed
/
secret.go
204 lines (179 loc) · 6.01 KB
/
secret.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
// Package secret lets you add secrests. Language Go makes you write extra.
package secret
import (
"encoding/json"
"fmt"
"github.com/brevdev/brev-cli/pkg/cmdcontext"
"github.com/brevdev/brev-cli/pkg/entity"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/brevdev/brev-cli/pkg/store"
"github.com/brevdev/brev-cli/pkg/terminal"
"github.com/spf13/cobra"
)
type SecretStore interface {
CreateSecret(req store.CreateSecretRequest) (*store.CreateSecretRequest, error)
GetCurrentUser() (*entity.User, error)
GetActiveOrganizationOrDefault() (*entity.Organization, error)
}
func NewCmdSecret(secretStore SecretStore, t *terminal.Terminal) *cobra.Command {
var envtype string
var name string
var value string
var path string
var scope string
cmd := &cobra.Command{
Annotations: map[string]string{"housekeeping": ""},
Use: "secret",
Short: "Add a secret/environment variable",
Long: "Add a secret/environment variable to your dev environment, all dev environment in an org, or all of your dev environment",
Example: `
brev secret --name naaamme --value vaaalluueee --type [file, variable] --file-path --scope personal
brev secret --name SERVER_URL --value https://brev.sh --type variable --scope personal
brev secret --name AWS_KEY --value ... --type file --file-path --scope personal
`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
err := cmdcontext.InvokeParentPersistentPreRun(cmd, args)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
},
// Args: cobra.MinimumNArgs(0),
// ValidArgs: []string{"orgs", "workspaces"},
RunE: func(cmd *cobra.Command, args []string) error {
err := addSecret(secretStore, t, envtype, name, value, path, scope)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
},
}
cmd.Flags().StringVarP(&envtype, "type", "t", "", "type of secret (env var or file)")
cmd.Flags().StringVarP(&name, "name", "n", "", "name of environment variable or secret file")
cmd.Flags().StringVarP(&value, "value", "v", "", "value of environment variable or secret file")
cmd.Flags().StringVarP(&path, "file-path", "p", "", "file path (if secret file)")
cmd.Flags().StringVarP(&scope, "scope", "s", "", "scope for env var (org or private)")
err := cmd.RegisterFlagCompletionFunc("type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"file", "variable"}, cobra.ShellCompDirectiveNoSpace
})
if err != nil {
breverrors.GetDefaultErrorReporter().ReportError(breverrors.WrapAndTrace(err))
fmt.Print(breverrors.WrapAndTrace(err))
}
err = cmd.RegisterFlagCompletionFunc("scope", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"org", "private"}, cobra.ShellCompDirectiveNoSpace
})
if err != nil {
breverrors.GetDefaultErrorReporter().ReportError(breverrors.WrapAndTrace(err))
fmt.Print(breverrors.WrapAndTrace(err))
}
return cmd
}
func addSecret(secretStore SecretStore, t *terminal.Terminal, envtype string, name string, value string, path string, scope string) error { //nolint:funlen, gocyclo // todo simplify me
if name == "" || envtype == "" || value == "" || path == "" {
t.Vprintf(t.Yellow("\nSome flags omitted, running interactive mode!\n"))
}
if name == "" {
name = terminal.PromptGetInput(terminal.PromptContent{
Label: "Environment variable/secret name: ",
ErrorMsg: "error",
})
}
if envtype == "" {
envtype = terminal.PromptSelectInput(terminal.PromptSelectContent{
Label: "Type of variable: ",
ErrorMsg: "error",
Items: []string{"file", "variable"},
})
}
if value == "" {
value = terminal.PromptGetInput(terminal.PromptContent{
Label: "Environment variable/secret value: ",
ErrorMsg: "error",
})
}
if path == "" && envtype == "file" {
path = terminal.PromptGetInput(terminal.PromptContent{
Label: "Path for the file: ",
ErrorMsg: "error",
Default: "/home/brev/workspace/secret.txt",
})
}
if scope == "" {
scope = terminal.PromptSelectInput(terminal.PromptSelectContent{
Label: "Scope: ",
ErrorMsg: "error",
Items: []string{"org", "user"},
})
}
if envtype == "file" {
t.Vprintf("brev secret --name %s --value %s --type %s --file-path %s --scope %s\n", name, value, envtype, path, scope)
} else {
t.Vprintf("brev secret --name %s --value %s --type %s --scope %s\n", name, value, envtype, scope)
}
s := t.NewSpinner()
s.Suffix = " encrypting and saving secret var"
s.Start()
iScope := store.Org
var hierarchyID string
if scope == "user" {
iScope = store.User
// get user id
me, err := secretStore.GetCurrentUser()
if err != nil {
return breverrors.WrapAndTrace(err)
}
hierarchyID = me.ID
} else {
// get org id
defaultOrg, err := secretStore.GetActiveOrganizationOrDefault()
if err != nil {
return breverrors.WrapAndTrace(err)
}
if defaultOrg == nil {
return fmt.Errorf("no orgs exist")
}
hierarchyID = defaultOrg.ID
}
var configDest store.DestConfig
iType := store.File
if envtype == "variable" {
iType = store.EnvVariable
configDest = store.DestConfig{
Name: name,
}
} else {
configDest = store.DestConfig{
Path: path,
}
}
// NOTE: hieararchyID needs to be the org ID user ID
b := store.CreateSecretRequest{
Name: name,
HierarchyType: iScope,
HierarchyID: hierarchyID,
Src: store.SecretReqSrc{
Type: store.KeyValue,
Config: store.SrcConfig{
Value: value,
},
},
Dest: store.SecretReqDest{
Type: iType,
Config: configDest,
},
}
asstring, _ := json.MarshalIndent(b, "", "\t")
fmt.Print(string(asstring))
secret, err := secretStore.CreateSecret(b)
if err != nil {
s.Stop()
t.Vprintf(t.Red(err.Error()))
return breverrors.WrapAndTrace(err)
}
t.Vprintf(secret.Name)
s.Suffix = " environment secret added"
s.Stop()
t.Vprintf(t.Green("\nEnvironment %s added\n", iType) + t.Yellow("\tNote: It might take up to 2 minutes to load into your environment."))
return nil
}