-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf-q-biz-setup.yaml
320 lines (305 loc) · 12.3 KB
/
cf-q-biz-setup.yaml
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
Conditions:
IfCreateIDC: !Equals
- !Ref CreateIDC
- "yes"
Resources:
IDCLambdaExecutionRole:
Type: AWS::IAM::Role
Condition: IfCreateIDC
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: SSOAdminPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- sso:CreateInstance
- sso:DescribeInstance
- sso:ListInstances
- iam:CreateServiceLinkedRole
- identitystore:CreateGroup
- identitystore:CreateGroupMembership
- identitystore:CreateUser
Resource:
- '*'
IDCLambda:
Type: AWS::Lambda::Function
Condition: IfCreateIDC
Properties:
Handler: index.handler
Runtime: python3.12
Timeout: 300
Description: Create IDC Local Instance
Role: !GetAtt IDCLambdaExecutionRole.Arn
Code:
ZipFile: |
import json
import time
import boto3
import cfnresponse
sso_client = boto3.client('sso-admin')
id_store_client = boto3.client('identitystore')
GROUPS = ["Users", "Admins", "AllUsers"]
USERS = [
{
"user_name": "doit",
"email": "[email protected]",
"display_name": "doiT",
"given_name": "doit",
"last_name": "DoiT",
"groups": ["Admins", "AllUsers"]
},
{
"user_name": "didit",
"email": "[email protected]",
"display_name": "didiT",
"given_name": "didit",
"last_name": " DoiT",
"groups": ["Users", "AllUsers"]
},
{
"user_name": "doneit",
"email": "[email protected]",
"display_name": "doneiT",
"given_name": "doneit",
"last_name": "DoiT",
"groups": ["AllUsers"]
}
]
def handler(event, context):
try:
print('Received event: ' + json.dumps(event, indent=4, default=str))
if event['RequestType'] == 'Create':
print("Check for existing IDC Instance")
list_inst_response = sso_client.list_instances()
if list_inst_response["Instances"]:
print("IDC Instance exists. Skipping creation")
instance_arn = list_inst_response["Instances"][0]['InstanceArn']
cfnresponse.send(event, context, cfnresponse.SUCCESS, {'InstanceArn': instance_arn}, instance_arn)
return
print("Creating IDC Instance")
create_response = sso_client.create_instance()
instance_arn = create_response['InstanceArn']
identity_store_id = None
for idx in range(10):
instance_dsec = sso_client.describe_instance(InstanceArn=instance_arn)
print("IDC Instance:" + json.dumps(instance_dsec, indent=4, default=str))
if instance_dsec['Status'] != "ACTIVE":
time.sleep(10)
else:
identity_store_id = instance_dsec['IdentityStoreId']
break
if identity_store_id:
group_idx = {}
for group in GROUPS:
resp = id_store_client.create_group(
IdentityStoreId=identity_store_id,
DisplayName=group,
Description=group
)
print("Group Creation:" + json.dumps(resp, indent=4, default=str))
group_idx[group] = resp["GroupId"]
for user in USERS:
user_resp = id_store_client.create_user(
IdentityStoreId=identity_store_id,
UserName=user["user_name"],
DisplayName=user["display_name"],
Emails=[{"Value": user["email"], "Type": "Work", "Primary": True}],
Name={"GivenName": user["given_name"], "FamilyName": user["last_name"]}
)
print("User Creation:" + json.dumps(user_resp, indent=4, default=str))
# Assign groups
for grp in user["groups"]:
if grp in group_idx:
member_resp = id_store_client.create_group_membership(
IdentityStoreId=identity_store_id,
GroupId=group_idx[grp],
MemberId={"UserId": user_resp["UserId"]}
)
print("Group Member Creation:" + json.dumps(member_resp, indent=4, default=str))
cfnresponse.send(event, context, cfnresponse.SUCCESS, {'InstanceArn': instance_arn}, instance_arn)
else:
cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
print("done")
except Exception as e:
print(e)
cfnresponse.send(event, context, cfnresponse.FAILED, {})
IDCInstance:
Type: AWS::CloudFormation::CustomResource
Condition: IfCreateIDC
Properties:
ServiceToken: !GetAtt IDCLambda.Arn
S3Bucket:
Type: AWS::S3::Bucket
Properties:
PublicAccessBlockConfiguration:
BlockPublicAcls: Yes
BlockPublicPolicy: Yes
IgnorePublicAcls: Yes
RestrictPublicBuckets: Yes
DataSourceRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Sid: ''
Effect: Allow
Principal:
Service:
- qbusiness.amazonaws.com
Action: 'sts:AssumeRole'
Condition:
StringEquals:
"aws:SourceAccount": !Ref AWS::AccountId
ArnLike:
"aws:SourceArn": !Sub "arn:aws:qbusiness:${AWS::Region}:${AWS::AccountId}:application/*"
Policies:
- PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 's3:GetObject'
Resource: !Sub
- 'arn:aws:s3:::${bucket}/*'
- bucket: !Ref S3Bucket
- Effect: Allow
Action:
- 's3:ListBucket'
Resource: !Sub
- 'arn:aws:s3:::${bucket}'
- bucket: !Ref S3Bucket
- Effect: Allow
Action:
- 'qbusiness:BatchPutDocument'
- 'qbusiness:BatchDeleteDocument'
- 'qbusiness:PutGroup'
- 'qbusiness:CreateUser'
- 'qbusiness:DeleteGroup'
- 'qbusiness:UpdateUser'
- 'qbusiness:ListGroups'
Resource:
- !Sub "arn:aws:qbusiness:${AWS::Region}:${AWS::AccountId}:application/*"
- !Sub "arn:aws:qbusiness:${AWS::Region}:${AWS::AccountId}:application/*/index/*"
- !Sub "arn:aws:qbusiness:${AWS::Region}:${AWS::AccountId}:application/*/index/*/data-source/*"
- Effect: Allow
Action:
- 'secretsmanager:GetSecretValue'
Resource:
- !Sub "arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:secret_id"
- Effect: Allow
Action:
- 'kms:Decrypt'
Resource:
- !Sub "arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/key_id"
PolicyName: DataSourcePolicy
WebExperienceRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Sid: ''
Effect: Allow
Principal:
Service: application.qbusiness.amazonaws.com
Action:
- 'sts:AssumeRole'
- 'sts:SetContext'
Condition:
StringEquals:
"aws:SourceAccount": !Ref AWS::AccountId
ArnLike:
"aws:SourceArn": !Sub "arn:aws:qbusiness:${AWS::Region}:${AWS::AccountId}:application/*"
Policies:
- PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'qbusiness:Chat'
- 'qbusiness:ChatSync'
- 'qbusiness:ListMessages'
- 'qbusiness:ListConversations'
- 'qbusiness:DeleteConversation'
- 'qbusiness:PutFeedback'
- 'qbusiness:GetWebExperience'
- 'qbusiness:GetApplication'
- 'qbusiness:ListPlugins'
- 'qbusiness:GetChatControlsConfiguration'
Resource:
- !Sub "arn:aws:qbusiness:${AWS::Region}:${AWS::AccountId}:application/*"
- Effect: Allow
Action:
- 'kms:Decrypt'
Resource:
- !Sub "arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/key_id"
- Effect: Allow
Action:
- 'qapps:CreateQApp'
- 'qapps:PredictProblemStatementFromConversation'
- 'qapps:PredictQAppFromProblemStatement'
- 'qapps:CopyQApp'
- 'qapps:GetQApp'
- 'qapps:ListQApps'
- 'qapps:UpdateQApp'
- 'qapps:DeleteQApp'
- 'qapps:AssociateQAppWithUser'
- 'qapps:DisassociateQAppFromUser'
- 'qapps:ImportDocumentToQApp'
- 'qapps:ImportDocumentToQAppSession'
- 'qapps:CreateLibraryItem'
- 'qapps:GetLibraryItem'
- 'qapps:UpdateLibraryItem'
- 'qapps:CreateLibraryItemReview'
- 'qapps:ListLibraryItems'
- 'qapps:CreateSubscriptionToken'
- 'qapps:StartQAppSession'
- 'qapps:StopQAppSession'
Resource:
- !Sub "arn:aws:qbusiness:${AWS::Region}:${AWS::AccountId}:application/*"
PolicyName: WebExperiencePolicy
Parameters:
CreateIDC:
Type: String
Default: "no"
AllowedValues:
- "no"
- "yes"
Description: Whether this stack should create AWS IAM Identity Center instance and setup users/groups. Answer "Yes" only if the AWS account does not have IDC enabled and is allow listed to enable the same. If not sure, leave as default of "no".
Outputs:
S3BucketUrl:
Description: Your S3 bucket to be used as a data source
Value:
Fn::Join:
- ""
- - "https://console.aws.amazon.com/s3/home?bucket="
- !Ref S3Bucket
IDCInstanceArn:
Condition: IfCreateIDC
Description: AWS IAM Identity Center instance Arn
Value: !GetAtt IDCInstance.InstanceArn
S3BucketNameExport:
Description: The name of your S3 bucket
Value: !Ref S3Bucket
Export:
Name: "eq-infra-setup-S3BucketName"
DataSourceRole:
Description: The DataSourceRole Arn
Value: !GetAtt DataSourceRole.Arn
WebExperienceRole:
Description: The WebExperienceRole Arn
Value: !GetAtt WebExperienceRole.Arn