Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS SRA Terraform Edition #204

Merged
merged 59 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
4ef921e
Initial push for TF SRA
tnguye001 Jan 9, 2024
f2465f7
TF Lambda Handler
tnguye001 Jan 9, 2024
73233fb
sechub enablement; updates to vars, template config.tfvars, add vars …
liamschn Jan 10, 2024
5bc6324
update config.tfvars template
liamschn Jan 10, 2024
4da49e3
Update variables.tf - leading zero in acct IDs
liamschn Jan 10, 2024
0e532a0
TF Docs for modules
tnguye001 Jan 11, 2024
c18ada7
adding gitignore; updating module name for iam pwd policy
liamschn Jan 12, 2024
9543da8
merge conflict resolution
liamschn Jan 12, 2024
2cc61f6
updating vars
liamschn Jan 12, 2024
4cbde40
module name for pwd policy config
liamschn Jan 12, 2024
b952c6a
add handling of terraform
liamschn Jan 18, 2024
ac69f5f
add handling of terraform
liamschn Jan 18, 2024
c42caf1
add disable inspector param
liamschn Jan 19, 2024
4987597
update macie so it will disable properly
liamschn Jan 23, 2024
c993a3b
update cloudtrail so it will disable properly
liamschn Jan 23, 2024
3c1439c
rename disable_macie var
liamschn Jan 24, 2024
4ac6192
rename disable_macie var
liamschn Jan 24, 2024
fd25c03
rename disable_macie var
liamschn Jan 25, 2024
17534c8
update execution role
liamschn Jan 31, 2024
06a422b
updating documentation
liamschn Feb 2, 2024
fa0cfa9
updating documentation
liamschn Feb 2, 2024
91777ef
updating documentation
liamschn Feb 2, 2024
2854030
updating documentation
liamschn Feb 2, 2024
49e626c
updating documentation
liamschn Feb 2, 2024
bce87fa
updating documentation
liamschn Feb 2, 2024
413772d
updating documentation
liamschn Feb 2, 2024
253d630
updating documentation
liamschn Feb 2, 2024
570c8b3
updating documentation
liamschn Feb 2, 2024
c42a939
updating/adding documentation
liamschn Feb 5, 2024
6a48013
updating/adding documentation
liamschn Feb 5, 2024
c981ccc
updating/adding documentation
liamschn Feb 5, 2024
59602dc
adding macie documentation
liamschn Feb 5, 2024
6cdbdc8
updating inspector doc
liamschn Feb 5, 2024
de4594b
adding documentation
liamschn Feb 5, 2024
dd10f41
updating documentation
liamschn Feb 5, 2024
6cd5798
Merge branch 'main' into terraform
liamschn Feb 5, 2024
a3bbe24
updating documentation
liamschn Feb 5, 2024
7f63b0c
updating documentation
liamschn Feb 5, 2024
f2abdd5
updating documentation
liamschn Feb 5, 2024
e19fcb4
bandit findings
liamschn Feb 8, 2024
c3b89d2
bandit findings
liamschn Feb 8, 2024
2c8c941
mypy findings
liamschn Feb 8, 2024
90ee1b7
mypy findings
liamschn Feb 8, 2024
283255e
Added checkov suppression
tnguye001 Feb 8, 2024
cd1c092
Merge branch 'terraform' of github.com:tnguye001/aws-security-referen…
Feb 8, 2024
b54c859
flake8 findings
liamschn Feb 8, 2024
0f73249
flake8 findings
liamschn Feb 8, 2024
8200e5c
flake8 findings
liamschn Feb 8, 2024
eaa8ffc
flake8 findings
liamschn Feb 8, 2024
5081b2d
black formatter
liamschn Feb 8, 2024
519403a
More checkov scans supression
Feb 8, 2024
67acd96
flake8 findings
liamschn Feb 8, 2024
c42de21
isort findings
liamschn Feb 8, 2024
05cf285
checkov findings
liamschn Feb 9, 2024
50a1b97
checkov findings
liamschn Feb 9, 2024
24ec51d
checkov findings
liamschn Feb 9, 2024
7cecd50
updating variable defaults
liamschn Feb 9, 2024
cb96b68
same checkov change
liamschn Feb 9, 2024
d78b0bc
same checkov change
liamschn Feb 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,13 @@ $RECYCLE.BIN/
**/sra_staging_manual_upload/*
**/dist-*/*
poetry.lock

# Ignore packages generated by TF
**/aws_sra_examples/terraform/**/lambda/
**.terraform
*.tfstate
*.tfstate.*
*.tfvars*
**./terraform
*.hcl

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified aws_sra_examples/docs/artifacts/where-to-start-process.pptx
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,21 @@ def lambda_handler(event: CloudFormationCustomResourceEvent, context: Context) -
except Exception:
LOGGER.exception(UNEXPECTED)
raise ValueError(f"Unexpected error executing Lambda function. Review CloudWatch logs '{context.log_group_name}' for details.") from None


def terraform_handler(event: CloudFormationCustomResourceEvent, context: Context) -> None:
"""Lambda Handler.

Args:
event: event data
context: runtime information

Raises:
ValueError: Unexpected error executing Lambda function

"""
try:
process_event(event, context)
except Exception:
LOGGER.exception(UNEXPECTED)
raise ValueError(f"Unexpected error executing Lambda function. Review CloudWatch logs '{context.log_group_name}' for details.") from None
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ Resources:
Properties:
Name: sra/cloudtrail_org_s3_bucket
Description: Organization CloudTrail S3 Bucket
SecretString: !Sub '{"OrganizationCloudTrailS3Bucket":"${rOrgTrailBucket}"}' # checkov:skip=CKV_SECRET_6
SecretString: !Sub '{"OrganizationCloudTrailS3Bucket":"${rOrgTrailBucket}"}' # checkov:skip=CKV_SECRET_6
KmsKeyId: !Ref pSRASecretsKeyAliasArn
Tags:
- Key: sra-solution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Resources:
Properties:
Name: sra/cloudtrail_org_key_arn
Description: Organization CloudTrail KMS Key ARN
SecretString: !Sub '{"OrganizationCloudTrailKeyArn":"${rOrganizationCloudTrailKey.Arn}"}' # checkov:skip=CKV_SECRET_6
SecretString: !Sub '{"OrganizationCloudTrailKeyArn":"${rOrganizationCloudTrailKey.Arn}"}' # checkov:skip=CKV_SECRET_6
KmsKeyId: !Ref pSRASecretsKeyAliasArn
Tags:
- Key: sra-solution
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,25 @@ def lambda_handler(event: CloudFormationCustomResourceEvent, context: Context) -
except Exception:
LOGGER.exception(UNEXPECTED)
raise ValueError(f"See the details in CloudWatch Log Stream: '{context.log_group_name}'") from None


def terraform_handler(event: dict, context: Context) -> dict:
"""Lambda Handler.

Args:
event: event data
context: runtime information

Raises:
ValueError: Unexpected error executing Lambda function

Returns:
dict: status code
"""
try:
create_update_event(event, context)
response = {"event": event, "statusCode": 200}
return response
except Exception as error_msg:
LOGGER.exception(UNEXPECTED)
raise ValueError(f"See the details in CloudWatch Log Stream: '{context.log_group_name}' Error Message: {error_msg}") from None
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,28 @@ def lambda_handler(
except Exception:
LOGGER.exception(UNEXPECTED)
raise ValueError(f"Unexpected error executing Lambda function. Review CloudWatch logs '{context.log_group_name}' for details.") from None


def terraform_handler(event: dict, context: Context) -> None:
"""Lambda Handler.

Args:
event: event data
context: runtime information

Raises:
ValueError: Unexpected error executing Lambda function
"""
LOGGER.info("....Lambda Handler Started....")
event_info = {"Event": event}
LOGGER.info(event_info)
try:
request_type = event["RequestType"]

if request_type == "Create":
create(event, context)
elif request_type == "Delete":
delete(event, context)
except Exception:
LOGGER.exception(UNEXPECTED)
raise ValueError(f"Unexpected error executing Lambda function. Review CloudWatch logs '{context.log_group_name}' for details.") from None
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Resources:
Properties:
Name: sra/config_org_delivery_key_arn
Description: Config Delivery KMS Key ARN
SecretString: !Sub '{"ConfigDeliveryKeyArn":"${rConfigDeliveryKey.Arn}"}' # checkov:skip=CKV_SECRET_6
SecretString: !Sub '{"ConfigDeliveryKeyArn":"${rConfigDeliveryKey.Arn}"}' # checkov:skip=CKV_SECRET_6
KmsKeyId: !Ref pSRASecretsKeyAliasArn
Tags:
- Key: sra-solution
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,30 @@ def lambda_handler(event: Dict[str, Any], context: Context) -> None:
except Exception:
LOGGER.exception(UNEXPECTED)
raise ValueError(f"Unexpected error executing Lambda function. Review CloudWatch logs '{context.log_group_name}' for details.") from None


def terraform_handler(event: Dict[str, Any], context: Context) -> None:
"""Lambda Handler.

Args:
event: event data
context: runtime information

Raises:
ValueError: Unexpected error executing Lambda function
"""
LOGGER.info("....Lambda Handler Started....")
event_info = {"Event": event}
LOGGER.info(event_info)
try:
if "Records" not in event and "RequestType" not in event and ("source" not in event and event["source"] != "aws.controltower"):
raise ValueError(
f"The event did not include Records or RequestType. Review CloudWatch logs '{context.log_group_name}' for details."
) from None
elif "Records" in event and event["Records"][0]["EventSource"] == "aws:sns":
process_sns_records(event["Records"])
elif "RequestType" in event:
process_cloudformation_event(event, context)
except Exception:
LOGGER.exception(UNEXPECTED)
raise ValueError(f"Unexpected error executing Lambda function. Review CloudWatch logs '{context.log_group_name}' for details.") from None
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Resources:
Properties:
Name: sra/guardduty_org_delivery_key_arn
Description: GuardDuty Delivery KMS Key ARN
SecretString: !Sub '{"GuardDutyDeliveryKeyArn":"${rGuardDutyDeliveryKey.Arn}"}' # checkov:skip=CKV_SECRET_6
SecretString: !Sub '{"GuardDutyDeliveryKeyArn":"${rGuardDutyDeliveryKey.Arn}"}' # checkov:skip=CKV_SECRET_6
KmsKeyId: !Ref pSRASecretsKeyAliasArn
Tags:
- Key: sra-solution
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ def lambda_handler(event: CloudFormationCustomResourceEvent, context: Context) -

"""
try:
helper(event, context)
if event.get("ResourceType") == "Terraform":
process_cloudformation_event(event, context)
else:
helper(event, context)
except Exception:
LOGGER.exception(UNEXPECTED)
raise ValueError(f"Unexpected error executing Lambda function. Review CloudWatch logs '{context.log_group_name}' for details.") from None
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,14 @@ def process_event(event: dict) -> None:
excluded_accounts: list = [params["DELEGATED_ADMIN_ACCOUNT_ID"]]
accounts = common.get_active_organization_accounts(excluded_accounts)
regions = common.get_enabled_regions(params["ENABLED_REGIONS"], params["CONTROL_TOWER_REGIONS_ONLY"] == "true")

process_add_update_event(params, regions, accounts)
if event.get("ResourceType") == "Terraform" and event.get("tf", {}).get("action") == "delete":
LOGGER.info("...Disable Inspector from Terraform")
disabled_inspector_service(params, regions)
elif event.get("RequestType") == "Delete":
LOGGER.info("...Disable Inspector via process_event")
disabled_inspector_service(params, regions)
else:
process_add_update_event(params, regions, accounts)


def parameter_pattern_validator(parameter_name: str, parameter_value: Optional[str], pattern: str, is_optional: bool = False) -> dict:
Expand Down Expand Up @@ -314,10 +320,14 @@ def disabled_inspector_service(params: dict, regions: list) -> None:
params: Configuration Parameters
regions: list of regions
"""
scan_components = params["SCAN_COMPONENTS"].split(",")
LOGGER.info("Remove inspector")
LOGGER.info(f"disabled_inspector_service: ALL_INSPECTOR_SCAN_COMPONENTS as ({ALL_INSPECTOR_SCAN_COMPONENTS})")
LOGGER.info(f"disabled_inspector_service: scan_components as ({scan_components})")
inspector.disable_inspector_in_associated_member_accounts(
params["DELEGATED_ADMIN_ACCOUNT_ID"], params["CONFIGURATION_ROLE_NAME"], regions, ALL_INSPECTOR_SCAN_COMPONENTS
params["DELEGATED_ADMIN_ACCOUNT_ID"],
params["CONFIGURATION_ROLE_NAME"],
regions,
scan_components,
)

inspector.disable_auto_scanning_in_org(params["DELEGATED_ADMIN_ACCOUNT_ID"], params["CONFIGURATION_ROLE_NAME"], regions)
Expand All @@ -329,7 +339,8 @@ def disabled_inspector_service(params: dict, regions: list) -> None:
params["CONFIGURATION_ROLE_NAME"],
params["MANAGEMENT_ACCOUNT_ID"],
params["DELEGATED_ADMIN_ACCOUNT_ID"],
ALL_INSPECTOR_SCAN_COMPONENTS,
# ALL_INSPECTOR_SCAN_COMPONENTS,
scan_components,
)

deregister_delegated_administrator(params["DELEGATED_ADMIN_ACCOUNT_ID"], SERVICE_NAME)
Expand Down Expand Up @@ -540,8 +551,12 @@ def orchestrator(event: Dict[str, Any], context: Any) -> None:
context: runtime information
"""
if event.get("RequestType"):
LOGGER.info("...calling helper...")
helper(event, context)
if event.get("ResourceType") and event["ResourceType"] == "Terraform":
LOGGER.info("...calling process_event from Terraform...")
process_event(event)
else:
LOGGER.info("...calling helper...")
helper(event, context)
elif event.get("Records") and event["Records"][0]["EventSource"] == "aws:sns":
LOGGER.info("...aws:sns record...")
process_event_sns(event)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
27 changes: 27 additions & 0 deletions aws_sra_examples/solutions/macie/macie_org/lambda/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,30 @@ def lambda_handler(event: Dict[str, Any], context: Context) -> None:
except Exception:
LOGGER.exception(UNEXPECTED)
raise ValueError(f"Unexpected error executing Lambda function. Review CloudWatch logs '{context.log_group_name}' for details.") from None


def terraform_handler(event: Dict[str, Any], context: Context) -> None:
"""Lambda Handler.

Args:
event: event data
context: runtime information

Raises:
ValueError: Unexpected error executing Lambda function
"""
LOGGER.info("....Terraform Lambda Handler Started....")
event_info = {"Event": event}
LOGGER.info(event_info)
try:
if "Records" not in event and "RequestType" not in event and ("source" not in event and event["source"] != "aws.controltower"):
raise ValueError(
f"The event did not include Records, RequestType, or source. Review CloudWatch logs '{context.log_group_name}' for details."
) from None
elif "Records" in event and event["Records"][0]["EventSource"] == "aws:sns":
process_sns_records(event["Records"])
elif "RequestType" in event:
process_cloudformation_event(event, context)
except Exception:
LOGGER.exception(UNEXPECTED)
raise ValueError(f"Unexpected error executing Lambda function. Review CloudWatch logs '{context.log_group_name}' for details.") from None
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Resources:
Properties:
Name: sra/macie_org_delivery_key_arn
Description: Macie Delivery KMS Key ARN
SecretString: !Sub '{"MacieOrgDeliveryKeyArn":"${rMacieOrgDeliveryKey.Arn}"}' # checkov:skip=CKV_SECRET_6
SecretString: !Sub '{"MacieOrgDeliveryKeyArn":"${rMacieOrgDeliveryKey.Arn}"}' # checkov:skip=CKV_SECRET_6
KmsKeyId: !Ref pSRASecretsKeyAliasArn
Tags:
- Key: sra-solution
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,34 @@ def lambda_handler(event: Dict[str, Any], context: Any) -> None:
except Exception:
LOGGER.exception(UNEXPECTED)
raise ValueError(f"Unexpected error executing Lambda function. Review CloudWatch logs '{context.log_group_name}' for details.") from None


def terraform_handler(event: Dict[str, Any], context: Context) -> None:
"""Lambda Handler.

Args:
event: event data
context: runtime information

Raises:
ValueError: Unexpected error executing Lambda function
"""
LOGGER.info("....Lambda Handler Started....")
event_info = {"Event": event}
LOGGER.info(event_info)
try:
if "Records" not in event and "RequestType" not in event and ("source" not in event and event["source"] != "aws.controltower"):
raise ValueError(
f"The event did not include Records or RequestType. Review CloudWatch logs '{context.log_group_name}' for details."
) from None
elif event.get("source") == "aws.controltower":
process_event_lifecycle(event)
elif event.get("source") == "aws.organizations":
process_event_organizations(event)
elif event.get("Records") and event["Records"][0]["EventSource"] == "aws:sns":
process_event_sns(event)
else:
process_event(event)
except Exception:
LOGGER.exception(UNEXPECTED)
raise ValueError(f"Unexpected error executing Lambda function. Review CloudWatch logs '{context.log_group_name}' for details.") from None
Loading
Loading