Links

Getting Started: Stub and Harden a Container Image with Docker and GitLab

Use the RapidFort Command Line Interface (CLI) tools to stub and harden a container image with Docker and GitLab

Prerequisites

You will need the following:
  • RapidFort Server (SaaS or On-Premises)
  • RapidFort Service Account
  • GitLab Runner
  • Deployment Environment (for example, Kubernetes, Docker, Docker-Compose, AWS Fargate)
    • The deployment environment must have HTTPS access to the RapidFort server
    • The deployment environment must provide support for adding the SYS_PTRACE Linux kernel capability
    • In this tutorial, we will deploy and test the stub image on the GitLab runner
  • Container Registry (for example, Amazon Elastic Container Registry, Docker Hub, Microsoft Azure Container Registry, and so forth)
    • In this tutorial, we will not push images to a container registry

Stub, Test, and Harden the NGINX Docker Image

In this tutorial, we will stub, test, and harden the NGINX Docker image.
Original Image: The NGINX Docker image available directly from Docker Hub. The original image has not yet been optimized and secured by RapidFort and may have known vulnerabilities.
Stub Image: The original image with additional dependencies necessary for RapidFort to trace the runtime behavior.
Hardened Image: The optimized and secured image generated by RapidFort.
Download the sample .gitlab-ci.yml file.
Update the following variables:
  • RF_ROOT_URL:
    • For SaaS users, specify https://frontrow.rapidfort.com.
    • For On-Premises users, specify the hostname or IP address of your RapidFort on-premises server (for example, https://rapidfort.example.com).
  • RF_ACCESS_ID: Specify the access id for your RapidFort service account.
  • RF_SECRET_ACCESS_KEY: Specify the secret access key for your RapidFort service account.
  • RF_CLI_UPDATE: Specify "no" to download and install the RapidFort CLI tools only if they are not already installed on the runner or "yes" to always download and install the tools (even if they are already installed).
  • RF_CLI_PATH: Specify the location where the RapidFort CLI tools will be installed on the GitLab runner.
We recommend adding these as environment variables.
.gitlab-ci.yml
variables:
DOCKER_IMAGE_NAME: "nginx"
TAG: latest
RF_ROOT_URL: https://frontrow.rapidfort.com
# generate service account & replace these
RF_ACCESS_ID: RFabcdefghijkl123456
RF_SECRET_ACCESS_KEY: 01234567891011abcdefghijklmnopqrstuvwxyz
RF_CLI_UPDATE: "no"
RF_CLI_PATH: /home/gitlab-runner/.local/bin
default:
tags:
- ubuntu
before_script:
- |
if [ -z "$(command -v rflogin)" ] || [ "${RF_CLI_UPDATE}" == "yes" ]; then
curl -ks "${RF_ROOT_URL}"/cli/ | bash
fi
export PATH="$RF_CLI_PATH:$PATH"
stages:
- Build
- Stub
- Deploy
- Test
- Harden
build:
stage: Build
script:
- |
docker pull $DOCKER_IMAGE_NAME:$TAG
docker tag $DOCKER_IMAGE_NAME:$TAG $DOCKER_IMAGE_NAME:$TAG-$CI_PIPELINE_ID
generate-stub:
stage: Stub
script:
- |
# ** GENERATE STUB **
rfstub $DOCKER_IMAGE_NAME:$TAG-$CI_PIPELINE_ID
docker images | grep $DOCKER_IMAGE_NAME | grep $CI_PIPELINE_ID
deploy:
stage: Deploy
script:
- |
# Run & test stub
docker run --rm --name $DOCKER_IMAGE_NAME-$TAG-$CI_PIPELINE_ID -p9999:80 --cap-add=SYS_PTRACE -d $DOCKER_IMAGE_NAME:$TAG-$CI_PIPELINE_ID-rfstub
test:
stage: Test
script:
- |
STATUS_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:9999 || /bin/true)
until [ "${STATUS_CODE}" == 200 ]; do
sleep 1
STATUS_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:9999 || /bin/true)
done
# Load test
ab -n 10 -c 10 http://localhost:9999/
# Remove running stub
docker stop $DOCKER_IMAGE_NAME-$TAG-$CI_PIPELINE_ID
Harden:
stage: Harden
script:
- |
# ** GENERATE HARDENED IMAGE **
rfharden $DOCKER_IMAGE_NAME:$TAG-$CI_PIPELINE_ID-rfstub && echo "Hardened Done" || echo "Hardened Failed"
docker images | grep $DOCKER_IMAGE_NAME | grep $CI_PIPELINE_ID
when: manual

Stub

During the Stub stage, run rfstub to generate a stub image.
In this tutorial, we will not push the stub image to a container registry, but your pipeline will need to do so.
When you generate a stub image, RapidFort also scans the original image for packages and known vulnerabilities and computes the estimated risk reduction opportunity if the image is hardened. You may optionally visit the RapidFort dashboard to view the vulnerabilities and packages that were found in the original image.

Deploy and Test

Next, deploy and test the stub image so that RapidFort can trace the runtime behavior of the application and build the runtime profile.
The SYS_PTRACE Linux kernel capability must be added when deploying stub images so that RapidFort can trace the runtime behavior.
If you are planning to deploy the stub image in a Kubernetes environment, you will need to allow privilege escalation and read/write access to the root filesystem:
  • allowPrivilegeEscalation: true
  • readOnlyRootFilesystem: false
Make sure that your deployment environment (in this tutorial, the GitLab runner) allows HTTPS access to the RapidFort server.

Verify that Tracing Information is Propagated to the RapidFort Server

Log into the RapidFort dashboard. You should see an image list.
Select the image that you deployed.
In the left column, select Logs. Verify that the Logs table contains tracing information. You should see file accesses, system calls, and so forth. The Logs table contents are updated in real time.
If the Logs table is empty, please verify the following:
  • The stub image (not the original image) was deployed
  • The deployment configuration added the SYS_PTRACE Linux kernel capability
    • For Kubernetes environments, the deployment configuration allows privilege escalation and enable read/write access for the root filesystem
      • allowPrivilegeEscalation: true
      • readOnlyRootFilesystem: false
  • The environment in which the stub image was deployed has HTTPS access to the RapidFort server
If there are any issues, please update your deployment configuration and environment and deploy the stub image again. RapidFort cannot harden an image unless it has a valid runtime profile.

Harden

After you have deployed and tested the stub image and verified that the runtime profile information was propagated to RapidFort, you are ready to harden it.
Note that the Harden stage is manual. You must deploy and test the stub image and verify that the runtime profile was generated successfully before hardening it.