Skip to content

Commit 8cdafbf

Browse files
IevIeievgeniia ieromenko
and
ievgeniia ieromenko
authoredMar 14, 2025··
Adding Security Lake resource management service-linked role (#289)
* adding resource management slr * account_alternate_contacts mypy fixes --------- Co-authored-by: ievgeniia ieromenko <[email protected]>
1 parent a771873 commit 8cdafbf

File tree

8 files changed

+138
-8
lines changed

8 files changed

+138
-8
lines changed
 

‎aws_sra_examples/solutions/account/account_alternate_contacts/lambda/src/app.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from aws_lambda_typing.context import Context
2525
from aws_lambda_typing.events import CloudFormationCustomResourceEvent
2626
from mypy_boto3_account import AccountClient
27-
from mypy_boto3_account.type_defs import DeleteAlternateContactRequestRequestTypeDef, PutAlternateContactRequestRequestTypeDef
27+
from mypy_boto3_account.type_defs import DeleteAlternateContactRequestTypeDef, PutAlternateContactRequestTypeDef
2828
from mypy_boto3_organizations import OrganizationsClient
2929
from mypy_boto3_organizations.type_defs import AccountTypeDef, DescribeAccountResponseTypeDef, TagTypeDef
3030
from mypy_boto3_sns import SNSClient
@@ -156,7 +156,7 @@ def add_alternate_contact(
156156
phone: Phone number for the alternate contact
157157
title: Title for the alternate contact
158158
"""
159-
contact_parameters: PutAlternateContactRequestRequestTypeDef = {
159+
contact_parameters: PutAlternateContactRequestTypeDef = {
160160
"AlternateContactType": contact_type,
161161
"EmailAddress": email,
162162
"Name": name,
@@ -178,7 +178,7 @@ def delete_alternate_contact(
178178
aws_account: AWS account to update
179179
contact_type: Alternate contact type you want to update
180180
"""
181-
contact_parameters: DeleteAlternateContactRequestRequestTypeDef = {"AlternateContactType": contact_type}
181+
contact_parameters: DeleteAlternateContactRequestTypeDef = {"AlternateContactType": contact_type}
182182
try:
183183
account_client.delete_alternate_contact(**contact_parameters)
184184
LOGGER.info(f"Deleted {contact_type} Alternate Contact for account: {aws_account['Id']} ({aws_account['Name']})")

‎aws_sra_examples/solutions/security_lake/security_lake_org/lambda/src/app.py

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
BOTO3_CONFIG = Config(retries={"max_attempts": 10, "mode": "standard"})
3838
UNEXPECTED = "Unexpected!"
3939
SERVICE_NAME = "securitylake.amazonaws.com"
40+
RESOURCE_MGMT_SERVICE_NAME = "resource-management.securitylake.amazonaws.com"
41+
SLR_NAME = "AWSServiceRoleForSecurityLakeResourceManagement"
4042
HOME_REGION = ssm.get_home_region()
4143
AUDIT_ACCT_ID = ssm.get_security_acct()
4244
AWS_LOG_SOURCES = ["ROUTE53", "VPC_FLOW", "SH_FINDINGS", "CLOUD_TRAIL_MGMT", "LAMBDA_EXECUTION", "S3_DATA", "EKS_AUDIT", "WAF"]
@@ -388,6 +390,14 @@ def add_log_sources(params: dict, regions: list, org_accounts: dict) -> None:
388390
aws_log_sources.append(configurations)
389391
if aws_log_sources:
390392
security_lake.add_aws_log_source(sl_client, aws_log_sources)
393+
for region in regions:
394+
formatted_region = region.replace("-", "_")
395+
lf_client = delegated_admin_session.client("lakeformation", region)
396+
principal_identifier = (
397+
f"arn:{PARTITION}:iam::{params['DELEGATED_ADMIN_ACCOUNT_ID']}:role/aws-service-role/{RESOURCE_MGMT_SERVICE_NAME}/{SLR_NAME}"
398+
)
399+
db_name = f"amazon_security_lake_glue_db_{formatted_region}"
400+
security_lake.set_lake_formation_permissions_for_slr(lf_client, params["DELEGATED_ADMIN_ACCOUNT_ID"], principal_identifier, db_name)
391401

392402

393403
def update_log_sources(params: dict, regions: list, org_accounts: dict) -> None:

‎aws_sra_examples/solutions/security_lake/security_lake_org/lambda/src/security_lake.py

+29
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,35 @@ def create_table_in_data_catalog(glue_client: GlueClient, shared_db_name: str, s
897897
raise ValueError(f"Error calling glue:CreateTable {e}") from None
898898

899899

900+
def set_lake_formation_permissions_for_slr(lf_client: LakeFormationClient, account: str, principal_identifier: str, db_name: str) -> None:
901+
"""Set Lake Formation permissions.
902+
903+
Args:
904+
lf_client: boto3 client
905+
account: AWS account
906+
principal_identifier: data lake principal identifier
907+
db_name: database name
908+
909+
Raises:
910+
ClientError: If there is an issue interacting with the AWS API
911+
912+
"""
913+
LOGGER.info(f"Setting lakeformation permissions for '{db_name}'")
914+
try:
915+
resource: Union[ResourceTypeDef] = {
916+
"Table": {"CatalogId": account, "DatabaseName": db_name, "TableWildcard": {}},
917+
}
918+
lf_client.grant_permissions(
919+
CatalogId=account,
920+
Principal={"DataLakePrincipalIdentifier": principal_identifier},
921+
Resource=resource,
922+
Permissions=["ALTER", "DESCRIBE"],
923+
)
924+
except ClientError as e:
925+
LOGGER.error(f"Error calling GrantPermissions {e}.")
926+
raise
927+
928+
900929
def set_lake_formation_permissions(lf_client: LakeFormationClient, account: str, db_name: str) -> None:
901930
"""Set Lake Formation permissions.
902931

‎aws_sra_examples/solutions/security_lake/security_lake_org/templates/sra-security-lake-org-configuration-role.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ Resources:
182182
Condition:
183183
StringLike:
184184
ram:ResourceShareName: !Sub "*-${pAuditAccountQuerySubscriberExternalId}"
185+
- PolicyName: sra-security-lake-org-policy-slr
186+
PolicyDocument:
187+
Version: 2012-10-17
188+
Statement:
189+
- Sid: AllowGrantPermissions
190+
Effect: Allow
191+
Action:
192+
- lakeformation:GrantPermissions
193+
- glue:GetDatabase
194+
- glue:GetTable
195+
Resource: "*"
185196
Tags:
186197
- Key: sra-solution
187198
Value: !Ref pSRASolutionName

‎aws_sra_examples/solutions/security_lake/security_lake_org/templates/sra-security-lake-org-configuration.yaml

+7-3
Original file line numberDiff line numberDiff line change
@@ -472,17 +472,21 @@ Resources:
472472
Resource: "*"
473473
- Sid: AllowCreateServiceLinkedRole
474474
Effect: Allow
475-
Action: iam:CreateServiceLinkedRole
475+
Action:
476+
- iam:CreateServiceLinkedRole
477+
- iam:GetRole
476478
Condition:
477479
StringLike:
478-
iam:AWSServiceName: securitylake.amazonaws.com
480+
iam:AWSServiceName:
481+
- securitylake.amazonaws.com
482+
- resource-management.securitylake.amazonaws.com
479483
Resource: "*"
480484
- Sid: SecurityLakeRemoveAdministratorAccess
481485
Effect: Allow
482486
Action:
483487
- organizations:DeregisterDelegatedAdministrator
484488
Resource: "*"
485-
- PolicyName: sra-account-alternate-contacts-policy-organizations
489+
- PolicyName: sra-security-lake-policy-organizations
486490
PolicyDocument:
487491
Version: 2012-10-17
488492
Statement:

‎aws_sra_examples/solutions/security_lake/security_lake_org/templates/sra-security-lake-org-kms-key.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ Resources:
122122
- kms:Decrypt
123123
Resource: '*'
124124
- !Ref AWS::NoValue
125+
- Sid: Allow SLR
126+
Effect: Allow
127+
Principal:
128+
AWS: !Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:role/aws-service-role/resource-management.securitylake.amazonaws.com/AWSServiceRoleForSecurityLakeResourceManagement'
129+
Action:
130+
- kms:Decrypt
131+
- kms:GenerateDataKey*
132+
Resource: '*'
133+
Condition:
134+
StringLike:
135+
'kms:EncryptionContext:aws:s3:arn':
136+
- !Sub 'arn:${AWS::Partition}:s3:::aws-security-data-lake-${AWS::Region}*'
137+
'kms:ViaService': !Sub 's3.${AWS::Region}.amazonaws.com'
125138
Tags:
126139
- Key: sra-solution
127140
Value: !Ref pSRASolutionName

‎aws_sra_examples/solutions/security_lake/security_lake_org/templates/sra-security-lake-org-main-ssm.yaml

+46-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Metadata:
3131
- pDisableSecurityLake
3232
- pControlTowerRegionsOnly
3333
- pEnabledRegions
34+
- pCreateAWSServiceRoleForSecurityLakeResourceManagementSlr
3435
- pSecurityLakeOrgKeyAlias
3536
- pSecurityLakeWarning
3637
- pSRASecurityLakeMetaStoreManagerRoleName
@@ -93,6 +94,8 @@ Metadata:
9394
default: CloudTrail - Lambda Data Events (recommended)
9495
pCloudTrailS3DataEvents:
9596
default: CloudTrail - S3 Data Events (high volume data)
97+
pCreateAWSServiceRoleForSecurityLakeResourceManagementSlr:
98+
default: Create AWS Service Role for Security Lake Resource Management.
9699
pCustomerControlTowerRegions:
97100
default: Customer Regions
98101
pSecurityHubFindings:
@@ -167,6 +170,11 @@ Metadata:
167170
default: Lambda Role Name
168171

169172
Parameters:
173+
pCreateAWSServiceRoleForSecurityLakeResourceManagementSlr:
174+
AllowedValues: ['true', 'false']
175+
Default: 'true'
176+
Description: Indicates whether to create a AWSServiceRoleForSecurityLakeResourceManagement service-linked role. Select True if this role does not exist in the Log Archive account
177+
Type: String
170178
pSecurityLakeOrgLambdaRoleName:
171179
AllowedPattern: '^[\w+=,.@-]{1,64}$'
172180
ConstraintDescription: Max 64 alphanumeric characters. Also special characters supported [+, =, ., @, -]
@@ -419,6 +427,9 @@ Conditions:
419427
cCreateLakeFormationSlr: !Equals
420428
- !Ref pCreateLakeFormationSlr
421429
- 'true'
430+
cAWSServiceRoleForSecurityLakeResourceManagement: !Equals
431+
- !Ref pCreateAWSServiceRoleForSecurityLakeResourceManagementSlr
432+
- 'true'
422433

423434
Rules:
424435
VerifySecurityLakeDisclaimer:
@@ -598,9 +609,42 @@ Resources:
598609
- Key: sra-solution
599610
Value: !Ref pSRASolutionName
600611

612+
613+
rAWSServiceRoleForSecurityLakeResourceManagementSLRStackSet:
614+
Type: AWS::CloudFormation::StackSet
615+
DeletionPolicy: Retain
616+
UpdateReplacePolicy: Retain
617+
Condition: cAWSServiceRoleForSecurityLakeResourceManagement
618+
Properties:
619+
StackSetName: sra-security-lake-resource-management-slr
620+
AdministrationRoleARN: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${pStackSetAdminRole}
621+
CallAs: SELF
622+
Capabilities:
623+
- CAPABILITY_NAMED_IAM
624+
Description: !Sub ${pSRASolutionVersion} - Deploys AWS Lake Formation service-linked role via ${pSRASolutionName}
625+
ExecutionRoleName: !Ref pStackExecutionRole
626+
ManagedExecution:
627+
Active: true
628+
OperationPreferences:
629+
FailureTolerancePercentage: 0
630+
MaxConcurrentPercentage: 100
631+
RegionConcurrencyType: PARALLEL
632+
PermissionModel: SELF_MANAGED
633+
StackInstancesGroup:
634+
- DeploymentTargets:
635+
Accounts:
636+
- !Ref pLogArchiveAccountId
637+
Regions:
638+
- !Ref AWS::Region
639+
TemplateURL: !Sub https://${pSRAStagingS3BucketName}.s3.${AWS::Region}.${AWS::URLSuffix}/${pSRASolutionName}/templates/sra-service-role-for-asl-resource-management.yaml
640+
Tags:
641+
- Key: sra-solution
642+
Value: !Ref pSRASolutionName
643+
601644
rSecurityLakeKMSKeyStackSet:
602645
Type: AWS::CloudFormation::StackSet
603-
DependsOn: rSecurityLakeConfigurationIAMRoleStackSet
646+
DependsOn:
647+
- rSecurityLakeConfigurationIAMRoleStackSet
604648
DeletionPolicy: Retain
605649
UpdateReplacePolicy: Retain
606650
Properties:
@@ -644,7 +688,7 @@ Resources:
644688

645689
rSecurityLakeMetaStoreManagerIAMRoleStackSet:
646690
Type: AWS::CloudFormation::StackSet
647-
DeletionPolicy: Delete
691+
DeletionPolicy: Retain
648692
UpdateReplacePolicy: Delete
649693
Properties:
650694
StackSetName: sra-security-lake-meta-store-manager-role
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
########################################################################
2+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
# SPDX-License-Identifier: MIT-0
4+
########################################################################
5+
AWSTemplateFormatVersion: 2010-09-09
6+
Description:
7+
This template creates an IAM role to configure the delegated administrator account - - 'security_lake_org' solution in the repo,
8+
https://github.com/aws-samples/aws-security-reference-architecture-examples (sra-1u3sd7f8p)
9+
10+
Metadata:
11+
SRA:
12+
Version: 1.0
13+
Order: 2
14+
15+
Resources:
16+
rAWSServiceRoleForSecurityLakeResourceManagementSLR:
17+
Type: AWS::IAM::ServiceLinkedRole
18+
Properties:
19+
AWSServiceName: resource-management.securitylake.amazonaws.com

0 commit comments

Comments
 (0)
Please sign in to comment.