-
Notifications
You must be signed in to change notification settings - Fork 0
feat: examples, smoke test, Makefile, dependabot #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d76cc2f
feat: add examples, smoke test, Makefile, dependabot
kanywst 2ab2758
ci: pin golangci-lint v2.12.2 (action v9) — v1.64 was built with Go 1…
kanywst 60e95bb
chore: address PR review feedback
kanywst 4dd0ba3
chore(release): use `go mod verify` (non-mutating) in goreleaser befo…
kanywst 3325591
ci: add smoke / vuln / actionlint / coverage jobs
kanywst dfcbb57
ci(vuln): correct osv-scanner-action path (uses subaction at osv-scan…
kanywst a2fc650
ci(vuln): drop --skip-git arg (removed in osv-scanner v2)
kanywst 99538b8
chore(deps): bump x/crypto v0.50→0.52 and x/sys v0.43→0.45 to clear o…
kanywst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| version: 2 | ||
| updates: | ||
| - package-ecosystem: gomod | ||
| directory: / | ||
| schedule: | ||
| interval: weekly | ||
| day: monday | ||
| open-pull-requests-limit: 5 | ||
| groups: | ||
| go-deps: | ||
| patterns: ["*"] | ||
|
|
||
| - package-ecosystem: github-actions | ||
| directory: / | ||
| schedule: | ||
| interval: weekly | ||
| day: monday | ||
| open-pull-requests-limit: 5 | ||
| groups: | ||
| actions: | ||
| patterns: ["*"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| BIN := mcp-opa | ||
| PKG := ./... | ||
|
|
||
| .PHONY: build test vet lint smoke clean fmt tidy | ||
|
|
||
| build: | ||
| go build -trimpath -ldflags "-s -w" -o $(BIN) . | ||
|
|
||
| test: | ||
| go test -race -count=1 -v $(PKG) | ||
|
|
||
| vet: | ||
| go vet $(PKG) | ||
|
|
||
| lint: | ||
| golangci-lint run $(PKG) | ||
|
|
||
| smoke: build | ||
| ./scripts/smoke.sh ./$(BIN) | ||
|
|
||
| fmt: | ||
| gofmt -s -w . | ||
|
|
||
| tidy: | ||
| go mod tidy | ||
|
|
||
| clean: | ||
| rm -f $(BIN) | ||
| rm -rf dist/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Examples | ||
|
|
||
| Reference Rego policies you can paste into `evaluate_policy` to see how `mcp-opa` behaves. | ||
|
|
||
| | File | Pattern | | ||
| | ------------------------------------------ | --------------------------------------------- | | ||
| | [`rbac.rego`](./rbac.rego) | Role-based: roles → permissions | | ||
| | [`abac.rego`](./abac.rego) | Attribute-based: subject + resource matching | | ||
| | [`k8s_admission.rego`](./k8s_admission.rego) | Kubernetes admission control (require labels) | | ||
|
|
||
| ## Running an example end-to-end | ||
|
|
||
| ```bash | ||
| # 1. Start mcp-opa from any MCP client config (Claude Code shown): | ||
| claude mcp add opa -- mcp-opa | ||
|
|
||
| # 2. In a session, ask: | ||
| # "Evaluate examples/rbac.rego for alice trying to delete document doc-1. | ||
| # What's the query?" | ||
| # | ||
| # Claude will read the rego, pick `data.rbac.allow`, send input | ||
| # {"user": "alice", "action": "delete", "resource": "doc-1"}, | ||
| # and read back the decision. | ||
| ``` | ||
|
|
||
| You can also drive it manually with `curl` over stdio (advanced — MCP is JSON-RPC over stdin/stdout): | ||
|
|
||
| ```bash | ||
| echo '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' | mcp-opa | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package abac | ||
|
|
||
| # Attribute-based access control: subject + resource attributes must align. | ||
| # | ||
| # Query: data.abac.allow | ||
| # Input: { | ||
| # "subject": {"id": "alice", "clearance": "secret"}, | ||
| # "resource": {"id": "report-99", "classification": "secret"}, | ||
| # "action": "read" | ||
| # } | ||
|
|
||
| default allow := false | ||
|
|
||
| levels := {"public": 0, "internal": 1, "confidential": 2, "secret": 3} | ||
|
|
||
| allow if { | ||
| input.action == "read" | ||
| levels[input.subject.clearance] >= levels[input.resource.classification] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package k8s.admission | ||
|
|
||
| # Kubernetes admission control: every Pod must carry an `app` label. | ||
| # | ||
| # Query: data.k8s.admission.violations[_] | ||
| # Input: the AdmissionReview request (or just the Pod object's metadata) | ||
|
|
||
| violations contains msg if { | ||
| input.kind.kind == "Pod" | ||
| not input.object.metadata.labels.app | ||
| msg := sprintf("Pod %s/%s is missing required label `app`", [ | ||
| input.object.metadata.namespace, | ||
| input.object.metadata.name, | ||
| ]) | ||
| } | ||
|
kanywst marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package rbac | ||
|
|
||
| # Role-based access control. | ||
| # | ||
| # Query: data.rbac.allow | ||
| # Input: {"user": "alice", "action": "read", "resource": "doc-1"} | ||
|
|
||
| default allow := false | ||
|
|
||
| roles := { | ||
| "alice": {"editor"}, | ||
| "bob": {"viewer"}, | ||
| "carol": {"viewer", "auditor"}, | ||
| } | ||
|
|
||
| permissions := { | ||
| "editor": {"read", "write", "delete"}, | ||
| "viewer": {"read"}, | ||
| "auditor": {"read", "audit"}, | ||
| } | ||
|
|
||
| allow if { | ||
| some role in roles[input.user] | ||
| some perm in permissions[role] | ||
| perm == input.action | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #!/usr/bin/env bash | ||
| # Drive mcp-opa over stdio with a full MCP handshake and one tools/call. | ||
| # Useful before tagging a release, or after upgrading mcp-go. | ||
| # | ||
| # Exit codes: | ||
| # 0 decision was true (policy allowed) | ||
| # 1 decision was false (policy denied or no allow rule matched) | ||
| # 2 protocol failure (no result, no decision in payload) | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| BIN="${1:-./mcp-opa}" | ||
| if [[ ! -x "$BIN" ]]; then | ||
| echo "build first: go build ." >&2 | ||
| exit 2 | ||
| fi | ||
|
|
||
| read -r -d '' REGO <<'EOF' || true | ||
| package smoke | ||
|
|
||
| default allow := false | ||
|
|
||
| allow if input.user == "alice" | ||
| EOF | ||
|
|
||
| OUT=$(printf '%s\n' \ | ||
| '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"smoke","version":"0"}}}' \ | ||
| '{"jsonrpc":"2.0","method":"notifications/initialized"}' \ | ||
| "$(jq -nc --arg r "$REGO" '{jsonrpc:"2.0",id:2,method:"tools/call",params:{name:"evaluate_policy",arguments:{rego:$r,query:"data.smoke.allow",input_json:"{\"user\":\"alice\"}"}}}')" \ | ||
| | "$BIN") | ||
|
|
||
| DECISION=$(printf '%s\n' "$OUT" | tail -1 | jq -r '.result.content[0].text' | jq -r '.[0].expressions[0].value') | ||
|
kanywst marked this conversation as resolved.
Outdated
|
||
|
|
||
| case "$DECISION" in | ||
| true) echo "✓ smoke: allow=true"; exit 0 ;; | ||
| false) echo "✗ smoke: allow=false (expected true)"; exit 1 ;; | ||
| *) echo "✗ smoke: protocol failure; payload:"; printf '%s\n' "$OUT"; exit 2 ;; | ||
| esac | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.