-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathiam-role-helper.py
105 lines (95 loc) · 2.57 KB
/
iam-role-helper.py
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
import boto3, botocore, argparse, sys, json
parser = argparse.ArgumentParser(description='aws IAM Role helper')
parser.add_argument('-c', '--create', action='store_true', help='create IAM Role for vidoe face rek')
parser.add_argument('-d', '--delete', action='store_true', help='delete IAM Role for vidoe face rek')
trustRole='''{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "rekognition.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}'''
rolePolicy='''{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sns:Publish"
],
"Resource": "arn:aws:sns:*:*:AmazonRekognition*"
},
{
"Effect": "Allow",
"Action": [
"kinesis:PutRecord",
"kinesis:PutRecords"
],
"Resource": "arn:aws:kinesis:*:*:stream/AmazonRekognition*"
},
{
"Effect": "Allow",
"Action": [
"kinesisvideo:GetDataEndpoint",
"kinesisvideo:GetMedia"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "kinesis:*",
"Resource": "*"
}
]
}'''
def createRole(roleName, trustRole, policyName, rolePolicy):
try:
response = client.create_role(
RoleName=roleName,
AssumeRolePolicyDocument=trustRole
)
print(response['Role']['Arn'])
client.put_role_policy(
RoleName=roleName,
PolicyName=policyName,
PolicyDocument=rolePolicy
)
print("Success: done adding inline policy to role")
except botocore.exceptions.ClientError as e:
print "Error: {0}".format(e)
def deleteRole(roleName, policyName):
try:
client.delete_role_policy(
RoleName=roleName,
PolicyName=policyName
)
client.delete_role(
RoleName=roleName
)
print("Success: done deleting role: " + roleName)
except botocore.exceptions.ClientError as e:
print "Error: {0}".format(e)
if __name__ == '__main__':
if len(sys.argv)==1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
with open('config.json') as json_data_file:
config = json.load(json_data_file)
roleName = config['iamRole']
policyName = config['iamPolicy']
client = boto3.client('iam')
if (args.create):
createRole(roleName, trustRole, policyName, rolePolicy)
elif (args.delete):
deleteRole(roleName, policyName)