Skip to content

Commit 5f3372f

Browse files
committed
Merge branch 'main' of github.com:aws-samples/preventive-security-controls-in-pulumi-iac-pipeline into main
2 parents 299a82a + 8bc5b20 commit 5f3372f

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,24 @@ The preventive controls and policies have been written as code and stored in the
3939

4040
#### Packaging & Distributing Pulumi CrossGuard Custom Policy packs using AWS CodeArtifact
4141
The folder named *customer-policy-crossguard-pkg* contains the code and the documents required for packaging a Python project into a package that can be easily distributed, without having to copy the source code. This folder needs to be uploaded into a CodeCommit repository, and future changes and distributions managed from there. Refer to this [link](https://docs.aws.amazon.com/codecommit/latest/userguide/how-to-create-repository.html) for more information on how to create a CodeCommit repository. This repository is maintained typically by the Security Engineering team.
42+
43+
Example how to create CodeCommit repository with AWS CLI:
44+
```bash
45+
aws codecommit create-repository --repository-name custom-policy-crossguard-pkg --repository-description "Pulumi CrossGuard Policies repository"
46+
```
47+
48+
Refer to the detailed instructions how to create [a CodeArtifact domain](https://docs.aws.amazon.com/codeartifact/latest/ug/domain-create.html) and [a CodeArtifact repository](https://docs.aws.amazon.com/codeartifact/latest/ug/create-repo.html) in the domain. Example how to create CodeArtifact domain and repository using AWS CLI:
49+
```bash
50+
aws codeartifact create-domain --domain <domain-name>
51+
aws codeartifact create-repository --domain <domain-name> --domain-owner <aws-account-id> --repository <repository-name> --description "Pulumi CrossGuard policies packages"
52+
```
53+
4254
The file *setup.cfg* contains the details of the package including the name and version number. Within the *src/* folder is the project folder that will be distributed as a package to be installed via pip.
4355

4456
To generate the distribution packages, the following commands are to be run from the same folder where *pyproject.toml* is located.
4557

4658
```bash
47-
python3 -m pip install upgrade build
59+
python3 -m pip install --upgrade build
4860
python3 -m build
4961
```
5062

@@ -60,18 +72,18 @@ To upload the generated distribution archives, we use the twine package. The ins
6072

6173
```bash
6274
python3 -m pip install --upgrade twine
63-
aws codeartifact login --tool twine --repository pulumi --domain pac --domain-owner <account Id>
75+
aws codeartifact login --tool twine --repository <repository-name> --domain <domain-name> --domain-owner <aws-account-id>
6476
export TWINE_USERNAME=aws
65-
export TWINE_PASSWORD=`aws codeartifact get-authorization-token --domain acme --domain-owner <account Id> --query authorizationToken --output text`
66-
export TWINE_REPOSITORY_URL=`aws codeartifact get-repository-endpoint --domain acme --domain-owner <account Id> --repository custompolicypack --format pypi --query repositoryEndpoint --output text`
67-
python3 -m twine upload --repository pulumi dist/*
77+
export TWINE_PASSWORD=`aws codeartifact get-authorization-token --domain <domain-name> --domain-owner <aws-account-id> --query authorizationToken --output text`
78+
export TWINE_REPOSITORY_URL=`aws codeartifact get-repository-endpoint --domain <domain-name> --domain-owner <aws-account-id> --repository <repository-name> --format pypi --query repositoryEndpoint --output text`
79+
python3 -m twine upload --repository <repository-name> dist/*
6880
```
6981

7082
This will upload the package to AWS CodeArtifact. This method allows for managing versions of the policy-as-code easily.
7183
To download the package and use it within the pipeline for policy enforcement, some additional commands are to be added into the *buildspec.yml* file for the pipeline. Specifically, these following command connects to the CodeArtifact repository and enable pip to download our custom package later. A sample buildspec file has been provided here *sample-code/sample-build-file/buildspec.yaml*.
7284

7385
```bash
74-
aws codeartifact login --tool pip --repository pulumi --domain pac --domain-owner <account Id>
86+
aws codeartifact login --tool pip --repository <repository-name> --domain <domain-name> --domain-owner <aws-account-id>
7587
```
7688

7789
The preventative checks in the custom policy package cover the following items mapped to checks from *[Prowler](https://github.com/prowler-cloud/prowler)*. Prowler is an open-source security tool to perform AWS security best practices assessments, audits, incident response, continuous monitoring, hardening and forensics readiness. It contains more than 200 controls covering CIS, PCI-DSS, ISO27001, GDPR, HIPAA, FFIEC, SOC2, AWS FTR, ENS and custom security frameworks.

custom-policy-crossguard-pkg/pyawsguard/src/pyawsguard/ec2_checks.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import datetime
3131
import os
3232

33-
from pyawsguard.metric_object import metric
34-
3533

3634
###################################
3735
# EC2 - Security Groups

custom-policy-crossguard-pkg/pyawsguard/src/pyawsguard/s3_checks.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,11 @@ def s3_public_access_block_validator(args: ResourceValidationArgs, report_violat
5454
# S3 Bucket policy validator
5555
def s3_ssl_requests_validator(args: ResourceValidationArgs, report_violation: ReportViolation):
5656
if args.resource_type == "aws:s3/bucketPolicy:BucketPolicy":
57-
f = open('data.txt', 'w')
5857
if "policy" in args.props:
5958
flag = 0
6059
policy = json.loads(args.props["policy"])
61-
if "Statement" in policy:
60+
if 'Statement' in policy:
6261
for stmt in policy["Statement"]:
63-
print(stmt["Condition"])
64-
print(stmt["Condition"]["Bool"])
6562
if "Condition" in stmt and "Bool" in stmt["Condition"] and "aws:SecureTransport" in stmt["Condition"]["Bool"] and stmt["Condition"]["Bool"]["aws:SecureTransport"] == "false":
6663
flag = 1
6764
break

sample-code/sample-build-file/buildspec.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ phases:
4242
#
4343
# Static Analysis
4444
#
45-
45+
46+
#
47+
# NOTE: When setting up an AWS CodeBuild project, make sure that sample-code/sample-resources/ is passed in
48+
# as the Source Directory to AWS CodeBuild. This can be verified by echoing the value of the
49+
# environment variable 'CODEBUILD_SRC_DIR'
50+
#
4651
- cd $CODEBUILD_SRC_DIR/resources
4752

4853
# Bandit
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
pulumi-policy==1.4.0
2-
pulumi-aws==4.33.1
1+
pulumi_policy>=1.5.0,<2.0.0clear
2+
pulumi-aws>=4.0.0,<5.0.0
33
## Please add the name of the custom package deployed to AWS codeArtifact

0 commit comments

Comments
 (0)