โ๏ธ Usageยถ
๐ณ Docker imageยถ
To get GitLab Guard, use the following Docker image:
harbor.confirm.ch/gitlab-guard/gitlab-guard
โ๏ธ CLIยถ
GitLab Guard provides a CLI
usage: gitlab-guard [-h] [-C] [-f] [-n NAMESPACE] [-t TOKEN] [-r GROUP] [--token-expiry-days DAYS] [-u URL] [-v]
The guardian of GitLab projects.
options:
-h, --help show this help message and exit
-C, --colours display ANSI colours during logging
-f, --fix fix violations automatically (alt. GLG_FIX environment variable)
-n, --namespace NAMESPACE GitLab namespace (alt. GLG_GITLAB_NAMESPACE environment variable)
-t, --token TOKEN GitLab access token (alt. GLG_GITLAB_TOKEN environment variable)
-r, --rotate-token-group GROUP rotate group access token (alt. GLG_ROTATE_TOKEN_GROUP environment variable)
--token-expiry-days DAYS lifetime of the rotated token (alt. GLG_TOKEN_EXPIRY_DAYS environment variable)
-u, --url URL GitLab URL (alt. GLG_GITLAB_URL environment variable)
-v, --verbose enable verbose mode (alt. GLG_VERBOSE environment variable)
Note
For example values, check the examples in the ๐ Environment variables below.
๐ Environment variablesยถ
Instead of the โ๏ธ CLI arguments, the following environment variables, resp. CI variables can be used:
Variable |
Description |
Example |
|---|---|---|
|
Fix violations automatically (aka donโt dry run) |
|
|
The GitLab namespace path |
|
|
The GitLab access token |
|
|
The GitLab base URL |
|
|
Group (ID/path) whose access token is rotated |
|
|
Lifetime (in days) of the rotated token |
|
|
Enable verbose mode (e.g. for debugging) |
|
Hint
Mark the GLG_GITLAB_TOKEN CI/CD variable as masked (and ideally protected) so the rotated token never appears in job logs.
๐ Token rotationยถ
Set GLG_ROTATE_TOKEN_GROUP to the ID (or path) of the group that owns the access token to enable rotation.
When it is set, GitLab Guard rotates its own token at the end of every run; when it is unset, rotation is skipped:
It calls the groupโs
access_tokens/self/rotateAPI endpoint to rotate the group access token it authenticated with.GitLab immediately revokes the old token and issues a new one.
The new token is written back into the
GLG_GITLAB_TOKENCI/CD variable of the project GitLab Guard runs in (identified via theCI_PROJECT_IDCI variable), so the next run picks it up automatically.
Important
Token rotation only works when the following requirements are met:
GitLab Guard runs in a GitLab CI pipeline (
CI_PROJECT_IDmust be set).The token is a group access token of the
GLG_ROTATE_TOKEN_GROUPgroup, with theapiscope and theOwnerrole.GLG_GITLAB_TOKENexists as a CI/CD variable on the project (so it can be updated).
Warning
A rotated token is short-lived.
If you donโt set GLG_TOKEN_EXPIRY_DAYS, GitLab applies its own default lifetime (one week).
Make sure the lifetime is longer than your pipeline schedule, otherwise the token expires between runs and locks GitLab Guard out.
๐ GitLab integrationยถ
GitLab CIยถ
Important
To run GitLab Guard, you must ensure the following requirements are met:
GitLab CI/CD is configured
A GitLab runner with the Docker executor is registered
Your project can access the runner with the Docker executor
If the requirements are met, GitLab Guard can be configured to run in the CI/CD pipeline.
Create a .gitlab-ci.yml file with the following content:
Run GitLab Guard:
stage: deploy
image: harbor.confirm.ch/gitlab-guard/gitlab-guard:latest
script:
- gitlab-guard -C -n NAMESPACE -f
rules:
- if: $CI_PIPELINE_SOURCE == 'web' # triggers when explicitly selecting ยซNew pipelineยป in the GitLab UI
- if: $CI_PIPELINE_SOURCE == 'schedule' # triggers when a scheduled pipeline is executed
Hint
Feel free to update the rules: according to your need, for example:
To run it on every push, remove the
rules:completely.To run it only on pushes to the main branch, use
if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCHTo run it only when a specific CI variable is set, use
if: $TRIGGER_GITLAB_GUARD
Schedulingยถ
If you want to run GitGuard scheduled, you can use scheduled pipelines.