-
Notifications
You must be signed in to change notification settings - Fork 878
/
main.go
82 lines (69 loc) · 2.25 KB
/
main.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
// Copyright 2016-2020, Pulumi Corporation. All rights reserved.
package main
import (
"encoding/json"
"github.com/pulumi/pulumi-aws/sdk/v4/go/aws/iam"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi/config"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
config := config.New(ctx, "")
unprivilegedUsername := config.Require("unprivilegedUsername")
unprivilegedUser, err := iam.NewUser(ctx, "unprivilegedUser", &iam.UserArgs{
Name: pulumi.String(unprivilegedUsername),
})
// additional_secret_outputs specify properties that must be encrypted as secrets
// https://www.pulumi.com/docs/intro/concepts/resources/#additionalsecretoutputs
secretString := []string{"secret"}
unprivilegedUserCreds, err := iam.NewAccessKey(ctx, "unprivileged-user-key", &iam.AccessKeyArgs{
User: unprivilegedUser.Name,
}, pulumi.AdditionalSecretOutputs(secretString))
if err != nil {
return err
}
tempPolicy := unprivilegedUser.Arn.ApplyT(func(arn string) (string, error) {
tempoJSON, err := json.Marshal(map[string]interface{}{
"Version": "2012-10-17",
"Statement": []interface{}{
map[string]interface{}{
"Sid": "AllowAssumeRole",
"Effect": "Allow",
"Principal": map[string]interface{}{"AWS": arn},
"Action": []string{"sts:AssumeRole"},
},
},
})
if err != nil {
return "", err
}
return string(tempoJSON), nil
})
allowS3ManagementRole, err := iam.NewRole(ctx, "allow-s3-management", &iam.RoleArgs{
Description: pulumi.String("Allow management of S3 buckets"),
AssumeRolePolicy: tempPolicy,
})
if err != nil {
return err
}
_, err = iam.NewRolePolicy(ctx, "allow-s3-management-policy", &iam.RolePolicyArgs{
Role: allowS3ManagementRole.Name,
Policy: pulumi.String(`{
"Version": "2012-10-17",
"Statement": [{
"Sid": "AllowS3Management",
"Effect": "Allow",
"Resource": "*",
"Action": "s3:*"
}]
}`),
}, pulumi.Parent(allowS3ManagementRole))
if err != nil {
return err
}
ctx.Export("roleArn", allowS3ManagementRole.Arn)
ctx.Export("accessKeyId", unprivilegedUserCreds.ID().ToStringOutput())
ctx.Export("secretAccessKey", unprivilegedUserCreds.Secret)
return nil
})
}