-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsaml-roles.yaml
More file actions
179 lines (172 loc) · 5.76 KB
/
saml-roles.yaml
File metadata and controls
179 lines (172 loc) · 5.76 KB
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
AWSTemplateFormatVersion: '2010-09-09'
Resources:
FederatedDBARole:
DependsOn:
- IdentityProvider
Properties:
AssumeRolePolicyDocument:
Statement:
Action: sts:AssumeRoleWithSAML
Condition:
StringEquals:
SAML:aud: https://signin.aws.amazon.com/saml
Effect: Allow
Principal:
Federated:
Fn::Join:
- ''
- - 'arn:aws:iam::'
- Ref: AWS::AccountId
- :saml-provider/AzureAD
Version: '2012-10-17'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/job-function/DatabaseAdministrator
Path: /
RoleName: FederatedDBARole
Type: AWS::IAM::Role
FederatedObserverRole:
DependsOn:
- IdentityProvider
Properties:
AssumeRolePolicyDocument:
Statement:
Action: sts:AssumeRoleWithSAML
Condition:
StringEquals:
SAML:aud: https://signin.aws.amazon.com/saml
Effect: Allow
Principal:
Federated:
Fn::Join:
- ''
- - 'arn:aws:iam::'
- Ref: AWS::AccountId
- :saml-provider/AzureAD
Version: '2012-10-17'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/ReadOnlyAccess
Path: /
RoleName: FederatedObserverRole
Type: AWS::IAM::Role
FederatedPowerUserRole:
DependsOn:
- IdentityProvider
Properties:
AssumeRolePolicyDocument:
Statement:
Action: sts:AssumeRoleWithSAML
Condition:
StringEquals:
SAML:aud: https://signin.aws.amazon.com/saml
Effect: Allow
Principal:
Federated:
Fn::Join:
- ''
- - 'arn:aws:iam::'
- Ref: AWS::AccountId
- :saml-provider/AzureAD
Version: '2012-10-17'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/PowerUserAccess
Path: /
RoleName: FederatedPowerUserRole
Type: AWS::IAM::Role
IdPLambda:
Type: AWS::Lambda::Function
Properties:
Runtime: python3.7
Handler: index.lambda_handler
MemorySize: 128
Role: !GetAtt IdPLambdaExecutionRole.Arn
Timeout: 30
Code:
ZipFile: !Sub |
import boto3
from botocore.exceptions import ClientError
import cfnresponse
iam = boto3.client("iam")
def create_idp(name, saml_metadata):
try:
resp = iam.create_saml_provider(SAMLMetadataDocument=saml_metadata, Name=name)
return True, resp['SAMLProviderArn']
except Exception as e:
return False, f"Could not create SAML IdP due to: {str(e)}"
def delete_idp(arn):
try:
iam.delete_saml_provider(SAMLProviderArn=arn)
return True, f"SAML provider with ARN {arn} deleted"
except ClientError as e:
if e.response['Error']['Code'] == "NoSuchEntity":
return True, f"SAML provider with ARN {arn} already deleted."
else:
return False, f"Could not delete SAML IdP with ARN {arn}: {str(e)}"
except Exception as e:
return False, f"Could not delete SAML IdP with ARN {arn}: {str(e)}"
def update_idp(arn, saml_metadata):
try:
resp = iam.update_saml_provider(SAMLMetadataDocument=saml_metadata, SAMLProviderArn=arn)
return True, f"SAML provider {arn} updated"
except Exception as e:
return False, f"Cannot update SAML provider {arn}: {str(e)}"
def lambda_handler(event, context):
saml_metadata = event['ResourceProperties']['Metadata']
idp_name = event['ResourceProperties']['Name']
if event['RequestType'] == 'Create':
res, idp_arn = create_idp(idp_name, saml_metadata)
reason = "Creation succeeded"
else:
idp_arn = "arn:aws:iam::${AWS::AccountId}:saml-provider/{}".format(idp_name)
if event['RequestType'] == 'Update':
res, reason = update_idp(idp_arn, saml_metadata)
elif event['RequestType'] == 'Delete':
res, reason = delete_idp(idp_arn)
else:
res = False
reason = "Unknown operation: " + event['RequestType']
response_data = dict()
response_data['Reason'] = reason
if res:
cfnresponse.send(event, context, cfnresponse.SUCCESS, response_data, idp_arn)
else:
cfnresponse.send(event, context, cfnresponse.FAILED, response_data, idp_arn)
IdPLambdaExecutionRole:
Properties:
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Version: '2012-10-17'
Path: /
Policies:
- PolicyDocument:
Statement:
- Action:
- iam:*SamlProvider
Effect: Allow
Resource: '*'
- Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Effect: Allow
Resource: '*'
Version: '2012-10-17'
PolicyName: root
Type: AWS::IAM::Role
IdentityProvider:
Type: Custom::IdentityProvider
Properties:
Metadata: |
<MetadataDocument>
Name: AzureAD
Region:
Ref: AWS::Region
ServiceToken:
Fn::GetAtt:
- IdPLambda
- Arn