Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 9cbe648

Browse files
committed
Add more secret creation input paths
* --from-literal * --from-file * fallback to stdin prompt
1 parent 3d90829 commit 9cbe648

File tree

4 files changed

+77
-23
lines changed

4 files changed

+77
-23
lines changed

ci/integration/integration_test.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ func TestSecrets(t *testing.T) {
115115
c.Run(ctx, "coder secrets create").Assert(t,
116116
tcli.Error(),
117117
tcli.StdoutEmpty(),
118-
tcli.StderrMatches("required flag"),
119118
)
120119

121-
c.Run(ctx, fmt.Sprintf("coder secrets create --name %s --value %s", name, value)).Assert(t,
120+
// this tests the "Value:" prompt fallback
121+
c.Run(ctx, fmt.Sprintf("echo %s | coder secrets create %s", value, name)).Assert(t,
122122
tcli.Success(),
123123
tcli.StderrEmpty(),
124124
)
@@ -146,6 +146,29 @@ func TestSecrets(t *testing.T) {
146146
tcli.Error(),
147147
tcli.StdoutEmpty(),
148148
)
149+
150+
name, value = randString(8), randString(8)
151+
c.Run(ctx, fmt.Sprintf("coder secrets create %s --from-literal %s", name, value)).Assert(t,
152+
tcli.Success(),
153+
tcli.StderrEmpty(),
154+
)
155+
156+
c.Run(ctx, "coder secrets view "+name).Assert(t,
157+
tcli.Success(),
158+
tcli.StdoutMatches(regexp.QuoteMeta(value)),
159+
)
160+
161+
name, value = randString(8), randString(8)
162+
c.Run(ctx, fmt.Sprintf("echo %s > ~/secret.json", value)).Assert(t,
163+
tcli.Success(),
164+
)
165+
c.Run(ctx, fmt.Sprintf("coder secrets create %s --from-file ~/secret.json", name)).Assert(t,
166+
tcli.Success(),
167+
)
168+
c.Run(ctx, "coder secrets view "+name).Assert(t,
169+
tcli.Success(),
170+
tcli.StdoutMatches(regexp.QuoteMeta(value)),
171+
)
149172
}
150173

151174
func stdoutUnmarshalsJSON(target interface{}) tcli.Assertion {
@@ -159,7 +182,7 @@ func stdoutUnmarshalsJSON(target interface{}) tcli.Assertion {
159182
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
160183

161184
func randString(length int) string {
162-
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
185+
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
163186
b := make([]byte, length)
164187
for i := range b {
165188
b[i] = charset[seededRand.Intn(len(charset))]

cmd/coder/secrets.go

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package main
22

33
import (
44
"fmt"
5+
"io/ioutil"
56
"os"
67

78
"cdr.dev/coder-cli/internal/entclient"
89
"cdr.dev/coder-cli/internal/x/xtabwriter"
9-
"cdr.dev/coder-cli/internal/x/xvalidate"
10+
"github.com/manifoldco/promptui"
1011
"github.com/spf13/pflag"
1112
"golang.org/x/xerrors"
1213

@@ -111,45 +112,63 @@ func (cmd viewSecretsCmd) Run(fl *pflag.FlagSet) {
111112
}
112113

113114
type createSecretCmd struct {
114-
name, value, description string
115-
}
116-
117-
func (cmd *createSecretCmd) Validate() (e []error) {
118-
if cmd.name == "" {
119-
e = append(e, xerrors.New("--name is a required flag"))
120-
}
121-
if cmd.value == "" {
122-
e = append(e, xerrors.New("--value is a required flag"))
123-
}
124-
return e
115+
description string
116+
fromFile string
117+
fromLiteral string
125118
}
126119

127120
func (cmd *createSecretCmd) Spec() cli.CommandSpec {
128121
return cli.CommandSpec{
129122
Name: "create",
130-
Usage: `--name MYSQL_KEY --value 123456 --description "MySQL credential for database access"`,
123+
Usage: `[secret_name] [...flags]`,
131124
Desc: "insert a new secret",
132125
}
133126
}
134127

135128
func (cmd *createSecretCmd) Run(fl *pflag.FlagSet) {
136129
var (
137130
client = requireAuth()
131+
name = fl.Arg(0)
132+
value string
133+
err error
138134
)
139-
xvalidate.Validate(cmd)
135+
if name == "" {
136+
exitUsage(fl)
137+
}
138+
139+
if cmd.fromLiteral != "" {
140+
value = cmd.fromLiteral
141+
} else if cmd.fromFile != "" {
142+
contents, err := ioutil.ReadFile(cmd.fromFile)
143+
requireSuccess(err, "read file: %v", err)
144+
value = string(contents)
145+
} else {
146+
prompt := promptui.Prompt{
147+
Label: "Value",
148+
Mask: '*',
149+
Validate: func(s string) error {
150+
if len(s) < 1 {
151+
return xerrors.Errorf("a length > 0 is required")
152+
}
153+
return nil
154+
},
155+
}
156+
value, err = prompt.Run()
157+
requireSuccess(err, "prompt for value: %v", err)
158+
}
140159

141-
err := client.InsertSecret(entclient.InsertSecretReq{
142-
Name: cmd.name,
143-
Value: cmd.value,
160+
err = client.InsertSecret(entclient.InsertSecretReq{
161+
Name: name,
162+
Value: value,
144163
Description: cmd.description,
145164
})
146165
requireSuccess(err, "failed to insert secret: %v", err)
147166
}
148167

149168
func (cmd *createSecretCmd) RegisterFlags(fl *pflag.FlagSet) {
150-
fl.StringVar(&cmd.name, "name", "", "the name of the secret")
151-
fl.StringVar(&cmd.value, "value", "", "the value of the secret")
152-
fl.StringVar(&cmd.description, "description", "", "a description of the secret")
169+
fl.StringVar(&cmd.fromFile, "from-file", "", "specify a file from which to read the value of the secret")
170+
fl.StringVar(&cmd.fromLiteral, "from-literal", "", "specify the value of the secret")
171+
fl.StringVar(&cmd.description, "description", "", "specify a description of the secret")
153172
}
154173

155174
type deleteSecretsCmd struct{}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/gorilla/websocket v1.4.1
1010
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
1111
github.com/klauspost/compress v1.10.8 // indirect
12+
github.com/manifoldco/promptui v0.7.0
1213
github.com/mattn/go-colorable v0.1.6 // indirect
1314
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
1415
github.com/rjeczalik/notify v0.9.2

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ github.com/alecthomas/kong-hcl v0.1.8-0.20190615233001-b21fea9723c8/go.mod h1:MR
3333
github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897 h1:p9Sln00KOTlrYkxI1zYWl1QLnEqAqEARBEYa8FQnQcY=
3434
github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ=
3535
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
36+
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
37+
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
38+
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
39+
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
3640
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
3741
github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw=
3842
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
@@ -111,6 +115,8 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS
111115
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
112116
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
113117
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
118+
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU=
119+
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU=
114120
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
115121
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0=
116122
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
@@ -125,6 +131,10 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
125131
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
126132
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
127133
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
134+
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a h1:weJVJJRzAJBFRlAiJQROKQs8oC9vOxvm4rZmBBk0ONw=
135+
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
136+
github.com/manifoldco/promptui v0.7.0 h1:3l11YT8tm9MnwGFQ4kETwkzpAwY2Jt9lCrumCUW4+z4=
137+
github.com/manifoldco/promptui v0.7.0/go.mod h1:n4zTdgP0vr0S3w7/O/g98U+e0gwLScEXGwov2nIKuGQ=
128138
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
129139
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
130140
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
@@ -225,6 +235,7 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a h1:WXEvlFVvvGxCJLG6REjsT03i
225235
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
226236
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
227237
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
238+
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
228239
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
229240
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
230241
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

0 commit comments

Comments
 (0)