/
sts.go
41 lines (34 loc) 路 869 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
40
41
package client
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sts"
)
type CallerIdentity struct {
UserId string
Account string
Arn string
}
type StsClientInterface interface {
GetCallerIdentity(ctx context.Context, params *sts.GetCallerIdentityInput, optFns ...func(*sts.Options)) (*sts.GetCallerIdentityOutput, error)
}
type StsClient struct {
client StsClientInterface
}
func NewSts(cfg aws.Config) *StsClient {
s := StsClient{
client: sts.NewFromConfig(cfg),
}
return &s
}
func (s *StsClient) GetCallerIdentity() (*CallerIdentity, error) {
identity, err := s.client.GetCallerIdentity(context.Background(), &sts.GetCallerIdentityInput{})
if err != nil {
return nil, err
}
return &CallerIdentity{
UserId: *identity.UserId,
Account: *identity.Account,
Arn: *identity.Arn,
}, nil
}