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

What's the easiest way to create a session from AssumeRole response? #801

Closed
tleyden opened this issue Aug 12, 2016 · 5 comments
Closed
Assignees
Labels
guidance Question that needs advice or information.

Comments

@tleyden
Copy link

tleyden commented Aug 12, 2016

Here's how I'm creating a session from an AssumeRole response:

    session, err := session.NewSession()
    if err != nil {
        return nil, err
    }

    stsService := sts.New(session, &aws.Config{Region: aws.String(p.AWSRegion)})

    params := &sts.AssumeRoleInput{
        RoleArn:         aws.String("arn:aws:iam::1234:role/foo"), 
        RoleSessionName: aws.String("mysession"),                          
        ExternalId:      aws.String("myexternalid"),                                    
    }
    resp, err := stsService.AssumeRole(params)
    if err != nil {
        return nil, err
    }


    provider := NewAssumeRoleCredentialsProvider(resp.Credentials)

    ec2Service := ec2.New(session,
        &aws.Config{
            Region: aws.String(p.AWSRegion),
            Credentials: credentials.NewCredentials(provider),
        },
    )

        // etc ..

The NewAssumeRoleCredentialsProvider() call is to create a custom credentials.Provider that I had to create:

import (
    "time"

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

func NewAssumeRoleCredentialsProvider(credentials *sts.Credentials) *AssumeRoleCredentialsProvider {
    return &AssumeRoleCredentialsProvider{
        AssumeRoleCredentials: credentials,
    }
}

type AssumeRoleCredentialsProvider struct {
    AssumeRoleCredentials *sts.Credentials
}

func (c AssumeRoleCredentialsProvider) Retrieve() (credentials.Value, error) {
    return credentials.Value{
        AccessKeyID:     *c.AssumeRoleCredentials.AccessKeyId,
        SecretAccessKey: *c.AssumeRoleCredentials.SecretAccessKey,
        SessionToken:    *c.AssumeRoleCredentials.SessionToken,
        ProviderName:    "AssumeRoleCredentialsProvider",
    }, nil

}

func (c AssumeRoleCredentialsProvider) IsExpired() bool {
    return c.AssumeRoleCredentials.Expiration.After(time.Now())

}

Is there already the equivalent of AssumeRoleCredentialsProvider somewhere in the API that I can use? Or a less awkward way of doing this?

Sorry if this isn't the best place to ask .. if not let me know where to ask questions like these.

@jasdel jasdel added the guidance Question that needs advice or information. label Aug 12, 2016
@jasdel
Copy link
Contributor

jasdel commented Aug 12, 2016

Hi @tleyden thanks for reaching out to us. This is a great place to ask questions, and contribute. With v1.3.0 the SDK now supports loading assume roles derived from the Shared Config files ~/.aws/config and ~/.aws/credentials. See the SDK's sessions wiki page for more information on this change.

Assuming a role from the shared config is great if you have access to those files, or want to create a session from them.

Alternatively if your use case needs the sessions to be created at runtime dynamically the SDK provides the stscreds.AssumeRoleProvider type that should make using assume roles much easier.

The way a session will setup assume role from the shared config is a good example how to setup a stscreds.AssumeRoleProvider.

@jasdel jasdel added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Aug 12, 2016
@jasdel jasdel self-assigned this Aug 12, 2016
@tleyden
Copy link
Author

tleyden commented Aug 14, 2016

Ok thanks! I think stscreds.AssumeRoleProvider is exactly what I was looking for.

@tleyden tleyden closed this as completed Aug 14, 2016
@diehlaws diehlaws removed response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Jan 30, 2019
skotambkar added a commit to skotambkar/aws-sdk-go that referenced this issue May 20, 2021
@ddrocco
Copy link

ddrocco commented Sep 18, 2021

It's been more than 5 years, and the AWS golang SDK is still incomprehensible with regards to how to do this more natively. I stared at the stscreds.AssumeRoleProvider file for 20 minutes and still can't figure it out.
@tleyden's code did the job though - thank you! :D

@vinodcc
Copy link

vinodcc commented Oct 6, 2021

Thanks very much, the above example helped me. For the benefit of others, I have used stscreds.AssumeRoleProvider to create a session in this way.

var sess *session.Session
sess, err = session.NewSession(&aws.Config{
  Credentials: stscreds.NewCredentials(
    sess, 
    "arn:aws:iam::1234:role/foo", 
    func(provider *stscreds.AssumeRoleProvider) {
      provider.RoleSessionName = "mysession"
    },
  ),
  Region: aws.String("us-west-2"),
})
if err != nil {
  return nil, err
}

The sess object has been used to create other aws service clients for accessing aws services.

@AlbertKostusev
Copy link

AlbertKostusev commented Jan 5, 2022

The example above worked for me when I also added provider.RoleARN = "your-role" to the stscreds.AssumeRoleProvider function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

6 participants