Policy as Code
Automate security policy enforcement with CEL-based rules
What is Policy as Code?
Policy as Code treats security policies as configuration files that can be evaluated by tools to make runtime decisions. This approach reduces the cost of compliance by automating the grunt work required to audit for policy violations.
Why Use Policy as Code?
Manual verification and approval of new software components is tedious, error-prone, and incomplete. It’s impossible to manually analyze a 3rd party dependency and all its transitive dependencies. Organizational software supply chain policies should be codified so tools can automatically evaluate every artifact for compliance.
Common Policy Examples
Vulnerability Policies
Dependencies must not have known critical vulnerabilities
Maintenance Policies
Dependencies must be actively maintained
Popularity Policies
Dependencies must have an extensive user base
Security Standards
Publishers must follow security standards like SLSA
How Filter Suites Work
A Filter Suite allows you to combine multiple security policies into a single, comprehensive ruleset. Instead of writing individual filters, you can express complex organizational policies in a structured format.
Example Filter Suite
Here’s a comprehensive filter suite that implements common security policies:
Using Filter Suites
Basic Usage
Apply a filter suite to scan a project:
Directory Scanning
Scan multiple manifest files:
CI/CD Integration
Use filter suites in your CI pipeline:
Advanced Policy Examples
License Compliance Policy
Supply Chain Security Policy
Development vs Production Policies
Filter Suite Evaluation
Filter suites are evaluated as:
- Ordered list of filters as defined in the suite file
- Stop on first match for each package
- Return violation if any filter matches
Order your filters from most specific to most general for optimal performance.
Best Practices
Version Control Policies
Version Control Policies
Store your filter suites in version control alongside your code to ensure policy changes are tracked and reviewed.
Environment-Specific Policies
Environment-Specific Policies
Use different policies for development, staging, and production environments with varying strictness levels.
Policy Testing
Policy Testing
Test your policies against known good and bad packages before deploying to production.
Gradual Rollout
Gradual Rollout
Start with warning-only mode before enforcing blocking policies to understand the impact.
CEL Language Reference
The Common Expression Language (CEL) provides the foundation for writing policies. Key concepts:
Available Data Fields
vulns.critical
,vulns.high
,vulns.medium
,vulns.low
- Vulnerability arrayslicenses
- Array of license identifiersscorecard.scores.*
- OpenSSF Scorecard metricspackage.*
- Package metadata (name, version, published_at, etc.)
Common Operations
size()
- Get array lengthexists(item, condition)
- Check if condition exists for any itemin
- Check membership in arraycontains()
- String contains check