Skip to content

Instantly share code, notes, and snippets.

@SamuelBagattin
Created April 4, 2022 17:36
Show Gist options
  • Save SamuelBagattin/25a7f66d95b7ccb170a4951d55d0f26f to your computer and use it in GitHub Desktop.
Save SamuelBagattin/25a7f66d95b7ccb170a4951d55d0f26f to your computer and use it in GitHub Desktop.
// Requesting temporary credentials
identity, err := initStsClient.AssumeRoleWithWebIdentity(
&sts.AssumeRoleWithWebIdentityInput{
RoleArn: aws.String(awsRoleArn),
RoleSessionName: aws.String("my-app"),
WebIdentityToken: aws.String(string(awsWebIdentityToken)),
DurationSeconds: aws.Int64(3600),
})
if err != nil {
panic(err)
}
// Creating a new session with the temporary credentials
sess = session.Must(session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentialsFromCreds(credentials.Value{
AccessKeyID: *identity.Credentials.AccessKeyId,
SecretAccessKey: *identity.Credentials.SecretAccessKey,
SessionToken: *identity.Credentials.SessionToken,
ProviderName: "AssumeRoleWithWebIdentity",
}),
}))
// Create a new sts client from IAM role's credentials and print the current identity
stsClient := sts.New(sess)
identity, err := stsClient.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
panic(err)
}
jsonIdentity, err := json.MarshalIndent(*identity, "", " ")
log.Printf("%s", string(jsonIdentity))
// Create a new S3 client and print all buckets
s3Client := s3.New(sess)
buckets, err := s3Client.ListBuckets(&s3.ListBucketsInput{})
if err != nil {
panic(err)
}
jsonBuckets, err := json.MarshalIndent(*buckets, "", " ")
log.Printf("%+v", string(jsonBuckets))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment