Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/dlm/iam.py
1567 views
1
import json
2
3
4
class IAM(object):
5
6
def __init__(self, iam_client):
7
self.iam_client = iam_client
8
9
def check_if_role_exists(self, role_name):
10
"""Method to verify if a particular role exists"""
11
try:
12
self.iam_client.get_role(RoleName=role_name)
13
except self.iam_client.exceptions.NoSuchEntityException:
14
return False
15
return True
16
17
def check_if_policy_exists(self, policy_arn):
18
"""Method to verify if a particular policy exists"""
19
try:
20
self.iam_client.get_policy(PolicyArn=policy_arn)
21
except self.iam_client.exceptions.NoSuchEntityException:
22
return False
23
return True
24
25
def attach_policy_to_role(self, policy_arn, role_name):
26
"""Method to attach LifecyclePolicy to role specified by role_name"""
27
return self.iam_client.attach_role_policy(
28
PolicyArn=policy_arn,
29
RoleName=role_name
30
)
31
32
def create_role_with_trust_policy(self, role_name, assume_role_policy):
33
"""Method to create role with a given role name
34
and assume_role_policy
35
"""
36
return self.iam_client.create_role(
37
RoleName=role_name,
38
AssumeRolePolicyDocument=json.dumps(assume_role_policy))
39
40
def get_policy(self, arn):
41
"""Method to get the Policy for a particular ARN
42
This is used to display the policy contents to the user
43
"""
44
pol_det = self.iam_client.get_policy(PolicyArn=arn)
45
policy_version_details = self.iam_client.get_policy_version(
46
PolicyArn=arn,
47
VersionId=pol_det.get("Policy", {}).get("DefaultVersionId", "")
48
)
49
return policy_version_details\
50
.get("PolicyVersion", {})\
51
.get("Document", {})
52
53