-
Notifications
You must be signed in to change notification settings - Fork 0
/
greengrass.go
86 lines (73 loc) · 2.15 KB
/
greengrass.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
package ggprov
import (
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/greengrass"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/pkg/errors"
)
const ggRoleDocument = `{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "greengrass.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}`
const ggThingPolicyDocument = `{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:*",
"greengrass:*"
],
"Resource": "*"
}
]
}`
const (
ggRoleName = "Greengrass-Service-Role"
ggRoleDescription = "Allows AWS Greengrass to call AWS Services on your behalf"
ggPolicyArn = "arn:aws:iam::aws:policy/service-role/AWSGreengrassResourceAccessRolePolicy"
)
// CreateOrGetServiceRoleForAccount create or get the service role
func (s *Svcs) CreateOrGetServiceRoleForAccount() (*IamRole, error) {
var role *IamRole
resp, err := s.GreengrassAPI.GetServiceRoleForAccount(&greengrass.GetServiceRoleForAccountInput{})
if err != nil && !isNotFoundErr(err) {
return nil, errors.Wrap(err, "Failed to get service role for account")
}
// create the resource
if isNotFoundErr(err) {
role, err = s.CreateOrGetIamRole(ggRoleName, ggRoleDescription, ggRoleDocument)
if err != nil {
return nil, err
}
// aws iam attach-role-policy --policy-arn arn:aws:iam::aws:policy/servicerole/AWSGreengrassResourceAccessRolePolicy \
// --role-name $IAMROLENAME
log.Println("Attach service role for account")
_, err = s.IAMAPI.AttachRolePolicy(&iam.AttachRolePolicyInput{
PolicyArn: aws.String(ggPolicyArn),
RoleName: aws.String(ggRoleName),
})
if err != nil {
return nil, errors.Wrap(err, "Failed to attach role policy")
}
log.Println("Associated Service Role to Account")
_, err = s.GreengrassAPI.AssociateServiceRoleToAccount(&greengrass.AssociateServiceRoleToAccountInput{
RoleArn: aws.String(role.Arn),
})
if err != nil {
return nil, errors.Wrap(err, "Failed to associate service role to account")
}
} else {
role = newRole(resp.RoleArn)
}
return role, nil
}