Securing Nexus login data retrieval in Dockerfile via HashiCorp Vault
· ☕ 7 min read
· 🐧 sysadmin
Introduction
To use HashiCorp Vault for storing and retrieving Nexus (NPM) login data during Docker image building, we need to focus on securely storing these credentials in Vault and then retrieving them inside the Docker container during the build process. The key aspect here is using the vault tool in the Docker container to fetch secrets directly during the image build.
Steps to follow
1.Storing Nexus (NPM) login data in HashiCorp Vault.
2.Modifying .gitlab-ci.yml to fetch this data inside the Docker container.
3.Modifying Dockerfile to fetch login data from Vault during image build.
Step 1: Storing NPM login data in HashiCorp Vault
First, add the Nexus (NPM) login data to HashiCorp Vault.
1
vault kv put secret/gitlab/npm NPM_USER="your-npm-username"NPM_PASS="your-npm-password"
Step 2: Modifying .gitlab-ci.yml
Update .gitlab-ci.yml to fetch login data from Vault and use it in the Docker container.
Here is the updated YAML file with added comments:
FROM node:22.2-alpine3.20# Install dependenciesRUN apk update > /dev/null &&\
apk add --no-cache curl jq unzip git openssh bash nano wget ca-certificates openssl > /dev/null# Use openssl to download the self-signed certificate and add it to the trusted certificatesRUNecho -n | openssl s_client -connect 10.10.0.150:8200 -servername 10.10.0.150 | openssl x509 -out /usr/local/share/ca-certificates/vault.crt &&\
update-ca-certificates# Set environment variables for VaultARG VAULT_ADDRARG VAULT_TOKEN# Retrieve NPM credentials from Vault and create .npmrc fileRUNNPM_SECRET=$(curl --verbose --header "X-Vault-Token: $VAULT_TOKEN"$VAULT_ADDR/v1/secret/data/gitlab/npm)&&\
NPM_USER=$(echo$NPM_SECRET| jq -r '.data.data.NPM_USER')&&\
NPM_PASS=$(echo$NPM_SECRET| jq -r '.data.data.NPM_PASS')&&\
echo"registry=https://nexus.sysadmin.homes/repository/npm-group/" > /root/.npmrc &&\
echo"//nexus.sysadmin.homes/repository/npm-group/:_auth=$(echo -n ${NPM_USER}:${NPM_PASS}| base64)" >> /root/.npmrc &&\
echo"always-auth=true" >> /root/.npmrc# Adding GitLab SSH key to known_hostsRUN mkdir -p /root/.ssh && ssh-keyscan gitlab.sysadmin.homes >> /root/.ssh/known_hosts# Gauge installationRUN curl -Ssl https://downloads.gauge.org/stable | sh# Installing Gauge plugins.RUN gauge install js &&\
gauge install screenshot &&\
gauge install html-report# Set npm registryRUN npm config set strict-ssl falseRUN npm config set registry "https://nexus.sysadmin.homes/repository/npm-group/"# Installation of the required npm packagesENVTAIKO_SKIP_CHROMIUM_DOWNLOAD=trueRUN npm install --no-fund --save -g npm@latest log4js@latest xml2js@latest isomorphic-fetch@latest taiko@latest# Disabling proxyENVhttp_proxy=ENVhttps_proxy=# Set environment variables ENVNPM_CONFIG_PREFIX=/usr/local/lib/node_modulesENVPATH="${NPM_CONFIG_PREFIX}/bin:${PATH}"ENVTAIKO_BROWSER_ARGS=--no-sandbox,--start-maximized,--disable-dev-shm-usage,--headless,--disable-gpuENVTAIKO_BROWSER_PATH=/usr/bin/chromium-browser# Install Chromium browserRUN apk add chromium
Summary
In the above steps, we added the capability to retrieve NPM login data from HashiCorp Vault inside the Docker container during the build process. We used the VAULT_ADDR and VAULT_TOKEN arguments in the Dockerfile to fetch secrets directly from Vault and set them as environment variables, which are then used to configure NPM credentials in the container. This ensures that the login data is securely retrieved and used within the container without the need to store it as CI/CD variables in GitLab.