generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add policy evaluation example #2
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 all commits
Commits
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
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,3 @@ | ||
| .venv/ | ||
| venv/ | ||
| bundles/ |
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,6 @@ | ||
| hooks: | ||
| PreToolUse: | ||
| - matcher: "oci_*" | ||
| hooks: | ||
| - type: command | ||
| command: "uv run ./hooks/opa.py" |
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 @@ | ||
| 3.13 |
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,13 @@ | ||
| .PHONY: all build test test clean | ||
|
|
||
| all: build | ||
|
|
||
| build: | ||
| mkdir -p bundles | ||
| opa build -o bundles/bundle.tar.gz policies | ||
|
|
||
| test: . | ||
| opa test -v policies | ||
|
|
||
| clean: | ||
| rm -rf bundles/* |
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,70 @@ | ||
| # Policy | ||
|
|
||
| Demonstration of out-of-band policy enforcement for MCP servers/tools | ||
|
|
||
| ## Getting started | ||
|
|
||
| ### Install dependencies | ||
| ```bash | ||
| brew install ollama python3 uv go opa podman podman-compose | ||
| ``` | ||
|
|
||
| ### Create container VM for running containers via podman | ||
| ```bash | ||
| podman machine init | ||
| podman machine start | ||
| ``` | ||
|
|
||
| ### Build policies | ||
| ``` | ||
| make build | ||
| ``` | ||
|
|
||
| ### Install Ollama and fetch model | ||
|
|
||
| Where `$MODEL` is the name of the model you prefer (ex: `gpt-oss`) | ||
|
|
||
| ```bash | ||
| ollama pull $MODEL | ||
| ``` | ||
|
|
||
| ### Install mcphost | ||
| ```bash | ||
| go install github.com/mark3labs/mcphost@latest | ||
| export PATH=$PATH:~/.go/bin | ||
| ``` | ||
|
|
||
| ### Authenticate | ||
|
|
||
| ```bash | ||
| oci session authenticate --profile-name <profile name, ex: DEFAULT> --region <region name: us-sanjose-1> | ||
| ``` | ||
|
|
||
| ### Start external policy engine (Open Policy Agent) | ||
| ```bash | ||
| podman compose up -d | ||
| ``` | ||
|
|
||
| ### Run mcphost | ||
|
|
||
| Where `$MODEL` is the name of the model you prefer (ex: `gpt-oss`) | ||
|
|
||
| ```bash | ||
| OCI_CONFIG_PROFILE=<profile name, ex: DEFAULT> mcphost -m ollama:$MODEL --config ./mcp.json | ||
| ``` | ||
|
|
||
| You can now interact with the servers via the mcphost prompt: | ||
|
|
||
| ``` | ||
| what tools are available to you? | ||
| ``` | ||
| or | ||
| ``` | ||
| list my instances | ||
| ``` | ||
|
|
||
| ## Testing | ||
|
|
||
| ```bash | ||
| make test | ||
| ``` |
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,37 @@ | ||
| version: "2" | ||
| services: | ||
| opa: | ||
| image: openpolicyagent/opa:latest | ||
| ports: | ||
| - 8181:8181 | ||
| # WARNING: OPA is NOT running with an authorization policy configured. This | ||
| # means that clients can read and write policies in OPA. If you are | ||
| # deploying OPA in an insecure environment, be sure to configure | ||
| # authentication and authorization on the daemon. See the Security page for | ||
| # details: https://www.openpolicyagent.org/docs/security.html. | ||
| command: | ||
| - "run" | ||
| - "--server" | ||
| - "--addr=0.0.0.0:8181" | ||
| - "--log-format=json-pretty" | ||
| - "--set=decision_logs.console=true" | ||
| - "--set=services.nginx.url=http://bundle_server" | ||
| - "--set=bundles.nginx.service=nginx" | ||
| - "--set=bundles.nginx.resource=bundles/bundle.tar.gz" | ||
| depends_on: | ||
| - bundle_server | ||
| api_server: | ||
| image: openpolicyagent/demo-restful-api:0.3 | ||
| ports: | ||
| - 5000:5000 | ||
| environment: | ||
| - OPA_ADDR=http://opa:8181 | ||
| - POLICY_PATH=/v1/data/httpapi/authz | ||
| depends_on: | ||
| - opa | ||
| bundle_server: | ||
| image: nginx:1.20.0-alpine | ||
| ports: | ||
| - 8888:80 | ||
| volumes: | ||
| - ./bundles:/usr/share/nginx/html/bundles |
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,22 @@ | ||
| import json | ||
| import sys | ||
|
|
||
| from opa_client.opa import OpaClient | ||
|
|
||
| # Initialize the OPA client | ||
| client = OpaClient(host="localhost", port=8181) | ||
|
|
||
| # Evaluate a policy | ||
| input_data = json.load(sys.stdin) | ||
| result = client.query_rule(input_data=input_data, package_path="mcp", rule_name="allow") | ||
| print(input_data) | ||
|
|
||
| print(result) | ||
|
|
||
| # Print the decision | ||
| if result.get("result"): | ||
| print("Access granted.") | ||
| sys.exit(0) | ||
| else: | ||
| print("Access denied.") | ||
| sys.exit(2) |
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,11 @@ | ||
| { | ||
| "mcpServers": { | ||
| "oracle-oci-api-mcp-server": { | ||
| "command": "uvx", | ||
| "args": [ | ||
| "oracle.oci-api-mcp-server" | ||
| ], | ||
| "transport": "stdio" | ||
| } | ||
| } | ||
| } |
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,11 @@ | ||
| package mcp | ||
|
|
||
| default allow := false | ||
|
|
||
| allow if { | ||
| not contains(input.tool_name, "terminate") | ||
| every arg in input.tool_args { | ||
| not contains(arg, "terminate") | ||
| not contains(arg, "delete") | ||
| } | ||
| } |
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 @@ | ||
| package mcp | ||
|
|
||
| test_tool_allowed if { | ||
| allow with input as {"tool_name": "safe", "tool_args": []} | ||
|
|
||
| allow with input as { | ||
| "tool_name": "oci", | ||
| "tool_args": ["compute", "instance", "create"] | ||
| } | ||
| } | ||
|
|
||
| test_tool_denied if { | ||
| not allow with input as {"tool_name": "terminate", "tool_args": []} | ||
| not allow with input as {"tool_name": "terminate_instance", "tool_args": []} | ||
| not allow with input as {"tool_name": "terminate something", "tool_args": []} | ||
| not allow with input as {"tool_name": "instance_terminate", "tool_args": []} | ||
|
|
||
| not allow with input as {"tool_name": "oci", "tool_args": ["compute", "instance", "terminate"]} | ||
| not allow with input as {"tool_name": "oci", "tool_args": ["compute", "terminate_instance"]} | ||
| not allow with input as {"tool_name": "oci", "tool_args": ["delete"]} | ||
| } |
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,9 @@ | ||
| [project] | ||
| name = "policy" | ||
| version = "0.1.0" | ||
| description = "Demonstration of out-of-band policy enforcement for MCP servers/tools" | ||
| readme = "README.md" | ||
| requires-python = ">=3.13" | ||
| dependencies = [ | ||
| "opa-python-client>=2.0.3", | ||
| ] |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to understand how the
.regofiles in thispoliciesfolder get consumed by the OPA podman instance. Is thispoliciesfolder just a standard naming convention for OPA that it knows to consume all the.regofiles inside of it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, they're: