-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
template.yaml
539 lines (489 loc) · 16.5 KB
/
template.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
---
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: |
This is used to keep your AWS IAM Identity Center (Successor to AWS Single Sign-On) groups and users in sync with your
Google Workspace directory using and AWS Lambda function.
Project URL: https://github.com/slashdevops/idp-scim-sync
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: "Lambda Function - Configuration"
Parameters:
- SyncMethod
- GWSGroupsFilter
- LogLevel
- LogFormat
- ScheduleExpression
- MemorySize
- Timeout
- LogGroupName
- LogGroupRetentionDays
- RoleNameSuffix
- Label:
default: "State File - Configuration"
Parameters:
- BucketNamePrefix
- BucketKey
- Label:
default: "Google Workspace - Credentials"
Parameters:
- GWSServiceAccountFile
- GWSServiceAccountFileSecretName
- GWSUserEmail
- GWSUserEmailSecretName
- Label:
default: "AWS Single Sign-On SCIM - Credentials"
Parameters:
- SCIMEndpoint
- SCIMEndpointSecretName
- SCIMAccessToken
- SCIMAccessTokenSecretName
AWS::ServerlessRepo::Application:
Name: idp-scim-sync
Description: |
keep your AWS Single Sign-On (SSO) groups and users in sync with your
Google Workspace directory using and AWS Lambda function.
Author: slashdevops idp-scim-sync Team
SpdxLicenseId: Apache-2.0
LicenseUrl: LICENSE
ReadmeUrl: README.md
Labels: # only 10 allowed
- Serverless
- SSO
- Lambda
- SCIM
- AWS
- Go
- Google-Workspace
- idp-scim-sync
- Groups
- Users
HomePageUrl: https://github.com/slashdevops/idp-scim-sync
SemanticVersion: 0.0.1 # will be overwritten by the CI/CD pipeline
SourceCodeUrl: https://github.com/slashdevops/idp-scim-sync
Parameters:
ScheduleExpression:
Type: String
Description: |
Schedule for trigger the execution of idp-scim-sync (see EventBridge rule schedule expressions)
Reference: https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html
Default: rate(15 minutes)
LogLevel:
Type: String
Description: Log level for Lambda function logging
Default: info
AllowedValues:
- panic
- fatal
- error
- warn
- info
- debug
- trace
LogFormat:
Type: String
Description: Log format for Lambda function logging
Default: json
AllowedValues:
- json
- text
BucketNamePrefix:
Type: String
Description: |
Prefix used in the bucket name where the state data will be stored.
NOTE: This template will use the 'AWS Region' and 'AWS Account ID' as a suffix to the 'bucket name' to avoid conflicts.
Example: 'BucketNamePrefix-AWS::AccountId-AWS::Region'
Default: idp-scim-sync-state
BucketKey:
Type: String
Description: |
The key "file" where the state data will be stored
Default: data/state.json
GWSServiceAccountFile:
Type: String
Description: |
The Google Workspace credentials file content (content of credentials.json after creates the service account: https://cloud.google.com/iam/docs/creating-managing-service-account-keys)
NoEcho: true
GWSServiceAccountFileSecretName:
Type: String
Description: |
The Google Workspace credentials file secret name
Default: IDPSCIM_GWSServiceAccountFile
GWSUserEmail:
Type: String
Description: |
The Google Workspace user email authorized on the creation creation of the service account
NoEcho: true
GWSUserEmailSecretName:
Type: String
Description: |
The Google Workspace user email secret name
Default: IDPSCIM_GWSUserEmail
SCIMEndpoint:
Type: String
Description: |
The AWS SSO SCIM Endpoint Url
Reference: https://docs.aws.amazon.com/singlesignon/latest/userguide/provision-automatically.html
NoEcho: true
SCIMEndpointSecretName:
Type: String
Description: |
The AWS SSO SCIM Endpoint Url secret name
Default: IDPSCIM_SCIMEndpoint
SCIMAccessToken:
Type: String
Description: |
The AWS SSO SCIM AccessToken
Reference: https://docs.aws.amazon.com/singlesignon/latest/userguide/provision-automatically.html
NoEcho: true
SCIMAccessTokenSecretName:
Type: String
Description: |
The AWS SSO SCIM AccessToken secret name
Default: IDPSCIM_SCIMAccessToken
GWSGroupsFilter:
Type: String
Description: |
The Google Workspace group filter query parameter, example: 'name:AWS* email:aws-*', see: https://developers.google.com/admin-sdk/directory/v1/guides/search-groups
Default: ""
SyncMethod:
Type: String
Description: |
The sync method to use
Default: groups
AllowedValues:
- groups
MemorySize:
Type: Number
Description: |
The amount of memory to allocate to the Lambda function.
The value must be a multiple of 64 MB.
NOTE: Your function is allocated CPU proportional to the memory configured.
Default: 256
MaxValue: 10240
MinValue: 128
Timeout:
Type: Number
Description: |
The amount of time that AWS Lambda service allows a function to run before terminating it.
The value must be greater than or equal to 60 seconds.
Default: 300
MaxValue: 900
MinValue: 3
LogGroupName:
Type: String
Description: The name of the CloudWatch log group
Default: "/aws/lambda/idp-scim-sync"
LogGroupRetentionDays:
Type: Number
Description: The number of days you want to keep logs for the created lambda function
Default: 7
AllowedValues:
- 1
- 3
- 5
- 7
- 14
- 30
- 60
- 90
- 120
- 150
- 180
- 365
- 400
- 545
- 731
- 1827
- 3653
Runtime:
Type: String
Description: |
The Lambda function runtime
Reference: https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html
Default: provided.al2
AllowedValues:
- provided.al2
Architecture:
Type: String
Description: |
The Lambda function architecture
Reference: https://docs.aws.amazon.com/lambda/latest/dg/foundation-arch.html#foundation-arch-adv
Default: arm64
AllowedValues:
- arm64
LambdaFunctionHandler:
Type: String
Description: |
The Lambda function handler
Reference: https://docs.aws.amazon.com/lambda/latest/dg/configuration-console.html
Default: bootstrap
RoleNameSuffix:
Type: String
Description: Suffix to apply to IAM to avoid conflicts when multiple stacks are running in the same account
Default: ""
LambdaFunctionName:
Type: String
Description: Name of the created Lambda function
Default: "idp-scim-sync"
Resources:
LambdaFunction:
Type: AWS::Serverless::Function
DependsOn:
- LambdaFunctionLogGroup
Properties:
FunctionName: !Ref LambdaFunctionName
Description: |
This Lambda function will sync the AWS SSO groups and users with the Google Workspace directory and it will be triggered by an EventBridge rule.
Project: https://github.com/slashdevops/idp-scim-sync
Runtime: !Ref Runtime
Architectures:
- !Ref Architecture
MemorySize: !Ref MemorySize
Handler: !Ref LambdaFunctionHandler # depends on the path inside .aws-sam/build/LambdaFunction/ during the build process using sam build
Timeout: !Ref Timeout
CodeUri: "."
Tracing: Active
Environment:
Variables:
IDPSCIM_LOG_LEVEL: !Ref LogLevel
IDPSCIM_LOG_FORMAT: !Ref LogFormat
IDPSCIM_SYNC_METHOD: !Ref SyncMethod
IDPSCIM_AWS_S3_BUCKET_NAME: !Sub "${BucketNamePrefix}-${AWS::AccountId}-${AWS::Region}"
IDPSCIM_AWS_S3_BUCKET_KEY: !Ref BucketKey
IDPSCIM_GWS_GROUPS_FILTER: !Ref GWSGroupsFilter
IDPSCIM_GWS_USER_EMAIL_SECRET_NAME: !Ref AWSGWSUserEmailSecret
IDPSCIM_GWS_SERVICE_ACCOUNT_FILE_SECRET_NAME: !Ref AWSGWSServiceAccountFileSecret
IDPSCIM_AWS_SCIM_ENDPOINT_SECRET_NAME: !Ref AWSSCIMEndpointSecret
IDPSCIM_AWS_SCIM_ACCESS_TOKEN_SECRET_NAME: !Ref AWSSCIMAccessTokenSecret
Role: !GetAtt LambdaFunctionRole.Arn
Events:
SyncScheduledEvent:
Type: Schedule
Name: AWSSyncSchedule
Properties:
Enabled: true
Schedule: !Ref ScheduleExpression
Metadata: # https://jiangsc.me/2021/01/24/Lessons-Learnt-On-Deploying-GO-Lambda-Application-on-AWS/ and https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/building-custom-runtimes.html
BuildMethod: makefile
LambdaFunctionRole:
Type: AWS::IAM::Role
Properties:
Description: !Sub "This role is used by the Lambda function ${AWS::StackName}"
RoleName: !Sub "serverless-idp-scim-sync-${AWS::AccountId}-${AWS::Region}${RoleNameSuffix}"
Path: /
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Action: sts:AssumeRole
Principal:
Service: "lambda.amazonaws.com"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess
Policies:
- PolicyName: CustomLambdaPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: SSMGetParameterPolicy
Effect: Allow
Action:
- secretsmanager:GetResourcePolicy
- secretsmanager:GetSecretValue
Resource:
- !Ref AWSGWSServiceAccountFileSecret
- !Ref AWSGWSUserEmailSecret
- !Ref AWSSCIMEndpointSecret
- !Ref AWSSCIMAccessTokenSecret
- Sid: S3Policy
Effect: Allow
Action:
- s3:GetObject
- s3:GetObjectAcl
- s3:GetObjectVersion
- s3:PutObject
- s3:PutObjectAcl
- s3:ListBucket
Resource:
- !Sub "arn:aws:s3:::${BucketNamePrefix}-${AWS::AccountId}-${AWS::Region}"
- !Sub "arn:aws:s3:::${BucketNamePrefix}-${AWS::AccountId}-${AWS::Region}/*"
- Sid: KMSGetDataPolicy
Effect: Allow
Action:
- kms:GenerateDataKeyPair
Resource:
- !GetAtt KMSKey.Arn
- Sid: KMSDecryptPolicy
Effect: Allow
Action:
- kms:Decrypt
- kms:Encrypt
- kms:GenerateDataKey
Resource:
- !GetAtt KMSKey.Arn
AWSGWSServiceAccountFileSecret:
Type: AWS::SecretsManager::Secret
Properties:
Name: !Ref GWSServiceAccountFileSecretName
SecretString: !Ref GWSServiceAccountFile
AWSGWSUserEmailSecret:
Type: AWS::SecretsManager::Secret
Properties:
Name: !Ref GWSUserEmailSecretName
SecretString: !Ref GWSUserEmail
AWSSCIMEndpointSecret:
Type: AWS::SecretsManager::Secret
Properties:
Name: !Ref SCIMEndpointSecretName
SecretString: !Ref SCIMEndpoint
AWSSCIMAccessTokenSecret:
Type: AWS::SecretsManager::Secret
Properties:
Name: !Ref SCIMAccessTokenSecretName
SecretString: !Ref SCIMAccessToken
KMSKey:
Type: AWS::KMS::Key
Properties:
Description: ipd-scim-sync state bucket kms key.
KeyPolicy:
Version: 2012-10-17
Id: ipd-scim-sync-state-bucket-key-policy
Statement:
- Sid: AllowIAMThisAccount
Effect: Allow
Principal:
AWS: !Sub "arn:aws:iam::${AWS::AccountId}:root"
Action: "kms:*"
Resource: "*"
- Sid: AllowAWSLambdaToRetrieveKMSKey
Effect: Allow
Principal:
Service: "lambda.amazonaws.com"
#AWS: !GetAtt LambdaFunctionRole.Arn # Fails because circular reference
#AWS: !Sub "arn:aws:iam::${AWS::AccountId}:role/serverless-idp-scim-sync-${AWS::AccountId}-${AWS::Region}" # Fails in runtime because the roles is not created yet
Action:
- kms:Encrypt
- kms:Decrypt
- kms:ReEncrypt*
- kms:GenerateDataKey*
- kms:DescribeKey
Resource: "*"
Condition:
StringEquals:
kms:CallerAccount: !Ref "AWS::AccountId"
KMSKeyAlias:
Type: AWS::KMS::Alias
Properties:
AliasName: !Sub "alias/${BucketNamePrefix}-${AWS::AccountId}-${AWS::Region}"
TargetKeyId:
Ref: KMSKey
Bucket:
DeletionPolicy: Delete
UpdateReplacePolicy: Retain
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "${BucketNamePrefix}-${AWS::AccountId}-${AWS::Region}"
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
KMSMasterKeyID: !Sub "arn:aws:kms:${AWS::Region}:${AWS::AccountId}:${KMSKeyAlias}"
SSEAlgorithm: "aws:kms"
BucketKeyEnabled: true # https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucket-key.html
BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref Bucket
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: AllowAWSLambdaFunction
Principal:
AWS:
- !Sub "arn:aws:iam::${AWS::AccountId}:role/serverless-idp-scim-sync-${AWS::AccountId}-${AWS::Region}${RoleNameSuffix}"
Effect: Allow
Action:
- s3:GetObject
- s3:GetObjectAcl
- s3:PutObject
- s3:PutObjectAcl
- s3:GetObjectVersion
Resource:
- !Sub "arn:${AWS::Partition}:s3:::${Bucket}/*"
- Sid: AllowSSLRequestsOnly
Principal: "*"
Action:
- s3:*
Effect: Deny
Resource:
- !Sub "arn:${AWS::Partition}:s3:::${Bucket}"
- !Sub "arn:${AWS::Partition}:s3:::${Bucket}/*"
Condition:
Bool:
"aws:SecureTransport": false
# NOTE: To implement the following policies is necessary to send the objet encrypted to the bucket
# and the bucket is already implementing "BucketKeyEnabled", so looks like it is not necessary
# but the problem is the AWS Config rule that want these enabled. Let's see in the future if
# I will implement these policies.
# - Sid: DenyPublishingUnencryptedResources
# Effect: Deny
# Principal: "*"
# Action:
# - s3:PutObject
# Resource: !Sub "arn:${AWS::Partition}:s3:::${Bucket}/*"
# Condition:
# "Null":
# "s3:x-amz-server-side-encryption": true
# - Sid: DenyUnEncryptedObjectUploads
# Principal: "*"
# Action: s3:PutObject
# Effect: Deny
# Resource: !Sub "arn:${AWS::Partition}:s3:::${Bucket}/*"
# Condition:
# "StringNotEquals":
# "s3:x-amz-server-side-encryption": "aws:kms"
LambdaFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Ref LogGroupName
RetentionInDays: !Ref LogGroupRetentionDays
Outputs:
LambdaFunctionArn:
Value: !GetAtt LambdaFunction.Arn
Description: >
The ARN of the Lambda function
LambdaFunctionName:
Value: !Ref LambdaFunction
Description: >
The name of the Lambda function
LambdaFunctionHandler:
Value: !Ref LambdaFunctionHandler
Description: >
The handler of the Lambda function
BucketName:
Value: !Ref Bucket
Description: >
The name of the S3 bucket
BucketArn:
Value: !GetAtt Bucket.Arn
Description: >
The ARN of the S3 bucket
KMSKeyId:
Value: !Ref KMSKey
Description: >
The ID of the KMS key
KMSKeyArn:
Value: !GetAtt KMSKey.Arn
Description: >
The ARN of the KMS key
Architecture:
Value: !Ref Architecture
Description: >
The Lambda function architecture