|
| 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 | + "fmt" |
| 23 | + |
| 24 | + prettyjson "github.com/hokaccha/go-prettyjson" |
| 25 | + "github.com/pkg/errors" |
| 26 | + "github.com/spf13/viper" |
| 27 | + "go.uber.org/zap" |
| 28 | +) |
| 29 | + |
| 30 | +// cliState holds the state of the entire Lacework CLI |
| 31 | +type cliState struct { |
| 32 | + Profile string |
| 33 | + Account string |
| 34 | + KeyID string |
| 35 | + Secret string |
| 36 | + Token string |
| 37 | + LogLevel string |
| 38 | + |
| 39 | + JsonF *prettyjson.Formatter |
| 40 | + Log *zap.SugaredLogger |
| 41 | + |
| 42 | + profileDetails map[string]interface{} |
| 43 | +} |
| 44 | + |
| 45 | +// NewDefaultState creates a new cliState with some defaults |
| 46 | +func NewDefaultState() cliState { |
| 47 | + return cliState{ |
| 48 | + Profile: "default", |
| 49 | + JsonF: prettyjson.NewFormatter(), |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// SetProfile sets the provided profile into the cliState and loads the entire |
| 54 | +// state of the Lacework CLI by calling 'LoadState()' |
| 55 | +func (c *cliState) SetProfile(profile string) error { |
| 56 | + if profile == "" { |
| 57 | + return errors.New("Specify a profile.") |
| 58 | + } |
| 59 | + |
| 60 | + c.Profile = profile |
| 61 | + c.Log.Debugw("custom profile", "profile", profile) |
| 62 | + return c.LoadState() |
| 63 | +} |
| 64 | + |
| 65 | +// LoadState loads the state of the cli in the following order, loads the |
| 66 | +// configured profile out from the viper loaded config, if the profile is |
| 67 | +// set to the default and it is not found, we assume that the user is running |
| 68 | +// the CLI with parameters or environment variables, so we proceed to load |
| 69 | +// those. Though, if the profile is NOT the default, we error out with some |
| 70 | +// breadcrumbs to help the user configure the CLI. After loading the profile, |
| 71 | +// this function verifies parameters and env variables coming from viper |
| 72 | +func (c *cliState) LoadState() error { |
| 73 | + c.profileDetails = viper.GetStringMap(c.Profile) |
| 74 | + if len(c.profileDetails) == 0 { |
| 75 | + if c.Profile != "default" { |
| 76 | + return fmt.Errorf( |
| 77 | + "The profile '%s' could not be found.\n\nTry running 'lacework configure --profile %s'.", |
| 78 | + c.Profile, c.Profile, |
| 79 | + ) |
| 80 | + } else { |
| 81 | + c.Log.Debugw("unable to load state from config") |
| 82 | + c.loadStateFromViper() |
| 83 | + return nil |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + c.KeyID = c.extractValueString("api_key") |
| 88 | + c.Secret = c.extractValueString("api_secret") |
| 89 | + c.Account = c.extractValueString("account") |
| 90 | + |
| 91 | + c.Log.Debugw("state loaded", |
| 92 | + "profile", c.Profile, |
| 93 | + "account", c.Account, |
| 94 | + "api_key", c.KeyID, |
| 95 | + "api_secret", c.Secret, |
| 96 | + ) |
| 97 | + |
| 98 | + c.loadStateFromViper() |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +// VerifySettings checks if the CLI state has the neccessary settings to run, |
| 103 | +// if not, it throws an error with breadcrumbs to help the user configure the CLI |
| 104 | +func (c *cliState) VerifySettings() error { |
| 105 | + if c.Profile == "" || |
| 106 | + c.Account == "" || |
| 107 | + c.Secret == "" || |
| 108 | + c.KeyID == "" { |
| 109 | + return fmt.Errorf( |
| 110 | + "there is one or more settings missing.\n\nTry running 'lacework configure'.", |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +// loadStateFromViper loads parameters and environment variables |
| 118 | +// coming from viper into the CLI state |
| 119 | +func (c *cliState) loadStateFromViper() { |
| 120 | + if v := viper.GetString("api_key"); v != "" { |
| 121 | + c.KeyID = v |
| 122 | + c.Log.Debugw("state updated", "api_key", c.KeyID) |
| 123 | + } |
| 124 | + |
| 125 | + if v := viper.GetString("api_secret"); v != "" { |
| 126 | + c.Secret = v |
| 127 | + c.Log.Debugw("state updated", "api_secret", c.Secret) |
| 128 | + } |
| 129 | + |
| 130 | + if v := viper.GetString("account"); v != "" { |
| 131 | + c.Account = v |
| 132 | + c.Log.Debugw("state updated", "account", c.Account) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func (c *cliState) extractValueString(key string) string { |
| 137 | + if val, ok := c.profileDetails[key]; ok { |
| 138 | + if str, ok := val.(string); ok { |
| 139 | + return str |
| 140 | + } |
| 141 | + c.Log.Warnw("config value type mismatch", |
| 142 | + "expected_type", "string", |
| 143 | + "file", viper.ConfigFileUsed(), |
| 144 | + "profile", c.Profile, |
| 145 | + "key", key, |
| 146 | + "value", val, |
| 147 | + ) |
| 148 | + return "" |
| 149 | + } |
| 150 | + c.Log.Warnw("unable to find key from config", |
| 151 | + "file", viper.ConfigFileUsed(), |
| 152 | + "profile", c.Profile, |
| 153 | + "key", key, |
| 154 | + ) |
| 155 | + return "" |
| 156 | +} |
0 commit comments