-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <afiune@lacework.net>
- Loading branch information
Showing
94 changed files
with
13,293 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// | ||
// Author:: Salim Afiune Maya (<afiune@lacework.net>) | ||
// Copyright:: Copyright 2020, Lacework Inc. | ||
// License:: Apache License, Version 2.0 | ||
// | ||
// 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 ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"path" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/manifoldco/promptui" | ||
homedir "github.com/mitchellh/go-homedir" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// the representation of the ~/.lacework.toml | ||
type config struct { | ||
Account string `toml:"account"` | ||
ApiKey string `toml:"api_key"` | ||
ApiSecret string `toml:"api_secret"` | ||
} | ||
|
||
var ( | ||
// configureCmd represents the configure command | ||
configureCmd = &cobra.Command{ | ||
Use: "configure", | ||
Short: "Set up your Lacework cli", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var ( | ||
promptAccount = promptui.Prompt{ | ||
Label: "Account", | ||
Default: cli.Account, | ||
Validate: func(input string) error { | ||
if len(input) == 0 { | ||
return errors.New( | ||
"The account subdomain of URL is required. (i.e. <ACCOUNT>.lacework.net)") | ||
} | ||
return nil | ||
}, | ||
} | ||
promptApiKey = promptui.Prompt{ | ||
Label: "Access Key ID", | ||
Default: cli.KeyID, | ||
Validate: func(input string) error { | ||
if len(input) < 55 { | ||
return errors.New( | ||
"The API access key id must have more than 55 characters.") | ||
} | ||
return nil | ||
}, | ||
} | ||
promptApiSecret = promptui.Prompt{ | ||
Label: "Secret Access Key", | ||
Default: cli.Secret, | ||
Validate: func(input string) error { | ||
if len(input) < 30 { | ||
return errors.New( | ||
"The API secret access key must have more than 30 characters.") | ||
} | ||
return nil | ||
}, | ||
} | ||
) | ||
account, err := promptAccount.Run() | ||
if err != nil { | ||
return err | ||
} | ||
apiKey, err := promptApiKey.Run() | ||
if err != nil { | ||
return err | ||
} | ||
apiSecret, err := promptApiSecret.Run() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var ( | ||
c = config{account, apiKey, apiSecret} | ||
buf = new(bytes.Buffer) | ||
confPath = viper.ConfigFileUsed() | ||
) | ||
|
||
if err := toml.NewEncoder(buf).Encode(c); err != nil { | ||
return err | ||
} | ||
|
||
if confPath == "" { | ||
home, err := homedir.Dir() | ||
if err != nil { | ||
return err | ||
} | ||
confPath = path.Join(home, ".lacework.toml") | ||
} | ||
|
||
err = ioutil.WriteFile(confPath, buf.Bytes(), 0600) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("\nYou are all set!") | ||
return nil | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(configureCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.