-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
iam.go
207 lines (172 loc) · 5.58 KB
/
iam.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package aws
import (
"context"
"fmt"
"net/url"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/aws-sdk-go-v2/service/iam/types"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/testing"
)
// GetIamCurrentUserName gets the username for the current IAM user.
func GetIamCurrentUserName(t testing.TestingT) string {
out, err := GetIamCurrentUserNameE(t)
if err != nil {
t.Fatal(err)
}
return out
}
// GetIamCurrentUserNameE gets the username for the current IAM user.
func GetIamCurrentUserNameE(t testing.TestingT) (string, error) {
iamClient, err := NewIamClientE(t, defaultRegion)
if err != nil {
return "", err
}
resp, err := iamClient.GetUser(context.Background(), &iam.GetUserInput{})
if err != nil {
return "", err
}
return *resp.User.UserName, nil
}
// GetIamCurrentUserArn gets the ARN for the current IAM user.
func GetIamCurrentUserArn(t testing.TestingT) string {
out, err := GetIamCurrentUserArnE(t)
if err != nil {
t.Fatal(err)
}
return out
}
// GetIamCurrentUserArnE gets the ARN for the current IAM user.
func GetIamCurrentUserArnE(t testing.TestingT) (string, error) {
iamClient, err := NewIamClientE(t, defaultRegion)
if err != nil {
return "", err
}
resp, err := iamClient.GetUser(context.Background(), &iam.GetUserInput{})
if err != nil {
return "", err
}
return *resp.User.Arn, nil
}
// GetIamPolicyDocument gets the most recent policy (JSON) document for an IAM policy.
func GetIamPolicyDocument(t testing.TestingT, region string, policyARN string) string {
out, err := GetIamPolicyDocumentE(t, region, policyARN)
if err != nil {
t.Fatal(err)
}
return out
}
// GetIamPolicyDocumentE gets the most recent policy (JSON) document for an IAM policy.
func GetIamPolicyDocumentE(t testing.TestingT, region string, policyARN string) (string, error) {
iamClient, err := NewIamClientE(t, region)
if err != nil {
return "", err
}
versions, err := iamClient.ListPolicyVersions(context.Background(), &iam.ListPolicyVersionsInput{
PolicyArn: &policyARN,
})
if err != nil {
return "", err
}
var defaultVersion string
for _, version := range versions.Versions {
if version.IsDefaultVersion == true {
defaultVersion = *version.VersionId
}
}
document, err := iamClient.GetPolicyVersion(context.Background(), &iam.GetPolicyVersionInput{
PolicyArn: aws.String(policyARN),
VersionId: aws.String(defaultVersion),
})
if err != nil {
return "", err
}
unescapedDocument := document.PolicyVersion.Document
if unescapedDocument == nil {
return "", fmt.Errorf("no policy document found for policy %s", policyARN)
}
escapedDocument, err := url.QueryUnescape(*unescapedDocument)
if err != nil {
return "", err
}
return escapedDocument, nil
}
// CreateMfaDevice creates an MFA device using the given IAM client.
func CreateMfaDevice(t testing.TestingT, iamClient *iam.Client, deviceName string) *types.VirtualMFADevice {
mfaDevice, err := CreateMfaDeviceE(t, iamClient, deviceName)
if err != nil {
t.Fatal(err)
}
return mfaDevice
}
// CreateMfaDeviceE creates an MFA device using the given IAM client.
func CreateMfaDeviceE(t testing.TestingT, iamClient *iam.Client, deviceName string) (*types.VirtualMFADevice, error) {
logger.Default.Logf(t, "Creating an MFA device called %s", deviceName)
output, err := iamClient.CreateVirtualMFADevice(context.Background(), &iam.CreateVirtualMFADeviceInput{
VirtualMFADeviceName: aws.String(deviceName),
})
if err != nil {
return nil, err
}
if err := EnableMfaDeviceE(t, iamClient, output.VirtualMFADevice); err != nil {
return nil, err
}
return output.VirtualMFADevice, nil
}
// EnableMfaDevice enables a newly created MFA Device by supplying the first two one-time passwords, so that it can be used for future
// logins by the given IAM User.
func EnableMfaDevice(t testing.TestingT, iamClient *iam.Client, mfaDevice *types.VirtualMFADevice) {
err := EnableMfaDeviceE(t, iamClient, mfaDevice)
if err != nil {
t.Fatal(err)
}
}
// EnableMfaDeviceE enables a newly created MFA Device by supplying the first two one-time passwords, so that it can be used for future
// logins by the given IAM User.
func EnableMfaDeviceE(t testing.TestingT, iamClient *iam.Client, mfaDevice *types.VirtualMFADevice) error {
logger.Default.Logf(t, "Enabling MFA device %s", aws.ToString(mfaDevice.SerialNumber))
iamUserName, err := GetIamCurrentUserArnE(t)
if err != nil {
return err
}
authCode1, err := GetTimeBasedOneTimePassword(mfaDevice)
if err != nil {
return err
}
logger.Default.Logf(t, "Waiting 30 seconds for a new MFA Token to be generated...")
time.Sleep(30 * time.Second)
authCode2, err := GetTimeBasedOneTimePassword(mfaDevice)
if err != nil {
return err
}
_, err = iamClient.EnableMFADevice(context.Background(), &iam.EnableMFADeviceInput{
AuthenticationCode1: aws.String(authCode1),
AuthenticationCode2: aws.String(authCode2),
SerialNumber: mfaDevice.SerialNumber,
UserName: aws.String(iamUserName),
})
if err != nil {
return err
}
logger.Log(t, "Waiting for MFA Device enablement to propagate.")
time.Sleep(10 * time.Second)
return nil
}
// NewIamClient creates a new IAM client.
func NewIamClient(t testing.TestingT, region string) *iam.Client {
client, err := NewIamClientE(t, region)
if err != nil {
t.Fatal(err)
}
return client
}
// NewIamClientE creates a new IAM client.
func NewIamClientE(t testing.TestingT, region string) (*iam.Client, error) {
sess, err := NewAuthenticatedSession(region)
if err != nil {
return nil, err
}
return iam.NewFromConfig(*sess), nil
}