Skip to content

Commit f334fda

Browse files
authored
feat: add configure command (#47)
Adding a new `configure` command to set up the Lacework CLI: ``` $ lacework configure ✔ Account: example ✔ Access Key ID: EXAMPLE_12FBEB06B01249B634D2129655B8652D0EBC2DD141B1234 ✔ Secret Access Key: _186bb1dcbde222d77ccbbb25c516al09 You are all set! ``` Closes #35 Signed-off-by: Salim Afiune Maya <[email protected]>
1 parent 7fe0719 commit f334fda

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+13293
-1
lines changed

cli/cmd/configure.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
//
2+
// Author:: Salim Afiune Maya (<[email protected]>)
3+
// Copyright:: Copyright 2020, Lacework Inc.
4+
// License:: Apache License, Version 2.0
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
19+
package cmd
20+
21+
import (
22+
"bytes"
23+
"fmt"
24+
"io/ioutil"
25+
"path"
26+
27+
"github.com/BurntSushi/toml"
28+
"github.com/manifoldco/promptui"
29+
homedir "github.com/mitchellh/go-homedir"
30+
"github.com/pkg/errors"
31+
"github.com/spf13/cobra"
32+
"github.com/spf13/viper"
33+
)
34+
35+
// the representation of the ~/.lacework.toml
36+
type config struct {
37+
Account string `toml:"account"`
38+
ApiKey string `toml:"api_key"`
39+
ApiSecret string `toml:"api_secret"`
40+
}
41+
42+
var (
43+
// configureCmd represents the configure command
44+
configureCmd = &cobra.Command{
45+
Use: "configure",
46+
Short: "Set up your Lacework cli",
47+
RunE: func(cmd *cobra.Command, args []string) error {
48+
var (
49+
promptAccount = promptui.Prompt{
50+
Label: "Account",
51+
Default: cli.Account,
52+
Validate: func(input string) error {
53+
if len(input) == 0 {
54+
return errors.New(
55+
"The account subdomain of URL is required. (i.e. <ACCOUNT>.lacework.net)")
56+
}
57+
return nil
58+
},
59+
}
60+
promptApiKey = promptui.Prompt{
61+
Label: "Access Key ID",
62+
Default: cli.KeyID,
63+
Validate: func(input string) error {
64+
if len(input) < 55 {
65+
return errors.New(
66+
"The API access key id must have more than 55 characters.")
67+
}
68+
return nil
69+
},
70+
}
71+
promptApiSecret = promptui.Prompt{
72+
Label: "Secret Access Key",
73+
Default: cli.Secret,
74+
Validate: func(input string) error {
75+
if len(input) < 30 {
76+
return errors.New(
77+
"The API secret access key must have more than 30 characters.")
78+
}
79+
return nil
80+
},
81+
}
82+
)
83+
account, err := promptAccount.Run()
84+
if err != nil {
85+
return err
86+
}
87+
apiKey, err := promptApiKey.Run()
88+
if err != nil {
89+
return err
90+
}
91+
apiSecret, err := promptApiSecret.Run()
92+
if err != nil {
93+
return err
94+
}
95+
96+
var (
97+
c = config{account, apiKey, apiSecret}
98+
buf = new(bytes.Buffer)
99+
confPath = viper.ConfigFileUsed()
100+
)
101+
102+
if err := toml.NewEncoder(buf).Encode(c); err != nil {
103+
return err
104+
}
105+
106+
if confPath == "" {
107+
home, err := homedir.Dir()
108+
if err != nil {
109+
return err
110+
}
111+
confPath = path.Join(home, ".lacework.toml")
112+
}
113+
114+
err = ioutil.WriteFile(confPath, buf.Bytes(), 0600)
115+
if err != nil {
116+
return err
117+
}
118+
fmt.Println("\nYou are all set!")
119+
return nil
120+
},
121+
}
122+
)
123+
124+
func init() {
125+
rootCmd.AddCommand(configureCmd)
126+
}

cli/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func init() {
8383
"access key id",
8484
)
8585
rootCmd.PersistentFlags().StringP("api_secret", "s", "",
86-
"access secret key",
86+
"secret access key",
8787
)
8888
rootCmd.PersistentFlags().StringP("account", "a", "",
8989
"account subdomain of URL (i.e. <ACCOUNT>.lacework.net)",

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ module github.com/lacework/go-sdk
33
go 1.14
44

55
require (
6+
github.com/BurntSushi/toml v0.3.1
67
github.com/fatih/color v1.9.0 // indirect
78
github.com/fsnotify/fsnotify v1.4.9 // indirect
89
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e
10+
github.com/manifoldco/promptui v0.7.0
911
github.com/mattn/go-colorable v0.1.6 // indirect
1012
github.com/mitchellh/go-homedir v1.1.0
1113
github.com/mitchellh/mapstructure v1.2.2 // indirect

go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5
88
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
99
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
1010
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
11+
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
12+
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
13+
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
14+
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
15+
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
16+
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
1117
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
1218
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
1319
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
@@ -56,6 +62,8 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
5662
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
5763
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
5864
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
65+
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU=
66+
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU=
5967
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
6068
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
6169
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
@@ -66,13 +74,19 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
6674
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
6775
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
6876
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
77+
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a h1:weJVJJRzAJBFRlAiJQROKQs8oC9vOxvm4rZmBBk0ONw=
78+
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
6979
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
7080
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
7181
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
7282
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
83+
github.com/manifoldco/promptui v0.7.0 h1:3l11YT8tm9MnwGFQ4kETwkzpAwY2Jt9lCrumCUW4+z4=
84+
github.com/manifoldco/promptui v0.7.0/go.mod h1:n4zTdgP0vr0S3w7/O/g98U+e0gwLScEXGwov2nIKuGQ=
85+
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
7386
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
7487
github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE=
7588
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
89+
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
7690
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
7791
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
7892
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
@@ -189,6 +203,7 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h
189203
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
190204
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
191205
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
206+
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
192207
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
193208
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
194209
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

vendor/github.com/BurntSushi/toml/.gitignore

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/BurntSushi/toml/.travis.yml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/BurntSushi/toml/COMPATIBLE

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/BurntSushi/toml/COPYING

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/BurntSushi/toml/Makefile

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)