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

Ecr image scanning for CI/CD #77

Merged
merged 3 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 20 additions & 4 deletions buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,25 @@ phases:
# List the docker images
- docker images

# Push the image to ECR in the same account and same region the pipeline is hosted.
- docker tag amazon/aws-for-fluent-bit:latest amazon/aws-for-fluent-bit-test:latest
- ecs-cli push amazon/aws-for-fluent-bit-test:latest
# Push the image to ECR with corresponding architecture as the tag.
- aws ecr get-login-password --region ${AWS_REGION}| docker login --username AWS --password-stdin ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com
- aws ecr create-repository --repository-name amazon/aws-for-fluent-bit-test --image-scanning-configuration scanOnPush=true --region ${AWS_REGION} || true
- architecture=$(docker inspect --format='{{.Architecture}}' amazon/aws-for-fluent-bit)
- docker tag amazon/aws-for-fluent-bit:latest ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:$architecture
- docker push ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:$architecture
- './scripts/publish.sh cicd-verify-ecr-image-scan ${AWS_REGION} amazon/aws-for-fluent-bit-test $architecture'

# Create manifest list
- export DOCKER_CLI_EXPERIMENTAL=enabled
- docker manifest create ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:latest ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:arm64 ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:amd64 || true
- docker manifest annotate --arch arm64 ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:latest ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:arm64 || true
- docker manifest annotate --arch amd64 ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:latest ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:amd64 || true

# Sanity check for the debug log
- docker manifest inspect ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:latest || true

# Push manifest list
- docker manifest push ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:latest || true
artifacts:
files:
- '**/*'
- '**/*'
9 changes: 5 additions & 4 deletions buildspec_integ.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ phases:
- 'export AWS_SESSION_TOKEN=`echo $CREDS | jq -r .Token`'

# Pull the image that we built and pushed in the `Build` stage
- 'ecs-cli pull amazon/aws-for-fluent-bit-test:latest'
- 'docker tag ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:latest amazon/aws-for-fluent-bit:latest'
- aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com
- docker pull ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:latest
- docker tag ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test amazon/aws-for-fluent-bit:latest

# List the images to do a double check
- 'docker images'
- docker images

# Command to run the integration test
- 'make integ'
- make integ
artifacts:
files:
- '**/*'
7 changes: 5 additions & 2 deletions buildspec_publish_dockerhub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ phases:
# Enforce STS regional endpoints
- export AWS_STS_REGIONAL_ENDPOINTS=regional
# Pull the image that we built and pushed in the `Build` stage
- 'ecs-cli pull amazon/aws-for-fluent-bit-test:latest'
- 'docker tag ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:latest amazon/aws-for-fluent-bit:latest'
- aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com
- docker pull ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:"amd64"
- docker pull ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:"arm64"
- docker tag ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:amd64 amazon/aws-for-fluent-bit:amd64
- docker tag ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:arm64 amazon/aws-for-fluent-bit:arm64

# List the docker images
- docker images
Expand Down
5 changes: 3 additions & 2 deletions buildspec_publish_ecr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ phases:
# Enforce STS regional endpoints
- export AWS_STS_REGIONAL_ENDPOINTS=regional
# Pull the image that we built and pushed in the `Build` stage
- 'ecs-cli pull amazon/aws-for-fluent-bit-test:latest'
- 'docker tag ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:latest amazon/aws-for-fluent-bit:latest'
- aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com
- docker pull ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:"amd64"
- docker pull ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/amazon/aws-for-fluent-bit-test:"arm64"

# List the docker images
- docker images
Expand Down
10 changes: 5 additions & 5 deletions integ/integ.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ export AWS_REGION="us-west-2"
export PROJECT_ROOT="$(pwd)"

test_cloudwatch() {
export LOG_GROUP_NAME="fluent-bit-integ-test"
export ARCHITECTURE=$(uname -m)
export LOG_GROUP_NAME="fluent-bit-integ-test-${ARCHITECTURE}"
# Tag is used to name the log stream; each test run has a unique (random) log stream name
export TAG=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 10)
docker-compose --file ./integ/test_cloudwatch/docker-compose.test.yml build
docker-compose --file ./integ/test_cloudwatch/docker-compose.test.yml up --abort-on-container-exit
sleep 10
sleep 120

# Creates a file as a flag for the validation failure
mkdir -p ./integ/out
Expand All @@ -20,7 +21,7 @@ test_cloudwatch() {
}

clean_cloudwatch() {
export LOG_GROUP_NAME="fluent-bit-integ-test"
export LOG_GROUP_NAME="fluent-bit-integ-test-${ARCHITECTURE}"
# Clean up resources that were created in the test
docker-compose --file ./integ/test_cloudwatch/docker-compose.clean.yml build
docker-compose --file ./integ/test_cloudwatch/docker-compose.clean.yml up --abort-on-container-exit
Expand Down Expand Up @@ -125,7 +126,6 @@ test_s3() {
clean_s3() {
validate_or_clean_s3 clean
}

if [ "${1}" = "cloudwatch" ]; then
export PLUGIN_UNDER_TEST="cloudwatch"
test_cloudwatch
Expand Down Expand Up @@ -228,4 +228,4 @@ fi

if [ "${1}" = "delete" ]; then
source ./integ/resources/delete_test_resources.sh
fi
fi
5 changes: 3 additions & 2 deletions integ/resources/create_test_resources.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

# Deploys the CloudFormation template to create the stack and necessary resources- kinesis data stream, s3 bucket, and kinesis firehose delivery stream
# Resource (stream, s3, delivery stream) names will start with the stack name "integ-test-fluent-bit"
aws cloudformation deploy --template-file ./integ/resources/cfn-kinesis-s3-firehose.yml --stack-name integ-test-fluent-bit --region us-west-2 --capabilities CAPABILITY_NAMED_IAM
# Resource (stream, s3, delivery stream) names will start with the stack name followed by the corresponding architecture. "integ-test-fluent-bit-architecture"
ARCHITECTURE=$(uname -m | tr '_' '-')
aws cloudformation deploy --template-file ./integ/resources/cfn-kinesis-s3-firehose.yml --stack-name integ-test-fluent-bit-${ARCHITECTURE} --region us-west-2 --capabilities CAPABILITY_NAMED_IAM
3 changes: 2 additions & 1 deletion integ/resources/delete_test_resources.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Delete the CloudFormation stack which created all the resources for running the integration test
aws cloudformation delete-stack --stack-name integ-test-fluent-bit
ARCHITECTURE=$(uname -m | tr '_' '-')
aws cloudformation delete-stack --stack-name integ-test-fluent-bit-${ARCHITECTURE}
3 changes: 2 additions & 1 deletion integ/resources/setup_test_environment.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/bash

# Using CloudFormation describe-stacks extracts the output values for kinesis stream and s3 bucket name, and sets them as environment variables
stackOutputs=$(aws cloudformation describe-stacks --stack-name integ-test-fluent-bit --output text --query 'Stacks[0].Outputs[*].OutputValue')
ARCHITECTURE=$(uname -m | tr '_' '-')
stackOutputs=$(aws cloudformation describe-stacks --stack-name integ-test-fluent-bit-${ARCHITECTURE} --output text --query 'Stacks[0].Outputs[*].OutputValue')
read -r -a outputArray <<< "$stackOutputs"
export FIREHOSE_STREAM="${outputArray[0]}"
export KINESIS_STREAM="${outputArray[1]}"
Expand Down
6 changes: 3 additions & 3 deletions integ/validate_cloudwatch/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

client = boto3.client('logs', region_name=os.environ.get('AWS_REGION'))
metrics_client = boto3.client("cloudwatch", region_name=os.environ["AWS_REGION"])
start_time = datetime.utcnow() - timedelta(seconds=60)
start_time = datetime.utcnow() - timedelta(seconds=600)
end_time = datetime.utcnow()

LOG_GROUP_NAME = os.environ.get('LOG_GROUP_NAME')
Expand Down Expand Up @@ -55,7 +55,7 @@ def validate_metric(test_name, metric_namespace, dim_key, dim_value, expected_sa
return True
attempts += 1
print(f"No metrics yet. Sleeping before trying again. Attempt # {attempts}")
time.sleep(2)
time.sleep(10)

sys.exit('TEST_FAILURE: failed to validate metric existence in CloudWatch')

Expand Down Expand Up @@ -102,4 +102,4 @@ def get_expected_metric_name():

if success_case_1 and success_case_2 and success_case_emf:
# if this file is still present, integ script will mark the test as a failure
os.remove("/out/cloudwatch-test")
os.remove("/out/cloudwatch-test")
Loading