GitLab CI/CD Security
DevSecOps Pipeline Protection 2026
SAST, DAST, Secrets Management, Compliance, OIDC & Vault Integration
SASTDASTOIDCVault
GitLab CI/CD Security Architecture
Moderne CI/CD-Pipelines sind High-Value Targets. GitLab bietet integrierte DevSecOps-Funktionen: Statische und dynamische Sicherheitstests, Secret Detection, Container Scanning und Compliance-Pipelines.
Code Security
- • SAST (Statische Analyse)
- • Secret Detection
- • Dependency Scanning
- • License Compliance
Runtime Security
- • DAST (Dynamische Tests)
- • Container Scanning
- • Fuzz Testing
- • Coverage-guided
Pipeline Security
- • OIDC Authentication
- • Vault Integration
- • Compliance Pipelines
- • Approval Gates
Secure CI/CD Pipeline (.gitlab-ci.yml)
# .gitlab-ci.yml - Secure DevSecOps Pipeline
stages:
- test
- security
- build
- deploy
variables:
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: "/certs"
# Never embed secrets in variables
VAULT_ADDR: "https://vault.company.com"
# Security Templates
include:
- template: Security/SAST.gitlab-ci.yml
- template: Security/Secret-Detection.gitlab-ci.yml
- template: Security/Dependency-Scanning.gitlab-ci.yml
- template: Security/Container-Scanning.gitlab-ci.yml
- template: Security/DAST.gitlab-ci.yml
# Pre-job: Authenticate to Vault
.vault-auth:
id_tokens:
VAULT_ID_TOKEN:
aud: $${VAULT_ADDR}
script:
- export VAULT_TOKEN=$(
curl -s --request POST \
--header "X-Vault-Namespace: admin" \
--data "{ \"jwt\": \"$${VAULT_ID_TOKEN}\", \"role\": \"$${CI_PROJECT_NAME}\" }" \
$${VAULT_ADDR}/v1/auth/jwt/login | jq -r '.auth.client_token'
)
- vault kv get -field=api_key secret/data/$${CI_PROJECT_NAME}/prod
# SAST Configuration
sast:
stage: security
variables:
SAST_EXCLUDED_PATHS: "spec, test, tests, tmp, node_modules"
SAST_ANALYZER_IMAGE_TAG: "latest"
artifacts:
reports:
sast: gl-sast-report.json
paths:
- gl-sast-report.json
expire_in: 1 week
# Secret Detection
secret_detection:
stage: security
variables:
SECRET_DETECTION_HISTORIC_SCAN: "false"
SECRET_DETECTION_EXCLUDED_PATHS: "tests/"
artifacts:
reports:
secret_detection: gl-secret-detection-report.json
expire_in: 1 week
# Dependency Scanning
dependency_scanning:
stage: security
variables:
DS_EXCLUDED_PATHS: "tests/"
DS_DEFAULT_ANALYZERS: "bundler-audit,gemnasium,gemnasium-maven"
artifacts:
reports:
dependency_scanning: gl-dependency-scanning-report.json
expire_in: 1 week
# Container Scanning with Trivy
container_scanning:
stage: security
image: aquasec/trivy:latest
variables:
TRIVY_CACHE_DIR: ".trivycache/"
TRIVY_NO_PROGRESS: "true"
TRIVY_EXIT_CODE: "1" # Fail on CRITICAL
TRIVY_SEVERITY: "CRITICAL,HIGH"
script:
- trivy fs --scanners vuln,secret,misconfig .
- trivy image --exit-code $${TRIVY_EXIT_CODE} \
--severity $${TRIVY_SEVERITY} \
$${CI_REGISTRY_IMAGE}:$${CI_COMMIT_SHA}
cache:
paths:
- .trivycache/
# DAST (Dynamic Application Security Testing)
dast:
stage: security
variables:
DAST_WEBSITE: "$${CI_ENVIRONMENT_URL}"
DAST_FULL_SCAN_ENABLED: "true"
DAST_ZAP_USE_AJAX_SPIDER: "true"
DAST_ZAP_ACTIVE_SCAN: "true"
rules:
- if: $CI_PIPELINE_SOURCE == "schedule" # Nightly only
when: always
- if: $CI_COMMIT_BRANCH == "main"
when: manual # Manual on main
# Build with security hardening
build:
stage: build
image: docker:latest
services:
- docker:dind
script:
- docker build \
--build-arg VAULT_TOKEN=$${VAULT_TOKEN} \
--tag $${CI_REGISTRY_IMAGE}:$${CI_COMMIT_SHA} \
--tag $${CI_REGISTRY_IMAGE}:latest \
--file Dockerfile \
.
- docker push $${CI_REGISTRY_IMAGE}:$${CI_COMMIT_SHA}
rules:
- if: $CI_COMMIT_BRANCH == "main"
# Deploy with approval
deploy_production:
stage: deploy
environment:
name: production
url: https://app.company.com
script:
- kubectl set image deployment/app app=$${CI_REGISTRY_IMAGE}:$${CI_COMMIT_SHA}
needs:
- job: container_scanning
artifacts: true
- job: sast
artifacts: true
- job: dependency_scanning
artifacts: true
when: manual # Require manual approval
only:
- mainOIDC Authentication (Vault/AWS/Azure)
# .gitlab-ci.yml - OIDC Authentication (No Long-Term Secrets)
# AWS OIDC Authentication
.aws-oidc:
id_tokens:
GITLAB_ID_TOKEN:
aud: https://gitlab.company.com
script:
- |
# Configure AWS with OIDC
export AWS_WEB_IDENTITY_TOKEN=$${GITLAB_ID_TOKEN}
export AWS_ROLE_ARN=arn:aws:iam::123456789:role/GitLabCIRole
# Assume role via OIDC
aws sts assume-role-with-web-identity \
--role-arn $${AWS_ROLE_ARN} \
--web-identity-token $${GITLAB_ID_TOKEN} \
--duration-seconds 3600 > /tmp/credentials.json
export AWS_ACCESS_KEY_ID=$(cat /tmp/credentials.json | jq -r '.Credentials.AccessKeyId')
export AWS_SECRET_ACCESS_KEY=$(cat /tmp/credentials.json | jq -r '.Credentials.SecretAccessKey')
export AWS_SESSION_TOKEN=$(cat /tmp/credentials.json | jq -r '.Credentials.SessionToken')
# Vault OIDC Authentication
.vault-oidc:
id_tokens:
VAULT_JWT:
aud: vault.company.com
script:
- |
# Authenticate to Vault via OIDC
VAULT_TOKEN=$(curl -s -X POST \
-H "Content-Type: application/json" \
-d "{ \"role\": \"$${CI_PROJECT_PATH_SLUG}\", \"jwt\": \"$${VAULT_JWT}\" }" \
https://vault.company.com/v1/auth/jwt/login | jq -r '.auth.client_token')
export VAULT_TOKEN
# Fetch dynamic AWS credentials from Vault
aws_creds=$(vault read -format=json aws/creds/deploy-role)
export AWS_ACCESS_KEY_ID=$(echo $aws_creds | jq -r '.data.access_key')
export AWS_SECRET_ACCESS_KEY=$(echo $aws_creds | jq -r '.data.secret_key')
# Azure OIDC
.azure-oidc:
id_tokens:
GITLAB_ID_TOKEN:
aud: api://AzureADTokenExchange
script:
- |
# Azure OIDC login
az login --service-principal \
--username $${AZURE_CLIENT_ID} \
--tenant $${AZURE_TENANT_ID} \
--federated-token $${GITLAB_ID_TOKEN} \
--allow-no-subscriptions
# Get AKS credentials
az aks get-credentials \
--resource-group production-rg \
--name production-clusterCompliance Pipeline (SOC 2/ISO 27001)
# .gitlab-ci.yml - Compliance Pipeline
compliance_check:
stage: .pre
image: alpine:latest
script:
# Check branch naming convention
- |
if [[ ! "$${CI_COMMIT_BRANCH}" =~ ^(feature|bugfix|hotfix|release)/.*$ ]]; then
echo "ERROR: Branch name does not follow convention"
exit 1
fi
# Require issue link in commit
- |
if ! git log -1 --pretty=%B | grep -qE "(Closes|Fixes|Refs) #[0-9]+"; then
echo "WARNING: No issue reference in commit"
fi
# Check DCO sign-off
- |
if ! git log -1 --pretty=%B | grep -q "Signed-off-by:"; then
echo "ERROR: DCO sign-off missing"
exit 1
fi
# Artifact retention for compliance
.compliance_retention:
artifacts:
expire_in: 7 years # SOC 2 requirement
reports:
junit: junit.xml
coverage_report:
coverage_format: cobertura
path: coverage/cobertura-coverage.xml
# Audit trail
audit_trail:
stage: deploy
script:
- |
# Log deployment to audit system
curl -X POST https://audit.company.com/api/events \
-H "Authorization: Bearer $${AUDIT_TOKEN}" \
-d "{
\"event_type\": \"deployment\",
\"project\": \"$${CI_PROJECT_PATH}\",
\"commit_sha\": \"$${CI_COMMIT_SHA}\",
\"user\": \"$${GITLAB_USER_LOGIN}\",
\"environment\": \"$${CI_ENVIRONMENT_NAME}\",
\"pipeline_id\": \"$${CI_PIPELINE_ID}\"
}"