Skip to content

Commit

Permalink
Merge pull request #602 from sledigabel/master
Browse files Browse the repository at this point in the history
Adding option to write to different cred file.
  • Loading branch information
Mark Wolfe authored Feb 18, 2021
2 parents e3bbc9b + 6d035e6 commit 2147b33
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 18 deletions.
34 changes: 22 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,29 +160,36 @@ Commands:
login [<flags>]
Login to a SAML 2.0 IDP and convert the SAML assertion to an STS token.
-p, --profile=PROFILE The AWS profile to save the temporary credentials. (env: SAML2AWS_PROFILE)
-p, --profile=PROFILE The AWS profile to save the temporary credentials. (env: SAML2AWS_PROFILE)
--duo-mfa-option=DUO-MFA-OPTION
The MFA option you want to use to authenticate with
--client-id=CLIENT-ID OneLogin client id, used to generate API access token. (env: ONELOGIN_CLIENT_ID)
The MFA option you want to use to authenticate with
--client-id=CLIENT-ID OneLogin client id, used to generate API access token. (env: ONELOGIN_CLIENT_ID)
--client-secret=CLIENT-SECRET
OneLogin client secret, used to generate API access token. (env: ONELOGIN_CLIENT_SECRET)
--force Refresh credentials even if not expired.
OneLogin client secret, used to generate API access token. (env: ONELOGIN_CLIENT_SECRET)
--force Refresh credentials even if not expired.
--credential-process Enables AWS Credential Process support by outputting credentials to STDOUT in a JSON message.
--credentials-file=CREDENTIALS-FILE
The file that will cache the credentials retrieved from AWS. When not specified, will use the default AWS credentials file location. (env: SAML2AWS_CREDENTIALS_FILE)
exec [<flags>] [<command>...]
Exec the supplied command with env vars from STS token.
-p, --profile=PROFILE The AWS profile to save the temporary credentials. (env: SAML2AWS_PROFILE)
-p, --profile=PROFILE The AWS profile to save the temporary credentials. (env: SAML2AWS_PROFILE)
--exec-profile=EXEC-PROFILE
The AWS profile to utilize for command execution. Useful to allow the aws cli to perform secondary role assumption. (env: SAML2AWS_EXEC_PROFILE)
The AWS profile to utilize for command execution. Useful to allow the aws cli to perform secondary role assumption. (env: SAML2AWS_EXEC_PROFILE)
--credentials-file=CREDENTIALS-FILE
The file that will cache the credentials retrieved from AWS. When not specified, will use the default AWS credentials file location. (env: SAML2AWS_CREDENTIALS_FILE)
console [<flags>]
Console will open the aws console after logging in.
--exec-profile=EXEC-PROFILE
The AWS profile to utilize for console execution. (env: SAML2AWS_EXEC_PROFILE)
-p, --profile=PROFILE The AWS profile to save the temporary credentials. (env: SAML2AWS_PROFILE)
--force Refresh credentials even if not expired.
The AWS profile to utilize for console execution. (env: SAML2AWS_EXEC_PROFILE)
-p, --profile=PROFILE The AWS profile to save the temporary credentials. (env: SAML2AWS_PROFILE)
--force Refresh credentials even if not expired.
--link Present link to AWS console instead of opening browser
--credentials-file=CREDENTIALS-FILE
The file that will cache the credentials retrieved from AWS. When not specified, will use the default AWS credentials file location. (env: SAML2AWS_CREDENTIALS_FILE)
list-roles
List available role ARNs.
Expand All @@ -191,8 +198,10 @@ Commands:
script [<flags>]
Emit a script that will export environment variables.
-p, --profile=PROFILE The AWS profile to save the temporary credentials. (env: SAML2AWS_PROFILE)
--shell=bash Type of shell environment. Options include: bash, powershell, fish
-p, --profile=PROFILE The AWS profile to save the temporary credentials. (env: SAML2AWS_PROFILE)
--shell=bash Type of shell environment. Options include: bash, powershell, fish
--credentials-file=CREDENTIALS-FILE
The file that will cache the credentials retrieved from AWS. When not specified, will use the default AWS credentials file location. (env: SAML2AWS_CREDENTIALS_FILE)
```
Expand Down Expand Up @@ -609,6 +618,7 @@ DUMP_CONTENT=true saml2aws login --verbose
[Credential Process](https://github.com/awslabs/awsprocesscreds) is a convenient way of interfacing credential providers with the AWS Cli.

You can use `saml2aws` as a credential provider by simply configuring it and then adding a profile to the AWS configuration. `saml2aws` has a flag `--credential-process` generating an output with the right JSON format, as well as a flag `--quiet` that will block the logging from being displayed.
The AWS credential file (typically ~/.aws/credentials) has precedence over the credential_process provider. That means that if credentials are present in the file, the credential process will not trigger. To counter that you can override the aws credential location of `saml2aws` to another file using `--credential-file` or specifying it during `configure`.

An example of the aws configuration (`~/.aws/config`):

Expand Down
2 changes: 1 addition & 1 deletion cmd/saml2aws/commands/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Console(consoleFlags *flags.ConsoleFlags) error {
return errors.Wrap(err, "error building login details")
}

sharedCreds := awsconfig.NewSharedCredentials(account.Profile)
sharedCreds := awsconfig.NewSharedCredentials(account.Profile, account.CredentialsFile)

// this checks if the credentials file has been created yet
// can only really be triggered if saml2aws exec is run on a new
Expand Down
2 changes: 1 addition & 1 deletion cmd/saml2aws/commands/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Exec(execFlags *flags.LoginExecFlags, cmdline []string) error {
return errors.Wrap(err, "error building login details")
}

sharedCreds := awsconfig.NewSharedCredentials(account.Profile)
sharedCreds := awsconfig.NewSharedCredentials(account.Profile, account.CredentialsFile)

// this checks if the credentials file has been created yet
// can only really be triggered if saml2aws exec is run on a new
Expand Down
2 changes: 1 addition & 1 deletion cmd/saml2aws/commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Login(loginFlags *flags.LoginExecFlags) error {
return errors.Wrap(err, "error building login details")
}

sharedCreds := awsconfig.NewSharedCredentials(account.Profile)
sharedCreds := awsconfig.NewSharedCredentials(account.Profile, account.CredentialsFile)

logger.Debug("check if Creds Exist")

Expand Down
2 changes: 1 addition & 1 deletion cmd/saml2aws/commands/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Script(execFlags *flags.LoginExecFlags, shell string) error {
return errors.Wrap(err, "error building login details")
}

sharedCreds := awsconfig.NewSharedCredentials(account.Profile)
sharedCreds := awsconfig.NewSharedCredentials(account.Profile, account.CredentialsFile)

// this checks if the credentials file has been created yet
// can only really be triggered if saml2aws exec is run on a new
Expand Down
5 changes: 5 additions & 0 deletions cmd/saml2aws/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func main() {
cmdConfigure.Flag("subdomain", "OneLogin subdomain of your company account. (env: ONELOGIN_SUBDOMAIN)").Envar("ONELOGIN_SUBDOMAIN").StringVar(&commonFlags.Subdomain)
cmdConfigure.Flag("profile", "The AWS profile to save the temporary credentials. (env: SAML2AWS_PROFILE)").Envar("SAML2AWS_PROFILE").Short('p').StringVar(&commonFlags.Profile)
cmdConfigure.Flag("resource-id", "F5APM SAML resource ID of your company account. (env: SAML2AWS_F5APM_RESOURCE_ID)").Envar("SAML2AWS_F5APM_RESOURCE_ID").StringVar(&commonFlags.ResourceID)
cmdConfigure.Flag("credentials-file", "The file that will cache the credentials retrieved from AWS. When not specified, will use the default AWS credentials file location. (env: SAML2AWS_CREDENTIALS_FILE)").Envar("SAML2AWS_CREDENTIALS_FILE").StringVar(&commonFlags.CredentialsFile)
configFlags := commonFlags

// `login` command and settings
Expand All @@ -103,13 +104,15 @@ func main() {
cmdLogin.Flag("client-secret", "OneLogin client secret, used to generate API access token. (env: ONELOGIN_CLIENT_SECRET)").Envar("ONELOGIN_CLIENT_SECRET").StringVar(&commonFlags.ClientSecret)
cmdLogin.Flag("force", "Refresh credentials even if not expired.").BoolVar(&loginFlags.Force)
cmdLogin.Flag("credential-process", "Enables AWS Credential Process support by outputting credentials to STDOUT in a JSON message.").BoolVar(&loginFlags.CredentialProcess)
cmdLogin.Flag("credentials-file", "The file that will cache the credentials retrieved from AWS. When not specified, will use the default AWS credentials file location. (env: SAML2AWS_CREDENTIALS_FILE)").Envar("SAML2AWS_CREDENTIALS_FILE").StringVar(&commonFlags.CredentialsFile)

// `exec` command and settings
cmdExec := app.Command("exec", "Exec the supplied command with env vars from STS token.")
execFlags := new(flags.LoginExecFlags)
execFlags.CommonFlags = commonFlags
cmdExec.Flag("profile", "The AWS profile to save the temporary credentials. (env: SAML2AWS_PROFILE)").Envar("SAML2AWS_PROFILE").Short('p').StringVar(&commonFlags.Profile)
cmdExec.Flag("exec-profile", "The AWS profile to utilize for command execution. Useful to allow the aws cli to perform secondary role assumption. (env: SAML2AWS_EXEC_PROFILE)").Envar("SAML2AWS_EXEC_PROFILE").StringVar(&execFlags.ExecProfile)
cmdExec.Flag("credentials-file", "The file that will cache the credentials retrieved from AWS. When not specified, will use the default AWS credentials file location. (env: SAML2AWS_CREDENTIALS_FILE)").Envar("SAML2AWS_CREDENTIALS_FILE").StringVar(&commonFlags.CredentialsFile)
cmdLine := buildCmdList(cmdExec.Arg("command", "The command to execute."))

// `console` command and settings
Expand All @@ -121,6 +124,7 @@ func main() {
cmdConsole.Flag("profile", "The AWS profile to save the temporary credentials. (env: SAML2AWS_PROFILE)").Envar("SAML2AWS_PROFILE").Short('p').StringVar(&commonFlags.Profile)
cmdConsole.Flag("force", "Refresh credentials even if not expired.").BoolVar(&consoleFlags.LoginExecFlags.Force)
cmdConsole.Flag("link", "Present link to AWS console instead of opening browser").BoolVar(&consoleFlags.Link)
cmdConsole.Flag("credentials-file", "The file that will cache the credentials retrieved from AWS. When not specified, will use the default AWS credentials file location. (env: SAML2AWS_CREDENTIALS_FILE)").Envar("SAML2AWS_CREDENTIALS_FILE").StringVar(&commonFlags.CredentialsFile)

// `list` command and settings
cmdListRoles := app.Command("list-roles", "List available role ARNs.")
Expand All @@ -132,6 +136,7 @@ func main() {
scriptFlags := new(flags.LoginExecFlags)
scriptFlags.CommonFlags = commonFlags
cmdScript.Flag("profile", "The AWS profile to save the temporary credentials. (env: SAML2AWS_PROFILE)").Envar("SAML2AWS_PROFILE").Short('p').StringVar(&commonFlags.Profile)
cmdScript.Flag("credentials-file", "The file that will cache the credentials retrieved from AWS. When not specified, will use the default AWS credentials file location. (env: SAML2AWS_CREDENTIALS_FILE)").Envar("SAML2AWS_CREDENTIALS_FILE").StringVar(&commonFlags.CredentialsFile)
var shell string
cmdScript.
Flag("shell", "Type of shell environment. Options include: bash, powershell, fish").
Expand Down
5 changes: 3 additions & 2 deletions pkg/awsconfig/awsconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ type CredentialsProvider struct {
}

// NewSharedCredentials helper to create the credentials provider
func NewSharedCredentials(profile string) *CredentialsProvider {
func NewSharedCredentials(profile string, filename string) *CredentialsProvider {
return &CredentialsProvider{
Profile: profile,
Filename: filename,
Profile: profile,
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type IDPAccount struct {
Region string `ini:"region"`
HttpAttemptsCount string `ini:"http_attempts_count"`
HttpRetryDelay string `ini:"http_retry_delay"`
CredentialsFile string `ini:"credentials_file"`
}

func (ia IDPAccount) String() string {
Expand Down
4 changes: 4 additions & 0 deletions pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type CommonFlags struct {
ResourceID string
DisableKeychain bool
Region string
CredentialsFile string
}

// LoginExecFlags flags for the Login / Exec commands
Expand Down Expand Up @@ -94,4 +95,7 @@ func ApplyFlagOverrides(commonFlags *CommonFlags, account *cfg.IDPAccount) {
if commonFlags.Region != "" {
account.Region = commonFlags.Region
}
if commonFlags.CredentialsFile != "" {
account.CredentialsFile = commonFlags.CredentialsFile
}
}

0 comments on commit 2147b33

Please sign in to comment.