-
Notifications
You must be signed in to change notification settings - Fork 18
/
credentials_getter.go
155 lines (129 loc) · 4.69 KB
/
credentials_getter.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"context"
"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/sts"
)
const (
roleARNSuffix = ":role"
roleSessionNameMaxSize = 64
)
// CredentialsGetter can get credentials.
type CredentialsGetter interface {
Get(ctx context.Context, role string, sessionDuration time.Duration) (*Credentials, error)
}
// Credentials defines fetched credentials including expiration time.
type Credentials struct {
RoleARN string
AccessKeyID string
SecretAccessKey string
SessionToken string
Expiration time.Time
}
type stsAPI interface {
AssumeRole(ctx context.Context, params *sts.AssumeRoleInput, optFns ...func(*sts.Options)) (*sts.AssumeRoleOutput, error)
}
// STSCredentialsGetter is a credentials getter for getting credentials from
// STS.
type STSCredentialsGetter struct {
svc stsAPI
baseRoleARN string
baseRoleARNPrefix string
}
// NewSTSCredentialsGetter initializes a new STS based credentials fetcher.
func NewSTSCredentialsGetter(cfg aws.Config, baseRoleARN, baseRoleARNPrefix string, configs ...*aws.Config) *STSCredentialsGetter {
return &STSCredentialsGetter{
svc: sts.NewFromConfig(cfg),
baseRoleARN: baseRoleARN,
baseRoleARNPrefix: baseRoleARNPrefix,
}
}
// Get gets new credentials for the specified role. The credentials are fetched
// via STS.
func (c *STSCredentialsGetter) Get(ctx context.Context, role string, sessionDuration time.Duration) (*Credentials, error) {
roleARN := c.baseRoleARN + role
if strings.HasPrefix(role, c.baseRoleARNPrefix) {
roleARN = role
}
roleSessionName, err := normalizeRoleARN(roleARN, c.baseRoleARNPrefix)
if err != nil {
return nil, err
}
params := &sts.AssumeRoleInput{
RoleArn: aws.String(roleARN),
RoleSessionName: aws.String(roleSessionName),
DurationSeconds: aws.Int32(int32(sessionDuration.Seconds())),
}
resp, err := c.svc.AssumeRole(ctx, params)
if err != nil {
return nil, err
}
return &Credentials{
RoleARN: roleARN,
AccessKeyID: aws.ToString(resp.Credentials.AccessKeyId),
SecretAccessKey: aws.ToString(resp.Credentials.SecretAccessKey),
SessionToken: aws.ToString(resp.Credentials.SessionToken),
Expiration: aws.ToTime(resp.Credentials.Expiration),
}, nil
}
// GetBaseRoleARN gets base role ARN from EC2 metadata service.
func GetBaseRoleARN(ctx context.Context, cfg aws.Config) (string, error) {
metadata := imds.NewFromConfig(cfg)
iamInfo, err := metadata.GetIAMInfo(ctx, &imds.GetIAMInfoInput{})
if err != nil {
return "", err
}
arn := strings.Replace(iamInfo.InstanceProfileArn, "instance-profile", "role", 1)
baseRoleARN := strings.Split(arn, "/")
if len(baseRoleARN) < 2 {
return "", fmt.Errorf("failed to determine BaseRoleARN")
}
return fmt.Sprintf("%s/", baseRoleARN[0]), nil
}
// normalizeRoleARN normalizes a role ARN by substituting special characters
// with characters allowed for a RoleSessionName according to:
// https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html
func normalizeRoleARN(roleARN, roleARNPrefix string) (string, error) {
parts := strings.Split(roleARN, "/")
if len(parts) < 2 {
return "", fmt.Errorf("invalid roleARN: %s", roleARN)
}
remainingChars := roleSessionNameMaxSize
accountID := strings.TrimPrefix(parts[0], roleARNPrefix)
accountID = strings.TrimSuffix(accountID, roleARNSuffix)
remainingChars -= len(accountID)
return accountID + normalizePath(parts[1:], remainingChars), nil
}
// normalizePath normalizes the path levels into a roleSession valid string.
// The last level always gets as many chars as possible leaving only a minimum
// of one char for each of the other levels.
// e.g. given the levels: ["aaaaa", "bbbbb", "ccccccc"], and remaining "12" it
// would be reduced to the string: ".a.b.ccccccc"
func normalizePath(levels []string, remaining int) string {
if len(levels) == 0 {
return ""
}
last := levels[len(levels)-1]
last = strings.Replace(last, ":", "_", -1)
otherLevels := len(levels[:len(levels)-1])
maxName := remaining - (otherLevels * 2) - 1
if len(last) > maxName {
last = last[:maxName]
}
return normalizePath(levels[:len(levels)-1], remaining-len(last)-1) + "." + last
}
// GetPrefixFromARN returns the prefix from an AWS ARN as a string.
// e.g. given the role: "arn:aws:iam::012345678910:role/role-name" it would
// return the string: "arn:aws:iam::"
func GetPrefixFromARN(inputARN string) (string, error) {
arn, err := arn.Parse(inputARN)
if err != nil {
return "", fmt.Errorf("error parsing ARN (%s): %s", inputARN, err)
}
return fmt.Sprintf("arn:%s:iam::", arn.Partition), nil
}