Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

feat(cyclops): query tenant Kubernetes state from PostgreSQL (#6641)

r33drichards3a58a8e76cb1
diff --git a/libs/fleet/backend/Dockerfile b/libs/fleet/backend/Dockerfile
index 3325a0f732..9d9e79edaf 100644
--- a/libs/fleet/backend/Dockerfile
+++ b/libs/fleet/backend/Dockerfile
@@ -1,29 +1,27 @@
-# Cyclops-CS backend — Go sidecar exposed inside the cyclops-cs Pod via
-# nginx (cyclops-cs/nginx.conf routes /api/{keys,gateway} to 127.0.0.1:8080).
-#
-# Build context is the repo root so the pkg/featureflags module (referenced
-# by cyclops-cs/backend/go.mod via a local `replace`) is reachable. The
-# build-cyclops-cs.yml workflow sets the context to `.` for this image.
-FROM --platform=linux/amd64 golang:1.24-alpine AS builder
+# Cyclops-CS backend and Kubernetes state projector.
+# Build context is the repository root.
+FROM --platform=linux/amd64 golang:1.24-bookworm AS go-builder
WORKDIR /src
COPY pkg/featureflags ./pkg/featureflags
COPY cyclops-cs/backend ./cyclops-cs/backend
WORKDIR /src/cyclops-cs/backend
RUN go mod download
-# Ensure go.mod/go.sum are tidy (catches manual edits without `go mod tidy`).
RUN go mod tidy
-# Regenerate docs/ in CI so the embedded swagger spec stays in sync with
-# annotated source. swag is installed via `go install` rather than added
-# as a runtime dep — we only need its CLI here.
RUN go install github.com/swaggo/swag/cmd/[email protected] && \
/go/bin/swag init -g main.go -o ./docs --parseDependency --parseInternal
-RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/cyclops-cs-backend .
+RUN CGO_ENABLED=1 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/cyclops-cs-backend .
-# Distroless static — Rego policy is embedded into the binary via
-# //go:embed in auth/middlewares.go, so no extra file is shipped here.
-FROM gcr.io/distroless/static:nonroot AS runtime
+FROM --platform=linux/amd64 rust:1.94-bookworm AS rust-builder
+WORKDIR /src/k8s-state-query
+COPY src/services/k8s-state-query/Cargo.toml src/services/k8s-state-query/Cargo.lock ./
+COPY src/services/k8s-state-query/src ./src
+RUN cargo build --release --locked --bin k8s-state-projector && \
+ cp target/release/k8s-state-projector /out-k8s-state-projector
+
+FROM gcr.io/distroless/cc-debian12:nonroot AS runtime
WORKDIR /app
-COPY --from=builder /out/cyclops-cs-backend /app/cyclops-cs-backend
+COPY --from=go-builder /out/cyclops-cs-backend /app/cyclops-cs-backend
+COPY --from=rust-builder /out-k8s-state-projector /app/k8s-state-projector
USER nonroot:nonroot
EXPOSE 8080
ENTRYPOINT ["/app/cyclops-cs-backend"]
diff --git a/libs/fleet/backend/auth/authz.rego b/libs/fleet/backend/auth/authz.rego
index f6d2410c6c..2b867dec15 100644
--- a/libs/fleet/backend/auth/authz.rego
+++ b/libs/fleet/backend/auth/authz.rego
@@ -385,3 +385,16 @@ valid_dns_label(s) {
count(s) <= 63
regex.match(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`, s)
}
+
+# Read-only state SQL. PostgreSQL RLS remains the row-visibility boundary.
+allow {
+ input.route == "/api/state/query"
+ input.user.sub != ""
+ is_interactive_client
+}
+
+allow {
+ input.route == "/api/state/query"
+ input.user.sub != ""
+ is_user_key_client
+}
diff --git a/libs/fleet/backend/auth/authz_test.rego b/libs/fleet/backend/auth/authz_test.rego
index cd2b13cbca..f1dc977e24 100644
--- a/libs/fleet/backend/auth/authz_test.rego
+++ b/libs/fleet/backend/auth/authz_test.rego
@@ -931,3 +931,43 @@ test_user_api_key_no_sub_denied if {
"user": {"sub": "", "azp": "ukey-test123abc", "namespace": "", "email": ""},
}
}
+
+test_state_query_spa_allowed if {
+ authz.allow with input as {
+ "route": "/api/state/query",
+ "method": "POST",
+ "path": "/api/state/query",
+ "params": {},
+ "user": {"sub": "alice", "azp": "cyclops-cs-spa"},
+ }
+}
+
+test_state_query_user_key_allowed if {
+ authz.allow with input as {
+ "route": "/api/state/query",
+ "method": "POST",
+ "path": "/api/state/query",
+ "params": {},
+ "user": {"sub": "alice", "azp": "ukey-alice"},
+ }
+}
+
+test_state_query_untrusted_client_denied if {
+ not authz.allow with input as {
+ "route": "/api/state/query",
+ "method": "POST",
+ "path": "/api/state/query",
+ "params": {},
+ "user": {"sub": "alice", "azp": "untrusted"},
+ }
+}
+
+test_state_query_empty_subject_denied if {
+ not authz.allow with input as {
+ "route": "/api/state/query",
+ "method": "POST",
+ "path": "/api/state/query",
+ "params": {},
+ "user": {"sub": "", "azp": "cyclops-cs-spa"},
+ }
+}
diff --git a/libs/fleet/backend/config/config.go b/libs/fleet/backend/config/config.go
index 44a26a075a..043a65c114 100644
--- a/libs/fleet/backend/config/config.go
+++ b/libs/fleet/backend/config/config.go
@@ -20,14 +20,15 @@ import (
)
type Configuration struct {
- WebServer WebServerConfiguration
- Auth AuthConfiguration
- Keycloak KeycloakConfiguration
- Gateway GatewayConfiguration
- Database DatabaseConfiguration
- Stripe StripeConfiguration
- Metrics MetricsConfiguration
- Telemetry TelemetryConfiguration
+ WebServer WebServerConfiguration
+ Auth AuthConfiguration
+ Keycloak KeycloakConfiguration
+ Gateway GatewayConfiguration
+ Database DatabaseConfiguration
+ StateQuery StateQueryConfiguration
+ Stripe StripeConfiguration
+ Metrics MetricsConfiguration
+ Telemetry TelemetryConfiguration
}
type WebServerConfiguration struct {
@@ -90,7 +91,17 @@ type StripeConfiguration struct {
// GitHub OIDC trust policies). An empty URL disables those routes (503),
// keeping the backend bootable without a database — see CUA-675.
type DatabaseConfiguration struct {
- URL string // DATABASE_URL — postgres://user:pass@host:5432/dbname?sslmode=require
+ URL string // DATABASE_URL — migration/control and GitHub trust-policy storage
+ StateQueryURL string // STATE_QUERY_DATABASE_URL — restricted query broker
+ StateWriterURL string // STATE_WRITER_DATABASE_URL — restricted projector writer
+ StateExporterURL string // STATE_EXPORTER_DATABASE_URL — restricted outbox exporter
+ StateRoleAdminURL string // STATE_ROLE_ADMIN_DATABASE_URL — tenant-role reconciler
+}
+
+type StateQueryConfiguration struct {
+ MaxRows int
+ TimeoutMS int
+ MaxSQLBytes int
}
type MetricsConfiguration struct {
@@ -134,6 +145,13 @@ var specs = []flagSpec{
{"gateway.port", "orch-port", "ORCH_PORT", "80", "orchestrator port"},
{"gateway.cluster-domain", "cluster-domain", "CLUSTER_DOMAIN", "svc.cluster.local", "in-cluster DNS domain"},
{"database.url", "database-url", "DATABASE_URL", "", "Postgres URL for trust-policy storage (enables /api/github-trust-policies)"},
+ {"database.state-query-url", "state-query-database-url", "STATE_QUERY_DATABASE_URL", "", "Restricted Postgres URL for Kubernetes state queries"},
+ {"database.state-writer-url", "state-writer-database-url", "STATE_WRITER_DATABASE_URL", "", "Restricted Postgres URL for Kubernetes state projection"},
+ {"database.state-exporter-url", "state-exporter-database-url", "STATE_EXPORTER_DATABASE_URL", "", "Restricted Postgres URL for Kubernetes state history export"},
+ {"database.state-role-admin-url", "state-role-admin-database-url", "STATE_ROLE_ADMIN_DATABASE_URL", "", "Restricted Postgres URL for Kubernetes tenant-role reconciliation"},
+ {"state-query.max-rows", "state-query-max-rows", "STATE_QUERY_MAX_ROWS", "1000", "Maximum rows returned by a state query"},
+ {"state-query.timeout-ms", "state-query-timeout-ms", "STATE_QUERY_TIMEOUT_MS", "5000", "State query statement timeout in milliseconds"},
+ {"state-query.max-sql-bytes", "state-query-max-sql-bytes", "STATE_QUERY_MAX_SQL_BYTES", "65536", "Maximum state query SQL size in bytes"},
{"stripe.secret-key", "stripe-secret-key", "STRIPE_SECRET_KEY", "", "Stripe secret key (server-only)"},
{"stripe.webhook-secret", "stripe-webhook-secret", "STRIPE_WEBHOOK_SECRET", "", "Stripe webhook signing secret"},
{"stripe.checkout-success-url", "stripe-checkout-success-url", "STRIPE_CHECKOUT_SUCCESS_URL", "", "Stripe Checkout success redirect URL"},
@@ -204,7 +222,18 @@ func LoadConfig() (*Configuration, error) {
Port: viper.GetString("gateway.port"),
ClusterDomain: viper.GetString("gateway.cluster-domain"),
},
- Database: DatabaseConfiguration{URL: viper.GetString("database.url")},
+ Database: DatabaseConfiguration{
+ URL: viper.GetString("database.url"),
+ StateQueryURL: viper.GetString("database.state-query-url"),
+ StateWriterURL: viper.GetString("database.state-writer-url"),
+ StateExporterURL: viper.GetString("database.state-exporter-url"),
+ StateRoleAdminURL: viper.GetString("database.state-role-admin-url"),
+ },
+ StateQuery: StateQueryConfiguration{
+ MaxRows: viper.GetInt("state-query.max-rows"),
+ TimeoutMS: viper.GetInt("state-query.timeout-ms"),
+ MaxSQLBytes: viper.GetInt("state-query.max-sql-bytes"),
+ },
Stripe: StripeConfiguration{
SecretKey: viper.GetString("stripe.secret-key"),
WebhookSecret: viper.GetString("stripe.webhook-secret"),
diff --git a/libs/fleet/backend/config/config_test.go b/libs/fleet/backend/config/config_test.go
index 79b22b6954..1b9e6572ce 100644
--- a/libs/fleet/backend/config/config_test.go
+++ b/libs/fleet/backend/config/config_test.go
@@ -105,3 +105,50 @@ func TestLoadConfig_StripeBillingDefaultsAbsent(t *testing.T) {
t.Fatalf("Stripe = %#v, want zero configuration", cfg.Stripe)
}
}
+
+func TestLoadConfig_StateDatabaseURLs(t *testing.T) {
+ viper.Reset()
+ t.Cleanup(viper.Reset)
+ t.Setenv("KC_ADMIN_CLIENT_SECRET", "secret")
+ t.Setenv("DATABASE_URL", "postgres://admin@db/cyclops")
+ t.Setenv("STATE_QUERY_DATABASE_URL", "postgres://k8s_query_broker@db/cyclops")
+ t.Setenv("STATE_WRITER_DATABASE_URL", "postgres://k8s_state_writer@db/cyclops")
+ t.Setenv("STATE_EXPORTER_DATABASE_URL", "postgres://k8s_state_exporter@db/cyclops")
+ t.Setenv("STATE_ROLE_ADMIN_DATABASE_URL", "postgres://k8s_role_admin@db/cyclops")
+ RegisterFlags(pflag.NewFlagSet("state-database-test", pflag.ContinueOnError))
+
+ cfg, err := LoadConfig()
+ if err != nil {
+ t.Fatalf("LoadConfig() error = %v", err)
+ }
+ if got, want := cfg.Database.StateQueryURL, "postgres://k8s_query_broker@db/cyclops"; got != want {
+ t.Fatalf("Database.StateQueryURL = %q, want %q", got, want)
+ }
+ if got, want := cfg.Database.StateWriterURL, "postgres://k8s_state_writer@db/cyclops"; got != want {
+ t.Fatalf("Database.StateWriterURL = %q, want %q", got, want)
+ }
+ if got, want := cfg.Database.StateExporterURL, "postgres://k8s_state_exporter@db/cyclops"; got != want {
+ t.Fatalf("Database.StateExporterURL = %q, want %q", got, want)
+ }
+ if got, want := cfg.Database.StateRoleAdminURL, "postgres://k8s_role_admin@db/cyclops"; got != want {
+ t.Fatalf("Database.StateRoleAdminURL = %q, want %q", got, want)
+ }
+}
+
+func TestLoadConfig_StateQueryLimits(t *testing.T) {
+ viper.Reset()
+ t.Cleanup(viper.Reset)
+ t.Setenv("KC_ADMIN_CLIENT_SECRET", "secret")
+ t.Setenv("STATE_QUERY_MAX_ROWS", "250")
+ t.Setenv("STATE_QUERY_TIMEOUT_MS", "3000")
+ t.Setenv("STATE_QUERY_MAX_SQL_BYTES", "8192")
+ RegisterFlags(pflag.NewFlagSet("state-query-limits-test", pflag.ContinueOnError))
+
+ cfg, err := LoadConfig()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if cfg.StateQuery.MaxRows != 250 || cfg.StateQuery.TimeoutMS != 3000 || cfg.StateQuery.MaxSQLBytes != 8192 {
+ t.Fatalf("StateQuery = %#v", cfg.StateQuery)
+ }
+}
diff --git a/libs/fleet/backend/go.mod b/libs/fleet/backend/go.mod
index 22716eb8c4..d3c3996ee5 100644
--- a/libs/fleet/backend/go.mod
+++ b/libs/fleet/backend/go.mod
@@ -12,8 +12,8 @@ require (
github.com/jackc/pgx/v5 v5.5.5
github.com/open-feature/go-sdk v1.17.0
github.com/open-policy-agent/opa v0.69.0
+ github.com/pganalyze/pg_query_go/v6 v6.2.2
github.com/prometheus/client_golang v1.20.4
- github.com/redis/go-redis/v9 v9.19.0
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
github.com/spf13/viper v1.21.0
@@ -36,11 +36,11 @@ require (
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
- github.com/jinzhu/inflection v1.0.0 // indirect
- github.com/jinzhu/now v1.1.5 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
+ github.com/jinzhu/inflection v1.0.0 // indirect
+ github.com/jinzhu/now v1.1.5 // indirect
github.com/open-feature/go-sdk-contrib/providers/aws-ssm v1.0.0 // indirect
github.com/open-feature/go-sdk-contrib/providers/from-env v0.1.6 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
@@ -111,12 +111,11 @@ require (
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/sdk v1.28.0
go.opentelemetry.io/otel/trace v1.28.0
- go.uber.org/atomic v1.11.0 // indirect
golang.org/x/mod v0.28.0 // indirect
golang.org/x/net v0.44.0 // indirect
golang.org/x/sys v0.36.0 // indirect
golang.org/x/tools v0.37.0 // indirect
- google.golang.org/protobuf v1.34.2 // indirect
+ google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v2 v2.4.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
diff --git a/libs/fleet/backend/go.sum b/libs/fleet/backend/go.sum
index 2b6322526b..54c035030e 100644
--- a/libs/fleet/backend/go.sum
+++ b/libs/fleet/backend/go.sum
@@ -42,10 +42,6 @@ github.com/aws/smithy-go v1.25.1 h1:J8ERsGSU7d+aCmdQur5Txg6bVoYelvQJgtZehD12GkI=
github.com/aws/smithy-go v1.25.1/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
-github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
-github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
-github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
-github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/bytecodealliance/wasmtime-go/v3 v3.0.2 h1:3uZCA/BLTIu+DqCfguByNMJa2HVHpXvjfy0Dy7g6fuA=
github.com/bytecodealliance/wasmtime-go/v3 v3.0.2/go.mod h1:RnUjnIXxEJcL6BgCvNyzCCRzZcxCgsZCi+RNlvYor5Q=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
@@ -133,12 +129,14 @@ github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
+github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
+github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
+github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
+github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
-github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
-github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
@@ -169,6 +167,8 @@ github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
+github.com/pganalyze/pg_query_go/v6 v6.2.2 h1:O0L6zMC226R82RF3X5n0Ki6HjytDsoAzuzp4ATVAHNo=
+github.com/pganalyze/pg_query_go/v6 v6.2.2/go.mod h1:Cn6+j4870kJz3iYNsb0VsNG04vpSWgEvBwc590J4qD0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -183,8 +183,6 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargg
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 h1:MkV+77GLUNo5oJ0jf870itWm3D0Sjh7+Za9gazKc5LQ=
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
-github.com/redis/go-redis/v9 v9.19.0 h1:XPVaaPSnG6RhYf7p+rmSa9zZfeVAnWsH5h3lxthOm/k=
-github.com/redis/go-redis/v9 v9.19.0/go.mod h1:v/M13XI1PVCDcm01VtPFOADfZtHf8YW3baQf57KlIkA=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -211,7 +209,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
-github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/stripe/stripe-go/v85 v85.2.0 h1:LomL8ulv13+y+nIKtFP48Cn/pCIZmi4IBz5qjdDn+FY=
@@ -232,8 +229,6 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/yashtewari/glob-intersection v0.2.0 h1:8iuHdN88yYuCzCdjt0gDe+6bAhUwBeEWqThExu54RFg=
github.com/yashtewari/glob-intersection v0.2.0/go.mod h1:LK7pIC3piUjovexikBbJ26Yml7g8xa5bsjfx2v1fwok=
-github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
-github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 h1:4K4tsIXefpVJtvA/8srF4V4y0akAoPHkIslgAkjixJA=
@@ -254,13 +249,10 @@ go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+
go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI=
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
-go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
-go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
-golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI=
golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8=
golang.org/x/mod v0.28.0 h1:gQBtGhjxykdjY9YhZpSlZIsbnaE2+PgjfLWUQTnoZ1U=
@@ -268,7 +260,6 @@ golang.org/x/mod v0.28.0/go.mod h1:yfB/L0NOf/kmEbXjzCPOx1iK1fRutOydrCMsqRhEBxI=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I=
golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
-golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -278,7 +269,6 @@ golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
-golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
@@ -304,13 +294,9 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
-sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
-sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
-github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
-github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
-github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
-github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314=
gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
+sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
+sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
diff --git a/libs/fleet/backend/handlers/handlers.go b/libs/fleet/backend/handlers/handlers.go
index 7087b3616d..00b3a8a037 100644
--- a/libs/fleet/backend/handlers/handlers.go
+++ b/libs/fleet/backend/handlers/handlers.go
@@ -4,6 +4,7 @@ package handlers
import (
"bufio"
+ "context"
"encoding/json"
"fmt"
"net"
@@ -24,11 +25,15 @@ type Handlers struct {
AuthCfg config.AuthConfiguration
KC config.KeycloakConfiguration
Stripe config.StripeConfiguration
+ StateQuery config.StateQueryConfiguration
Billing BillingService
WebhookVerifier WebhookVerifier
GitHubTrustPolicies githubtrust.Store
+ StateQueryExecutor StateQueryExecutor
+ StateQueryAuthorize func(context.Context, *auth.User) (bool, error)
+
// WorkloadAdmin manages per-tenant clients in the workloads realm so
// OSGym pool VMs can obtain a tenant-scoped OIDC token. nil disables
// the feature (CreateNamespace then skips OIDC credential provisioning).
@@ -46,6 +51,7 @@ func New(admin *keycloak.Admin, cfg *config.Configuration) Handlers {
AuthCfg: cfg.Auth,
KC: cfg.Keycloak,
Stripe: cfg.Stripe,
+ StateQuery: cfg.StateQuery,
}
}
diff --git a/libs/fleet/backend/handlers/state_query.go b/libs/fleet/backend/handlers/state_query.go
new file mode 100644
index 0000000000..3e6aa344c8
--- /dev/null
+++ b/libs/fleet/backend/handlers/state_query.go
@@ -0,0 +1,77 @@
+package handlers
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "net/http"
+
+ "cyclops-cs-backend/auth"
+ "cyclops-cs-backend/identity"
+ "cyclops-cs-backend/statequery"
+)
+
+type StateQueryExecutor interface {
+ Execute(context.Context, string, bool, statequery.ValidatedQuery) (statequery.ResultSet, error)
+}
+
+type stateQueryRequest struct {
+ SQL string `json:"sql"`
+}
+
+func (h Handlers) QueryState(w http.ResponseWriter, r *http.Request) {
+ if h.StateQueryExecutor == nil {
+ writeErr(w, http.StatusServiceUnavailable, "state query unavailable")
+ return
+ }
+ user := currentUser(r)
+ if user == nil || user.ID == "" {
+ writeErr(w, http.StatusUnauthorized, "missing user")
+ return
+ }
+
+ limits := statequery.DefaultLimits()
+ if h.StateQuery.MaxRows > 0 {
+ limits.MaxRows = h.StateQuery.MaxRows
+ }
+ if h.StateQuery.TimeoutMS > 0 {
+ limits.TimeoutMS = h.StateQuery.TimeoutMS
+ }
+ if h.StateQuery.MaxSQLBytes > 0 {
+ limits.MaxSQLBytes = h.StateQuery.MaxSQLBytes
+ }
+ decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, int64(limits.MaxSQLBytes+1024)))
+ decoder.DisallowUnknownFields()
+ var request stateQueryRequest
+ if err := decoder.Decode(&request); err != nil {
+ writeErr(w, http.StatusBadRequest, "invalid request")
+ return
+ }
+ validated, err := statequery.Validate(request.SQL, limits)
+ if err != nil {
+ writeErr(w, http.StatusBadRequest, "invalid state query")
+ return
+ }
+
+ authorize := h.StateQueryAuthorize
+ if authorize == nil {
+ authorize = auth.EvalIsAdmin
+ }
+ admin, err := authorize(r.Context(), user)
+ if err != nil {
+ writeErr(w, http.StatusForbidden, "state query denied")
+ return
+ }
+ result, err := h.StateQueryExecutor.Execute(
+ r.Context(), identity.PersonalGroup(r.Context(), user.ID), admin, validated,
+ )
+ if err != nil {
+ if errors.Is(r.Context().Err(), context.DeadlineExceeded) {
+ writeErr(w, http.StatusGatewayTimeout, "state query timed out")
+ return
+ }
+ writeErr(w, http.StatusServiceUnavailable, "state query failed")
+ return
+ }
+ writeJSON(w, http.StatusOK, result)
+}
diff --git a/libs/fleet/backend/handlers/state_query_test.go b/libs/fleet/backend/handlers/state_query_test.go
new file mode 100644
index 0000000000..70fb57ddc5
--- /dev/null
+++ b/libs/fleet/backend/handlers/state_query_test.go
@@ -0,0 +1,78 @@
+package handlers
+
+import (
+ "context"
+ "errors"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "cyclops-cs-backend/auth"
+ "cyclops-cs-backend/statequery"
+)
+
+type fakeStateQueryExecutor struct {
+ tenant string
+ admin bool
+ query statequery.ValidatedQuery
+ result statequery.ResultSet
+ err error
+}
+
+func (fake *fakeStateQueryExecutor) Execute(_ context.Context, tenant string, admin bool, query statequery.ValidatedQuery) (statequery.ResultSet, error) {
+ fake.tenant, fake.admin, fake.query = tenant, admin, query
+ return fake.result, fake.err
+}
+
+func TestQueryStateReturnsUnavailableWithoutExecutor(t *testing.T) {
+ h := Handlers{}
+ request := withUser(httptest.NewRequest(http.MethodPost, "/api/state/query", strings.NewReader(`{"sql":"select 1"}`)), &auth.User{ID: "alice"})
+ response := httptest.NewRecorder()
+ h.QueryState(response, request)
+ if response.Code != http.StatusServiceUnavailable {
+ t.Fatalf("status = %d, body = %s", response.Code, response.Body.String())
+ }
+}
+
+func TestQueryStateRejectsUnsafeSQL(t *testing.T) {
+ executor := &fakeStateQueryExecutor{}
+ h := Handlers{StateQueryExecutor: executor}
+ request := withUser(httptest.NewRequest(http.MethodPost, "/api/state/query", strings.NewReader(`{"sql":"select pg_sleep(60)"}`)), &auth.User{ID: "alice"})
+ response := httptest.NewRecorder()
+ h.QueryState(response, request)
+ if response.Code != http.StatusBadRequest {
+ t.Fatalf("status = %d, body = %s", response.Code, response.Body.String())
+ }
+}
+
+func TestQueryStateUsesAuthenticatedCapsuleTenant(t *testing.T) {
+ executor := &fakeStateQueryExecutor{result: statequery.ResultSet{Rows: [][]any{{"alice-ns"}}}}
+ h := Handlers{
+ StateQueryExecutor: executor,
+ StateQueryAuthorize: func(context.Context, *auth.User) (bool, error) { return false, nil },
+ }
+ request := withUser(httptest.NewRequest(http.MethodPost, "/api/state/query", strings.NewReader(`{"sql":"select name from k8s_api.current_resources"}`)), &auth.User{ID: "alice"})
+ response := httptest.NewRecorder()
+ h.QueryState(response, request)
+ if response.Code != http.StatusOK {
+ t.Fatalf("status = %d, body = %s", response.Code, response.Body.String())
+ }
+ if executor.tenant != "user-alice" || executor.admin {
+ t.Fatalf("tenant/admin = %q/%t", executor.tenant, executor.admin)
+ }
+}
+
+func TestQueryStateFailsClosedOnAuthorizationError(t *testing.T) {
+ executor := &fakeStateQueryExecutor{}
+ h := Handlers{
+ StateQueryExecutor: executor,
+ StateQueryAuthorize: func(context.Context, *auth.User) (bool, error) { return false, errors.New("opa unavailable") },
+ }
+ request := withUser(httptest.NewRequest(http.MethodPost, "/api/state/query", strings.NewReader(`{"sql":"select name from k8s_api.current_resources"}`)), &auth.User{ID: "alice"})
+ response := httptest.NewRecorder()
+ h.QueryState(response, request)
+ if response.Code != http.StatusForbidden {
+ t.Fatalf("status = %d, body = %s", response.Code, response.Body.String())
+ }
+}
diff --git a/libs/fleet/backend/main.go b/libs/fleet/backend/main.go
index 1b06d52053..ba52fb393f 100644
--- a/libs/fleet/backend/main.go
+++ b/libs/fleet/backend/main.go
@@ -51,6 +51,7 @@ import (
"cyclops-cs-backend/keycloak"
"cyclops-cs-backend/metrics"
"cyclops-cs-backend/middlewares"
+ "cyclops-cs-backend/statequery"
"cyclops-cs-backend/telemetry"
httpSwagger "github.com/swaggo/http-swagger/v2"
@@ -99,6 +100,9 @@ func setupRouter(c handlers.Handlers) http.Handler {
r.Handle("GET /api/config",
withMiddlewares("/api/config", nil, c.GetConfig))
+ r.Handle("POST /api/state/query",
+ withMiddlewares("/api/state/query", nil, c.QueryState))
+
// Stripe-hosted billing. Browser routes require the normal SPA JWT; the
// webhook uses Stripe signature verification as its authentication boundary.
r.Handle("GET /api/billing/summary",
@@ -316,6 +320,35 @@ func run() error {
h.WorkloadAdmin = workloadAdmin
h.WorkloadAudience = cfg.Keycloak.WorkloadAudience
h.WorkloadTokenURL = cfg.Keycloak.WorkloadTokenURL
+ stateSchemaReady := false
+ stateRoleURLs := statequery.FixedRoleURLs{
+ Writer: cfg.Database.StateWriterURL,
+ Exporter: cfg.Database.StateExporterURL,
+ Query: cfg.Database.StateQueryURL,
+ RoleAdmin: cfg.Database.StateRoleAdminURL,
+ }
+ if stateRoleURLs.Writer != "" || stateRoleURLs.Exporter != "" || stateRoleURLs.Query != "" || stateRoleURLs.RoleAdmin != "" {
+ if err := statequery.ReconcileFixedRoles(ctx, cfg.Database.URL, stateRoleURLs); err != nil {
+ slog.Error("kubernetes state query: fixed-role reconciliation failed", "err", err)
+ } else if err := statequery.Migrate(ctx, cfg.Database.URL); err != nil {
+ slog.Error("kubernetes state query: schema migration failed", "err", err)
+ } else {
+ slog.Info("kubernetes state query: schema ready")
+ stateSchemaReady = true
+ }
+ } else {
+ slog.Info("kubernetes state query: disabled (restricted database URLs unset)")
+ }
+ if stateSchemaReady {
+ executor, err := statequery.NewExecutor(ctx, cfg.Database.StateQueryURL, cfg.Database.StateRoleAdminURL)
+ if err != nil {
+ slog.Error("kubernetes state query: executor init failed; /api/state/query will return 503", "err", err)
+ } else {
+ defer executor.Close()
+ h.StateQueryExecutor = executor
+ slog.Info("kubernetes state query: query endpoint enabled")
+ }
+ }
// Postgres-backed GitHub OIDC trust policies (CUA-675). A connection
// failure is non-fatal — the routes stay registered and reply 503 until
// the database is reachable, mirroring how the other optional surfaces
diff --git a/libs/fleet/backend/statequery/e2e_pg_test.go b/libs/fleet/backend/statequery/e2e_pg_test.go
new file mode 100644
index 0000000000..8e7bbfbb5a
--- /dev/null
+++ b/libs/fleet/backend/statequery/e2e_pg_test.go
@@ -0,0 +1,47 @@
+package statequery
+
+import (
+ "context"
+ "os"
+ "testing"
+)
+
+func TestE2EListsProjectedNamespacesForTenant(t *testing.T) {
+ queryURL := os.Getenv("CYCLOPS_E2E_STATE_QUERY_URL")
+ roleAdminURL := os.Getenv("CYCLOPS_E2E_STATE_ROLE_ADMIN_URL")
+ tenant := os.Getenv("CYCLOPS_E2E_CAPSULE_TENANT")
+ clusterID := os.Getenv("CYCLOPS_E2E_CLUSTER_ID")
+ if queryURL == "" || roleAdminURL == "" || tenant == "" || clusterID == "" {
+ t.Skip("set CYCLOPS_E2E_STATE_QUERY_URL, CYCLOPS_E2E_STATE_ROLE_ADMIN_URL, CYCLOPS_E2E_CAPSULE_TENANT, and CYCLOPS_E2E_CLUSTER_ID")
+ }
+
+ ctx := context.Background()
+ executor, err := NewExecutor(ctx, queryURL, roleAdminURL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer executor.Close()
+ query, err := Validate(`
+ select name, object->'metadata'->'labels'->>'capsule.clastix.io/tenant' as owner
+ from k8s_api.current_resources
+ where cluster_id = '`+clusterID+`'
+ and api_group = ''
+ and resource = 'namespaces'
+ order by name`, DefaultLimits())
+ if err != nil {
+ t.Fatal(err)
+ }
+ result, err := executor.Execute(ctx, tenant, false, query)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(result.Rows) == 0 {
+ t.Fatal("tenant query returned no projected namespaces")
+ }
+ for _, row := range result.Rows {
+ if len(row) != 2 || row[1] != tenant {
+ t.Fatalf("row escaped tenant RLS: %#v, want tenant %q", row, tenant)
+ }
+ t.Logf("projected namespace: %s", row[0])
+ }
+}
diff --git a/libs/fleet/backend/statequery/executor.go b/libs/fleet/backend/statequery/executor.go
new file mode 100644
index 0000000000..a0c349926c
--- /dev/null
+++ b/libs/fleet/backend/statequery/executor.go
@@ -0,0 +1,185 @@
+package statequery
+
+import (
+ "context"
+ "crypto/sha256"
+ "encoding/hex"
+ "errors"
+ "fmt"
+ "time"
+
+ "github.com/jackc/pgx/v5"
+ "github.com/jackc/pgx/v5/pgxpool"
+)
+
+var ErrExecution = errors.New("state query execution failed")
+
+type Column struct {
+ Name string `json:"name"`
+ Type string `json:"type"`
+}
+
+type ResultSet struct {
+ Columns []Column `json:"columns"`
+ Rows [][]any `json:"rows"`
+ Truncated bool `json:"truncated"`
+ DurationMS int64 `json:"duration_ms"`
+ Checkpoints []any `json:"checkpoints"`
+}
+
+type Executor struct {
+ queryPool *pgxpool.Pool
+ roleAdminPool *pgxpool.Pool
+}
+
+func TenantRoleName(tenant string) string {
+ digest := sha256.Sum256([]byte(tenant))
+ return "k8s_tenant_" + hex.EncodeToString(digest[:16])
+}
+
+func NewExecutor(ctx context.Context, queryURL, roleAdminURL string) (*Executor, error) {
+ if queryURL == "" || roleAdminURL == "" {
+ return nil, fmt.Errorf("state query database URLs are required")
+ }
+ queryPool, err := pgxpool.New(ctx, queryURL)
+ if err != nil {
+ return nil, fmt.Errorf("open state query pool: %w", err)
+ }
+ roleAdminPool, err := pgxpool.New(ctx, roleAdminURL)
+ if err != nil {
+ queryPool.Close()
+ return nil, fmt.Errorf("open state role-admin pool: %w", err)
+ }
+ if err := queryPool.Ping(ctx); err != nil {
+ queryPool.Close()
+ roleAdminPool.Close()
+ return nil, fmt.Errorf("ping state query pool: %w", err)
+ }
+ if err := roleAdminPool.Ping(ctx); err != nil {
+ queryPool.Close()
+ roleAdminPool.Close()
+ return nil, fmt.Errorf("ping state role-admin pool: %w", err)
+ }
+ return &Executor{queryPool: queryPool, roleAdminPool: roleAdminPool}, nil
+}
+
+func (e *Executor) Close() {
+ e.queryPool.Close()
+ e.roleAdminPool.Close()
+}
+
+func (e *Executor) Execute(ctx context.Context, tenant string, admin bool, query ValidatedQuery) (ResultSet, error) {
+ role := "k8s_query_admin"
+ if !admin {
+ if tenant == "" {
+ return ResultSet{}, fmt.Errorf("%w: provision tenant role", ErrExecution)
+ }
+ role = TenantRoleName(tenant)
+ if err := e.ensureTenantRole(ctx, role, tenant); err != nil {
+ return ResultSet{}, fmt.Errorf("%w: provision tenant role", ErrExecution)
+ }
+ }
+
+ started := time.Now()
+ tx, err := e.queryPool.BeginTx(ctx, pgx.TxOptions{AccessMode: pgx.ReadOnly})
+ if err != nil {
+ return ResultSet{}, fmt.Errorf("%w: begin transaction", ErrExecution)
+ }
+ defer tx.Rollback(ctx)
+ if _, err := tx.Exec(ctx, "set local role "+pgx.Identifier{role}.Sanitize()); err != nil {
+ return ResultSet{}, fmt.Errorf("%w: assume query role", ErrExecution)
+ }
+ for setting, value := range map[string]string{
+ "statement_timeout": fmt.Sprintf("%dms", query.TimeoutMS),
+ "lock_timeout": "500ms",
+ "idle_in_transaction_session_timeout": "10s",
+ "work_mem": "16MB",
+ } {
+ if _, err := tx.Exec(ctx, `select set_config($1, $2, true)`, setting, value); err != nil {
+ return ResultSet{}, fmt.Errorf("%w: apply query limits", ErrExecution)
+ }
+ }
+
+ rows, err := tx.Query(ctx, query.SQL)
+ if err != nil {
+ return ResultSet{}, fmt.Errorf("%w: execute select", ErrExecution)
+ }
+ defer rows.Close()
+ fields := rows.FieldDescriptions()
+ result := ResultSet{
+ Columns: make([]Column, len(fields)),
+ Rows: make([][]any, 0, query.MaxRows),
+ Checkpoints: []any{},
+ }
+ for index, field := range fields {
+ result.Columns[index] = Column{Name: field.Name, Type: postgresTypeName(field.DataTypeOID)}
+ }
+ for rows.Next() {
+ values, err := rows.Values()
+ if err != nil {
+ return ResultSet{}, fmt.Errorf("%w: decode result row", ErrExecution)
+ }
+ if len(result.Rows) == query.MaxRows {
+ result.Truncated = true
+ break
+ }
+ result.Rows = append(result.Rows, values)
+ }
+ if rows.Err() != nil {
+ return ResultSet{}, fmt.Errorf("%w: stream result rows", ErrExecution)
+ }
+ result.DurationMS = time.Since(started).Milliseconds()
+ return result, nil
+}
+
+func (e *Executor) ensureTenantRole(ctx context.Context, role, tenant string) error {
+ tx, err := e.roleAdminPool.Begin(ctx)
+ if err != nil {
+ return err
+ }
+ defer tx.Rollback(ctx)
+ if _, err := tx.Exec(ctx, `select pg_advisory_xact_lock(hashtext($1))`, "cyclops-state-role:"+role); err != nil {
+ return err
+ }
+ var exists bool
+ if err := tx.QueryRow(ctx, `select exists(select 1 from pg_roles where rolname = $1)`, role).Scan(&exists); err != nil {
+ return err
+ }
+ identifier := pgx.Identifier{role}.Sanitize()
+ if !exists {
+ if _, err := tx.Exec(ctx, "create role "+identifier+" nologin inherit nocreaterole"); err != nil {
+ return err
+ }
+ }
+ if _, err := tx.Exec(ctx, "grant k8s_query_tenant to "+identifier); err != nil {
+ return err
+ }
+ if _, err := tx.Exec(ctx, "grant "+identifier+" to k8s_query_broker"); err != nil {
+ return err
+ }
+ if _, err := tx.Exec(ctx, `select k8s_state.register_tenant_role($1, $2)`, role, tenant); err != nil {
+ return err
+ }
+ return tx.Commit(ctx)
+}
+
+func postgresTypeName(oid uint32) string {
+ switch oid {
+ case 16:
+ return "bool"
+ case 20:
+ return "int8"
+ case 23:
+ return "int4"
+ case 25:
+ return "text"
+ case 114:
+ return "json"
+ case 1184:
+ return "timestamptz"
+ case 3802:
+ return "jsonb"
+ default:
+ return fmt.Sprintf("oid:%d", oid)
+ }
+}
diff --git a/libs/fleet/backend/statequery/executor_test.go b/libs/fleet/backend/statequery/executor_test.go
new file mode 100644
index 0000000000..e7487980be
--- /dev/null
+++ b/libs/fleet/backend/statequery/executor_test.go
@@ -0,0 +1,72 @@
+package statequery
+
+import (
+ "context"
+ "os"
+ "testing"
+
+ "github.com/jackc/pgx/v5/pgxpool"
+)
+
+func TestTenantRoleNameIsStableAndOpaque(t *testing.T) {
+ got := TenantRoleName("user-alice")
+ if got != "k8s_tenant_0e7b8c3e3b7f94ed81538a568a6408c6" {
+ t.Fatalf("TenantRoleName = %q", got)
+ }
+ if got == TenantRoleName("user-bob") {
+ t.Fatal("different tenants received the same role")
+ }
+}
+
+func TestExecutorListsOnlyTenantOwnedNamespaces(t *testing.T) {
+ adminURL := os.Getenv("CYCLOPS_TEST_DATABASE_URL")
+ if adminURL == "" {
+ t.Skip("set CYCLOPS_TEST_DATABASE_URL to run the PostgreSQL RLS test")
+ }
+ ctx := context.Background()
+ urls := fixedTestRoleURLs(t, adminURL)
+ if err := ReconcileFixedRoles(ctx, adminURL, urls); err != nil {
+ t.Fatal(err)
+ }
+ if err := Migrate(ctx, adminURL); err != nil {
+ t.Fatal(err)
+ }
+ admin, err := pgxpool.New(ctx, adminURL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer admin.Close()
+ _, err = admin.Exec(ctx, `
+ delete from k8s_state.resource_state where cluster_id = 'rls-test';
+ insert into k8s_state.resource_state
+ (cluster_id, api_group, resource, namespace, name, capsule_tenant, schema_hash, watch_epoch, observed_sequence, labels, object)
+ values
+ ('rls-test', '', 'namespaces', '', 'alice-ns', 'user-alice', 's', 1, 1, '{}', '{"kind":"Namespace"}'),
+ ('rls-test', '', 'namespaces', '', 'bob-ns', 'user-bob', 's', 1, 2, '{}', '{"kind":"Namespace"}'),
+ ('rls-test', '', 'nodes', '', 'node-a', 'user-alice', 's', 1, 3, '{}', '{"kind":"Node"}'),
+ ('rls-test', '', 'pods', 'alice-ns', 'pod-a', 'user-alice', 's', 1, 4, '{}', '{"kind":"Pod"}')
+ `)
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() {
+ _, _ = admin.Exec(context.Background(), `delete from k8s_state.resource_state where cluster_id = 'rls-test'`)
+ })
+
+ executor, err := NewExecutor(ctx, urls.Query, urls.RoleAdmin)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer executor.Close()
+ validated, err := Validate(`select name from k8s_api.current_resources where cluster_id = 'rls-test' and resource = 'namespaces' order by name`, DefaultLimits())
+ if err != nil {
+ t.Fatal(err)
+ }
+ result, err := executor.Execute(ctx, "user-alice", false, validated)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(result.Rows) != 1 || result.Rows[0][0] != "alice-ns" {
+ t.Fatalf("rows = %#v, want only alice-ns", result.Rows)
+ }
+}
diff --git a/libs/fleet/backend/statequery/migrations/0001_postgres_state.sql b/libs/fleet/backend/statequery/migrations/0001_postgres_state.sql
new file mode 100644
index 0000000000..e6383e39dd
--- /dev/null
+++ b/libs/fleet/backend/statequery/migrations/0001_postgres_state.sql
@@ -0,0 +1,166 @@
+CREATE TABLE k8s_state.resource_state (
+ cluster_id text NOT NULL,
+ api_group text NOT NULL,
+ resource text NOT NULL,
+ namespace text NOT NULL,
+ name text NOT NULL,
+ capsule_tenant text,
+ uid text,
+ resource_version text,
+ schema_hash text NOT NULL,
+ watch_epoch bigint NOT NULL,
+ observed_sequence bigint NOT NULL,
+ labels jsonb NOT NULL DEFAULT '{}'::jsonb,
+ object jsonb NOT NULL,
+ source_time timestamptz,
+ ingested_at timestamptz NOT NULL DEFAULT clock_timestamp(),
+ PRIMARY KEY (cluster_id, api_group, resource, namespace, name),
+ CHECK (jsonb_typeof(labels) = 'object'),
+ CHECK (jsonb_typeof(object) = 'object')
+);
+
+CREATE INDEX resource_state_tenant_gvr_namespace_idx
+ ON k8s_state.resource_state
+ (capsule_tenant, cluster_id, api_group, resource, namespace, name);
+
+CREATE INDEX resource_state_labels_gin_idx
+ ON k8s_state.resource_state USING gin (labels jsonb_path_ops);
+
+CREATE TABLE k8s_state.watch_checkpoint (
+ cluster_id text NOT NULL,
+ api_group text NOT NULL,
+ resource text NOT NULL,
+ watch_epoch bigint NOT NULL,
+ last_resource_version text NOT NULL,
+ last_observed_sequence bigint NOT NULL,
+ initial_list_complete boolean NOT NULL,
+ updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
+ PRIMARY KEY (cluster_id, api_group, resource)
+);
+
+CREATE TABLE k8s_state.resource_event_outbox (
+ event_id uuid PRIMARY KEY,
+ cluster_id text NOT NULL,
+ api_group text NOT NULL,
+ resource text NOT NULL,
+ namespace text NOT NULL,
+ name text NOT NULL,
+ capsule_tenant text,
+ uid text,
+ resource_version text,
+ schema_hash text NOT NULL,
+ event_type text NOT NULL CHECK (event_type IN ('ADDED', 'MODIFIED', 'DELETED')),
+ watch_epoch bigint NOT NULL,
+ observed_sequence bigint NOT NULL,
+ object jsonb NOT NULL,
+ observed_at timestamptz NOT NULL,
+ claimed_at timestamptz,
+ claim_id uuid,
+ exported_at timestamptz,
+ UNIQUE (cluster_id, api_group, resource, watch_epoch, observed_sequence),
+ CHECK (jsonb_typeof(object) = 'object')
+);
+
+CREATE INDEX resource_event_outbox_pending_idx
+ ON k8s_state.resource_event_outbox (observed_at, event_id)
+ WHERE exported_at IS NULL;
+
+CREATE TABLE k8s_state.resource_schema (
+ schema_hash text PRIMARY KEY,
+ api_group text NOT NULL,
+ api_version text NOT NULL,
+ kind text NOT NULL,
+ resource text NOT NULL,
+ structural_schema jsonb NOT NULL,
+ first_seen_at timestamptz NOT NULL DEFAULT clock_timestamp(),
+ CHECK (jsonb_typeof(structural_schema) = 'object')
+);
+
+CREATE TABLE k8s_state.query_tenant_role (
+ role_name name PRIMARY KEY,
+ capsule_tenant text NOT NULL UNIQUE,
+ created_at timestamptz NOT NULL DEFAULT clock_timestamp()
+);
+
+CREATE FUNCTION k8s_state.tenant_for_role(role_name name)
+RETURNS text
+LANGUAGE sql
+STABLE
+SECURITY DEFINER
+SET search_path = k8s_state, pg_catalog
+AS $$
+ SELECT capsule_tenant
+ FROM k8s_state.query_tenant_role
+ WHERE query_tenant_role.role_name = tenant_for_role.role_name
+$$;
+
+REVOKE ALL ON FUNCTION k8s_state.tenant_for_role(name) FROM PUBLIC;
+GRANT EXECUTE ON FUNCTION k8s_state.tenant_for_role(name) TO k8s_query_tenant;
+
+ALTER TABLE k8s_state.resource_state ENABLE ROW LEVEL SECURITY;
+ALTER TABLE k8s_state.resource_state FORCE ROW LEVEL SECURITY;
+
+CREATE POLICY writer_current_state ON k8s_state.resource_state
+FOR ALL TO k8s_state_writer
+USING (true)
+WITH CHECK (true);
+
+CREATE POLICY tenant_current_state ON k8s_state.resource_state
+FOR SELECT TO k8s_query_tenant
+USING (
+ capsule_tenant = k8s_state.tenant_for_role(current_user)
+ AND (
+ namespace <> ''
+ OR (api_group = '' AND resource = 'namespaces')
+ )
+);
+
+CREATE POLICY admin_current_state ON k8s_state.resource_state
+FOR SELECT TO k8s_query_admin
+USING (true);
+
+CREATE VIEW k8s_api.current_resources
+WITH (security_barrier = true, security_invoker = true) AS
+SELECT
+ cluster_id,
+ api_group,
+ resource,
+ namespace,
+ name,
+ uid,
+ resource_version,
+ schema_hash,
+ labels,
+ object,
+ source_time,
+ ingested_at
+FROM k8s_state.resource_state;
+
+REVOKE ALL ON ALL TABLES IN SCHEMA k8s_state FROM PUBLIC;
+REVOKE ALL ON ALL TABLES IN SCHEMA k8s_api FROM PUBLIC;
+GRANT USAGE ON SCHEMA k8s_state TO k8s_state_writer, k8s_state_exporter;
+GRANT SELECT, INSERT, UPDATE, DELETE ON k8s_state.resource_state TO k8s_state_writer;
+GRANT SELECT, INSERT, UPDATE ON k8s_state.watch_checkpoint TO k8s_state_writer;
+GRANT SELECT, INSERT, UPDATE ON k8s_state.resource_schema TO k8s_state_writer;
+GRANT SELECT, INSERT ON k8s_state.resource_event_outbox TO k8s_state_writer;
+GRANT SELECT, UPDATE, DELETE ON k8s_state.resource_event_outbox TO k8s_state_exporter;
+GRANT USAGE ON SCHEMA k8s_api TO k8s_query_tenant, k8s_query_admin;
+GRANT SELECT ON k8s_state.resource_state TO k8s_query_tenant, k8s_query_admin;
+GRANT SELECT ON k8s_api.current_resources TO k8s_query_tenant, k8s_query_admin;
+
+CREATE FUNCTION k8s_state.register_tenant_role(p_role_name name, p_capsule_tenant text)
+RETURNS void
+LANGUAGE sql
+VOLATILE
+SECURITY DEFINER
+SET search_path = k8s_state, pg_catalog
+AS $$
+ INSERT INTO k8s_state.query_tenant_role (role_name, capsule_tenant)
+ VALUES (p_role_name, p_capsule_tenant)
+ ON CONFLICT (role_name) DO UPDATE
+ SET capsule_tenant = EXCLUDED.capsule_tenant
+$$;
+
+REVOKE ALL ON FUNCTION k8s_state.register_tenant_role(name, text) FROM PUBLIC;
+GRANT USAGE ON SCHEMA k8s_state TO k8s_role_admin;
+GRANT EXECUTE ON FUNCTION k8s_state.register_tenant_role(name, text) TO k8s_role_admin;
diff --git a/libs/fleet/backend/statequery/roles.go b/libs/fleet/backend/statequery/roles.go
new file mode 100644
index 0000000000..c2dbb5af28
--- /dev/null
+++ b/libs/fleet/backend/statequery/roles.go
@@ -0,0 +1,142 @@
+package statequery
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/jackc/pgx/v5"
+)
+
+type FixedRoleURLs struct {
+ Writer string
+ Exporter string
+ Query string
+ RoleAdmin string
+}
+
+var fixedLoginRoles = []struct {
+ name string
+ url func(FixedRoleURLs) string
+ attr string
+}{
+ {"k8s_state_writer", func(urls FixedRoleURLs) string { return urls.Writer }, "LOGIN INHERIT NOCREATEROLE"},
+ {"k8s_state_exporter", func(urls FixedRoleURLs) string { return urls.Exporter }, "LOGIN INHERIT NOCREATEROLE"},
+ {"k8s_query_broker", func(urls FixedRoleURLs) string { return urls.Query }, "LOGIN NOINHERIT NOCREATEROLE"},
+ {"k8s_role_admin", func(urls FixedRoleURLs) string { return urls.RoleAdmin }, "LOGIN NOINHERIT CREATEROLE"},
+}
+
+func parseFixedRoleURLs(urls FixedRoleURLs) (map[string]string, error) {
+ configured := 0
+ for _, role := range fixedLoginRoles {
+ if role.url(urls) != "" {
+ configured++
+ }
+ }
+ if configured == 0 {
+ return map[string]string{}, nil
+ }
+ if configured != len(fixedLoginRoles) {
+ return nil, fmt.Errorf("all fixed state role database URLs must be configured together")
+ }
+
+ credentials := make(map[string]string, len(fixedLoginRoles))
+ for _, role := range fixedLoginRoles {
+ config, err := pgx.ParseConfig(role.url(urls))
+ if err != nil {
+ return nil, fmt.Errorf("parse database URL for %s: %w", role.name, err)
+ }
+ if config.User != role.name {
+ return nil, fmt.Errorf("database URL for %s uses unexpected user %q", role.name, config.User)
+ }
+ if config.Password == "" {
+ return nil, fmt.Errorf("database URL for %s has no password", role.name)
+ }
+ credentials[role.name] = config.Password
+ }
+ return credentials, nil
+}
+
+// ReconcileFixedRoles creates the fixed least-privilege roles and rotates their passwords.
+func ReconcileFixedRoles(ctx context.Context, adminURL string, urls FixedRoleURLs) error {
+ credentials, err := parseFixedRoleURLs(urls)
+ if err != nil {
+ return err
+ }
+ if len(credentials) == 0 {
+ return nil
+ }
+ adminConfig, err := pgx.ParseConfig(adminURL)
+ if err != nil {
+ return fmt.Errorf("parse state role administrator URL: %w", err)
+ }
+ conn, err := pgx.ConnectConfig(ctx, adminConfig)
+ if err != nil {
+ return fmt.Errorf("connect state role administrator: %w", err)
+ }
+ defer conn.Close(ctx)
+
+ tx, err := conn.Begin(ctx)
+ if err != nil {
+ return fmt.Errorf("begin fixed role reconciliation: %w", err)
+ }
+ defer tx.Rollback(ctx)
+ if _, err := tx.Exec(ctx, `select pg_advisory_xact_lock(hashtext('cyclops-k8s-state-fixed-roles'))`); err != nil {
+ return fmt.Errorf("lock fixed role reconciliation: %w", err)
+ }
+
+ for _, role := range []struct{ name, attr string }{
+ {"k8s_state_owner", "NOLOGIN INHERIT NOCREATEROLE"},
+ {"k8s_query_tenant", "NOLOGIN INHERIT NOCREATEROLE"},
+ {"k8s_query_admin", "NOLOGIN INHERIT NOCREATEROLE"},
+ } {
+ if err := ensureRole(ctx, tx, role.name, role.attr, ""); err != nil {
+ return err
+ }
+ }
+ for _, role := range fixedLoginRoles {
+ if err := ensureRole(ctx, tx, role.name, role.attr, credentials[role.name]); err != nil {
+ return err
+ }
+ }
+
+ adminIdentifier := pgx.Identifier{adminConfig.User}.Sanitize()
+ for _, statement := range []string{
+ "grant k8s_state_owner to " + adminIdentifier,
+ "grant k8s_query_admin to k8s_query_broker",
+ "grant k8s_query_tenant to k8s_role_admin with admin option",
+ } {
+ if _, err := tx.Exec(ctx, statement); err != nil {
+ return fmt.Errorf("apply fixed role membership: %w", err)
+ }
+ }
+
+ if err := tx.Commit(ctx); err != nil {
+ return fmt.Errorf("commit fixed role reconciliation: %w", err)
+ }
+ return nil
+}
+
+func ensureRole(ctx context.Context, tx pgx.Tx, name, attributes, password string) error {
+ var exists bool
+ if err := tx.QueryRow(ctx, `select exists(select 1 from pg_roles where rolname = $1)`, name).Scan(&exists); err != nil {
+ return fmt.Errorf("check role %s: %w", name, err)
+ }
+ identifier := pgx.Identifier{name}.Sanitize()
+ if !exists {
+ if _, err := tx.Exec(ctx, "create role "+identifier); err != nil {
+ return fmt.Errorf("create role %s: %w", name, err)
+ }
+ }
+ statement := "alter role " + identifier + " " + attributes
+ if password != "" {
+ var passwordClause string
+ if err := tx.QueryRow(ctx, `select format(' password %L', $1::text)`, password).Scan(&passwordClause); err != nil {
+ return fmt.Errorf("quote password for role %s: %w", name, err)
+ }
+ statement += passwordClause
+ }
+ if _, err := tx.Exec(ctx, statement); err != nil {
+ return fmt.Errorf("configure role %s: %w", name, err)
+ }
+ return nil
+}
diff --git a/libs/fleet/backend/statequery/roles_pg_test.go b/libs/fleet/backend/statequery/roles_pg_test.go
new file mode 100644
index 0000000000..9d4c97efac
--- /dev/null
+++ b/libs/fleet/backend/statequery/roles_pg_test.go
@@ -0,0 +1,68 @@
+package statequery
+
+import (
+ "context"
+ "net/url"
+ "os"
+ "testing"
+
+ "github.com/jackc/pgx/v5/pgxpool"
+)
+
+func fixedTestRoleURLs(t *testing.T, adminURL string) FixedRoleURLs {
+ t.Helper()
+ parsed, err := url.Parse(adminURL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ withRole := func(role, password string) string {
+ copy := *parsed
+ copy.User = url.UserPassword(role, password)
+ return copy.String()
+ }
+ return FixedRoleURLs{
+ Writer: withRole("k8s_state_writer", "test-writer"),
+ Exporter: withRole("k8s_state_exporter", "test-exporter"),
+ Query: withRole("k8s_query_broker", "test-query"),
+ RoleAdmin: withRole("k8s_role_admin", "test-role-admin"),
+ }
+}
+
+func TestReconcileFixedRolesCreatesLeastPrivilegeRoles(t *testing.T) {
+ adminURL := os.Getenv("CYCLOPS_TEST_DATABASE_URL")
+ if adminURL == "" {
+ t.Skip("set CYCLOPS_TEST_DATABASE_URL to run the Postgres role test")
+ }
+ ctx := context.Background()
+ if err := ReconcileFixedRoles(ctx, adminURL, fixedTestRoleURLs(t, adminURL)); err != nil {
+ t.Fatalf("ReconcileFixedRoles: %v", err)
+ }
+
+ pool, err := pgxpool.New(ctx, adminURL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer pool.Close()
+
+ expected := map[string]struct {
+ login, inherit, createRole bool
+ }{
+ "k8s_state_owner": {false, true, false},
+ "k8s_state_writer": {true, true, false},
+ "k8s_state_exporter": {true, true, false},
+ "k8s_query_tenant": {false, true, false},
+ "k8s_query_admin": {false, true, false},
+ "k8s_query_broker": {true, false, false},
+ "k8s_role_admin": {true, false, true},
+ }
+ for role, want := range expected {
+ var login, inherit, createRole bool
+ err := pool.QueryRow(ctx, `select rolcanlogin, rolinherit, rolcreaterole from pg_roles where rolname = $1`, role).Scan(&login, &inherit, &createRole)
+ if err != nil {
+ t.Fatalf("query role %s: %v", role, err)
+ }
+ if login != want.login || inherit != want.inherit || createRole != want.createRole {
+ t.Errorf("role %s flags = login:%t inherit:%t createrole:%t, want %+v", role, login, inherit, createRole, want)
+ }
+ }
+}
diff --git a/libs/fleet/backend/statequery/roles_test.go b/libs/fleet/backend/statequery/roles_test.go
new file mode 100644
index 0000000000..fa7a12ce72
--- /dev/null
+++ b/libs/fleet/backend/statequery/roles_test.go
@@ -0,0 +1,43 @@
+package statequery
+
+import "testing"
+
+func TestParseFixedRoleURLsRequiresExpectedUsers(t *testing.T) {
+ urls := FixedRoleURLs{
+ Writer: "postgres://k8s_state_writer:[email protected]/cyclops",
+ Exporter: "postgres://k8s_state_exporter:[email protected]/cyclops",
+ Query: "postgres://k8s_query_broker:[email protected]/cyclops",
+ RoleAdmin: "postgres://k8s_role_admin:[email protected]/cyclops",
+ }
+ credentials, err := parseFixedRoleURLs(urls)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got := credentials["k8s_query_broker"]; got != "query" {
+ t.Fatalf("query broker password = %q", got)
+ }
+
+ urls.Writer = "postgres://wrong:[email protected]/cyclops"
+ if _, err := parseFixedRoleURLs(urls); err == nil {
+ t.Fatal("expected unexpected writer username to fail")
+ }
+}
+
+func TestParseFixedRoleURLsAllowsAllURLsAbsent(t *testing.T) {
+ credentials, err := parseFixedRoleURLs(FixedRoleURLs{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(credentials) != 0 {
+ t.Fatalf("credentials = %#v, want empty", credentials)
+ }
+}
+
+func TestParseFixedRoleURLsRejectsPartialConfiguration(t *testing.T) {
+ _, err := parseFixedRoleURLs(FixedRoleURLs{
+ Writer: "postgres://k8s_state_writer:[email protected]/cyclops",
+ })
+ if err == nil {
+ t.Fatal("expected partial fixed-role URL configuration to fail")
+ }
+}
diff --git a/libs/fleet/backend/statequery/schema.go b/libs/fleet/backend/statequery/schema.go
new file mode 100644
index 0000000000..b055439e7b
--- /dev/null
+++ b/libs/fleet/backend/statequery/schema.go
@@ -0,0 +1,153 @@
+package statequery
+
+import (
+ "context"
+ "crypto/sha256"
+ "embed"
+ "encoding/hex"
+ "errors"
+ "fmt"
+ "io/fs"
+ "sort"
+ "strings"
+
+ "github.com/jackc/pgx/v5"
+)
+
+//go:embed migrations/*.sql
+var migrations embed.FS
+
+type migrationFile struct {
+ Name string
+ SQL string
+ Digest string
+}
+
+func migrationSQL(name string) (string, error) {
+ contents, err := migrations.ReadFile("migrations/" + name)
+ if err != nil {
+ return "", fmt.Errorf("read state migration %s: %w", name, err)
+ }
+ return string(contents), nil
+}
+
+func embeddedMigrations() ([]migrationFile, error) {
+ entries, err := fs.ReadDir(migrations, "migrations")
+ if err != nil {
+ return nil, fmt.Errorf("list state migrations: %w", err)
+ }
+
+ files := make([]migrationFile, 0, len(entries))
+ for _, entry := range entries {
+ if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sql") {
+ continue
+ }
+ sql, err := migrationSQL(entry.Name())
+ if err != nil {
+ return nil, err
+ }
+ digest := sha256.Sum256([]byte(sql))
+ files = append(files, migrationFile{
+ Name: entry.Name(),
+ SQL: sql,
+ Digest: hex.EncodeToString(digest[:]),
+ })
+ }
+ sort.Slice(files, func(i, j int) bool { return files[i].Name < files[j].Name })
+ return files, nil
+}
+
+func checkAppliedDigest(name, applied, current string) error {
+ if applied != current {
+ return fmt.Errorf("state migration %s changed after application: recorded %s, current %s", name, applied, current)
+ }
+ return nil
+}
+
+// Migrate applies embedded state-query migrations exactly once in filename order.
+func Migrate(ctx context.Context, adminURL string) error {
+ if adminURL == "" {
+ return nil
+ }
+ files, err := embeddedMigrations()
+ if err != nil {
+ return err
+ }
+ config, err := pgx.ParseConfig(adminURL)
+ if err != nil {
+ return fmt.Errorf("parse state migration database URL: %w", err)
+ }
+ conn, err := pgx.ConnectConfig(ctx, config)
+ if err != nil {
+ return fmt.Errorf("connect state migration database: %w", err)
+ }
+ defer conn.Close(ctx)
+
+ tx, err := conn.Begin(ctx)
+ if err != nil {
+ return fmt.Errorf("begin state migration transaction: %w", err)
+ }
+ defer tx.Rollback(ctx)
+
+ if _, err := tx.Exec(ctx, `select pg_advisory_xact_lock(hashtext('cyclops-k8s-state-migrations'))`); err != nil {
+ return fmt.Errorf("lock state migrations: %w", err)
+ }
+ if _, err := tx.Exec(ctx, `create schema if not exists k8s_state authorization k8s_state_owner`); err != nil {
+ return fmt.Errorf("create state migration schema: %w", err)
+ }
+ if _, err := tx.Exec(ctx, `alter schema k8s_state owner to k8s_state_owner`); err != nil {
+ return fmt.Errorf("own state migration schema: %w", err)
+ }
+ if _, err := tx.Exec(ctx, `create schema if not exists k8s_api authorization k8s_state_owner`); err != nil {
+ return fmt.Errorf("create state API schema: %w", err)
+ }
+ if _, err := tx.Exec(ctx, `alter schema k8s_api owner to k8s_state_owner`); err != nil {
+ return fmt.Errorf("own state API schema: %w", err)
+ }
+ if _, err := tx.Exec(ctx, `revoke create on schema public, k8s_state, k8s_api from public`); err != nil {
+ return fmt.Errorf("revoke public state schema creation: %w", err)
+ }
+ if _, err := tx.Exec(ctx, `
+ create table if not exists k8s_state.schema_migrations (
+ filename text primary key,
+ sha256 text not null,
+ applied_at timestamptz not null default clock_timestamp()
+ )`); err != nil {
+ return fmt.Errorf("create state migration ledger: %w", err)
+ }
+ if _, err := tx.Exec(ctx, `alter table k8s_state.schema_migrations owner to k8s_state_owner`); err != nil {
+ return fmt.Errorf("own state migration ledger: %w", err)
+ }
+ if _, err := tx.Exec(ctx, `set local role k8s_state_owner`); err != nil {
+ return fmt.Errorf("assume state owner role: %w", err)
+ }
+
+ for _, file := range files {
+ var appliedDigest string
+ err := tx.QueryRow(ctx, `select sha256 from k8s_state.schema_migrations where filename = $1`, file.Name).Scan(&appliedDigest)
+ switch {
+ case err == nil:
+ if err := checkAppliedDigest(file.Name, appliedDigest, file.Digest); err != nil {
+ return err
+ }
+ continue
+ case !errors.Is(err, pgx.ErrNoRows):
+ return fmt.Errorf("read state migration %s: %w", file.Name, err)
+ }
+
+ if _, err := tx.Exec(ctx, file.SQL); err != nil {
+ return fmt.Errorf("apply state migration %s: %w", file.Name, err)
+ }
+ if _, err := tx.Exec(ctx,
+ `insert into k8s_state.schema_migrations (filename, sha256) values ($1, $2)`,
+ file.Name, file.Digest,
+ ); err != nil {
+ return fmt.Errorf("record state migration %s: %w", file.Name, err)
+ }
+ }
+
+ if err := tx.Commit(ctx); err != nil {
+ return fmt.Errorf("commit state migrations: %w", err)
+ }
+ return nil
+}
diff --git a/libs/fleet/backend/statequery/schema_pg_test.go b/libs/fleet/backend/statequery/schema_pg_test.go
new file mode 100644
index 0000000000..b777d70ad4
--- /dev/null
+++ b/libs/fleet/backend/statequery/schema_pg_test.go
@@ -0,0 +1,57 @@
+package statequery
+
+import (
+ "context"
+ "os"
+ "testing"
+
+ "github.com/jackc/pgx/v5/pgxpool"
+)
+
+func TestMigrateCreatesStateSchemaAndRLS(t *testing.T) {
+ url := os.Getenv("CYCLOPS_TEST_DATABASE_URL")
+ if url == "" {
+ t.Skip("set CYCLOPS_TEST_DATABASE_URL to run the Postgres schema test")
+ }
+ ctx := context.Background()
+ if err := ReconcileFixedRoles(ctx, url, fixedTestRoleURLs(t, url)); err != nil {
+ t.Fatalf("ReconcileFixedRoles: %v", err)
+ }
+ if err := Migrate(ctx, url); err != nil {
+ t.Fatalf("Migrate: %v", err)
+ }
+ if err := Migrate(ctx, url); err != nil {
+ t.Fatalf("idempotent Migrate: %v", err)
+ }
+
+ pool, err := pgxpool.New(ctx, url)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer pool.Close()
+
+ for _, relation := range []string{
+ "k8s_state.resource_state",
+ "k8s_state.watch_checkpoint",
+ "k8s_state.resource_event_outbox",
+ "k8s_state.resource_schema",
+ "k8s_state.query_tenant_role",
+ "k8s_api.current_resources",
+ } {
+ var exists bool
+ if err := pool.QueryRow(ctx, `select to_regclass($1) is not null`, relation).Scan(&exists); err != nil {
+ t.Fatal(err)
+ }
+ if !exists {
+ t.Errorf("relation %s does not exist", relation)
+ }
+ }
+
+ var forced bool
+ if err := pool.QueryRow(ctx, `select relforcerowsecurity from pg_class where oid = 'k8s_state.resource_state'::regclass`).Scan(&forced); err != nil {
+ t.Fatal(err)
+ }
+ if !forced {
+ t.Fatal("resource_state does not FORCE ROW LEVEL SECURITY")
+ }
+}
diff --git a/libs/fleet/backend/statequery/schema_test.go b/libs/fleet/backend/statequery/schema_test.go
new file mode 100644
index 0000000000..fb32996f39
--- /dev/null
+++ b/libs/fleet/backend/statequery/schema_test.go
@@ -0,0 +1,60 @@
+package statequery
+
+import (
+ "encoding/hex"
+ "strings"
+ "testing"
+)
+
+func TestEmbeddedMigrationDefinesStateSchemaAndRLS(t *testing.T) {
+ migration, err := migrationSQL("0001_postgres_state.sql")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ for _, required := range []string{
+ "CREATE TABLE k8s_state.resource_state",
+ "CREATE TABLE k8s_state.watch_checkpoint",
+ "CREATE TABLE k8s_state.resource_event_outbox",
+ "CREATE TABLE k8s_state.resource_schema",
+ "CREATE TABLE k8s_state.query_tenant_role",
+ "ALTER TABLE k8s_state.resource_state FORCE ROW LEVEL SECURITY",
+ "CREATE POLICY writer_current_state",
+ "CREATE POLICY tenant_current_state",
+ "resource = 'namespaces'",
+ "CREATE VIEW k8s_api.current_resources",
+ "GRANT SELECT ON k8s_api.current_resources TO k8s_query_tenant, k8s_query_admin",
+ } {
+ if !strings.Contains(migration, required) {
+ t.Errorf("migration missing %q", required)
+ }
+ }
+}
+
+func TestEmbeddedMigrationsAreSortedAndDigested(t *testing.T) {
+ files, err := embeddedMigrations()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(files) != 1 {
+ t.Fatalf("len(files) = %d, want 1", len(files))
+ }
+ if files[0].Name != "0001_postgres_state.sql" {
+ t.Fatalf("Name = %q", files[0].Name)
+ }
+ if len(files[0].Digest) != 64 {
+ t.Fatalf("digest length = %d, want 64", len(files[0].Digest))
+ }
+ if _, err := hex.DecodeString(files[0].Digest); err != nil {
+ t.Fatalf("digest is not hexadecimal: %v", err)
+ }
+}
+
+func TestCheckAppliedDigestRejectsChangedMigration(t *testing.T) {
+ if err := checkAppliedDigest("0001.sql", "old", "new"); err == nil {
+ t.Fatal("expected changed migration digest to fail")
+ }
+ if err := checkAppliedDigest("0001.sql", "same", "same"); err != nil {
+ t.Fatalf("unchanged digest failed: %v", err)
+ }
+}
diff --git a/libs/fleet/backend/statequery/validator.go b/libs/fleet/backend/statequery/validator.go
new file mode 100644
index 0000000000..6213370481
--- /dev/null
+++ b/libs/fleet/backend/statequery/validator.go
@@ -0,0 +1,139 @@
+package statequery
+
+import (
+ "errors"
+ "fmt"
+ "strings"
+
+ pg_query "github.com/pganalyze/pg_query_go/v6"
+ "google.golang.org/protobuf/reflect/protoreflect"
+)
+
+var ErrInvalidQuery = errors.New("invalid state query")
+
+type Limits struct {
+ MaxRows int
+ TimeoutMS int
+ MaxSQLBytes int
+}
+
+type ValidatedQuery struct {
+ SQL string
+ MaxRows int
+ TimeoutMS int
+}
+
+func DefaultLimits() Limits {
+ return Limits{MaxRows: 1000, TimeoutMS: 5000, MaxSQLBytes: 65536}
+}
+
+func Validate(sql string, limits Limits) (ValidatedQuery, error) {
+ sql = strings.TrimSpace(sql)
+ if sql == "" {
+ return ValidatedQuery{}, fmt.Errorf("%w: SQL is empty", ErrInvalidQuery)
+ }
+ if limits.MaxRows <= 0 || limits.TimeoutMS <= 0 || limits.MaxSQLBytes <= 0 {
+ return ValidatedQuery{}, fmt.Errorf("%w: invalid server limits", ErrInvalidQuery)
+ }
+ if len(sql) > limits.MaxSQLBytes {
+ return ValidatedQuery{}, fmt.Errorf("%w: SQL exceeds byte limit", ErrInvalidQuery)
+ }
+
+ tree, err := pg_query.Parse(sql)
+ if err != nil {
+ return ValidatedQuery{}, fmt.Errorf("%w: parse failed", ErrInvalidQuery)
+ }
+ if len(tree.Stmts) != 1 || tree.Stmts[0].Stmt.GetSelectStmt() == nil {
+ return ValidatedQuery{}, fmt.Errorf("%w: exactly one SELECT is required", ErrInvalidQuery)
+ }
+
+ ctes := map[string]bool{}
+ if err := walkMessage(tree.ProtoReflect(), func(message protoreflect.Message) error {
+ if cte, ok := message.Interface().(*pg_query.CommonTableExpr); ok {
+ ctes[cte.GetCtename()] = true
+ }
+ return nil
+ }); err != nil {
+ return ValidatedQuery{}, err
+ }
+ if err := walkMessage(tree.ProtoReflect(), func(message protoreflect.Message) error {
+ switch value := message.Interface().(type) {
+ case *pg_query.InsertStmt, *pg_query.UpdateStmt, *pg_query.DeleteStmt,
+ *pg_query.MergeStmt, *pg_query.CopyStmt, *pg_query.VariableSetStmt,
+ *pg_query.TransactionStmt, *pg_query.CreateStmt, *pg_query.AlterTableStmt,
+ *pg_query.DropStmt, *pg_query.TruncateStmt, *pg_query.CallStmt,
+ *pg_query.DoStmt, *pg_query.IntoClause:
+ return fmt.Errorf("%w: writable or utility statement", ErrInvalidQuery)
+ case *pg_query.RangeVar:
+ if value.GetSchemaname() == "" && ctes[value.GetRelname()] {
+ return nil
+ }
+ if value.GetSchemaname() != "k8s_api" || value.GetRelname() != "current_resources" {
+ return fmt.Errorf("%w: relation is not allowlisted", ErrInvalidQuery)
+ }
+ case *pg_query.FuncCall:
+ name := functionName(value)
+ if !allowedFunction(name) {
+ return fmt.Errorf("%w: function %s is not allowlisted", ErrInvalidQuery, name)
+ }
+ }
+ return nil
+ }); err != nil {
+ return ValidatedQuery{}, err
+ }
+
+ inner := strings.TrimSuffix(sql, ";")
+ return ValidatedQuery{
+ SQL: fmt.Sprintf("SELECT * FROM (%s) AS cyclops_state_query LIMIT %d", inner, limits.MaxRows+1),
+ MaxRows: limits.MaxRows,
+ TimeoutMS: limits.TimeoutMS,
+ }, nil
+}
+
+func walkMessage(message protoreflect.Message, visit func(protoreflect.Message) error) error {
+ if !message.IsValid() {
+ return nil
+ }
+ if err := visit(message); err != nil {
+ return err
+ }
+ var walkErr error
+ message.Range(func(field protoreflect.FieldDescriptor, value protoreflect.Value) bool {
+ if field.IsList() && field.Kind() == protoreflect.MessageKind {
+ list := value.List()
+ for index := 0; index < list.Len(); index++ {
+ if err := walkMessage(list.Get(index).Message(), visit); err != nil {
+ walkErr = err
+ return false
+ }
+ }
+ return true
+ }
+ if field.Kind() == protoreflect.MessageKind {
+ walkErr = walkMessage(value.Message(), visit)
+ return walkErr == nil
+ }
+ return true
+ })
+ return walkErr
+}
+
+func functionName(call *pg_query.FuncCall) string {
+ parts := make([]string, 0, len(call.GetFuncname()))
+ for _, node := range call.GetFuncname() {
+ if value := node.GetString_(); value != nil {
+ parts = append(parts, strings.ToLower(value.GetSval()))
+ }
+ }
+ return strings.Join(parts, ".")
+}
+
+func allowedFunction(name string) bool {
+ switch name {
+ case "count", "min", "max", "sum", "avg", "coalesce", "lower", "upper",
+ "length", "jsonb_typeof", "jsonb_path_exists", "date_trunc":
+ return true
+ default:
+ return false
+ }
+}
diff --git a/libs/fleet/backend/statequery/validator_test.go b/libs/fleet/backend/statequery/validator_test.go
new file mode 100644
index 0000000000..f3a9ffadd2
--- /dev/null
+++ b/libs/fleet/backend/statequery/validator_test.go
@@ -0,0 +1,59 @@
+package statequery
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestValidateRejectsUnsafeSQL(t *testing.T) {
+ for _, query := range []string{
+ "select 1; select 2",
+ "delete from k8s_api.current_resources",
+ "with gone as (delete from k8s_api.current_resources returning *) select * from gone",
+ "select * into leaked from k8s_api.current_resources",
+ "copy k8s_api.current_resources to stdout",
+ "set role k8s_query_admin",
+ "select set_config('role', 'k8s_query_admin', false)",
+ "select * from pg_catalog.pg_roles",
+ "select pg_sleep(60)",
+ "select * from public.current_resources",
+ "select * from current_resources",
+ } {
+ t.Run(query, func(t *testing.T) {
+ if _, err := Validate(query, DefaultLimits()); err == nil {
+ t.Fatalf("accepted unsafe SQL: %s", query)
+ }
+ })
+ }
+}
+
+func TestValidateAcceptsReadOnlyStateQueries(t *testing.T) {
+ for _, query := range []string{
+ `select name, object->'metadata'->>'name' from k8s_api.current_resources order by name`,
+ `with owned as (select * from k8s_api.current_resources where resource = 'namespaces') select count(*) from owned`,
+ `select capsule_tenant, count(*) from k8s_api.current_resources group by capsule_tenant`,
+ `select name from k8s_api.current_resources where labels @> '{"team":"a"}'::jsonb`,
+ } {
+ t.Run(query, func(t *testing.T) {
+ validated, err := Validate(query, Limits{MaxRows: 25, TimeoutMS: 5000, MaxSQLBytes: 65536})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(validated.SQL, "LIMIT 26") {
+ t.Fatalf("validated SQL does not include truncation sentinel: %s", validated.SQL)
+ }
+ if validated.MaxRows != 25 {
+ t.Fatalf("MaxRows = %d", validated.MaxRows)
+ }
+ })
+ }
+}
+
+func TestValidateEnforcesInputLimits(t *testing.T) {
+ if _, err := Validate("", DefaultLimits()); err == nil {
+ t.Fatal("accepted empty SQL")
+ }
+ if _, err := Validate("select * from k8s_api.current_resources", Limits{MaxRows: 0, TimeoutMS: 1, MaxSQLBytes: 1}); err == nil {
+ t.Fatal("accepted SQL exceeding byte limit")
+ }
+}