GitLab DevOps Integration
The InsightCloudSec GitLab DevOps integration helps your security and development teams implement infrastructure-as-code (IaC) security and compliance scans (serviced using the CLI IaC Scanning Tool mimics) with GitLab.
Prerequisites
- InsightCloudSec Domain Admin permissions
- GitLab user with Maintainer permissions
Example repository structure
This page provides some example configuration based on the sample repository structure found in this section. There are two key directories in the sample repository:
iac_templates
- Contains IaC templates for various providers and is sorted in to subdirectories accordingly.pipeline_configurations
- Stores the pipelines jobs according to their IaC driver. In this example, there are separate files based on whether or not the scan consumes an ICS IaC configuration or is configured with custom options (for example, a fixed list of desired insights). Explore themimics
documentation for configuration details.
You need to configure any example files found on this page to work with your unique environment, including where your IaC configurations reside, any defined environmental variables, whether or not Docker is used, and how the pipeline is triggered.
Example repository structure
bash
1.2├── .gitlab-ci.yml3├── README.md4├── iac_templates5│ ├── .gitkeep6│ ├── cfn7│ │ ├── .gitkeep8│ │ └── my-cfn.yaml9│ └── terraform10│ ├── .gitkeep11│ └── my-tf.tf12└── pipeline_configurations13├── .gitkeep14├── cfn-remote-config-gitlab-ci.yaml15├── cfn-docker.yaml16├── terraform-docker.yaml17└── terraform-remote-config-gitlab-ci.yaml
Example environment variables
In the example files on this page, you may find environment variables to keep sensitive data secure and to interact with InsightCloudSec:
BASE_URL
- The web address of your InsightCloudSec environment, which can be found in System Settings.ICS_API_KEY
- API Key used to access the InsightCloudSec instance pointed to byBASE_URL
. Used to retrieve an IaC configuration created in ICS.ICS_CONFIG_NAME
- Name of desired IaC configuration which defines the scanning behavior ofmimics
.CFN_IAC_TEMPLATE_DIR_PATH
- Path to directory containing Cloudformation (CFN
) YAML templates.TERRAFORM_IAC_TEMPLATE_DIR_PATH
- Path to directory containing Terraform (TF
) YAML templates.TERRAFORM_VERSION
- Version of terraform that should be downloaded and used to generate plans from any scanned Terraform files.
Integrate mimics into a GitLab pipeline
To integrate the InsightCloudSec (ICS) IaC scanning tool (mimics
) with a GitLab CI/CD pipeline, you'll need to invoke mimics
from a pipeline configuration YAML file. This file defines the stages and jobs that run against configured directories of IaC templates (such as Cloudformation or Terraform files). How mimics
integrates with a pipeline depends on how you are currently using mimics
. Currently, InsightCloudSec supports running mimics
as an executable or using Docker. The only difference between the two approaches is the way in which the mimics
scanning tool is obtained in the pipeline job.
GitLab Pipeline YAML structure and configuration
Each job triggers its corresponding pipeline file as indicated by the path in the include
section of each pipeline configuration. In the example in this section, each of the jobs is triggered when there is a change to a configured directory defined in CFN_IAC_TEMPLATE_DIR_PATH
.
Any change to files in this directory (for example, edits or new files) will result in a being scan triggered and a pipeline job being created.
To configure a pipeline for a desired project, navigate to the GitLab Pipeline editor and follow the steps indicated.
Example pipeline configuration
yaml
1stages:2- triggers34trigger_cfn:5stage: triggers6trigger:7include: pipeline_configurations/cfn-remote-config-gitlab-ci.yaml8rules:9changes:10- $CFN_IAC_TEMPLATE_DIR_PATH/*1112trigger_tf:13stage: triggers14trigger:15include: pipeline_configurations/terraform-remote-config-gitlab-ci.yaml16rules:17changes:18- $TERRAFORM_IAC_TEMPLATE_DIR_PATH/*19
Visit the third-party vendor's documentation
For the most accurate information, we recommend that you visit the third-party vendor's product documentation:
- GitLab CI/CD Pipelines: https://docs.gitlab.com/ee/ci/pipelines/
- GitLab CI/CD YAML syntax reference: https://docs.gitlab.com/ee/ci/yaml/
Run mimics as an executable
The CLI IaC Scanning Tool can be run as an executable and downloaded directly into the GitLab pipeline to be run against the files to be scanned. The scanning tool's Insight configuration is pulled directly from your InsightCloudSec instance, so the following variables are required:
BASE_URL
ICS_API_KEY
ICS_CONFIG_NAME
Example CloudFormation configuration
yaml
1stages:2- mimics_install3- mimics_setup4- mimics_scan56image: alpine78mimics-install-job:9stage: mimics_install10script:11- echo "Obtaining mimics binary..."12- mkdir -p $CI_PROJECT_NAME/mimics-reports13- wget https://artifacts.rapid7.com/cloudsec/mimics/latest/mimics_latest_linux_amd64 -O $CI_PROJECT_NAME/mimics14- chmod +x $CI_PROJECT_NAME/mimics15artifacts:16paths:17- $CI_PROJECT_NAME/mimics1819mimics-setup-job:20stage: mimics_setup21script:22- echo "Creating report directory..."23- mkdir -p $CI_PROJECT_NAME/mimics-reports24artifacts:25paths:26- $CI_PROJECT_NAME/mimics-reports2728mimics-scan-job:29stage: mimics_scan30script:31- echo "Initiating scan..."32- $CI_PROJECT_NAME/mimics scan $CFN_IAC_TEMPLATE_DIR_PATH --api-key $ICS_API_KEY --base-url $ICS_BASE_URL --ics-config "$ICS_CONFIG_NAME" --report-path $CI_PROJECT_NAME/mimics-reports --report-formats all --report-name mimics_scan_ci_cd33artifacts:34when: always35paths:36- $CI_PROJECT_NAME/mimics-reports/mimics_scan_ci_cd*
Example Terraform configuration
yaml
1stages:2- mimics_install3- terraform_install4- mimics_setup5- terraform_setup6- mimics_scan78image: alpine910variables:11TERRAFORM_DOWNLOAD_URL: "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip"12MIMICS_DOWNLOAD_URL: "https://artifacts.rapid7.com/cloudsec/mimics/latest/mimics_latest_linux_amd64"1314mimics-install-job:15stage: mimics_install16script:17- echo "Obtaining mimics binary..."18- mkdir -p $CI_PROJECT_NAME/mimics-reports19- wget $MIMICS_DOWNLOAD_URL -O $CI_PROJECT_NAME/mimics20- chmod +x $CI_PROJECT_NAME/mimics21artifacts:22paths:23- $CI_PROJECT_NAME/mimics2425terraform-install-job:26stage: terraform_install27script:28- |29echo "Downloading Terraform..."30wget $TERRAFORM_DOWNLOAD_URL3132echo "Unzipping & placing Terraform in PATH..."33unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d terraform_temp3435chmod +x terraform_temp/terraform36mkdir -p $CI_PROJECT_NAME/tf-bin37mv terraform_temp/terraform $CI_PROJECT_NAME/tf-bin/terraform38artifacts:39paths:40- $CI_PROJECT_NAME/tf-bin4142mimics-setup-job:43stage: mimics_setup44script:45- echo "Creating report directory..."46- mkdir -p $CI_PROJECT_NAME/mimics-reports47artifacts:48paths:49- $CI_PROJECT_NAME/mimics-reports5051terraform-setup-job:52stage: terraform_setup53before_script:54- apk update55- apk add jq56script:57- |58echo "Copying binary to TF folder..."59cp $CI_PROJECT_NAME/tf-bin/terraform $TERRAFORM_IAC_TEMPLATE_DIR_PATH/terraform6061echo "Moving to Terraform directory..."62cd $TERRAFORM_IAC_TEMPLATE_DIR_PATH6364echo "Generating Terraform Plan..."65./terraform init66./terraform plan -out=tf.plan67./terraform show -json tf.plan > plan.json6869echo "Formatting Terraform Plan..."70jq "." ./plan.json > formatted_plan.json71mv formatted_plan.json plan.json72artifacts:73paths:74- $TERRAFORM_IAC_TEMPLATE_DIR_PATH/plan.json7576mimics-scan-job:77stage: mimics_scan78script:79- echo "Initiating scan..."80- $CI_PROJECT_NAME/mimics scan $TERRAFORM_IAC_TEMPLATE_DIR_PATH/plan.json --api-key $ICS_API_KEY --base-url $ICS_BASE_URL --ics-config "$ICS_CONFIG_NAME" --report-path $CI_PROJECT_NAME/mimics-reports --report-formats all --report-name mimics_scan_ci_cd81artifacts:82when: always83paths:84- $CI_PROJECT_NAME/mimics-reports/mimics_scan_ci_cd*
Run mimics using Docker
The CLI IaC Scanning Tool can be invoked with an InsightCloudSec Docker container accessed using Rapid7's public AWS Elastic Container Registry (ECR) repository. To do so, valid AWS credentials are required and must be exposed with environment variables for access in any jobs requiring the ECR repository:
AWS_ACCESS_KEY_ID
- The ID of the Access Key associated with an IAM User to enable programmatic requestsAWS_SECRET_ACCESS_KEY
- The secret value of the Access Key
Example CloudFormation Docker configuration
yaml
1stages:2- docker_scan34image: docker:latest5services:6- docker:dind78scan-job:9stage: docker_scan10before_script:11- apk update12- apk add aws-cli1314script:15- |16aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID17aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY1819mkdir -p ./$CI_PROJECT_NAME/mimics-reports2021docker run \22-v ./$CFN_IAC_TEMPLATE_DIR_PATH:/data \23-v ./$CI_PROJECT_NAME/mimics-reports:/mimics-reports \24--name iac-mimics-container \25public.ecr.aws/rapid7-insightcloudsec/ics/mimics:latest scan /data --api-key $ICS_API_KEY --base-url $ICS_BASE_URL --ics-config "$ICS_CONFIG_NAME" --report-path /mimics-reports --report-formats all --report-name mimics_scan_ci_cd2627artifacts:28when: always29paths:30- ./$CI_PROJECT_NAME/mimics-reports/mimics_scan_ci_cd*
Example Terraform Docker configuration
yaml
1stages:2- terraform_install3- terraform_setup4- docker_scan56image: docker:latest7services:8- docker:dind910variables:11TERRAFORM_DOWNLOAD_URL: "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip"1213terraform-install-job:14stage: terraform_install15script:16- |17echo "Downloading Terraform..."18wget $TERRAFORM_DOWNLOAD_URL1920echo "Unzipping & placing Terraform in PATH..."21unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d terraform_temp2223chmod +x terraform_temp/terraform24mkdir -p $CI_PROJECT_NAME/tf-bin25mv terraform_temp/terraform $CI_PROJECT_NAME/tf-bin/terraform26artifacts:27paths:28- $CI_PROJECT_NAME/tf-bin2930terraform-setup-job:31stage: terraform_setup32before_script:33- apk update34- apk add jq35script:36- |37echo "Copying binary to TF folder..."38cp $CI_PROJECT_NAME/tf-bin/terraform $TERRAFORM_IAC_TEMPLATE_DIR_PATH/terraform3940echo "Moving to Terraform directory..."41cd $TERRAFORM_IAC_TEMPLATE_DIR_PATH4243echo "Generating Terraform Plan..."44./terraform init45./terraform plan -out=tf.plan46./terraform show -json tf.plan > plan.json4748echo "Formatting Terraform Plan..."49jq "." ./plan.json > formatted_plan.json50mv formatted_plan.json plan.json51artifacts:52paths:53- $TERRAFORM_IAC_TEMPLATE_DIR_PATH/plan.json545556scan-job:57stage: docker_scan58before_script:59- apk update60- apk add aws-cli6162script:63- |64aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID65aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY6667mkdir -p ./$CI_PROJECT_NAME/mimics-reports6869docker run \70-v ./$CFN_IAC_TEMPLATE_DIR_PATH:/data \71-v ./$CI_PROJECT_NAME/mimics-reports:/mimics-reports \72--name iac-mimics-container \73public.ecr.aws/rapid7-insightcloudsec/ics/mimics:latest scan /data --api-key $ICS_API_KEY --base-url $ICS_BASE_URL --ics-config "$ICS_CONFIG_NAME" --report-path /mimics-reports --report-formats all --report-name mimics_scan_ci_cd7475artifacts:76when: always77paths:78- ./$CI_PROJECT_NAME/mimics-reports/mimics_scan_ci_cd*
Analyzing scan results
Once a pipeline job has completed, its scan results are uploaded to GitLab as an artifact. Artifacts are uploaded at each stage of the pipeline and defined in each pipeline configuration using the artifacts
variable.
To retrieve a pipeline artifact:
- Navigate to the desired GitLab Project.
- Under the Build menu, click Jobs.
- Select the job you wish to retrieve the artifacts for.
- Select Download in the Job Artifacts section of the job.