|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go/aws" |
| 8 | + "github.com/aws/aws-sdk-go/aws/session" |
| 9 | + "github.com/aws/aws-sdk-go/service/sts" |
| 10 | + "github.com/dnitsch/aws-cli-auth/internal/config" |
| 11 | + "github.com/dnitsch/aws-cli-auth/internal/util" |
| 12 | + "github.com/pkg/errors" |
| 13 | +) |
| 14 | + |
| 15 | +// AWSRole aws role attributes |
| 16 | +type AWSRole struct { |
| 17 | + RoleARN string |
| 18 | + PrincipalARN string |
| 19 | + Name string |
| 20 | +} |
| 21 | + |
| 22 | +// LoginStsSaml exchanges saml response for STS creds |
| 23 | +func LoginStsSaml(samlResponse string, role *util.AWSRole) (*util.AWSCredentials, error) { |
| 24 | + sess, err := session.NewSession() |
| 25 | + if err != nil { |
| 26 | + return nil, errors.Wrap(err, "Failed to create session") |
| 27 | + } |
| 28 | + |
| 29 | + svc := sts.New(sess) |
| 30 | + |
| 31 | + params := &sts.AssumeRoleWithSAMLInput{ |
| 32 | + PrincipalArn: aws.String(role.PrincipalARN), // Required |
| 33 | + RoleArn: aws.String(role.RoleARN), // Required |
| 34 | + SAMLAssertion: aws.String(samlResponse), // Required |
| 35 | + DurationSeconds: aws.Int64(3600), |
| 36 | + } |
| 37 | + |
| 38 | + resp, err := svc.AssumeRoleWithSAML(params) |
| 39 | + if err != nil { |
| 40 | + return nil, errors.Wrap(err, "Failed to retrieve STS credentials using SAML") |
| 41 | + } |
| 42 | + |
| 43 | + return &util.AWSCredentials{ |
| 44 | + AWSAccessKey: aws.StringValue(resp.Credentials.AccessKeyId), |
| 45 | + AWSSecretKey: aws.StringValue(resp.Credentials.SecretAccessKey), |
| 46 | + AWSSessionToken: aws.StringValue(resp.Credentials.SessionToken), |
| 47 | + PrincipalARN: aws.StringValue(resp.AssumedRoleUser.Arn), |
| 48 | + Expires: resp.Credentials.Expiration.Local(), |
| 49 | + }, nil |
| 50 | +} |
| 51 | + |
| 52 | +func LoginAwsWebToken(username string) (*util.AWSCredentials, error) { |
| 53 | + // var role string |
| 54 | + sess, err := session.NewSession() |
| 55 | + if err != nil { |
| 56 | + return nil, errors.Wrap(err, "Failed to create session") |
| 57 | + } |
| 58 | + |
| 59 | + svc := sts.New(sess) |
| 60 | + r, exists := os.LookupEnv(config.AWS_ROLE_ARN) |
| 61 | + if !exists { |
| 62 | + util.Exit(fmt.Errorf("Role Var Not Found")) |
| 63 | + } |
| 64 | + token, err := util.GetWebIdTokenFileContents() |
| 65 | + if err != nil { |
| 66 | + util.Exit(err) |
| 67 | + } |
| 68 | + sessionName := util.SessionName(username, config.SELF_NAME) |
| 69 | + input := &sts.AssumeRoleWithWebIdentityInput{ |
| 70 | + RoleArn: &r, |
| 71 | + RoleSessionName: &sessionName, |
| 72 | + WebIdentityToken: &token, |
| 73 | + } |
| 74 | + |
| 75 | + resp, err := svc.AssumeRoleWithWebIdentity(input) |
| 76 | + if err != nil { |
| 77 | + return nil, errors.Wrap(err, "Failed to retrieve STS credentials using SAML") |
| 78 | + } |
| 79 | + |
| 80 | + return &util.AWSCredentials{ |
| 81 | + AWSAccessKey: aws.StringValue(resp.Credentials.AccessKeyId), |
| 82 | + AWSSecretKey: aws.StringValue(resp.Credentials.SecretAccessKey), |
| 83 | + AWSSessionToken: aws.StringValue(resp.Credentials.SessionToken), |
| 84 | + PrincipalARN: aws.StringValue(resp.AssumedRoleUser.Arn), |
| 85 | + Expires: resp.Credentials.Expiration.Local(), |
| 86 | + }, nil |
| 87 | +} |
0 commit comments