CodeNotary
Integrations

With CodeNotary you can create a fully trusted GitLab pipeline with the following steps:

  • Authenticate the Git repository
  • Build the container image based on the repository, if authentic
  • Notarize the container image before publishing
  • Publish the container image to your registry

Requirements

Please install the latest version the CodeNotary vcn CLI. It can be downloaded from GitHub vcn repository or directly from the dashboard https://dashboard.codenotary.io/download.

Setup the variables in your project for DockerHub and CodeNotary

Please set the following variables in your project’s CI/CD settings on GitLab:

  • codenotary_user
  • codenotary_pass
  • dockerhub (type docker info, in our case: https://index.docker.io/v1/)
  • dockerhub_user
  • dockerhub_pass

Setup the .gitlab-ci.yml

Like most CI/CD pipelines we start with the basic setup. Make sure to create the .gitlab-ci.yml file in your repository:

image: docker:18

stages:
  - authgit
  - build
  - notarize
  - push

variables:
    CONTAINER_IMAGE: "<dockerhub_repository_name>"


before_script:
  - echo -n $dockerhub_pass | docker login -u $dockerhub_user --password-stdin $dockerhub

Authenticate the sources

The next step is about authenticating the sources to make sure only notarized git commits are used during the build. In that post we use the vcn version 0.7.3, but you can use the latest of course.

Be aware, as we’re using an Alpine OS based image to use the static linked version of vcn.

Most importantly, we want to make sure, that nobody tampered with the vcn executable we downloaded, therefore we do an independent check using the CodeNotary.io API with curl.

# Authenticate Git source
Authenticate Git:
  stage: authgit
  script:
    - apk add --no-cache curl
    - curl -L -o /tmp/vcn https://github.com/vchain-us/vcn/releases/download/v0.7.3/vcn-v0.7.3-linux-amd64-static
    - CHECKSUM=$(sha256sum /tmp/vcn | cut -d " " -f 1)
    - echo $CHECKSUM
    - curl -s https://api.codenotary.io/authenticate/$CHECKSUM?org=vchain.us | grep -q :0
    - chmod +x /tmp/vcn
    - /tmp/vcn a git://$CI_PROJECT_DIR</code></pre>

Build the image

Next, you need to build the container image and push it just with the hash, but no additional tagging. That’s going to happen in a later step.

Build:
  stage: build
  script:
    - >
      docker build
      --pull
      --build-arg VCS_REF=$CI_COMMIT_SHA
      --build-arg VCS_URL=$CI_PROJECT_URL
      --tag $CONTAINER_IMAGE:$CI_COMMIT_SHA
      .
    - docker push $CONTAINER_IMAGE:$CI_COMMIT_SHA</code></pre>

Notarize the container image

That’s is mainly a copy of the authentication steps from the other build steps before. The reason is, that you can’t know if its just one or different GitLab Runner that pick up the tasks – so we make sure, they always are at the same stage:

# notarize container
Notarize:
  stage: notarize
  script:
    - apk add --no-cache curl
    - curl -L -o /tmp/vcn https://github.com/vchain-us/vcn/releases/download/v0.7.3/vcn-v0.7.3-linux-amd64-static
    - CHECKSUM=$(sha256sum /tmp/vcn | cut -d " " -f 1)
    - echo $CHECKSUM
    - curl -s https://api.codenotary.io/authenticate/$CHECKSUM?org=vchain.us | grep -q :0
    - chmod +x /tmp/vcn
    - VCN_USER=$codenotary_user VCN_PASSWORD=$codenotary_pass /tmp/vcn login
    - VCN_NOTARIZATION_PASSWORD=$codenotary_pass /tmp/vcn n --attr GitLab="$CI_COMMIT_SHA" docker://$CONTAINER_IMAGE:latest

Push the image

The last steps are about pushing the newly build container image with different tags, the checksum (reference to the GitLab job) and latest.

# Here, the goal is to tag the "master" branch as "latest"
Push latest:
  variables:
    GIT_STRATEGY: none
  stage: push
  only:
    # Only "master" should be tagged "latest"
    - master
  script:
    # no guarantee that this job will be picked up by the same runner 
    # that built the image in the previous step, pull it again locally
    - docker pull $CONTAINER_IMAGE:$CI_COMMIT_SHA
    # Then we tag it "latest"
    - docker tag $CONTAINER_IMAGE:$CI_COMMIT_SHA $CONTAINER_IMAGE:latest
    # push it.
    - docker push $CONTAINER_IMAGE:latest

Push tag:
  variables:
    GIT_STRATEGY: none
  stage: push
  only:
    # run on tags only.
    - tags
  script:
    - docker pull $CONTAINER_IMAGE:$CI_COMMIT_SHA
    - docker tag $CONTAINER_IMAGE:$CI_COMMIT_SHA $CONTAINER_IMAGE:$CI_COMMIT_REF_NAME
    - docker push $CONTAINER_IMAGE:$CI_COMMIT_REF_NAME

Result

Make sure to notarize the repository commits, when changing the .gitlab-ci.yml file, as that change will also stop the container image from being build – Security first!

If all went well, you should be able to get nice green checks for every single pipeline step.

 

Go to documentation