Proč je CI/CD Security důležitá?
CI/CD pipeline má přístup k vašemu zdrojovému kódu, credentials, production prostředím a artifact repositories.
Kompromitovaná pipeline může být využita k nasazení malware, krádeži secrets nebo přístupu do production.
Zabezpečení pipeline je kritickou součástí DevSecOps strategie.
DevSecOps Pipeline architektura
Security Gates v CI/CD
1. Pre-commit
Kontroly před commitnutím kódu
- Pre-commit hooks (secrets detection)
- Linting a formátování
- Local SAST scan
2. Build Stage
Kontroly při buildování
- SAST - statická analýza kódu
- SCA - analýza závislostí
- Secret scanning (GitLeaks)
- License compliance
3. Test Stage
Security testy
- Unit testy se security asserty
- Container image scanning (Trivy)
- IaC scanning (tfsec, Checkov)
4. Deploy Stage
Kontroly před nasazením
- DAST - dynamické testování
- Approval gates pro production
- Deployment verification
5. Post-deploy
Kontinuální monitoring
- Runtime security monitoring
- Vulnerability alerts
- Compliance checks
Secrets Management
Správné zacházení se secrets (API klíče, hesla, certificates) je jedna z nejdůležitějších částí CI/CD security.
NIKDY nedělejte
- Hardcoded secrets v kódu nebo pipeline konfiguracích
- Secrets v environment variables bez šifrování
- Sdílení secrets mezi prostředími (dev/prod)
- Dlouhodobé statické access keys
- Secrets v Docker images
Správné řešení secrets
# GitLab CI - použití protected variables
variables:
# Reference na secret uložený v GitLab CI/CD settings
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
# Lepší - použití OIDC (žádné statické klíče)
deploy:
id_tokens:
GITLAB_OIDC_TOKEN:
aud: https://gitlab.com
script:
- |
export $(aws sts assume-role-with-web-identity \
--role-arn $AWS_ROLE_ARN \
--role-session-name "gitlab-$CI_JOB_ID" \
--web-identity-token $GITLAB_OIDC_TOKEN \
--query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]' \
--output text | xargs -n1 echo)
# Nejlepší - použití external secrets manager
deploy:
before_script:
- |
# Získání secrets z AWS Secrets Manager
export DB_PASSWORD=$(aws secretsmanager get-secret-value \
--secret-id prod/db/password \
--query SecretString --output text)
Příklad: Bezpečná DevSecOps Pipeline
# .gitlab-ci.yml - DevSecOps Pipeline
stages:
- test
- build
- scan
- deploy
variables:
DOCKER_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
# ===== TEST STAGE =====
# Secret detection
gitleaks:
stage: test
image: zricethezav/gitleaks:latest
script:
- gitleaks detect --source . --report-path gitleaks-report.json
artifacts:
paths: [gitleaks-report.json]
when: always
allow_failure: true
# SAST - statická analýza
semgrep:
stage: test
image: returntocorp/semgrep
script:
- semgrep ci --json --output semgrep-results.json
artifacts:
paths: [semgrep-results.json]
when: always
allow_failure: true
# SCA - dependency scanning
dependency_check:
stage: test
image: owasp/dependency-check:latest
script:
- /usr/share/dependency-check/bin/dependency-check.sh \
--project "MyApp" \
--scan . \
--format JSON \
--out dependency-check-report.json
artifacts:
paths: [dependency-check-report.json]
when: always
allow_failure: true
# ===== BUILD STAGE =====
build_image:
stage: build
image: docker:24
services:
- docker:24-dind
script:
- docker build -t $DOCKER_IMAGE .
- docker push $DOCKER_IMAGE
only:
- main
# ===== SCAN STAGE =====
# Container image scanning
trivy:
stage: scan
image: aquasec/trivy:latest
script:
- trivy image --exit-code 0 --severity HIGH,CRITICAL \
--format json --output trivy-results.json $DOCKER_IMAGE
artifacts:
paths: [trivy-results.json]
when: always
dependencies:
- build_image
# ===== DEPLOY STAGE =====
deploy_staging:
stage: deploy
environment:
name: staging
script:
- echo "Deploying to staging..."
only:
- main
deploy_production:
stage: deploy
environment:
name: production
script:
- echo "Deploying to production..."
when: manual # Vyžaduje manuální schválení
only:
- main
dependencies:
- deploy_staging
CI/CD Security Best Practices
Pipeline Configuration
- Pipeline as Code v Git repozitáři
- Protected branches - žádné přímé pushe do main
- Required code reviews
- Signed commits
- Pinned versions nástrojů a images
Secrets & Access
- OIDC/Workload Identity místo statických klíčů
- External secrets manager (Vault, AWS Secrets Manager)
- Rotace secrets
- Least privilege pro pipeline service accounts
- Audit logging všech přístupů
Security Gates
- Fail pipeline na critical findings
- Manual approval pro production deploys
- Security review pro infrastructure změny
- Rollback mechanismy
- Deployment windows
Bezpečnostní rizika v CI/CD
- Poisoned Pipeline Execution (PPE) - manipulace pipeline konfigurací
- Dependency Confusion - zneužití package managerů
- Credential theft z pipeline logů
- Supply chain attacks přes build nástroje
- Privilege escalation přes self-hosted runnery