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

Lambda tests #2

Open
wants to merge 43 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
6f66cac
Test Guard Lambda by deploying to test account
Nov 9, 2022
78124b7
Changed version for checkout action and updated name for step
Nov 9, 2022
4f5ae19
Added token ID
Nov 9, 2022
93f53aa
Moved token ID to correct job
Nov 9, 2022
cd9f7bf
Removed delete commands at the beginning
Nov 9, 2022
3984f63
Added managed role policy for Lambda
Nov 9, 2022
749b3aa
Added a sleep before creating lambda
Nov 9, 2022
16fa70d
Added condition for clean up resources step to be run, added arg for …
Nov 9, 2022
0d62887
Detach policy statement added
Nov 10, 2022
581c66e
Added debug for outputs
Nov 10, 2022
729c6f9
Persisting lambda to debug
Nov 10, 2022
87f9803
Bump up simple_logger version
Nov 10, 2022
23a4f12
Updated the readme for variable names and added a note for format flag
Nov 10, 2022
5d0d0b7
Added commit has to name of resources
Nov 10, 2022
b5b7283
Removed redundant variables
Nov 10, 2022
3e60f4c
Moved generate identifiers to a different step
Nov 10, 2022
61bd145
Changed trigger branch
Nov 10, 2022
06cf806
Test OIDC connection
Mar 1, 2023
f3ec9ce
Added enviroment
Mar 1, 2023
7426dc3
Added environment secrets
Mar 1, 2023
65af3b7
Merge branch 'main-fork' into lambda_tests
Mar 1, 2023
46fe8ac
Merged with main
Mar 1, 2023
a056fe3
Test commit to trigger workflow
Mar 1, 2023
92ffb88
Testing with PR target trigger
Mar 1, 2023
e14e406
Changed the trigger back
Mar 1, 2023
158d2d4
Removing role and account from secrets
Mar 6, 2023
d55e1d6
Changing permissions
Mar 7, 2023
ccc31cb
Revert "Changing permissions"
Mar 7, 2023
3dcb0a1
Logging
Mar 7, 2023
7f43cfb
Changed autogenerated names
Mar 7, 2023
cd5c7c0
Added buildspec for codebuild
Mar 8, 2023
3771223
Added PR target
Mar 9, 2023
7c93d05
Remove label and cat readme
Mar 9, 2023
42140c6
Added secrets back
Mar 9, 2023
3e6d890
Removed debugging
Mar 9, 2023
0fa189d
Using environment files instead of set output
Mar 9, 2023
9ea5f0c
Revert "Using environment files instead of set output"
Mar 9, 2023
53e0231
Added debugging
Mar 9, 2023
44b100e
Separated actions
Mar 9, 2023
bdb0217
Merge branch 'main-fork' into lambda_tests
Mar 9, 2023
434b866
Added name to workflow and license for new action
Mar 10, 2023
dcce767
Upgraded version for configure AWS creds action and added remote bran…
Mar 16, 2023
a6113ef
Removed the remote branch after testing
Mar 16, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Rust
name: Build & Test

on:
push:
Expand Down
143 changes: 143 additions & 0 deletions .github/workflows/pr_lambda_integration_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
name: Integration Tests

on:
pull_request_target:
branches: [main, development]
types: [labeled, opened, synchronize]

permissions:
pull-requests: write
id-token: write
contents: read

env:
CARGO_TERM_COLOR: always

jobs:
remove-label:
name: Remove 'run-tests' label
runs-on: ubuntu-latest
if: |
(github.event.pull_request.head.repo.full_name == github.repository) ||
(github.event.action == 'labeled' && github.event.label.name == 'run-tests')
steps:
- name: Remove run-tests label, if applicable
if: always() && github.event.label.name == 'run-tests'
uses: actions/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { issue: { number: issue_number }, repo: { owner, repo } } = context;
const label = 'run-tests';
github.issues.removeLabel({ owner, repo, issue_number, name: label });

cfn-lambda-integration-tests:
name: Integration tests for cfn-guard-lambda
needs: remove-label
runs-on: ubuntu-latest
environment: Production # this environment needs manual approval for workflow to be deployed
env:
region: ${{ secrets.LAMBDA_AWS_REGION }}
role: ${{ secrets.LAMBDA_GITHUB_ACTION_ROLE }}
account_id: ${{ secrets.LAMBDA_AWS_ACCOUNT }}

steps:
- name: Check out code
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-region: ${{ env.region }}
role-to-assume: ${{ env.role }}
role-session-name: LambdaTestGitHubAction

- name: Generate identifiers
id: generate-identifiers
run: |
LAMBDA_FUNCTION_PREFIX=GhCfnGrd
LAMBDA_FUNCTION_SUFFIX=$(date +%s_%4N)
LAMBDA_FUNCTION_NAME=${LAMBDA_FUNCTION_PREFIX}_${LAMBDA_FUNCTION_SUFFIX}
ROLE_NAME="${LAMBDA_FUNCTION_NAME}_Role"

echo "LAMBDA_FUNCTION_NAME=${LAMBDA_FUNCTION_NAME}" >> $GITHUB_OUTPUT
echo "ROLE_NAME=${ROLE_NAME}" >> $GITHUB_OUTPUT

echo "Lambda Function Name: ${LAMBDA_FUNCTION_NAME}"
echo "Role Name: ${ROLE_NAME}"


- name: Deploy cfn-guard-lambda
env:
AWS_ACCOUNT_ID: ${{ env.account_id }}
AWS_REGION: ${{ env.region }}
LAMBDA_FUNCTION_NAME: ${{ steps.generate-identifiers.outputs.LAMBDA_FUNCTION_NAME }}
ROLE_NAME: ${{ steps.generate-identifiers.outputs.ROLE_NAME }}
run: |
rustup target add x86_64-unknown-linux-musl
cd guard-lambda
cargo build --release --target x86_64-unknown-linux-musl --verbose
cp ./../target/x86_64-unknown-linux-musl/release/cfn-guard-lambda ./bootstrap && zip lambda.zip bootstrap && rm bootstrap

aws iam create-role \
--role-name $ROLE_NAME \
--assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}'

aws iam attach-role-policy --role-name $ROLE_NAME \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

sleep 10

aws lambda create-function \
--function-name $LAMBDA_FUNCTION_NAME \
--handler guard.handler \
--zip-file fileb://./lambda.zip \
--runtime provided \
--role "arn:aws:iam::${AWS_ACCOUNT_ID}:role/${ROLE_NAME}" \
--environment Variables={RUST_BACKTRACE=1} \
--tracing-config Mode=Active \
--region $AWS_REGION


- name: Invoke Lambda and test output
env:
LAMBDA_FUNCTION_NAME: ${{ steps.generate-identifiers.outputs.LAMBDA_FUNCTION_NAME }}
run: |
aws lambda invoke \
--function-name $LAMBDA_FUNCTION_NAME \
--payload '{"data":"{\"Resources\":{\"NewVolume\":{\"Type\":\"AWS::EC2::Volume\",\"Properties\":{\"Size\":500,\"Encrypted\":true,\"AvailabilityZone\":\"us-west-2b\"}},\"NewVolume2\":{\"Type\":\"AWS::EC2::Volume\",\"Properties\":{\"Size\":50,\"Encrypted\":true,\"AvailabilityZone\":\"us-west-2c\"}}}}","rules":["let ec2_volumes = Resources.*[ Type == /EC2::Volume/ ]\nrule EC2_ENCRYPTION_BY_DEFAULT when %ec2_volumes !empty {\n %ec2_volumes.Properties.Encrypted == true \n <<\n Violation: All EBS Volumes should be encryped \n Fix: Set Encrypted property to true\n >>\n}"],"verbose":false}' \
--cli-binary-format raw-in-base64-out \
output.json

echo '{"message":[{"data_from":"lambda-payload","rules_from":"lambda-rule","not_compliant":{},"not_applicable":[],"compliant":["EC2_ENCRYPTION_BY_DEFAULT"]}]}' > expected-output.json

difference=`diff expected-output.json output.json -w | wc -c`

if [ "$difference" != 0 ]
then
echo "Lambda output does not match the expected one"
echo "--------------------------------"
echo "Actual output:"
cat output.json
echo "--------------------------------"
echo "Expected output:"
cat expected-output.json
echo "--------------------------------"
echo "diff:"
echo "$difference"
exit 1
fi

- name: Clean up resources
if: success() || failure()
env:
LAMBDA_FUNCTION_NAME: ${{ steps.generate-identifiers.outputs.LAMBDA_FUNCTION_NAME }}
ROLE_NAME: ${{ steps.generate-identifiers.outputs.ROLE_NAME }}
run: |
aws lambda delete-function --function-name $LAMBDA_FUNCTION_NAME
aws iam detach-role-policy \
--role-name $ROLE_NAME \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
aws iam delete-role --role-name $ROLE_NAME
15 changes: 14 additions & 1 deletion ATTRIBUTION
Original file line number Diff line number Diff line change
Expand Up @@ -1687,4 +1687,17 @@ SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
DEALINGS IN THE SOFTWARE.

--
actions/github-script

The MIT License (MIT)

Copyright (c) 2019 GitHub, Inc. and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.