Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move duplicate code to aws package #59

Merged
merged 4 commits into from Sep 13, 2018
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
39 changes: 39 additions & 0 deletions aws/sts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
)

func AssumeSAMLRole(PrincipalArn, RoleArn, SAMLAssertion string) (*Credentials, error) {
input := sts.AssumeRoleWithSAMLInput{
PrincipalArn: aws.String(PrincipalArn),
RoleArn: aws.String(RoleArn),
SAMLAssertion: aws.String(SAMLAssertion),
}

sess := session.Must(session.NewSession())
svc := sts.New(sess)

aResp, err := svc.AssumeRoleWithSAML(&input)
if err != nil {
return nil, fmt.Errorf("assuming role: %v", err)
}

keyID := *aResp.Credentials.AccessKeyId
secretKey := *aResp.Credentials.SecretAccessKey
sessionToken := *aResp.Credentials.SessionToken
expiration := *aResp.Credentials.Expiration

creds := Credentials{
AccessKeyID: keyID,
SecretAccessKey: secretKey,
SessionToken: sessionToken,
Expiration: expiration,
}

return &creds, nil
}
36 changes: 4 additions & 32 deletions okta/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ package okta
import (
"fmt"

awsprovider "github.com/allcloud-io/clisso/aws"
"github.com/allcloud-io/clisso/aws"
"github.com/allcloud-io/clisso/config"
"github.com/allcloud-io/clisso/saml"
"github.com/allcloud-io/clisso/spinner"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/howeyc/gopass"
)

// Get gets temporary credentials for the given app.
func Get(app, provider string) (*awsprovider.Credentials, error) {
func Get(app, provider string) (*aws.Credentials, error) {
// Get provider config
p, err := config.GetOktaProvider(provider)
if err != nil {
Expand Down Expand Up @@ -102,34 +99,9 @@ func Get(app, provider string) (*awsprovider.Credentials, error) {
return nil, err
}

// Assume role
input := sts.AssumeRoleWithSAMLInput{
PrincipalArn: aws.String(arn.Provider),
RoleArn: aws.String(arn.Role),
SAMLAssertion: aws.String(*samlAssertion),
}

sess := session.Must(session.NewSession())
svc := sts.New(sess)

s.Start()
aResp, err := svc.AssumeRoleWithSAML(&input)
creds, err := aws.AssumeSAMLRole(arn.Provider, arn.Role, *samlAssertion)
s.Stop()
if err != nil {
return nil, fmt.Errorf("assuming role: %v", err)
}

keyID := *aResp.Credentials.AccessKeyId
secretKey := *aResp.Credentials.SecretAccessKey
sessionToken := *aResp.Credentials.SessionToken
expiration := *aResp.Credentials.Expiration

creds := awsprovider.Credentials{
AccessKeyID: keyID,
SecretAccessKey: secretKey,
SessionToken: sessionToken,
Expiration: expiration,
}

return &creds, nil
return creds, err
}
36 changes: 4 additions & 32 deletions onelogin/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import (
"fmt"
"time"

awsprovider "github.com/allcloud-io/clisso/aws"
"github.com/allcloud-io/clisso/aws"
"github.com/allcloud-io/clisso/config"
"github.com/allcloud-io/clisso/saml"
"github.com/allcloud-io/clisso/spinner"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/howeyc/gopass"
)

Expand All @@ -29,7 +26,7 @@ const (

// Get gets temporary credentials for the given app.
// TODO Move AWS logic outside this function.
func Get(app, provider string) (*awsprovider.Credentials, error) {
func Get(app, provider string) (*aws.Credentials, error) {
// Read config
p, err := config.GetOneLoginProvider(provider)
if err != nil {
Expand Down Expand Up @@ -185,34 +182,9 @@ func Get(app, provider string) (*awsprovider.Credentials, error) {
return nil, err
}

// Assume role
pAssumeRole := sts.AssumeRoleWithSAMLInput{
PrincipalArn: aws.String(arn.Provider),
RoleArn: aws.String(arn.Role),
SAMLAssertion: aws.String(rMfa.Data),
}

sess := session.Must(session.NewSession())
svc := sts.New(sess)

s.Start()
resp, err := svc.AssumeRoleWithSAML(&pAssumeRole)
creds, err := aws.AssumeSAMLRole(arn.Provider, arn.Role, rMfa.Data)
s.Stop()
if err != nil {
return nil, fmt.Errorf("assuming role: %v", err)
}

keyID := *resp.Credentials.AccessKeyId
secretKey := *resp.Credentials.SecretAccessKey
sessionToken := *resp.Credentials.SessionToken
expiration := *resp.Credentials.Expiration

creds := awsprovider.Credentials{
AccessKeyID: keyID,
SecretAccessKey: secretKey,
SessionToken: sessionToken,
Expiration: expiration,
}

return &creds, nil
return creds, err
}