Security Hardening and Audit
Harden your OpenCode deployment for security. Learn permission systems, audit logging, secrets management, and compliance patterns for production environments.
Security Layers
Permission System
Principle of Least Privilege
{
"permissions": [
{
"tool": "bash",
"allow": ["npm test", "npm run lint", "git status"],
"deny": ["sudo *", "rm -rf *", "git push --force"]
},
{
"tool": "write",
"allow": ["src/**", "tests/**"],
"deny": [".env*", "secrets/**", "*.key", "*.pem"]
},
{
"tool": "read",
"allow": ["**"],
"deny": [".env*", "secrets/**"]
}
]
}Permission Patterns
Pattern | Example | Matches |
|---|---|---|
Exact | npm test | Only npm test |
Wildcard | npm * | Any npm command |
Path | src/** | Any file in src/ |
Negation | *.key | Any .key file |
Audit Logging
Enable Comprehensive Logging
{
"logging": {
"enabled": true,
"level": "info",
"file": "opencode-audit.log",
"rotate": {
"maxSize": "10MB",
"maxFiles": 30
}
}
}Log Format
{
"timestamp": "2024-01-15T10:30:00Z",
"level": "info",
"event": "tool.execute",
"tool": "bash",
"args": {"command": "npm test"},
"result": "success",
"user": "developer1",
"session": "abc123"
}Analyzing Logs
# Find all failed operations
grep '"result":"error"' opencode-audit.log
# Find bash commands
grep '"tool":"bash"' opencode-audit.log
# Find write operations to sensitive files
grep '"tool":"write"' opencode-audit.log | grep -E '\.env|secrets'Secrets Management
Environment Variables
# .env (never commit)
OPENAI_API_KEY=sk-your-key
ANTHROPIC_API_KEY=sk-ant-your-key
DATABASE_URL=postgresql://...Reference in Config
{
"providers": {
"openai": {
"apiKey": "${OPENAI_API_KEY}"
}
}
}.gitignore
.env
.env.*
*.key
*.pem
secrets/Network Security
API Key Rotation
# Generate new key
# Update .env
# Restart OpenCode
# Verify old key is invalidatedRate Limiting
{
"providers": {
"openai": {
"apiKey": "${OPENAI_API_KEY}",
"rateLimit": {
"requests": 60,
"window": "1m"
}
}
}
}Timeout Configuration
{
"providers": {
"openai": {
"timeout": 30000
}
}
}Compliance Patterns
Data Residency
{
"providers": {
"openai": {
"apiKey": "${OPENAI_API_KEY}",
"region": "us-east-1"
}
}
}Data Retention
{
"logging": {
"retention": {
"days": 90,
"autoDelete": true
}
}
}PII Redaction
{
"security": {
"redact": {
"patterns": [
"\\b\\d{3}-\\d{2}-\\d{4}\\b",
"\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b"
],
"replacement": "[REDACTED]"
}
}
}Security Checklist
Before Deployment
- API keys in environment variables
- .env in .gitignore
- Permission rules configured
- Audit logging enabled
- Rate limiting configured
- Timeouts set
- PII redaction enabled
Regular Audits
- Review audit logs weekly
- Rotate API keys monthly
- Update permission rules quarterly
- Review security patterns annually
Practice Questions
How to answer
Multiple-choice question with 4 options. Select one option, then choose Check Answer.
What is the principle of least privilege?
How to answer
Multiple-choice question with 4 options. Select one option, then choose Check Answer.
Where should API keys be stored?
How to answer
Multiple-choice question with 4 options. Select one option, then choose Check Answer.
What does PII redaction do?
How to answer
Multiple-choice question with 4 options. Select one option, then choose Check Answer.
How often should you rotate API keys?
How to answer
Multiple-choice question with 4 options. Select one option, then choose Check Answer.
What should audit logs track?
Key Takeaways
- Apply least privilege principle to all permission rules
- Store API keys in environment variables, never in code
- Enable comprehensive audit logging for compliance
- Use PII redaction to protect sensitive data in logs
- Rotate API keys monthly and review permissions quarterly
- Configure rate limits and timeouts to prevent abuse
- Regular security audits catch issues before they become breaches