Skip to content

Commit

Permalink
feat(cli): preconfigure using key JSON file (WebUI)
Browse files Browse the repository at this point in the history
This change is adding an extra flag called `--json_file`, short that `-j`
that will allow users load a generated API key JSON file from the WebUI.

Example: On this example, we create a set of API keys by logging in into
the Lacework WebUI, then download the keys and pass them to the Lacework
CLI to preconfigure it.
```
$ lacework configure --json_file /path/to/EXAMPLE_D3EFA214SD12DFSGRD3EFA214SD12DFSGR.json
```

The result of the above command is to only provide the "Account"
information, the API access key and secret will be preloaded.

Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed May 7, 2020
1 parent b195ed0 commit 80c48e7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
10 changes: 6 additions & 4 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ CLI installation. The command prompts you for three things:
* `api_key`: API Access Key
* `api_secret`: API Access Secret

>To create a set of API keys, log in to your Lacework account and navigate to
>Settings -> API Keys, then click + Create New. Enter a name for the key and
>an optional description and click Save. To get the secret key, download the
>generated API key file and open it in an editor.
>To create a set of API keys, log in to your Lacework account via WebUI and
>navigate to Settings > API Keys and click + Create New. Enter a name for
>the key and an optional description, then click Save. To get the secret key,
>download the generated API key file.
_**NOTE:** Use the argument `--json_file` to preload the downloaded API key file._

The following example shows sample values. Replace them with your own.

Expand Down
46 changes: 46 additions & 0 deletions cli/cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ package cmd

import (
"bytes"
"encoding/json"
"io/ioutil"
"path"

"github.com/AlecAivazis/survey/v2"
"github.com/BurntSushi/toml"
homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -51,7 +53,17 @@ type credsDetails struct {
ApiSecret string `toml:"api_secret" json:"api_secret" survey:"api_secret"`
}

// apiKeyDetails represents the details of an API key, we use this struct
// internally to unmarshal the JSON file provided by the Lacework WebUI
type apiKeyDetails struct {
KeyID string `json:"keyId"`
Secret string `json:"secret"`
}

var (
// configureJsonFile is the API key file downloaded form the Lacework WebUI
configureJsonFile string

// configureCmd represents the configure command
configureCmd = &cobra.Command{
Use: "configure",
Expand All @@ -61,6 +73,13 @@ var (
Configure settings that the Lacework CLI uses to interact with the Lacework
platform. These include your Lacework account, API access key and secret.
To create a set of API keys, log in to your Lacework account via WebUI and
navigate to Settings > API Keys and click + Create New. Enter a name for
the key and an optional description, then click Save. To get the secret key,
download the generated API key file.
Use the argument --json_file to preload the downloaded API key file.
If this command is run with no arguments, the Lacework CLI will store all
settings under the default profile. The information in the default profile
is used any time you run a Lacework CLI command that doesn't explicitly
Expand All @@ -77,10 +96,24 @@ Lacework CLI will create it for you.`,

func init() {
rootCmd.AddCommand(configureCmd)

configureCmd.PersistentFlags().StringVarP(&configureJsonFile,
"json_file", "j", "", "loads the generated API key JSON file from the WebUI",
)
}

func promptConfigureSetup() error {
cli.Log.Debugw("configuring cli", "profile", cli.Profile)

if len(configureJsonFile) != 0 {
auth, err := loadKeysFromJsonFile(configureJsonFile)
if err != nil {
return errors.Wrap(err, "unable to load keys from the provided json file")
}
cli.KeyID = auth.KeyID
cli.Secret = auth.Secret
}

questions := []*survey.Question{
{
Name: "account",
Expand Down Expand Up @@ -169,3 +202,16 @@ func promptConfigureSetup() error {
cli.OutputHuman("\nYou are all set!\n")
return nil
}

func loadKeysFromJsonFile(file string) (*apiKeyDetails, error) {
cli.Log.Debugw("loading API key JSON file", "path", file)
jsonData, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}

cli.Log.Debugw("keys from file", "raw", string(jsonData))
var auth apiKeyDetails
err = json.Unmarshal(jsonData, &auth)
return &auth, err
}

0 comments on commit 80c48e7

Please sign in to comment.