All articles

Gitlab CI/CD and passbolt secrets automation

8 min. read

Jean-Christophe Vassort

Jean-Christophe Vassort

7 February, 2022

Why use passbolt for managing CI/CD secrets?

Illustration of a passbolt <> gitlab integration

Demo project

Passbolt setup

server:#!/usr/bin/env bash

set -euo pipefail

TMPGNUPGHOME=$(mktemp -d)
EMAIL="[email protected]"
PASSPHRASE="strong-passphrase"
FIRSTNAME="John"
LASTNAME="Doe"
KEYSIZE=2048
PASSBOLT_FQDN="passbolt.domain.tld"

# Register a new user and get its uuid + token registration
REGISTRATION_URL=$(sudo -H -u www-data bash -c "/usr/share/php/passbolt/bin/cake passbolt register_user -u ${EMAIL} -f ${FIRSTNAME} -l ${LASTNAME} -r user" | grep http)

USER_UUID=$(echo "${REGISTRATION_URL}" | cut -d/ -f6)
USER_TOKEN=$(echo "${REGISTRATION_URL}" | cut -d/ -f7)

# Generate OpenPGP keys
gpg --homedir ${TMPGNUPGHOME} --batch --no-tty --gen-key <<EOF
  Key-Type: default
  Key-Length: ${KEYSIZE}
  Subkey-Type: default
  Subkey-Length: 2048
  Name-Real: ${FIRSTNAME} ${LASTNAME}
  Name-Email: ${EMAIL}
  Expire-Date: 0
  Passphrase: ${PASSPHRASE}
  %commit
EOF

gpg --passphrase ${PASSPHRASE} --batch --pinentry-mode=loopback --armor --homedir ${TMPGNUPGHOME} --export-secret-keys ${EMAIL} > secret.asc
gpg --homedir ${TMPGNUPGHOME} --armor --export ${EMAIL} > public.asc

rm -rf ${TMPGNUPGHOME}

# Make an API call to register user
curl "https://${PASSBOLT_FQDN}/setup/complete/${USER_UUID}" \
  -H "authority: ${PASSBOLT_FQDN}" \
  -H "accept: application/json" \
  -H "content-type: application/json" \
  --data-raw "{\"authenticationtoken\":{\"token\":\"${USER_TOKEN}\"},\"gpgkey\":{\"armored_key\":\"$(sed -z 's/\n/\\n/g' public.asc)\"}}" \
  --compressed

Let’s create a test secret with passbolt cli

passbolt create resource \
 - name "docker.com token for gitlab" \
 - username "[email protected]" \
 - password "Ch4ng3-m3-pl35E" \
 - uri "https://docker.io"

Creating a gitlab project

  1. Build a docker image and store it in Gitlab registry
  2. Test the image for vulnerabilities
  3. Push image in Docker Hub

Gitlab Job setup for retrieving secrets

# Set the configuration file containing the private OpenPGP key and passphrase
cat ${PASSBOLT_CLI_CONFIG} | base64 -d > /root/.config/go-passbolt-cli/go-passbolt-cli.toml

# Login to gitlab CI registry
# CI_REGISTRY_* variables are dynamically set by gitlab
docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}

###
# login to Docker HUB registry / fetch password from passbolt
###

# Define resource name and username as they are named in passbolt
NAME="docker.com token for gitlab"
USERNAME="[email protected]"

# Get passbolt resource ID from passbolt resource name and username
ID="$(passbolt list resource - column ID - column Name - column Username \
| grep "${NAME}" | grep "${USERNAME}" | head -n1 | awk '{print $1}')"

# Get passbolt resource password from its ID
PASSWORD=$(passbolt get resource - id ${ID} | grep Password | sed 's/^Password: //g')

# Authenticate to Docker Hub with password retrieved from passbolt
docker login -u ${USERNAME} -p ${PASSWORD} docker.io

# Pull image from Gitlab registry
IMAGE="$(echo ${CI_REGISTRY}/${CI_PROJECT_PATH}/nginx-distroless-unprivileged:${NGINX_VERSION} | tr '[:upper:]' '[:lower:]')"
docker pull ${IMAGE}

# Tag image with docker hub tag
docker tag ${IMAGE} anatomicjc/nginx-distroless-unprivileged:${NGINX_VERSION}

# Push it
docker push anatomicjc/nginx-distroless-unprivileged:${NGINX_VERSION}

Mitigate the risks

Security improvement: setup our own self-hosted and trusted gitlab-runner

gitlab project:gitlab-runner register — url https://gitlab.com/ — registration-token xxxxx
(…)
volumes = [“/cache”, “/root/go-passbolt-cli.toml:/root/.config/go-passbolt-cli/go-passbolt-cli.toml”]
(…)
(…)
[runners.docker]
security_opt = [“seccomp:unconfined”, “apparmor:unconfined”]
(…)
tags:
  - self-hosted-runner
# Set the configuration file containing the private OpenPGP key and passphrase
cat ${PASSBOLT_CLI_CONFIG} | base64 -d > /root/.config/go-passbolt-cli/go-passbolt-cli.toml

Gitlab CI configuration hardening

rules:
  - if: $CI_COMMIT_REF_NAME == "main"
.gitlab-ci.yml @AnatomicJC
CODEOWNERS @AnatomicJC

Conclusion

Continue reading

Security and compliance roundup

4 min. read

Security and compliance roundup

A summary of the recent achievements on the security and compliance front: SOC2 Type II, independent audits, Security Made in Europe label

Passbolt team

Passbolt team

14 February, 2022

What’s cooking for 2022 ?

4 min. read

What’s cooking for 2022 ?

It’s the end of the year and even though a lot happened in the past 12 months there is more in preparation. We thought that it would be…

Vivien Muller

Vivien Muller

15 December, 2021

Flag of European UnionMade in Europe. Privacy by default.