Skip to content

Commit

Permalink
Specified by option
Browse files Browse the repository at this point in the history
  • Loading branch information
youyo committed May 18, 2018
1 parent 001ce44 commit 5794146
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
8 changes: 4 additions & 4 deletions awslogin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func NewSession(sourceProfile string) (s *session.Session, err error) {
return
}

func NewCredentials(sess *session.Session, arn, roleSessionName, mfaSerial string) (creds credentials.Value, err error) {
assumeRoleProvider := buildAssumeRoleProvider(roleSessionName, mfaSerial)
func NewCredentials(sess *session.Session, arn, roleSessionName, mfaSerial string, durationSeconds int) (creds credentials.Value, err error) {
assumeRoleProvider := buildAssumeRoleProvider(roleSessionName, mfaSerial, durationSeconds)
creds, err = stscreds.NewCredentials(sess, arn, assumeRoleProvider).Get()
return
}

func buildAssumeRoleProvider(roleSessionName, mfaSerial string) (f func(p *stscreds.AssumeRoleProvider)) {
func buildAssumeRoleProvider(roleSessionName, mfaSerial string, durationSeconds int) (f func(p *stscreds.AssumeRoleProvider)) {
f = func(p *stscreds.AssumeRoleProvider) {
p.Duration = time.Duration(60*60*12) * time.Second
p.Duration = time.Duration(durationSeconds) * time.Second
p.RoleSessionName = roleSessionName
if mfaSerial != "" {
p.SerialNumber = aws.String(mfaSerial)
Expand Down
1 change: 1 addition & 0 deletions awslogin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestBuildAssumeRoleProvider(t *testing.T) {
p := buildAssumeRoleProvider(
"test.xxxxxxxxxxxx@awslogin",
"arn:aws:iam::123456789012:mfa/jonsmith",
3600,
)
var expected func(p *stscreds.AssumeRoleProvider)
if reflect.TypeOf(p) != reflect.TypeOf(expected) {
Expand Down
20 changes: 11 additions & 9 deletions cmd/awslogin/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import (
)

var (
Name string
Version string
CommitHash string
BuildTime string
GoVersion string
app string
profile string
readFromEnv bool
Name string
Version string
CommitHash string
BuildTime string
GoVersion string
app string
profile string
readFromEnv bool
durationSeconds int
)

const (
Expand Down Expand Up @@ -63,7 +64,7 @@ func execRoot() (err error) {
return
}

creds, err := awslogin.NewCredentials(sess, cfg.ARN, cfg.RoleSessionName, cfg.MfaSerial)
creds, err := awslogin.NewCredentials(sess, cfg.ARN, cfg.RoleSessionName, cfg.MfaSerial, durationSeconds)
if err != nil {
return
}
Expand Down Expand Up @@ -145,6 +146,7 @@ func init() {
RootCmd.Flags().StringVarP(&app, "app", "a", "", "Opens with the specified application.")
RootCmd.Flags().StringVarP(&profile, "profile", "p", "", "Use a specific profile.")
RootCmd.Flags().BoolVarP(&readFromEnv, "read-from-env", "e", false, "Use a specific profile read from the environment. [$AWS_PROFILE]")
RootCmd.Flags().IntVarP(&durationSeconds, "duration-seconds", "d", 3600, "Request a session duration seconds. 900 - 43200")
}

func initConfig() {}

0 comments on commit 5794146

Please sign in to comment.