first commit
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
# =============================================================================
|
||||
# AI Gateway Policy — OPA/Rego Reference Implementation
|
||||
# =============================================================================
|
||||
# This file is a reference-only translation of policies.yaml into OPA Rego.
|
||||
# It is NOT processed by the gateway; it exists to show how the same rules
|
||||
# would look if evaluated by Open Policy Agent.
|
||||
# =============================================================================
|
||||
|
||||
package gateway.policy
|
||||
|
||||
import rego.v1
|
||||
|
||||
# ---------- data ----------
|
||||
allowed_projects := {"SAFE", "PLATFORM", "OPS"}
|
||||
allowed_changelogs := {"release_*.xml", "hotfix_*.xml"}
|
||||
business_hours := {"start": "08:00", "end": "18:00", "timezone": "Europe/Oslo"}
|
||||
rate_limit_default := 30
|
||||
|
||||
# ---------- default decision ----------
|
||||
default decision := "deny"
|
||||
|
||||
# ---------- scope-based allow ----------
|
||||
decision := "allow" if {
|
||||
input.tool == "jira.*" # pattern: starts_with(input.tool, "jira.")
|
||||
startswith(input.tool, "jira.")
|
||||
"jira.read" in input.subject.scopes
|
||||
input.action in {"read", "get", "list", "search"}
|
||||
}
|
||||
|
||||
decision := "allow" if {
|
||||
startswith(input.tool, "jira.")
|
||||
"jira.write" in input.subject.scopes
|
||||
input.action in {"create", "update", "delete"}
|
||||
}
|
||||
|
||||
decision := "allow" if {
|
||||
input.tool == "db.queryReadonly"
|
||||
"db.read" in input.subject.scopes
|
||||
}
|
||||
|
||||
# ---------- environment-based rules ----------
|
||||
decision := "allow" if {
|
||||
input.tool == "jenkins.deploy"
|
||||
input.context.environment == "staging"
|
||||
}
|
||||
|
||||
decision := "require_approval" if {
|
||||
input.tool == "jenkins.deploy"
|
||||
input.context.environment == "prod"
|
||||
}
|
||||
|
||||
# ---------- argument allowlist ----------
|
||||
decision := "deny" if {
|
||||
input.tool == "jira.createTicket"
|
||||
not input.arguments.project in allowed_projects
|
||||
}
|
||||
|
||||
decision := "deny" if {
|
||||
input.tool == "db.runLiquibase"
|
||||
not glob_match_any(allowed_changelogs, input.arguments.changelog)
|
||||
}
|
||||
|
||||
# ---------- block rule ----------
|
||||
decision := "deny" if {
|
||||
input.tool == "db.executeScript"
|
||||
}
|
||||
|
||||
# ---------- rate limit ----------
|
||||
decision := "deny" if {
|
||||
input.context.rate.current > rate_limit_default
|
||||
}
|
||||
|
||||
# ---------- time window ----------
|
||||
decision := "deny" if {
|
||||
input.tool == "jenkins.deploy"
|
||||
not within_business_hours(input.context.timestamp)
|
||||
}
|
||||
|
||||
# ---------- helpers ----------
|
||||
within_business_hours(timestamp) if {
|
||||
# Simplified: real implementation would parse timestamp and convert to Europe/Oslo
|
||||
hour := to_number(substring(timestamp, 11, 2))
|
||||
hour >= 8
|
||||
hour < 18
|
||||
}
|
||||
|
||||
glob_match_any(patterns, value) if {
|
||||
some pattern in patterns
|
||||
glob.match(pattern, ["/"], value)
|
||||
}
|
||||
|
||||
# ---------- reason ----------
|
||||
reason := "Project not in allowlist" if {
|
||||
input.tool == "jira.createTicket"
|
||||
not input.arguments.project in allowed_projects
|
||||
}
|
||||
|
||||
reason := "Changelog not in allowlist" if {
|
||||
input.tool == "db.runLiquibase"
|
||||
not glob_match_any(allowed_changelogs, input.arguments.changelog)
|
||||
}
|
||||
|
||||
reason := "db.executeScript is blocked by policy" if {
|
||||
input.tool == "db.executeScript"
|
||||
}
|
||||
|
||||
reason := "Rate limit exceeded" if {
|
||||
input.context.rate.current > rate_limit_default
|
||||
}
|
||||
|
||||
reason := "Deployments only allowed during business hours" if {
|
||||
input.tool == "jenkins.deploy"
|
||||
not within_business_hours(input.context.timestamp)
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
apiVersion: gateway.bquarkz.net/v1
|
||||
kind: PolicyConfig
|
||||
metadata:
|
||||
name: ai-gateway-policies
|
||||
spec:
|
||||
defaultDecision: deny
|
||||
data:
|
||||
allowed_projects: [SAFE, PLATFORM, OPS]
|
||||
allowed_changelogs: ["release_*.xml", "hotfix_*.xml"]
|
||||
business_hours:
|
||||
start: "08:00"
|
||||
end: "18:00"
|
||||
timezone: "Europe/Oslo"
|
||||
rate_limits:
|
||||
default: 30
|
||||
db.queryReadonly: 60
|
||||
rules:
|
||||
- name: env-deploy-rules
|
||||
type: environment
|
||||
tool: jenkins.deploy
|
||||
when:
|
||||
staging: allow
|
||||
prod: require_approval
|
||||
- name: allow-jira-read
|
||||
type: scope
|
||||
decision: allow
|
||||
tool: "jira.*"
|
||||
required_scope: jira.read
|
||||
actions: [read, get, list, search]
|
||||
- name: allow-jira-write
|
||||
type: scope
|
||||
decision: allow
|
||||
tool: "jira.*"
|
||||
required_scope: jira.write
|
||||
actions: [create, update, delete]
|
||||
- name: allow-db-readonly
|
||||
type: scope
|
||||
decision: allow
|
||||
tool: db.queryReadonly
|
||||
required_scope: db.read
|
||||
- name: restrict-jira-projects
|
||||
type: argument_allowlist
|
||||
decision: deny
|
||||
tool: jira.createTicket
|
||||
argument: project
|
||||
allowlist: "$data.allowed_projects"
|
||||
reason: "Project not in allowlist"
|
||||
- name: restrict-liquibase-changelogs
|
||||
type: argument_allowlist
|
||||
decision: deny
|
||||
tool: db.runLiquibase
|
||||
argument: changelog
|
||||
allowlist: "$data.allowed_changelogs"
|
||||
reason: "Changelog not in allowlist"
|
||||
- name: deny-db-execute-script
|
||||
type: block
|
||||
tool: db.executeScript
|
||||
reason: "db.executeScript is blocked by policy"
|
||||
- name: rate-limit-default
|
||||
type: rate_limit
|
||||
decision: deny
|
||||
tool: "*"
|
||||
max_per_minute: 30
|
||||
reason: "Rate limit exceeded"
|
||||
- name: deny-deploy-outside-hours
|
||||
type: time_window
|
||||
decision: deny
|
||||
tool: jenkins.deploy
|
||||
window:
|
||||
start: "08:00"
|
||||
end: "18:00"
|
||||
timezone: "Europe/Oslo"
|
||||
reason: "Deployments only allowed during business hours"
|
||||
Reference in New Issue
Block a user