DevSecOps Fundamentals
Learn how to integrate security practices throughout the software development and deployment lifecycle.
What is DevSecOps?β
DevSecOps (Development + Security + Operations) is the practice of integrating security checks, testing, and tools throughout the entire software delivery pipelineβfrom development through production. Security is everyone's responsibility, not just the security team's.
Core Principlesβ
- Shift-Left Security β Move security checks earlier in the pipeline
- Automation β Automate security checks to enable fast feedback
- Collaboration β Development, operations, and security teams work together
- Continuous Monitoring β Monitor security throughout the application lifecycle
- Measurable Security β Track and measure security metrics
- Compliance-as-Code β Encode security and compliance requirements as code
Shift-Left Securityβ
Shift-left means moving security checks earlier in the development process, from right (deployment) to left (development).
DevSecOps Pipeline Diagramβ
Shift-Left vs Traditional Approachβ
Traditional Approach (Shift-Right)β
Code β Build β Test β Deploy β Security Scan
(too late!)
Problems:
- Vulnerabilities found late (expensive to fix)
- Developer has moved on to other work
- Fix takes time, blocks deployment
- Security seen as blocker, not enabler
DevSecOps Approach (Shift-Left)β
Pre-commit β Code β Build β Security Test β Deploy
β β
SAST, DAST, Secrets Monitoring
Linting, Scanning, SCA
Secrets
(catch early!)
Benefits:
- Issues caught when developer still focused
- Cheaper and faster to fix
- Security integrated into development
- Developers invest in security
Implementation Levelsβ
Level 1: Pre-commit hooks
# .git/hooks/pre-commit
#!/bin/bash
gitleaks detect --staged # Check for exposed secrets
eslint . # Check code quality
exit $?
Level 2: CI/CD pipeline
- SAST on every commit
- Dependency scanning
- Container image scanning
Level 3: IDE plugins
- Real-time feedback while coding
- Security warnings in editor
- Automated fixes
SAST (Static Application Security Testing)β
SAST analyzes source code without running it to find vulnerabilities.
How SAST Worksβ
Source Code
β
Lexical Analysis β Parse into tokens
β
Syntax Analysis β Build abstract syntax tree
β
Semantic Analysis β Check for vulnerabilities
β
Report Issues
Common Vulnerabilities SAST Findsβ
- SQL Injection β Unsanitized user input in database queries
- Cross-Site Scripting (XSS) β Unsanitized output to HTML
- Cross-Site Request Forgery (CSRF) β Missing CSRF tokens
- Insecure Deserialization β Untrusted data deserialization
- Hardcoded Secrets β API keys, passwords in code
- Buffer Overflow β Unchecked array/buffer access
- Weak Cryptography β Use of outdated encryption
Popular SAST Toolsβ
| Tool | Language | Type |
|---|---|---|
| SonarQube | Multiple | Self-hosted |
| Checkmarx | Multiple | Commercial |
| Semgrep | Multiple | Open-source |
| Snyk | Multiple | Cloud + CLI |
| Fortify | Multiple | Commercial |
| Bandit | Python | Open-source |
| Brakeman | Ruby | Open-source |
SAST Integration in Pipelineβ
# GitHub Actions example
- name: Run Semgrep SAST
run: |
docker run --rm -v "${{ github.workspace }}:/src" semgrep/semgrep:1.186.0 \
semgrep scan --config p/security-audit --config p/owasp-top-ten \
--sarif --output semgrep.sarif
- name: Upload SARIF
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: semgrep.sarif
DAST (Dynamic Application Security Testing)β
DAST tests running applications to find vulnerabilities in behavior.
How DAST Worksβ
Running Application
β
Send Requests β Observe Responses
β
Check for Vulnerabilities
β
Report Issues
Common Vulnerabilities DAST Findsβ
- Authentication issues β Bypass, weak session management
- Authorization flaws β Access control bypass
- Business logic flaws β Race conditions, workflow bypass
- API security β Missing auth, injection in APIs
- Configuration issues β Debug mode enabled, verbose errors
- Cryptographic issues β Weak TLS, deprecated protocols
Popular DAST Toolsβ
| Tool | Type | Best For |
|---|---|---|
| OWASP ZAP | Open-source | General web apps |
| Burp Suite | Commercial | Detailed testing |
| Acunetix | Commercial | Automated scanning |
| w3af | Open-source | Web application testing |
| Nuclei | Open-source | Template-based testing |
DAST in Pipelineβ
# GitLab CI example
dast:
stage: deploy
script:
- docker run -t owasp/zap2docker-stable zap-baseline.py
-t https://staging.example.com
allow_failure: true
SCA (Software Composition Analysis)β
SCA identifies vulnerabilities in third-party dependencies and open-source libraries.
How SCA Worksβ
Dependencies (package.json, requirements.txt, etc.)
β
Compare against vulnerability database
β
Report known vulnerabilities
β
Recommend patches/upgrades
Types of Issues Foundβ
- Known vulnerabilities in dependencies
- Outdated versions with patches available
- License compliance issues
- Malicious packages
- Unmaintained packages
Popular SCA Toolsβ
| Tool | Type | Features |
|---|---|---|
| Snyk | Cloud/CLI | Fix recommendations |
| Dependabot | GitHub-native | Auto-generated PRs |
| Black Duck | Commercial | License scanning |
| Whitesource | Commercial | Enterprise features |
| Safety | Open-source | Python-focused |
SCA Integrationβ
# Snyk CLI example
snyk test # Check for vulnerabilities
snyk fix # Auto-fix some issues
snyk monitor # Continuous monitoring
Container Security Scanningβ
Scan Docker images for vulnerabilities before deployment.
Scanning Approachesβ
Build-time scanning:
# Before pushing to registry
docker build -t myapp:latest .
trivy image myapp:latest
Registry scanning:
# Images are scanned when pushed to registry
docker push registry.example.com/myapp:latest
# Registry scans automatically
Runtime scanning:
# Monitor running containers
falco --rules=/etc/falco/rules.yaml
Popular Container Scanning Toolsβ
| Tool | Type | Features |
|---|---|---|
| Trivy | Open-source | Fast, comprehensive |
| Grype | Open-source | Accurate CVE detection |
| Clair | Open-source | Registry integration |
| Aqua | Commercial | Advanced scanning |
| Harbor | Open-source | Registry + scanning |
Scanning in Pipelineβ
# GitHub Actions
- name: Scan image with Trivy
uses: aquasecurity/trivy-action@v0.36.0
with:
image-ref: myapp:${{ github.sha }}
format: 'sarif'
output: 'trivy-results.sarif'
Secrets Managementβ
Never store secrets (passwords, keys, tokens) in code or version control.
Secrets to Protectβ
- Database credentials
- API keys and tokens
- SSH keys
- Private certificates
- Encryption keys
- OAuth credentials
Secret Detection Toolsβ
| Tool | Purpose | Method |
|---|---|---|
| GitLeaks | Detect exposed secrets | Regex patterns |
| TruffleHog | Find secrets in Git history | Entropy checking |
| Detect-Secrets | Prevent secret commits | Pre-commit hooks |
| Vault | Centralized secret storage | Encrypted backend |
Secret Management Best Practicesβ
- Never commit secrets to Git
- Use secrets management tools (Vault, AWS Secrets Manager, Azure Key Vault)
- Rotate secrets regularly (every 30-90 days)
- Scan for exposed secrets in commits and deployments
- Limit secret access (principle of least privilege)
- Audit secret access (who accessed what and when)
- Use short-lived credentials when possible
- Never log secrets (sanitize logs)
GitLeaks Exampleβ
# Pre-commit hook to prevent secret commits
gitleaks detect --staged
# Scan entire repository history
gitleaks detect --verbose
# Scan specific commit
gitleaks detect --commit abc123def
Supply Chain Securityβ
Secure the entire software supply chain from source to production.
Supply Chain Threatsβ
- Compromised dependencies β Malicious code in libraries
- Compromised source β Attacker gains access to repository
- Compromised build β Build system is compromised
- Compromised artifact β Docker image is tampered with
- Compromised registry β Package registry is compromised
SBOM (Software Bill of Materials)β
Document every component in your software:
{
"bomFormat": "CycloneDX",
"specVersion": "1.4",
"components": [
{
"type": "library",
"name": "lodash",
"version": "4.17.21",
"purl": "pkg:npm/lodash@4.17.21"
}
]
}
Supply Chain Protectionsβ
- Dependency pinning β Lock exact versions
- Signature verification β Verify artifact signatures
- Build attestation β Certify who built what
- Artifact scanning β Scan for vulnerabilities
- Source code scanning β SAST and secrets detection
- Access controls β Limit who can change what
Compliance-as-Codeβ
Encode security and compliance requirements as code to enforce them automatically.
Examplesβ
# Terraform security check
resource "aws_s3_bucket" "example" {
bucket = "my-bucket"
# Enforce encryption
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
# Block public access
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
Infrastructure-as-Code Scanningβ
# Checkov scans Terraform for security issues
checkov -f main.tf --framework terraform
# Snyk scans Kubernetes manifests
snyk iac test kubernetes-deployment.yaml
OWASP Top 10β
The 10 most critical web application security risks.
| Risk | Description | Prevention |
|---|---|---|
| Injection | SQL injection, command injection | Use parameterized queries, input validation |
| Broken Authentication | Weak password, session fixation | MFA, strong passwords, secure session handling |
| Sensitive Data Exposure | Unencrypted PII, weak encryption | Use HTTPS, encrypt data, mask PII |
| XML External Entities (XXE) | XXE injection attacks | Disable XXE parsing, validate XML |
| Broken Access Control | Authorization bypass | Implement proper access control checks |
| Security Misconfiguration | Debug mode, outdated libraries | Keep systems patched, secure defaults |
| XSS (Cross-Site Scripting) | Execute script in user browser | Input validation, output encoding, CSP headers |
| Insecure Deserialization | Untrusted data deserialization | Avoid deserialization, use safe libraries |
| Using Components with Known Vulnerabilities | Outdated dependencies | Keep dependencies updated, use SCA tools |
| Insufficient Logging & Monitoring | Missing security logs | Log security events, monitor for anomalies |
DevSecOps Pipeline Exampleβ
stages:
- source
- build
- test
- deploy
- monitor
pre-commit:
stage: source
script:
- gitleaks detect --staged
- eslint .
- npm audit
build:
stage: build
script:
- npm install
- npm run build
- docker build -t myapp:$CI_COMMIT_SHA .
sast:
stage: test
script:
- semgrep --config=p/security-audit --json --output=semgrep.json .
sca:
stage: test
script:
- snyk test --json > snyk-results.json
container-scan:
stage: test
script:
- trivy image myapp:$CI_COMMIT_SHA --format sarif
dast:
stage: test
script:
- docker run -t owasp/zap2docker-stable zap-baseline.py -t https://staging.example.com
deploy:
stage: deploy
script:
- docker push registry.example.com/myapp:$CI_COMMIT_SHA
monitor:
stage: monitor
script:
- snyk monitor
- falco monitor
Best Practicesβ
1. Integrate Security Earlyβ
- Pre-commit hooks for local checks
- SAST on every commit
- Dependency scanning for all changes
2. Make Security Accessibleβ
- Show security issues in code review
- Provide remediation guidance
- Don't blame, educate
3. Automate Everythingβ
- Automated SAST/DAST/SCA
- Automated scanning in pipeline
- Automated patching for some vulnerabilities
4. Monitor Continuouslyβ
- Scan production applications
- Monitor for new vulnerabilities
- Track security metrics
5. Secure the Supply Chainβ
- Verify software integrity
- Document dependencies (SBOM)
- Scan artifacts before deployment
Exercisesβ
Exercise 1: SAST Setupβ
Set up Semgrep or SonarQube for a project and:
- Identify top 10 security issues
- Fix the most critical ones
- Integrate into CI/CD pipeline
Exercise 2: Secrets Detectionβ
Scan a repository for exposed secrets:
- Use GitLeaks to find existing secrets
- Add pre-commit hook to prevent new secrets
- Rotate any found secrets
Exercise 3: Container Securityβ
Build and scan a Docker image:
- Create a Dockerfile
- Scan with Trivy
- Fix identified vulnerabilities
- Scan again to verify fixes
Exercise 4: OWASP Top 10 Reviewβ
For a web application:
- Identify which OWASP risks apply
- Find examples of each in code
- Propose fixes
- Implement fixes
Key Takeawaysβ
- DevSecOps integrates security throughout the pipeline, not just at the end
- Shift-left moves security earlier, enabling faster, cheaper fixes
- SAST finds code vulnerabilities before runtime
- DAST finds application vulnerabilities in running code
- SCA identifies vulnerable dependencies and licenses
- Container scanning catches image vulnerabilities before deployment
- Secrets must never be in code β use management tools instead
- Supply chain security protects the entire development process
- Compliance-as-code automates security and compliance checking
- OWASP Top 10 represents the most critical web security risks