Skip to content
This repository has been archived by the owner on May 18, 2021. It is now read-only.

feat: allow override of role arn to assume #219

Merged
merged 1 commit into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
var (
sessionTTL time.Duration
assumeRoleTTL time.Duration
assumeRoleARN string
)

func mustListProfiles() lib.Profiles {
Expand All @@ -44,6 +45,7 @@ func init() {
RootCmd.AddCommand(execCmd)
execCmd.Flags().DurationVarP(&sessionTTL, "session-ttl", "t", time.Hour, "Expiration time for okta role session")
execCmd.Flags().DurationVarP(&assumeRoleTTL, "assume-role-ttl", "a", time.Hour, "Expiration time for assumed role")
execCmd.Flags().StringVarP(&assumeRoleARN, "assume-role-arn", "r", "", "Role arn to assume, overrides arn in profile")
}

func loadDurationFlagFromEnv(cmd *cobra.Command, flagName string, envVar string, val *time.Duration) error {
Expand All @@ -66,6 +68,21 @@ func loadDurationFlagFromEnv(cmd *cobra.Command, flagName string, envVar string,
return nil
}

func loadStringFlagFromEnv(cmd *cobra.Command, flagName string, envVar string, val *string) error {
if cmd.Flags().Lookup(flagName).Changed {
return nil
}

fromEnv, ok := os.LookupEnv(envVar)
if !ok {
return nil
}

cmd.Flags().Lookup(flagName).Changed = true
*val = fromEnv
return nil
}

func updateDurationFromConfigProfile(profiles lib.Profiles, profile string, val *time.Duration) error {
fromProfile, _, err := profiles.GetValue(profile, "assume_role_ttl")
if err != nil {
Expand All @@ -89,6 +106,9 @@ func execPre(cmd *cobra.Command, args []string) {
if err := loadDurationFlagFromEnv(cmd, "assume-role-ttl", "AWS_ASSUME_ROLE_TTL", &assumeRoleTTL); err != nil {
fmt.Fprintln(os.Stderr, "warning: failed to parse duration from AWS_ASSUME_ROLE_TTL")
}
if err := loadStringFlagFromEnv(cmd, "assume-role-arn", "AWS_ASSUME_ROLE_ARN", &assumeRoleARN); err != nil {
fmt.Fprintln(os.Stderr, "warning: failed to parse duration from AWS_ASSUME_ROLE_ARN")
}
}

func execRun(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -142,6 +162,7 @@ func execRun(cmd *cobra.Command, args []string) error {
Profiles: profiles,
SessionDuration: sessionTTL,
AssumeRoleDuration: assumeRoleTTL,
AssumeRoleArn: assumeRoleARN,
}

var allowedBackends []keyring.BackendType
Expand Down
19 changes: 13 additions & 6 deletions lib/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type ProviderOptions struct {
ExpiryWindow time.Duration
Profiles Profiles
MFAConfig MFAConfig

AssumeRoleArn string
// if true, use store_singlekritem SessionCache (new)
// if false, use store_kritempersession SessionCache (old)
SessionCacheSingleItem bool
Expand Down Expand Up @@ -201,18 +201,25 @@ func (p *Provider) getOktaSessionCookieKey() string {
}

func (p *Provider) getSamlSessionCreds() (sts.Credentials, error) {
var profileARN string
var ok bool
source := sourceProfile(p.profile, p.profiles)
oktaAwsSAMLUrl, err := p.getSamlURL()
if err != nil {
return sts.Credentials{}, err
}
oktaSessionCookieKey := p.getOktaSessionCookieKey()

profileARN, ok := p.profiles[source]["role_arn"]
if !ok {
return sts.Credentials{}, errors.New("Source profile must provide `role_arn`")
// if the assumable role is passed it have it override what is in the profile
if p.AssumeRoleArn != "" {
profileARN = p.AssumeRoleArn
log.Debug("Overriding Assumable role with: ", profileARN)
} else {
profileARN, ok = p.profiles[source]["role_arn"]
if !ok {
return sts.Credentials{}, errors.New("Source profile must provide `role_arn`")
}
}

provider := OktaProvider{
MFAConfig: p.ProviderOptions.MFAConfig,
Keyring: p.keyring,
Expand All @@ -221,7 +228,7 @@ func (p *Provider) getSamlSessionCreds() (sts.Credentials, error) {
OktaAwsSAMLUrl: oktaAwsSAMLUrl,
OktaSessionCookieKey: oktaSessionCookieKey,
}

if region := p.profiles[source]["region"]; region != "" {
provider.AwsRegion = region
}
Expand Down