Skip to content

Commit

Permalink
Use "default" profile for file credentials by default (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
castrapel authored Nov 20, 2020
1 parent 495231b commit 942766a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 25 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ Then run `aws sts get-caller-identity` to confirm that your credentials work pro

### Credentials file

Write retrieved credentials to an AWS credentials file (`~/.aws/credentials` by default with the profile name `consoleme`).
Write retrieved credentials to an AWS credentials file (`~/.aws/credentials` by default).
Weep will prompt for confirmation before overwriting existing credentials in the file.

```bash
weep file exampleRole
Expand All @@ -152,11 +153,13 @@ weep file exampleRole
weep file stagingRole --profile staging
weep file prodRole --profile prod

# don't prompt before overwriting existing creds
weep file prodRole --profile prod -f

# or you can save it to a different place
weep file exampleRole -o /tmp/credentials
```

Weep will do its best to preserve existing credentials in the file (but it will overwrite a conflicting profile name, so be careful!).

### Credentials Process
The AWS CLI can source credentials from weep using the `credential_process` configuration which can be defined for a
Expand Down
22 changes: 21 additions & 1 deletion cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import (
func init() {
fileCmd.PersistentFlags().BoolVarP(&noIpRestrict, "no-ip", "n", false, "remove IP restrictions")
fileCmd.PersistentFlags().StringVarP(&destination, "output", "o", getDefaultCredentialsFile(), "output file for credentials")
fileCmd.PersistentFlags().StringVarP(&profileName, "profile", "p", "consoleme", "profile name")
fileCmd.PersistentFlags().StringVarP(&profileName, "profile", "p", "default", "profile name")
fileCmd.PersistentFlags().BoolVarP(&force, "force", "f", false, "overwrite existing profile without prompting")
rootCmd.AddCommand(fileCmd)
}

Expand Down Expand Up @@ -78,6 +79,17 @@ func getDefaultAwsConfigFile() string {
return path.Join(home, ".aws", "config")
}

func shouldOverwriteCredentials() bool {
if force {
return true
}
userForce, err := util.PromptBool(fmt.Sprintf("Overwrite %s profile?", profileName))
if err != nil {
return false
}
return userForce
}

func writeCredentialsFile(credentials consoleme.AwsCredentials) error {
var credentialsINI *ini.File
var err error
Expand All @@ -95,6 +107,14 @@ func writeCredentialsFile(credentials consoleme.AwsCredentials) error {
credentialsINI = ini.Empty()
}

if _, err := credentialsINI.GetSection(profileName); err == nil {
// section already exists, should we overwrite?
if !shouldOverwriteCredentials() {
// user says no, so we'll just bail out
return fmt.Errorf("not overwriting %s profile", profileName)
}
}

credentialsINI.Section(profileName).Key("aws_access_key_id").SetValue(credentials.AccessKeyId)
credentialsINI.Section(profileName).Key("aws_secret_access_key").SetValue(credentials.SecretAccessKey)
credentialsINI.Section(profileName).Key("aws_session_token").SetValue(credentials.SessionToken)
Expand Down
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"runtime"
"strings"

"github.com/netflix/weep/util"

"github.com/mattn/go-isatty"

"github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -82,7 +84,7 @@ func initConfig() {
if _, ok := err.(viper.ConfigFileNotFoundError); ok && config.EmbeddedConfigFile != "" {
log.Debugf("no config file found, trying to use embedded config")
} else if isatty.IsTerminal(os.Stdout.Fd()) {
err = config.FirstRunPrompt()
err = util.FirstRunPrompt()
if err != nil {
log.Fatalf("config bootstrap failed: %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
profileName string
destination string
destinationConfig string
force bool
noIpRestrict bool
metadataRegion string
metadataListenAddr string
Expand Down
10 changes: 10 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@

package config

import "github.com/spf13/viper"

func init() {
// Set default configuration values here
viper.SetDefault("mtls_settings.old_cert_message", "mTLS certificate is too old, please refresh mtls certificate")
viper.SetDefault("server.http_timeout", 20)
viper.SetDefault("server.metadata_port", 9090)
viper.SetDefault("server.ecs_credential_provider_port", 9091)
}

var (
Config WeepConfig
)
Expand Down
33 changes: 12 additions & 21 deletions config/bootstrap.go → util/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package config
package util

import (
"errors"
Expand All @@ -24,18 +24,9 @@ import (

"github.com/manifoldco/promptui"
"github.com/mitchellh/go-homedir"
"github.com/netflix/weep/util"
"github.com/spf13/viper"
)

func init() {
// Set default configuration values here
viper.SetDefault("mtls_settings.old_cert_message", "mTLS certificate is too old, please refresh mtls certificate")
viper.SetDefault("server.http_timeout", 20)
viper.SetDefault("server.metadata_port", 9090)
viper.SetDefault("server.ecs_credential_provider_port", 9091)
}

// FirstRunPrompt gets user input to bootstrap a bare-minimum configuration.
func FirstRunPrompt() error {
fmt.Println("Welcome to weep, the ConsoleMe CLI!")
Expand All @@ -55,31 +46,31 @@ func FirstRunPrompt() error {
viper.Set("authentication_method", authMethod)

if authMethod == "mtls" {
cert, err := promptFilePath("mTLS certificate path", "")
cert, err := PromptFilePath("mTLS certificate path", "")
if err != nil {
return err
}
viper.Set("mtls_settings.cert", cert)

key, err := promptFilePath("mTLS key path", "")
key, err := PromptFilePath("mTLS key path", "")
if err != nil {
return err
}
viper.Set("mtls_settings.key", key)

ca, err := promptFilePath("mTLS CA bundle path", "")
ca, err := PromptFilePath("mTLS CA bundle path", "")
if err != nil {
return err
}
viper.Set("mtls_settings.cafile", ca)

insecure, err := promptBool("Skip validation of mTLS hostname?")
insecure, err := PromptBool("Skip validation of mTLS hostname?")
if err != nil {
return err
}
viper.Set("mtls_settings.insecure", insecure)
} else if authMethod == "challenge" {
challengeUser, err := promptString("ConsoleMe username")
challengeUser, err := PromptString("ConsoleMe username")
if err != nil {
return err
}
Expand All @@ -91,7 +82,7 @@ func FirstRunPrompt() error {
return err
}
defaultConfig := path.Join(home, ".weep.yaml")
saveLocation, err := promptFilePathNoValidate("Config destination", defaultConfig)
saveLocation, err := PromptFilePathNoValidate("Config destination", defaultConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -140,9 +131,9 @@ func promptAuthMethod() (string, error) {
return result, nil
}

func promptFilePath(label, defaultValue string) (string, error) {
func PromptFilePath(label, defaultValue string) (string, error) {
validateFile := func(input string) error {
if util.FileExists(input) {
if FileExists(input) {
return nil
} else {
return fmt.Errorf("file not found: %s", input)
Expand All @@ -163,7 +154,7 @@ func promptFilePath(label, defaultValue string) (string, error) {
return result, nil
}

func promptFilePathNoValidate(label, defaultValue string) (string, error) {
func PromptFilePathNoValidate(label, defaultValue string) (string, error) {
prompt := promptui.Prompt{
Label: label,
Default: defaultValue,
Expand All @@ -178,7 +169,7 @@ func promptFilePathNoValidate(label, defaultValue string) (string, error) {
return result, nil
}

func promptBool(label string) (bool, error) {
func PromptBool(label string) (bool, error) {
prompt := promptui.Select{
Label: label,
Items: []string{"true", "false"},
Expand All @@ -193,7 +184,7 @@ func promptBool(label string) (bool, error) {
return index == 0, nil
}

func promptString(label string) (string, error) {
func PromptString(label string) (string, error) {
prompt := promptui.Prompt{
Label: label,
}
Expand Down

0 comments on commit 942766a

Please sign in to comment.