Integrating with GitHub Actions – Amazon CodeGuru in your DevSecOps Pipeline

March 22, 2023 By Mark Otto Off

Many organizations have adopted DevOps practices to streamline and automate software delivery and IT operations. A DevOps model can be adopted without sacrificing security by using automated compliance policies, fine-grained controls, and configuration management techniques. However, one of the key challenges customers face is analyzing code and detecting any vulnerabilities in the code pipeline due to a lack of access to the right tool. Amazon CodeGuru addresses this challenge by using machine learning and automated reasoning to identify critical issues and hard-to-find bugs during application development and deployment, thus improving code quality.

We discussed how you can build a CI/CD pipeline to deploy a web application in our previous post “Integrating with GitHub Actions – CI/CD pipeline to deploy a Web App to Amazon EC2”. In this post, we will use that pipeline to include security checks and integrate it with Amazon CodeGuru Reviewer to analyze and detect potential security vulnerabilities in the code before deploying it.

Amazon CodeGuru Reviewer helps you improve code security and provides recommendations based on common vulnerabilities (OWASP Top 10) and AWS security best practices. CodeGuru analyzes Java and Python code and provides recommendations for remediation. CodeGuru Reviewer detects a deviation from best practices when using AWS APIs and SDKs, and also identifies concurrency issues, resource leaks, security vulnerabilities and validates input parameters. For every workflow run, CodeGuru Reviewer’s GitHub Action copies your code and build artifacts into an S3 bucket and calls CodeGuru Reviewer APIs to analyze the artifacts and provide recommendations. Refer to the code detector library here for more information about CodeGuru Reviewer’s security and code quality detectors.

With GitHub Actions, developers can easily integrate CodeGuru Reviewer into their CI workflows, conducting code quality and security analysis. They can view CodeGuru Reviewer recommendations directly within the GitHub user interface to quickly identify and fix code issues and security vulnerabilities. Any pull request or push to the master branch will trigger a scan of the changed lines of code, and scheduled pipeline runs will trigger a full scan of the entire repository, ensuring comprehensive analysis and continuous improvement.

Solution overview

The solution comprises of the following components:

  1. GitHub Actions – Workflow Orchestration tool that will host the Pipeline.
  2. AWS CodeDeploy – AWS service to manage deployment on Amazon EC2 Autoscaling Group.
  3. AWS Auto Scaling – AWS service to help maintain application availability and elasticity by automatically adding or removing Amazon EC2 instances.
  4. Amazon EC2 – Destination Compute server for the application deployment.
  5. Amazon CodeGuru – AWS Service to detect security vulnerabilities and automate code reviews.
  6. AWS CloudFormation – AWS infrastructure as code (IaC) service used to orchestrate the infrastructure creation on AWS.
  7. AWS Identity and Access Management (IAM) OIDC identity provider – Federated authentication service to establish trust between GitHub and AWS to allow GitHub Actions to deploy on AWS without maintaining AWS Secrets and credentials.
  8. Amazon Simple Storage Service (Amazon S3) – Amazon S3 to store deployment and code scan artifacts.

The following diagram illustrates the architecture:

Figure 1. Architecture Diagram of the proposed solution in the blog.

Figure 1. Architecture Diagram of the proposed solution in the blog

  1. Developer commits code changes from their local repository to the GitHub repository. In this post, the GitHub action is triggered manually, but this can be automated.
  2. GitHub action triggers the build stage.
  3. GitHub’s Open ID Connector (OIDC) uses the tokens to authenticate to AWS and access resources.
  4. GitHub action uploads the deployment artifacts to Amazon S3.
  5. GitHub action invokes Amazon CodeGuru.
  6. The source code gets uploaded into an S3 bucket when the CodeGuru scan starts.
  7. GitHub action invokes CodeDeploy.
  8. CodeDeploy triggers the deployment to Amazon EC2 instances in an Autoscaling group.
  9. CodeDeploy downloads the artifacts from Amazon S3 and deploys to Amazon EC2 instances.

Prerequisites

This blog post is a continuation of our previous post – Integrating with GitHub Actions – CI/CD pipeline to deploy a Web App to Amazon EC2. You will need to setup your pipeline by following instructions in that blog.

After completing the steps, you should have a local repository with the below directory structure, and one completed Actions run.

Figure 2. Directory structure

Figure 2. Directory structure

To enable automated deployment upon git push, you will need to make a change to your .github/workflow/deploy.yml file. Specifically, you can activate the automation by modifying the following line of code in the deploy.yml file:

From:

workflow_dispatch: {}

To:

 #workflow_dispatch: {} push: branches: [ main ] pull_request:

Solution walkthrough

The following steps provide a high-level overview of the walkthrough:

  1. Create an S3 bucket for the Amazon CodeGuru Reviewer.
  2. Update the IAM role to include permissions for Amazon CodeGuru.
  3. Associate the repository in Amazon CodeGuru.
  4. Add Vulnerable code.
  5. Update GitHub Actions Job to run the Amazon CodeGuru Scan.
  6. Push the code to the repository.
  7. Verify the pipeline.
  8. Check the Amazon CodeGuru recommendations in the GitHub user interface.

1. Create an S3 bucket for the Amazon CodeGuru Reviewer

    • When you run a CodeGuru scan, your code is first uploaded to an S3 bucket in your AWS account.

Note that CodeGuru Reviewer expects the S3 bucket name to begin with codeguru-reviewer-.

    • You can create this bucket using the bucket policy outlined in this CloudFormation template (JSON or YAML) or by following these instructions.

2.  Update the IAM role to add permissions for Amazon CodeGuru

  • Locate the role created in the pre-requisite section, named “CodeDeployRoleforGitHub”.
  • Next, create an inline policy by following these steps. Give it a name, such as “codegurupolicy” and add the following permissions to the policy.
{ “Version”: “2012-10-17", “Statement”: [ { “Action”: [ “codeguru-reviewer:ListRepositoryAssociations”, “codeguru-reviewer:AssociateRepository”, “codeguru-reviewer:DescribeRepositoryAssociation”, “codeguru-reviewer:CreateCodeReview”, “codeguru-reviewer:DescribeCodeReview”, “codeguru-reviewer:ListRecommendations”, “iam:CreateServiceLinkedRole” ], “Resource”: “*”, “Effect”: “Allow” }, { “Action”: [ “s3:CreateBucket”, “s3:GetBucket*“, “s3:List*“, “s3:GetObject”, “s3:PutObject”, “s3:DeleteObject” ], “Resource”: [ “arn:aws:s3:::codeguru-reviewer-*“, “arn:aws:s3:::codeguru-reviewer-*/*” ], “Effect”: “Allow” } ]
}

3.  Associate the repository in Amazon CodeGuru

Figure 3. associate the repository

Figure 3. Associate the repository

At this point, you will have completed your initial full analysis run. However, since this is a simple “helloWorld” program, you may not receive any recommendations. In the following steps, you will incorporate vulnerable code and trigger the analysis again, allowing CodeGuru to identify and provide recommendations for potential issues.

4.  Add Vulnerable code

  • Create a file application.conf
    at /aws-codedeploy-github-actions-deployment/spring-boot-hello-world-example
  • Add the following content in application.conf file.
db.default.url="postgres://test-ojxarsxivjuyjc:ubKveYbvNjQ5a0CU8vK4YoVIhl@ec2-54-225-223-40.compute-1.amazonaws.com:5432/dcectn1pto16vi?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory" db.default.url=${?DATABASE_URL} db.default.port="3000" db.default.datasource.username="root" db.default.datasource.password="testsk_live_454kjkj4545FD3434Srere7878" db.default.jpa.generate-ddl="true" db.default.jpa.hibernate.ddl-auto="create"

5. Update GitHub Actions Job to run Amazon CodeGuru Scan

  • You will need to add a new job definition in the GitHub Actions’ yaml file. This new section should be inserted between the Build and Deploy sections for optimal workflow.
  • Additionally, you will need to adjust the dependency in the deploy section to reflect the new flow: Build -> CodeScan -> Deploy.
  • Review sample GitHub actions code for running security scan on Amazon CodeGuru Reviewer.
codescan: needs: build runs-on: ubuntu-latest permissions: id-token: write contents: read security-events: write steps: - name: Download an artifact uses: actions/download-artifact@v2 with: name: build-file - name: Configure AWS credentials id: iam-role continue-on-error: true uses: aws-actions/configure-aws-credentials@v1 with: role-to-assume: ${{ secrets.IAMROLE_GITHUB }} role-session-name: GitHub-Action-Role aws-region: ${{ env.AWS_REGION }} - uses: actions/checkout@v2 if: steps.iam-role.outcome == 'success' with: fetch-depth: 0 - name: CodeGuru Reviewer uses: aws-actions/[email protected] if: ${{ always() }} continue-on-error: false with: s3_bucket: ${{ env.S3bucket_CodeGuru }} build_path: . - name: Store SARIF file if: steps.iam-role.outcome == 'success' uses: actions/upload-artifact@v2 with: name: SARIF_recommendations path: ./codeguru-results.sarif.json - name: Upload review result uses: github/codeql-action/upload-sarif@v2 with: sarif_file: codeguru-results.sarif.json - run: | echo "Check for critical volnurability" count=$(cat codeguru-results.sarif.json | jq '.runs[].results[] | select(.level == "error") | .level' | wc -l) if (( $count > 0 )); then echo "There are $count critical findings, hence stopping the pipeline." exit 1 fi

  • Refer to the complete file provided below for your reference. It is important to note that you will need to replace the following environment variables with your specific values.
    • S3bucket_CodeGuru
    • AWS_REGION
    • S3BUCKET
name: Build and Deploy on: #workflow_dispatch: {} push: branches: [ main ] pull_request: env: applicationfolder: spring-boot-hello-world-example AWS_REGION: us-east-1 # <replace this with your AWS region> S3BUCKET: *<Replace your bucket name here>* S3bucket_CodeGuru: codeguru-reviewer-<*replacebucketnameher*> # S3 Bucket with "codeguru-reviewer-*" prefix jobs: build: name: Build and Package runs-on: ubuntu-latest permissions: id-token: write contents: read steps: - uses: actions/checkout@v2 name: Checkout Repository - uses: aws-actions/configure-aws-credentials@v1 with: role-to-assume: ${{ secrets.IAMROLE_GITHUB }} role-session-name: GitHub-Action-Role aws-region: ${{ env.AWS_REGION }} - name: Set up JDK 1.8 uses: actions/setup-java@v1 with: java-version: 1.8 - name: chmod run: chmod -R +x ./.github - name: Build and Package Maven id: package working-directory: ${{ env.applicationfolder }} run: $GITHUB_WORKSPACE/.github/scripts/build.sh - name: Upload Artifact to s3 working-directory: ${{ env.applicationfolder }}/target run: aws s3 cp *.war s3://${{ env.S3BUCKET }}/ - name: Artifacts for codescan action uses: actions/upload-artifact@v2 with: name: build-file path: ${{ env.applicationfolder }}/target/*.war codescan: needs: build runs-on: ubuntu-latest permissions: id-token: write contents: read security-events: write steps: - name: Download an artifact uses: actions/download-artifact@v2 with: name: build-file - name: Configure AWS credentials id: iam-role continue-on-error: true uses: aws-actions/configure-aws-credentials@v1 with: role-to-assume: ${{ secrets.IAMROLE_GITHUB }} role-session-name: GitHub-Action-Role aws-region: ${{ env.AWS_REGION }} - uses: actions/checkout@v2 if: steps.iam-role.outcome == 'success' with: fetch-depth: 0 - name: CodeGuru Reviewer uses: aws-actions/[email protected] if: ${{ always() }} continue-on-error: false with: s3_bucket: ${{ env.S3bucket_CodeGuru }} build_path: . - name: Store SARIF file if: steps.iam-role.outcome == 'success' uses: actions/upload-artifact@v2 with: name: SARIF_recommendations path: ./codeguru-results.sarif.json - name: Upload review result uses: github/codeql-action/upload-sarif@v2 with: sarif_file: codeguru-results.sarif.json - run: | echo "Check for critical volnurability" count=$(cat codeguru-results.sarif.json | jq '.runs[].results[] | select(.level == "error") | .level' | wc -l) if (( $count > 0 )); then echo "There are $count critical findings, hence stopping the pipeline." exit 1 fi deploy: needs: codescan runs-on: ubuntu-latest environment: Dev permissions: id-token: write contents: read steps: - uses: actions/checkout@v2 - uses: aws-actions/configure-aws-credentials@v1 with: role-to-assume: ${{ secrets.IAMROLE_GITHUB }} role-session-name: GitHub-Action-Role aws-region: ${{ env.AWS_REGION }} - run: | echo "Deploying branch ${{ env.GITHUB_REF }} to ${{ github.event.inputs.environment }}" commit_hash=`git rev-parse HEAD` aws deploy create-deployment --application-name CodeDeployAppNameWithASG --deployment-group-name CodeDeployGroupName --github-location repository=$GITHUB_REPOSITORY,commitId=$commit_hash --ignore-application-stop-failures

6.  Push the code to the repository:

  • Remember to save all the files that you have modified.
  • To ensure that you are in your git repository folder, you can run the command:
git remote -v

  • The command should return the remote branch address, which should be similar to the following:
username@3c22fb075f8a GitActionsDeploytoAWS % git remote -v origin	[email protected]:<username>/GitActionsDeploytoAWS.git (fetch) origin	[email protected]:<username>/GitActionsDeploytoAWS.git (push)

  • To push your code to the remote branch, run the following commands:

git add . git commit -m “Adding Security Scan” git push

Your code has been pushed to the repository and will trigger the workflow as per the configuration in GitHub Actions.

7.  Verify the pipeline

  • Your pipeline is set up to fail upon the detection of a critical vulnerability. You can also suppress recommendations from CodeGuru Reviewer if you think it is not relevant for setup. In this example, as there are two critical vulnerabilities, the pipeline will not proceed to the next step.
  • To view the status of the pipeline, navigate to the Actions tab on your GitHub console. You can refer to the following image for guidance.
Figure 4. github actions pipeline

Figure 4. GitHub Actions pipeline

  • To view the details of the error, you can expand the “codescan” job in the GitHub Actions console. This will provide you with more information about the specific vulnerabilities that caused the pipeline to fail and help you to address them accordingly.
Figure 5. Codescan actions logs

Figure 5. Codescan actions logs

8. Check the Amazon CodeGuru recommendations in the GitHub user interface

Once you have run the CodeGuru Reviewer Action, any security findings and recommendations will be displayed on the Security tab within the GitHub user interface. This will provide you with a clear and convenient way to view and address any issues that were identified during the analysis.

Figure 6. security tab with results

Figure 6. Security tab with results

Clean up

To avoid incurring future charges, you should clean up the resources that you created.

  1. Empty the Amazon S3 bucket.
  2. Delete the CloudFormation stack (CodeDeployStack) from the AWS console.
  3. Delete codeguru Amazon S3 bucket.
  4. Disassociate the GitHub repository in CodeGuru Reviewer.
  5. Delete the GitHub Secret (‘IAMROLE_GITHUB’)
    1. Go to the repository settings on GitHub Page.
    2. Select Secrets under Actions.
    3. Select IAMROLE_GITHUB, and delete it.

Conclusion

Amazon CodeGuru is a valuable tool for software development teams looking to improve the quality and efficiency of their code. With its advanced AI capabilities, CodeGuru automates the manual parts of code review and helps identify performance, cost, security, and maintainability issues. CodeGuru also integrates with popular development tools and provides customizable recommendations, making it easy to use within existing workflows. By using Amazon CodeGuru, teams can improve code quality, increase development speed, lower costs, and enhance security, ultimately leading to better software and a more successful overall development process.

In this post, we explained how to integrate Amazon CodeGuru Reviewer into your code build pipeline using GitHub actions. This integration serves as a quality gate by performing code analysis and identifying challenges in your code. Now you can access the CodeGuru Reviewer recommendations directly within the GitHub user interface for guidance on resolving identified issues.

About the author:

Mahesh Biradar

Mahesh Biradar is a Solutions Architect at AWS. He is a DevOps enthusiast and enjoys helping customers implement cost-effective architectures that scale.

Suresh Moolya

Suresh Moolya is a Senior Cloud Application Architect with Amazon Web Services. He works with customers to architect, design, and automate business software at scale on AWS cloud.

Shikhar Mishra

Shikhar is a Solutions Architect at Amazon Web Services. He is a cloud security enthusiast and enjoys helping customers design secure, reliable, and cost-effective solutions on AWS.