Skip to content

Commit 765e9a2

Browse files
committed
Rework custom integration
1 parent b081466 commit 765e9a2

File tree

5 files changed

+45
-56
lines changed

5 files changed

+45
-56
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
FROM ghcr.io/developmentseed/stac-auth-proxy:0.1.2
1+
ARG STAC_AUTH_PROXY_VERSION
2+
FROM ghcr.io/developmentseed/stac-auth-proxy:${STAC_AUTH_PROXY_VERSION}
23

3-
ADD . /opa
4+
ADD . /opt/stac-auth-proxy-integration
45

5-
RUN pip install /opa
6+
RUN pip install /opt/stac-auth-proxy-integration
Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
1-
# Open Policy Agent (OPA) Integration
1+
# Custom Integration Example
22

3-
This example demonstrates how to integrate with an Open Policy Agent (OPA) to authorize requests to a STAC API.
3+
This example demonstrates how to integrate with a custom filter generator.
44

55
## Running the Example
66

77
From the root directory, run:
88

99
```sh
10-
docker compose -f docker-compose.yaml -f examples/opa/docker-compose.yaml up
10+
docker compose -f docker-compose.yaml -f examples/custom-integration/docker-compose.yaml up
1111
```
12-
13-
## Testing OPA
14-
15-
```sh
16-
▶ curl -X POST "http://localhost:8181/v1/data/stac/cql2" \
17-
-H "Content-Type: application/json" \
18-
-d '{"input":{"payload": null}}'
19-
{"result":"private = true"}
20-
```
21-
22-
```sh
23-
▶ curl -X POST "http://localhost:8181/v1/data/stac/cql2" \
24-
-H "Content-Type: application/json" \
25-
-d '{"input":{"payload": {"sub": "user1"}}}'
26-
{"result":"1=1"}
27-
```
Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
1+
# This compose file is intended to be run alongside the `docker-compose.yaml` file in the
2+
# root directory.
3+
14
services:
25
proxy:
3-
depends_on:
4-
- stac
5-
- opa
66
build:
7-
context: examples/opa
8-
# environment:
9-
# UPSTREAM_URL: ${UPSTREAM_URL:-http://stac:8001}
10-
# OIDC_DISCOVERY_URL: ${OIDC_DISCOVERY_URL:-http://localhost:8888/.well-known/openid-configuration}
11-
# OIDC_DISCOVERY_INTERNAL_URL: ${OIDC_DISCOVERY_INTERNAL_URL:-http://oidc:8888/.well-known/openid-configuration}
12-
# ITEMS_FILTER_CLS: opa_integration:OpaIntegration
13-
# ITEMS_FILTER_ARGS: '["http://opa:8181", "stac/cql2"]'
14-
env_file:
15-
- path: .env
16-
required: false
17-
ports:
18-
- "8000:8000"
19-
volumes:
20-
- ./src:/app/src
21-
22-
opa:
23-
image: openpolicyagent/opa:latest
24-
command: "run --server --addr=:8181 --watch /policies"
25-
ports:
26-
- "8181:8181"
27-
volumes:
28-
- ./examples/opa/policies:/policies
29-
depends_on:
30-
- stac
7+
context: examples/custom-integration
8+
args:
9+
STAC_AUTH_PROXY_VERSION: 0.1.2
10+
environment:
11+
ITEMS_FILTER_CLS: custom_integration:cql2_builder
12+
ITEMS_FILTER_KWARGS: '{"admin_user": "user123"}'

examples/custom-integration/policies/stac/policy.rego

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
A custom integration example.
3+
4+
In this example, we're intentionally using a functional pattern but you could also use a
5+
class like we do in the integrations found in stac_auth_proxy.filters.
6+
"""
7+
8+
from typing import Any
9+
10+
11+
def cql2_builder(admin_user: str):
12+
"""CQL2 builder integration filter."""
13+
# NOTE: This is where you would set up things like connection pools.
14+
# NOTE: args/kwargs are passed in via environment variables.
15+
16+
def custom_integration_filter(ctx: dict[str, Any]) -> str:
17+
"""
18+
Generate CQL2 expressions based on the request context.
19+
20+
Returns a CQL2 expression, either as a string (cql2-text) or as a dict (cql2-json).
21+
"""
22+
# NOTE: This is where you would perform a lookup from a database, API, etc.
23+
# NOTE: ctx is the request context, which includes the payload, headers, etc.
24+
25+
if ctx["payload"]["sub"] == admin_user:
26+
return "1=1"
27+
return "private = true"
28+
29+
return custom_integration_filter

0 commit comments

Comments
 (0)