-
Notifications
You must be signed in to change notification settings - Fork 0
/
sts.go
39 lines (33 loc) · 857 Bytes
/
sts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package sts
import (
"time"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
)
type Credentials struct {
AccessKeyId string
SecretAccessKey string
SessionToken string
Expiration time.Time
}
func AssumeRole(principal, role, samlAssertion string) (*Credentials, error) {
session, err := session.NewSession()
if err != nil {
return nil, err
}
svc := sts.New(session)
resp, err := svc.AssumeRoleWithSAML(&sts.AssumeRoleWithSAMLInput{
PrincipalArn: &principal,
RoleArn: &role,
SAMLAssertion: &samlAssertion,
})
if err != nil {
return nil, err
}
return &Credentials{
AccessKeyId: *resp.Credentials.AccessKeyId,
SecretAccessKey: *resp.Credentials.SecretAccessKey,
SessionToken: *resp.Credentials.SessionToken,
Expiration: *resp.Credentials.Expiration,
}, nil
}