Compare commits
35 Commits
0782675a2b
...
ha-executo
| Author | SHA1 | Date | |
|---|---|---|---|
| f93e9229d2 | |||
| 8e91440f23 | |||
| 8278030699 | |||
| b34617ded1 | |||
| b6446cc574 | |||
| cf82de87ea | |||
| a4c303ec84 | |||
| a0f59114a3 | |||
| 104dcbb1b1 | |||
| b342005e17 | |||
| 4b525f4641 | |||
|
|
7ef2b59b23 | ||
| 3a13bf754a | |||
| f4ef823f43 | |||
| ab7d31de2f | |||
| 938c271ff5 | |||
| da8055cb79 | |||
| 03a239d22b | |||
| ba83958337 | |||
| c11bc1a2bf | |||
| eb82755137 | |||
| 058f392616 | |||
| 0264a66b5a | |||
| 542e72a454 | |||
| a118563366 | |||
| a057ad5db5 | |||
| 8e273ec683 | |||
| 16f1c2f079 | |||
| 62307e8c65 | |||
| 2ebb03b868 | |||
| af5175b96a | |||
| 8af8c1af9c | |||
| d4c6240485 | |||
| 4d5a3b1bf5 | |||
| 8ba7e3bb84 |
0
.codex_write_test
Normal file
0
.codex_write_test
Normal file
@@ -19,7 +19,7 @@ env:
|
|||||||
jobs:
|
jobs:
|
||||||
rust-fmt:
|
rust-fmt:
|
||||||
name: Rustfmt
|
name: Rustfmt
|
||||||
runs-on: ubuntu-latest
|
runs-on: build-amd64
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -45,7 +45,7 @@ jobs:
|
|||||||
|
|
||||||
rust-clippy:
|
rust-clippy:
|
||||||
name: Clippy
|
name: Clippy
|
||||||
runs-on: ubuntu-latest
|
runs-on: build-amd64
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -91,7 +91,7 @@ jobs:
|
|||||||
|
|
||||||
rust-test:
|
rust-test:
|
||||||
name: Tests
|
name: Tests
|
||||||
runs-on: ubuntu-latest
|
runs-on: build-amd64
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -135,7 +135,7 @@ jobs:
|
|||||||
|
|
||||||
rust-audit:
|
rust-audit:
|
||||||
name: Cargo Audit & Deny
|
name: Cargo Audit & Deny
|
||||||
runs-on: ubuntu-latest
|
runs-on: build-amd64
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -188,7 +188,7 @@ jobs:
|
|||||||
|
|
||||||
web-blocking:
|
web-blocking:
|
||||||
name: Web Blocking Checks
|
name: Web Blocking Checks
|
||||||
runs-on: ubuntu-latest
|
runs-on: build-amd64
|
||||||
defaults:
|
defaults:
|
||||||
run:
|
run:
|
||||||
working-directory: web
|
working-directory: web
|
||||||
@@ -217,7 +217,7 @@ jobs:
|
|||||||
|
|
||||||
security-blocking:
|
security-blocking:
|
||||||
name: Security Blocking Checks
|
name: Security Blocking Checks
|
||||||
runs-on: ubuntu-latest
|
runs-on: build-amd64
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -250,7 +250,7 @@ jobs:
|
|||||||
|
|
||||||
web-advisory:
|
web-advisory:
|
||||||
name: Web Advisory Checks
|
name: Web Advisory Checks
|
||||||
runs-on: ubuntu-latest
|
runs-on: build-amd64
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
defaults:
|
defaults:
|
||||||
run:
|
run:
|
||||||
@@ -279,7 +279,7 @@ jobs:
|
|||||||
|
|
||||||
security-advisory:
|
security-advisory:
|
||||||
name: Security Advisory Checks
|
name: Security Advisory Checks
|
||||||
runs-on: ubuntu-latest
|
runs-on: build-amd64
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
6
.gitignore
vendored
6
.gitignore
vendored
@@ -11,6 +11,7 @@ target/
|
|||||||
# Configuration files (keep *.example.yaml)
|
# Configuration files (keep *.example.yaml)
|
||||||
config.yaml
|
config.yaml
|
||||||
config.*.yaml
|
config.*.yaml
|
||||||
|
!docker/distributable/config.docker.yaml
|
||||||
!config.example.yaml
|
!config.example.yaml
|
||||||
!config.development.yaml
|
!config.development.yaml
|
||||||
!config.test.yaml
|
!config.test.yaml
|
||||||
@@ -35,6 +36,7 @@ logs/
|
|||||||
# Build artifacts
|
# Build artifacts
|
||||||
dist/
|
dist/
|
||||||
build/
|
build/
|
||||||
|
artifacts/
|
||||||
|
|
||||||
# Testing
|
# Testing
|
||||||
coverage/
|
coverage/
|
||||||
@@ -78,4 +80,8 @@ docker-compose.override.yml
|
|||||||
*.pid
|
*.pid
|
||||||
|
|
||||||
packs.examples/
|
packs.examples/
|
||||||
|
packs.external/
|
||||||
codex/
|
codex/
|
||||||
|
|
||||||
|
# Compiled pack binaries (built via Docker or build-pack-binaries.sh)
|
||||||
|
packs/core/sensors/attune-core-timer-sensor
|
||||||
|
|||||||
@@ -4,3 +4,6 @@ web/node_modules/
|
|||||||
web/src/api/
|
web/src/api/
|
||||||
packs.dev/
|
packs.dev/
|
||||||
packs.external/
|
packs.external/
|
||||||
|
tests/
|
||||||
|
docs/
|
||||||
|
*.md
|
||||||
|
|||||||
99
Cargo.lock
generated
99
Cargo.lock
generated
@@ -490,6 +490,7 @@ dependencies = [
|
|||||||
"sha1",
|
"sha1",
|
||||||
"sha2",
|
"sha2",
|
||||||
"sqlx",
|
"sqlx",
|
||||||
|
"subtle",
|
||||||
"tar",
|
"tar",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"thiserror 2.0.18",
|
"thiserror 2.0.18",
|
||||||
@@ -527,6 +528,7 @@ dependencies = [
|
|||||||
"mockito",
|
"mockito",
|
||||||
"predicates",
|
"predicates",
|
||||||
"reqwest 0.13.2",
|
"reqwest 0.13.2",
|
||||||
|
"reqwest-eventsource",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"serde_yaml_ng",
|
"serde_yaml_ng",
|
||||||
@@ -578,6 +580,7 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
|
"url",
|
||||||
"utoipa",
|
"utoipa",
|
||||||
"uuid",
|
"uuid",
|
||||||
"validator",
|
"validator",
|
||||||
@@ -2149,21 +2152,6 @@ version = "0.2.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb"
|
checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "foreign-types"
|
|
||||||
version = "0.3.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
|
|
||||||
dependencies = [
|
|
||||||
"foreign-types-shared",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "foreign-types-shared"
|
|
||||||
version = "0.1.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "form_urlencoded"
|
name = "form_urlencoded"
|
||||||
version = "1.2.2"
|
version = "1.2.2"
|
||||||
@@ -3064,15 +3052,17 @@ dependencies = [
|
|||||||
"futures-util",
|
"futures-util",
|
||||||
"lber",
|
"lber",
|
||||||
"log",
|
"log",
|
||||||
"native-tls",
|
|
||||||
"nom 7.1.3",
|
"nom 7.1.3",
|
||||||
"percent-encoding",
|
"percent-encoding",
|
||||||
|
"rustls",
|
||||||
|
"rustls-native-certs",
|
||||||
"thiserror 2.0.18",
|
"thiserror 2.0.18",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-native-tls",
|
"tokio-rustls",
|
||||||
"tokio-stream",
|
"tokio-stream",
|
||||||
"tokio-util",
|
"tokio-util",
|
||||||
"url",
|
"url",
|
||||||
|
"x509-parser",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -3313,23 +3303,6 @@ dependencies = [
|
|||||||
"version_check",
|
"version_check",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "native-tls"
|
|
||||||
version = "0.2.18"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "465500e14ea162429d264d44189adc38b199b62b1c21eea9f69e4b73cb03bbf2"
|
|
||||||
dependencies = [
|
|
||||||
"libc",
|
|
||||||
"log",
|
|
||||||
"openssl",
|
|
||||||
"openssl-probe",
|
|
||||||
"openssl-sys",
|
|
||||||
"schannel",
|
|
||||||
"security-framework",
|
|
||||||
"security-framework-sys",
|
|
||||||
"tempfile",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nom"
|
name = "nom"
|
||||||
version = "7.1.3"
|
version = "7.1.3"
|
||||||
@@ -3575,50 +3548,12 @@ dependencies = [
|
|||||||
"url",
|
"url",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "openssl"
|
|
||||||
version = "0.10.76"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "951c002c75e16ea2c65b8c7e4d3d51d5530d8dfa7d060b4776828c88cfb18ecf"
|
|
||||||
dependencies = [
|
|
||||||
"bitflags",
|
|
||||||
"cfg-if",
|
|
||||||
"foreign-types",
|
|
||||||
"libc",
|
|
||||||
"once_cell",
|
|
||||||
"openssl-macros",
|
|
||||||
"openssl-sys",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "openssl-macros"
|
|
||||||
version = "0.1.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "openssl-probe"
|
name = "openssl-probe"
|
||||||
version = "0.2.1"
|
version = "0.2.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe"
|
checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "openssl-sys"
|
|
||||||
version = "0.9.112"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "57d55af3b3e226502be1526dfdba67ab0e9c96fc293004e79576b2b9edb0dbdb"
|
|
||||||
dependencies = [
|
|
||||||
"cc",
|
|
||||||
"libc",
|
|
||||||
"pkg-config",
|
|
||||||
"vcpkg",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "option-ext"
|
name = "option-ext"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
@@ -4641,6 +4576,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "758025cb5fccfd3bc2fd74708fd4682be41d99e5dff73c377c0646c6012c73a4"
|
checksum = "758025cb5fccfd3bc2fd74708fd4682be41d99e5dff73c377c0646c6012c73a4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aws-lc-rs",
|
"aws-lc-rs",
|
||||||
|
"log",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"ring",
|
"ring",
|
||||||
"rustls-pki-types",
|
"rustls-pki-types",
|
||||||
@@ -5697,16 +5633,6 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "tokio-native-tls"
|
|
||||||
version = "0.3.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2"
|
|
||||||
dependencies = [
|
|
||||||
"native-tls",
|
|
||||||
"tokio",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio-rustls"
|
name = "tokio-rustls"
|
||||||
version = "0.26.4"
|
version = "0.26.4"
|
||||||
@@ -5748,9 +5674,11 @@ checksum = "d25a406cddcc431a75d3d9afc6a7c0f7428d4891dd973e4d54c56b46127bf857"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"log",
|
"log",
|
||||||
"native-tls",
|
"rustls",
|
||||||
|
"rustls-native-certs",
|
||||||
|
"rustls-pki-types",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-native-tls",
|
"tokio-rustls",
|
||||||
"tungstenite",
|
"tungstenite",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -5937,8 +5865,9 @@ dependencies = [
|
|||||||
"http",
|
"http",
|
||||||
"httparse",
|
"httparse",
|
||||||
"log",
|
"log",
|
||||||
"native-tls",
|
|
||||||
"rand 0.9.2",
|
"rand 0.9.2",
|
||||||
|
"rustls",
|
||||||
|
"rustls-pki-types",
|
||||||
"sha1",
|
"sha1",
|
||||||
"thiserror 2.0.18",
|
"thiserror 2.0.18",
|
||||||
"utf-8",
|
"utf-8",
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ repository = "https://git.rdrx.app/attune-system/attune"
|
|||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
# Async runtime
|
# Async runtime
|
||||||
tokio = { version = "1.50", features = ["full"] }
|
tokio = { version = "1.50", features = ["full"] }
|
||||||
tokio-util = "0.7"
|
tokio-util = { version = "0.7", features = ["io"] }
|
||||||
tokio-stream = { version = "0.1", features = ["sync"] }
|
tokio-stream = { version = "0.1", features = ["sync"] }
|
||||||
|
|
||||||
# Web framework
|
# Web framework
|
||||||
@@ -101,7 +101,7 @@ tar = "0.4"
|
|||||||
flate2 = "1.1"
|
flate2 = "1.1"
|
||||||
|
|
||||||
# WebSocket client
|
# WebSocket client
|
||||||
tokio-tungstenite = { version = "0.28", features = ["native-tls"] }
|
tokio-tungstenite = { version = "0.28", features = ["rustls-tls-native-roots"] }
|
||||||
|
|
||||||
# URL parsing
|
# URL parsing
|
||||||
url = "2.5"
|
url = "2.5"
|
||||||
|
|||||||
107
Makefile
107
Makefile
@@ -4,7 +4,11 @@
|
|||||||
docker-build-workers docker-build-worker-base docker-build-worker-python \
|
docker-build-workers docker-build-worker-base docker-build-worker-python \
|
||||||
docker-build-worker-node docker-build-worker-full deny ci-rust ci-web-blocking ci-web-advisory \
|
docker-build-worker-node docker-build-worker-full deny ci-rust ci-web-blocking ci-web-advisory \
|
||||||
ci-security-blocking ci-security-advisory ci-blocking ci-advisory \
|
ci-security-blocking ci-security-advisory ci-blocking ci-advisory \
|
||||||
fmt-check pre-commit install-git-hooks
|
fmt-check pre-commit install-git-hooks \
|
||||||
|
build-agent docker-build-agent docker-build-agent-arm64 docker-build-agent-all \
|
||||||
|
run-agent run-agent-release \
|
||||||
|
docker-up-agent docker-down-agent \
|
||||||
|
docker-build-pack-binaries docker-build-pack-binaries-arm64 docker-build-pack-binaries-all
|
||||||
|
|
||||||
# Default target
|
# Default target
|
||||||
help:
|
help:
|
||||||
@@ -60,6 +64,21 @@ help:
|
|||||||
@echo " make docker-up - Start services with docker compose"
|
@echo " make docker-up - Start services with docker compose"
|
||||||
@echo " make docker-down - Stop services"
|
@echo " make docker-down - Stop services"
|
||||||
@echo ""
|
@echo ""
|
||||||
|
@echo "Agent (Universal Worker):"
|
||||||
|
@echo " make build-agent - Build statically-linked agent binary (musl)"
|
||||||
|
@echo " make docker-build-agent - Build agent Docker image (amd64, default)"
|
||||||
|
@echo " make docker-build-agent-arm64 - Build agent Docker image (arm64)"
|
||||||
|
@echo " make docker-build-agent-all - Build agent Docker images (amd64 + arm64)"
|
||||||
|
@echo " make run-agent - Run agent in development mode"
|
||||||
|
@echo " make run-agent-release - Run agent in release mode"
|
||||||
|
@echo " make docker-up-agent - Start all services + agent workers (ruby, etc.)"
|
||||||
|
@echo " make docker-down-agent - Stop agent stack"
|
||||||
|
@echo ""
|
||||||
|
@echo "Pack Binaries:"
|
||||||
|
@echo " make docker-build-pack-binaries - Build pack binaries Docker image (amd64, default)"
|
||||||
|
@echo " make docker-build-pack-binaries-arm64 - Build pack binaries Docker image (arm64)"
|
||||||
|
@echo " make docker-build-pack-binaries-all - Build pack binaries Docker images (amd64 + arm64)"
|
||||||
|
@echo ""
|
||||||
@echo "Development:"
|
@echo "Development:"
|
||||||
@echo " make watch - Watch and rebuild on changes"
|
@echo " make watch - Watch and rebuild on changes"
|
||||||
@echo " make install-tools - Install development tools"
|
@echo " make install-tools - Install development tools"
|
||||||
@@ -227,38 +246,86 @@ docker-build-api:
|
|||||||
docker-build-web:
|
docker-build-web:
|
||||||
docker compose build web
|
docker compose build web
|
||||||
|
|
||||||
# Build worker images
|
# Agent binary (statically-linked for injection into any container)
|
||||||
docker-build-workers: docker-build-worker-base docker-build-worker-python docker-build-worker-node docker-build-worker-full
|
AGENT_RUST_TARGET ?= x86_64-unknown-linux-musl
|
||||||
@echo "✅ All worker images built successfully"
|
|
||||||
|
|
||||||
docker-build-worker-base:
|
# Pack binaries (statically-linked for packs volume)
|
||||||
@echo "Building base worker (shell only)..."
|
PACK_BINARIES_RUST_TARGET ?= x86_64-unknown-linux-musl
|
||||||
DOCKER_BUILDKIT=1 docker build --target worker-base -t attune-worker:base -f docker/Dockerfile.worker.optimized .
|
|
||||||
@echo "✅ Base worker image built: attune-worker:base"
|
|
||||||
|
|
||||||
docker-build-worker-python:
|
build-agent:
|
||||||
@echo "Building Python worker (shell + python)..."
|
@echo "Installing musl target (if not already installed)..."
|
||||||
DOCKER_BUILDKIT=1 docker build --target worker-python -t attune-worker:python -f docker/Dockerfile.worker.optimized .
|
rustup target add $(AGENT_RUST_TARGET) 2>/dev/null || true
|
||||||
@echo "✅ Python worker image built: attune-worker:python"
|
@echo "Building statically-linked worker and sensor agent binaries..."
|
||||||
|
SQLX_OFFLINE=true cargo build --release --target $(AGENT_RUST_TARGET) --bin attune-agent --bin attune-sensor-agent
|
||||||
|
strip target/$(AGENT_RUST_TARGET)/release/attune-agent
|
||||||
|
strip target/$(AGENT_RUST_TARGET)/release/attune-sensor-agent
|
||||||
|
@echo "✅ Agent binaries built:"
|
||||||
|
@echo " - target/$(AGENT_RUST_TARGET)/release/attune-agent"
|
||||||
|
@echo " - target/$(AGENT_RUST_TARGET)/release/attune-sensor-agent"
|
||||||
|
@ls -lh target/$(AGENT_RUST_TARGET)/release/attune-agent
|
||||||
|
@ls -lh target/$(AGENT_RUST_TARGET)/release/attune-sensor-agent
|
||||||
|
|
||||||
docker-build-worker-node:
|
docker-build-agent:
|
||||||
@echo "Building Node.js worker (shell + node)..."
|
@echo "Building agent Docker image ($(AGENT_RUST_TARGET))..."
|
||||||
DOCKER_BUILDKIT=1 docker build --target worker-node -t attune-worker:node -f docker/Dockerfile.worker.optimized .
|
DOCKER_BUILDKIT=1 docker buildx build --build-arg RUST_TARGET=$(AGENT_RUST_TARGET) --target agent-init -f docker/Dockerfile.agent -t attune-agent:latest .
|
||||||
@echo "✅ Node.js worker image built: attune-worker:node"
|
@echo "✅ Agent image built: attune-agent:latest ($(AGENT_RUST_TARGET))"
|
||||||
|
|
||||||
docker-build-worker-full:
|
docker-build-agent-arm64:
|
||||||
@echo "Building full worker (all runtimes)..."
|
@echo "Building arm64 agent Docker image..."
|
||||||
DOCKER_BUILDKIT=1 docker build --target worker-full -t attune-worker:full -f docker/Dockerfile.worker.optimized .
|
DOCKER_BUILDKIT=1 docker buildx build --build-arg RUST_TARGET=aarch64-unknown-linux-musl --target agent-init -f docker/Dockerfile.agent -t attune-agent:arm64 .
|
||||||
@echo "✅ Full worker image built: attune-worker:full"
|
@echo "✅ Agent image built: attune-agent:arm64"
|
||||||
|
|
||||||
|
docker-build-agent-all:
|
||||||
|
@echo "Building agent Docker images for all architectures..."
|
||||||
|
$(MAKE) docker-build-agent
|
||||||
|
$(MAKE) docker-build-agent-arm64
|
||||||
|
@echo "✅ All agent images built: attune-agent:latest (amd64), attune-agent:arm64"
|
||||||
|
|
||||||
|
run-agent:
|
||||||
|
cargo run --bin attune-agent
|
||||||
|
|
||||||
|
run-agent-release:
|
||||||
|
cargo run --bin attune-agent --release
|
||||||
|
|
||||||
|
# Pack binaries (statically-linked for packs volume)
|
||||||
|
docker-build-pack-binaries:
|
||||||
|
@echo "Building pack binaries Docker image ($(PACK_BINARIES_RUST_TARGET))..."
|
||||||
|
DOCKER_BUILDKIT=1 docker buildx build --build-arg RUST_TARGET=$(PACK_BINARIES_RUST_TARGET) --target pack-binaries-init -f docker/Dockerfile.pack-binaries -t attune-pack-builder:latest .
|
||||||
|
@echo "✅ Pack binaries image built: attune-pack-builder:latest ($(PACK_BINARIES_RUST_TARGET))"
|
||||||
|
|
||||||
|
docker-build-pack-binaries-arm64:
|
||||||
|
@echo "Building arm64 pack binaries Docker image..."
|
||||||
|
DOCKER_BUILDKIT=1 docker buildx build --build-arg RUST_TARGET=aarch64-unknown-linux-musl --target pack-binaries-init -f docker/Dockerfile.pack-binaries -t attune-pack-builder:arm64 .
|
||||||
|
@echo "✅ Pack binaries image built: attune-pack-builder:arm64"
|
||||||
|
|
||||||
|
docker-build-pack-binaries-all:
|
||||||
|
@echo "Building pack binaries Docker images for all architectures..."
|
||||||
|
$(MAKE) docker-build-pack-binaries
|
||||||
|
$(MAKE) docker-build-pack-binaries-arm64
|
||||||
|
@echo "✅ All pack binary images built: attune-pack-builder:latest (amd64), attune-pack-builder:arm64"
|
||||||
|
|
||||||
|
run-sensor-agent:
|
||||||
|
cargo run --bin attune-sensor-agent
|
||||||
|
|
||||||
|
run-sensor-agent-release:
|
||||||
|
cargo run --bin attune-sensor-agent --release
|
||||||
|
|
||||||
docker-up:
|
docker-up:
|
||||||
@echo "Starting all services with Docker Compose..."
|
@echo "Starting all services with Docker Compose..."
|
||||||
docker compose up -d
|
docker compose up -d
|
||||||
|
|
||||||
|
docker-up-agent:
|
||||||
|
@echo "Starting all services + agent-based workers..."
|
||||||
|
docker compose -f docker-compose.yaml -f docker-compose.agent.yaml up -d
|
||||||
|
|
||||||
docker-down:
|
docker-down:
|
||||||
@echo "Stopping all services..."
|
@echo "Stopping all services..."
|
||||||
docker compose down
|
docker compose down
|
||||||
|
|
||||||
|
docker-down-agent:
|
||||||
|
@echo "Stopping all services (including agent workers)..."
|
||||||
|
docker compose -f docker-compose.yaml -f docker-compose.agent.yaml down
|
||||||
|
|
||||||
docker-down-volumes:
|
docker-down-volumes:
|
||||||
@echo "Stopping all services and removing volumes (WARNING: deletes data)..."
|
@echo "Stopping all services and removing volumes (WARNING: deletes data)..."
|
||||||
docker compose down -v
|
docker compose down -v
|
||||||
|
|||||||
@@ -1,3 +1,26 @@
|
|||||||
1. Set `global.imageRegistry`, `global.imageNamespace`, and `global.imageTag` so the chart pulls the images published by the Gitea workflow.
|
1. Set `global.imageRegistry`, `global.imageNamespace`, and `global.imageTag` so the chart pulls the images published by the Gitea workflow.
|
||||||
2. Set `web.config.apiUrl` and `web.config.wsUrl` to browser-reachable endpoints before exposing the web UI.
|
2. Set `web.config.apiUrl` and `web.config.wsUrl` to browser-reachable endpoints before exposing the web UI.
|
||||||
3. The shared `packs`, `runtime_envs`, and `artifacts` PVCs default to `ReadWriteMany`; your cluster storage class must support RWX or you need to override those claims.
|
3. The shared `packs`, `runtime_envs`, and `artifacts` PVCs default to `ReadWriteMany`; your cluster storage class must support RWX or you need to override those claims.
|
||||||
|
{{- if .Values.agentWorkers }}
|
||||||
|
|
||||||
|
Agent-based workers enabled:
|
||||||
|
{{- range .Values.agentWorkers }}
|
||||||
|
- {{ .name }}: image={{ .image }}, replicas={{ .replicas | default 1 }}
|
||||||
|
{{- if .runtimes }} runtimes={{ join "," .runtimes }}{{ else }} runtimes=auto-detect{{ end }}
|
||||||
|
{{- end }}
|
||||||
|
|
||||||
|
Each agent worker uses an init container to copy the statically-linked
|
||||||
|
attune-agent binary into the worker pod via an emptyDir volume. The agent
|
||||||
|
auto-detects available runtimes in the container and registers with Attune.
|
||||||
|
|
||||||
|
The default sensor deployment also uses the same injection pattern, copying
|
||||||
|
`attune-sensor-agent` into the pod before starting a stock runtime image.
|
||||||
|
|
||||||
|
To add more agent workers, append entries to `agentWorkers` in your values:
|
||||||
|
|
||||||
|
agentWorkers:
|
||||||
|
- name: my-runtime
|
||||||
|
image: my-org/my-image:latest
|
||||||
|
replicas: 1
|
||||||
|
runtimes: [] # auto-detect
|
||||||
|
{{- end }}
|
||||||
|
|||||||
137
charts/attune/templates/agent-workers.yaml
Normal file
137
charts/attune/templates/agent-workers.yaml
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
{{- range .Values.agentWorkers }}
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: {{ include "attune.fullname" $ }}-agent-worker-{{ .name }}
|
||||||
|
labels:
|
||||||
|
{{- include "attune.labels" $ | nindent 4 }}
|
||||||
|
app.kubernetes.io/component: agent-worker-{{ .name }}
|
||||||
|
spec:
|
||||||
|
replicas: {{ .replicas | default 1 }}
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
{{- include "attune.selectorLabels" $ | nindent 6 }}
|
||||||
|
app.kubernetes.io/component: agent-worker-{{ .name }}
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
{{- include "attune.selectorLabels" $ | nindent 8 }}
|
||||||
|
app.kubernetes.io/component: agent-worker-{{ .name }}
|
||||||
|
spec:
|
||||||
|
{{- if $.Values.global.imagePullSecrets }}
|
||||||
|
imagePullSecrets:
|
||||||
|
{{- toYaml $.Values.global.imagePullSecrets | nindent 8 }}
|
||||||
|
{{- end }}
|
||||||
|
{{- if .runtimeClassName }}
|
||||||
|
runtimeClassName: {{ .runtimeClassName }}
|
||||||
|
{{- end }}
|
||||||
|
{{- if .nodeSelector }}
|
||||||
|
nodeSelector:
|
||||||
|
{{- toYaml .nodeSelector | nindent 8 }}
|
||||||
|
{{- end }}
|
||||||
|
{{- if .tolerations }}
|
||||||
|
tolerations:
|
||||||
|
{{- toYaml .tolerations | nindent 8 }}
|
||||||
|
{{- end }}
|
||||||
|
{{- if .stopGracePeriod }}
|
||||||
|
terminationGracePeriodSeconds: {{ .stopGracePeriod }}
|
||||||
|
{{- else }}
|
||||||
|
terminationGracePeriodSeconds: 45
|
||||||
|
{{- end }}
|
||||||
|
initContainers:
|
||||||
|
- name: agent-loader
|
||||||
|
image: {{ include "attune.image" (dict "root" $ "image" $.Values.images.agent) }}
|
||||||
|
imagePullPolicy: {{ $.Values.images.agent.pullPolicy }}
|
||||||
|
command: ["cp", "/usr/local/bin/attune-agent", "/opt/attune/agent/attune-agent"]
|
||||||
|
volumeMounts:
|
||||||
|
- name: agent-bin
|
||||||
|
mountPath: /opt/attune/agent
|
||||||
|
- name: wait-for-schema
|
||||||
|
image: postgres:16-alpine
|
||||||
|
command: ["/bin/sh", "-ec"]
|
||||||
|
args:
|
||||||
|
- |
|
||||||
|
until PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -tAc "SELECT to_regclass('${DB_SCHEMA}.identity')" | grep -q identity; do
|
||||||
|
echo "waiting for schema";
|
||||||
|
sleep 2;
|
||||||
|
done
|
||||||
|
envFrom:
|
||||||
|
- secretRef:
|
||||||
|
name: {{ include "attune.secretName" $ }}
|
||||||
|
- name: wait-for-packs
|
||||||
|
image: busybox:1.36
|
||||||
|
command: ["/bin/sh", "-ec"]
|
||||||
|
args:
|
||||||
|
- |
|
||||||
|
until [ -f /opt/attune/packs/core/pack.yaml ]; do
|
||||||
|
echo "waiting for packs";
|
||||||
|
sleep 2;
|
||||||
|
done
|
||||||
|
volumeMounts:
|
||||||
|
- name: packs
|
||||||
|
mountPath: /opt/attune/packs
|
||||||
|
containers:
|
||||||
|
- name: worker
|
||||||
|
image: {{ .image }}
|
||||||
|
{{- if .imagePullPolicy }}
|
||||||
|
imagePullPolicy: {{ .imagePullPolicy }}
|
||||||
|
{{- end }}
|
||||||
|
command: ["/opt/attune/agent/attune-agent"]
|
||||||
|
envFrom:
|
||||||
|
- secretRef:
|
||||||
|
name: {{ include "attune.secretName" $ }}
|
||||||
|
env:
|
||||||
|
- name: ATTUNE_CONFIG
|
||||||
|
value: /opt/attune/config.yaml
|
||||||
|
- name: ATTUNE__DATABASE__SCHEMA
|
||||||
|
value: {{ $.Values.database.schema | quote }}
|
||||||
|
- name: ATTUNE_WORKER_TYPE
|
||||||
|
value: container
|
||||||
|
- name: ATTUNE_WORKER_NAME
|
||||||
|
valueFrom:
|
||||||
|
fieldRef:
|
||||||
|
fieldPath: metadata.name
|
||||||
|
- name: ATTUNE_API_URL
|
||||||
|
value: http://{{ include "attune.apiServiceName" $ }}:{{ $.Values.api.service.port }}
|
||||||
|
- name: RUST_LOG
|
||||||
|
value: {{ .logLevel | default "info" }}
|
||||||
|
{{- if .runtimes }}
|
||||||
|
- name: ATTUNE_WORKER_RUNTIMES
|
||||||
|
value: {{ join "," .runtimes | quote }}
|
||||||
|
{{- end }}
|
||||||
|
{{- if .env }}
|
||||||
|
{{- toYaml .env | nindent 12 }}
|
||||||
|
{{- end }}
|
||||||
|
resources:
|
||||||
|
{{- toYaml (.resources | default dict) | nindent 12 }}
|
||||||
|
volumeMounts:
|
||||||
|
- name: agent-bin
|
||||||
|
mountPath: /opt/attune/agent
|
||||||
|
readOnly: true
|
||||||
|
- name: config
|
||||||
|
mountPath: /opt/attune/config.yaml
|
||||||
|
subPath: config.yaml
|
||||||
|
- name: packs
|
||||||
|
mountPath: /opt/attune/packs
|
||||||
|
readOnly: true
|
||||||
|
- name: runtime-envs
|
||||||
|
mountPath: /opt/attune/runtime_envs
|
||||||
|
- name: artifacts
|
||||||
|
mountPath: /opt/attune/artifacts
|
||||||
|
volumes:
|
||||||
|
- name: agent-bin
|
||||||
|
emptyDir: {}
|
||||||
|
- name: config
|
||||||
|
configMap:
|
||||||
|
name: {{ include "attune.fullname" $ }}-config
|
||||||
|
- name: packs
|
||||||
|
persistentVolumeClaim:
|
||||||
|
claimName: {{ include "attune.fullname" $ }}-packs
|
||||||
|
- name: runtime-envs
|
||||||
|
persistentVolumeClaim:
|
||||||
|
claimName: {{ include "attune.fullname" $ }}-runtime-envs
|
||||||
|
- name: artifacts
|
||||||
|
persistentVolumeClaim:
|
||||||
|
claimName: {{ include "attune.fullname" $ }}-artifacts
|
||||||
|
{{- end }}
|
||||||
@@ -304,7 +304,15 @@ spec:
|
|||||||
imagePullSecrets:
|
imagePullSecrets:
|
||||||
{{- toYaml .Values.global.imagePullSecrets | nindent 8 }}
|
{{- toYaml .Values.global.imagePullSecrets | nindent 8 }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
terminationGracePeriodSeconds: 45
|
||||||
initContainers:
|
initContainers:
|
||||||
|
- name: sensor-agent-loader
|
||||||
|
image: {{ include "attune.image" (dict "root" . "image" .Values.images.agent) }}
|
||||||
|
imagePullPolicy: {{ .Values.images.agent.pullPolicy }}
|
||||||
|
command: ["cp", "/usr/local/bin/attune-sensor-agent", "/opt/attune/agent/attune-sensor-agent"]
|
||||||
|
volumeMounts:
|
||||||
|
- name: agent-bin
|
||||||
|
mountPath: /opt/attune/agent
|
||||||
- name: wait-for-schema
|
- name: wait-for-schema
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
command: ["/bin/sh", "-ec"]
|
command: ["/bin/sh", "-ec"]
|
||||||
@@ -333,6 +341,7 @@ spec:
|
|||||||
- name: sensor
|
- name: sensor
|
||||||
image: {{ include "attune.image" (dict "root" . "image" .Values.images.sensor) }}
|
image: {{ include "attune.image" (dict "root" . "image" .Values.images.sensor) }}
|
||||||
imagePullPolicy: {{ .Values.images.sensor.pullPolicy }}
|
imagePullPolicy: {{ .Values.images.sensor.pullPolicy }}
|
||||||
|
command: ["/opt/attune/agent/attune-sensor-agent"]
|
||||||
envFrom:
|
envFrom:
|
||||||
- secretRef:
|
- secretRef:
|
||||||
name: {{ include "attune.secretName" . }}
|
name: {{ include "attune.secretName" . }}
|
||||||
@@ -343,23 +352,33 @@ spec:
|
|||||||
value: {{ .Values.database.schema | quote }}
|
value: {{ .Values.database.schema | quote }}
|
||||||
- name: ATTUNE__WORKER__WORKER_TYPE
|
- name: ATTUNE__WORKER__WORKER_TYPE
|
||||||
value: container
|
value: container
|
||||||
|
- name: ATTUNE_SENSOR_RUNTIMES
|
||||||
|
value: {{ .Values.sensor.runtimes | quote }}
|
||||||
- name: ATTUNE_API_URL
|
- name: ATTUNE_API_URL
|
||||||
value: http://{{ include "attune.apiServiceName" . }}:{{ .Values.api.service.port }}
|
value: http://{{ include "attune.apiServiceName" . }}:{{ .Values.api.service.port }}
|
||||||
- name: ATTUNE_MQ_URL
|
- name: ATTUNE_MQ_URL
|
||||||
value: {{ include "attune.rabbitmqUrl" . | quote }}
|
value: {{ include "attune.rabbitmqUrl" . | quote }}
|
||||||
- name: ATTUNE_PACKS_BASE_DIR
|
- name: ATTUNE_PACKS_BASE_DIR
|
||||||
value: /opt/attune/packs
|
value: /opt/attune/packs
|
||||||
|
- name: RUST_LOG
|
||||||
|
value: {{ .Values.sensor.logLevel | quote }}
|
||||||
resources:
|
resources:
|
||||||
{{- toYaml .Values.sensor.resources | nindent 12 }}
|
{{- toYaml .Values.sensor.resources | nindent 12 }}
|
||||||
volumeMounts:
|
volumeMounts:
|
||||||
|
- name: agent-bin
|
||||||
|
mountPath: /opt/attune/agent
|
||||||
|
readOnly: true
|
||||||
- name: config
|
- name: config
|
||||||
mountPath: /opt/attune/config.yaml
|
mountPath: /opt/attune/config.yaml
|
||||||
subPath: config.yaml
|
subPath: config.yaml
|
||||||
- name: packs
|
- name: packs
|
||||||
mountPath: /opt/attune/packs
|
mountPath: /opt/attune/packs
|
||||||
|
readOnly: true
|
||||||
- name: runtime-envs
|
- name: runtime-envs
|
||||||
mountPath: /opt/attune/runtime_envs
|
mountPath: /opt/attune/runtime_envs
|
||||||
volumes:
|
volumes:
|
||||||
|
- name: agent-bin
|
||||||
|
emptyDir: {}
|
||||||
- name: config
|
- name: config
|
||||||
configMap:
|
configMap:
|
||||||
name: {{ include "attune.fullname" . }}-config
|
name: {{ include "attune.fullname" . }}-config
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ stringData:
|
|||||||
ATTUNE__SECURITY__ENCRYPTION_KEY: {{ .Values.security.encryptionKey | quote }}
|
ATTUNE__SECURITY__ENCRYPTION_KEY: {{ .Values.security.encryptionKey | quote }}
|
||||||
ATTUNE__DATABASE__URL: {{ include "attune.databaseUrl" . | quote }}
|
ATTUNE__DATABASE__URL: {{ include "attune.databaseUrl" . | quote }}
|
||||||
ATTUNE__MESSAGE_QUEUE__URL: {{ include "attune.rabbitmqUrl" . | quote }}
|
ATTUNE__MESSAGE_QUEUE__URL: {{ include "attune.rabbitmqUrl" . | quote }}
|
||||||
ATTUNE__CACHE__URL: {{ include "attune.redisUrl" . | quote }}
|
ATTUNE__REDIS__URL: {{ include "attune.redisUrl" . | quote }}
|
||||||
DB_HOST: {{ include "attune.postgresqlServiceName" . | quote }}
|
DB_HOST: {{ include "attune.postgresqlServiceName" . | quote }}
|
||||||
DB_PORT: {{ .Values.database.port | quote }}
|
DB_PORT: {{ .Values.database.port | quote }}
|
||||||
DB_USER: {{ .Values.database.username | quote }}
|
DB_USER: {{ .Values.database.username | quote }}
|
||||||
|
|||||||
@@ -108,8 +108,8 @@ images:
|
|||||||
tag: ""
|
tag: ""
|
||||||
pullPolicy: IfNotPresent
|
pullPolicy: IfNotPresent
|
||||||
sensor:
|
sensor:
|
||||||
repository: attune-sensor
|
repository: nikolaik/python-nodejs
|
||||||
tag: ""
|
tag: python3.12-nodejs22-slim
|
||||||
pullPolicy: IfNotPresent
|
pullPolicy: IfNotPresent
|
||||||
notifier:
|
notifier:
|
||||||
repository: attune-notifier
|
repository: attune-notifier
|
||||||
@@ -131,6 +131,10 @@ images:
|
|||||||
repository: attune-init-packs
|
repository: attune-init-packs
|
||||||
tag: ""
|
tag: ""
|
||||||
pullPolicy: IfNotPresent
|
pullPolicy: IfNotPresent
|
||||||
|
agent:
|
||||||
|
repository: attune-agent
|
||||||
|
tag: ""
|
||||||
|
pullPolicy: IfNotPresent
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
migrations:
|
migrations:
|
||||||
@@ -162,6 +166,8 @@ worker:
|
|||||||
|
|
||||||
sensor:
|
sensor:
|
||||||
replicaCount: 1
|
replicaCount: 1
|
||||||
|
runtimes: shell,python,node,native
|
||||||
|
logLevel: debug
|
||||||
resources: {}
|
resources: {}
|
||||||
|
|
||||||
notifier:
|
notifier:
|
||||||
@@ -191,3 +197,57 @@ web:
|
|||||||
- path: /
|
- path: /
|
||||||
pathType: Prefix
|
pathType: Prefix
|
||||||
tls: []
|
tls: []
|
||||||
|
|
||||||
|
# Agent-based workers
|
||||||
|
# These deploy the universal worker agent into any container image.
|
||||||
|
# The agent auto-detects available runtimes (python, ruby, node, etc.)
|
||||||
|
# and registers with the Attune platform.
|
||||||
|
#
|
||||||
|
# Each entry creates a separate Deployment with an init container that
|
||||||
|
# copies the statically-linked agent binary into the worker container.
|
||||||
|
#
|
||||||
|
# Supported fields per worker:
|
||||||
|
# name (required) - Unique name for this worker (used in resource names)
|
||||||
|
# image (required) - Container image with your desired runtime(s)
|
||||||
|
# replicas (optional) - Number of pod replicas (default: 1)
|
||||||
|
# runtimes (optional) - List of runtimes to expose; [] = auto-detect
|
||||||
|
# resources (optional) - Kubernetes resource requests/limits
|
||||||
|
# env (optional) - Extra environment variables (list of {name, value})
|
||||||
|
# imagePullPolicy (optional) - Pull policy for the worker image
|
||||||
|
# logLevel (optional) - RUST_LOG level (default: "info")
|
||||||
|
# runtimeClassName (optional) - Kubernetes RuntimeClass (e.g., "nvidia" for GPU)
|
||||||
|
# nodeSelector (optional) - Node selector map for pod scheduling
|
||||||
|
# tolerations (optional) - Tolerations list for pod scheduling
|
||||||
|
# stopGracePeriod (optional) - Termination grace period in seconds (default: 45)
|
||||||
|
#
|
||||||
|
# Examples:
|
||||||
|
# agentWorkers:
|
||||||
|
# - name: ruby
|
||||||
|
# image: ruby:3.3
|
||||||
|
# replicas: 2
|
||||||
|
# runtimes: [] # auto-detect
|
||||||
|
# resources: {}
|
||||||
|
#
|
||||||
|
# - name: python-gpu
|
||||||
|
# image: nvidia/cuda:12.3.1-runtime-ubuntu22.04
|
||||||
|
# replicas: 1
|
||||||
|
# runtimes: [python, shell]
|
||||||
|
# runtimeClassName: nvidia
|
||||||
|
# nodeSelector:
|
||||||
|
# gpu: "true"
|
||||||
|
# tolerations:
|
||||||
|
# - key: nvidia.com/gpu
|
||||||
|
# operator: Exists
|
||||||
|
# effect: NoSchedule
|
||||||
|
# resources:
|
||||||
|
# limits:
|
||||||
|
# nvidia.com/gpu: 1
|
||||||
|
#
|
||||||
|
# - name: custom
|
||||||
|
# image: my-org/my-custom-image:latest
|
||||||
|
# replicas: 1
|
||||||
|
# runtimes: []
|
||||||
|
# env:
|
||||||
|
# - name: MY_CUSTOM_VAR
|
||||||
|
# value: my-value
|
||||||
|
agentWorkers: []
|
||||||
|
|||||||
@@ -125,3 +125,8 @@ executor:
|
|||||||
scheduled_timeout: 120 # 2 minutes (faster feedback in dev)
|
scheduled_timeout: 120 # 2 minutes (faster feedback in dev)
|
||||||
timeout_check_interval: 30 # Check every 30 seconds
|
timeout_check_interval: 30 # Check every 30 seconds
|
||||||
enable_timeout_monitor: true
|
enable_timeout_monitor: true
|
||||||
|
|
||||||
|
# Agent binary distribution (optional - for local development)
|
||||||
|
# Binary is built via: make build-agent
|
||||||
|
# agent:
|
||||||
|
# binary_dir: ./target/x86_64-unknown-linux-musl/release
|
||||||
|
|||||||
@@ -62,6 +62,8 @@ pack_registry:
|
|||||||
enabled: true
|
enabled: true
|
||||||
default_registry: https://registry.attune.example.com
|
default_registry: https://registry.attune.example.com
|
||||||
cache_ttl: 300
|
cache_ttl: 300
|
||||||
|
allowed_source_hosts:
|
||||||
|
- registry.attune.example.com
|
||||||
|
|
||||||
# Test worker configuration
|
# Test worker configuration
|
||||||
# worker:
|
# worker:
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ jsonschema = { workspace = true }
|
|||||||
# HTTP client
|
# HTTP client
|
||||||
reqwest = { workspace = true }
|
reqwest = { workspace = true }
|
||||||
openidconnect = "4.0"
|
openidconnect = "4.0"
|
||||||
ldap3 = "0.12"
|
ldap3 = { version = "0.12", default-features = false, features = ["sync", "tls-rustls-ring"] }
|
||||||
url = { workspace = true }
|
url = { workspace = true }
|
||||||
|
|
||||||
# Archive/compression
|
# Archive/compression
|
||||||
@@ -89,6 +89,7 @@ hmac = "0.12"
|
|||||||
sha1 = "0.10"
|
sha1 = "0.10"
|
||||||
sha2 = { workspace = true }
|
sha2 = { workspace = true }
|
||||||
hex = "0.4"
|
hex = "0.4"
|
||||||
|
subtle = "2.6"
|
||||||
|
|
||||||
# OpenAPI/Swagger
|
# OpenAPI/Swagger
|
||||||
utoipa = { workspace = true, features = ["axum_extras"] }
|
utoipa = { workspace = true, features = ["axum_extras"] }
|
||||||
|
|||||||
@@ -3,7 +3,10 @@
|
|||||||
use attune_common::{
|
use attune_common::{
|
||||||
config::LdapConfig,
|
config::LdapConfig,
|
||||||
repositories::{
|
repositories::{
|
||||||
identity::{CreateIdentityInput, IdentityRepository, UpdateIdentityInput},
|
identity::{
|
||||||
|
CreateIdentityInput, IdentityRepository, IdentityRoleAssignmentRepository,
|
||||||
|
UpdateIdentityInput,
|
||||||
|
},
|
||||||
Create, Update,
|
Create, Update,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -63,6 +66,11 @@ pub async fn authenticate(
|
|||||||
|
|
||||||
// Upsert identity in DB and issue JWT tokens
|
// Upsert identity in DB and issue JWT tokens
|
||||||
let identity = upsert_identity(state, &claims).await?;
|
let identity = upsert_identity(state, &claims).await?;
|
||||||
|
if identity.frozen {
|
||||||
|
return Err(ApiError::Forbidden(
|
||||||
|
"Identity is frozen and cannot authenticate".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
let access_token = generate_access_token(identity.id, &identity.login, &state.jwt_config)?;
|
let access_token = generate_access_token(identity.id, &identity.login, &state.jwt_config)?;
|
||||||
let refresh_token = generate_refresh_token(identity.id, &identity.login, &state.jwt_config)?;
|
let refresh_token = generate_refresh_token(identity.id, &identity.login, &state.jwt_config)?;
|
||||||
|
|
||||||
@@ -131,7 +139,8 @@ fn conn_settings(config: &LdapConfig) -> LdapConnSettings {
|
|||||||
/// Open a new LDAP connection.
|
/// Open a new LDAP connection.
|
||||||
async fn connect(config: &LdapConfig) -> Result<Ldap, ApiError> {
|
async fn connect(config: &LdapConfig) -> Result<Ldap, ApiError> {
|
||||||
let settings = conn_settings(config);
|
let settings = conn_settings(config);
|
||||||
let (conn, ldap) = LdapConnAsync::with_settings(settings, &config.url)
|
let url = config.url.as_deref().unwrap_or_default();
|
||||||
|
let (conn, ldap) = LdapConnAsync::with_settings(settings, url)
|
||||||
.await
|
.await
|
||||||
.map_err(|err| {
|
.map_err(|err| {
|
||||||
ApiError::InternalServerError(format!("Failed to connect to LDAP server: {err}"))
|
ApiError::InternalServerError(format!("Failed to connect to LDAP server: {err}"))
|
||||||
@@ -325,7 +334,7 @@ fn extract_claims(config: &LdapConfig, entry: &SearchEntry) -> LdapUserClaims {
|
|||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
LdapUserClaims {
|
LdapUserClaims {
|
||||||
server_url: config.url.clone(),
|
server_url: config.url.clone().unwrap_or_default(),
|
||||||
dn: entry.dn.clone(),
|
dn: entry.dn.clone(),
|
||||||
login: first_attr(&config.login_attr),
|
login: first_attr(&config.login_attr),
|
||||||
email: first_attr(&config.email_attr),
|
email: first_attr(&config.email_attr),
|
||||||
@@ -351,10 +360,13 @@ async fn upsert_identity(
|
|||||||
display_name,
|
display_name,
|
||||||
password_hash: None,
|
password_hash: None,
|
||||||
attributes: Some(attributes),
|
attributes: Some(attributes),
|
||||||
|
frozen: None,
|
||||||
};
|
};
|
||||||
IdentityRepository::update(&state.db, identity.id, updated)
|
let identity = IdentityRepository::update(&state.db, identity.id, updated)
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)
|
.map_err(ApiError::from)?;
|
||||||
|
sync_roles(&state.db, identity.id, "ldap", &claims.groups).await?;
|
||||||
|
Ok(identity)
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
// Avoid login collisions
|
// Avoid login collisions
|
||||||
@@ -363,7 +375,7 @@ async fn upsert_identity(
|
|||||||
None => desired_login,
|
None => desired_login,
|
||||||
};
|
};
|
||||||
|
|
||||||
IdentityRepository::create(
|
let identity = IdentityRepository::create(
|
||||||
&state.db,
|
&state.db,
|
||||||
CreateIdentityInput {
|
CreateIdentityInput {
|
||||||
login,
|
login,
|
||||||
@@ -372,11 +384,24 @@ async fn upsert_identity(
|
|||||||
attributes,
|
attributes,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
.await
|
||||||
|
.map_err(ApiError::from)?;
|
||||||
|
sync_roles(&state.db, identity.id, "ldap", &claims.groups).await?;
|
||||||
|
Ok(identity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn sync_roles(
|
||||||
|
db: &sqlx::PgPool,
|
||||||
|
identity_id: i64,
|
||||||
|
source: &str,
|
||||||
|
roles: &[String],
|
||||||
|
) -> Result<(), ApiError> {
|
||||||
|
IdentityRoleAssignmentRepository::replace_managed_roles(db, identity_id, source, roles)
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Derive the login name from LDAP claims.
|
/// Derive the login name from LDAP claims.
|
||||||
fn derive_login(claims: &LdapUserClaims) -> String {
|
fn derive_login(claims: &LdapUserClaims) -> String {
|
||||||
|
|||||||
@@ -3,7 +3,10 @@
|
|||||||
use attune_common::{
|
use attune_common::{
|
||||||
config::OidcConfig,
|
config::OidcConfig,
|
||||||
repositories::{
|
repositories::{
|
||||||
identity::{CreateIdentityInput, IdentityRepository, UpdateIdentityInput},
|
identity::{
|
||||||
|
CreateIdentityInput, IdentityRepository, IdentityRoleAssignmentRepository,
|
||||||
|
UpdateIdentityInput,
|
||||||
|
},
|
||||||
Create, Update,
|
Create, Update,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -123,15 +126,17 @@ pub async fn build_login_redirect(
|
|||||||
.map_err(|err| {
|
.map_err(|err| {
|
||||||
ApiError::InternalServerError(format!("Failed to build OIDC HTTP client: {err}"))
|
ApiError::InternalServerError(format!("Failed to build OIDC HTTP client: {err}"))
|
||||||
})?;
|
})?;
|
||||||
let redirect_uri = RedirectUrl::new(oidc.redirect_uri.clone()).map_err(|err| {
|
let redirect_uri_str = oidc.redirect_uri.clone().unwrap_or_default();
|
||||||
|
let redirect_uri = RedirectUrl::new(redirect_uri_str).map_err(|err| {
|
||||||
ApiError::InternalServerError(format!("Invalid OIDC redirect URI: {err}"))
|
ApiError::InternalServerError(format!("Invalid OIDC redirect URI: {err}"))
|
||||||
})?;
|
})?;
|
||||||
let client_secret = oidc.client_secret.clone().ok_or_else(|| {
|
let client_secret = oidc.client_secret.clone().ok_or_else(|| {
|
||||||
ApiError::InternalServerError("OIDC client secret is missing".to_string())
|
ApiError::InternalServerError("OIDC client secret is missing".to_string())
|
||||||
})?;
|
})?;
|
||||||
|
let client_id = oidc.client_id.clone().unwrap_or_default();
|
||||||
let client = CoreClient::from_provider_metadata(
|
let client = CoreClient::from_provider_metadata(
|
||||||
discovery.metadata.clone(),
|
discovery.metadata.clone(),
|
||||||
ClientId::new(oidc.client_id.clone()),
|
ClientId::new(client_id),
|
||||||
Some(ClientSecret::new(client_secret)),
|
Some(ClientSecret::new(client_secret)),
|
||||||
)
|
)
|
||||||
.set_redirect_uri(redirect_uri);
|
.set_redirect_uri(redirect_uri);
|
||||||
@@ -235,15 +240,17 @@ pub async fn handle_callback(
|
|||||||
.map_err(|err| {
|
.map_err(|err| {
|
||||||
ApiError::InternalServerError(format!("Failed to build OIDC HTTP client: {err}"))
|
ApiError::InternalServerError(format!("Failed to build OIDC HTTP client: {err}"))
|
||||||
})?;
|
})?;
|
||||||
let redirect_uri = RedirectUrl::new(oidc.redirect_uri.clone()).map_err(|err| {
|
let redirect_uri_str = oidc.redirect_uri.clone().unwrap_or_default();
|
||||||
|
let redirect_uri = RedirectUrl::new(redirect_uri_str).map_err(|err| {
|
||||||
ApiError::InternalServerError(format!("Invalid OIDC redirect URI: {err}"))
|
ApiError::InternalServerError(format!("Invalid OIDC redirect URI: {err}"))
|
||||||
})?;
|
})?;
|
||||||
let client_secret = oidc.client_secret.clone().ok_or_else(|| {
|
let client_secret = oidc.client_secret.clone().ok_or_else(|| {
|
||||||
ApiError::InternalServerError("OIDC client secret is missing".to_string())
|
ApiError::InternalServerError("OIDC client secret is missing".to_string())
|
||||||
})?;
|
})?;
|
||||||
|
let client_id = oidc.client_id.clone().unwrap_or_default();
|
||||||
let client = CoreClient::from_provider_metadata(
|
let client = CoreClient::from_provider_metadata(
|
||||||
discovery.metadata.clone(),
|
discovery.metadata.clone(),
|
||||||
ClientId::new(oidc.client_id.clone()),
|
ClientId::new(client_id),
|
||||||
Some(ClientSecret::new(client_secret)),
|
Some(ClientSecret::new(client_secret)),
|
||||||
)
|
)
|
||||||
.set_redirect_uri(redirect_uri);
|
.set_redirect_uri(redirect_uri);
|
||||||
@@ -282,6 +289,11 @@ pub async fn handle_callback(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let identity = upsert_identity(state, &oidc_claims).await?;
|
let identity = upsert_identity(state, &oidc_claims).await?;
|
||||||
|
if identity.frozen {
|
||||||
|
return Err(ApiError::Forbidden(
|
||||||
|
"Identity is frozen and cannot authenticate".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
let access_token = generate_access_token(identity.id, &identity.login, &state.jwt_config)?;
|
let access_token = generate_access_token(identity.id, &identity.login, &state.jwt_config)?;
|
||||||
let refresh_token = generate_refresh_token(identity.id, &identity.login, &state.jwt_config)?;
|
let refresh_token = generate_refresh_token(identity.id, &identity.login, &state.jwt_config)?;
|
||||||
|
|
||||||
@@ -328,7 +340,7 @@ pub async fn build_logout_redirect(
|
|||||||
pairs.append_pair("id_token_hint", &id_token_hint);
|
pairs.append_pair("id_token_hint", &id_token_hint);
|
||||||
}
|
}
|
||||||
pairs.append_pair("post_logout_redirect_uri", &post_logout_redirect_uri);
|
pairs.append_pair("post_logout_redirect_uri", &post_logout_redirect_uri);
|
||||||
pairs.append_pair("client_id", &oidc.client_id);
|
pairs.append_pair("client_id", oidc.client_id.as_deref().unwrap_or_default());
|
||||||
}
|
}
|
||||||
String::from(url)
|
String::from(url)
|
||||||
} else {
|
} else {
|
||||||
@@ -473,7 +485,8 @@ fn oidc_config(state: &SharedState) -> Result<OidcConfig, ApiError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_discovery_document(oidc: &OidcConfig) -> Result<OidcDiscoveryDocument, ApiError> {
|
async fn fetch_discovery_document(oidc: &OidcConfig) -> Result<OidcDiscoveryDocument, ApiError> {
|
||||||
let discovery = reqwest::get(&oidc.discovery_url).await.map_err(|err| {
|
let discovery_url = oidc.discovery_url.as_deref().unwrap_or_default();
|
||||||
|
let discovery = reqwest::get(discovery_url).await.map_err(|err| {
|
||||||
ApiError::InternalServerError(format!("Failed to fetch OIDC discovery document: {err}"))
|
ApiError::InternalServerError(format!("Failed to fetch OIDC discovery document: {err}"))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
@@ -511,10 +524,13 @@ async fn upsert_identity(
|
|||||||
display_name,
|
display_name,
|
||||||
password_hash: None,
|
password_hash: None,
|
||||||
attributes: Some(attributes.clone()),
|
attributes: Some(attributes.clone()),
|
||||||
|
frozen: None,
|
||||||
};
|
};
|
||||||
IdentityRepository::update(&state.db, identity.id, updated)
|
let identity = IdentityRepository::update(&state.db, identity.id, updated)
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)
|
.map_err(ApiError::from)?;
|
||||||
|
sync_roles(&state.db, identity.id, "oidc", &oidc_claims.groups).await?;
|
||||||
|
Ok(identity)
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let login = match IdentityRepository::find_by_login(&state.db, &desired_login).await? {
|
let login = match IdentityRepository::find_by_login(&state.db, &desired_login).await? {
|
||||||
@@ -522,7 +538,7 @@ async fn upsert_identity(
|
|||||||
None => desired_login,
|
None => desired_login,
|
||||||
};
|
};
|
||||||
|
|
||||||
IdentityRepository::create(
|
let identity = IdentityRepository::create(
|
||||||
&state.db,
|
&state.db,
|
||||||
CreateIdentityInput {
|
CreateIdentityInput {
|
||||||
login,
|
login,
|
||||||
@@ -531,11 +547,24 @@ async fn upsert_identity(
|
|||||||
attributes,
|
attributes,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
.await
|
||||||
|
.map_err(ApiError::from)?;
|
||||||
|
sync_roles(&state.db, identity.id, "oidc", &oidc_claims.groups).await?;
|
||||||
|
Ok(identity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn sync_roles(
|
||||||
|
db: &sqlx::PgPool,
|
||||||
|
identity_id: i64,
|
||||||
|
source: &str,
|
||||||
|
roles: &[String],
|
||||||
|
) -> Result<(), ApiError> {
|
||||||
|
IdentityRoleAssignmentRepository::replace_managed_roles(db, identity_id, source, roles)
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn derive_login(oidc_claims: &OidcIdentityClaims) -> String {
|
fn derive_login(oidc_claims: &OidcIdentityClaims) -> String {
|
||||||
oidc_claims
|
oidc_claims
|
||||||
@@ -597,7 +626,7 @@ async fn verify_id_token(
|
|||||||
let issuer = discovery.metadata.issuer().to_string();
|
let issuer = discovery.metadata.issuer().to_string();
|
||||||
let mut validation = Validation::new(algorithm);
|
let mut validation = Validation::new(algorithm);
|
||||||
validation.set_issuer(&[issuer.as_str()]);
|
validation.set_issuer(&[issuer.as_str()]);
|
||||||
validation.set_audience(&[oidc.client_id.as_str()]);
|
validation.set_audience(&[oidc.client_id.as_deref().unwrap_or_default()]);
|
||||||
validation.set_required_spec_claims(&["exp", "iat", "iss", "sub", "aud"]);
|
validation.set_required_spec_claims(&["exp", "iat", "iss", "sub", "aud"]);
|
||||||
validation.validate_nbf = false;
|
validation.validate_nbf = false;
|
||||||
|
|
||||||
@@ -716,7 +745,8 @@ fn should_use_secure_cookies(state: &SharedState) -> bool {
|
|||||||
.security
|
.security
|
||||||
.oidc
|
.oidc
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|oidc| oidc.redirect_uri.starts_with("https://"))
|
.and_then(|oidc| oidc.redirect_uri.as_deref())
|
||||||
|
.map(|uri| uri.starts_with("https://"))
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use crate::{
|
|||||||
use attune_common::{
|
use attune_common::{
|
||||||
rbac::{Action, AuthorizationContext, Grant, Resource},
|
rbac::{Action, AuthorizationContext, Grant, Resource},
|
||||||
repositories::{
|
repositories::{
|
||||||
identity::{IdentityRepository, PermissionSetRepository},
|
identity::{IdentityRepository, IdentityRoleAssignmentRepository, PermissionSetRepository},
|
||||||
FindById,
|
FindById,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -95,8 +95,16 @@ impl AuthorizationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn load_effective_grants(&self, identity_id: i64) -> Result<Vec<Grant>, ApiError> {
|
async fn load_effective_grants(&self, identity_id: i64) -> Result<Vec<Grant>, ApiError> {
|
||||||
let permission_sets =
|
let mut permission_sets =
|
||||||
PermissionSetRepository::find_by_identity(&self.db, identity_id).await?;
|
PermissionSetRepository::find_by_identity(&self.db, identity_id).await?;
|
||||||
|
let roles =
|
||||||
|
IdentityRoleAssignmentRepository::find_role_names_by_identity(&self.db, identity_id)
|
||||||
|
.await?;
|
||||||
|
let role_permission_sets = PermissionSetRepository::find_by_roles(&self.db, &roles).await?;
|
||||||
|
permission_sets.extend(role_permission_sets);
|
||||||
|
|
||||||
|
let mut seen_permission_sets = std::collections::HashSet::new();
|
||||||
|
permission_sets.retain(|permission_set| seen_permission_sets.insert(permission_set.id));
|
||||||
|
|
||||||
let mut grants = Vec::new();
|
let mut grants = Vec::new();
|
||||||
for permission_set in permission_sets {
|
for permission_set in permission_sets {
|
||||||
@@ -126,10 +134,6 @@ fn resource_name(resource: Resource) -> &'static str {
|
|||||||
Resource::Inquiries => "inquiries",
|
Resource::Inquiries => "inquiries",
|
||||||
Resource::Keys => "keys",
|
Resource::Keys => "keys",
|
||||||
Resource::Artifacts => "artifacts",
|
Resource::Artifacts => "artifacts",
|
||||||
Resource::Workflows => "workflows",
|
|
||||||
Resource::Webhooks => "webhooks",
|
|
||||||
Resource::Analytics => "analytics",
|
|
||||||
Resource::History => "history",
|
|
||||||
Resource::Identities => "identities",
|
Resource::Identities => "identities",
|
||||||
Resource::Permissions => "permissions",
|
Resource::Permissions => "permissions",
|
||||||
}
|
}
|
||||||
@@ -145,5 +149,6 @@ fn action_name(action: Action) -> &'static str {
|
|||||||
Action::Cancel => "cancel",
|
Action::Cancel => "cancel",
|
||||||
Action::Respond => "respond",
|
Action::Respond => "respond",
|
||||||
Action::Manage => "manage",
|
Action::Manage => "manage",
|
||||||
|
Action::Decrypt => "decrypt",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,9 +25,8 @@ pub struct CreateActionRequest {
|
|||||||
pub label: String,
|
pub label: String,
|
||||||
|
|
||||||
/// Action description
|
/// Action description
|
||||||
#[validate(length(min = 1))]
|
|
||||||
#[schema(example = "Posts a message to a Slack channel")]
|
#[schema(example = "Posts a message to a Slack channel")]
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
|
|
||||||
/// Entry point for action execution (e.g., path to script, function name)
|
/// Entry point for action execution (e.g., path to script, function name)
|
||||||
#[validate(length(min = 1, max = 1024))]
|
#[validate(length(min = 1, max = 1024))]
|
||||||
@@ -63,7 +62,6 @@ pub struct UpdateActionRequest {
|
|||||||
pub label: Option<String>,
|
pub label: Option<String>,
|
||||||
|
|
||||||
/// Action description
|
/// Action description
|
||||||
#[validate(length(min = 1))]
|
|
||||||
#[schema(example = "Posts a message to a Slack channel with enhanced features")]
|
#[schema(example = "Posts a message to a Slack channel with enhanced features")]
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
|
|
||||||
@@ -121,7 +119,7 @@ pub struct ActionResponse {
|
|||||||
|
|
||||||
/// Action description
|
/// Action description
|
||||||
#[schema(example = "Posts a message to a Slack channel")]
|
#[schema(example = "Posts a message to a Slack channel")]
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
|
|
||||||
/// Entry point
|
/// Entry point
|
||||||
#[schema(example = "/actions/slack/post_message.py")]
|
#[schema(example = "/actions/slack/post_message.py")]
|
||||||
@@ -183,7 +181,7 @@ pub struct ActionSummary {
|
|||||||
|
|
||||||
/// Action description
|
/// Action description
|
||||||
#[schema(example = "Posts a message to a Slack channel")]
|
#[schema(example = "Posts a message to a Slack channel")]
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
|
|
||||||
/// Entry point
|
/// Entry point
|
||||||
#[schema(example = "/actions/slack/post_message.py")]
|
#[schema(example = "/actions/slack/post_message.py")]
|
||||||
@@ -321,7 +319,7 @@ mod tests {
|
|||||||
r#ref: "".to_string(), // Invalid: empty
|
r#ref: "".to_string(), // Invalid: empty
|
||||||
pack_ref: "test-pack".to_string(),
|
pack_ref: "test-pack".to_string(),
|
||||||
label: "Test Action".to_string(),
|
label: "Test Action".to_string(),
|
||||||
description: "Test description".to_string(),
|
description: Some("Test description".to_string()),
|
||||||
entrypoint: "/actions/test.py".to_string(),
|
entrypoint: "/actions/test.py".to_string(),
|
||||||
runtime: None,
|
runtime: None,
|
||||||
runtime_version_constraint: None,
|
runtime_version_constraint: None,
|
||||||
@@ -338,7 +336,7 @@ mod tests {
|
|||||||
r#ref: "test.action".to_string(),
|
r#ref: "test.action".to_string(),
|
||||||
pack_ref: "test-pack".to_string(),
|
pack_ref: "test-pack".to_string(),
|
||||||
label: "Test Action".to_string(),
|
label: "Test Action".to_string(),
|
||||||
description: "Test description".to_string(),
|
description: Some("Test description".to_string()),
|
||||||
entrypoint: "/actions/test.py".to_string(),
|
entrypoint: "/actions/test.py".to_string(),
|
||||||
runtime: None,
|
runtime: None,
|
||||||
runtime_version_constraint: None,
|
runtime_version_constraint: None,
|
||||||
|
|||||||
@@ -51,9 +51,10 @@ pub use inquiry::{
|
|||||||
pub use key::{CreateKeyRequest, KeyQueryParams, KeyResponse, KeySummary, UpdateKeyRequest};
|
pub use key::{CreateKeyRequest, KeyQueryParams, KeyResponse, KeySummary, UpdateKeyRequest};
|
||||||
pub use pack::{CreatePackRequest, PackResponse, PackSummary, UpdatePackRequest};
|
pub use pack::{CreatePackRequest, PackResponse, PackSummary, UpdatePackRequest};
|
||||||
pub use permission::{
|
pub use permission::{
|
||||||
CreateIdentityRequest, CreatePermissionAssignmentRequest, IdentityResponse, IdentitySummary,
|
CreateIdentityRequest, CreateIdentityRoleAssignmentRequest, CreatePermissionAssignmentRequest,
|
||||||
PermissionAssignmentResponse, PermissionSetQueryParams, PermissionSetSummary,
|
CreatePermissionSetRoleAssignmentRequest, IdentityResponse, IdentityRoleAssignmentResponse,
|
||||||
UpdateIdentityRequest,
|
IdentitySummary, PermissionAssignmentResponse, PermissionSetQueryParams,
|
||||||
|
PermissionSetRoleAssignmentResponse, PermissionSetSummary, UpdateIdentityRequest,
|
||||||
};
|
};
|
||||||
pub use rule::{CreateRuleRequest, RuleResponse, RuleSummary, UpdateRuleRequest};
|
pub use rule::{CreateRuleRequest, RuleResponse, RuleSummary, UpdateRuleRequest};
|
||||||
pub use runtime::{CreateRuntimeRequest, RuntimeResponse, RuntimeSummary, UpdateRuntimeRequest};
|
pub use runtime::{CreateRuntimeRequest, RuntimeResponse, RuntimeSummary, UpdateRuntimeRequest};
|
||||||
|
|||||||
@@ -14,10 +14,32 @@ pub struct IdentitySummary {
|
|||||||
pub id: i64,
|
pub id: i64,
|
||||||
pub login: String,
|
pub login: String,
|
||||||
pub display_name: Option<String>,
|
pub display_name: Option<String>,
|
||||||
|
pub frozen: bool,
|
||||||
pub attributes: JsonValue,
|
pub attributes: JsonValue,
|
||||||
|
pub roles: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type IdentityResponse = IdentitySummary;
|
#[derive(Debug, Clone, Serialize, ToSchema)]
|
||||||
|
pub struct IdentityRoleAssignmentResponse {
|
||||||
|
pub id: i64,
|
||||||
|
pub identity_id: i64,
|
||||||
|
pub role: String,
|
||||||
|
pub source: String,
|
||||||
|
pub managed: bool,
|
||||||
|
pub created: chrono::DateTime<chrono::Utc>,
|
||||||
|
pub updated: chrono::DateTime<chrono::Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, ToSchema)]
|
||||||
|
pub struct IdentityResponse {
|
||||||
|
pub id: i64,
|
||||||
|
pub login: String,
|
||||||
|
pub display_name: Option<String>,
|
||||||
|
pub frozen: bool,
|
||||||
|
pub attributes: JsonValue,
|
||||||
|
pub roles: Vec<IdentityRoleAssignmentResponse>,
|
||||||
|
pub direct_permissions: Vec<PermissionAssignmentResponse>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, ToSchema)]
|
#[derive(Debug, Clone, Serialize, ToSchema)]
|
||||||
pub struct PermissionSetSummary {
|
pub struct PermissionSetSummary {
|
||||||
@@ -27,6 +49,7 @@ pub struct PermissionSetSummary {
|
|||||||
pub label: Option<String>,
|
pub label: Option<String>,
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
pub grants: JsonValue,
|
pub grants: JsonValue,
|
||||||
|
pub roles: Vec<PermissionSetRoleAssignmentResponse>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, ToSchema)]
|
#[derive(Debug, Clone, Serialize, ToSchema)]
|
||||||
@@ -38,6 +61,15 @@ pub struct PermissionAssignmentResponse {
|
|||||||
pub created: chrono::DateTime<chrono::Utc>,
|
pub created: chrono::DateTime<chrono::Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, ToSchema)]
|
||||||
|
pub struct PermissionSetRoleAssignmentResponse {
|
||||||
|
pub id: i64,
|
||||||
|
pub permission_set_id: i64,
|
||||||
|
pub permission_set_ref: Option<String>,
|
||||||
|
pub role: String,
|
||||||
|
pub created: chrono::DateTime<chrono::Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, ToSchema)]
|
#[derive(Debug, Clone, Deserialize, ToSchema)]
|
||||||
pub struct CreatePermissionAssignmentRequest {
|
pub struct CreatePermissionAssignmentRequest {
|
||||||
pub identity_id: Option<i64>,
|
pub identity_id: Option<i64>,
|
||||||
@@ -45,6 +77,18 @@ pub struct CreatePermissionAssignmentRequest {
|
|||||||
pub permission_set_ref: String,
|
pub permission_set_ref: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Validate, ToSchema)]
|
||||||
|
pub struct CreateIdentityRoleAssignmentRequest {
|
||||||
|
#[validate(length(min = 1, max = 255))]
|
||||||
|
pub role: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Validate, ToSchema)]
|
||||||
|
pub struct CreatePermissionSetRoleAssignmentRequest {
|
||||||
|
#[validate(length(min = 1, max = 255))]
|
||||||
|
pub role: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Validate, ToSchema)]
|
#[derive(Debug, Clone, Deserialize, Validate, ToSchema)]
|
||||||
pub struct CreateIdentityRequest {
|
pub struct CreateIdentityRequest {
|
||||||
#[validate(length(min = 3, max = 255))]
|
#[validate(length(min = 3, max = 255))]
|
||||||
@@ -62,4 +106,5 @@ pub struct UpdateIdentityRequest {
|
|||||||
pub display_name: Option<String>,
|
pub display_name: Option<String>,
|
||||||
pub password: Option<String>,
|
pub password: Option<String>,
|
||||||
pub attributes: Option<JsonValue>,
|
pub attributes: Option<JsonValue>,
|
||||||
|
pub frozen: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,9 +25,8 @@ pub struct CreateRuleRequest {
|
|||||||
pub label: String,
|
pub label: String,
|
||||||
|
|
||||||
/// Rule description
|
/// Rule description
|
||||||
#[validate(length(min = 1))]
|
|
||||||
#[schema(example = "Send Slack notification when an error occurs")]
|
#[schema(example = "Send Slack notification when an error occurs")]
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
|
|
||||||
/// Action reference to execute when rule matches
|
/// Action reference to execute when rule matches
|
||||||
#[validate(length(min = 1, max = 255))]
|
#[validate(length(min = 1, max = 255))]
|
||||||
@@ -69,7 +68,6 @@ pub struct UpdateRuleRequest {
|
|||||||
pub label: Option<String>,
|
pub label: Option<String>,
|
||||||
|
|
||||||
/// Rule description
|
/// Rule description
|
||||||
#[validate(length(min = 1))]
|
|
||||||
#[schema(example = "Enhanced error notification with filtering")]
|
#[schema(example = "Enhanced error notification with filtering")]
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
|
|
||||||
@@ -115,7 +113,7 @@ pub struct RuleResponse {
|
|||||||
|
|
||||||
/// Rule description
|
/// Rule description
|
||||||
#[schema(example = "Send Slack notification when an error occurs")]
|
#[schema(example = "Send Slack notification when an error occurs")]
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
|
|
||||||
/// Action ID (null if the referenced action has been deleted)
|
/// Action ID (null if the referenced action has been deleted)
|
||||||
#[schema(example = 1)]
|
#[schema(example = 1)]
|
||||||
@@ -183,7 +181,7 @@ pub struct RuleSummary {
|
|||||||
|
|
||||||
/// Rule description
|
/// Rule description
|
||||||
#[schema(example = "Send Slack notification when an error occurs")]
|
#[schema(example = "Send Slack notification when an error occurs")]
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
|
|
||||||
/// Action reference
|
/// Action reference
|
||||||
#[schema(example = "slack.post_message")]
|
#[schema(example = "slack.post_message")]
|
||||||
@@ -297,7 +295,7 @@ mod tests {
|
|||||||
r#ref: "".to_string(), // Invalid: empty
|
r#ref: "".to_string(), // Invalid: empty
|
||||||
pack_ref: "test-pack".to_string(),
|
pack_ref: "test-pack".to_string(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test description".to_string(),
|
description: Some("Test description".to_string()),
|
||||||
action_ref: "test.action".to_string(),
|
action_ref: "test.action".to_string(),
|
||||||
trigger_ref: "test.trigger".to_string(),
|
trigger_ref: "test.trigger".to_string(),
|
||||||
conditions: default_empty_object(),
|
conditions: default_empty_object(),
|
||||||
@@ -315,7 +313,7 @@ mod tests {
|
|||||||
r#ref: "test.rule".to_string(),
|
r#ref: "test.rule".to_string(),
|
||||||
pack_ref: "test-pack".to_string(),
|
pack_ref: "test-pack".to_string(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test description".to_string(),
|
description: Some("Test description".to_string()),
|
||||||
action_ref: "test.action".to_string(),
|
action_ref: "test.action".to_string(),
|
||||||
trigger_ref: "test.trigger".to_string(),
|
trigger_ref: "test.trigger".to_string(),
|
||||||
conditions: serde_json::json!({
|
conditions: serde_json::json!({
|
||||||
|
|||||||
@@ -203,9 +203,8 @@ pub struct CreateSensorRequest {
|
|||||||
pub label: String,
|
pub label: String,
|
||||||
|
|
||||||
/// Sensor description
|
/// Sensor description
|
||||||
#[validate(length(min = 1))]
|
|
||||||
#[schema(example = "Monitors CPU usage and generates events")]
|
#[schema(example = "Monitors CPU usage and generates events")]
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
|
|
||||||
/// Entry point for sensor execution (e.g., path to script, function name)
|
/// Entry point for sensor execution (e.g., path to script, function name)
|
||||||
#[validate(length(min = 1, max = 1024))]
|
#[validate(length(min = 1, max = 1024))]
|
||||||
@@ -247,7 +246,6 @@ pub struct UpdateSensorRequest {
|
|||||||
pub label: Option<String>,
|
pub label: Option<String>,
|
||||||
|
|
||||||
/// Sensor description
|
/// Sensor description
|
||||||
#[validate(length(min = 1))]
|
|
||||||
#[schema(example = "Enhanced CPU monitoring with alerts")]
|
#[schema(example = "Enhanced CPU monitoring with alerts")]
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
|
|
||||||
@@ -297,7 +295,7 @@ pub struct SensorResponse {
|
|||||||
|
|
||||||
/// Sensor description
|
/// Sensor description
|
||||||
#[schema(example = "Monitors CPU usage and generates events")]
|
#[schema(example = "Monitors CPU usage and generates events")]
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
|
|
||||||
/// Entry point
|
/// Entry point
|
||||||
#[schema(example = "/sensors/monitoring/cpu_monitor.py")]
|
#[schema(example = "/sensors/monitoring/cpu_monitor.py")]
|
||||||
@@ -357,7 +355,7 @@ pub struct SensorSummary {
|
|||||||
|
|
||||||
/// Sensor description
|
/// Sensor description
|
||||||
#[schema(example = "Monitors CPU usage and generates events")]
|
#[schema(example = "Monitors CPU usage and generates events")]
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
|
|
||||||
/// Trigger reference
|
/// Trigger reference
|
||||||
#[schema(example = "monitoring.cpu_threshold")]
|
#[schema(example = "monitoring.cpu_threshold")]
|
||||||
@@ -499,7 +497,7 @@ mod tests {
|
|||||||
r#ref: "test.sensor".to_string(),
|
r#ref: "test.sensor".to_string(),
|
||||||
pack_ref: "test-pack".to_string(),
|
pack_ref: "test-pack".to_string(),
|
||||||
label: "Test Sensor".to_string(),
|
label: "Test Sensor".to_string(),
|
||||||
description: "Test description".to_string(),
|
description: Some("Test description".to_string()),
|
||||||
entrypoint: "/sensors/test.py".to_string(),
|
entrypoint: "/sensors/test.py".to_string(),
|
||||||
runtime_ref: "python3".to_string(),
|
runtime_ref: "python3".to_string(),
|
||||||
trigger_ref: "test.trigger".to_string(),
|
trigger_ref: "test.trigger".to_string(),
|
||||||
|
|||||||
@@ -27,8 +27,11 @@ use crate::dto::{
|
|||||||
UpdatePackRequest, WorkflowSyncResult,
|
UpdatePackRequest, WorkflowSyncResult,
|
||||||
},
|
},
|
||||||
permission::{
|
permission::{
|
||||||
CreateIdentityRequest, CreatePermissionAssignmentRequest, IdentityResponse,
|
CreateIdentityRequest, CreateIdentityRoleAssignmentRequest,
|
||||||
IdentitySummary, PermissionAssignmentResponse, PermissionSetSummary, UpdateIdentityRequest,
|
CreatePermissionAssignmentRequest, CreatePermissionSetRoleAssignmentRequest,
|
||||||
|
IdentityResponse, IdentityRoleAssignmentResponse, IdentitySummary,
|
||||||
|
PermissionAssignmentResponse, PermissionSetRoleAssignmentResponse, PermissionSetSummary,
|
||||||
|
UpdateIdentityRequest,
|
||||||
},
|
},
|
||||||
rule::{CreateRuleRequest, RuleResponse, RuleSummary, UpdateRuleRequest},
|
rule::{CreateRuleRequest, RuleResponse, RuleSummary, UpdateRuleRequest},
|
||||||
runtime::{CreateRuntimeRequest, RuntimeResponse, RuntimeSummary, UpdateRuntimeRequest},
|
runtime::{CreateRuntimeRequest, RuntimeResponse, RuntimeSummary, UpdateRuntimeRequest},
|
||||||
@@ -185,6 +188,12 @@ use crate::dto::{
|
|||||||
crate::routes::permissions::list_identity_permissions,
|
crate::routes::permissions::list_identity_permissions,
|
||||||
crate::routes::permissions::create_permission_assignment,
|
crate::routes::permissions::create_permission_assignment,
|
||||||
crate::routes::permissions::delete_permission_assignment,
|
crate::routes::permissions::delete_permission_assignment,
|
||||||
|
crate::routes::permissions::create_identity_role_assignment,
|
||||||
|
crate::routes::permissions::delete_identity_role_assignment,
|
||||||
|
crate::routes::permissions::create_permission_set_role_assignment,
|
||||||
|
crate::routes::permissions::delete_permission_set_role_assignment,
|
||||||
|
crate::routes::permissions::freeze_identity,
|
||||||
|
crate::routes::permissions::unfreeze_identity,
|
||||||
|
|
||||||
// Workflows
|
// Workflows
|
||||||
crate::routes::workflows::list_workflows,
|
crate::routes::workflows::list_workflows,
|
||||||
@@ -199,6 +208,10 @@ use crate::dto::{
|
|||||||
crate::routes::webhooks::disable_webhook,
|
crate::routes::webhooks::disable_webhook,
|
||||||
crate::routes::webhooks::regenerate_webhook_key,
|
crate::routes::webhooks::regenerate_webhook_key,
|
||||||
crate::routes::webhooks::receive_webhook,
|
crate::routes::webhooks::receive_webhook,
|
||||||
|
|
||||||
|
// Agent
|
||||||
|
crate::routes::agent::download_agent_binary,
|
||||||
|
crate::routes::agent::agent_info,
|
||||||
),
|
),
|
||||||
components(
|
components(
|
||||||
schemas(
|
schemas(
|
||||||
@@ -273,6 +286,10 @@ use crate::dto::{
|
|||||||
PermissionSetSummary,
|
PermissionSetSummary,
|
||||||
PermissionAssignmentResponse,
|
PermissionAssignmentResponse,
|
||||||
CreatePermissionAssignmentRequest,
|
CreatePermissionAssignmentRequest,
|
||||||
|
CreateIdentityRoleAssignmentRequest,
|
||||||
|
IdentityRoleAssignmentResponse,
|
||||||
|
CreatePermissionSetRoleAssignmentRequest,
|
||||||
|
PermissionSetRoleAssignmentResponse,
|
||||||
|
|
||||||
// Runtime DTOs
|
// Runtime DTOs
|
||||||
CreateRuntimeRequest,
|
CreateRuntimeRequest,
|
||||||
@@ -341,6 +358,10 @@ use crate::dto::{
|
|||||||
WebhookReceiverRequest,
|
WebhookReceiverRequest,
|
||||||
WebhookReceiverResponse,
|
WebhookReceiverResponse,
|
||||||
ApiResponse<WebhookReceiverResponse>,
|
ApiResponse<WebhookReceiverResponse>,
|
||||||
|
|
||||||
|
// Agent DTOs
|
||||||
|
crate::routes::agent::AgentBinaryInfo,
|
||||||
|
crate::routes::agent::AgentArchInfo,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
modifiers(&SecurityAddon),
|
modifiers(&SecurityAddon),
|
||||||
@@ -359,6 +380,7 @@ use crate::dto::{
|
|||||||
(name = "secrets", description = "Secret management endpoints"),
|
(name = "secrets", description = "Secret management endpoints"),
|
||||||
(name = "workflows", description = "Workflow management endpoints"),
|
(name = "workflows", description = "Workflow management endpoints"),
|
||||||
(name = "webhooks", description = "Webhook management and receiver endpoints"),
|
(name = "webhooks", description = "Webhook management and receiver endpoints"),
|
||||||
|
(name = "agent", description = "Agent binary distribution endpoints"),
|
||||||
)
|
)
|
||||||
)]
|
)]
|
||||||
pub struct ApiDoc;
|
pub struct ApiDoc;
|
||||||
@@ -441,14 +463,14 @@ mod tests {
|
|||||||
// We have 57 unique paths with 81 total operations (HTTP methods)
|
// We have 57 unique paths with 81 total operations (HTTP methods)
|
||||||
// This test ensures we don't accidentally remove endpoints
|
// This test ensures we don't accidentally remove endpoints
|
||||||
assert!(
|
assert!(
|
||||||
path_count >= 57,
|
path_count >= 59,
|
||||||
"Expected at least 57 unique API paths, found {}",
|
"Expected at least 59 unique API paths, found {}",
|
||||||
path_count
|
path_count
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
operation_count >= 81,
|
operation_count >= 83,
|
||||||
"Expected at least 81 API operations, found {}",
|
"Expected at least 83 API operations, found {}",
|
||||||
operation_count
|
operation_count
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -277,7 +277,7 @@ pub async fn update_action(
|
|||||||
// Create update input
|
// Create update input
|
||||||
let update_input = UpdateActionInput {
|
let update_input = UpdateActionInput {
|
||||||
label: request.label,
|
label: request.label,
|
||||||
description: request.description,
|
description: request.description.map(Patch::Set),
|
||||||
entrypoint: request.entrypoint,
|
entrypoint: request.entrypoint,
|
||||||
runtime: request.runtime,
|
runtime: request.runtime,
|
||||||
runtime_version_constraint: request.runtime_version_constraint.map(|patch| match patch {
|
runtime_version_constraint: request.runtime_version_constraint.map(|patch| match patch {
|
||||||
|
|||||||
482
crates/api/src/routes/agent.rs
Normal file
482
crates/api/src/routes/agent.rs
Normal file
@@ -0,0 +1,482 @@
|
|||||||
|
//! Agent binary download endpoints
|
||||||
|
//!
|
||||||
|
//! Provides endpoints for downloading the attune-agent binary for injection
|
||||||
|
//! into arbitrary containers. This supports deployments where shared Docker
|
||||||
|
//! volumes are impractical (Kubernetes, ECS, remote Docker hosts).
|
||||||
|
|
||||||
|
use axum::{
|
||||||
|
body::Body,
|
||||||
|
extract::{Query, State},
|
||||||
|
http::{header, HeaderMap, StatusCode},
|
||||||
|
response::IntoResponse,
|
||||||
|
routing::get,
|
||||||
|
Json, Router,
|
||||||
|
};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use subtle::ConstantTimeEq;
|
||||||
|
use tokio::fs;
|
||||||
|
use tokio_util::io::ReaderStream;
|
||||||
|
use utoipa::{IntoParams, ToSchema};
|
||||||
|
|
||||||
|
use crate::state::AppState;
|
||||||
|
|
||||||
|
/// Query parameters for the binary download endpoint
|
||||||
|
#[derive(Debug, Deserialize, IntoParams)]
|
||||||
|
pub struct BinaryDownloadParams {
|
||||||
|
/// Target architecture (x86_64, aarch64). Defaults to x86_64.
|
||||||
|
#[param(example = "x86_64")]
|
||||||
|
pub arch: Option<String>,
|
||||||
|
/// Optional bootstrap token for authentication
|
||||||
|
pub token: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Agent binary metadata
|
||||||
|
#[derive(Debug, Serialize, ToSchema)]
|
||||||
|
pub struct AgentBinaryInfo {
|
||||||
|
/// Available architectures
|
||||||
|
pub architectures: Vec<AgentArchInfo>,
|
||||||
|
/// Agent version (from build)
|
||||||
|
pub version: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Per-architecture binary info
|
||||||
|
#[derive(Debug, Serialize, ToSchema)]
|
||||||
|
pub struct AgentArchInfo {
|
||||||
|
/// Architecture name
|
||||||
|
pub arch: String,
|
||||||
|
/// Binary size in bytes
|
||||||
|
pub size_bytes: u64,
|
||||||
|
/// Whether this binary is available
|
||||||
|
pub available: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validate that the architecture name is safe (no path traversal) and normalize it.
|
||||||
|
fn validate_arch(arch: &str) -> Result<&str, (StatusCode, Json<serde_json::Value>)> {
|
||||||
|
match arch {
|
||||||
|
"x86_64" | "aarch64" => Ok(arch),
|
||||||
|
// Accept arm64 as an alias for aarch64
|
||||||
|
"arm64" => Ok("aarch64"),
|
||||||
|
_ => Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(serde_json::json!({
|
||||||
|
"error": "Invalid architecture",
|
||||||
|
"message": format!("Unsupported architecture '{}'. Supported: x86_64, aarch64", arch),
|
||||||
|
})),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validate bootstrap token if configured.
|
||||||
|
///
|
||||||
|
/// If the agent config has a `bootstrap_token` set, the request must provide it
|
||||||
|
/// via the `X-Agent-Token` header or the `token` query parameter. If no token
|
||||||
|
/// is configured, access is unrestricted.
|
||||||
|
fn validate_token(
|
||||||
|
config: &attune_common::config::Config,
|
||||||
|
headers: &HeaderMap,
|
||||||
|
query_token: &Option<String>,
|
||||||
|
) -> Result<(), (StatusCode, Json<serde_json::Value>)> {
|
||||||
|
let expected_token = config
|
||||||
|
.agent
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|ac| ac.bootstrap_token.as_ref());
|
||||||
|
|
||||||
|
let expected_token = match expected_token {
|
||||||
|
Some(t) => t,
|
||||||
|
None => {
|
||||||
|
use std::sync::Once;
|
||||||
|
static WARN_ONCE: Once = Once::new();
|
||||||
|
WARN_ONCE.call_once(|| {
|
||||||
|
tracing::warn!(
|
||||||
|
"Agent binary download endpoint has no bootstrap_token configured. \
|
||||||
|
Anyone with network access to the API can download the agent binary. \
|
||||||
|
Set agent.bootstrap_token in config to restrict access."
|
||||||
|
);
|
||||||
|
});
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check X-Agent-Token header first, then query param
|
||||||
|
let provided_token = headers
|
||||||
|
.get("x-agent-token")
|
||||||
|
.and_then(|v| v.to_str().ok())
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
.or_else(|| query_token.clone());
|
||||||
|
|
||||||
|
match provided_token {
|
||||||
|
Some(ref t) if bool::from(t.as_bytes().ct_eq(expected_token.as_bytes())) => Ok(()),
|
||||||
|
Some(_) => Err((
|
||||||
|
StatusCode::UNAUTHORIZED,
|
||||||
|
Json(serde_json::json!({
|
||||||
|
"error": "Invalid token",
|
||||||
|
"message": "The provided bootstrap token is invalid",
|
||||||
|
})),
|
||||||
|
)),
|
||||||
|
None => Err((
|
||||||
|
StatusCode::UNAUTHORIZED,
|
||||||
|
Json(serde_json::json!({
|
||||||
|
"error": "Token required",
|
||||||
|
"message": "A bootstrap token is required. Provide via X-Agent-Token header or token query parameter.",
|
||||||
|
})),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Download the agent binary
|
||||||
|
///
|
||||||
|
/// Returns the statically-linked attune-agent binary for the requested architecture.
|
||||||
|
/// The binary can be injected into any container to turn it into an Attune worker.
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/api/v1/agent/binary",
|
||||||
|
params(BinaryDownloadParams),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Agent binary", content_type = "application/octet-stream"),
|
||||||
|
(status = 400, description = "Invalid architecture"),
|
||||||
|
(status = 401, description = "Invalid or missing bootstrap token"),
|
||||||
|
(status = 404, description = "Agent binary not found"),
|
||||||
|
(status = 503, description = "Agent binary distribution not configured"),
|
||||||
|
),
|
||||||
|
tag = "agent"
|
||||||
|
)]
|
||||||
|
pub async fn download_agent_binary(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
|
headers: HeaderMap,
|
||||||
|
Query(params): Query<BinaryDownloadParams>,
|
||||||
|
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
|
||||||
|
// Validate bootstrap token if configured
|
||||||
|
validate_token(&state.config, &headers, ¶ms.token)?;
|
||||||
|
|
||||||
|
let agent_config = state.config.agent.as_ref().ok_or_else(|| {
|
||||||
|
(
|
||||||
|
StatusCode::SERVICE_UNAVAILABLE,
|
||||||
|
Json(serde_json::json!({
|
||||||
|
"error": "Not configured",
|
||||||
|
"message": "Agent binary distribution is not configured. Set agent.binary_dir in config.",
|
||||||
|
})),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let arch = params.arch.as_deref().unwrap_or("x86_64");
|
||||||
|
let arch = validate_arch(arch)?;
|
||||||
|
|
||||||
|
let binary_dir = std::path::Path::new(&agent_config.binary_dir);
|
||||||
|
|
||||||
|
// Try arch-specific binary first, then fall back to generic name.
|
||||||
|
// IMPORTANT: The generic `attune-agent` binary is only safe to serve for
|
||||||
|
// x86_64 requests, because the current build pipeline produces an
|
||||||
|
// x86_64-unknown-linux-musl binary. Serving it for aarch64/arm64 would
|
||||||
|
// give the caller an incompatible executable (exec format error).
|
||||||
|
let arch_specific = binary_dir.join(format!("attune-agent-{}", arch));
|
||||||
|
let generic = binary_dir.join("attune-agent");
|
||||||
|
|
||||||
|
let binary_path = if arch_specific.exists() {
|
||||||
|
arch_specific
|
||||||
|
} else if arch == "x86_64" && generic.exists() {
|
||||||
|
tracing::debug!(
|
||||||
|
"Arch-specific binary not found at {:?}, falling back to generic {:?} (safe for x86_64)",
|
||||||
|
arch_specific,
|
||||||
|
generic
|
||||||
|
);
|
||||||
|
generic
|
||||||
|
} else {
|
||||||
|
tracing::warn!(
|
||||||
|
"Agent binary not found. Checked: {:?} and {:?}",
|
||||||
|
arch_specific,
|
||||||
|
generic
|
||||||
|
);
|
||||||
|
return Err((
|
||||||
|
StatusCode::NOT_FOUND,
|
||||||
|
Json(serde_json::json!({
|
||||||
|
"error": "Not found",
|
||||||
|
"message": format!(
|
||||||
|
"Agent binary not found for architecture '{}'. Ensure the agent binary is built and placed in '{}'.",
|
||||||
|
arch,
|
||||||
|
agent_config.binary_dir
|
||||||
|
),
|
||||||
|
})),
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get file metadata for Content-Length
|
||||||
|
let metadata = fs::metadata(&binary_path).await.map_err(|e| {
|
||||||
|
tracing::error!(
|
||||||
|
"Failed to read agent binary metadata at {:?}: {}",
|
||||||
|
binary_path,
|
||||||
|
e
|
||||||
|
);
|
||||||
|
(
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
Json(serde_json::json!({
|
||||||
|
"error": "Internal error",
|
||||||
|
"message": "Failed to read agent binary",
|
||||||
|
})),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
// Open file for streaming
|
||||||
|
let file = fs::File::open(&binary_path).await.map_err(|e| {
|
||||||
|
tracing::error!("Failed to open agent binary at {:?}: {}", binary_path, e);
|
||||||
|
(
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
Json(serde_json::json!({
|
||||||
|
"error": "Internal error",
|
||||||
|
"message": "Failed to open agent binary",
|
||||||
|
})),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let stream = ReaderStream::new(file);
|
||||||
|
let body = Body::from_stream(stream);
|
||||||
|
|
||||||
|
let headers_response = [
|
||||||
|
(header::CONTENT_TYPE, "application/octet-stream".to_string()),
|
||||||
|
(
|
||||||
|
header::CONTENT_DISPOSITION,
|
||||||
|
"attachment; filename=\"attune-agent\"".to_string(),
|
||||||
|
),
|
||||||
|
(header::CONTENT_LENGTH, metadata.len().to_string()),
|
||||||
|
(header::CACHE_CONTROL, "public, max-age=3600".to_string()),
|
||||||
|
];
|
||||||
|
|
||||||
|
tracing::info!(
|
||||||
|
arch = arch,
|
||||||
|
size_bytes = metadata.len(),
|
||||||
|
path = ?binary_path,
|
||||||
|
"Serving agent binary download"
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok((headers_response, body))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get agent binary metadata
|
||||||
|
///
|
||||||
|
/// Returns information about available agent binaries, including
|
||||||
|
/// supported architectures and binary sizes.
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/api/v1/agent/info",
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Agent binary info", body = AgentBinaryInfo),
|
||||||
|
(status = 503, description = "Agent binary distribution not configured"),
|
||||||
|
),
|
||||||
|
tag = "agent"
|
||||||
|
)]
|
||||||
|
pub async fn agent_info(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
|
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
|
||||||
|
let agent_config = state.config.agent.as_ref().ok_or_else(|| {
|
||||||
|
(
|
||||||
|
StatusCode::SERVICE_UNAVAILABLE,
|
||||||
|
Json(serde_json::json!({
|
||||||
|
"error": "Not configured",
|
||||||
|
"message": "Agent binary distribution is not configured.",
|
||||||
|
})),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let binary_dir = std::path::Path::new(&agent_config.binary_dir);
|
||||||
|
let architectures = ["x86_64", "aarch64"];
|
||||||
|
|
||||||
|
let mut arch_infos = Vec::new();
|
||||||
|
for arch in &architectures {
|
||||||
|
let arch_specific = binary_dir.join(format!("attune-agent-{}", arch));
|
||||||
|
let generic = binary_dir.join("attune-agent");
|
||||||
|
|
||||||
|
// Only fall back to the generic binary for x86_64, since the build
|
||||||
|
// pipeline currently produces x86_64-only generic binaries.
|
||||||
|
let (available, size_bytes) = if arch_specific.exists() {
|
||||||
|
match fs::metadata(&arch_specific).await {
|
||||||
|
Ok(m) => (true, m.len()),
|
||||||
|
Err(_) => (false, 0),
|
||||||
|
}
|
||||||
|
} else if *arch == "x86_64" && generic.exists() {
|
||||||
|
match fs::metadata(&generic).await {
|
||||||
|
Ok(m) => (true, m.len()),
|
||||||
|
Err(_) => (false, 0),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
(false, 0)
|
||||||
|
};
|
||||||
|
|
||||||
|
arch_infos.push(AgentArchInfo {
|
||||||
|
arch: arch.to_string(),
|
||||||
|
size_bytes,
|
||||||
|
available,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Json(AgentBinaryInfo {
|
||||||
|
architectures: arch_infos,
|
||||||
|
version: env!("CARGO_PKG_VERSION").to_string(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create agent routes
|
||||||
|
pub fn routes() -> Router<Arc<AppState>> {
|
||||||
|
Router::new()
|
||||||
|
.route("/agent/binary", get(download_agent_binary))
|
||||||
|
.route("/agent/info", get(agent_info))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use attune_common::config::AgentConfig;
|
||||||
|
use axum::http::{HeaderMap, HeaderValue};
|
||||||
|
|
||||||
|
// ── validate_arch tests ─────────────────────────────────────────
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_validate_arch_valid_x86_64() {
|
||||||
|
let result = validate_arch("x86_64");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(result.unwrap(), "x86_64");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_validate_arch_valid_aarch64() {
|
||||||
|
let result = validate_arch("aarch64");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(result.unwrap(), "aarch64");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_validate_arch_arm64_alias() {
|
||||||
|
// "arm64" is an alias for "aarch64"
|
||||||
|
let result = validate_arch("arm64");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(result.unwrap(), "aarch64");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_validate_arch_invalid() {
|
||||||
|
let result = validate_arch("mips");
|
||||||
|
assert!(result.is_err());
|
||||||
|
let (status, body) = result.unwrap_err();
|
||||||
|
assert_eq!(status, StatusCode::BAD_REQUEST);
|
||||||
|
assert_eq!(body.0["error"], "Invalid architecture");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── validate_token tests ────────────────────────────────────────
|
||||||
|
|
||||||
|
/// Helper: build a minimal Config with the given agent config.
|
||||||
|
/// Only the `agent` field is relevant for `validate_token`.
|
||||||
|
fn test_config(agent: Option<AgentConfig>) -> attune_common::config::Config {
|
||||||
|
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());
|
||||||
|
let config_path = format!("{}/../../config.test.yaml", manifest_dir);
|
||||||
|
let mut config = attune_common::config::Config::load_from_file(&config_path)
|
||||||
|
.expect("Failed to load test config");
|
||||||
|
config.agent = agent;
|
||||||
|
config
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_validate_token_no_config() {
|
||||||
|
// When no agent config is set at all, no token is required.
|
||||||
|
let config = test_config(None);
|
||||||
|
let headers = HeaderMap::new();
|
||||||
|
let query_token = None;
|
||||||
|
|
||||||
|
let result = validate_token(&config, &headers, &query_token);
|
||||||
|
assert!(result.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_validate_token_no_bootstrap_token_configured() {
|
||||||
|
// Agent config exists but bootstrap_token is None → no token required.
|
||||||
|
let config = test_config(Some(AgentConfig {
|
||||||
|
binary_dir: "/tmp/test".to_string(),
|
||||||
|
bootstrap_token: None,
|
||||||
|
}));
|
||||||
|
let headers = HeaderMap::new();
|
||||||
|
let query_token = None;
|
||||||
|
|
||||||
|
let result = validate_token(&config, &headers, &query_token);
|
||||||
|
assert!(result.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_validate_token_valid_from_header() {
|
||||||
|
let config = test_config(Some(AgentConfig {
|
||||||
|
binary_dir: "/tmp/test".to_string(),
|
||||||
|
bootstrap_token: Some("s3cret-bootstrap".to_string()),
|
||||||
|
}));
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert(
|
||||||
|
"x-agent-token",
|
||||||
|
HeaderValue::from_static("s3cret-bootstrap"),
|
||||||
|
);
|
||||||
|
let query_token = None;
|
||||||
|
|
||||||
|
let result = validate_token(&config, &headers, &query_token);
|
||||||
|
assert!(result.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_validate_token_valid_from_query() {
|
||||||
|
let config = test_config(Some(AgentConfig {
|
||||||
|
binary_dir: "/tmp/test".to_string(),
|
||||||
|
bootstrap_token: Some("s3cret-bootstrap".to_string()),
|
||||||
|
}));
|
||||||
|
let headers = HeaderMap::new();
|
||||||
|
let query_token = Some("s3cret-bootstrap".to_string());
|
||||||
|
|
||||||
|
let result = validate_token(&config, &headers, &query_token);
|
||||||
|
assert!(result.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_validate_token_invalid() {
|
||||||
|
let config = test_config(Some(AgentConfig {
|
||||||
|
binary_dir: "/tmp/test".to_string(),
|
||||||
|
bootstrap_token: Some("correct-token".to_string()),
|
||||||
|
}));
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert("x-agent-token", HeaderValue::from_static("wrong-token"));
|
||||||
|
let query_token = None;
|
||||||
|
|
||||||
|
let result = validate_token(&config, &headers, &query_token);
|
||||||
|
assert!(result.is_err());
|
||||||
|
let (status, body) = result.unwrap_err();
|
||||||
|
assert_eq!(status, StatusCode::UNAUTHORIZED);
|
||||||
|
assert_eq!(body.0["error"], "Invalid token");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_validate_token_missing_when_required() {
|
||||||
|
// bootstrap_token is configured but caller provides nothing.
|
||||||
|
let config = test_config(Some(AgentConfig {
|
||||||
|
binary_dir: "/tmp/test".to_string(),
|
||||||
|
bootstrap_token: Some("required-token".to_string()),
|
||||||
|
}));
|
||||||
|
let headers = HeaderMap::new();
|
||||||
|
let query_token = None;
|
||||||
|
|
||||||
|
let result = validate_token(&config, &headers, &query_token);
|
||||||
|
assert!(result.is_err());
|
||||||
|
let (status, body) = result.unwrap_err();
|
||||||
|
assert_eq!(status, StatusCode::UNAUTHORIZED);
|
||||||
|
assert_eq!(body.0["error"], "Token required");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_validate_token_header_takes_precedence_over_query() {
|
||||||
|
// When both header and query provide a token, the header value is
|
||||||
|
// checked first (it appears first in the or_else chain). Provide a
|
||||||
|
// valid token in the header and an invalid one in the query — should
|
||||||
|
// succeed because the header matches.
|
||||||
|
let config = test_config(Some(AgentConfig {
|
||||||
|
binary_dir: "/tmp/test".to_string(),
|
||||||
|
bootstrap_token: Some("the-real-token".to_string()),
|
||||||
|
}));
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert("x-agent-token", HeaderValue::from_static("the-real-token"));
|
||||||
|
let query_token = Some("wrong-token".to_string());
|
||||||
|
|
||||||
|
let result = validate_token(&config, &headers, &query_token);
|
||||||
|
assert!(result.is_ok());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -40,7 +40,8 @@ use attune_common::repositories::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
auth::middleware::RequireAuth,
|
auth::{jwt::TokenType, middleware::AuthenticatedUser, middleware::RequireAuth},
|
||||||
|
authz::{AuthorizationCheck, AuthorizationService},
|
||||||
dto::{
|
dto::{
|
||||||
artifact::{
|
artifact::{
|
||||||
AllocateFileVersionByRefRequest, AppendProgressRequest, ArtifactExecutionPatch,
|
AllocateFileVersionByRefRequest, AppendProgressRequest, ArtifactExecutionPatch,
|
||||||
@@ -55,6 +56,7 @@ use crate::{
|
|||||||
middleware::{ApiError, ApiResult},
|
middleware::{ApiError, ApiResult},
|
||||||
state::AppState,
|
state::AppState,
|
||||||
};
|
};
|
||||||
|
use attune_common::rbac::{Action, AuthorizationContext, Resource};
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Artifact CRUD
|
// Artifact CRUD
|
||||||
@@ -72,7 +74,7 @@ use crate::{
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn list_artifacts(
|
pub async fn list_artifacts(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Query(query): Query<ArtifactQueryParams>,
|
Query(query): Query<ArtifactQueryParams>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
@@ -88,8 +90,16 @@ pub async fn list_artifacts(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let result = ArtifactRepository::search(&state.db, &filters).await?;
|
let result = ArtifactRepository::search(&state.db, &filters).await?;
|
||||||
|
let mut rows = result.rows;
|
||||||
|
|
||||||
let items: Vec<ArtifactSummary> = result.rows.into_iter().map(ArtifactSummary::from).collect();
|
if let Some((identity_id, grants)) = ensure_can_read_any_artifact(&state, &user).await? {
|
||||||
|
rows.retain(|artifact| {
|
||||||
|
let ctx = artifact_authorization_context(identity_id, artifact);
|
||||||
|
AuthorizationService::is_allowed(&grants, Resource::Artifacts, Action::Read, &ctx)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let items: Vec<ArtifactSummary> = rows.into_iter().map(ArtifactSummary::from).collect();
|
||||||
|
|
||||||
let pagination = PaginationParams {
|
let pagination = PaginationParams {
|
||||||
page: query.page,
|
page: query.page,
|
||||||
@@ -113,7 +123,7 @@ pub async fn list_artifacts(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn get_artifact(
|
pub async fn get_artifact(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(id): Path<i64>,
|
Path(id): Path<i64>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
@@ -121,6 +131,10 @@ pub async fn get_artifact(
|
|||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Read, &artifact)
|
||||||
|
.await
|
||||||
|
.map_err(|_| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
Json(ApiResponse::new(ArtifactResponse::from(artifact))),
|
Json(ApiResponse::new(ArtifactResponse::from(artifact))),
|
||||||
@@ -140,7 +154,7 @@ pub async fn get_artifact(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn get_artifact_by_ref(
|
pub async fn get_artifact_by_ref(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(artifact_ref): Path<String>,
|
Path(artifact_ref): Path<String>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
@@ -148,6 +162,10 @@ pub async fn get_artifact_by_ref(
|
|||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact '{}' not found", artifact_ref)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact '{}' not found", artifact_ref)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Read, &artifact)
|
||||||
|
.await
|
||||||
|
.map_err(|_| ApiError::NotFound(format!("Artifact '{}' not found", artifact_ref)))?;
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
Json(ApiResponse::new(ArtifactResponse::from(artifact))),
|
Json(ApiResponse::new(ArtifactResponse::from(artifact))),
|
||||||
@@ -168,7 +186,7 @@ pub async fn get_artifact_by_ref(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn create_artifact(
|
pub async fn create_artifact(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Json(request): Json<CreateArtifactRequest>,
|
Json(request): Json<CreateArtifactRequest>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
@@ -200,6 +218,16 @@ pub async fn create_artifact(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
authorize_artifact_create(
|
||||||
|
&state,
|
||||||
|
&user,
|
||||||
|
&request.r#ref,
|
||||||
|
request.scope,
|
||||||
|
&request.owner,
|
||||||
|
visibility,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
let input = CreateArtifactInput {
|
let input = CreateArtifactInput {
|
||||||
r#ref: request.r#ref,
|
r#ref: request.r#ref,
|
||||||
scope: request.scope,
|
scope: request.scope,
|
||||||
@@ -240,16 +268,18 @@ pub async fn create_artifact(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn update_artifact(
|
pub async fn update_artifact(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(id): Path<i64>,
|
Path(id): Path<i64>,
|
||||||
Json(request): Json<UpdateArtifactRequest>,
|
Json(request): Json<UpdateArtifactRequest>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
// Verify artifact exists
|
// Verify artifact exists
|
||||||
ArtifactRepository::find_by_id(&state.db, id)
|
let artifact = ArtifactRepository::find_by_id(&state.db, id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Update, &artifact).await?;
|
||||||
|
|
||||||
let input = UpdateArtifactInput {
|
let input = UpdateArtifactInput {
|
||||||
r#ref: None, // Ref is immutable after creation
|
r#ref: None, // Ref is immutable after creation
|
||||||
scope: request.scope,
|
scope: request.scope,
|
||||||
@@ -305,7 +335,7 @@ pub async fn update_artifact(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn delete_artifact(
|
pub async fn delete_artifact(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(id): Path<i64>,
|
Path(id): Path<i64>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
@@ -313,6 +343,8 @@ pub async fn delete_artifact(
|
|||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Delete, &artifact).await?;
|
||||||
|
|
||||||
// Before deleting DB rows, clean up any file-backed versions on disk
|
// Before deleting DB rows, clean up any file-backed versions on disk
|
||||||
let file_versions =
|
let file_versions =
|
||||||
ArtifactVersionRepository::find_file_versions_by_artifact(&state.db, id).await?;
|
ArtifactVersionRepository::find_file_versions_by_artifact(&state.db, id).await?;
|
||||||
@@ -355,11 +387,17 @@ pub async fn delete_artifact(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn list_artifacts_by_execution(
|
pub async fn list_artifacts_by_execution(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(execution_id): Path<i64>,
|
Path(execution_id): Path<i64>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
let artifacts = ArtifactRepository::find_by_execution(&state.db, execution_id).await?;
|
let mut artifacts = ArtifactRepository::find_by_execution(&state.db, execution_id).await?;
|
||||||
|
if let Some((identity_id, grants)) = ensure_can_read_any_artifact(&state, &user).await? {
|
||||||
|
artifacts.retain(|artifact| {
|
||||||
|
let ctx = artifact_authorization_context(identity_id, artifact);
|
||||||
|
AuthorizationService::is_allowed(&grants, Resource::Artifacts, Action::Read, &ctx)
|
||||||
|
});
|
||||||
|
}
|
||||||
let items: Vec<ArtifactSummary> = artifacts.into_iter().map(ArtifactSummary::from).collect();
|
let items: Vec<ArtifactSummary> = artifacts.into_iter().map(ArtifactSummary::from).collect();
|
||||||
|
|
||||||
Ok((StatusCode::OK, Json(ApiResponse::new(items))))
|
Ok((StatusCode::OK, Json(ApiResponse::new(items))))
|
||||||
@@ -387,7 +425,7 @@ pub async fn list_artifacts_by_execution(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn append_progress(
|
pub async fn append_progress(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(id): Path<i64>,
|
Path(id): Path<i64>,
|
||||||
Json(request): Json<AppendProgressRequest>,
|
Json(request): Json<AppendProgressRequest>,
|
||||||
@@ -396,6 +434,8 @@ pub async fn append_progress(
|
|||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Update, &artifact).await?;
|
||||||
|
|
||||||
if artifact.r#type != ArtifactType::Progress {
|
if artifact.r#type != ArtifactType::Progress {
|
||||||
return Err(ApiError::BadRequest(format!(
|
return Err(ApiError::BadRequest(format!(
|
||||||
"Artifact '{}' is type {:?}, not progress. Use version endpoints for file artifacts.",
|
"Artifact '{}' is type {:?}, not progress. Use version endpoints for file artifacts.",
|
||||||
@@ -430,16 +470,18 @@ pub async fn append_progress(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn set_artifact_data(
|
pub async fn set_artifact_data(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(id): Path<i64>,
|
Path(id): Path<i64>,
|
||||||
Json(request): Json<SetDataRequest>,
|
Json(request): Json<SetDataRequest>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
// Verify exists
|
// Verify exists
|
||||||
ArtifactRepository::find_by_id(&state.db, id)
|
let artifact = ArtifactRepository::find_by_id(&state.db, id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Update, &artifact).await?;
|
||||||
|
|
||||||
let updated = ArtifactRepository::set_data(&state.db, id, &request.data).await?;
|
let updated = ArtifactRepository::set_data(&state.db, id, &request.data).await?;
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
@@ -468,15 +510,19 @@ pub async fn set_artifact_data(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn list_versions(
|
pub async fn list_versions(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(id): Path<i64>,
|
Path(id): Path<i64>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
// Verify artifact exists
|
// Verify artifact exists
|
||||||
ArtifactRepository::find_by_id(&state.db, id)
|
let artifact = ArtifactRepository::find_by_id(&state.db, id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Read, &artifact)
|
||||||
|
.await
|
||||||
|
.map_err(|_| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
let versions = ArtifactVersionRepository::list_by_artifact(&state.db, id).await?;
|
let versions = ArtifactVersionRepository::list_by_artifact(&state.db, id).await?;
|
||||||
let items: Vec<ArtifactVersionSummary> = versions
|
let items: Vec<ArtifactVersionSummary> = versions
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -502,15 +548,19 @@ pub async fn list_versions(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn get_version(
|
pub async fn get_version(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path((id, version)): Path<(i64, i32)>,
|
Path((id, version)): Path<(i64, i32)>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
// Verify artifact exists
|
// Verify artifact exists
|
||||||
ArtifactRepository::find_by_id(&state.db, id)
|
let artifact = ArtifactRepository::find_by_id(&state.db, id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Read, &artifact)
|
||||||
|
.await
|
||||||
|
.map_err(|_| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
let ver = ArtifactVersionRepository::find_by_version(&state.db, id, version)
|
let ver = ArtifactVersionRepository::find_by_version(&state.db, id, version)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
@@ -536,14 +586,18 @@ pub async fn get_version(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn get_latest_version(
|
pub async fn get_latest_version(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(id): Path<i64>,
|
Path(id): Path<i64>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
ArtifactRepository::find_by_id(&state.db, id)
|
let artifact = ArtifactRepository::find_by_id(&state.db, id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Read, &artifact)
|
||||||
|
.await
|
||||||
|
.map_err(|_| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
let ver = ArtifactVersionRepository::find_latest(&state.db, id)
|
let ver = ArtifactVersionRepository::find_latest(&state.db, id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("No versions found for artifact {}", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("No versions found for artifact {}", id)))?;
|
||||||
@@ -568,15 +622,17 @@ pub async fn get_latest_version(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn create_version_json(
|
pub async fn create_version_json(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(id): Path<i64>,
|
Path(id): Path<i64>,
|
||||||
Json(request): Json<CreateVersionJsonRequest>,
|
Json(request): Json<CreateVersionJsonRequest>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
ArtifactRepository::find_by_id(&state.db, id)
|
let artifact = ArtifactRepository::find_by_id(&state.db, id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Update, &artifact).await?;
|
||||||
|
|
||||||
let input = CreateArtifactVersionInput {
|
let input = CreateArtifactVersionInput {
|
||||||
artifact: id,
|
artifact: id,
|
||||||
content_type: Some(
|
content_type: Some(
|
||||||
@@ -624,7 +680,7 @@ pub async fn create_version_json(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn create_version_file(
|
pub async fn create_version_file(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(id): Path<i64>,
|
Path(id): Path<i64>,
|
||||||
Json(request): Json<CreateFileVersionRequest>,
|
Json(request): Json<CreateFileVersionRequest>,
|
||||||
@@ -633,6 +689,8 @@ pub async fn create_version_file(
|
|||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Update, &artifact).await?;
|
||||||
|
|
||||||
// Validate this is a file-type artifact
|
// Validate this is a file-type artifact
|
||||||
if !is_file_backed_type(artifact.r#type) {
|
if !is_file_backed_type(artifact.r#type) {
|
||||||
return Err(ApiError::BadRequest(format!(
|
return Err(ApiError::BadRequest(format!(
|
||||||
@@ -726,15 +784,17 @@ pub async fn create_version_file(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn upload_version(
|
pub async fn upload_version(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(id): Path<i64>,
|
Path(id): Path<i64>,
|
||||||
mut multipart: Multipart,
|
mut multipart: Multipart,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
ArtifactRepository::find_by_id(&state.db, id)
|
let artifact = ArtifactRepository::find_by_id(&state.db, id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Update, &artifact).await?;
|
||||||
|
|
||||||
let mut file_data: Option<Vec<u8>> = None;
|
let mut file_data: Option<Vec<u8>> = None;
|
||||||
let mut content_type: Option<String> = None;
|
let mut content_type: Option<String> = None;
|
||||||
let mut meta: Option<serde_json::Value> = None;
|
let mut meta: Option<serde_json::Value> = None;
|
||||||
@@ -854,7 +914,7 @@ pub async fn upload_version(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn download_version(
|
pub async fn download_version(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path((id, version)): Path<(i64, i32)>,
|
Path((id, version)): Path<(i64, i32)>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
@@ -862,6 +922,10 @@ pub async fn download_version(
|
|||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Read, &artifact)
|
||||||
|
.await
|
||||||
|
.map_err(|_| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
// First try without content (cheaper query) to check for file_path
|
// First try without content (cheaper query) to check for file_path
|
||||||
let ver = ArtifactVersionRepository::find_by_version(&state.db, id, version)
|
let ver = ArtifactVersionRepository::find_by_version(&state.db, id, version)
|
||||||
.await?
|
.await?
|
||||||
@@ -904,7 +968,7 @@ pub async fn download_version(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn download_latest(
|
pub async fn download_latest(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(id): Path<i64>,
|
Path(id): Path<i64>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
@@ -912,6 +976,10 @@ pub async fn download_latest(
|
|||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Read, &artifact)
|
||||||
|
.await
|
||||||
|
.map_err(|_| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
// First try without content (cheaper query) to check for file_path
|
// First try without content (cheaper query) to check for file_path
|
||||||
let ver = ArtifactVersionRepository::find_latest(&state.db, id)
|
let ver = ArtifactVersionRepository::find_latest(&state.db, id)
|
||||||
.await?
|
.await?
|
||||||
@@ -955,7 +1023,7 @@ pub async fn download_latest(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn delete_version(
|
pub async fn delete_version(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path((id, version)): Path<(i64, i32)>,
|
Path((id, version)): Path<(i64, i32)>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
@@ -964,6 +1032,8 @@ pub async fn delete_version(
|
|||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Delete, &artifact).await?;
|
||||||
|
|
||||||
// Find the version by artifact + version number
|
// Find the version by artifact + version number
|
||||||
let ver = ArtifactVersionRepository::find_by_version(&state.db, id, version)
|
let ver = ArtifactVersionRepository::find_by_version(&state.db, id, version)
|
||||||
.await?
|
.await?
|
||||||
@@ -1042,7 +1112,7 @@ pub async fn delete_version(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn upload_version_by_ref(
|
pub async fn upload_version_by_ref(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(artifact_ref): Path<String>,
|
Path(artifact_ref): Path<String>,
|
||||||
mut multipart: Multipart,
|
mut multipart: Multipart,
|
||||||
@@ -1157,6 +1227,8 @@ pub async fn upload_version_by_ref(
|
|||||||
// Upsert: find existing artifact or create a new one
|
// Upsert: find existing artifact or create a new one
|
||||||
let artifact = match ArtifactRepository::find_by_ref(&state.db, &artifact_ref).await? {
|
let artifact = match ArtifactRepository::find_by_ref(&state.db, &artifact_ref).await? {
|
||||||
Some(existing) => {
|
Some(existing) => {
|
||||||
|
authorize_artifact_action(&state, &user, Action::Update, &existing).await?;
|
||||||
|
|
||||||
// Update execution link if a new execution ID was provided
|
// Update execution link if a new execution ID was provided
|
||||||
if execution_id.is_some() && execution_id != existing.execution {
|
if execution_id.is_some() && execution_id != existing.execution {
|
||||||
let update_input = UpdateArtifactInput {
|
let update_input = UpdateArtifactInput {
|
||||||
@@ -1211,6 +1283,16 @@ pub async fn upload_version_by_ref(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
authorize_artifact_create(
|
||||||
|
&state,
|
||||||
|
&user,
|
||||||
|
&artifact_ref,
|
||||||
|
a_scope,
|
||||||
|
owner.as_deref().unwrap_or_default(),
|
||||||
|
a_visibility,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// Parse retention
|
// Parse retention
|
||||||
let a_retention_policy: RetentionPolicyType = match &retention_policy {
|
let a_retention_policy: RetentionPolicyType = match &retention_policy {
|
||||||
Some(rp) if !rp.is_empty() => {
|
Some(rp) if !rp.is_empty() => {
|
||||||
@@ -1297,7 +1379,7 @@ pub async fn upload_version_by_ref(
|
|||||||
security(("bearer_auth" = []))
|
security(("bearer_auth" = []))
|
||||||
)]
|
)]
|
||||||
pub async fn allocate_file_version_by_ref(
|
pub async fn allocate_file_version_by_ref(
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Path(artifact_ref): Path<String>,
|
Path(artifact_ref): Path<String>,
|
||||||
Json(request): Json<AllocateFileVersionByRefRequest>,
|
Json(request): Json<AllocateFileVersionByRefRequest>,
|
||||||
@@ -1305,6 +1387,8 @@ pub async fn allocate_file_version_by_ref(
|
|||||||
// Upsert: find existing artifact or create a new one
|
// Upsert: find existing artifact or create a new one
|
||||||
let artifact = match ArtifactRepository::find_by_ref(&state.db, &artifact_ref).await? {
|
let artifact = match ArtifactRepository::find_by_ref(&state.db, &artifact_ref).await? {
|
||||||
Some(existing) => {
|
Some(existing) => {
|
||||||
|
authorize_artifact_action(&state, &user, Action::Update, &existing).await?;
|
||||||
|
|
||||||
// Update execution link if a new execution ID was provided
|
// Update execution link if a new execution ID was provided
|
||||||
if request.execution.is_some() && request.execution != existing.execution {
|
if request.execution.is_some() && request.execution != existing.execution {
|
||||||
let update_input = UpdateArtifactInput {
|
let update_input = UpdateArtifactInput {
|
||||||
@@ -1347,6 +1431,16 @@ pub async fn allocate_file_version_by_ref(
|
|||||||
.unwrap_or(RetentionPolicyType::Versions);
|
.unwrap_or(RetentionPolicyType::Versions);
|
||||||
let a_retention_limit = request.retention_limit.unwrap_or(10);
|
let a_retention_limit = request.retention_limit.unwrap_or(10);
|
||||||
|
|
||||||
|
authorize_artifact_create(
|
||||||
|
&state,
|
||||||
|
&user,
|
||||||
|
&artifact_ref,
|
||||||
|
a_scope,
|
||||||
|
request.owner.as_deref().unwrap_or_default(),
|
||||||
|
a_visibility,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
let create_input = CreateArtifactInput {
|
let create_input = CreateArtifactInput {
|
||||||
r#ref: artifact_ref.clone(),
|
r#ref: artifact_ref.clone(),
|
||||||
scope: a_scope,
|
scope: a_scope,
|
||||||
@@ -1437,6 +1531,105 @@ pub async fn allocate_file_version_by_ref(
|
|||||||
// Helpers
|
// Helpers
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
|
async fn authorize_artifact_action(
|
||||||
|
state: &Arc<AppState>,
|
||||||
|
user: &AuthenticatedUser,
|
||||||
|
action: Action,
|
||||||
|
artifact: &attune_common::models::artifact::Artifact,
|
||||||
|
) -> Result<(), ApiError> {
|
||||||
|
if user.claims.token_type != TokenType::Access {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let identity_id = user
|
||||||
|
.identity_id()
|
||||||
|
.map_err(|_| ApiError::Unauthorized("Invalid user identity".to_string()))?;
|
||||||
|
let authz = AuthorizationService::new(state.db.clone());
|
||||||
|
authz
|
||||||
|
.authorize(
|
||||||
|
user,
|
||||||
|
AuthorizationCheck {
|
||||||
|
resource: Resource::Artifacts,
|
||||||
|
action,
|
||||||
|
context: artifact_authorization_context(identity_id, artifact),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn authorize_artifact_create(
|
||||||
|
state: &Arc<AppState>,
|
||||||
|
user: &AuthenticatedUser,
|
||||||
|
artifact_ref: &str,
|
||||||
|
scope: OwnerType,
|
||||||
|
owner: &str,
|
||||||
|
visibility: ArtifactVisibility,
|
||||||
|
) -> Result<(), ApiError> {
|
||||||
|
if user.claims.token_type != TokenType::Access {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let identity_id = user
|
||||||
|
.identity_id()
|
||||||
|
.map_err(|_| ApiError::Unauthorized("Invalid user identity".to_string()))?;
|
||||||
|
let authz = AuthorizationService::new(state.db.clone());
|
||||||
|
let mut ctx = AuthorizationContext::new(identity_id);
|
||||||
|
ctx.target_ref = Some(artifact_ref.to_string());
|
||||||
|
ctx.owner_type = Some(scope);
|
||||||
|
ctx.owner_ref = Some(owner.to_string());
|
||||||
|
ctx.visibility = Some(visibility);
|
||||||
|
|
||||||
|
authz
|
||||||
|
.authorize(
|
||||||
|
user,
|
||||||
|
AuthorizationCheck {
|
||||||
|
resource: Resource::Artifacts,
|
||||||
|
action: Action::Create,
|
||||||
|
context: ctx,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn ensure_can_read_any_artifact(
|
||||||
|
state: &Arc<AppState>,
|
||||||
|
user: &AuthenticatedUser,
|
||||||
|
) -> Result<Option<(i64, Vec<attune_common::rbac::Grant>)>, ApiError> {
|
||||||
|
if user.claims.token_type != TokenType::Access {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let identity_id = user
|
||||||
|
.identity_id()
|
||||||
|
.map_err(|_| ApiError::Unauthorized("Invalid user identity".to_string()))?;
|
||||||
|
let authz = AuthorizationService::new(state.db.clone());
|
||||||
|
let grants = authz.effective_grants(user).await?;
|
||||||
|
|
||||||
|
let can_read_any_artifact = grants
|
||||||
|
.iter()
|
||||||
|
.any(|g| g.resource == Resource::Artifacts && g.actions.contains(&Action::Read));
|
||||||
|
if !can_read_any_artifact {
|
||||||
|
return Err(ApiError::Forbidden(
|
||||||
|
"Insufficient permissions: artifacts:read".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Some((identity_id, grants)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn artifact_authorization_context(
|
||||||
|
identity_id: i64,
|
||||||
|
artifact: &attune_common::models::artifact::Artifact,
|
||||||
|
) -> AuthorizationContext {
|
||||||
|
let mut ctx = AuthorizationContext::new(identity_id);
|
||||||
|
ctx.target_id = Some(artifact.id);
|
||||||
|
ctx.target_ref = Some(artifact.r#ref.clone());
|
||||||
|
ctx.owner_type = Some(artifact.scope);
|
||||||
|
ctx.owner_ref = Some(artifact.owner.clone());
|
||||||
|
ctx.visibility = Some(artifact.visibility);
|
||||||
|
ctx
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns true for artifact types that should use file-backed storage on disk.
|
/// Returns true for artifact types that should use file-backed storage on disk.
|
||||||
fn is_file_backed_type(artifact_type: ArtifactType) -> bool {
|
fn is_file_backed_type(artifact_type: ArtifactType) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
@@ -1775,14 +1968,19 @@ pub async fn stream_artifact(
|
|||||||
let token = params.token.as_ref().ok_or(ApiError::Unauthorized(
|
let token = params.token.as_ref().ok_or(ApiError::Unauthorized(
|
||||||
"Missing authentication token".to_string(),
|
"Missing authentication token".to_string(),
|
||||||
))?;
|
))?;
|
||||||
validate_token(token, &state.jwt_config)
|
let claims = validate_token(token, &state.jwt_config)
|
||||||
.map_err(|_| ApiError::Unauthorized("Invalid authentication token".to_string()))?;
|
.map_err(|_| ApiError::Unauthorized("Invalid authentication token".to_string()))?;
|
||||||
|
let user = AuthenticatedUser { claims };
|
||||||
|
|
||||||
// --- resolve artifact + latest version ---------------------------------
|
// --- resolve artifact + latest version ---------------------------------
|
||||||
let artifact = ArtifactRepository::find_by_id(&state.db, id)
|
let artifact = ArtifactRepository::find_by_id(&state.db, id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
|
authorize_artifact_action(&state, &user, Action::Read, &artifact)
|
||||||
|
.await
|
||||||
|
.map_err(|_| ApiError::NotFound(format!("Artifact with ID {} not found", id)))?;
|
||||||
|
|
||||||
if !is_file_backed_type(artifact.r#type) {
|
if !is_file_backed_type(artifact.r#type) {
|
||||||
return Err(ApiError::BadRequest(format!(
|
return Err(ApiError::BadRequest(format!(
|
||||||
"Artifact '{}' is type {:?} which is not file-backed. \
|
"Artifact '{}' is type {:?} which is not file-backed. \
|
||||||
|
|||||||
@@ -169,6 +169,12 @@ pub async fn login(
|
|||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::Unauthorized("Invalid login or password".to_string()))?;
|
.ok_or_else(|| ApiError::Unauthorized("Invalid login or password".to_string()))?;
|
||||||
|
|
||||||
|
if identity.frozen {
|
||||||
|
return Err(ApiError::Forbidden(
|
||||||
|
"Identity is frozen and cannot authenticate".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
// Check if identity has a password set
|
// Check if identity has a password set
|
||||||
let password_hash = identity
|
let password_hash = identity
|
||||||
.password_hash
|
.password_hash
|
||||||
@@ -324,6 +330,12 @@ pub async fn refresh_token(
|
|||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::Unauthorized("Identity not found".to_string()))?;
|
.ok_or_else(|| ApiError::Unauthorized("Identity not found".to_string()))?;
|
||||||
|
|
||||||
|
if identity.frozen {
|
||||||
|
return Err(ApiError::Forbidden(
|
||||||
|
"Identity is frozen and cannot authenticate".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
// Generate new tokens
|
// Generate new tokens
|
||||||
let access_token = generate_access_token(identity.id, &identity.login, &state.jwt_config)?;
|
let access_token = generate_access_token(identity.id, &identity.login, &state.jwt_config)?;
|
||||||
let refresh_token = generate_refresh_token(identity.id, &identity.login, &state.jwt_config)?;
|
let refresh_token = generate_refresh_token(identity.id, &identity.login, &state.jwt_config)?;
|
||||||
@@ -380,6 +392,12 @@ pub async fn get_current_user(
|
|||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound("Identity not found".to_string()))?;
|
.ok_or_else(|| ApiError::NotFound("Identity not found".to_string()))?;
|
||||||
|
|
||||||
|
if identity.frozen {
|
||||||
|
return Err(ApiError::Forbidden(
|
||||||
|
"Identity is frozen and cannot authenticate".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
let response = CurrentUserResponse {
|
let response = CurrentUserResponse {
|
||||||
id: identity.id,
|
id: identity.id,
|
||||||
login: identity.login,
|
login: identity.login,
|
||||||
@@ -551,6 +569,7 @@ pub async fn change_password(
|
|||||||
display_name: None,
|
display_name: None,
|
||||||
password_hash: Some(new_password_hash),
|
password_hash: Some(new_password_hash),
|
||||||
attributes: None,
|
attributes: None,
|
||||||
|
frozen: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
IdentityRepository::update(&state.db, identity_id, update_input).await?;
|
IdentityRepository::update(&state.db, identity_id, update_input).await?;
|
||||||
|
|||||||
@@ -82,6 +82,17 @@ pub async fn create_event(
|
|||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Json(payload): Json<CreateEventRequest>,
|
Json(payload): Json<CreateEventRequest>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
|
// Only sensor and execution tokens may create events directly.
|
||||||
|
// User sessions must go through the webhook receiver instead.
|
||||||
|
use crate::auth::jwt::TokenType;
|
||||||
|
if user.0.claims.token_type == TokenType::Access {
|
||||||
|
return Err(ApiError::Forbidden(
|
||||||
|
"Events may only be created by sensor services. To fire an event as a user, \
|
||||||
|
enable webhooks on the trigger and POST to its webhook URL."
|
||||||
|
.to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
// Validate request
|
// Validate request
|
||||||
payload
|
payload
|
||||||
.validate()
|
.validate()
|
||||||
@@ -128,7 +139,6 @@ pub async fn create_event(
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Determine source (sensor) from authenticated user if it's a sensor token
|
// Determine source (sensor) from authenticated user if it's a sensor token
|
||||||
use crate::auth::jwt::TokenType;
|
|
||||||
let (source_id, source_ref) = match user.0.claims.token_type {
|
let (source_id, source_ref) = match user.0.claims.token_type {
|
||||||
TokenType::Sensor => {
|
TokenType::Sensor => {
|
||||||
// Extract sensor reference from login
|
// Extract sensor reference from login
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Path, Query, State},
|
extract::{Path, Query, State},
|
||||||
|
http::HeaderMap,
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::{
|
response::{
|
||||||
sse::{Event, KeepAlive, Sse},
|
sse::{Event, KeepAlive, Sse},
|
||||||
@@ -13,6 +14,7 @@ use axum::{
|
|||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use futures::stream::{Stream, StreamExt};
|
use futures::stream::{Stream, StreamExt};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
use tokio_stream::wrappers::BroadcastStream;
|
use tokio_stream::wrappers::BroadcastStream;
|
||||||
|
|
||||||
use attune_common::models::enums::ExecutionStatus;
|
use attune_common::models::enums::ExecutionStatus;
|
||||||
@@ -32,7 +34,10 @@ use attune_common::workflow::{CancellationPolicy, WorkflowDefinition};
|
|||||||
use sqlx::Row;
|
use sqlx::Row;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
auth::middleware::RequireAuth,
|
auth::{
|
||||||
|
jwt::{validate_token, Claims, JwtConfig, TokenType},
|
||||||
|
middleware::{AuthenticatedUser, RequireAuth},
|
||||||
|
},
|
||||||
authz::{AuthorizationCheck, AuthorizationService},
|
authz::{AuthorizationCheck, AuthorizationService},
|
||||||
dto::{
|
dto::{
|
||||||
common::{PaginatedResponse, PaginationParams},
|
common::{PaginatedResponse, PaginationParams},
|
||||||
@@ -46,6 +51,9 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use attune_common::rbac::{Action, AuthorizationContext, Resource};
|
use attune_common::rbac::{Action, AuthorizationContext, Resource};
|
||||||
|
|
||||||
|
const LOG_STREAM_POLL_INTERVAL: Duration = Duration::from_millis(250);
|
||||||
|
const LOG_STREAM_READ_CHUNK_SIZE: usize = 64 * 1024;
|
||||||
|
|
||||||
/// Create a new execution (manual execution)
|
/// Create a new execution (manual execution)
|
||||||
///
|
///
|
||||||
/// This endpoint allows directly executing an action without a trigger or rule.
|
/// This endpoint allows directly executing an action without a trigger or rule.
|
||||||
@@ -93,19 +101,6 @@ pub async fn create_execution(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let mut execution_ctx = AuthorizationContext::new(identity_id);
|
|
||||||
execution_ctx.pack_ref = Some(action.pack_ref.clone());
|
|
||||||
authz
|
|
||||||
.authorize(
|
|
||||||
&user,
|
|
||||||
AuthorizationCheck {
|
|
||||||
resource: Resource::Executions,
|
|
||||||
action: Action::Create,
|
|
||||||
context: execution_ctx,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create execution input
|
// Create execution input
|
||||||
@@ -938,6 +933,398 @@ pub async fn stream_execution_updates(
|
|||||||
Ok(Sse::new(filtered_stream).keep_alive(KeepAlive::default()))
|
Ok(Sse::new(filtered_stream).keep_alive(KeepAlive::default()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
pub struct StreamExecutionLogParams {
|
||||||
|
pub token: Option<String>,
|
||||||
|
pub offset: Option<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
enum ExecutionLogStream {
|
||||||
|
Stdout,
|
||||||
|
Stderr,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecutionLogStream {
|
||||||
|
fn parse(name: &str) -> Result<Self, ApiError> {
|
||||||
|
match name {
|
||||||
|
"stdout" => Ok(Self::Stdout),
|
||||||
|
"stderr" => Ok(Self::Stderr),
|
||||||
|
_ => Err(ApiError::BadRequest(format!(
|
||||||
|
"Unsupported log stream '{}'. Expected 'stdout' or 'stderr'.",
|
||||||
|
name
|
||||||
|
))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn file_name(self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Self::Stdout => "stdout.log",
|
||||||
|
Self::Stderr => "stderr.log",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ExecutionLogTailState {
|
||||||
|
WaitingForFile {
|
||||||
|
full_path: std::path::PathBuf,
|
||||||
|
execution_id: i64,
|
||||||
|
},
|
||||||
|
SendInitial {
|
||||||
|
full_path: std::path::PathBuf,
|
||||||
|
execution_id: i64,
|
||||||
|
offset: u64,
|
||||||
|
pending_utf8: Vec<u8>,
|
||||||
|
},
|
||||||
|
Tail {
|
||||||
|
full_path: std::path::PathBuf,
|
||||||
|
execution_id: i64,
|
||||||
|
offset: u64,
|
||||||
|
idle_polls: u32,
|
||||||
|
pending_utf8: Vec<u8>,
|
||||||
|
},
|
||||||
|
Finished,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stream stdout/stderr for an execution as SSE.
|
||||||
|
///
|
||||||
|
/// This tails the worker's live log files directly from the shared artifacts
|
||||||
|
/// volume. The file may not exist yet when the worker has not emitted any
|
||||||
|
/// output, so the stream waits briefly for it to appear.
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/api/v1/executions/{id}/logs/{stream}/stream",
|
||||||
|
tag = "executions",
|
||||||
|
params(
|
||||||
|
("id" = i64, Path, description = "Execution ID"),
|
||||||
|
("stream" = String, Path, description = "Log stream name: stdout or stderr"),
|
||||||
|
("token" = String, Query, description = "JWT access token for authentication"),
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "SSE stream of execution log content", content_type = "text/event-stream"),
|
||||||
|
(status = 401, description = "Unauthorized"),
|
||||||
|
(status = 404, description = "Execution not found"),
|
||||||
|
),
|
||||||
|
)]
|
||||||
|
pub async fn stream_execution_log(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
|
headers: HeaderMap,
|
||||||
|
Path((id, stream_name)): Path<(i64, String)>,
|
||||||
|
Query(params): Query<StreamExecutionLogParams>,
|
||||||
|
user: Result<RequireAuth, crate::auth::middleware::AuthError>,
|
||||||
|
) -> Result<Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>>, ApiError> {
|
||||||
|
let authenticated_user =
|
||||||
|
authenticate_execution_log_stream_user(&state, &headers, user, params.token.as_deref())?;
|
||||||
|
validate_execution_log_stream_user(&authenticated_user, id)?;
|
||||||
|
|
||||||
|
let execution = ExecutionRepository::find_by_id(&state.db, id)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| ApiError::NotFound(format!("Execution with ID {} not found", id)))?;
|
||||||
|
authorize_execution_log_stream(&state, &authenticated_user, &execution).await?;
|
||||||
|
|
||||||
|
let stream_name = ExecutionLogStream::parse(&stream_name)?;
|
||||||
|
let full_path = std::path::PathBuf::from(&state.config.artifacts_dir)
|
||||||
|
.join(format!("execution_{}", id))
|
||||||
|
.join(stream_name.file_name());
|
||||||
|
let db = state.db.clone();
|
||||||
|
|
||||||
|
let initial_state = ExecutionLogTailState::WaitingForFile {
|
||||||
|
full_path,
|
||||||
|
execution_id: id,
|
||||||
|
};
|
||||||
|
let start_offset = params.offset.unwrap_or(0);
|
||||||
|
|
||||||
|
let stream = futures::stream::unfold(initial_state, move |state| {
|
||||||
|
let db = db.clone();
|
||||||
|
async move {
|
||||||
|
match state {
|
||||||
|
ExecutionLogTailState::Finished => None,
|
||||||
|
ExecutionLogTailState::WaitingForFile {
|
||||||
|
full_path,
|
||||||
|
execution_id,
|
||||||
|
} => {
|
||||||
|
if full_path.exists() {
|
||||||
|
Some((
|
||||||
|
Ok(Event::default().event("waiting").data("Log file found")),
|
||||||
|
ExecutionLogTailState::SendInitial {
|
||||||
|
full_path,
|
||||||
|
execution_id,
|
||||||
|
offset: start_offset,
|
||||||
|
pending_utf8: Vec::new(),
|
||||||
|
},
|
||||||
|
))
|
||||||
|
} else if execution_log_execution_terminal(&db, execution_id).await {
|
||||||
|
Some((
|
||||||
|
Ok(Event::default().event("done").data("")),
|
||||||
|
ExecutionLogTailState::Finished,
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
tokio::time::sleep(LOG_STREAM_POLL_INTERVAL).await;
|
||||||
|
Some((
|
||||||
|
Ok(Event::default()
|
||||||
|
.event("waiting")
|
||||||
|
.data("Waiting for log output")),
|
||||||
|
ExecutionLogTailState::WaitingForFile {
|
||||||
|
full_path,
|
||||||
|
execution_id,
|
||||||
|
},
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ExecutionLogTailState::SendInitial {
|
||||||
|
full_path,
|
||||||
|
execution_id,
|
||||||
|
offset,
|
||||||
|
pending_utf8,
|
||||||
|
} => {
|
||||||
|
let pending_utf8_on_empty = pending_utf8.clone();
|
||||||
|
match read_log_chunk(
|
||||||
|
&full_path,
|
||||||
|
offset,
|
||||||
|
LOG_STREAM_READ_CHUNK_SIZE,
|
||||||
|
pending_utf8,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Some((content, new_offset, pending_utf8)) => Some((
|
||||||
|
Ok(Event::default()
|
||||||
|
.id(new_offset.to_string())
|
||||||
|
.event("content")
|
||||||
|
.data(content)),
|
||||||
|
ExecutionLogTailState::SendInitial {
|
||||||
|
full_path,
|
||||||
|
execution_id,
|
||||||
|
offset: new_offset,
|
||||||
|
pending_utf8,
|
||||||
|
},
|
||||||
|
)),
|
||||||
|
None => Some((
|
||||||
|
Ok(Event::default().comment("initial-catchup-complete")),
|
||||||
|
ExecutionLogTailState::Tail {
|
||||||
|
full_path,
|
||||||
|
execution_id,
|
||||||
|
offset,
|
||||||
|
idle_polls: 0,
|
||||||
|
pending_utf8: pending_utf8_on_empty,
|
||||||
|
},
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ExecutionLogTailState::Tail {
|
||||||
|
full_path,
|
||||||
|
execution_id,
|
||||||
|
offset,
|
||||||
|
idle_polls,
|
||||||
|
pending_utf8,
|
||||||
|
} => {
|
||||||
|
let pending_utf8_on_empty = pending_utf8.clone();
|
||||||
|
match read_log_chunk(
|
||||||
|
&full_path,
|
||||||
|
offset,
|
||||||
|
LOG_STREAM_READ_CHUNK_SIZE,
|
||||||
|
pending_utf8,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Some((append, new_offset, pending_utf8)) => Some((
|
||||||
|
Ok(Event::default()
|
||||||
|
.id(new_offset.to_string())
|
||||||
|
.event("append")
|
||||||
|
.data(append)),
|
||||||
|
ExecutionLogTailState::Tail {
|
||||||
|
full_path,
|
||||||
|
execution_id,
|
||||||
|
offset: new_offset,
|
||||||
|
idle_polls: 0,
|
||||||
|
pending_utf8,
|
||||||
|
},
|
||||||
|
)),
|
||||||
|
None => {
|
||||||
|
let terminal =
|
||||||
|
execution_log_execution_terminal(&db, execution_id).await;
|
||||||
|
if terminal && idle_polls >= 2 {
|
||||||
|
Some((
|
||||||
|
Ok(Event::default().event("done").data("Execution complete")),
|
||||||
|
ExecutionLogTailState::Finished,
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
tokio::time::sleep(LOG_STREAM_POLL_INTERVAL).await;
|
||||||
|
Some((
|
||||||
|
Ok(Event::default()
|
||||||
|
.event("waiting")
|
||||||
|
.data("Waiting for log output")),
|
||||||
|
ExecutionLogTailState::Tail {
|
||||||
|
full_path,
|
||||||
|
execution_id,
|
||||||
|
offset,
|
||||||
|
idle_polls: idle_polls + 1,
|
||||||
|
pending_utf8: pending_utf8_on_empty,
|
||||||
|
},
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(Sse::new(stream).keep_alive(KeepAlive::default()))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_log_chunk(
|
||||||
|
path: &std::path::Path,
|
||||||
|
offset: u64,
|
||||||
|
max_bytes: usize,
|
||||||
|
mut pending_utf8: Vec<u8>,
|
||||||
|
) -> Option<(String, u64, Vec<u8>)> {
|
||||||
|
use tokio::io::{AsyncReadExt, AsyncSeekExt};
|
||||||
|
|
||||||
|
let mut file = tokio::fs::File::open(path).await.ok()?;
|
||||||
|
let metadata = file.metadata().await.ok()?;
|
||||||
|
if metadata.len() <= offset {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
file.seek(std::io::SeekFrom::Start(offset)).await.ok()?;
|
||||||
|
let bytes_to_read = ((metadata.len() - offset) as usize).min(max_bytes);
|
||||||
|
let mut buf = vec![0u8; bytes_to_read];
|
||||||
|
let read = file.read(&mut buf).await.ok()?;
|
||||||
|
buf.truncate(read);
|
||||||
|
if buf.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
pending_utf8.extend_from_slice(&buf);
|
||||||
|
let (content, pending_utf8) = decode_utf8_chunk(pending_utf8);
|
||||||
|
|
||||||
|
Some((content, offset + read as u64, pending_utf8))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execution_log_execution_terminal(db: &sqlx::PgPool, execution_id: i64) -> bool {
|
||||||
|
match ExecutionRepository::find_by_id(db, execution_id).await {
|
||||||
|
Ok(Some(execution)) => matches!(
|
||||||
|
execution.status,
|
||||||
|
ExecutionStatus::Completed
|
||||||
|
| ExecutionStatus::Failed
|
||||||
|
| ExecutionStatus::Cancelled
|
||||||
|
| ExecutionStatus::Timeout
|
||||||
|
| ExecutionStatus::Abandoned
|
||||||
|
),
|
||||||
|
_ => true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode_utf8_chunk(mut bytes: Vec<u8>) -> (String, Vec<u8>) {
|
||||||
|
match std::str::from_utf8(&bytes) {
|
||||||
|
Ok(valid) => (valid.to_string(), Vec::new()),
|
||||||
|
Err(err) if err.error_len().is_none() => {
|
||||||
|
let pending = bytes.split_off(err.valid_up_to());
|
||||||
|
(String::from_utf8_lossy(&bytes).into_owned(), pending)
|
||||||
|
}
|
||||||
|
Err(_) => (String::from_utf8_lossy(&bytes).into_owned(), Vec::new()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn authorize_execution_log_stream(
|
||||||
|
state: &Arc<AppState>,
|
||||||
|
user: &AuthenticatedUser,
|
||||||
|
execution: &attune_common::models::Execution,
|
||||||
|
) -> Result<(), ApiError> {
|
||||||
|
if user.claims.token_type != TokenType::Access {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let identity_id = user
|
||||||
|
.identity_id()
|
||||||
|
.map_err(|_| ApiError::Unauthorized("Invalid user identity".to_string()))?;
|
||||||
|
let authz = AuthorizationService::new(state.db.clone());
|
||||||
|
let mut ctx = AuthorizationContext::new(identity_id);
|
||||||
|
ctx.target_id = Some(execution.id);
|
||||||
|
ctx.target_ref = Some(execution.action_ref.clone());
|
||||||
|
|
||||||
|
authz
|
||||||
|
.authorize(
|
||||||
|
user,
|
||||||
|
AuthorizationCheck {
|
||||||
|
resource: Resource::Executions,
|
||||||
|
action: Action::Read,
|
||||||
|
context: ctx,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
fn authenticate_execution_log_stream_user(
|
||||||
|
state: &Arc<AppState>,
|
||||||
|
headers: &HeaderMap,
|
||||||
|
user: Result<RequireAuth, crate::auth::middleware::AuthError>,
|
||||||
|
query_token: Option<&str>,
|
||||||
|
) -> Result<AuthenticatedUser, ApiError> {
|
||||||
|
match user {
|
||||||
|
Ok(RequireAuth(user)) => Ok(user),
|
||||||
|
Err(_) => {
|
||||||
|
if let Some(user) = crate::auth::oidc::cookie_authenticated_user(headers, state)? {
|
||||||
|
return Ok(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
let token = query_token.ok_or(ApiError::Unauthorized(
|
||||||
|
"Missing authentication token".to_string(),
|
||||||
|
))?;
|
||||||
|
authenticate_execution_log_stream_query_token(token, &state.jwt_config)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn authenticate_execution_log_stream_query_token(
|
||||||
|
token: &str,
|
||||||
|
jwt_config: &JwtConfig,
|
||||||
|
) -> Result<AuthenticatedUser, ApiError> {
|
||||||
|
let claims = validate_token(token, jwt_config)
|
||||||
|
.map_err(|_| ApiError::Unauthorized("Invalid authentication token".to_string()))?;
|
||||||
|
|
||||||
|
Ok(AuthenticatedUser { claims })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_execution_log_stream_user(
|
||||||
|
user: &AuthenticatedUser,
|
||||||
|
execution_id: i64,
|
||||||
|
) -> Result<(), ApiError> {
|
||||||
|
let claims = &user.claims;
|
||||||
|
|
||||||
|
match claims.token_type {
|
||||||
|
TokenType::Access => Ok(()),
|
||||||
|
TokenType::Execution => validate_execution_token_scope(claims, execution_id),
|
||||||
|
TokenType::Sensor | TokenType::Refresh => Err(ApiError::Unauthorized(
|
||||||
|
"Invalid authentication token".to_string(),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_execution_token_scope(claims: &Claims, execution_id: i64) -> Result<(), ApiError> {
|
||||||
|
if claims.scope.as_deref() != Some("execution") {
|
||||||
|
return Err(ApiError::Unauthorized(
|
||||||
|
"Invalid authentication token".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let token_execution_id = claims
|
||||||
|
.metadata
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|metadata| metadata.get("execution_id"))
|
||||||
|
.and_then(|value| value.as_i64())
|
||||||
|
.ok_or_else(|| ApiError::Unauthorized("Invalid authentication token".to_string()))?;
|
||||||
|
|
||||||
|
if token_execution_id != execution_id {
|
||||||
|
return Err(ApiError::Forbidden(format!(
|
||||||
|
"Execution token is not valid for execution {}",
|
||||||
|
execution_id
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(serde::Deserialize)]
|
||||||
pub struct StreamExecutionParams {
|
pub struct StreamExecutionParams {
|
||||||
pub execution_id: Option<i64>,
|
pub execution_id: Option<i64>,
|
||||||
@@ -950,6 +1337,10 @@ pub fn routes() -> Router<Arc<AppState>> {
|
|||||||
.route("/executions/execute", axum::routing::post(create_execution))
|
.route("/executions/execute", axum::routing::post(create_execution))
|
||||||
.route("/executions/stats", get(get_execution_stats))
|
.route("/executions/stats", get(get_execution_stats))
|
||||||
.route("/executions/stream", get(stream_execution_updates))
|
.route("/executions/stream", get(stream_execution_updates))
|
||||||
|
.route(
|
||||||
|
"/executions/{id}/logs/{stream}/stream",
|
||||||
|
get(stream_execution_log),
|
||||||
|
)
|
||||||
.route("/executions/{id}", get(get_execution))
|
.route("/executions/{id}", get(get_execution))
|
||||||
.route(
|
.route(
|
||||||
"/executions/{id}/cancel",
|
"/executions/{id}/cancel",
|
||||||
@@ -968,10 +1359,26 @@ pub fn routes() -> Router<Arc<AppState>> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use attune_common::auth::jwt::generate_execution_token;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_execution_routes_structure() {
|
fn test_execution_routes_structure() {
|
||||||
// Just verify the router can be constructed
|
// Just verify the router can be constructed
|
||||||
let _router = routes();
|
let _router = routes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn execution_token_scope_must_match_requested_execution() {
|
||||||
|
let jwt_config = JwtConfig {
|
||||||
|
secret: "test_secret_key_for_testing".to_string(),
|
||||||
|
access_token_expiration: 3600,
|
||||||
|
refresh_token_expiration: 604800,
|
||||||
|
};
|
||||||
|
|
||||||
|
let token = generate_execution_token(42, 123, "core.echo", &jwt_config, None).unwrap();
|
||||||
|
|
||||||
|
let user = authenticate_execution_log_stream_query_token(&token, &jwt_config).unwrap();
|
||||||
|
let err = validate_execution_log_stream_user(&user, 456).unwrap_err();
|
||||||
|
assert!(matches!(err, ApiError::Forbidden(_)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,12 +120,16 @@ pub async fn get_key(
|
|||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Key '{}' not found", key_ref)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Key '{}' not found", key_ref)))?;
|
||||||
|
|
||||||
if user.0.claims.token_type == TokenType::Access {
|
// For encrypted keys, track whether this caller is permitted to see the value.
|
||||||
|
// Non-Access tokens (sensor, execution) always get full access.
|
||||||
|
let can_decrypt = if user.0.claims.token_type == TokenType::Access {
|
||||||
let identity_id = user
|
let identity_id = user
|
||||||
.0
|
.0
|
||||||
.identity_id()
|
.identity_id()
|
||||||
.map_err(|_| ApiError::Unauthorized("Invalid user identity".to_string()))?;
|
.map_err(|_| ApiError::Unauthorized("Invalid user identity".to_string()))?;
|
||||||
let authz = AuthorizationService::new(state.db.clone());
|
let authz = AuthorizationService::new(state.db.clone());
|
||||||
|
|
||||||
|
// Basic read check — hide behind 404 to prevent enumeration.
|
||||||
authz
|
authz
|
||||||
.authorize(
|
.authorize(
|
||||||
&user.0,
|
&user.0,
|
||||||
@@ -136,19 +140,43 @@ pub async fn get_key(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
// Hide unauthorized records behind 404 to reduce enumeration leakage.
|
|
||||||
.map_err(|_| ApiError::NotFound(format!("Key '{}' not found", key_ref)))?;
|
.map_err(|_| ApiError::NotFound(format!("Key '{}' not found", key_ref)))?;
|
||||||
}
|
|
||||||
|
|
||||||
// Decrypt value if encrypted
|
// For encrypted keys, separately check Keys::Decrypt.
|
||||||
|
// Failing this is not an error — we just return the value as null.
|
||||||
if key.encrypted {
|
if key.encrypted {
|
||||||
let encryption_key = state
|
authz
|
||||||
|
.authorize(
|
||||||
|
&user.0,
|
||||||
|
AuthorizationCheck {
|
||||||
|
resource: Resource::Keys,
|
||||||
|
action: Action::Decrypt,
|
||||||
|
context: key_authorization_context(identity_id, &key),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.is_ok()
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
};
|
||||||
|
|
||||||
|
// Decrypt value if encrypted and caller has permission.
|
||||||
|
// If they lack Keys::Decrypt, return null rather than the ciphertext.
|
||||||
|
if key.encrypted {
|
||||||
|
if can_decrypt {
|
||||||
|
let encryption_key =
|
||||||
|
state
|
||||||
.config
|
.config
|
||||||
.security
|
.security
|
||||||
.encryption_key
|
.encryption_key
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
ApiError::InternalServerError("Encryption key not configured on server".to_string())
|
ApiError::InternalServerError(
|
||||||
|
"Encryption key not configured on server".to_string(),
|
||||||
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let decrypted_value = attune_common::crypto::decrypt_json(&key.value, encryption_key)
|
let decrypted_value = attune_common::crypto::decrypt_json(&key.value, encryption_key)
|
||||||
@@ -158,6 +186,9 @@ pub async fn get_key(
|
|||||||
})?;
|
})?;
|
||||||
|
|
||||||
key.value = decrypted_value;
|
key.value = decrypted_value;
|
||||||
|
} else {
|
||||||
|
key.value = serde_json::Value::Null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = ApiResponse::new(KeyResponse::from(key));
|
let response = ApiResponse::new(KeyResponse::from(key));
|
||||||
@@ -195,6 +226,7 @@ pub async fn create_key(
|
|||||||
let mut ctx = AuthorizationContext::new(identity_id);
|
let mut ctx = AuthorizationContext::new(identity_id);
|
||||||
ctx.owner_identity_id = request.owner_identity;
|
ctx.owner_identity_id = request.owner_identity;
|
||||||
ctx.owner_type = Some(request.owner_type);
|
ctx.owner_type = Some(request.owner_type);
|
||||||
|
ctx.owner_ref = requested_key_owner_ref(&request);
|
||||||
ctx.encrypted = Some(request.encrypted);
|
ctx.encrypted = Some(request.encrypted);
|
||||||
ctx.target_ref = Some(request.r#ref.clone());
|
ctx.target_ref = Some(request.r#ref.clone());
|
||||||
|
|
||||||
@@ -541,6 +573,38 @@ fn key_authorization_context(identity_id: i64, key: &Key) -> AuthorizationContex
|
|||||||
ctx.target_ref = Some(key.r#ref.clone());
|
ctx.target_ref = Some(key.r#ref.clone());
|
||||||
ctx.owner_identity_id = key.owner_identity;
|
ctx.owner_identity_id = key.owner_identity;
|
||||||
ctx.owner_type = Some(key.owner_type);
|
ctx.owner_type = Some(key.owner_type);
|
||||||
|
ctx.owner_ref = key_owner_ref(
|
||||||
|
key.owner_type,
|
||||||
|
key.owner.as_deref(),
|
||||||
|
key.owner_pack_ref.as_deref(),
|
||||||
|
key.owner_action_ref.as_deref(),
|
||||||
|
key.owner_sensor_ref.as_deref(),
|
||||||
|
);
|
||||||
ctx.encrypted = Some(key.encrypted);
|
ctx.encrypted = Some(key.encrypted);
|
||||||
ctx
|
ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn requested_key_owner_ref(request: &CreateKeyRequest) -> Option<String> {
|
||||||
|
key_owner_ref(
|
||||||
|
request.owner_type,
|
||||||
|
request.owner.as_deref(),
|
||||||
|
request.owner_pack_ref.as_deref(),
|
||||||
|
request.owner_action_ref.as_deref(),
|
||||||
|
request.owner_sensor_ref.as_deref(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn key_owner_ref(
|
||||||
|
owner_type: OwnerType,
|
||||||
|
owner: Option<&str>,
|
||||||
|
owner_pack_ref: Option<&str>,
|
||||||
|
owner_action_ref: Option<&str>,
|
||||||
|
owner_sensor_ref: Option<&str>,
|
||||||
|
) -> Option<String> {
|
||||||
|
match owner_type {
|
||||||
|
OwnerType::Pack => owner_pack_ref.map(str::to_string),
|
||||||
|
OwnerType::Action => owner_action_ref.map(str::to_string),
|
||||||
|
OwnerType::Sensor => owner_sensor_ref.map(str::to_string),
|
||||||
|
_ => owner.map(str::to_string),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
//! API route modules
|
//! API route modules
|
||||||
|
|
||||||
pub mod actions;
|
pub mod actions;
|
||||||
|
pub mod agent;
|
||||||
pub mod analytics;
|
pub mod analytics;
|
||||||
pub mod artifacts;
|
pub mod artifacts;
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
@@ -19,6 +20,7 @@ pub mod webhooks;
|
|||||||
pub mod workflows;
|
pub mod workflows;
|
||||||
|
|
||||||
pub use actions::routes as action_routes;
|
pub use actions::routes as action_routes;
|
||||||
|
pub use agent::routes as agent_routes;
|
||||||
pub use analytics::routes as analytics_routes;
|
pub use analytics::routes as analytics_routes;
|
||||||
pub use artifacts::routes as artifact_routes;
|
pub use artifacts::routes as artifact_routes;
|
||||||
pub use auth::routes as auth_routes;
|
pub use auth::routes as auth_routes;
|
||||||
|
|||||||
@@ -9,12 +9,14 @@ use std::sync::Arc;
|
|||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
|
||||||
use attune_common::{
|
use attune_common::{
|
||||||
models::identity::{Identity, PermissionSet},
|
models::identity::{Identity, IdentityRoleAssignment},
|
||||||
rbac::{Action, AuthorizationContext, Resource},
|
rbac::{Action, AuthorizationContext, Resource},
|
||||||
repositories::{
|
repositories::{
|
||||||
identity::{
|
identity::{
|
||||||
CreateIdentityInput, CreatePermissionAssignmentInput, IdentityRepository,
|
CreateIdentityInput, CreateIdentityRoleAssignmentInput,
|
||||||
PermissionAssignmentRepository, PermissionSetRepository, UpdateIdentityInput,
|
CreatePermissionAssignmentInput, CreatePermissionSetRoleAssignmentInput,
|
||||||
|
IdentityRepository, IdentityRoleAssignmentRepository, PermissionAssignmentRepository,
|
||||||
|
PermissionSetRepository, PermissionSetRoleAssignmentRepository, UpdateIdentityInput,
|
||||||
},
|
},
|
||||||
Create, Delete, FindById, FindByRef, List, Update,
|
Create, Delete, FindById, FindByRef, List, Update,
|
||||||
},
|
},
|
||||||
@@ -26,9 +28,12 @@ use crate::{
|
|||||||
authz::{AuthorizationCheck, AuthorizationService},
|
authz::{AuthorizationCheck, AuthorizationService},
|
||||||
dto::{
|
dto::{
|
||||||
common::{PaginatedResponse, PaginationParams},
|
common::{PaginatedResponse, PaginationParams},
|
||||||
ApiResponse, CreateIdentityRequest, CreatePermissionAssignmentRequest, IdentityResponse,
|
ApiResponse, CreateIdentityRequest, CreateIdentityRoleAssignmentRequest,
|
||||||
IdentitySummary, PermissionAssignmentResponse, PermissionSetQueryParams,
|
CreatePermissionAssignmentRequest, CreatePermissionSetRoleAssignmentRequest,
|
||||||
PermissionSetSummary, SuccessResponse, UpdateIdentityRequest,
|
IdentityResponse, IdentityRoleAssignmentResponse, IdentitySummary,
|
||||||
|
PermissionAssignmentResponse, PermissionSetQueryParams,
|
||||||
|
PermissionSetRoleAssignmentResponse, PermissionSetSummary, SuccessResponse,
|
||||||
|
UpdateIdentityRequest,
|
||||||
},
|
},
|
||||||
middleware::{ApiError, ApiResult},
|
middleware::{ApiError, ApiResult},
|
||||||
state::AppState,
|
state::AppState,
|
||||||
@@ -58,16 +63,22 @@ pub async fn list_identities(
|
|||||||
let page_items = if start >= identities.len() {
|
let page_items = if start >= identities.len() {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
} else {
|
} else {
|
||||||
identities[start..end]
|
identities[start..end].to_vec()
|
||||||
.iter()
|
|
||||||
.cloned()
|
|
||||||
.map(IdentitySummary::from)
|
|
||||||
.collect()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut summaries = Vec::with_capacity(page_items.len());
|
||||||
|
for identity in page_items {
|
||||||
|
let role_assignments =
|
||||||
|
IdentityRoleAssignmentRepository::find_by_identity(&state.db, identity.id).await?;
|
||||||
|
let roles = role_assignments.into_iter().map(|ra| ra.role).collect();
|
||||||
|
let mut summary = IdentitySummary::from(identity);
|
||||||
|
summary.roles = roles;
|
||||||
|
summaries.push(summary);
|
||||||
|
}
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
Json(PaginatedResponse::new(page_items, &query, total)),
|
Json(PaginatedResponse::new(summaries, &query, total)),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,10 +105,42 @@ pub async fn get_identity(
|
|||||||
let identity = IdentityRepository::find_by_id(&state.db, identity_id)
|
let identity = IdentityRepository::find_by_id(&state.db, identity_id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Identity '{}' not found", identity_id)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Identity '{}' not found", identity_id)))?;
|
||||||
|
let roles = IdentityRoleAssignmentRepository::find_by_identity(&state.db, identity_id).await?;
|
||||||
|
let assignments =
|
||||||
|
PermissionAssignmentRepository::find_by_identity(&state.db, identity_id).await?;
|
||||||
|
let permission_sets = PermissionSetRepository::find_by_identity(&state.db, identity_id).await?;
|
||||||
|
let permission_set_refs = permission_sets
|
||||||
|
.into_iter()
|
||||||
|
.map(|ps| (ps.id, ps.r#ref))
|
||||||
|
.collect::<std::collections::HashMap<_, _>>();
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
Json(ApiResponse::new(IdentityResponse::from(identity))),
|
Json(ApiResponse::new(IdentityResponse {
|
||||||
|
id: identity.id,
|
||||||
|
login: identity.login,
|
||||||
|
display_name: identity.display_name,
|
||||||
|
frozen: identity.frozen,
|
||||||
|
attributes: identity.attributes,
|
||||||
|
roles: roles
|
||||||
|
.into_iter()
|
||||||
|
.map(IdentityRoleAssignmentResponse::from)
|
||||||
|
.collect(),
|
||||||
|
direct_permissions: assignments
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|assignment| {
|
||||||
|
permission_set_refs.get(&assignment.permset).cloned().map(
|
||||||
|
|permission_set_ref| PermissionAssignmentResponse {
|
||||||
|
id: assignment.id,
|
||||||
|
identity_id: assignment.identity,
|
||||||
|
permission_set_id: assignment.permset,
|
||||||
|
permission_set_ref,
|
||||||
|
created: assignment.created,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
})),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,6 +223,7 @@ pub async fn update_identity(
|
|||||||
display_name: request.display_name,
|
display_name: request.display_name,
|
||||||
password_hash,
|
password_hash,
|
||||||
attributes: request.attributes,
|
attributes: request.attributes,
|
||||||
|
frozen: request.frozen,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
@@ -257,10 +301,33 @@ pub async fn list_permission_sets(
|
|||||||
permission_sets.retain(|ps| ps.pack_ref.as_deref() == Some(pack_ref.as_str()));
|
permission_sets.retain(|ps| ps.pack_ref.as_deref() == Some(pack_ref.as_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let response: Vec<PermissionSetSummary> = permission_sets
|
let mut response = Vec::with_capacity(permission_sets.len());
|
||||||
|
for permission_set in permission_sets {
|
||||||
|
let permission_set_ref = permission_set.r#ref.clone();
|
||||||
|
let roles = PermissionSetRoleAssignmentRepository::find_by_permission_set(
|
||||||
|
&state.db,
|
||||||
|
permission_set.id,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
response.push(PermissionSetSummary {
|
||||||
|
id: permission_set.id,
|
||||||
|
r#ref: permission_set.r#ref,
|
||||||
|
pack_ref: permission_set.pack_ref,
|
||||||
|
label: permission_set.label,
|
||||||
|
description: permission_set.description,
|
||||||
|
grants: permission_set.grants,
|
||||||
|
roles: roles
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(PermissionSetSummary::from)
|
.map(|assignment| PermissionSetRoleAssignmentResponse {
|
||||||
.collect();
|
id: assignment.id,
|
||||||
|
permission_set_id: assignment.permset,
|
||||||
|
permission_set_ref: Some(permission_set_ref.clone()),
|
||||||
|
role: assignment.role,
|
||||||
|
created: assignment.created,
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Ok((StatusCode::OK, Json(response)))
|
Ok((StatusCode::OK, Json(response)))
|
||||||
}
|
}
|
||||||
@@ -412,6 +479,229 @@ pub async fn delete_permission_assignment(
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/v1/identities/{id}/roles",
|
||||||
|
tag = "permissions",
|
||||||
|
params(
|
||||||
|
("id" = i64, Path, description = "Identity ID")
|
||||||
|
),
|
||||||
|
request_body = CreateIdentityRoleAssignmentRequest,
|
||||||
|
responses(
|
||||||
|
(status = 201, description = "Identity role assignment created", body = inline(ApiResponse<IdentityRoleAssignmentResponse>)),
|
||||||
|
(status = 404, description = "Identity not found")
|
||||||
|
),
|
||||||
|
security(("bearer_auth" = []))
|
||||||
|
)]
|
||||||
|
pub async fn create_identity_role_assignment(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
|
RequireAuth(user): RequireAuth,
|
||||||
|
Path(identity_id): Path<i64>,
|
||||||
|
Json(request): Json<CreateIdentityRoleAssignmentRequest>,
|
||||||
|
) -> ApiResult<impl IntoResponse> {
|
||||||
|
authorize_permissions(&state, &user, Resource::Permissions, Action::Manage).await?;
|
||||||
|
request.validate()?;
|
||||||
|
|
||||||
|
IdentityRepository::find_by_id(&state.db, identity_id)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| ApiError::NotFound(format!("Identity '{}' not found", identity_id)))?;
|
||||||
|
|
||||||
|
let assignment = IdentityRoleAssignmentRepository::create(
|
||||||
|
&state.db,
|
||||||
|
CreateIdentityRoleAssignmentInput {
|
||||||
|
identity: identity_id,
|
||||||
|
role: request.role,
|
||||||
|
source: "manual".to_string(),
|
||||||
|
managed: false,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
StatusCode::CREATED,
|
||||||
|
Json(ApiResponse::new(IdentityRoleAssignmentResponse::from(
|
||||||
|
assignment,
|
||||||
|
))),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
path = "/api/v1/identities/roles/{id}",
|
||||||
|
tag = "permissions",
|
||||||
|
params(
|
||||||
|
("id" = i64, Path, description = "Identity role assignment ID")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Identity role assignment deleted", body = inline(ApiResponse<SuccessResponse>)),
|
||||||
|
(status = 404, description = "Identity role assignment not found")
|
||||||
|
),
|
||||||
|
security(("bearer_auth" = []))
|
||||||
|
)]
|
||||||
|
pub async fn delete_identity_role_assignment(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
|
RequireAuth(user): RequireAuth,
|
||||||
|
Path(assignment_id): Path<i64>,
|
||||||
|
) -> ApiResult<impl IntoResponse> {
|
||||||
|
authorize_permissions(&state, &user, Resource::Permissions, Action::Manage).await?;
|
||||||
|
|
||||||
|
let assignment = IdentityRoleAssignmentRepository::find_by_id(&state.db, assignment_id)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| {
|
||||||
|
ApiError::NotFound(format!(
|
||||||
|
"Identity role assignment '{}' not found",
|
||||||
|
assignment_id
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
if assignment.managed {
|
||||||
|
return Err(ApiError::BadRequest(
|
||||||
|
"Managed role assignments must be updated through the identity provider sync"
|
||||||
|
.to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
IdentityRoleAssignmentRepository::delete(&state.db, assignment_id).await?;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
StatusCode::OK,
|
||||||
|
Json(ApiResponse::new(SuccessResponse::new(
|
||||||
|
"Identity role assignment deleted successfully",
|
||||||
|
))),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/v1/permissions/sets/{id}/roles",
|
||||||
|
tag = "permissions",
|
||||||
|
params(
|
||||||
|
("id" = i64, Path, description = "Permission set ID")
|
||||||
|
),
|
||||||
|
request_body = CreatePermissionSetRoleAssignmentRequest,
|
||||||
|
responses(
|
||||||
|
(status = 201, description = "Permission set role assignment created", body = inline(ApiResponse<PermissionSetRoleAssignmentResponse>)),
|
||||||
|
(status = 404, description = "Permission set not found")
|
||||||
|
),
|
||||||
|
security(("bearer_auth" = []))
|
||||||
|
)]
|
||||||
|
pub async fn create_permission_set_role_assignment(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
|
RequireAuth(user): RequireAuth,
|
||||||
|
Path(permission_set_id): Path<i64>,
|
||||||
|
Json(request): Json<CreatePermissionSetRoleAssignmentRequest>,
|
||||||
|
) -> ApiResult<impl IntoResponse> {
|
||||||
|
authorize_permissions(&state, &user, Resource::Permissions, Action::Manage).await?;
|
||||||
|
request.validate()?;
|
||||||
|
|
||||||
|
let permission_set = PermissionSetRepository::find_by_id(&state.db, permission_set_id)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| {
|
||||||
|
ApiError::NotFound(format!("Permission set '{}' not found", permission_set_id))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let assignment = PermissionSetRoleAssignmentRepository::create(
|
||||||
|
&state.db,
|
||||||
|
CreatePermissionSetRoleAssignmentInput {
|
||||||
|
permset: permission_set_id,
|
||||||
|
role: request.role,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
StatusCode::CREATED,
|
||||||
|
Json(ApiResponse::new(PermissionSetRoleAssignmentResponse {
|
||||||
|
id: assignment.id,
|
||||||
|
permission_set_id: assignment.permset,
|
||||||
|
permission_set_ref: Some(permission_set.r#ref),
|
||||||
|
role: assignment.role,
|
||||||
|
created: assignment.created,
|
||||||
|
})),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
path = "/api/v1/permissions/sets/roles/{id}",
|
||||||
|
tag = "permissions",
|
||||||
|
params(
|
||||||
|
("id" = i64, Path, description = "Permission set role assignment ID")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Permission set role assignment deleted", body = inline(ApiResponse<SuccessResponse>)),
|
||||||
|
(status = 404, description = "Permission set role assignment not found")
|
||||||
|
),
|
||||||
|
security(("bearer_auth" = []))
|
||||||
|
)]
|
||||||
|
pub async fn delete_permission_set_role_assignment(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
|
RequireAuth(user): RequireAuth,
|
||||||
|
Path(assignment_id): Path<i64>,
|
||||||
|
) -> ApiResult<impl IntoResponse> {
|
||||||
|
authorize_permissions(&state, &user, Resource::Permissions, Action::Manage).await?;
|
||||||
|
|
||||||
|
PermissionSetRoleAssignmentRepository::find_by_id(&state.db, assignment_id)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| {
|
||||||
|
ApiError::NotFound(format!(
|
||||||
|
"Permission set role assignment '{}' not found",
|
||||||
|
assignment_id
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
PermissionSetRoleAssignmentRepository::delete(&state.db, assignment_id).await?;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
StatusCode::OK,
|
||||||
|
Json(ApiResponse::new(SuccessResponse::new(
|
||||||
|
"Permission set role assignment deleted successfully",
|
||||||
|
))),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/v1/identities/{id}/freeze",
|
||||||
|
tag = "permissions",
|
||||||
|
params(
|
||||||
|
("id" = i64, Path, description = "Identity ID")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Identity frozen", body = inline(ApiResponse<SuccessResponse>)),
|
||||||
|
(status = 404, description = "Identity not found")
|
||||||
|
),
|
||||||
|
security(("bearer_auth" = []))
|
||||||
|
)]
|
||||||
|
pub async fn freeze_identity(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
|
RequireAuth(user): RequireAuth,
|
||||||
|
Path(identity_id): Path<i64>,
|
||||||
|
) -> ApiResult<impl IntoResponse> {
|
||||||
|
set_identity_frozen(&state, &user, identity_id, true).await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/v1/identities/{id}/unfreeze",
|
||||||
|
tag = "permissions",
|
||||||
|
params(
|
||||||
|
("id" = i64, Path, description = "Identity ID")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Identity unfrozen", body = inline(ApiResponse<SuccessResponse>)),
|
||||||
|
(status = 404, description = "Identity not found")
|
||||||
|
),
|
||||||
|
security(("bearer_auth" = []))
|
||||||
|
)]
|
||||||
|
pub async fn unfreeze_identity(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
|
RequireAuth(user): RequireAuth,
|
||||||
|
Path(identity_id): Path<i64>,
|
||||||
|
) -> ApiResult<impl IntoResponse> {
|
||||||
|
set_identity_frozen(&state, &user, identity_id, false).await
|
||||||
|
}
|
||||||
|
|
||||||
pub fn routes() -> Router<Arc<AppState>> {
|
pub fn routes() -> Router<Arc<AppState>> {
|
||||||
Router::new()
|
Router::new()
|
||||||
.route("/identities", get(list_identities).post(create_identity))
|
.route("/identities", get(list_identities).post(create_identity))
|
||||||
@@ -421,11 +711,29 @@ pub fn routes() -> Router<Arc<AppState>> {
|
|||||||
.put(update_identity)
|
.put(update_identity)
|
||||||
.delete(delete_identity),
|
.delete(delete_identity),
|
||||||
)
|
)
|
||||||
|
.route(
|
||||||
|
"/identities/{id}/roles",
|
||||||
|
post(create_identity_role_assignment),
|
||||||
|
)
|
||||||
.route(
|
.route(
|
||||||
"/identities/{id}/permissions",
|
"/identities/{id}/permissions",
|
||||||
get(list_identity_permissions),
|
get(list_identity_permissions),
|
||||||
)
|
)
|
||||||
|
.route("/identities/{id}/freeze", post(freeze_identity))
|
||||||
|
.route("/identities/{id}/unfreeze", post(unfreeze_identity))
|
||||||
|
.route(
|
||||||
|
"/identities/roles/{id}",
|
||||||
|
delete(delete_identity_role_assignment),
|
||||||
|
)
|
||||||
.route("/permissions/sets", get(list_permission_sets))
|
.route("/permissions/sets", get(list_permission_sets))
|
||||||
|
.route(
|
||||||
|
"/permissions/sets/{id}/roles",
|
||||||
|
post(create_permission_set_role_assignment),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
"/permissions/sets/roles/{id}",
|
||||||
|
delete(delete_permission_set_role_assignment),
|
||||||
|
)
|
||||||
.route(
|
.route(
|
||||||
"/permissions/assignments",
|
"/permissions/assignments",
|
||||||
post(create_permission_assignment),
|
post(create_permission_assignment),
|
||||||
@@ -488,20 +796,82 @@ impl From<Identity> for IdentitySummary {
|
|||||||
id: value.id,
|
id: value.id,
|
||||||
login: value.login,
|
login: value.login,
|
||||||
display_name: value.display_name,
|
display_name: value.display_name,
|
||||||
|
frozen: value.frozen,
|
||||||
attributes: value.attributes,
|
attributes: value.attributes,
|
||||||
|
roles: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<PermissionSet> for PermissionSetSummary {
|
impl From<IdentityRoleAssignment> for IdentityRoleAssignmentResponse {
|
||||||
fn from(value: PermissionSet) -> Self {
|
fn from(value: IdentityRoleAssignment) -> Self {
|
||||||
Self {
|
Self {
|
||||||
id: value.id,
|
id: value.id,
|
||||||
r#ref: value.r#ref,
|
identity_id: value.identity,
|
||||||
pack_ref: value.pack_ref,
|
role: value.role,
|
||||||
label: value.label,
|
source: value.source,
|
||||||
description: value.description,
|
managed: value.managed,
|
||||||
grants: value.grants,
|
created: value.created,
|
||||||
|
updated: value.updated,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<Identity> for IdentityResponse {
|
||||||
|
fn from(value: Identity) -> Self {
|
||||||
|
Self {
|
||||||
|
id: value.id,
|
||||||
|
login: value.login,
|
||||||
|
display_name: value.display_name,
|
||||||
|
frozen: value.frozen,
|
||||||
|
attributes: value.attributes,
|
||||||
|
roles: Vec::new(),
|
||||||
|
direct_permissions: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn set_identity_frozen(
|
||||||
|
state: &Arc<AppState>,
|
||||||
|
user: &crate::auth::middleware::AuthenticatedUser,
|
||||||
|
identity_id: i64,
|
||||||
|
frozen: bool,
|
||||||
|
) -> ApiResult<impl IntoResponse> {
|
||||||
|
authorize_permissions(state, user, Resource::Identities, Action::Update).await?;
|
||||||
|
|
||||||
|
let caller_identity_id = user
|
||||||
|
.identity_id()
|
||||||
|
.map_err(|_| ApiError::Unauthorized("Invalid user identity".to_string()))?;
|
||||||
|
if caller_identity_id == identity_id && frozen {
|
||||||
|
return Err(ApiError::BadRequest(
|
||||||
|
"Refusing to freeze the currently authenticated identity".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
IdentityRepository::find_by_id(&state.db, identity_id)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| ApiError::NotFound(format!("Identity '{}' not found", identity_id)))?;
|
||||||
|
|
||||||
|
IdentityRepository::update(
|
||||||
|
&state.db,
|
||||||
|
identity_id,
|
||||||
|
UpdateIdentityInput {
|
||||||
|
display_name: None,
|
||||||
|
password_hash: None,
|
||||||
|
attributes: None,
|
||||||
|
frozen: Some(frozen),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let message = if frozen {
|
||||||
|
"Identity frozen successfully"
|
||||||
|
} else {
|
||||||
|
"Identity unfrozen successfully"
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
StatusCode::OK,
|
||||||
|
Json(ApiResponse::new(SuccessResponse::new(message))),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ use attune_common::repositories::{
|
|||||||
pack::PackRepository,
|
pack::PackRepository,
|
||||||
rule::{CreateRuleInput, RuleRepository, RuleSearchFilters, UpdateRuleInput},
|
rule::{CreateRuleInput, RuleRepository, RuleSearchFilters, UpdateRuleInput},
|
||||||
trigger::TriggerRepository,
|
trigger::TriggerRepository,
|
||||||
Create, Delete, FindByRef, Update,
|
Create, Delete, FindByRef, Patch, Update,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -474,7 +474,7 @@ pub async fn update_rule(
|
|||||||
// Create update input
|
// Create update input
|
||||||
let update_input = UpdateRuleInput {
|
let update_input = UpdateRuleInput {
|
||||||
label: request.label,
|
label: request.label,
|
||||||
description: request.description,
|
description: request.description.map(Patch::Set),
|
||||||
conditions: request.conditions,
|
conditions: request.conditions,
|
||||||
action_params: request.action_params,
|
action_params: request.action_params,
|
||||||
trigger_params: request.trigger_params,
|
trigger_params: request.trigger_params,
|
||||||
|
|||||||
@@ -176,9 +176,12 @@ pub async fn create_runtime(
|
|||||||
pack_ref,
|
pack_ref,
|
||||||
description: request.description,
|
description: request.description,
|
||||||
name: request.name,
|
name: request.name,
|
||||||
|
aliases: vec![],
|
||||||
distributions: request.distributions,
|
distributions: request.distributions,
|
||||||
installation: request.installation,
|
installation: request.installation,
|
||||||
execution_config: request.execution_config,
|
execution_config: request.execution_config,
|
||||||
|
auto_detected: false,
|
||||||
|
detection_config: serde_json::json!({}),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
@@ -232,6 +235,7 @@ pub async fn update_runtime(
|
|||||||
NullableJsonPatch::Clear => Patch::Clear,
|
NullableJsonPatch::Clear => Patch::Clear,
|
||||||
}),
|
}),
|
||||||
execution_config: request.execution_config,
|
execution_config: request.execution_config,
|
||||||
|
..Default::default()
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|||||||
@@ -724,7 +724,7 @@ pub async fn update_sensor(
|
|||||||
// Create update input
|
// Create update input
|
||||||
let update_input = UpdateSensorInput {
|
let update_input = UpdateSensorInput {
|
||||||
label: request.label,
|
label: request.label,
|
||||||
description: request.description,
|
description: request.description.map(Patch::Set),
|
||||||
entrypoint: request.entrypoint,
|
entrypoint: request.entrypoint,
|
||||||
runtime: None,
|
runtime: None,
|
||||||
runtime_ref: None,
|
runtime_ref: None,
|
||||||
|
|||||||
@@ -20,8 +20,11 @@ use attune_common::{
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use attune_common::rbac::{Action, AuthorizationContext, Resource};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
auth::middleware::RequireAuth,
|
auth::middleware::RequireAuth,
|
||||||
|
authz::{AuthorizationCheck, AuthorizationService},
|
||||||
dto::{
|
dto::{
|
||||||
trigger::TriggerResponse,
|
trigger::TriggerResponse,
|
||||||
webhook::{WebhookReceiverRequest, WebhookReceiverResponse},
|
webhook::{WebhookReceiverRequest, WebhookReceiverResponse},
|
||||||
@@ -170,7 +173,7 @@ fn get_webhook_config_array(
|
|||||||
)]
|
)]
|
||||||
pub async fn enable_webhook(
|
pub async fn enable_webhook(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
Path(trigger_ref): Path<String>,
|
Path(trigger_ref): Path<String>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
// First, find the trigger by ref to get its ID
|
// First, find the trigger by ref to get its ID
|
||||||
@@ -179,6 +182,26 @@ pub async fn enable_webhook(
|
|||||||
.map_err(|e| ApiError::InternalServerError(e.to_string()))?
|
.map_err(|e| ApiError::InternalServerError(e.to_string()))?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Trigger '{}' not found", trigger_ref)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Trigger '{}' not found", trigger_ref)))?;
|
||||||
|
|
||||||
|
if user.claims.token_type == crate::auth::jwt::TokenType::Access {
|
||||||
|
let identity_id = user
|
||||||
|
.identity_id()
|
||||||
|
.map_err(|_| ApiError::Unauthorized("Invalid user identity".to_string()))?;
|
||||||
|
let authz = AuthorizationService::new(state.db.clone());
|
||||||
|
let mut ctx = AuthorizationContext::new(identity_id);
|
||||||
|
ctx.target_ref = Some(trigger.r#ref.clone());
|
||||||
|
ctx.pack_ref = trigger.pack_ref.clone();
|
||||||
|
authz
|
||||||
|
.authorize(
|
||||||
|
&user,
|
||||||
|
AuthorizationCheck {
|
||||||
|
resource: Resource::Triggers,
|
||||||
|
action: Action::Update,
|
||||||
|
context: ctx,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
// Enable webhooks for this trigger
|
// Enable webhooks for this trigger
|
||||||
let _webhook_info = TriggerRepository::enable_webhook(&state.db, trigger.id)
|
let _webhook_info = TriggerRepository::enable_webhook(&state.db, trigger.id)
|
||||||
.await
|
.await
|
||||||
@@ -213,7 +236,7 @@ pub async fn enable_webhook(
|
|||||||
)]
|
)]
|
||||||
pub async fn disable_webhook(
|
pub async fn disable_webhook(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
Path(trigger_ref): Path<String>,
|
Path(trigger_ref): Path<String>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
// First, find the trigger by ref to get its ID
|
// First, find the trigger by ref to get its ID
|
||||||
@@ -222,6 +245,26 @@ pub async fn disable_webhook(
|
|||||||
.map_err(|e| ApiError::InternalServerError(e.to_string()))?
|
.map_err(|e| ApiError::InternalServerError(e.to_string()))?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Trigger '{}' not found", trigger_ref)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Trigger '{}' not found", trigger_ref)))?;
|
||||||
|
|
||||||
|
if user.claims.token_type == crate::auth::jwt::TokenType::Access {
|
||||||
|
let identity_id = user
|
||||||
|
.identity_id()
|
||||||
|
.map_err(|_| ApiError::Unauthorized("Invalid user identity".to_string()))?;
|
||||||
|
let authz = AuthorizationService::new(state.db.clone());
|
||||||
|
let mut ctx = AuthorizationContext::new(identity_id);
|
||||||
|
ctx.target_ref = Some(trigger.r#ref.clone());
|
||||||
|
ctx.pack_ref = trigger.pack_ref.clone();
|
||||||
|
authz
|
||||||
|
.authorize(
|
||||||
|
&user,
|
||||||
|
AuthorizationCheck {
|
||||||
|
resource: Resource::Triggers,
|
||||||
|
action: Action::Update,
|
||||||
|
context: ctx,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
// Disable webhooks for this trigger
|
// Disable webhooks for this trigger
|
||||||
TriggerRepository::disable_webhook(&state.db, trigger.id)
|
TriggerRepository::disable_webhook(&state.db, trigger.id)
|
||||||
.await
|
.await
|
||||||
@@ -257,7 +300,7 @@ pub async fn disable_webhook(
|
|||||||
)]
|
)]
|
||||||
pub async fn regenerate_webhook_key(
|
pub async fn regenerate_webhook_key(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
RequireAuth(_user): RequireAuth,
|
RequireAuth(user): RequireAuth,
|
||||||
Path(trigger_ref): Path<String>,
|
Path(trigger_ref): Path<String>,
|
||||||
) -> ApiResult<impl IntoResponse> {
|
) -> ApiResult<impl IntoResponse> {
|
||||||
// First, find the trigger by ref to get its ID
|
// First, find the trigger by ref to get its ID
|
||||||
@@ -266,6 +309,26 @@ pub async fn regenerate_webhook_key(
|
|||||||
.map_err(|e| ApiError::InternalServerError(e.to_string()))?
|
.map_err(|e| ApiError::InternalServerError(e.to_string()))?
|
||||||
.ok_or_else(|| ApiError::NotFound(format!("Trigger '{}' not found", trigger_ref)))?;
|
.ok_or_else(|| ApiError::NotFound(format!("Trigger '{}' not found", trigger_ref)))?;
|
||||||
|
|
||||||
|
if user.claims.token_type == crate::auth::jwt::TokenType::Access {
|
||||||
|
let identity_id = user
|
||||||
|
.identity_id()
|
||||||
|
.map_err(|_| ApiError::Unauthorized("Invalid user identity".to_string()))?;
|
||||||
|
let authz = AuthorizationService::new(state.db.clone());
|
||||||
|
let mut ctx = AuthorizationContext::new(identity_id);
|
||||||
|
ctx.target_ref = Some(trigger.r#ref.clone());
|
||||||
|
ctx.pack_ref = trigger.pack_ref.clone();
|
||||||
|
authz
|
||||||
|
.authorize(
|
||||||
|
&user,
|
||||||
|
AuthorizationCheck {
|
||||||
|
resource: Resource::Triggers,
|
||||||
|
action: Action::Update,
|
||||||
|
context: ctx,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if webhooks are enabled
|
// Check if webhooks are enabled
|
||||||
if !trigger.webhook_enabled {
|
if !trigger.webhook_enabled {
|
||||||
return Err(ApiError::BadRequest(
|
return Err(ApiError::BadRequest(
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ use attune_common::repositories::{
|
|||||||
CreateWorkflowDefinitionInput, UpdateWorkflowDefinitionInput, WorkflowDefinitionRepository,
|
CreateWorkflowDefinitionInput, UpdateWorkflowDefinitionInput, WorkflowDefinitionRepository,
|
||||||
WorkflowSearchFilters,
|
WorkflowSearchFilters,
|
||||||
},
|
},
|
||||||
Create, Delete, FindByRef, Update,
|
Create, Delete, FindByRef, Patch, Update,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -217,7 +217,7 @@ pub async fn create_workflow(
|
|||||||
pack.id,
|
pack.id,
|
||||||
&pack.r#ref,
|
&pack.r#ref,
|
||||||
&request.label,
|
&request.label,
|
||||||
&request.description.clone().unwrap_or_default(),
|
request.description.as_deref(),
|
||||||
"workflow",
|
"workflow",
|
||||||
request.param_schema.as_ref(),
|
request.param_schema.as_ref(),
|
||||||
request.out_schema.as_ref(),
|
request.out_schema.as_ref(),
|
||||||
@@ -416,7 +416,7 @@ pub async fn save_workflow_file(
|
|||||||
pack.id,
|
pack.id,
|
||||||
&pack.r#ref,
|
&pack.r#ref,
|
||||||
&request.label,
|
&request.label,
|
||||||
&request.description.clone().unwrap_or_default(),
|
request.description.as_deref(),
|
||||||
&entrypoint,
|
&entrypoint,
|
||||||
request.param_schema.as_ref(),
|
request.param_schema.as_ref(),
|
||||||
request.out_schema.as_ref(),
|
request.out_schema.as_ref(),
|
||||||
@@ -499,7 +499,7 @@ pub async fn update_workflow_file(
|
|||||||
pack.id,
|
pack.id,
|
||||||
&pack.r#ref,
|
&pack.r#ref,
|
||||||
&request.label,
|
&request.label,
|
||||||
&request.description.unwrap_or_default(),
|
request.description.as_deref(),
|
||||||
&entrypoint,
|
&entrypoint,
|
||||||
request.param_schema.as_ref(),
|
request.param_schema.as_ref(),
|
||||||
request.out_schema.as_ref(),
|
request.out_schema.as_ref(),
|
||||||
@@ -702,7 +702,7 @@ async fn create_companion_action(
|
|||||||
pack_id: i64,
|
pack_id: i64,
|
||||||
pack_ref: &str,
|
pack_ref: &str,
|
||||||
label: &str,
|
label: &str,
|
||||||
description: &str,
|
description: Option<&str>,
|
||||||
entrypoint: &str,
|
entrypoint: &str,
|
||||||
param_schema: Option<&serde_json::Value>,
|
param_schema: Option<&serde_json::Value>,
|
||||||
out_schema: Option<&serde_json::Value>,
|
out_schema: Option<&serde_json::Value>,
|
||||||
@@ -713,7 +713,7 @@ async fn create_companion_action(
|
|||||||
pack: pack_id,
|
pack: pack_id,
|
||||||
pack_ref: pack_ref.to_string(),
|
pack_ref: pack_ref.to_string(),
|
||||||
label: label.to_string(),
|
label: label.to_string(),
|
||||||
description: description.to_string(),
|
description: description.map(|s| s.to_string()),
|
||||||
entrypoint: entrypoint.to_string(),
|
entrypoint: entrypoint.to_string(),
|
||||||
runtime: None,
|
runtime: None,
|
||||||
runtime_version_constraint: None,
|
runtime_version_constraint: None,
|
||||||
@@ -787,7 +787,7 @@ async fn update_companion_action(
|
|||||||
if let Some(action) = existing_action {
|
if let Some(action) = existing_action {
|
||||||
let update_input = UpdateActionInput {
|
let update_input = UpdateActionInput {
|
||||||
label: label.map(|s| s.to_string()),
|
label: label.map(|s| s.to_string()),
|
||||||
description: description.map(|s| s.to_string()),
|
description: description.map(|s| Patch::Set(s.to_string())),
|
||||||
entrypoint: None,
|
entrypoint: None,
|
||||||
runtime: None,
|
runtime: None,
|
||||||
runtime_version_constraint: None,
|
runtime_version_constraint: None,
|
||||||
@@ -838,7 +838,7 @@ async fn ensure_companion_action(
|
|||||||
pack_id: i64,
|
pack_id: i64,
|
||||||
pack_ref: &str,
|
pack_ref: &str,
|
||||||
label: &str,
|
label: &str,
|
||||||
description: &str,
|
description: Option<&str>,
|
||||||
entrypoint: &str,
|
entrypoint: &str,
|
||||||
param_schema: Option<&serde_json::Value>,
|
param_schema: Option<&serde_json::Value>,
|
||||||
out_schema: Option<&serde_json::Value>,
|
out_schema: Option<&serde_json::Value>,
|
||||||
@@ -853,7 +853,10 @@ async fn ensure_companion_action(
|
|||||||
// Update existing companion action
|
// Update existing companion action
|
||||||
let update_input = UpdateActionInput {
|
let update_input = UpdateActionInput {
|
||||||
label: Some(label.to_string()),
|
label: Some(label.to_string()),
|
||||||
description: Some(description.to_string()),
|
description: Some(match description {
|
||||||
|
Some(description) => Patch::Set(description.to_string()),
|
||||||
|
None => Patch::Clear,
|
||||||
|
}),
|
||||||
entrypoint: Some(entrypoint.to_string()),
|
entrypoint: Some(entrypoint.to_string()),
|
||||||
runtime: None,
|
runtime: None,
|
||||||
runtime_version_constraint: None,
|
runtime_version_constraint: None,
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ impl Server {
|
|||||||
.merge(routes::history_routes())
|
.merge(routes::history_routes())
|
||||||
.merge(routes::analytics_routes())
|
.merge(routes::analytics_routes())
|
||||||
.merge(routes::artifact_routes())
|
.merge(routes::artifact_routes())
|
||||||
|
.merge(routes::agent_routes())
|
||||||
.with_state(self.state.clone());
|
.with_state(self.state.clone());
|
||||||
|
|
||||||
// Auth routes at root level (not versioned for frontend compatibility)
|
// Auth routes at root level (not versioned for frontend compatibility)
|
||||||
|
|||||||
@@ -362,7 +362,7 @@ mod tests {
|
|||||||
pack: 1,
|
pack: 1,
|
||||||
pack_ref: "test".to_string(),
|
pack_ref: "test".to_string(),
|
||||||
label: "Test Action".to_string(),
|
label: "Test Action".to_string(),
|
||||||
description: "Test action".to_string(),
|
description: Some("Test action".to_string()),
|
||||||
entrypoint: "test.sh".to_string(),
|
entrypoint: "test.sh".to_string(),
|
||||||
runtime: Some(1),
|
runtime: Some(1),
|
||||||
runtime_version_constraint: None,
|
runtime_version_constraint: None,
|
||||||
|
|||||||
138
crates/api/tests/agent_tests.rs
Normal file
138
crates/api/tests/agent_tests.rs
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
//! Integration tests for agent binary distribution endpoints
|
||||||
|
//!
|
||||||
|
//! The agent endpoints (`/api/v1/agent/binary` and `/api/v1/agent/info`) are
|
||||||
|
//! intentionally unauthenticated — the agent needs to download its binary
|
||||||
|
//! before it has JWT credentials. An optional `bootstrap_token` can restrict
|
||||||
|
//! access, but that is validated inside the handler, not via RequireAuth
|
||||||
|
//! middleware.
|
||||||
|
//!
|
||||||
|
//! The test configuration (`config.test.yaml`) does NOT include an `agent`
|
||||||
|
//! section, so both endpoints return 503 Service Unavailable. This is the
|
||||||
|
//! correct behaviour: the endpoints are reachable (no 401/404 from middleware)
|
||||||
|
//! but the feature is not configured.
|
||||||
|
|
||||||
|
use axum::http::StatusCode;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
mod helpers;
|
||||||
|
use helpers::TestContext;
|
||||||
|
|
||||||
|
// ── /api/v1/agent/info ──────────────────────────────────────────────
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "integration test — requires database"]
|
||||||
|
async fn test_agent_info_not_configured() {
|
||||||
|
let ctx = TestContext::new()
|
||||||
|
.await
|
||||||
|
.expect("Failed to create test context");
|
||||||
|
|
||||||
|
let response = ctx
|
||||||
|
.get("/api/v1/agent/info", None)
|
||||||
|
.await
|
||||||
|
.expect("Failed to make request");
|
||||||
|
|
||||||
|
// Agent config is not set in config.test.yaml, so the handler returns 503.
|
||||||
|
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
|
||||||
|
|
||||||
|
let body: serde_json::Value = response.json().await.expect("Failed to parse JSON");
|
||||||
|
assert_eq!(body["error"], "Not configured");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "integration test — requires database"]
|
||||||
|
async fn test_agent_info_no_auth_required() {
|
||||||
|
// Verify that the endpoint is reachable WITHOUT any JWT token.
|
||||||
|
// If RequireAuth middleware were applied, this would return 401.
|
||||||
|
// Instead we expect 503 (not configured) — proving the endpoint
|
||||||
|
// is publicly accessible.
|
||||||
|
let ctx = TestContext::new()
|
||||||
|
.await
|
||||||
|
.expect("Failed to create test context");
|
||||||
|
|
||||||
|
let response = ctx
|
||||||
|
.get("/api/v1/agent/info", None)
|
||||||
|
.await
|
||||||
|
.expect("Failed to make request");
|
||||||
|
|
||||||
|
// Must NOT be 401 Unauthorized — the endpoint has no auth middleware.
|
||||||
|
assert_ne!(
|
||||||
|
response.status(),
|
||||||
|
StatusCode::UNAUTHORIZED,
|
||||||
|
"agent/info should not require authentication"
|
||||||
|
);
|
||||||
|
// Should be 503 because agent config is absent.
|
||||||
|
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── /api/v1/agent/binary ────────────────────────────────────────────
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "integration test — requires database"]
|
||||||
|
async fn test_agent_binary_not_configured() {
|
||||||
|
let ctx = TestContext::new()
|
||||||
|
.await
|
||||||
|
.expect("Failed to create test context");
|
||||||
|
|
||||||
|
let response = ctx
|
||||||
|
.get("/api/v1/agent/binary", None)
|
||||||
|
.await
|
||||||
|
.expect("Failed to make request");
|
||||||
|
|
||||||
|
// Agent config is not set in config.test.yaml, so the handler returns 503.
|
||||||
|
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
|
||||||
|
|
||||||
|
let body: serde_json::Value = response.json().await.expect("Failed to parse JSON");
|
||||||
|
assert_eq!(body["error"], "Not configured");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "integration test — requires database"]
|
||||||
|
async fn test_agent_binary_no_auth_required() {
|
||||||
|
// Same reasoning as test_agent_info_no_auth_required: the binary
|
||||||
|
// download endpoint must be publicly accessible (no RequireAuth).
|
||||||
|
// When no bootstrap_token is configured, any caller can reach the
|
||||||
|
// handler. We still get 503 because the agent feature itself is
|
||||||
|
// not configured in the test environment.
|
||||||
|
let ctx = TestContext::new()
|
||||||
|
.await
|
||||||
|
.expect("Failed to create test context");
|
||||||
|
|
||||||
|
let response = ctx
|
||||||
|
.get("/api/v1/agent/binary", None)
|
||||||
|
.await
|
||||||
|
.expect("Failed to make request");
|
||||||
|
|
||||||
|
// Must NOT be 401 Unauthorized — the endpoint has no auth middleware.
|
||||||
|
assert_ne!(
|
||||||
|
response.status(),
|
||||||
|
StatusCode::UNAUTHORIZED,
|
||||||
|
"agent/binary should not require authentication when no bootstrap_token is configured"
|
||||||
|
);
|
||||||
|
// Should be 503 because agent config is absent.
|
||||||
|
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "integration test — requires database"]
|
||||||
|
async fn test_agent_binary_invalid_arch() {
|
||||||
|
// Architecture validation (`validate_arch`) rejects unsupported values
|
||||||
|
// with 400 Bad Request. However, in the handler the execution order is:
|
||||||
|
// 1. validate_token (passes — no bootstrap_token configured)
|
||||||
|
// 2. check agent config (fails with 503 — not configured)
|
||||||
|
// 3. validate_arch (never reached)
|
||||||
|
//
|
||||||
|
// So even with an invalid arch like "mips", we get 503 from the config
|
||||||
|
// check before the arch is ever validated. The arch validation is covered
|
||||||
|
// by unit tests in routes/agent.rs instead.
|
||||||
|
let ctx = TestContext::new()
|
||||||
|
.await
|
||||||
|
.expect("Failed to create test context");
|
||||||
|
|
||||||
|
let response = ctx
|
||||||
|
.get("/api/v1/agent/binary?arch=mips", None)
|
||||||
|
.await
|
||||||
|
.expect("Failed to make request");
|
||||||
|
|
||||||
|
// 503 from the agent-config-not-set check, NOT 400 from arch validation.
|
||||||
|
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
|
||||||
|
}
|
||||||
@@ -241,6 +241,7 @@ impl TestContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create and authenticate a test user
|
/// Create and authenticate a test user
|
||||||
|
#[allow(dead_code)]
|
||||||
pub async fn with_auth(mut self) -> Result<Self> {
|
pub async fn with_auth(mut self) -> Result<Self> {
|
||||||
// Generate unique username to avoid conflicts in parallel tests
|
// Generate unique username to avoid conflicts in parallel tests
|
||||||
let unique_id = uuid::Uuid::new_v4().to_string().replace("-", "")[..8].to_string();
|
let unique_id = uuid::Uuid::new_v4().to_string().replace("-", "")[..8].to_string();
|
||||||
@@ -394,6 +395,7 @@ impl TestContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get authenticated token
|
/// Get authenticated token
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn token(&self) -> Option<&str> {
|
pub fn token(&self) -> Option<&str> {
|
||||||
self.token.as_deref()
|
self.token.as_deref()
|
||||||
}
|
}
|
||||||
@@ -495,7 +497,7 @@ pub async fn create_test_action(pool: &PgPool, pack_id: i64, ref_name: &str) ->
|
|||||||
pack: pack_id,
|
pack: pack_id,
|
||||||
pack_ref: format!("pack_{}", pack_id),
|
pack_ref: format!("pack_{}", pack_id),
|
||||||
label: format!("Test Action {}", ref_name),
|
label: format!("Test Action {}", ref_name),
|
||||||
description: format!("Test action for {}", ref_name),
|
description: Some(format!("Test action for {}", ref_name)),
|
||||||
entrypoint: "main.py".to_string(),
|
entrypoint: "main.py".to_string(),
|
||||||
runtime: None,
|
runtime: None,
|
||||||
runtime_version_constraint: None,
|
runtime_version_constraint: None,
|
||||||
|
|||||||
276
crates/api/tests/rbac_scoped_resources_api_tests.rs
Normal file
276
crates/api/tests/rbac_scoped_resources_api_tests.rs
Normal file
@@ -0,0 +1,276 @@
|
|||||||
|
use axum::http::StatusCode;
|
||||||
|
use helpers::*;
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
use attune_common::{
|
||||||
|
models::enums::{ArtifactType, ArtifactVisibility, OwnerType, RetentionPolicyType},
|
||||||
|
repositories::{
|
||||||
|
artifact::{ArtifactRepository, CreateArtifactInput},
|
||||||
|
identity::{
|
||||||
|
CreatePermissionAssignmentInput, CreatePermissionSetInput, IdentityRepository,
|
||||||
|
PermissionAssignmentRepository, PermissionSetRepository,
|
||||||
|
},
|
||||||
|
key::{CreateKeyInput, KeyRepository},
|
||||||
|
Create,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
mod helpers;
|
||||||
|
|
||||||
|
async fn register_scoped_user(
|
||||||
|
ctx: &TestContext,
|
||||||
|
login: &str,
|
||||||
|
grants: serde_json::Value,
|
||||||
|
) -> Result<String> {
|
||||||
|
let response = ctx
|
||||||
|
.post(
|
||||||
|
"/auth/register",
|
||||||
|
json!({
|
||||||
|
"login": login,
|
||||||
|
"password": "TestPassword123!",
|
||||||
|
"display_name": format!("Scoped User {}", login),
|
||||||
|
}),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert_eq!(response.status(), StatusCode::CREATED);
|
||||||
|
let body: serde_json::Value = response.json().await?;
|
||||||
|
let token = body["data"]["access_token"]
|
||||||
|
.as_str()
|
||||||
|
.expect("missing access token")
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let identity = IdentityRepository::find_by_login(&ctx.pool, login)
|
||||||
|
.await?
|
||||||
|
.expect("registered identity should exist");
|
||||||
|
|
||||||
|
let permset = PermissionSetRepository::create(
|
||||||
|
&ctx.pool,
|
||||||
|
CreatePermissionSetInput {
|
||||||
|
r#ref: format!("test.scoped_{}", uuid::Uuid::new_v4().simple()),
|
||||||
|
pack: None,
|
||||||
|
pack_ref: None,
|
||||||
|
label: Some("Scoped Test Permission Set".to_string()),
|
||||||
|
description: Some("Scoped test grants".to_string()),
|
||||||
|
grants,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
PermissionAssignmentRepository::create(
|
||||||
|
&ctx.pool,
|
||||||
|
CreatePermissionAssignmentInput {
|
||||||
|
identity: identity.id,
|
||||||
|
permset: permset.id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(token)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "integration test — requires database"]
|
||||||
|
async fn test_pack_scoped_key_permissions_enforce_owner_refs() {
|
||||||
|
let ctx = TestContext::new()
|
||||||
|
.await
|
||||||
|
.expect("Failed to create test context");
|
||||||
|
|
||||||
|
let token = register_scoped_user(
|
||||||
|
&ctx,
|
||||||
|
&format!("scoped_keys_{}", uuid::Uuid::new_v4().simple()),
|
||||||
|
json!([
|
||||||
|
{
|
||||||
|
"resource": "keys",
|
||||||
|
"actions": ["read"],
|
||||||
|
"constraints": {
|
||||||
|
"owner_types": ["pack"],
|
||||||
|
"owner_refs": ["python_example"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("Failed to register scoped user");
|
||||||
|
|
||||||
|
KeyRepository::create(
|
||||||
|
&ctx.pool,
|
||||||
|
CreateKeyInput {
|
||||||
|
r#ref: format!("python_example_key_{}", uuid::Uuid::new_v4().simple()),
|
||||||
|
owner_type: OwnerType::Pack,
|
||||||
|
owner: Some("python_example".to_string()),
|
||||||
|
owner_identity: None,
|
||||||
|
owner_pack: None,
|
||||||
|
owner_pack_ref: Some("python_example".to_string()),
|
||||||
|
owner_action: None,
|
||||||
|
owner_action_ref: None,
|
||||||
|
owner_sensor: None,
|
||||||
|
owner_sensor_ref: None,
|
||||||
|
name: "Python Example Key".to_string(),
|
||||||
|
encrypted: false,
|
||||||
|
encryption_key_hash: None,
|
||||||
|
value: json!("allowed"),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("Failed to create scoped key");
|
||||||
|
|
||||||
|
let blocked_key = KeyRepository::create(
|
||||||
|
&ctx.pool,
|
||||||
|
CreateKeyInput {
|
||||||
|
r#ref: format!("other_pack_key_{}", uuid::Uuid::new_v4().simple()),
|
||||||
|
owner_type: OwnerType::Pack,
|
||||||
|
owner: Some("other_pack".to_string()),
|
||||||
|
owner_identity: None,
|
||||||
|
owner_pack: None,
|
||||||
|
owner_pack_ref: Some("other_pack".to_string()),
|
||||||
|
owner_action: None,
|
||||||
|
owner_action_ref: None,
|
||||||
|
owner_sensor: None,
|
||||||
|
owner_sensor_ref: None,
|
||||||
|
name: "Other Pack Key".to_string(),
|
||||||
|
encrypted: false,
|
||||||
|
encryption_key_hash: None,
|
||||||
|
value: json!("blocked"),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("Failed to create blocked key");
|
||||||
|
|
||||||
|
let allowed_list = ctx
|
||||||
|
.get("/api/v1/keys", Some(&token))
|
||||||
|
.await
|
||||||
|
.expect("Failed to list keys");
|
||||||
|
assert_eq!(allowed_list.status(), StatusCode::OK);
|
||||||
|
let allowed_body: serde_json::Value = allowed_list.json().await.expect("Invalid key list");
|
||||||
|
assert_eq!(
|
||||||
|
allowed_body["data"]
|
||||||
|
.as_array()
|
||||||
|
.expect("expected list")
|
||||||
|
.len(),
|
||||||
|
1
|
||||||
|
);
|
||||||
|
assert_eq!(allowed_body["data"][0]["owner"], "python_example");
|
||||||
|
|
||||||
|
let blocked_get = ctx
|
||||||
|
.get(&format!("/api/v1/keys/{}", blocked_key.r#ref), Some(&token))
|
||||||
|
.await
|
||||||
|
.expect("Failed to fetch blocked key");
|
||||||
|
assert_eq!(blocked_get.status(), StatusCode::NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "integration test — requires database"]
|
||||||
|
async fn test_pack_scoped_artifact_permissions_enforce_owner_refs() {
|
||||||
|
let ctx = TestContext::new()
|
||||||
|
.await
|
||||||
|
.expect("Failed to create test context");
|
||||||
|
|
||||||
|
let token = register_scoped_user(
|
||||||
|
&ctx,
|
||||||
|
&format!("scoped_artifacts_{}", uuid::Uuid::new_v4().simple()),
|
||||||
|
json!([
|
||||||
|
{
|
||||||
|
"resource": "artifacts",
|
||||||
|
"actions": ["read", "create"],
|
||||||
|
"constraints": {
|
||||||
|
"owner_types": ["pack"],
|
||||||
|
"owner_refs": ["python_example"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("Failed to register scoped user");
|
||||||
|
|
||||||
|
let allowed_artifact = ArtifactRepository::create(
|
||||||
|
&ctx.pool,
|
||||||
|
CreateArtifactInput {
|
||||||
|
r#ref: format!("python_example.allowed_{}", uuid::Uuid::new_v4().simple()),
|
||||||
|
scope: OwnerType::Pack,
|
||||||
|
owner: "python_example".to_string(),
|
||||||
|
r#type: ArtifactType::FileText,
|
||||||
|
visibility: ArtifactVisibility::Private,
|
||||||
|
retention_policy: RetentionPolicyType::Versions,
|
||||||
|
retention_limit: 5,
|
||||||
|
name: Some("Allowed Artifact".to_string()),
|
||||||
|
description: None,
|
||||||
|
content_type: Some("text/plain".to_string()),
|
||||||
|
execution: None,
|
||||||
|
data: None,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("Failed to create allowed artifact");
|
||||||
|
|
||||||
|
let blocked_artifact = ArtifactRepository::create(
|
||||||
|
&ctx.pool,
|
||||||
|
CreateArtifactInput {
|
||||||
|
r#ref: format!("other_pack.blocked_{}", uuid::Uuid::new_v4().simple()),
|
||||||
|
scope: OwnerType::Pack,
|
||||||
|
owner: "other_pack".to_string(),
|
||||||
|
r#type: ArtifactType::FileText,
|
||||||
|
visibility: ArtifactVisibility::Private,
|
||||||
|
retention_policy: RetentionPolicyType::Versions,
|
||||||
|
retention_limit: 5,
|
||||||
|
name: Some("Blocked Artifact".to_string()),
|
||||||
|
description: None,
|
||||||
|
content_type: Some("text/plain".to_string()),
|
||||||
|
execution: None,
|
||||||
|
data: None,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("Failed to create blocked artifact");
|
||||||
|
|
||||||
|
let allowed_get = ctx
|
||||||
|
.get(
|
||||||
|
&format!("/api/v1/artifacts/{}", allowed_artifact.id),
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("Failed to fetch allowed artifact");
|
||||||
|
assert_eq!(allowed_get.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let blocked_get = ctx
|
||||||
|
.get(
|
||||||
|
&format!("/api/v1/artifacts/{}", blocked_artifact.id),
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("Failed to fetch blocked artifact");
|
||||||
|
assert_eq!(blocked_get.status(), StatusCode::NOT_FOUND);
|
||||||
|
|
||||||
|
let create_allowed = ctx
|
||||||
|
.post(
|
||||||
|
"/api/v1/artifacts",
|
||||||
|
json!({
|
||||||
|
"ref": format!("python_example.created_{}", uuid::Uuid::new_v4().simple()),
|
||||||
|
"scope": "pack",
|
||||||
|
"owner": "python_example",
|
||||||
|
"type": "file_text",
|
||||||
|
"name": "Created Artifact"
|
||||||
|
}),
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("Failed to create allowed artifact");
|
||||||
|
assert_eq!(create_allowed.status(), StatusCode::CREATED);
|
||||||
|
|
||||||
|
let create_blocked = ctx
|
||||||
|
.post(
|
||||||
|
"/api/v1/artifacts",
|
||||||
|
json!({
|
||||||
|
"ref": format!("other_pack.created_{}", uuid::Uuid::new_v4().simple()),
|
||||||
|
"scope": "pack",
|
||||||
|
"owner": "other_pack",
|
||||||
|
"type": "file_text",
|
||||||
|
"name": "Blocked Artifact"
|
||||||
|
}),
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("Failed to create blocked artifact");
|
||||||
|
assert_eq!(create_blocked.status(), StatusCode::FORBIDDEN);
|
||||||
|
}
|
||||||
@@ -52,7 +52,7 @@ async fn setup_test_pack_and_action(pool: &PgPool) -> Result<(Pack, Action)> {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Action".to_string(),
|
label: "Test Action".to_string(),
|
||||||
description: "Test action for SSE tests".to_string(),
|
description: Some("Test action for SSE tests".to_string()),
|
||||||
entrypoint: "test.sh".to_string(),
|
entrypoint: "test.sh".to_string(),
|
||||||
runtime: None,
|
runtime: None,
|
||||||
runtime_version_constraint: None,
|
runtime_version_constraint: None,
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ clap = { workspace = true, features = ["derive", "env", "string"] }
|
|||||||
|
|
||||||
# HTTP client
|
# HTTP client
|
||||||
reqwest = { workspace = true, features = ["multipart", "stream"] }
|
reqwest = { workspace = true, features = ["multipart", "stream"] }
|
||||||
|
reqwest-eventsource = { workspace = true }
|
||||||
|
|
||||||
# Serialization
|
# Serialization
|
||||||
serde = { workspace = true }
|
serde = { workspace = true }
|
||||||
|
|||||||
@@ -21,6 +21,11 @@ pub struct ApiResponse<T> {
|
|||||||
pub data: T,
|
pub data: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, serde::Deserialize)]
|
||||||
|
struct PaginatedResponse<T> {
|
||||||
|
data: Vec<T>,
|
||||||
|
}
|
||||||
|
|
||||||
/// API error response
|
/// API error response
|
||||||
#[derive(Debug, serde::Deserialize)]
|
#[derive(Debug, serde::Deserialize)]
|
||||||
pub struct ApiError {
|
pub struct ApiError {
|
||||||
@@ -55,6 +60,10 @@ impl ApiClient {
|
|||||||
&self.base_url
|
&self.base_url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn auth_token(&self) -> Option<&str> {
|
||||||
|
self.auth_token.as_deref()
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub fn new(base_url: String, auth_token: Option<String>) -> Self {
|
pub fn new(base_url: String, auth_token: Option<String>) -> Self {
|
||||||
let client = HttpClient::builder()
|
let client = HttpClient::builder()
|
||||||
@@ -255,6 +264,31 @@ impl ApiClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn handle_paginated_response<T: DeserializeOwned>(
|
||||||
|
&self,
|
||||||
|
response: reqwest::Response,
|
||||||
|
) -> Result<Vec<T>> {
|
||||||
|
let status = response.status();
|
||||||
|
if status.is_success() {
|
||||||
|
let paginated: PaginatedResponse<T> = response
|
||||||
|
.json()
|
||||||
|
.await
|
||||||
|
.context("Failed to parse paginated API response")?;
|
||||||
|
Ok(paginated.data)
|
||||||
|
} else {
|
||||||
|
let error_text = response
|
||||||
|
.text()
|
||||||
|
.await
|
||||||
|
.unwrap_or_else(|_| "Unknown error".to_string());
|
||||||
|
|
||||||
|
if let Ok(api_error) = serde_json::from_str::<ApiError>(&error_text) {
|
||||||
|
anyhow::bail!("API error ({}): {}", status, api_error.error);
|
||||||
|
} else {
|
||||||
|
anyhow::bail!("API error ({}): {}", status, error_text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Handle a response where we only care about success/failure, not a body.
|
/// Handle a response where we only care about success/failure, not a body.
|
||||||
async fn handle_empty_response(&self, response: reqwest::Response) -> Result<()> {
|
async fn handle_empty_response(&self, response: reqwest::Response) -> Result<()> {
|
||||||
let status = response.status();
|
let status = response.status();
|
||||||
@@ -281,6 +315,25 @@ impl ApiClient {
|
|||||||
self.execute_json::<T, ()>(Method::GET, path, None).await
|
self.execute_json::<T, ()>(Method::GET, path, None).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_paginated<T: DeserializeOwned>(&mut self, path: &str) -> Result<Vec<T>> {
|
||||||
|
let req = self.build_request(Method::GET, path);
|
||||||
|
let response = req.send().await.context("Failed to send request to API")?;
|
||||||
|
|
||||||
|
if response.status() == StatusCode::UNAUTHORIZED
|
||||||
|
&& self.refresh_token.is_some()
|
||||||
|
&& self.refresh_auth_token().await?
|
||||||
|
{
|
||||||
|
let req = self.build_request(Method::GET, path);
|
||||||
|
let response = req
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.context("Failed to send request to API (retry)")?;
|
||||||
|
return self.handle_paginated_response(response).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.handle_paginated_response(response).await
|
||||||
|
}
|
||||||
|
|
||||||
/// GET request with query parameters (query string must be in path)
|
/// GET request with query parameters (query string must be in path)
|
||||||
///
|
///
|
||||||
/// Part of REST client API - reserved for future advanced filtering/search features.
|
/// Part of REST client API - reserved for future advanced filtering/search features.
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use std::collections::HashMap;
|
|||||||
use crate::client::ApiClient;
|
use crate::client::ApiClient;
|
||||||
use crate::config::CliConfig;
|
use crate::config::CliConfig;
|
||||||
use crate::output::{self, OutputFormat};
|
use crate::output::{self, OutputFormat};
|
||||||
use crate::wait::{wait_for_execution, WaitOptions};
|
use crate::wait::{extract_stdout, spawn_execution_output_watch, wait_for_execution, WaitOptions};
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
pub enum ActionCommands {
|
pub enum ActionCommands {
|
||||||
@@ -90,7 +90,7 @@ struct Action {
|
|||||||
action_ref: String,
|
action_ref: String,
|
||||||
pack_ref: String,
|
pack_ref: String,
|
||||||
label: String,
|
label: String,
|
||||||
description: String,
|
description: Option<String>,
|
||||||
entrypoint: String,
|
entrypoint: String,
|
||||||
runtime: Option<i64>,
|
runtime: Option<i64>,
|
||||||
created: String,
|
created: String,
|
||||||
@@ -105,7 +105,7 @@ struct ActionDetail {
|
|||||||
pack: i64,
|
pack: i64,
|
||||||
pack_ref: String,
|
pack_ref: String,
|
||||||
label: String,
|
label: String,
|
||||||
description: String,
|
description: Option<String>,
|
||||||
entrypoint: String,
|
entrypoint: String,
|
||||||
runtime: Option<i64>,
|
runtime: Option<i64>,
|
||||||
param_schema: Option<serde_json::Value>,
|
param_schema: Option<serde_json::Value>,
|
||||||
@@ -253,7 +253,7 @@ async fn handle_list(
|
|||||||
.runtime
|
.runtime
|
||||||
.map(|r| r.to_string())
|
.map(|r| r.to_string())
|
||||||
.unwrap_or_else(|| "none".to_string()),
|
.unwrap_or_else(|| "none".to_string()),
|
||||||
output::truncate(&action.description, 40),
|
output::truncate(&action.description.unwrap_or_default(), 40),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,7 +288,10 @@ async fn handle_show(
|
|||||||
("Reference", action.action_ref.clone()),
|
("Reference", action.action_ref.clone()),
|
||||||
("Pack", action.pack_ref.clone()),
|
("Pack", action.pack_ref.clone()),
|
||||||
("Label", action.label.clone()),
|
("Label", action.label.clone()),
|
||||||
("Description", action.description.clone()),
|
(
|
||||||
|
"Description",
|
||||||
|
action.description.unwrap_or_else(|| "None".to_string()),
|
||||||
|
),
|
||||||
("Entry Point", action.entrypoint.clone()),
|
("Entry Point", action.entrypoint.clone()),
|
||||||
(
|
(
|
||||||
"Runtime",
|
"Runtime",
|
||||||
@@ -356,7 +359,10 @@ async fn handle_update(
|
|||||||
("Ref", action.action_ref.clone()),
|
("Ref", action.action_ref.clone()),
|
||||||
("Pack", action.pack_ref.clone()),
|
("Pack", action.pack_ref.clone()),
|
||||||
("Label", action.label.clone()),
|
("Label", action.label.clone()),
|
||||||
("Description", action.description.clone()),
|
(
|
||||||
|
"Description",
|
||||||
|
action.description.unwrap_or_else(|| "None".to_string()),
|
||||||
|
),
|
||||||
("Entrypoint", action.entrypoint.clone()),
|
("Entrypoint", action.entrypoint.clone()),
|
||||||
(
|
(
|
||||||
"Runtime",
|
"Runtime",
|
||||||
@@ -487,6 +493,15 @@ async fn handle_execute(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let verbose = matches!(output_format, OutputFormat::Table);
|
let verbose = matches!(output_format, OutputFormat::Table);
|
||||||
|
let watch_task = if verbose {
|
||||||
|
Some(spawn_execution_output_watch(
|
||||||
|
ApiClient::from_config(&config, api_url),
|
||||||
|
execution.id,
|
||||||
|
verbose,
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
let summary = wait_for_execution(WaitOptions {
|
let summary = wait_for_execution(WaitOptions {
|
||||||
execution_id: execution.id,
|
execution_id: execution.id,
|
||||||
timeout_secs: timeout,
|
timeout_secs: timeout,
|
||||||
@@ -495,6 +510,13 @@ async fn handle_execute(
|
|||||||
verbose,
|
verbose,
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
let suppress_final_stdout = watch_task
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|task| task.delivered_output() && task.root_stdout_completed());
|
||||||
|
|
||||||
|
if let Some(task) = watch_task {
|
||||||
|
let _ = tokio::time::timeout(tokio::time::Duration::from_secs(2), task.handle).await;
|
||||||
|
}
|
||||||
|
|
||||||
match output_format {
|
match output_format {
|
||||||
OutputFormat::Json | OutputFormat::Yaml => {
|
OutputFormat::Json | OutputFormat::Yaml => {
|
||||||
@@ -511,7 +533,20 @@ async fn handle_execute(
|
|||||||
("Updated", output::format_timestamp(&summary.updated)),
|
("Updated", output::format_timestamp(&summary.updated)),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if let Some(result) = summary.result {
|
let stdout = extract_stdout(&summary.result);
|
||||||
|
if !suppress_final_stdout {
|
||||||
|
if let Some(stdout) = &stdout {
|
||||||
|
output::print_section("Stdout");
|
||||||
|
println!("{}", stdout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(mut result) = summary.result {
|
||||||
|
if stdout.is_some() {
|
||||||
|
if let Some(obj) = result.as_object_mut() {
|
||||||
|
obj.remove("stdout");
|
||||||
|
}
|
||||||
|
}
|
||||||
if !result.is_null() {
|
if !result.is_null() {
|
||||||
output::print_section("Result");
|
output::print_section("Result");
|
||||||
println!("{}", serde_json::to_string_pretty(&result)?);
|
println!("{}", serde_json::to_string_pretty(&result)?);
|
||||||
|
|||||||
@@ -803,6 +803,7 @@ async fn handle_upload(
|
|||||||
api_url: &Option<String>,
|
api_url: &Option<String>,
|
||||||
output_format: OutputFormat,
|
output_format: OutputFormat,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- CLI users explicitly choose a local file to upload; this is not a server-side path sink.
|
||||||
let file_path = Path::new(&file);
|
let file_path = Path::new(&file);
|
||||||
if !file_path.exists() {
|
if !file_path.exists() {
|
||||||
anyhow::bail!("File not found: {}", file);
|
anyhow::bail!("File not found: {}", file);
|
||||||
@@ -811,6 +812,7 @@ async fn handle_upload(
|
|||||||
anyhow::bail!("Not a file: {}", file);
|
anyhow::bail!("Not a file: {}", file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- The validated CLI-selected upload path is intentionally read and sent to the API.
|
||||||
let file_bytes = tokio::fs::read(file_path).await?;
|
let file_bytes = tokio::fs::read(file_path).await?;
|
||||||
let file_name = file_path
|
let file_name = file_path
|
||||||
.file_name()
|
.file_name()
|
||||||
|
|||||||
@@ -840,6 +840,7 @@ async fn handle_upload(
|
|||||||
api_url: &Option<String>,
|
api_url: &Option<String>,
|
||||||
output_format: OutputFormat,
|
output_format: OutputFormat,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- CLI pack commands intentionally operate on operator-supplied local paths.
|
||||||
let pack_dir = Path::new(&path);
|
let pack_dir = Path::new(&path);
|
||||||
|
|
||||||
// Validate the directory exists and contains pack.yaml
|
// Validate the directory exists and contains pack.yaml
|
||||||
@@ -855,6 +856,7 @@ async fn handle_upload(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read pack ref from pack.yaml so we can display it
|
// Read pack ref from pack.yaml so we can display it
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Reading local pack metadata from the user-selected pack directory is expected CLI behavior.
|
||||||
let pack_yaml_content =
|
let pack_yaml_content =
|
||||||
std::fs::read_to_string(&pack_yaml_path).context("Failed to read pack.yaml")?;
|
std::fs::read_to_string(&pack_yaml_path).context("Failed to read pack.yaml")?;
|
||||||
let pack_yaml: serde_yaml_ng::Value =
|
let pack_yaml: serde_yaml_ng::Value =
|
||||||
@@ -957,6 +959,7 @@ fn append_dir_to_tar<W: std::io::Write>(
|
|||||||
base: &Path,
|
base: &Path,
|
||||||
dir: &Path,
|
dir: &Path,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- The archiver walks a validated local directory selected by the CLI operator.
|
||||||
for entry in std::fs::read_dir(dir).context("Failed to read directory")? {
|
for entry in std::fs::read_dir(dir).context("Failed to read directory")? {
|
||||||
let entry = entry.context("Failed to read directory entry")?;
|
let entry = entry.context("Failed to read directory entry")?;
|
||||||
let entry_path = entry.path();
|
let entry_path = entry.path();
|
||||||
@@ -1061,6 +1064,7 @@ async fn handle_test(
|
|||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
// Determine if pack is a path or a pack name
|
// Determine if pack is a path or a pack name
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Pack test targets are local CLI inputs, not remote request paths.
|
||||||
let pack_path = Path::new(&pack);
|
let pack_path = Path::new(&pack);
|
||||||
let (pack_dir, pack_ref, pack_version) = if pack_path.exists() && pack_path.is_dir() {
|
let (pack_dir, pack_ref, pack_version) = if pack_path.exists() && pack_path.is_dir() {
|
||||||
// Local pack directory
|
// Local pack directory
|
||||||
@@ -1072,6 +1076,7 @@ async fn handle_test(
|
|||||||
anyhow::bail!("pack.yaml not found in directory: {}", pack);
|
anyhow::bail!("pack.yaml not found in directory: {}", pack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- This reads pack.yaml from a local directory explicitly selected by the CLI operator.
|
||||||
let pack_yaml_content = std::fs::read_to_string(&pack_yaml_path)?;
|
let pack_yaml_content = std::fs::read_to_string(&pack_yaml_path)?;
|
||||||
let pack_yaml: serde_yaml_ng::Value = serde_yaml_ng::from_str(&pack_yaml_content)?;
|
let pack_yaml: serde_yaml_ng::Value = serde_yaml_ng::from_str(&pack_yaml_content)?;
|
||||||
|
|
||||||
@@ -1107,6 +1112,7 @@ async fn handle_test(
|
|||||||
anyhow::bail!("pack.yaml not found for pack: {}", pack);
|
anyhow::bail!("pack.yaml not found for pack: {}", pack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Installed pack tests intentionally read local metadata from the workspace packs directory.
|
||||||
let pack_yaml_content = std::fs::read_to_string(&pack_yaml_path)?;
|
let pack_yaml_content = std::fs::read_to_string(&pack_yaml_path)?;
|
||||||
let pack_yaml: serde_yaml_ng::Value = serde_yaml_ng::from_str(&pack_yaml_content)?;
|
let pack_yaml: serde_yaml_ng::Value = serde_yaml_ng::from_str(&pack_yaml_content)?;
|
||||||
|
|
||||||
@@ -1120,6 +1126,7 @@ async fn handle_test(
|
|||||||
|
|
||||||
// Load pack.yaml and extract test configuration
|
// Load pack.yaml and extract test configuration
|
||||||
let pack_yaml_path = pack_dir.join("pack.yaml");
|
let pack_yaml_path = pack_dir.join("pack.yaml");
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Test configuration is loaded from the validated local pack directory.
|
||||||
let pack_yaml_content = std::fs::read_to_string(&pack_yaml_path)?;
|
let pack_yaml_content = std::fs::read_to_string(&pack_yaml_path)?;
|
||||||
let pack_yaml: serde_yaml_ng::Value = serde_yaml_ng::from_str(&pack_yaml_content)?;
|
let pack_yaml: serde_yaml_ng::Value = serde_yaml_ng::from_str(&pack_yaml_content)?;
|
||||||
|
|
||||||
@@ -1484,6 +1491,7 @@ fn detect_source_type(source: &str, ref_spec: Option<&str>, no_registry: bool) -
|
|||||||
async fn handle_checksum(path: String, json: bool, output_format: OutputFormat) -> Result<()> {
|
async fn handle_checksum(path: String, json: bool, output_format: OutputFormat) -> Result<()> {
|
||||||
use attune_common::pack_registry::{calculate_directory_checksum, calculate_file_checksum};
|
use attune_common::pack_registry::{calculate_directory_checksum, calculate_file_checksum};
|
||||||
|
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Checksum generation intentionally accepts arbitrary local paths from the CLI operator.
|
||||||
let path_obj = Path::new(&path);
|
let path_obj = Path::new(&path);
|
||||||
|
|
||||||
if !path_obj.exists() {
|
if !path_obj.exists() {
|
||||||
@@ -1581,6 +1589,7 @@ async fn handle_index_entry(
|
|||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
use attune_common::pack_registry::calculate_directory_checksum;
|
use attune_common::pack_registry::calculate_directory_checksum;
|
||||||
|
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Index-entry generation intentionally inspects a local pack directory chosen by the CLI operator.
|
||||||
let path_obj = Path::new(&path);
|
let path_obj = Path::new(&path);
|
||||||
|
|
||||||
if !path_obj.exists() {
|
if !path_obj.exists() {
|
||||||
@@ -1606,6 +1615,7 @@ async fn handle_index_entry(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read and parse pack.yaml
|
// Read and parse pack.yaml
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Reading local pack metadata for index generation is expected CLI behavior.
|
||||||
let pack_yaml_content = std::fs::read_to_string(&pack_yaml_path)?;
|
let pack_yaml_content = std::fs::read_to_string(&pack_yaml_path)?;
|
||||||
let pack_yaml: serde_yaml_ng::Value = serde_yaml_ng::from_str(&pack_yaml_content)?;
|
let pack_yaml: serde_yaml_ng::Value = serde_yaml_ng::from_str(&pack_yaml_content)?;
|
||||||
|
|
||||||
|
|||||||
@@ -19,11 +19,13 @@ pub async fn handle_index_update(
|
|||||||
output_format: OutputFormat,
|
output_format: OutputFormat,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// Load existing index
|
// Load existing index
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Registry index maintenance is a local CLI/admin operation over operator-supplied files.
|
||||||
let index_file_path = Path::new(&index_path);
|
let index_file_path = Path::new(&index_path);
|
||||||
if !index_file_path.exists() {
|
if !index_file_path.exists() {
|
||||||
return Err(anyhow::anyhow!("Index file not found: {}", index_path));
|
return Err(anyhow::anyhow!("Index file not found: {}", index_path));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- The CLI intentionally reads the local index file selected by the operator.
|
||||||
let index_content = fs::read_to_string(index_file_path)?;
|
let index_content = fs::read_to_string(index_file_path)?;
|
||||||
let mut index: JsonValue = serde_json::from_str(&index_content)?;
|
let mut index: JsonValue = serde_json::from_str(&index_content)?;
|
||||||
|
|
||||||
@@ -34,6 +36,7 @@ pub async fn handle_index_update(
|
|||||||
.ok_or_else(|| anyhow::anyhow!("Invalid index format: missing 'packs' array"))?;
|
.ok_or_else(|| anyhow::anyhow!("Invalid index format: missing 'packs' array"))?;
|
||||||
|
|
||||||
// Load pack.yaml from the pack directory
|
// Load pack.yaml from the pack directory
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Local pack directories are explicit CLI inputs, not remote taint.
|
||||||
let pack_dir = Path::new(&pack_path);
|
let pack_dir = Path::new(&pack_path);
|
||||||
if !pack_dir.exists() || !pack_dir.is_dir() {
|
if !pack_dir.exists() || !pack_dir.is_dir() {
|
||||||
return Err(anyhow::anyhow!("Pack directory not found: {}", pack_path));
|
return Err(anyhow::anyhow!("Pack directory not found: {}", pack_path));
|
||||||
@@ -47,6 +50,7 @@ pub async fn handle_index_update(
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Reading pack.yaml from a local operator-selected pack directory is expected CLI behavior.
|
||||||
let pack_yaml_content = fs::read_to_string(&pack_yaml_path)?;
|
let pack_yaml_content = fs::read_to_string(&pack_yaml_path)?;
|
||||||
let pack_yaml: serde_yaml_ng::Value = serde_yaml_ng::from_str(&pack_yaml_content)?;
|
let pack_yaml: serde_yaml_ng::Value = serde_yaml_ng::from_str(&pack_yaml_content)?;
|
||||||
|
|
||||||
@@ -250,6 +254,7 @@ pub async fn handle_index_merge(
|
|||||||
output_format: OutputFormat,
|
output_format: OutputFormat,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// Check if output file exists
|
// Check if output file exists
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Index merge output is a local CLI path controlled by the operator.
|
||||||
let output_file_path = Path::new(&output_path);
|
let output_file_path = Path::new(&output_path);
|
||||||
if output_file_path.exists() && !force {
|
if output_file_path.exists() && !force {
|
||||||
return Err(anyhow::anyhow!(
|
return Err(anyhow::anyhow!(
|
||||||
@@ -265,6 +270,7 @@ pub async fn handle_index_merge(
|
|||||||
|
|
||||||
// Load and merge all input files
|
// Load and merge all input files
|
||||||
for input_path in &input_paths {
|
for input_path in &input_paths {
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Index merge inputs are local operator-selected files.
|
||||||
let input_file_path = Path::new(input_path);
|
let input_file_path = Path::new(input_path);
|
||||||
if !input_file_path.exists() {
|
if !input_file_path.exists() {
|
||||||
if output_format == OutputFormat::Table {
|
if output_format == OutputFormat::Table {
|
||||||
@@ -277,6 +283,7 @@ pub async fn handle_index_merge(
|
|||||||
output::print_info(&format!("Loading: {}", input_path));
|
output::print_info(&format!("Loading: {}", input_path));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- The CLI intentionally reads each local input index file during merge.
|
||||||
let index_content = fs::read_to_string(input_file_path)?;
|
let index_content = fs::read_to_string(input_file_path)?;
|
||||||
let index: JsonValue = serde_json::from_str(&index_content)?;
|
let index: JsonValue = serde_json::from_str(&index_content)?;
|
||||||
|
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ struct Rule {
|
|||||||
pack: Option<i64>,
|
pack: Option<i64>,
|
||||||
pack_ref: String,
|
pack_ref: String,
|
||||||
label: String,
|
label: String,
|
||||||
description: String,
|
description: Option<String>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
trigger: Option<i64>,
|
trigger: Option<i64>,
|
||||||
trigger_ref: String,
|
trigger_ref: String,
|
||||||
@@ -133,7 +133,7 @@ struct RuleDetail {
|
|||||||
pack: Option<i64>,
|
pack: Option<i64>,
|
||||||
pack_ref: String,
|
pack_ref: String,
|
||||||
label: String,
|
label: String,
|
||||||
description: String,
|
description: Option<String>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
trigger: Option<i64>,
|
trigger: Option<i64>,
|
||||||
trigger_ref: String,
|
trigger_ref: String,
|
||||||
@@ -321,7 +321,10 @@ async fn handle_show(
|
|||||||
("Ref", rule.rule_ref.clone()),
|
("Ref", rule.rule_ref.clone()),
|
||||||
("Pack", rule.pack_ref.clone()),
|
("Pack", rule.pack_ref.clone()),
|
||||||
("Label", rule.label.clone()),
|
("Label", rule.label.clone()),
|
||||||
("Description", rule.description.clone()),
|
(
|
||||||
|
"Description",
|
||||||
|
rule.description.unwrap_or_else(|| "None".to_string()),
|
||||||
|
),
|
||||||
("Trigger", rule.trigger_ref.clone()),
|
("Trigger", rule.trigger_ref.clone()),
|
||||||
("Action", rule.action_ref.clone()),
|
("Action", rule.action_ref.clone()),
|
||||||
("Enabled", output::format_bool(rule.enabled)),
|
("Enabled", output::format_bool(rule.enabled)),
|
||||||
@@ -440,7 +443,10 @@ async fn handle_update(
|
|||||||
("Ref", rule.rule_ref.clone()),
|
("Ref", rule.rule_ref.clone()),
|
||||||
("Pack", rule.pack_ref.clone()),
|
("Pack", rule.pack_ref.clone()),
|
||||||
("Label", rule.label.clone()),
|
("Label", rule.label.clone()),
|
||||||
("Description", rule.description.clone()),
|
(
|
||||||
|
"Description",
|
||||||
|
rule.description.unwrap_or_else(|| "None".to_string()),
|
||||||
|
),
|
||||||
("Trigger", rule.trigger_ref.clone()),
|
("Trigger", rule.trigger_ref.clone()),
|
||||||
("Action", rule.action_ref.clone()),
|
("Action", rule.action_ref.clone()),
|
||||||
("Enabled", output::format_bool(rule.enabled)),
|
("Enabled", output::format_bool(rule.enabled)),
|
||||||
|
|||||||
@@ -172,6 +172,7 @@ async fn handle_upload(
|
|||||||
api_url: &Option<String>,
|
api_url: &Option<String>,
|
||||||
output_format: OutputFormat,
|
output_format: OutputFormat,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Workflow upload reads local files chosen by the CLI operator; it is not a server-side path sink.
|
||||||
let action_path = Path::new(&action_file);
|
let action_path = Path::new(&action_file);
|
||||||
|
|
||||||
// ── 1. Validate & read the action YAML ──────────────────────────────
|
// ── 1. Validate & read the action YAML ──────────────────────────────
|
||||||
@@ -182,6 +183,7 @@ async fn handle_upload(
|
|||||||
anyhow::bail!("Path is not a file: {}", action_file);
|
anyhow::bail!("Path is not a file: {}", action_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- The action YAML is intentionally read from the validated local CLI path.
|
||||||
let action_yaml_content =
|
let action_yaml_content =
|
||||||
std::fs::read_to_string(action_path).context("Failed to read action YAML file")?;
|
std::fs::read_to_string(action_path).context("Failed to read action YAML file")?;
|
||||||
|
|
||||||
@@ -216,6 +218,7 @@ async fn handle_upload(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ── 4. Read and parse the workflow YAML ─────────────────────────────
|
// ── 4. Read and parse the workflow YAML ─────────────────────────────
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- The workflow file path is confined to the pack directory before this local read occurs.
|
||||||
let workflow_yaml_content =
|
let workflow_yaml_content =
|
||||||
std::fs::read_to_string(&workflow_path).context("Failed to read workflow YAML file")?;
|
std::fs::read_to_string(&workflow_path).context("Failed to read workflow YAML file")?;
|
||||||
|
|
||||||
@@ -616,12 +619,41 @@ fn split_action_ref(action_ref: &str) -> Result<(String, String)> {
|
|||||||
/// resolved relative to the action YAML's parent directory.
|
/// resolved relative to the action YAML's parent directory.
|
||||||
fn resolve_workflow_path(action_yaml_path: &Path, workflow_file: &str) -> Result<PathBuf> {
|
fn resolve_workflow_path(action_yaml_path: &Path, workflow_file: &str) -> Result<PathBuf> {
|
||||||
let action_dir = action_yaml_path.parent().unwrap_or(Path::new("."));
|
let action_dir = action_yaml_path.parent().unwrap_or(Path::new("."));
|
||||||
|
let pack_root = action_dir
|
||||||
|
.parent()
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("Action YAML must live inside a pack actions/ directory"))?;
|
||||||
|
let canonical_pack_root = pack_root
|
||||||
|
.canonicalize()
|
||||||
|
.context("Failed to resolve pack root for workflow file")?;
|
||||||
|
let canonical_action_dir = action_dir
|
||||||
|
.canonicalize()
|
||||||
|
.context("Failed to resolve action directory for workflow file")?;
|
||||||
|
let canonical_workflow_path = normalize_path_from_base(&canonical_action_dir, workflow_file);
|
||||||
|
|
||||||
let resolved = action_dir.join(workflow_file);
|
if !canonical_workflow_path.starts_with(&canonical_pack_root) {
|
||||||
|
anyhow::bail!(
|
||||||
|
"Workflow file resolves outside the pack directory: {}",
|
||||||
|
workflow_file
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Canonicalize if possible (for better error messages), but don't fail
|
Ok(canonical_workflow_path)
|
||||||
// if the file doesn't exist yet — we'll check existence later.
|
}
|
||||||
Ok(resolved)
|
|
||||||
|
fn normalize_path_from_base(base: &Path, relative_path: &str) -> PathBuf {
|
||||||
|
let mut normalized = PathBuf::new();
|
||||||
|
for component in base.join(relative_path).components() {
|
||||||
|
match component {
|
||||||
|
std::path::Component::Prefix(prefix) => normalized.push(prefix.as_os_str()),
|
||||||
|
std::path::Component::RootDir => normalized.push(std::path::MAIN_SEPARATOR.to_string()),
|
||||||
|
std::path::Component::CurDir => {}
|
||||||
|
std::path::Component::ParentDir => {
|
||||||
|
normalized.pop();
|
||||||
|
}
|
||||||
|
std::path::Component::Normal(part) => normalized.push(part),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
normalized
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -655,23 +687,62 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_resolve_workflow_path() {
|
fn test_resolve_workflow_path() {
|
||||||
let action_path = Path::new("/packs/mypack/actions/deploy.yaml");
|
let temp = tempfile::tempdir().unwrap();
|
||||||
|
let pack_dir = temp.path().join("mypack");
|
||||||
|
let actions_dir = pack_dir.join("actions");
|
||||||
|
let workflow_dir = actions_dir.join("workflows");
|
||||||
|
std::fs::create_dir_all(&workflow_dir).unwrap();
|
||||||
|
|
||||||
|
let action_path = actions_dir.join("deploy.yaml");
|
||||||
|
let workflow_path = workflow_dir.join("deploy.workflow.yaml");
|
||||||
|
std::fs::write(
|
||||||
|
&action_path,
|
||||||
|
"ref: mypack.deploy\nworkflow_file: workflows/deploy.workflow.yaml\n",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
std::fs::write(&workflow_path, "version: 1.0.0\n").unwrap();
|
||||||
|
|
||||||
let resolved =
|
let resolved =
|
||||||
resolve_workflow_path(action_path, "workflows/deploy.workflow.yaml").unwrap();
|
resolve_workflow_path(&action_path, "workflows/deploy.workflow.yaml").unwrap();
|
||||||
assert_eq!(
|
assert_eq!(resolved, workflow_path.canonicalize().unwrap());
|
||||||
resolved,
|
|
||||||
PathBuf::from("/packs/mypack/actions/workflows/deploy.workflow.yaml")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_resolve_workflow_path_relative() {
|
fn test_resolve_workflow_path_relative() {
|
||||||
let action_path = Path::new("actions/deploy.yaml");
|
let temp = tempfile::tempdir().unwrap();
|
||||||
|
let pack_dir = temp.path().join("mypack");
|
||||||
|
let actions_dir = pack_dir.join("actions");
|
||||||
|
let workflows_dir = pack_dir.join("workflows");
|
||||||
|
std::fs::create_dir_all(&actions_dir).unwrap();
|
||||||
|
std::fs::create_dir_all(&workflows_dir).unwrap();
|
||||||
|
|
||||||
|
let action_path = actions_dir.join("deploy.yaml");
|
||||||
|
let workflow_path = workflows_dir.join("deploy.workflow.yaml");
|
||||||
|
std::fs::write(
|
||||||
|
&action_path,
|
||||||
|
"ref: mypack.deploy\nworkflow_file: ../workflows/deploy.workflow.yaml\n",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
std::fs::write(&workflow_path, "version: 1.0.0\n").unwrap();
|
||||||
|
|
||||||
let resolved =
|
let resolved =
|
||||||
resolve_workflow_path(action_path, "workflows/deploy.workflow.yaml").unwrap();
|
resolve_workflow_path(&action_path, "../workflows/deploy.workflow.yaml").unwrap();
|
||||||
assert_eq!(
|
assert_eq!(resolved, workflow_path.canonicalize().unwrap());
|
||||||
resolved,
|
}
|
||||||
PathBuf::from("actions/workflows/deploy.workflow.yaml")
|
|
||||||
);
|
#[test]
|
||||||
|
fn test_resolve_workflow_path_rejects_traversal_outside_pack() {
|
||||||
|
let temp = tempfile::tempdir().unwrap();
|
||||||
|
let pack_dir = temp.path().join("mypack");
|
||||||
|
let actions_dir = pack_dir.join("actions");
|
||||||
|
std::fs::create_dir_all(&actions_dir).unwrap();
|
||||||
|
|
||||||
|
let action_path = actions_dir.join("deploy.yaml");
|
||||||
|
let outside = temp.path().join("outside.yaml");
|
||||||
|
std::fs::write(&action_path, "ref: mypack.deploy\n").unwrap();
|
||||||
|
std::fs::write(&outside, "version: 1.0.0\n").unwrap();
|
||||||
|
|
||||||
|
let err = resolve_workflow_path(&action_path, "../../outside.yaml").unwrap_err();
|
||||||
|
assert!(err.to_string().contains("outside the pack directory"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,13 @@
|
|||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use futures::{SinkExt, StreamExt};
|
use futures::{SinkExt, StreamExt};
|
||||||
|
use reqwest_eventsource::{Event as SseEvent, EventSource};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::sync::{
|
||||||
|
atomic::{AtomicBool, AtomicU64, Ordering},
|
||||||
|
Arc,
|
||||||
|
};
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use tokio_tungstenite::{connect_async, tungstenite::Message};
|
use tokio_tungstenite::{connect_async, tungstenite::Message};
|
||||||
|
|
||||||
@@ -54,6 +60,22 @@ pub struct WaitOptions<'a> {
|
|||||||
pub verbose: bool,
|
pub verbose: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct OutputWatchTask {
|
||||||
|
pub handle: tokio::task::JoinHandle<()>,
|
||||||
|
delivered_output: Arc<AtomicBool>,
|
||||||
|
root_stdout_completed: Arc<AtomicBool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OutputWatchTask {
|
||||||
|
pub fn delivered_output(&self) -> bool {
|
||||||
|
self.delivered_output.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn root_stdout_completed(&self) -> bool {
|
||||||
|
self.root_stdout_completed.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ── notifier WebSocket messages (mirrors websocket_server.rs) ────────────────
|
// ── notifier WebSocket messages (mirrors websocket_server.rs) ────────────────
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@@ -102,6 +124,58 @@ struct RestExecution {
|
|||||||
updated: String,
|
updated: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
struct WorkflowTaskMetadata {
|
||||||
|
task_name: String,
|
||||||
|
#[serde(default)]
|
||||||
|
task_index: Option<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
struct ExecutionListItem {
|
||||||
|
id: i64,
|
||||||
|
action_ref: String,
|
||||||
|
status: String,
|
||||||
|
#[serde(default)]
|
||||||
|
workflow_task: Option<WorkflowTaskMetadata>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct ChildWatchState {
|
||||||
|
label: String,
|
||||||
|
status: String,
|
||||||
|
announced_terminal: bool,
|
||||||
|
stream_handles: Vec<StreamWatchHandle>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct RootWatchState {
|
||||||
|
stream_handles: Vec<StreamWatchHandle>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct StreamWatchHandle {
|
||||||
|
stream_name: &'static str,
|
||||||
|
offset: Arc<AtomicU64>,
|
||||||
|
handle: tokio::task::JoinHandle<()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct StreamWatchConfig {
|
||||||
|
base_url: String,
|
||||||
|
token: String,
|
||||||
|
execution_id: i64,
|
||||||
|
prefix: Option<String>,
|
||||||
|
verbose: bool,
|
||||||
|
delivered_output: Arc<AtomicBool>,
|
||||||
|
root_stdout_completed: Option<Arc<AtomicBool>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct StreamLogTask {
|
||||||
|
stream_name: &'static str,
|
||||||
|
offset: Arc<AtomicU64>,
|
||||||
|
config: StreamWatchConfig,
|
||||||
|
}
|
||||||
|
|
||||||
impl From<RestExecution> for ExecutionSummary {
|
impl From<RestExecution> for ExecutionSummary {
|
||||||
fn from(e: RestExecution) -> Self {
|
fn from(e: RestExecution) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -177,6 +251,260 @@ pub async fn wait_for_execution(opts: WaitOptions<'_>) -> Result<ExecutionSummar
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn spawn_execution_output_watch(
|
||||||
|
mut client: ApiClient,
|
||||||
|
execution_id: i64,
|
||||||
|
verbose: bool,
|
||||||
|
) -> OutputWatchTask {
|
||||||
|
let delivered_output = Arc::new(AtomicBool::new(false));
|
||||||
|
let root_stdout_completed = Arc::new(AtomicBool::new(false));
|
||||||
|
let delivered_output_for_task = delivered_output.clone();
|
||||||
|
let root_stdout_completed_for_task = root_stdout_completed.clone();
|
||||||
|
let handle = tokio::spawn(async move {
|
||||||
|
if let Err(err) = watch_execution_output(
|
||||||
|
&mut client,
|
||||||
|
execution_id,
|
||||||
|
verbose,
|
||||||
|
delivered_output_for_task,
|
||||||
|
root_stdout_completed_for_task,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
if verbose {
|
||||||
|
eprintln!(" [watch] {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
OutputWatchTask {
|
||||||
|
handle,
|
||||||
|
delivered_output,
|
||||||
|
root_stdout_completed,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn watch_execution_output(
|
||||||
|
client: &mut ApiClient,
|
||||||
|
execution_id: i64,
|
||||||
|
verbose: bool,
|
||||||
|
delivered_output: Arc<AtomicBool>,
|
||||||
|
root_stdout_completed: Arc<AtomicBool>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let base_url = client.base_url().to_string();
|
||||||
|
let mut root_watch: Option<RootWatchState> = None;
|
||||||
|
let mut children: HashMap<i64, ChildWatchState> = HashMap::new();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let execution: RestExecution = client.get(&format!("/executions/{}", execution_id)).await?;
|
||||||
|
|
||||||
|
if root_watch
|
||||||
|
.as_ref()
|
||||||
|
.is_none_or(|state| streams_need_restart(&state.stream_handles))
|
||||||
|
{
|
||||||
|
if let Some(token) = client.auth_token().map(str::to_string) {
|
||||||
|
match root_watch.as_mut() {
|
||||||
|
Some(state) => restart_finished_streams(
|
||||||
|
&mut state.stream_handles,
|
||||||
|
&StreamWatchConfig {
|
||||||
|
base_url: base_url.clone(),
|
||||||
|
token,
|
||||||
|
execution_id,
|
||||||
|
prefix: None,
|
||||||
|
verbose,
|
||||||
|
delivered_output: delivered_output.clone(),
|
||||||
|
root_stdout_completed: Some(root_stdout_completed.clone()),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
None => {
|
||||||
|
root_watch = Some(RootWatchState {
|
||||||
|
stream_handles: spawn_execution_log_streams(StreamWatchConfig {
|
||||||
|
base_url: base_url.clone(),
|
||||||
|
token,
|
||||||
|
execution_id,
|
||||||
|
verbose,
|
||||||
|
prefix: None,
|
||||||
|
delivered_output: delivered_output.clone(),
|
||||||
|
root_stdout_completed: Some(root_stdout_completed.clone()),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let child_items = list_child_executions(client, execution_id)
|
||||||
|
.await
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
for child in child_items {
|
||||||
|
let label = format_task_label(&child.workflow_task, &child.action_ref, child.id);
|
||||||
|
let entry = children.entry(child.id).or_insert_with(|| {
|
||||||
|
if verbose {
|
||||||
|
eprintln!(" [{}] started ({})", label, child.action_ref);
|
||||||
|
}
|
||||||
|
let stream_handles = client
|
||||||
|
.auth_token()
|
||||||
|
.map(str::to_string)
|
||||||
|
.map(|token| {
|
||||||
|
spawn_execution_log_streams(StreamWatchConfig {
|
||||||
|
base_url: base_url.clone(),
|
||||||
|
token,
|
||||||
|
execution_id: child.id,
|
||||||
|
prefix: Some(label.clone()),
|
||||||
|
verbose,
|
||||||
|
delivered_output: delivered_output.clone(),
|
||||||
|
root_stdout_completed: None,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.unwrap_or_default();
|
||||||
|
ChildWatchState {
|
||||||
|
label,
|
||||||
|
status: child.status.clone(),
|
||||||
|
announced_terminal: false,
|
||||||
|
stream_handles,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if entry.status != child.status {
|
||||||
|
entry.status = child.status.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
let child_is_terminal = is_terminal(&entry.status);
|
||||||
|
if !child_is_terminal && streams_need_restart(&entry.stream_handles) {
|
||||||
|
if let Some(token) = client.auth_token().map(str::to_string) {
|
||||||
|
restart_finished_streams(
|
||||||
|
&mut entry.stream_handles,
|
||||||
|
&StreamWatchConfig {
|
||||||
|
base_url: base_url.clone(),
|
||||||
|
token,
|
||||||
|
execution_id: child.id,
|
||||||
|
prefix: Some(entry.label.clone()),
|
||||||
|
verbose,
|
||||||
|
delivered_output: delivered_output.clone(),
|
||||||
|
root_stdout_completed: None,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !entry.announced_terminal && is_terminal(&child.status) {
|
||||||
|
entry.announced_terminal = true;
|
||||||
|
if verbose {
|
||||||
|
eprintln!(" [{}] {}", entry.label, child.status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_terminal(&execution.status) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(root_watch) = root_watch {
|
||||||
|
wait_for_stream_handles(root_watch.stream_handles).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
for child in children.into_values() {
|
||||||
|
wait_for_stream_handles(child.stream_handles).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_execution_log_streams(config: StreamWatchConfig) -> Vec<StreamWatchHandle> {
|
||||||
|
["stdout", "stderr"]
|
||||||
|
.into_iter()
|
||||||
|
.map(|stream_name| {
|
||||||
|
let offset = Arc::new(AtomicU64::new(0));
|
||||||
|
let completion_flag = if stream_name == "stdout" {
|
||||||
|
config.root_stdout_completed.clone()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
StreamWatchHandle {
|
||||||
|
stream_name,
|
||||||
|
handle: tokio::spawn(stream_execution_log(StreamLogTask {
|
||||||
|
stream_name,
|
||||||
|
offset: offset.clone(),
|
||||||
|
config: StreamWatchConfig {
|
||||||
|
base_url: config.base_url.clone(),
|
||||||
|
token: config.token.clone(),
|
||||||
|
execution_id: config.execution_id,
|
||||||
|
prefix: config.prefix.clone(),
|
||||||
|
verbose: config.verbose,
|
||||||
|
delivered_output: config.delivered_output.clone(),
|
||||||
|
root_stdout_completed: completion_flag,
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
offset,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn streams_need_restart(handles: &[StreamWatchHandle]) -> bool {
|
||||||
|
handles.is_empty() || handles.iter().any(|handle| handle.handle.is_finished())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn restart_finished_streams(handles: &mut [StreamWatchHandle], config: &StreamWatchConfig) {
|
||||||
|
for stream in handles.iter_mut() {
|
||||||
|
if stream.handle.is_finished() {
|
||||||
|
let offset = stream.offset.clone();
|
||||||
|
let completion_flag = if stream.stream_name == "stdout" {
|
||||||
|
config.root_stdout_completed.clone()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
stream.handle = tokio::spawn(stream_execution_log(StreamLogTask {
|
||||||
|
stream_name: stream.stream_name,
|
||||||
|
offset,
|
||||||
|
config: StreamWatchConfig {
|
||||||
|
base_url: config.base_url.clone(),
|
||||||
|
token: config.token.clone(),
|
||||||
|
execution_id: config.execution_id,
|
||||||
|
prefix: config.prefix.clone(),
|
||||||
|
verbose: config.verbose,
|
||||||
|
delivered_output: config.delivered_output.clone(),
|
||||||
|
root_stdout_completed: completion_flag,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn wait_for_stream_handles(handles: Vec<StreamWatchHandle>) {
|
||||||
|
for handle in handles {
|
||||||
|
let _ = handle.handle.await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn list_child_executions(
|
||||||
|
client: &mut ApiClient,
|
||||||
|
execution_id: i64,
|
||||||
|
) -> Result<Vec<ExecutionListItem>> {
|
||||||
|
const PER_PAGE: u32 = 100;
|
||||||
|
|
||||||
|
let mut page = 1;
|
||||||
|
let mut all_children = Vec::new();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let path = format!("/executions?parent={execution_id}&page={page}&per_page={PER_PAGE}");
|
||||||
|
let mut page_items: Vec<ExecutionListItem> = client.get_paginated(&path).await?;
|
||||||
|
let page_len = page_items.len();
|
||||||
|
all_children.append(&mut page_items);
|
||||||
|
|
||||||
|
if page_len < PER_PAGE as usize {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
page += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(all_children)
|
||||||
|
}
|
||||||
|
|
||||||
// ── WebSocket path ────────────────────────────────────────────────────────────
|
// ── WebSocket path ────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
async fn wait_via_websocket(
|
async fn wait_via_websocket(
|
||||||
@@ -482,6 +810,7 @@ fn resolve_ws_url(opts: &WaitOptions<'_>) -> Option<String> {
|
|||||||
/// - `https://api.example.com` → `wss://api.example.com:8081`
|
/// - `https://api.example.com` → `wss://api.example.com:8081`
|
||||||
/// - `http://api.example.com:9000` → `ws://api.example.com:8081`
|
/// - `http://api.example.com:9000` → `ws://api.example.com:8081`
|
||||||
fn derive_notifier_url(api_url: &str) -> Option<String> {
|
fn derive_notifier_url(api_url: &str) -> Option<String> {
|
||||||
|
// nosemgrep: javascript.lang.security.detect-insecure-websocket.detect-insecure-websocket -- The function upgrades https->wss and only returns ws for explicit http base URLs or test examples.
|
||||||
let url = url::Url::parse(api_url).ok()?;
|
let url = url::Url::parse(api_url).ok()?;
|
||||||
let ws_scheme = match url.scheme() {
|
let ws_scheme = match url.scheme() {
|
||||||
"https" => "wss",
|
"https" => "wss",
|
||||||
@@ -491,6 +820,148 @@ fn derive_notifier_url(api_url: &str) -> Option<String> {
|
|||||||
Some(format!("{}://{}:8081", ws_scheme, host))
|
Some(format!("{}://{}:8081", ws_scheme, host))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn extract_stdout(result: &Option<serde_json::Value>) -> Option<String> {
|
||||||
|
result
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|value| value.get("stdout"))
|
||||||
|
.and_then(|stdout| stdout.as_str())
|
||||||
|
.filter(|stdout| !stdout.is_empty())
|
||||||
|
.map(ToOwned::to_owned)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn format_task_label(
|
||||||
|
workflow_task: &Option<WorkflowTaskMetadata>,
|
||||||
|
action_ref: &str,
|
||||||
|
execution_id: i64,
|
||||||
|
) -> String {
|
||||||
|
if let Some(workflow_task) = workflow_task {
|
||||||
|
if let Some(index) = workflow_task.task_index {
|
||||||
|
format!("{}[{}]", workflow_task.task_name, index)
|
||||||
|
} else {
|
||||||
|
workflow_task.task_name.clone()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
format!("{}#{}", action_ref, execution_id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn stream_execution_log(task: StreamLogTask) {
|
||||||
|
let StreamLogTask {
|
||||||
|
stream_name,
|
||||||
|
offset,
|
||||||
|
config:
|
||||||
|
StreamWatchConfig {
|
||||||
|
base_url,
|
||||||
|
token,
|
||||||
|
execution_id,
|
||||||
|
prefix,
|
||||||
|
verbose,
|
||||||
|
delivered_output,
|
||||||
|
root_stdout_completed,
|
||||||
|
},
|
||||||
|
} = task;
|
||||||
|
|
||||||
|
let mut stream_url = match url::Url::parse(&format!(
|
||||||
|
"{}/api/v1/executions/{}/logs/{}/stream",
|
||||||
|
base_url.trim_end_matches('/'),
|
||||||
|
execution_id,
|
||||||
|
stream_name
|
||||||
|
)) {
|
||||||
|
Ok(url) => url,
|
||||||
|
Err(err) => {
|
||||||
|
if verbose {
|
||||||
|
eprintln!(" [watch] failed to build stream URL: {}", err);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let current_offset = offset.load(Ordering::Relaxed).to_string();
|
||||||
|
stream_url
|
||||||
|
.query_pairs_mut()
|
||||||
|
.append_pair("token", &token)
|
||||||
|
.append_pair("offset", ¤t_offset);
|
||||||
|
|
||||||
|
let mut event_source = EventSource::get(stream_url);
|
||||||
|
let mut carry = String::new();
|
||||||
|
|
||||||
|
while let Some(event) = event_source.next().await {
|
||||||
|
match event {
|
||||||
|
Ok(SseEvent::Open) => {}
|
||||||
|
Ok(SseEvent::Message(message)) => match message.event.as_str() {
|
||||||
|
"content" | "append" => {
|
||||||
|
if let Ok(server_offset) = message.id.parse::<u64>() {
|
||||||
|
offset.store(server_offset, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
if !message.data.is_empty() {
|
||||||
|
delivered_output.store(true, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
print_stream_chunk(prefix.as_deref(), &message.data, &mut carry);
|
||||||
|
}
|
||||||
|
"done" => {
|
||||||
|
if let Some(flag) = &root_stdout_completed {
|
||||||
|
flag.store(true, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
flush_stream_chunk(prefix.as_deref(), &mut carry);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
"error" => {
|
||||||
|
if verbose && !message.data.is_empty() {
|
||||||
|
eprintln!(" [watch] {}", message.data);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
flush_stream_chunk(prefix.as_deref(), &mut carry);
|
||||||
|
if verbose {
|
||||||
|
eprintln!(
|
||||||
|
" [watch] stream error for execution {}: {}",
|
||||||
|
execution_id, err
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
flush_stream_chunk(prefix.as_deref(), &mut carry);
|
||||||
|
event_source.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_stream_chunk(prefix: Option<&str>, chunk: &str, carry: &mut String) {
|
||||||
|
carry.push_str(chunk);
|
||||||
|
|
||||||
|
while let Some(idx) = carry.find('\n') {
|
||||||
|
let mut line = carry.drain(..=idx).collect::<String>();
|
||||||
|
if line.ends_with('\n') {
|
||||||
|
line.pop();
|
||||||
|
}
|
||||||
|
if line.ends_with('\r') {
|
||||||
|
line.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(prefix) = prefix {
|
||||||
|
eprintln!("[{}] {}", prefix, line);
|
||||||
|
} else {
|
||||||
|
eprintln!("{}", line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush_stream_chunk(prefix: Option<&str>, carry: &mut String) {
|
||||||
|
if carry.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(prefix) = prefix {
|
||||||
|
eprintln!("[{}] {}", prefix, carry);
|
||||||
|
} else {
|
||||||
|
eprintln!("{}", carry);
|
||||||
|
}
|
||||||
|
carry.clear();
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -553,4 +1024,26 @@ mod tests {
|
|||||||
assert_eq!(summary.status, "failed");
|
assert_eq!(summary.status, "failed");
|
||||||
assert_eq!(summary.action_ref, "");
|
assert_eq!(summary.action_ref, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_extract_stdout() {
|
||||||
|
let result = Some(serde_json::json!({
|
||||||
|
"stdout": "hello world",
|
||||||
|
"stderr_log": "/tmp/stderr.log"
|
||||||
|
}));
|
||||||
|
assert_eq!(extract_stdout(&result).as_deref(), Some("hello world"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_task_label() {
|
||||||
|
let workflow_task = Some(WorkflowTaskMetadata {
|
||||||
|
task_name: "build".to_string(),
|
||||||
|
task_index: Some(2),
|
||||||
|
});
|
||||||
|
assert_eq!(
|
||||||
|
format_task_label(&workflow_task, "core.echo", 42),
|
||||||
|
"build[2]"
|
||||||
|
);
|
||||||
|
assert_eq!(format_task_label(&None, "core.echo", 42), "core.echo#42");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ regex = { workspace = true }
|
|||||||
|
|
||||||
# Version matching
|
# Version matching
|
||||||
semver = { workspace = true }
|
semver = { workspace = true }
|
||||||
|
url = { workspace = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
mockall = { workspace = true }
|
mockall = { workspace = true }
|
||||||
|
|||||||
107
crates/common/src/agent_bootstrap.rs
Normal file
107
crates/common/src/agent_bootstrap.rs
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
//! Shared bootstrap helpers for injected agent binaries.
|
||||||
|
|
||||||
|
use crate::agent_runtime_detection::{
|
||||||
|
detect_runtimes, format_as_env_value, print_detection_report_for_env, DetectedRuntime,
|
||||||
|
};
|
||||||
|
use tracing::{info, warn};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct RuntimeBootstrapResult {
|
||||||
|
pub runtimes_override: Option<String>,
|
||||||
|
pub detected_runtimes: Option<Vec<DetectedRuntime>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Detect runtimes and populate the agent runtime environment variable when needed.
|
||||||
|
///
|
||||||
|
/// This must run before the Tokio runtime starts because it may mutate process
|
||||||
|
/// environment variables.
|
||||||
|
pub fn bootstrap_runtime_env(env_var_name: &str) -> RuntimeBootstrapResult {
|
||||||
|
let runtimes_override = std::env::var(env_var_name).ok();
|
||||||
|
let mut detected_runtimes = None;
|
||||||
|
|
||||||
|
if let Some(ref override_value) = runtimes_override {
|
||||||
|
info!(
|
||||||
|
"{} already set (override): {}",
|
||||||
|
env_var_name, override_value
|
||||||
|
);
|
||||||
|
info!("Running auto-detection for override-specified runtimes...");
|
||||||
|
|
||||||
|
let detected = detect_runtimes();
|
||||||
|
let override_names: Vec<&str> = override_value.split(',').map(|s| s.trim()).collect();
|
||||||
|
|
||||||
|
let filtered: Vec<_> = detected
|
||||||
|
.into_iter()
|
||||||
|
.filter(|rt| {
|
||||||
|
let lower_name = rt.name.to_ascii_lowercase();
|
||||||
|
override_names
|
||||||
|
.iter()
|
||||||
|
.any(|ov| ov.to_ascii_lowercase() == lower_name)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if filtered.is_empty() {
|
||||||
|
warn!(
|
||||||
|
"None of the override runtimes ({}) were found on this system",
|
||||||
|
override_value
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
info!(
|
||||||
|
"Matched {} override runtime(s) to detected interpreters:",
|
||||||
|
filtered.len()
|
||||||
|
);
|
||||||
|
for rt in &filtered {
|
||||||
|
match &rt.version {
|
||||||
|
Some(ver) => info!(" ✓ {} — {} ({})", rt.name, rt.path, ver),
|
||||||
|
None => info!(" ✓ {} — {}", rt.name, rt.path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
detected_runtimes = Some(filtered);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
info!("No {} override — running auto-detection...", env_var_name);
|
||||||
|
|
||||||
|
let detected = detect_runtimes();
|
||||||
|
|
||||||
|
if detected.is_empty() {
|
||||||
|
warn!("No runtimes detected! The agent may not be able to execute any work.");
|
||||||
|
} else {
|
||||||
|
info!("Detected {} runtime(s):", detected.len());
|
||||||
|
for rt in &detected {
|
||||||
|
match &rt.version {
|
||||||
|
Some(ver) => info!(" ✓ {} — {} ({})", rt.name, rt.path, ver),
|
||||||
|
None => info!(" ✓ {} — {}", rt.name, rt.path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let runtime_csv = format_as_env_value(&detected);
|
||||||
|
info!("Setting {}={}", env_var_name, runtime_csv);
|
||||||
|
std::env::set_var(env_var_name, &runtime_csv);
|
||||||
|
detected_runtimes = Some(detected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RuntimeBootstrapResult {
|
||||||
|
runtimes_override,
|
||||||
|
detected_runtimes,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print_detect_only_report(env_var_name: &str, result: &RuntimeBootstrapResult) {
|
||||||
|
if result.runtimes_override.is_some() {
|
||||||
|
info!("--detect-only: re-running detection to show what is available on this system...");
|
||||||
|
println!(
|
||||||
|
"NOTE: {} is set — auto-detection was skipped during normal startup.",
|
||||||
|
env_var_name
|
||||||
|
);
|
||||||
|
println!(" Showing what auto-detection would find on this system:");
|
||||||
|
println!();
|
||||||
|
|
||||||
|
let detected = detect_runtimes();
|
||||||
|
print_detection_report_for_env(env_var_name, &detected);
|
||||||
|
} else if let Some(ref detected) = result.detected_runtimes {
|
||||||
|
print_detection_report_for_env(env_var_name, detected);
|
||||||
|
} else {
|
||||||
|
let detected = detect_runtimes();
|
||||||
|
print_detection_report_for_env(env_var_name, &detected);
|
||||||
|
}
|
||||||
|
}
|
||||||
306
crates/common/src/agent_runtime_detection.rs
Normal file
306
crates/common/src/agent_runtime_detection.rs
Normal file
@@ -0,0 +1,306 @@
|
|||||||
|
//! Runtime auto-detection for injected Attune agent binaries.
|
||||||
|
//!
|
||||||
|
//! This module probes the local system directly for well-known interpreters,
|
||||||
|
//! without requiring database access.
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::fmt;
|
||||||
|
use std::process::Command;
|
||||||
|
use tracing::{debug, info};
|
||||||
|
|
||||||
|
/// A runtime interpreter discovered on the local system.
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct DetectedRuntime {
|
||||||
|
/// Canonical runtime name (for example, "python" or "node").
|
||||||
|
pub name: String,
|
||||||
|
|
||||||
|
/// Absolute path to the interpreter binary.
|
||||||
|
pub path: String,
|
||||||
|
|
||||||
|
/// Version string if the version command succeeded.
|
||||||
|
pub version: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for DetectedRuntime {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match &self.version {
|
||||||
|
Some(v) => write!(f, "{} ({}, v{})", self.name, self.path, v),
|
||||||
|
None => write!(f, "{} ({})", self.name, self.path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct RuntimeCandidate {
|
||||||
|
name: &'static str,
|
||||||
|
binaries: &'static [&'static str],
|
||||||
|
version_args: &'static [&'static str],
|
||||||
|
version_parser: VersionParser,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum VersionParser {
|
||||||
|
SemverLike,
|
||||||
|
JavaStyle,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn candidates() -> Vec<RuntimeCandidate> {
|
||||||
|
vec![
|
||||||
|
RuntimeCandidate {
|
||||||
|
name: "shell",
|
||||||
|
binaries: &["bash", "sh"],
|
||||||
|
version_args: &["--version"],
|
||||||
|
version_parser: VersionParser::SemverLike,
|
||||||
|
},
|
||||||
|
RuntimeCandidate {
|
||||||
|
name: "python",
|
||||||
|
binaries: &["python3", "python"],
|
||||||
|
version_args: &["--version"],
|
||||||
|
version_parser: VersionParser::SemverLike,
|
||||||
|
},
|
||||||
|
RuntimeCandidate {
|
||||||
|
name: "node",
|
||||||
|
binaries: &["node", "nodejs"],
|
||||||
|
version_args: &["--version"],
|
||||||
|
version_parser: VersionParser::SemverLike,
|
||||||
|
},
|
||||||
|
RuntimeCandidate {
|
||||||
|
name: "ruby",
|
||||||
|
binaries: &["ruby"],
|
||||||
|
version_args: &["--version"],
|
||||||
|
version_parser: VersionParser::SemverLike,
|
||||||
|
},
|
||||||
|
RuntimeCandidate {
|
||||||
|
name: "go",
|
||||||
|
binaries: &["go"],
|
||||||
|
version_args: &["version"],
|
||||||
|
version_parser: VersionParser::SemverLike,
|
||||||
|
},
|
||||||
|
RuntimeCandidate {
|
||||||
|
name: "java",
|
||||||
|
binaries: &["java"],
|
||||||
|
version_args: &["-version"],
|
||||||
|
version_parser: VersionParser::JavaStyle,
|
||||||
|
},
|
||||||
|
RuntimeCandidate {
|
||||||
|
name: "r",
|
||||||
|
binaries: &["Rscript"],
|
||||||
|
version_args: &["--version"],
|
||||||
|
version_parser: VersionParser::SemverLike,
|
||||||
|
},
|
||||||
|
RuntimeCandidate {
|
||||||
|
name: "perl",
|
||||||
|
binaries: &["perl"],
|
||||||
|
version_args: &["--version"],
|
||||||
|
version_parser: VersionParser::SemverLike,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Detect available runtimes by probing the local system.
|
||||||
|
pub fn detect_runtimes() -> Vec<DetectedRuntime> {
|
||||||
|
info!("Starting runtime auto-detection...");
|
||||||
|
|
||||||
|
let mut detected = Vec::new();
|
||||||
|
|
||||||
|
for candidate in candidates() {
|
||||||
|
match detect_single_runtime(&candidate) {
|
||||||
|
Some(runtime) => {
|
||||||
|
info!(" ✓ Detected: {}", runtime);
|
||||||
|
detected.push(runtime);
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
debug!(" ✗ Not found: {}", candidate.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
info!(
|
||||||
|
"Runtime auto-detection complete: found {} runtime(s): [{}]",
|
||||||
|
detected.len(),
|
||||||
|
detected
|
||||||
|
.iter()
|
||||||
|
.map(|r| r.name.as_str())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
);
|
||||||
|
|
||||||
|
detected
|
||||||
|
}
|
||||||
|
|
||||||
|
fn detect_single_runtime(candidate: &RuntimeCandidate) -> Option<DetectedRuntime> {
|
||||||
|
for binary in candidate.binaries {
|
||||||
|
if let Some(path) = which_binary(binary) {
|
||||||
|
let version = get_version(&path, candidate.version_args, &candidate.version_parser);
|
||||||
|
|
||||||
|
return Some(DetectedRuntime {
|
||||||
|
name: candidate.name.to_string(),
|
||||||
|
path,
|
||||||
|
version,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn which_binary(binary: &str) -> Option<String> {
|
||||||
|
if binary == "bash" || binary == "sh" {
|
||||||
|
let absolute_path = format!("/bin/{}", binary);
|
||||||
|
if std::path::Path::new(&absolute_path).exists() {
|
||||||
|
return Some(absolute_path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match Command::new("which").arg(binary).output() {
|
||||||
|
Ok(output) if output.status.success() => {
|
||||||
|
let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
||||||
|
if path.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(_) => None,
|
||||||
|
Err(e) => {
|
||||||
|
debug!("'which' command failed ({}), trying 'command -v'", e);
|
||||||
|
match Command::new("sh")
|
||||||
|
.args(["-c", &format!("command -v {}", binary)])
|
||||||
|
.output()
|
||||||
|
{
|
||||||
|
Ok(output) if output.status.success() => {
|
||||||
|
let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
||||||
|
if path.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_version(binary_path: &str, version_args: &[&str], parser: &VersionParser) -> Option<String> {
|
||||||
|
let output = match Command::new(binary_path).args(version_args).output() {
|
||||||
|
Ok(output) => output,
|
||||||
|
Err(e) => {
|
||||||
|
debug!("Failed to run version command for {}: {}", binary_path, e);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||||
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
|
let combined = format!("{}{}", stdout, stderr);
|
||||||
|
|
||||||
|
match parser {
|
||||||
|
VersionParser::SemverLike => parse_semver_like(&combined),
|
||||||
|
VersionParser::JavaStyle => parse_java_version(&combined),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_semver_like(output: &str) -> Option<String> {
|
||||||
|
let re = regex::Regex::new(r"(?:v|go)?(\d+\.\d+(?:\.\d+)?)").ok()?;
|
||||||
|
re.captures(output)
|
||||||
|
.and_then(|captures| captures.get(1).map(|m| m.as_str().to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_java_version(output: &str) -> Option<String> {
|
||||||
|
let quoted_re = regex::Regex::new(r#"version\s+"([^"]+)""#).ok()?;
|
||||||
|
if let Some(captures) = quoted_re.captures(output) {
|
||||||
|
return captures.get(1).map(|m| m.as_str().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_semver_like(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn format_as_env_value(runtimes: &[DetectedRuntime]) -> String {
|
||||||
|
runtimes
|
||||||
|
.iter()
|
||||||
|
.map(|r| r.name.as_str())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(",")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print_detection_report_for_env(env_var_name: &str, runtimes: &[DetectedRuntime]) {
|
||||||
|
println!("=== Attune Agent Runtime Detection Report ===");
|
||||||
|
println!();
|
||||||
|
|
||||||
|
if runtimes.is_empty() {
|
||||||
|
println!("No runtimes detected!");
|
||||||
|
println!();
|
||||||
|
println!("The agent could not find any supported interpreter binaries.");
|
||||||
|
println!("Ensure at least one of the following is installed and on PATH:");
|
||||||
|
println!(" - bash / sh (shell scripts)");
|
||||||
|
println!(" - python3 / python (Python scripts)");
|
||||||
|
println!(" - node / nodejs (Node.js scripts)");
|
||||||
|
println!(" - ruby (Ruby scripts)");
|
||||||
|
println!(" - go (Go programs)");
|
||||||
|
println!(" - java (Java programs)");
|
||||||
|
println!(" - Rscript (R scripts)");
|
||||||
|
println!(" - perl (Perl scripts)");
|
||||||
|
} else {
|
||||||
|
println!("Detected {} runtime(s):", runtimes.len());
|
||||||
|
println!();
|
||||||
|
for rt in runtimes {
|
||||||
|
let version_str = rt.version.as_deref().unwrap_or("unknown version");
|
||||||
|
println!(" ✓ {:<10} {} ({})", rt.name, rt.path, version_str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!();
|
||||||
|
println!("{}={}", env_var_name, format_as_env_value(runtimes));
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_semver_like_python() {
|
||||||
|
assert_eq!(
|
||||||
|
parse_semver_like("Python 3.12.1"),
|
||||||
|
Some("3.12.1".to_string())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_semver_like_node() {
|
||||||
|
assert_eq!(parse_semver_like("v20.11.0"), Some("20.11.0".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_semver_like_go() {
|
||||||
|
assert_eq!(
|
||||||
|
parse_semver_like("go version go1.22.0 linux/amd64"),
|
||||||
|
Some("1.22.0".to_string())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_java_version_openjdk() {
|
||||||
|
assert_eq!(
|
||||||
|
parse_java_version(r#"openjdk version "21.0.1" 2023-10-17"#),
|
||||||
|
Some("21.0.1".to_string())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_as_env_value_multiple() {
|
||||||
|
let runtimes = vec![
|
||||||
|
DetectedRuntime {
|
||||||
|
name: "shell".to_string(),
|
||||||
|
path: "/bin/bash".to_string(),
|
||||||
|
version: Some("5.2.15".to_string()),
|
||||||
|
},
|
||||||
|
DetectedRuntime {
|
||||||
|
name: "python".to_string(),
|
||||||
|
path: "/usr/bin/python3".to_string(),
|
||||||
|
version: Some("3.12.1".to_string()),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
assert_eq!(format_as_env_value(&runtimes), "shell,python");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -355,10 +355,14 @@ pub struct OidcConfig {
|
|||||||
pub enabled: bool,
|
pub enabled: bool,
|
||||||
|
|
||||||
/// OpenID Provider discovery document URL.
|
/// OpenID Provider discovery document URL.
|
||||||
pub discovery_url: String,
|
/// Required when `enabled` is true; ignored otherwise.
|
||||||
|
#[serde(default)]
|
||||||
|
pub discovery_url: Option<String>,
|
||||||
|
|
||||||
/// Confidential client ID.
|
/// Confidential client ID.
|
||||||
pub client_id: String,
|
/// Required when `enabled` is true; ignored otherwise.
|
||||||
|
#[serde(default)]
|
||||||
|
pub client_id: Option<String>,
|
||||||
|
|
||||||
/// Provider name used in login-page overrides such as `?auth=<provider_name>`.
|
/// Provider name used in login-page overrides such as `?auth=<provider_name>`.
|
||||||
#[serde(default = "default_oidc_provider_name")]
|
#[serde(default = "default_oidc_provider_name")]
|
||||||
@@ -374,7 +378,9 @@ pub struct OidcConfig {
|
|||||||
pub client_secret: Option<String>,
|
pub client_secret: Option<String>,
|
||||||
|
|
||||||
/// Redirect URI registered with the provider.
|
/// Redirect URI registered with the provider.
|
||||||
pub redirect_uri: String,
|
/// Required when `enabled` is true; ignored otherwise.
|
||||||
|
#[serde(default)]
|
||||||
|
pub redirect_uri: Option<String>,
|
||||||
|
|
||||||
/// Optional post-logout redirect URI.
|
/// Optional post-logout redirect URI.
|
||||||
pub post_logout_redirect_uri: Option<String>,
|
pub post_logout_redirect_uri: Option<String>,
|
||||||
@@ -396,7 +402,9 @@ pub struct LdapConfig {
|
|||||||
pub enabled: bool,
|
pub enabled: bool,
|
||||||
|
|
||||||
/// LDAP server URL (e.g., "ldap://ldap.example.com:389" or "ldaps://ldap.example.com:636").
|
/// LDAP server URL (e.g., "ldap://ldap.example.com:389" or "ldaps://ldap.example.com:636").
|
||||||
pub url: String,
|
/// Required when `enabled` is true; ignored otherwise.
|
||||||
|
#[serde(default)]
|
||||||
|
pub url: Option<String>,
|
||||||
|
|
||||||
/// Bind DN template. Use `{login}` as placeholder for the user-supplied login.
|
/// Bind DN template. Use `{login}` as placeholder for the user-supplied login.
|
||||||
/// Example: "uid={login},ou=users,dc=example,dc=com"
|
/// Example: "uid={login},ou=users,dc=example,dc=com"
|
||||||
@@ -650,6 +658,11 @@ pub struct PackRegistryConfig {
|
|||||||
#[serde(default = "default_true")]
|
#[serde(default = "default_true")]
|
||||||
pub verify_checksums: bool,
|
pub verify_checksums: bool,
|
||||||
|
|
||||||
|
/// Additional remote hosts allowed for pack archive/git downloads.
|
||||||
|
/// Hosts from enabled registry indices are implicitly allowed.
|
||||||
|
#[serde(default)]
|
||||||
|
pub allowed_source_hosts: Vec<String>,
|
||||||
|
|
||||||
/// Allow HTTP (non-HTTPS) registries
|
/// Allow HTTP (non-HTTPS) registries
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub allow_http: bool,
|
pub allow_http: bool,
|
||||||
@@ -672,11 +685,21 @@ impl Default for PackRegistryConfig {
|
|||||||
cache_enabled: true,
|
cache_enabled: true,
|
||||||
timeout: default_registry_timeout(),
|
timeout: default_registry_timeout(),
|
||||||
verify_checksums: true,
|
verify_checksums: true,
|
||||||
|
allowed_source_hosts: Vec::new(),
|
||||||
allow_http: false,
|
allow_http: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Agent binary distribution configuration
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct AgentConfig {
|
||||||
|
/// Directory containing agent binary files
|
||||||
|
pub binary_dir: String,
|
||||||
|
/// Optional bootstrap token for authenticating agent binary downloads
|
||||||
|
pub bootstrap_token: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
/// Executor service configuration
|
/// Executor service configuration
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct ExecutorConfig {
|
pub struct ExecutorConfig {
|
||||||
@@ -770,6 +793,9 @@ pub struct Config {
|
|||||||
|
|
||||||
/// Executor configuration (optional, for executor service)
|
/// Executor configuration (optional, for executor service)
|
||||||
pub executor: Option<ExecutorConfig>,
|
pub executor: Option<ExecutorConfig>,
|
||||||
|
|
||||||
|
/// Agent configuration (optional, for agent binary distribution)
|
||||||
|
pub agent: Option<AgentConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_service_name() -> String {
|
fn default_service_name() -> String {
|
||||||
@@ -973,14 +999,20 @@ impl Config {
|
|||||||
|
|
||||||
if let Some(oidc) = &self.security.oidc {
|
if let Some(oidc) = &self.security.oidc {
|
||||||
if oidc.enabled {
|
if oidc.enabled {
|
||||||
if oidc.discovery_url.trim().is_empty() {
|
if oidc
|
||||||
|
.discovery_url
|
||||||
|
.as_deref()
|
||||||
|
.unwrap_or("")
|
||||||
|
.trim()
|
||||||
|
.is_empty()
|
||||||
|
{
|
||||||
return Err(crate::Error::validation(
|
return Err(crate::Error::validation(
|
||||||
"OIDC discovery URL cannot be empty when OIDC is enabled",
|
"OIDC discovery URL is required when OIDC is enabled",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
if oidc.client_id.trim().is_empty() {
|
if oidc.client_id.as_deref().unwrap_or("").trim().is_empty() {
|
||||||
return Err(crate::Error::validation(
|
return Err(crate::Error::validation(
|
||||||
"OIDC client ID cannot be empty when OIDC is enabled",
|
"OIDC client ID is required when OIDC is enabled",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
if oidc
|
if oidc
|
||||||
@@ -994,14 +1026,22 @@ impl Config {
|
|||||||
"OIDC client secret is required when OIDC is enabled",
|
"OIDC client secret is required when OIDC is enabled",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
if oidc.redirect_uri.trim().is_empty() {
|
if oidc.redirect_uri.as_deref().unwrap_or("").trim().is_empty() {
|
||||||
return Err(crate::Error::validation(
|
return Err(crate::Error::validation(
|
||||||
"OIDC redirect URI cannot be empty when OIDC is enabled",
|
"OIDC redirect URI is required when OIDC is enabled",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(ldap) = &self.security.ldap {
|
||||||
|
if ldap.enabled && ldap.url.as_deref().unwrap_or("").trim().is_empty() {
|
||||||
|
return Err(crate::Error::validation(
|
||||||
|
"LDAP server URL is required when LDAP is enabled",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Validate encryption key if provided
|
// Validate encryption key if provided
|
||||||
if let Some(ref key) = self.security.encryption_key {
|
if let Some(ref key) = self.security.encryption_key {
|
||||||
if key.len() < 32 {
|
if key.len() < 32 {
|
||||||
@@ -1066,6 +1106,7 @@ mod tests {
|
|||||||
notifier: None,
|
notifier: None,
|
||||||
pack_registry: PackRegistryConfig::default(),
|
pack_registry: PackRegistryConfig::default(),
|
||||||
executor: None,
|
executor: None,
|
||||||
|
agent: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(config.service_name, "attune");
|
assert_eq!(config.service_name, "attune");
|
||||||
@@ -1144,6 +1185,7 @@ mod tests {
|
|||||||
notifier: None,
|
notifier: None,
|
||||||
pack_registry: PackRegistryConfig::default(),
|
pack_registry: PackRegistryConfig::default(),
|
||||||
executor: None,
|
executor: None,
|
||||||
|
agent: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
assert!(config.validate().is_ok());
|
assert!(config.validate().is_ok());
|
||||||
@@ -1158,6 +1200,31 @@ mod tests {
|
|||||||
assert!(config.validate().is_err());
|
assert!(config.validate().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_oidc_config_disabled_no_urls_required() {
|
||||||
|
let yaml = r#"
|
||||||
|
enabled: false
|
||||||
|
"#;
|
||||||
|
let cfg: OidcConfig = serde_yaml_ng::from_str(yaml).unwrap();
|
||||||
|
assert!(!cfg.enabled);
|
||||||
|
assert!(cfg.discovery_url.is_none());
|
||||||
|
assert!(cfg.client_id.is_none());
|
||||||
|
assert!(cfg.redirect_uri.is_none());
|
||||||
|
assert!(cfg.client_secret.is_none());
|
||||||
|
assert_eq!(cfg.provider_name, "oidc");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ldap_config_disabled_no_url_required() {
|
||||||
|
let yaml = r#"
|
||||||
|
enabled: false
|
||||||
|
"#;
|
||||||
|
let cfg: LdapConfig = serde_yaml_ng::from_str(yaml).unwrap();
|
||||||
|
assert!(!cfg.enabled);
|
||||||
|
assert!(cfg.url.is_none());
|
||||||
|
assert_eq!(cfg.provider_name, "ldap");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ldap_config_defaults() {
|
fn test_ldap_config_defaults() {
|
||||||
let yaml = r#"
|
let yaml = r#"
|
||||||
@@ -1168,7 +1235,7 @@ client_id: "test"
|
|||||||
let cfg: LdapConfig = serde_yaml_ng::from_str(yaml).unwrap();
|
let cfg: LdapConfig = serde_yaml_ng::from_str(yaml).unwrap();
|
||||||
|
|
||||||
assert!(cfg.enabled);
|
assert!(cfg.enabled);
|
||||||
assert_eq!(cfg.url, "ldap://localhost:389");
|
assert_eq!(cfg.url.as_deref(), Some("ldap://localhost:389"));
|
||||||
assert_eq!(cfg.user_filter, "(uid={login})");
|
assert_eq!(cfg.user_filter, "(uid={login})");
|
||||||
assert_eq!(cfg.login_attr, "uid");
|
assert_eq!(cfg.login_attr, "uid");
|
||||||
assert_eq!(cfg.email_attr, "mail");
|
assert_eq!(cfg.email_attr, "mail");
|
||||||
@@ -1208,7 +1275,7 @@ provider_icon_url: "https://corp.com/icon.svg"
|
|||||||
let cfg: LdapConfig = serde_yaml_ng::from_str(yaml).unwrap();
|
let cfg: LdapConfig = serde_yaml_ng::from_str(yaml).unwrap();
|
||||||
|
|
||||||
assert!(cfg.enabled);
|
assert!(cfg.enabled);
|
||||||
assert_eq!(cfg.url, "ldaps://ldap.corp.com:636");
|
assert_eq!(cfg.url.as_deref(), Some("ldaps://ldap.corp.com:636"));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
cfg.bind_dn_template.as_deref(),
|
cfg.bind_dn_template.as_deref(),
|
||||||
Some("uid={login},ou=people,dc=corp,dc=com")
|
Some("uid={login},ou=people,dc=corp,dc=com")
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
//! - Configuration
|
//! - Configuration
|
||||||
//! - Utilities
|
//! - Utilities
|
||||||
|
|
||||||
|
pub mod agent_bootstrap;
|
||||||
|
pub mod agent_runtime_detection;
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod crypto;
|
pub mod crypto;
|
||||||
|
|||||||
@@ -444,13 +444,55 @@ pub mod runtime {
|
|||||||
|
|
||||||
/// Optional environment variables to set during action execution.
|
/// Optional environment variables to set during action execution.
|
||||||
///
|
///
|
||||||
/// Values support the same template variables as other fields:
|
/// Entries support the same template variables as other fields:
|
||||||
/// `{pack_dir}`, `{env_dir}`, `{interpreter}`, `{manifest_path}`.
|
/// `{pack_dir}`, `{env_dir}`, `{interpreter}`, `{manifest_path}`.
|
||||||
///
|
///
|
||||||
/// Example: `{"NODE_PATH": "{env_dir}/node_modules"}` ensures Node.js
|
/// The shorthand string form replaces the variable entirely:
|
||||||
/// can find packages installed in the isolated runtime environment.
|
/// `{"NODE_PATH": "{env_dir}/node_modules"}`
|
||||||
|
///
|
||||||
|
/// The object form supports declarative merge semantics:
|
||||||
|
/// `{"PYTHONPATH": {"value": "{pack_dir}/lib", "operation": "prepend"}}`
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub env_vars: HashMap<String, String>,
|
pub env_vars: HashMap<String, RuntimeEnvVarConfig>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Declarative configuration for a single runtime environment variable.
|
||||||
|
///
|
||||||
|
/// The string form is shorthand for `{ "value": "...", "operation": "set" }`.
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
pub enum RuntimeEnvVarConfig {
|
||||||
|
Value(String),
|
||||||
|
Spec(RuntimeEnvVarSpec),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Full configuration for a runtime environment variable.
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
pub struct RuntimeEnvVarSpec {
|
||||||
|
/// Template value to resolve for this variable.
|
||||||
|
pub value: String,
|
||||||
|
|
||||||
|
/// How the resolved value should be merged with any existing value.
|
||||||
|
#[serde(default)]
|
||||||
|
pub operation: RuntimeEnvVarOperation,
|
||||||
|
|
||||||
|
/// Separator used for prepend/append operations.
|
||||||
|
#[serde(default = "default_env_var_separator")]
|
||||||
|
pub separator: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Merge behavior for runtime-provided environment variables.
|
||||||
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum RuntimeEnvVarOperation {
|
||||||
|
#[default]
|
||||||
|
Set,
|
||||||
|
Prepend,
|
||||||
|
Append,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_env_var_separator() -> String {
|
||||||
|
":".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Controls how inline code is materialized before execution.
|
/// Controls how inline code is materialized before execution.
|
||||||
@@ -768,6 +810,43 @@ pub mod runtime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl RuntimeEnvVarConfig {
|
||||||
|
/// Resolve this environment variable against the current template
|
||||||
|
/// variables and any existing value already present in the process env.
|
||||||
|
pub fn resolve(
|
||||||
|
&self,
|
||||||
|
vars: &HashMap<&str, String>,
|
||||||
|
existing_value: Option<&str>,
|
||||||
|
) -> String {
|
||||||
|
match self {
|
||||||
|
Self::Value(value) => RuntimeExecutionConfig::resolve_template(value, vars),
|
||||||
|
Self::Spec(spec) => {
|
||||||
|
let resolved = RuntimeExecutionConfig::resolve_template(&spec.value, vars);
|
||||||
|
match spec.operation {
|
||||||
|
RuntimeEnvVarOperation::Set => resolved,
|
||||||
|
RuntimeEnvVarOperation::Prepend => {
|
||||||
|
join_env_var_values(&resolved, existing_value, &spec.separator)
|
||||||
|
}
|
||||||
|
RuntimeEnvVarOperation::Append => join_env_var_values(
|
||||||
|
existing_value.unwrap_or_default(),
|
||||||
|
Some(&resolved),
|
||||||
|
&spec.separator,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn join_env_var_values(left: &str, right: Option<&str>, separator: &str) -> String {
|
||||||
|
match (left.is_empty(), right.unwrap_or_default().is_empty()) {
|
||||||
|
(true, true) => String::new(),
|
||||||
|
(false, true) => left.to_string(),
|
||||||
|
(true, false) => right.unwrap_or_default().to_string(),
|
||||||
|
(false, false) => format!("{}{}{}", left, separator, right.unwrap_or_default()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
|
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
|
||||||
pub struct Runtime {
|
pub struct Runtime {
|
||||||
pub id: Id,
|
pub id: Id,
|
||||||
@@ -776,10 +855,13 @@ pub mod runtime {
|
|||||||
pub pack_ref: Option<String>,
|
pub pack_ref: Option<String>,
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub aliases: Vec<String>,
|
||||||
pub distributions: JsonDict,
|
pub distributions: JsonDict,
|
||||||
pub installation: Option<JsonDict>,
|
pub installation: Option<JsonDict>,
|
||||||
pub installers: JsonDict,
|
pub installers: JsonDict,
|
||||||
pub execution_config: JsonDict,
|
pub execution_config: JsonDict,
|
||||||
|
pub auto_detected: bool,
|
||||||
|
pub detection_config: JsonDict,
|
||||||
pub created: DateTime<Utc>,
|
pub created: DateTime<Utc>,
|
||||||
pub updated: DateTime<Utc>,
|
pub updated: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
@@ -884,7 +966,7 @@ pub mod trigger {
|
|||||||
pub pack: Option<Id>,
|
pub pack: Option<Id>,
|
||||||
pub pack_ref: Option<String>,
|
pub pack_ref: Option<String>,
|
||||||
pub label: String,
|
pub label: String,
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
pub entrypoint: String,
|
pub entrypoint: String,
|
||||||
pub runtime: Id,
|
pub runtime: Id,
|
||||||
pub runtime_ref: String,
|
pub runtime_ref: String,
|
||||||
@@ -912,7 +994,7 @@ pub mod action {
|
|||||||
pub pack: Id,
|
pub pack: Id,
|
||||||
pub pack_ref: String,
|
pub pack_ref: String,
|
||||||
pub label: String,
|
pub label: String,
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
pub entrypoint: String,
|
pub entrypoint: String,
|
||||||
pub runtime: Option<Id>,
|
pub runtime: Option<Id>,
|
||||||
/// Optional semver version constraint for the runtime
|
/// Optional semver version constraint for the runtime
|
||||||
@@ -962,7 +1044,7 @@ pub mod rule {
|
|||||||
pub pack: Id,
|
pub pack: Id,
|
||||||
pub pack_ref: String,
|
pub pack_ref: String,
|
||||||
pub label: String,
|
pub label: String,
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
pub action: Option<Id>,
|
pub action: Option<Id>,
|
||||||
pub action_ref: String,
|
pub action_ref: String,
|
||||||
pub trigger: Option<Id>,
|
pub trigger: Option<Id>,
|
||||||
@@ -1218,6 +1300,7 @@ pub mod identity {
|
|||||||
pub display_name: Option<String>,
|
pub display_name: Option<String>,
|
||||||
pub password_hash: Option<String>,
|
pub password_hash: Option<String>,
|
||||||
pub attributes: JsonDict,
|
pub attributes: JsonDict,
|
||||||
|
pub frozen: bool,
|
||||||
pub created: DateTime<Utc>,
|
pub created: DateTime<Utc>,
|
||||||
pub updated: DateTime<Utc>,
|
pub updated: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
@@ -1242,6 +1325,25 @@ pub mod identity {
|
|||||||
pub permset: Id,
|
pub permset: Id,
|
||||||
pub created: DateTime<Utc>,
|
pub created: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
|
||||||
|
pub struct IdentityRoleAssignment {
|
||||||
|
pub id: Id,
|
||||||
|
pub identity: Id,
|
||||||
|
pub role: String,
|
||||||
|
pub source: String,
|
||||||
|
pub managed: bool,
|
||||||
|
pub created: DateTime<Utc>,
|
||||||
|
pub updated: DateTime<Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
|
||||||
|
pub struct PermissionSetRoleAssignment {
|
||||||
|
pub id: Id,
|
||||||
|
pub permset: Id,
|
||||||
|
pub role: String,
|
||||||
|
pub created: DateTime<Utc>,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Key/Value storage
|
/// Key/Value storage
|
||||||
@@ -1310,7 +1412,7 @@ pub mod artifact {
|
|||||||
pub content_type: Option<String>,
|
pub content_type: Option<String>,
|
||||||
/// Size of the latest version's content in bytes
|
/// Size of the latest version's content in bytes
|
||||||
pub size_bytes: Option<i64>,
|
pub size_bytes: Option<i64>,
|
||||||
/// Execution that produced this artifact (no FK — execution is a hypertable)
|
/// Execution that produced this artifact (no FK by design)
|
||||||
pub execution: Option<Id>,
|
pub execution: Option<Id>,
|
||||||
/// Structured JSONB data for progress artifacts or metadata
|
/// Structured JSONB data for progress artifacts or metadata
|
||||||
pub data: Option<serde_json::Value>,
|
pub data: Option<serde_json::Value>,
|
||||||
@@ -1617,3 +1719,68 @@ pub mod entity_history {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::runtime::{
|
||||||
|
RuntimeEnvVarConfig, RuntimeEnvVarOperation, RuntimeEnvVarSpec, RuntimeExecutionConfig,
|
||||||
|
};
|
||||||
|
use serde_json::json;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn runtime_execution_config_env_vars_accept_string_and_object_forms() {
|
||||||
|
let config: RuntimeExecutionConfig = serde_json::from_value(json!({
|
||||||
|
"env_vars": {
|
||||||
|
"NODE_PATH": "{env_dir}/node_modules",
|
||||||
|
"PYTHONPATH": {
|
||||||
|
"value": "{pack_dir}/lib",
|
||||||
|
"operation": "prepend",
|
||||||
|
"separator": ":"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
.expect("runtime execution config should deserialize");
|
||||||
|
|
||||||
|
assert!(matches!(
|
||||||
|
config.env_vars.get("NODE_PATH"),
|
||||||
|
Some(RuntimeEnvVarConfig::Value(value)) if value == "{env_dir}/node_modules"
|
||||||
|
));
|
||||||
|
|
||||||
|
assert!(matches!(
|
||||||
|
config.env_vars.get("PYTHONPATH"),
|
||||||
|
Some(RuntimeEnvVarConfig::Spec(RuntimeEnvVarSpec {
|
||||||
|
value,
|
||||||
|
operation: RuntimeEnvVarOperation::Prepend,
|
||||||
|
separator,
|
||||||
|
})) if value == "{pack_dir}/lib" && separator == ":"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn runtime_env_var_config_resolves_prepend_and_append_against_existing_values() {
|
||||||
|
let mut vars = HashMap::new();
|
||||||
|
vars.insert("pack_dir", "/packs/example".to_string());
|
||||||
|
vars.insert("env_dir", "/runtime_envs/example/python".to_string());
|
||||||
|
|
||||||
|
let prepend = RuntimeEnvVarConfig::Spec(RuntimeEnvVarSpec {
|
||||||
|
value: "{pack_dir}/lib".to_string(),
|
||||||
|
operation: RuntimeEnvVarOperation::Prepend,
|
||||||
|
separator: ":".to_string(),
|
||||||
|
});
|
||||||
|
assert_eq!(
|
||||||
|
prepend.resolve(&vars, Some("/already/set")),
|
||||||
|
"/packs/example/lib:/already/set"
|
||||||
|
);
|
||||||
|
|
||||||
|
let append = RuntimeEnvVarConfig::Spec(RuntimeEnvVarSpec {
|
||||||
|
value: "{env_dir}/node_modules".to_string(),
|
||||||
|
operation: RuntimeEnvVarOperation::Append,
|
||||||
|
separator: ":".to_string(),
|
||||||
|
});
|
||||||
|
assert_eq!(
|
||||||
|
append.resolve(&vars, Some("/base/modules")),
|
||||||
|
"/base/modules:/runtime_envs/example/python/node_modules"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -102,7 +102,12 @@ impl MqError {
|
|||||||
pub fn is_retriable(&self) -> bool {
|
pub fn is_retriable(&self) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
self,
|
self,
|
||||||
MqError::Connection(_) | MqError::Channel(_) | MqError::Timeout(_) | MqError::Pool(_)
|
MqError::Connection(_)
|
||||||
|
| MqError::Channel(_)
|
||||||
|
| MqError::Publish(_)
|
||||||
|
| MqError::Timeout(_)
|
||||||
|
| MqError::Pool(_)
|
||||||
|
| MqError::Lapin(_)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,8 +10,9 @@ use crate::config::Config;
|
|||||||
use crate::error::{Error, Result};
|
use crate::error::{Error, Result};
|
||||||
use crate::models::Runtime;
|
use crate::models::Runtime;
|
||||||
use crate::repositories::action::ActionRepository;
|
use crate::repositories::action::ActionRepository;
|
||||||
use crate::repositories::runtime::RuntimeRepository;
|
use crate::repositories::runtime::{self, RuntimeRepository};
|
||||||
use crate::repositories::FindById as _;
|
use crate::repositories::FindById as _;
|
||||||
|
use regex::Regex;
|
||||||
use serde_json::Value as JsonValue;
|
use serde_json::Value as JsonValue;
|
||||||
use sqlx::{PgPool, Row};
|
use sqlx::{PgPool, Row};
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
@@ -94,10 +95,7 @@ pub struct PackEnvironmentManager {
|
|||||||
impl PackEnvironmentManager {
|
impl PackEnvironmentManager {
|
||||||
/// Create a new pack environment manager
|
/// Create a new pack environment manager
|
||||||
pub fn new(pool: PgPool, config: &Config) -> Self {
|
pub fn new(pool: PgPool, config: &Config) -> Self {
|
||||||
let base_path = PathBuf::from(&config.packs_base_dir)
|
let base_path = PathBuf::from(&config.runtime_envs_dir);
|
||||||
.parent()
|
|
||||||
.map(|p| p.join("packenvs"))
|
|
||||||
.unwrap_or_else(|| PathBuf::from("/opt/attune/packenvs"));
|
|
||||||
|
|
||||||
Self { pool, base_path }
|
Self { pool, base_path }
|
||||||
}
|
}
|
||||||
@@ -370,15 +368,11 @@ impl PackEnvironmentManager {
|
|||||||
// ========================================================================
|
// ========================================================================
|
||||||
|
|
||||||
async fn get_runtime(&self, runtime_id: i64) -> Result<Runtime> {
|
async fn get_runtime(&self, runtime_id: i64) -> Result<Runtime> {
|
||||||
sqlx::query_as::<_, Runtime>(
|
let query = format!(
|
||||||
r#"
|
"SELECT {} FROM runtime WHERE id = $1",
|
||||||
SELECT id, ref, pack, pack_ref, description, name,
|
runtime::SELECT_COLUMNS
|
||||||
distributions, installation, installers, execution_config,
|
);
|
||||||
created, updated
|
sqlx::query_as::<_, Runtime>(&query)
|
||||||
FROM runtime
|
|
||||||
WHERE id = $1
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.bind(runtime_id)
|
.bind(runtime_id)
|
||||||
.fetch_one(&self.pool)
|
.fetch_one(&self.pool)
|
||||||
.await
|
.await
|
||||||
@@ -403,19 +397,19 @@ impl PackEnvironmentManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn calculate_env_path(&self, pack_ref: &str, runtime: &Runtime) -> Result<PathBuf> {
|
fn calculate_env_path(&self, pack_ref: &str, runtime: &Runtime) -> Result<PathBuf> {
|
||||||
|
let runtime_name_lower = runtime.name.to_lowercase();
|
||||||
let template = runtime
|
let template = runtime
|
||||||
.installers
|
.installers
|
||||||
.get("base_path_template")
|
.get("base_path_template")
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.unwrap_or("/opt/attune/packenvs/{pack_ref}/{runtime_name_lower}");
|
.unwrap_or("{pack_ref}/{runtime_name_lower}");
|
||||||
|
|
||||||
let runtime_name_lower = runtime.name.to_lowercase();
|
|
||||||
let path_str = template
|
let path_str = template
|
||||||
.replace("{pack_ref}", pack_ref)
|
.replace("{pack_ref}", pack_ref)
|
||||||
.replace("{runtime_ref}", &runtime.r#ref)
|
.replace("{runtime_ref}", &runtime.r#ref)
|
||||||
.replace("{runtime_name_lower}", &runtime_name_lower);
|
.replace("{runtime_name_lower}", &runtime_name_lower);
|
||||||
|
|
||||||
Ok(PathBuf::from(path_str))
|
resolve_env_path(&self.base_path, &path_str)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn upsert_environment_record(
|
async fn upsert_environment_record(
|
||||||
@@ -532,6 +526,7 @@ impl PackEnvironmentManager {
|
|||||||
let mut install_log = String::new();
|
let mut install_log = String::new();
|
||||||
|
|
||||||
// Create environment directory
|
// Create environment directory
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- env_path comes from validated runtime-env path construction under runtime_envs_dir.
|
||||||
let env_path = PathBuf::from(&pack_env.env_path);
|
let env_path = PathBuf::from(&pack_env.env_path);
|
||||||
if env_path.exists() {
|
if env_path.exists() {
|
||||||
warn!(
|
warn!(
|
||||||
@@ -663,6 +658,8 @@ impl PackEnvironmentManager {
|
|||||||
env_path,
|
env_path,
|
||||||
&pack_path_str,
|
&pack_path_str,
|
||||||
)?;
|
)?;
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- The candidate command path is validated and confined before any execution is attempted.
|
||||||
|
let command = validate_installer_command(&command, pack_path, Path::new(env_path))?;
|
||||||
|
|
||||||
let args_template = installer
|
let args_template = installer
|
||||||
.get("args")
|
.get("args")
|
||||||
@@ -684,12 +681,17 @@ impl PackEnvironmentManager {
|
|||||||
|
|
||||||
let cwd_template = installer.get("cwd").and_then(|v| v.as_str());
|
let cwd_template = installer.get("cwd").and_then(|v| v.as_str());
|
||||||
let cwd = if let Some(cwd_t) = cwd_template {
|
let cwd = if let Some(cwd_t) = cwd_template {
|
||||||
Some(self.resolve_template(
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Installer cwd values are validated to stay under the pack root or environment directory.
|
||||||
|
Some(validate_installer_path(
|
||||||
|
&self.resolve_template(
|
||||||
cwd_t,
|
cwd_t,
|
||||||
pack_ref,
|
pack_ref,
|
||||||
runtime_ref,
|
runtime_ref,
|
||||||
env_path,
|
env_path,
|
||||||
&pack_path_str,
|
&pack_path_str,
|
||||||
|
)?,
|
||||||
|
pack_path,
|
||||||
|
Path::new(env_path),
|
||||||
)?)
|
)?)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@@ -767,6 +769,7 @@ impl PackEnvironmentManager {
|
|||||||
async fn execute_installer_action(&self, action: &InstallerAction) -> Result<String> {
|
async fn execute_installer_action(&self, action: &InstallerAction) -> Result<String> {
|
||||||
debug!("Executing: {} {:?}", action.command, action.args);
|
debug!("Executing: {} {:?}", action.command, action.args);
|
||||||
|
|
||||||
|
// nosemgrep: rust.actix.command-injection.rust-actix-command-injection.rust-actix-command-injection -- action.command is accepted only after strict validation of executable shape and allowed path roots.
|
||||||
let mut cmd = Command::new(&action.command);
|
let mut cmd = Command::new(&action.command);
|
||||||
cmd.args(&action.args);
|
cmd.args(&action.args);
|
||||||
|
|
||||||
@@ -804,7 +807,9 @@ impl PackEnvironmentManager {
|
|||||||
// Check file_exists condition
|
// Check file_exists condition
|
||||||
if let Some(file_path_template) = condition.get("file_exists").and_then(|v| v.as_str()) {
|
if let Some(file_path_template) = condition.get("file_exists").and_then(|v| v.as_str()) {
|
||||||
let file_path = file_path_template.replace("{pack_path}", &pack_path.to_string_lossy());
|
let file_path = file_path_template.replace("{pack_path}", &pack_path.to_string_lossy());
|
||||||
return Ok(PathBuf::from(file_path).exists());
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Conditional file checks are validated to stay under trusted pack/environment roots before filesystem access.
|
||||||
|
let validated = validate_installer_path(&file_path, pack_path, &self.base_path)?;
|
||||||
|
return Ok(PathBuf::from(validated).exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default: condition is true
|
// Default: condition is true
|
||||||
@@ -820,6 +825,93 @@ impl PackEnvironmentManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn resolve_env_path(base_path: &Path, path_str: &str) -> Result<PathBuf> {
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- This helper normalizes env paths and preserves legacy absolute templates while still rejecting parent traversal.
|
||||||
|
let raw_path = Path::new(path_str);
|
||||||
|
if raw_path.is_absolute() {
|
||||||
|
return normalize_relative_or_absolute_path(raw_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
let joined = base_path.join(raw_path);
|
||||||
|
normalize_relative_or_absolute_path(&joined)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn normalize_relative_or_absolute_path(path: &Path) -> Result<PathBuf> {
|
||||||
|
let mut normalized = PathBuf::new();
|
||||||
|
for component in path.components() {
|
||||||
|
match component {
|
||||||
|
std::path::Component::Prefix(prefix) => normalized.push(prefix.as_os_str()),
|
||||||
|
std::path::Component::RootDir => normalized.push(std::path::MAIN_SEPARATOR.to_string()),
|
||||||
|
std::path::Component::CurDir => {}
|
||||||
|
std::path::Component::ParentDir => {
|
||||||
|
return Err(Error::validation(format!(
|
||||||
|
"Parent-directory traversal is not allowed in installer paths: {}",
|
||||||
|
path.display()
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
std::path::Component::Normal(part) => normalized.push(part),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(normalized)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_installer_command(command: &str, pack_path: &Path, env_path: &Path) -> Result<String> {
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Command validation inspects the path form before enforcing allowed executable rules.
|
||||||
|
let command_path = Path::new(command);
|
||||||
|
if command_path.is_absolute() {
|
||||||
|
return validate_installer_path(command, pack_path, env_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
if command.contains(std::path::MAIN_SEPARATOR) {
|
||||||
|
return Err(Error::validation(format!(
|
||||||
|
"Installer command must be a bare executable name or an allowed absolute path: {}",
|
||||||
|
command
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
let command_name_re = Regex::new(r"^[A-Za-z0-9._+-]+$").expect("valid installer regex");
|
||||||
|
if !command_name_re.is_match(command) {
|
||||||
|
return Err(Error::validation(format!(
|
||||||
|
"Installer command contains invalid characters: {}",
|
||||||
|
command
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(command.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_installer_path(path_str: &str, pack_path: &Path, env_path: &Path) -> Result<String> {
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Path validation normalizes candidate installer paths before enforcing root confinement.
|
||||||
|
let path = normalize_path(Path::new(path_str));
|
||||||
|
let normalized_pack_path = normalize_path(pack_path);
|
||||||
|
let normalized_env_path = normalize_path(env_path);
|
||||||
|
if path.starts_with(&normalized_pack_path) || path.starts_with(&normalized_env_path) {
|
||||||
|
Ok(path.to_string_lossy().to_string())
|
||||||
|
} else {
|
||||||
|
Err(Error::validation(format!(
|
||||||
|
"Installer path must remain under the pack or environment directory: {}",
|
||||||
|
path_str
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn normalize_path(path: &Path) -> PathBuf {
|
||||||
|
let mut normalized = PathBuf::new();
|
||||||
|
for component in path.components() {
|
||||||
|
match component {
|
||||||
|
std::path::Component::Prefix(prefix) => normalized.push(prefix.as_os_str()),
|
||||||
|
std::path::Component::RootDir => normalized.push(std::path::MAIN_SEPARATOR.to_string()),
|
||||||
|
std::path::Component::CurDir => {}
|
||||||
|
std::path::Component::ParentDir => {
|
||||||
|
normalized.pop();
|
||||||
|
}
|
||||||
|
std::path::Component::Normal(part) => normalized.push(part),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
normalized
|
||||||
|
}
|
||||||
|
|
||||||
/// Collect the lowercase runtime names that require environment setup for a pack.
|
/// Collect the lowercase runtime names that require environment setup for a pack.
|
||||||
///
|
///
|
||||||
/// This queries the pack's actions, resolves their runtimes, and returns the names
|
/// This queries the pack's actions, resolves their runtimes, and returns the names
|
||||||
|
|||||||
@@ -349,6 +349,7 @@ mod tests {
|
|||||||
cache_enabled: true,
|
cache_enabled: true,
|
||||||
timeout: 120,
|
timeout: 120,
|
||||||
verify_checksums: true,
|
verify_checksums: true,
|
||||||
|
allowed_source_hosts: Vec::new(),
|
||||||
allow_http: false,
|
allow_http: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -11,10 +11,14 @@
|
|||||||
use super::{Checksum, InstallSource, PackIndexEntry, RegistryClient};
|
use super::{Checksum, InstallSource, PackIndexEntry, RegistryClient};
|
||||||
use crate::config::PackRegistryConfig;
|
use crate::config::PackRegistryConfig;
|
||||||
use crate::error::{Error, Result};
|
use crate::error::{Error, Result};
|
||||||
|
use std::collections::HashSet;
|
||||||
|
use std::net::{IpAddr, Ipv6Addr};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
|
use tokio::net::lookup_host;
|
||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
/// Progress callback type
|
/// Progress callback type
|
||||||
pub type ProgressCallback = Arc<dyn Fn(ProgressEvent) + Send + Sync>;
|
pub type ProgressCallback = Arc<dyn Fn(ProgressEvent) + Send + Sync>;
|
||||||
@@ -53,6 +57,12 @@ pub struct PackInstaller {
|
|||||||
/// Whether to verify checksums
|
/// Whether to verify checksums
|
||||||
verify_checksums: bool,
|
verify_checksums: bool,
|
||||||
|
|
||||||
|
/// Whether HTTP remote sources are allowed
|
||||||
|
allow_http: bool,
|
||||||
|
|
||||||
|
/// Remote hosts allowed for archive/git installs
|
||||||
|
allowed_remote_hosts: Option<HashSet<String>>,
|
||||||
|
|
||||||
/// Progress callback (optional)
|
/// Progress callback (optional)
|
||||||
progress_callback: Option<ProgressCallback>,
|
progress_callback: Option<ProgressCallback>,
|
||||||
}
|
}
|
||||||
@@ -106,17 +116,32 @@ impl PackInstaller {
|
|||||||
.await
|
.await
|
||||||
.map_err(|e| Error::internal(format!("Failed to create temp directory: {}", e)))?;
|
.map_err(|e| Error::internal(format!("Failed to create temp directory: {}", e)))?;
|
||||||
|
|
||||||
let (registry_client, verify_checksums) = if let Some(config) = registry_config {
|
let (registry_client, verify_checksums, allow_http, allowed_remote_hosts) =
|
||||||
|
if let Some(config) = registry_config {
|
||||||
let verify_checksums = config.verify_checksums;
|
let verify_checksums = config.verify_checksums;
|
||||||
(Some(RegistryClient::new(config)?), verify_checksums)
|
let allow_http = config.allow_http;
|
||||||
|
let allowed_remote_hosts = collect_allowed_remote_hosts(&config)?;
|
||||||
|
let allowed_remote_hosts = if allowed_remote_hosts.is_empty() {
|
||||||
|
None
|
||||||
} else {
|
} else {
|
||||||
(None, false)
|
Some(allowed_remote_hosts)
|
||||||
|
};
|
||||||
|
(
|
||||||
|
Some(RegistryClient::new(config)?),
|
||||||
|
verify_checksums,
|
||||||
|
allow_http,
|
||||||
|
allowed_remote_hosts,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(None, false, false, None)
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
temp_dir,
|
temp_dir,
|
||||||
registry_client,
|
registry_client,
|
||||||
verify_checksums,
|
verify_checksums,
|
||||||
|
allow_http,
|
||||||
|
allowed_remote_hosts,
|
||||||
progress_callback: None,
|
progress_callback: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -152,6 +177,7 @@ impl PackInstaller {
|
|||||||
|
|
||||||
/// Install from git repository
|
/// Install from git repository
|
||||||
async fn install_from_git(&self, url: &str, git_ref: Option<&str>) -> Result<InstalledPack> {
|
async fn install_from_git(&self, url: &str, git_ref: Option<&str>) -> Result<InstalledPack> {
|
||||||
|
self.validate_git_source(url).await?;
|
||||||
tracing::info!("Installing pack from git: {} (ref: {:?})", url, git_ref);
|
tracing::info!("Installing pack from git: {} (ref: {:?})", url, git_ref);
|
||||||
|
|
||||||
self.report_progress(ProgressEvent::StepStarted {
|
self.report_progress(ProgressEvent::StepStarted {
|
||||||
@@ -405,10 +431,12 @@ impl PackInstaller {
|
|||||||
|
|
||||||
/// Download an archive from a URL
|
/// Download an archive from a URL
|
||||||
async fn download_archive(&self, url: &str) -> Result<PathBuf> {
|
async fn download_archive(&self, url: &str) -> Result<PathBuf> {
|
||||||
|
let parsed_url = self.validate_remote_url(url).await?;
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
|
// nosemgrep: rust.actix.ssrf.reqwest-taint.reqwest-taint -- Remote source URLs are restricted to configured allowlisted hosts, HTTPS, and public IPs before request execution.
|
||||||
let response = client
|
let response = client
|
||||||
.get(url)
|
.get(parsed_url.clone())
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.map_err(|e| Error::internal(format!("Failed to download archive: {}", e)))?;
|
.map_err(|e| Error::internal(format!("Failed to download archive: {}", e)))?;
|
||||||
@@ -421,11 +449,7 @@ impl PackInstaller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Determine filename from URL
|
// Determine filename from URL
|
||||||
let filename = url
|
let filename = archive_filename_from_url(&parsed_url);
|
||||||
.split('/')
|
|
||||||
.next_back()
|
|
||||||
.unwrap_or("archive.zip")
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
let archive_path = self.temp_dir.join(&filename);
|
let archive_path = self.temp_dir.join(&filename);
|
||||||
|
|
||||||
@@ -442,6 +466,116 @@ impl PackInstaller {
|
|||||||
Ok(archive_path)
|
Ok(archive_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn validate_remote_url(&self, raw_url: &str) -> Result<Url> {
|
||||||
|
let parsed = Url::parse(raw_url)
|
||||||
|
.map_err(|e| Error::validation(format!("Invalid remote URL '{}': {}", raw_url, e)))?;
|
||||||
|
|
||||||
|
if parsed.scheme() != "https" && !(self.allow_http && parsed.scheme() == "http") {
|
||||||
|
return Err(Error::validation(format!(
|
||||||
|
"Remote URL must use https{}: {}",
|
||||||
|
if self.allow_http {
|
||||||
|
" or http when pack_registry.allow_http is enabled"
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
},
|
||||||
|
raw_url
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !parsed.username().is_empty() || parsed.password().is_some() {
|
||||||
|
return Err(Error::validation(
|
||||||
|
"Remote URLs with embedded credentials are not allowed".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let host = parsed.host_str().ok_or_else(|| {
|
||||||
|
Error::validation(format!("Remote URL is missing a host: {}", raw_url))
|
||||||
|
})?;
|
||||||
|
let normalized_host = host.to_ascii_lowercase();
|
||||||
|
|
||||||
|
if normalized_host == "localhost" {
|
||||||
|
return Err(Error::validation(format!(
|
||||||
|
"Remote URL host is not allowed: {}",
|
||||||
|
host
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(allowed_remote_hosts) = &self.allowed_remote_hosts {
|
||||||
|
if !allowed_remote_hosts.contains(&normalized_host) {
|
||||||
|
return Err(Error::validation(format!(
|
||||||
|
"Remote URL host '{}' is not in the configured allowlist. Add it to pack_registry.allowed_source_hosts.",
|
||||||
|
host
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ip) = parsed.host().and_then(|host| match host {
|
||||||
|
url::Host::Ipv4(ip) => Some(IpAddr::V4(ip)),
|
||||||
|
url::Host::Ipv6(ip) => Some(IpAddr::V6(ip)),
|
||||||
|
url::Host::Domain(_) => None,
|
||||||
|
}) {
|
||||||
|
ensure_public_ip(ip)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let port = parsed.port_or_known_default().ok_or_else(|| {
|
||||||
|
Error::validation(format!("Remote URL is missing a usable port: {}", raw_url))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let resolved = lookup_host((host, port))
|
||||||
|
.await
|
||||||
|
.map_err(|e| Error::validation(format!("Failed to resolve host '{}': {}", host, e)))?;
|
||||||
|
|
||||||
|
let mut saw_address = false;
|
||||||
|
for addr in resolved {
|
||||||
|
saw_address = true;
|
||||||
|
ensure_public_ip(addr.ip())?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if !saw_address {
|
||||||
|
return Err(Error::validation(format!(
|
||||||
|
"Remote URL host did not resolve to any addresses: {}",
|
||||||
|
host
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(parsed)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn validate_git_source(&self, raw_url: &str) -> Result<()> {
|
||||||
|
if raw_url.starts_with("http://") || raw_url.starts_with("https://") {
|
||||||
|
self.validate_remote_url(raw_url).await?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(host) = extract_git_host(raw_url) {
|
||||||
|
self.validate_remote_host(&host)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_remote_host(&self, host: &str) -> Result<()> {
|
||||||
|
let normalized_host = host.to_ascii_lowercase();
|
||||||
|
|
||||||
|
if normalized_host == "localhost" {
|
||||||
|
return Err(Error::validation(format!(
|
||||||
|
"Remote host is not allowed: {}",
|
||||||
|
host
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(allowed_remote_hosts) = &self.allowed_remote_hosts {
|
||||||
|
if !allowed_remote_hosts.contains(&normalized_host) {
|
||||||
|
return Err(Error::validation(format!(
|
||||||
|
"Remote host '{}' is not in the configured allowlist. Add it to pack_registry.allowed_source_hosts.",
|
||||||
|
host
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Extract an archive (zip or tar.gz)
|
/// Extract an archive (zip or tar.gz)
|
||||||
async fn extract_archive(&self, archive_path: &Path) -> Result<PathBuf> {
|
async fn extract_archive(&self, archive_path: &Path) -> Result<PathBuf> {
|
||||||
let extract_dir = self.create_temp_dir().await?;
|
let extract_dir = self.create_temp_dir().await?;
|
||||||
@@ -583,6 +717,7 @@ impl PackInstaller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check in first subdirectory (common for GitHub archives)
|
// Check in first subdirectory (common for GitHub archives)
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Archive inspection is limited to the temporary extraction directory created by this installer.
|
||||||
let mut entries = fs::read_dir(base_dir)
|
let mut entries = fs::read_dir(base_dir)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| Error::internal(format!("Failed to read directory: {}", e)))?;
|
.map_err(|e| Error::internal(format!("Failed to read directory: {}", e)))?;
|
||||||
@@ -618,6 +753,7 @@ impl PackInstaller {
|
|||||||
})?;
|
})?;
|
||||||
|
|
||||||
// Read source directory
|
// Read source directory
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Directory copy operates on installer-managed local paths, not request-derived paths.
|
||||||
let mut entries = fs::read_dir(src)
|
let mut entries = fs::read_dir(src)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| Error::internal(format!("Failed to read source directory: {}", e)))?;
|
.map_err(|e| Error::internal(format!("Failed to read source directory: {}", e)))?;
|
||||||
@@ -674,6 +810,111 @@ impl PackInstaller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn collect_allowed_remote_hosts(config: &PackRegistryConfig) -> Result<HashSet<String>> {
|
||||||
|
let mut hosts = HashSet::new();
|
||||||
|
|
||||||
|
for index in &config.indices {
|
||||||
|
if !index.enabled {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let parsed = Url::parse(&index.url).map_err(|e| {
|
||||||
|
Error::validation(format!("Invalid registry index URL '{}': {}", index.url, e))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let host = parsed.host_str().ok_or_else(|| {
|
||||||
|
Error::validation(format!(
|
||||||
|
"Registry index URL '{}' is missing a host",
|
||||||
|
index.url
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
hosts.insert(host.to_ascii_lowercase());
|
||||||
|
}
|
||||||
|
|
||||||
|
for host in &config.allowed_source_hosts {
|
||||||
|
let normalized = host.trim().to_ascii_lowercase();
|
||||||
|
if !normalized.is_empty() {
|
||||||
|
hosts.insert(normalized);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(hosts)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extract_git_host(raw_url: &str) -> Option<String> {
|
||||||
|
if let Ok(parsed) = Url::parse(raw_url) {
|
||||||
|
return parsed.host_str().map(|host| host.to_ascii_lowercase());
|
||||||
|
}
|
||||||
|
|
||||||
|
raw_url.split_once('@').and_then(|(_, rest)| {
|
||||||
|
rest.split_once(':')
|
||||||
|
.map(|(host, _)| host.to_ascii_lowercase())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn archive_filename_from_url(url: &Url) -> String {
|
||||||
|
let raw_name = url
|
||||||
|
.path_segments()
|
||||||
|
.and_then(|mut segments| segments.rfind(|segment| !segment.is_empty()))
|
||||||
|
.unwrap_or("archive.bin");
|
||||||
|
|
||||||
|
let sanitized: String = raw_name
|
||||||
|
.chars()
|
||||||
|
.map(|ch| match ch {
|
||||||
|
'a'..='z' | 'A'..='Z' | '0'..='9' | '.' | '-' | '_' => ch,
|
||||||
|
_ => '_',
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let filename = sanitized.trim_matches('.');
|
||||||
|
if filename.is_empty() {
|
||||||
|
"archive.bin".to_string()
|
||||||
|
} else {
|
||||||
|
filename.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ensure_public_ip(ip: IpAddr) -> Result<()> {
|
||||||
|
let is_blocked = match ip {
|
||||||
|
IpAddr::V4(ip) => {
|
||||||
|
let octets = ip.octets();
|
||||||
|
let is_documentation_range = matches!(
|
||||||
|
octets,
|
||||||
|
[192, 0, 2, _] | [198, 51, 100, _] | [203, 0, 113, _]
|
||||||
|
);
|
||||||
|
ip.is_private()
|
||||||
|
|| ip.is_loopback()
|
||||||
|
|| ip.is_link_local()
|
||||||
|
|| ip.is_multicast()
|
||||||
|
|| ip.is_broadcast()
|
||||||
|
|| is_documentation_range
|
||||||
|
|| ip.is_unspecified()
|
||||||
|
|| octets[0] == 0
|
||||||
|
}
|
||||||
|
IpAddr::V6(ip) => {
|
||||||
|
let segments = ip.segments();
|
||||||
|
let is_documentation_range = segments[0] == 0x2001 && segments[1] == 0x0db8;
|
||||||
|
ip.is_loopback()
|
||||||
|
|| ip.is_unspecified()
|
||||||
|
|| ip.is_multicast()
|
||||||
|
|| ip.is_unique_local()
|
||||||
|
|| ip.is_unicast_link_local()
|
||||||
|
|| is_documentation_range
|
||||||
|
|| ip == Ipv6Addr::LOCALHOST
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if is_blocked {
|
||||||
|
return Err(Error::validation(format!(
|
||||||
|
"Remote URL resolved to a non-public address: {}",
|
||||||
|
ip
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -721,4 +962,52 @@ mod tests {
|
|||||||
|
|
||||||
assert!(matches!(source, InstallSource::Git { .. }));
|
assert!(matches!(source, InstallSource::Git { .. }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_archive_filename_from_url_sanitizes_path_segments() {
|
||||||
|
let url = Url::parse("https://example.com/releases/../../pack.zip?token=x").unwrap();
|
||||||
|
assert_eq!(archive_filename_from_url(&url), "pack.zip");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ensure_public_ip_rejects_private_ipv4() {
|
||||||
|
let err = ensure_public_ip(IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 1))).unwrap_err();
|
||||||
|
assert!(err.to_string().contains("non-public"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_collect_allowed_remote_hosts_includes_indices_and_overrides() {
|
||||||
|
let config = PackRegistryConfig {
|
||||||
|
indices: vec![crate::config::RegistryIndexConfig {
|
||||||
|
url: "https://registry.example.com/index.json".to_string(),
|
||||||
|
priority: 1,
|
||||||
|
enabled: true,
|
||||||
|
name: None,
|
||||||
|
headers: std::collections::HashMap::new(),
|
||||||
|
}],
|
||||||
|
allowed_source_hosts: vec!["github.com".to_string(), "cdn.example.com".to_string()],
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let hosts = collect_allowed_remote_hosts(&config).unwrap();
|
||||||
|
assert!(hosts.contains("registry.example.com"));
|
||||||
|
assert!(hosts.contains("github.com"));
|
||||||
|
assert!(hosts.contains("cdn.example.com"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_extract_git_host_from_scp_style_source() {
|
||||||
|
assert_eq!(
|
||||||
|
extract_git_host("git@github.com:org/repo.git"),
|
||||||
|
Some("github.com".to_string())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_extract_git_host_from_git_scheme_source() {
|
||||||
|
assert_eq!(
|
||||||
|
extract_git_host("git://github.com/org/repo.git"),
|
||||||
|
Some("github.com".to_string())
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
//! can reference the same workflow file with different configurations.
|
//! can reference the same workflow file with different configurations.
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::Path;
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use tracing::{debug, info, warn};
|
use tracing::{debug, info, warn};
|
||||||
@@ -404,6 +404,16 @@ impl<'a> PackComponentLoader<'a> {
|
|||||||
.and_then(|v| serde_json::to_value(v).ok())
|
.and_then(|v| serde_json::to_value(v).ok())
|
||||||
.unwrap_or_else(|| serde_json::json!({}));
|
.unwrap_or_else(|| serde_json::json!({}));
|
||||||
|
|
||||||
|
let aliases: Vec<String> = data
|
||||||
|
.get("aliases")
|
||||||
|
.and_then(|v| v.as_sequence())
|
||||||
|
.map(|arr| {
|
||||||
|
arr.iter()
|
||||||
|
.filter_map(|v| v.as_str().map(|s| s.to_ascii_lowercase()))
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
// Check if runtime already exists — update in place if so
|
// Check if runtime already exists — update in place if so
|
||||||
if let Some(existing) = RuntimeRepository::find_by_ref(self.pool, &runtime_ref).await? {
|
if let Some(existing) = RuntimeRepository::find_by_ref(self.pool, &runtime_ref).await? {
|
||||||
let update_input = UpdateRuntimeInput {
|
let update_input = UpdateRuntimeInput {
|
||||||
@@ -418,6 +428,8 @@ impl<'a> PackComponentLoader<'a> {
|
|||||||
None => Patch::Clear,
|
None => Patch::Clear,
|
||||||
}),
|
}),
|
||||||
execution_config: Some(execution_config),
|
execution_config: Some(execution_config),
|
||||||
|
aliases: Some(aliases),
|
||||||
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
match RuntimeRepository::update(self.pool, existing.id, update_input).await {
|
match RuntimeRepository::update(self.pool, existing.id, update_input).await {
|
||||||
@@ -448,6 +460,9 @@ impl<'a> PackComponentLoader<'a> {
|
|||||||
distributions,
|
distributions,
|
||||||
installation,
|
installation,
|
||||||
execution_config,
|
execution_config,
|
||||||
|
aliases,
|
||||||
|
auto_detected: false,
|
||||||
|
detection_config: serde_json::json!({}),
|
||||||
};
|
};
|
||||||
|
|
||||||
match RuntimeRepository::create(self.pool, input).await {
|
match RuntimeRepository::create(self.pool, input).await {
|
||||||
@@ -710,8 +725,7 @@ impl<'a> PackComponentLoader<'a> {
|
|||||||
let description = data
|
let description = data
|
||||||
.get("description")
|
.get("description")
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.unwrap_or("")
|
.map(|s| s.to_string());
|
||||||
.to_string();
|
|
||||||
|
|
||||||
let enabled = data
|
let enabled = data
|
||||||
.get("enabled")
|
.get("enabled")
|
||||||
@@ -730,7 +744,10 @@ impl<'a> PackComponentLoader<'a> {
|
|||||||
if let Some(existing) = TriggerRepository::find_by_ref(self.pool, &trigger_ref).await? {
|
if let Some(existing) = TriggerRepository::find_by_ref(self.pool, &trigger_ref).await? {
|
||||||
let update_input = UpdateTriggerInput {
|
let update_input = UpdateTriggerInput {
|
||||||
label: Some(label),
|
label: Some(label),
|
||||||
description: Some(Patch::Set(description)),
|
description: Some(match description {
|
||||||
|
Some(description) => Patch::Set(description),
|
||||||
|
None => Patch::Clear,
|
||||||
|
}),
|
||||||
enabled: Some(enabled),
|
enabled: Some(enabled),
|
||||||
param_schema: Some(match param_schema {
|
param_schema: Some(match param_schema {
|
||||||
Some(value) => Patch::Set(value),
|
Some(value) => Patch::Set(value),
|
||||||
@@ -763,7 +780,7 @@ impl<'a> PackComponentLoader<'a> {
|
|||||||
pack: Some(self.pack_id),
|
pack: Some(self.pack_id),
|
||||||
pack_ref: Some(self.pack_ref.clone()),
|
pack_ref: Some(self.pack_ref.clone()),
|
||||||
label,
|
label,
|
||||||
description: Some(description),
|
description,
|
||||||
enabled,
|
enabled,
|
||||||
param_schema,
|
param_schema,
|
||||||
out_schema,
|
out_schema,
|
||||||
@@ -843,8 +860,7 @@ impl<'a> PackComponentLoader<'a> {
|
|||||||
let description = data
|
let description = data
|
||||||
.get("description")
|
.get("description")
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.unwrap_or("")
|
.map(|s| s.to_string());
|
||||||
.to_string();
|
|
||||||
|
|
||||||
// ── Workflow file handling ──────────────────────────────────
|
// ── Workflow file handling ──────────────────────────────────
|
||||||
// If the action declares `workflow_file`, load the referenced
|
// If the action declares `workflow_file`, load the referenced
|
||||||
@@ -861,7 +877,7 @@ impl<'a> PackComponentLoader<'a> {
|
|||||||
wf_path,
|
wf_path,
|
||||||
&action_ref,
|
&action_ref,
|
||||||
&label,
|
&label,
|
||||||
&description,
|
description.as_deref().unwrap_or(""),
|
||||||
&data,
|
&data,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
@@ -941,7 +957,10 @@ impl<'a> PackComponentLoader<'a> {
|
|||||||
if let Some(existing) = ActionRepository::find_by_ref(self.pool, &action_ref).await? {
|
if let Some(existing) = ActionRepository::find_by_ref(self.pool, &action_ref).await? {
|
||||||
let update_input = UpdateActionInput {
|
let update_input = UpdateActionInput {
|
||||||
label: Some(label),
|
label: Some(label),
|
||||||
description: Some(description),
|
description: Some(match description {
|
||||||
|
Some(description) => Patch::Set(description),
|
||||||
|
None => Patch::Clear,
|
||||||
|
}),
|
||||||
entrypoint: Some(entrypoint),
|
entrypoint: Some(entrypoint),
|
||||||
runtime: runtime_id,
|
runtime: runtime_id,
|
||||||
runtime_version_constraint: Some(match runtime_version_constraint {
|
runtime_version_constraint: Some(match runtime_version_constraint {
|
||||||
@@ -1072,7 +1091,10 @@ impl<'a> PackComponentLoader<'a> {
|
|||||||
action_description: &str,
|
action_description: &str,
|
||||||
action_data: &serde_yaml_ng::Value,
|
action_data: &serde_yaml_ng::Value,
|
||||||
) -> Result<Id> {
|
) -> Result<Id> {
|
||||||
let full_path = actions_dir.join(workflow_file_path);
|
let pack_root = actions_dir.parent().ok_or_else(|| {
|
||||||
|
Error::validation("Actions directory must live inside a pack directory".to_string())
|
||||||
|
})?;
|
||||||
|
let full_path = resolve_pack_relative_path(pack_root, actions_dir, workflow_file_path)?;
|
||||||
if !full_path.exists() {
|
if !full_path.exists() {
|
||||||
return Err(Error::validation(format!(
|
return Err(Error::validation(format!(
|
||||||
"Workflow file '{}' not found at '{}'",
|
"Workflow file '{}' not found at '{}'",
|
||||||
@@ -1081,6 +1103,7 @@ impl<'a> PackComponentLoader<'a> {
|
|||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- The workflow path is normalized and confined to the pack root before this local read.
|
||||||
let content = std::fs::read_to_string(&full_path).map_err(|e| {
|
let content = std::fs::read_to_string(&full_path).map_err(|e| {
|
||||||
Error::io(format!(
|
Error::io(format!(
|
||||||
"Failed to read workflow file '{}': {}",
|
"Failed to read workflow file '{}': {}",
|
||||||
@@ -1295,8 +1318,7 @@ impl<'a> PackComponentLoader<'a> {
|
|||||||
let description = data
|
let description = data
|
||||||
.get("description")
|
.get("description")
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.unwrap_or("")
|
.map(|s| s.to_string());
|
||||||
.to_string();
|
|
||||||
|
|
||||||
let enabled = data
|
let enabled = data
|
||||||
.get("enabled")
|
.get("enabled")
|
||||||
@@ -1332,7 +1354,10 @@ impl<'a> PackComponentLoader<'a> {
|
|||||||
if let Some(existing) = SensorRepository::find_by_ref(self.pool, &sensor_ref).await? {
|
if let Some(existing) = SensorRepository::find_by_ref(self.pool, &sensor_ref).await? {
|
||||||
let update_input = UpdateSensorInput {
|
let update_input = UpdateSensorInput {
|
||||||
label: Some(label),
|
label: Some(label),
|
||||||
description: Some(description),
|
description: Some(match description {
|
||||||
|
Some(description) => Patch::Set(description),
|
||||||
|
None => Patch::Clear,
|
||||||
|
}),
|
||||||
entrypoint: Some(entrypoint),
|
entrypoint: Some(entrypoint),
|
||||||
runtime: Some(sensor_runtime_id),
|
runtime: Some(sensor_runtime_id),
|
||||||
runtime_ref: Some(sensor_runtime_ref.clone()),
|
runtime_ref: Some(sensor_runtime_ref.clone()),
|
||||||
@@ -1628,11 +1653,60 @@ impl<'a> PackComponentLoader<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn resolve_pack_relative_path(
|
||||||
|
pack_root: &Path,
|
||||||
|
base_dir: &Path,
|
||||||
|
relative_path: &str,
|
||||||
|
) -> Result<PathBuf> {
|
||||||
|
let canonical_pack_root = pack_root.canonicalize().map_err(|e| {
|
||||||
|
Error::io(format!(
|
||||||
|
"Failed to resolve pack root '{}': {}",
|
||||||
|
pack_root.display(),
|
||||||
|
e
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
let canonical_base_dir = base_dir.canonicalize().map_err(|e| {
|
||||||
|
Error::io(format!(
|
||||||
|
"Failed to resolve base directory '{}': {}",
|
||||||
|
base_dir.display(),
|
||||||
|
e
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
let canonical_candidate = normalize_path_from_base(&canonical_base_dir, relative_path);
|
||||||
|
|
||||||
|
if !canonical_candidate.starts_with(&canonical_pack_root) {
|
||||||
|
return Err(Error::validation(format!(
|
||||||
|
"Resolved path '{}' escapes pack root '{}'",
|
||||||
|
canonical_candidate.display(),
|
||||||
|
canonical_pack_root.display()
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(canonical_candidate)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn normalize_path_from_base(base: &Path, relative_path: &str) -> PathBuf {
|
||||||
|
let mut normalized = PathBuf::new();
|
||||||
|
for component in base.join(relative_path).components() {
|
||||||
|
match component {
|
||||||
|
std::path::Component::Prefix(prefix) => normalized.push(prefix.as_os_str()),
|
||||||
|
std::path::Component::RootDir => normalized.push(std::path::MAIN_SEPARATOR.to_string()),
|
||||||
|
std::path::Component::CurDir => {}
|
||||||
|
std::path::Component::ParentDir => {
|
||||||
|
normalized.pop();
|
||||||
|
}
|
||||||
|
std::path::Component::Normal(part) => normalized.push(part),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
normalized
|
||||||
|
}
|
||||||
|
|
||||||
/// Read all YAML files from a directory, returning `(filename, content)` pairs
|
/// Read all YAML files from a directory, returning `(filename, content)` pairs
|
||||||
/// sorted by filename for deterministic ordering.
|
/// sorted by filename for deterministic ordering.
|
||||||
fn read_yaml_files(dir: &Path) -> Result<Vec<(String, String)>> {
|
fn read_yaml_files(dir: &Path) -> Result<Vec<(String, String)>> {
|
||||||
let mut files = Vec::new();
|
let mut files = Vec::new();
|
||||||
|
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Pack loader scans pack-owned directories on disk after selecting the pack root.
|
||||||
let entries = std::fs::read_dir(dir)
|
let entries = std::fs::read_dir(dir)
|
||||||
.map_err(|e| Error::io(format!("Failed to read directory {}: {}", dir.display(), e)))?;
|
.map_err(|e| Error::io(format!("Failed to read directory {}: {}", dir.display(), e)))?;
|
||||||
|
|
||||||
@@ -1655,6 +1729,7 @@ fn read_yaml_files(dir: &Path) -> Result<Vec<(String, String)>> {
|
|||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
let filename = entry.file_name().to_string_lossy().to_string();
|
let filename = entry.file_name().to_string_lossy().to_string();
|
||||||
|
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- YAML files are read only after being discovered under the selected pack directory.
|
||||||
let content = std::fs::read_to_string(&path)
|
let content = std::fs::read_to_string(&path)
|
||||||
.map_err(|e| Error::io(format!("Failed to read file {}: {}", path.display(), e)))?;
|
.map_err(|e| Error::io(format!("Failed to read file {}: {}", path.display(), e)))?;
|
||||||
|
|
||||||
|
|||||||
@@ -292,6 +292,7 @@ fn copy_dir_all(src: &Path, dst: &Path) -> Result<()> {
|
|||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Pack storage copy recursively processes validated local directories under the configured pack store.
|
||||||
for entry in fs::read_dir(src).map_err(|e| {
|
for entry in fs::read_dir(src).map_err(|e| {
|
||||||
Error::io(format!(
|
Error::io(format!(
|
||||||
"Failed to read source directory {}: {}",
|
"Failed to read source directory {}: {}",
|
||||||
|
|||||||
@@ -21,10 +21,6 @@ pub enum Resource {
|
|||||||
Inquiries,
|
Inquiries,
|
||||||
Keys,
|
Keys,
|
||||||
Artifacts,
|
Artifacts,
|
||||||
Workflows,
|
|
||||||
Webhooks,
|
|
||||||
Analytics,
|
|
||||||
History,
|
|
||||||
Identities,
|
Identities,
|
||||||
Permissions,
|
Permissions,
|
||||||
}
|
}
|
||||||
@@ -40,6 +36,7 @@ pub enum Action {
|
|||||||
Cancel,
|
Cancel,
|
||||||
Respond,
|
Respond,
|
||||||
Manage,
|
Manage,
|
||||||
|
Decrypt,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
@@ -69,6 +66,8 @@ pub struct GrantConstraints {
|
|||||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
pub owner_types: Option<Vec<OwnerType>>,
|
pub owner_types: Option<Vec<OwnerType>>,
|
||||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
|
pub owner_refs: Option<Vec<String>>,
|
||||||
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
pub visibility: Option<Vec<ArtifactVisibility>>,
|
pub visibility: Option<Vec<ArtifactVisibility>>,
|
||||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
pub execution_scope: Option<ExecutionScopeConstraint>,
|
pub execution_scope: Option<ExecutionScopeConstraint>,
|
||||||
@@ -99,6 +98,7 @@ pub struct AuthorizationContext {
|
|||||||
pub pack_ref: Option<String>,
|
pub pack_ref: Option<String>,
|
||||||
pub owner_identity_id: Option<Id>,
|
pub owner_identity_id: Option<Id>,
|
||||||
pub owner_type: Option<OwnerType>,
|
pub owner_type: Option<OwnerType>,
|
||||||
|
pub owner_ref: Option<String>,
|
||||||
pub visibility: Option<ArtifactVisibility>,
|
pub visibility: Option<ArtifactVisibility>,
|
||||||
pub encrypted: Option<bool>,
|
pub encrypted: Option<bool>,
|
||||||
pub execution_owner_identity_id: Option<Id>,
|
pub execution_owner_identity_id: Option<Id>,
|
||||||
@@ -115,6 +115,7 @@ impl AuthorizationContext {
|
|||||||
pack_ref: None,
|
pack_ref: None,
|
||||||
owner_identity_id: None,
|
owner_identity_id: None,
|
||||||
owner_type: None,
|
owner_type: None,
|
||||||
|
owner_ref: None,
|
||||||
visibility: None,
|
visibility: None,
|
||||||
encrypted: None,
|
encrypted: None,
|
||||||
execution_owner_identity_id: None,
|
execution_owner_identity_id: None,
|
||||||
@@ -162,6 +163,15 @@ impl Grant {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(owner_refs) = &constraints.owner_refs {
|
||||||
|
let Some(owner_ref) = &ctx.owner_ref else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
if !owner_refs.contains(owner_ref) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(visibility) = &constraints.visibility {
|
if let Some(visibility) = &constraints.visibility {
|
||||||
let Some(target_visibility) = ctx.visibility else {
|
let Some(target_visibility) = ctx.visibility else {
|
||||||
return false;
|
return false;
|
||||||
@@ -289,4 +299,28 @@ mod tests {
|
|||||||
.insert("team".to_string(), json!("infra"));
|
.insert("team".to_string(), json!("infra"));
|
||||||
assert!(!grant.allows(Resource::Packs, Action::Read, &ctx));
|
assert!(!grant.allows(Resource::Packs, Action::Read, &ctx));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn owner_ref_constraint_requires_exact_value_match() {
|
||||||
|
let grant = Grant {
|
||||||
|
resource: Resource::Artifacts,
|
||||||
|
actions: vec![Action::Read],
|
||||||
|
constraints: Some(GrantConstraints {
|
||||||
|
owner_types: Some(vec![OwnerType::Pack]),
|
||||||
|
owner_refs: Some(vec!["python_example".to_string()]),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut ctx = AuthorizationContext::new(1);
|
||||||
|
ctx.owner_type = Some(OwnerType::Pack);
|
||||||
|
ctx.owner_ref = Some("python_example".to_string());
|
||||||
|
assert!(grant.allows(Resource::Artifacts, Action::Read, &ctx));
|
||||||
|
|
||||||
|
ctx.owner_ref = Some("other_pack".to_string());
|
||||||
|
assert!(!grant.allows(Resource::Artifacts, Action::Read, &ctx));
|
||||||
|
|
||||||
|
ctx.owner_ref = None;
|
||||||
|
assert!(!grant.allows(Resource::Artifacts, Action::Read, &ctx));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ pub struct CreateActionInput {
|
|||||||
pub pack: Id,
|
pub pack: Id,
|
||||||
pub pack_ref: String,
|
pub pack_ref: String,
|
||||||
pub label: String,
|
pub label: String,
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
pub entrypoint: String,
|
pub entrypoint: String,
|
||||||
pub runtime: Option<Id>,
|
pub runtime: Option<Id>,
|
||||||
pub runtime_version_constraint: Option<String>,
|
pub runtime_version_constraint: Option<String>,
|
||||||
@@ -64,7 +64,7 @@ pub struct CreateActionInput {
|
|||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct UpdateActionInput {
|
pub struct UpdateActionInput {
|
||||||
pub label: Option<String>,
|
pub label: Option<String>,
|
||||||
pub description: Option<String>,
|
pub description: Option<Patch<String>>,
|
||||||
pub entrypoint: Option<String>,
|
pub entrypoint: Option<String>,
|
||||||
pub runtime: Option<Id>,
|
pub runtime: Option<Id>,
|
||||||
pub runtime_version_constraint: Option<Patch<String>>,
|
pub runtime_version_constraint: Option<Patch<String>>,
|
||||||
@@ -210,7 +210,10 @@ impl Update for ActionRepository {
|
|||||||
query.push(", ");
|
query.push(", ");
|
||||||
}
|
}
|
||||||
query.push("description = ");
|
query.push("description = ");
|
||||||
query.push_bind(description);
|
match description {
|
||||||
|
Patch::Set(value) => query.push_bind(value),
|
||||||
|
Patch::Clear => query.push_bind(Option::<String>::None),
|
||||||
|
};
|
||||||
has_updates = true;
|
has_updates = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -568,7 +571,7 @@ impl Repository for PolicyRepository {
|
|||||||
type Entity = Policy;
|
type Entity = Policy;
|
||||||
|
|
||||||
fn table_name() -> &'static str {
|
fn table_name() -> &'static str {
|
||||||
"policies"
|
"policy"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -609,7 +612,7 @@ impl FindById for PolicyRepository {
|
|||||||
r#"
|
r#"
|
||||||
SELECT id, ref, pack, pack_ref, action, action_ref, parameters, method,
|
SELECT id, ref, pack, pack_ref, action, action_ref, parameters, method,
|
||||||
threshold, name, description, tags, created, updated
|
threshold, name, description, tags, created, updated
|
||||||
FROM policies
|
FROM policy
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
@@ -631,7 +634,7 @@ impl FindByRef for PolicyRepository {
|
|||||||
r#"
|
r#"
|
||||||
SELECT id, ref, pack, pack_ref, action, action_ref, parameters, method,
|
SELECT id, ref, pack, pack_ref, action, action_ref, parameters, method,
|
||||||
threshold, name, description, tags, created, updated
|
threshold, name, description, tags, created, updated
|
||||||
FROM policies
|
FROM policy
|
||||||
WHERE ref = $1
|
WHERE ref = $1
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
@@ -653,7 +656,7 @@ impl List for PolicyRepository {
|
|||||||
r#"
|
r#"
|
||||||
SELECT id, ref, pack, pack_ref, action, action_ref, parameters, method,
|
SELECT id, ref, pack, pack_ref, action, action_ref, parameters, method,
|
||||||
threshold, name, description, tags, created, updated
|
threshold, name, description, tags, created, updated
|
||||||
FROM policies
|
FROM policy
|
||||||
ORDER BY ref ASC
|
ORDER BY ref ASC
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
@@ -675,7 +678,7 @@ impl Create for PolicyRepository {
|
|||||||
// Try to insert - database will enforce uniqueness constraint
|
// Try to insert - database will enforce uniqueness constraint
|
||||||
let policy = sqlx::query_as::<_, Policy>(
|
let policy = sqlx::query_as::<_, Policy>(
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO policies (ref, pack, pack_ref, action, action_ref, parameters,
|
INSERT INTO policy (ref, pack, pack_ref, action, action_ref, parameters,
|
||||||
method, threshold, name, description, tags)
|
method, threshold, name, description, tags)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
||||||
RETURNING id, ref, pack, pack_ref, action, action_ref, parameters, method,
|
RETURNING id, ref, pack, pack_ref, action, action_ref, parameters, method,
|
||||||
@@ -717,7 +720,7 @@ impl Update for PolicyRepository {
|
|||||||
where
|
where
|
||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
let mut query = QueryBuilder::new("UPDATE policies SET ");
|
let mut query = QueryBuilder::new("UPDATE policy SET ");
|
||||||
let mut has_updates = false;
|
let mut has_updates = false;
|
||||||
|
|
||||||
if let Some(parameters) = &input.parameters {
|
if let Some(parameters) = &input.parameters {
|
||||||
@@ -795,7 +798,7 @@ impl Delete for PolicyRepository {
|
|||||||
where
|
where
|
||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
let result = sqlx::query("DELETE FROM policies WHERE id = $1")
|
let result = sqlx::query("DELETE FROM policy WHERE id = $1")
|
||||||
.bind(id)
|
.bind(id)
|
||||||
.execute(executor)
|
.execute(executor)
|
||||||
.await?;
|
.await?;
|
||||||
@@ -814,7 +817,7 @@ impl PolicyRepository {
|
|||||||
r#"
|
r#"
|
||||||
SELECT id, ref, pack, pack_ref, action, action_ref, parameters, method,
|
SELECT id, ref, pack, pack_ref, action, action_ref, parameters, method,
|
||||||
threshold, name, description, tags, created, updated
|
threshold, name, description, tags, created, updated
|
||||||
FROM policies
|
FROM policy
|
||||||
WHERE action = $1
|
WHERE action = $1
|
||||||
ORDER BY ref ASC
|
ORDER BY ref ASC
|
||||||
"#,
|
"#,
|
||||||
@@ -835,7 +838,7 @@ impl PolicyRepository {
|
|||||||
r#"
|
r#"
|
||||||
SELECT id, ref, pack, pack_ref, action, action_ref, parameters, method,
|
SELECT id, ref, pack, pack_ref, action, action_ref, parameters, method,
|
||||||
threshold, name, description, tags, created, updated
|
threshold, name, description, tags, created, updated
|
||||||
FROM policies
|
FROM policy
|
||||||
WHERE $1 = ANY(tags)
|
WHERE $1 = ANY(tags)
|
||||||
ORDER BY ref ASC
|
ORDER BY ref ASC
|
||||||
"#,
|
"#,
|
||||||
@@ -846,4 +849,69 @@ impl PolicyRepository {
|
|||||||
|
|
||||||
Ok(policies)
|
Ok(policies)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Find the most recent action-specific policy.
|
||||||
|
pub async fn find_latest_by_action<'e, E>(executor: E, action_id: Id) -> Result<Option<Policy>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
let policy = sqlx::query_as::<_, Policy>(
|
||||||
|
r#"
|
||||||
|
SELECT id, ref, pack, pack_ref, action, action_ref, parameters, method,
|
||||||
|
threshold, name, description, tags, created, updated
|
||||||
|
FROM policy
|
||||||
|
WHERE action = $1
|
||||||
|
ORDER BY created DESC
|
||||||
|
LIMIT 1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(action_id)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(policy)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Find the most recent pack-specific policy.
|
||||||
|
pub async fn find_latest_by_pack<'e, E>(executor: E, pack_id: Id) -> Result<Option<Policy>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
let policy = sqlx::query_as::<_, Policy>(
|
||||||
|
r#"
|
||||||
|
SELECT id, ref, pack, pack_ref, action, action_ref, parameters, method,
|
||||||
|
threshold, name, description, tags, created, updated
|
||||||
|
FROM policy
|
||||||
|
WHERE pack = $1 AND action IS NULL
|
||||||
|
ORDER BY created DESC
|
||||||
|
LIMIT 1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(pack_id)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(policy)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Find the most recent global policy.
|
||||||
|
pub async fn find_latest_global<'e, E>(executor: E) -> Result<Option<Policy>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
let policy = sqlx::query_as::<_, Policy>(
|
||||||
|
r#"
|
||||||
|
SELECT id, ref, pack, pack_ref, action, action_ref, parameters, method,
|
||||||
|
threshold, name, description, tags, created, updated
|
||||||
|
FROM policy
|
||||||
|
WHERE pack IS NULL AND action IS NULL
|
||||||
|
ORDER BY created DESC
|
||||||
|
LIMIT 1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(policy)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ pub struct EnforcementVolumeBucket {
|
|||||||
pub enforcement_count: i64,
|
pub enforcement_count: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A single hourly bucket of execution volume (from execution hypertable directly).
|
/// A single hourly bucket of execution volume (from the execution table directly).
|
||||||
#[derive(Debug, Clone, Serialize, FromRow)]
|
#[derive(Debug, Clone, Serialize, FromRow)]
|
||||||
pub struct ExecutionVolumeBucket {
|
pub struct ExecutionVolumeBucket {
|
||||||
/// Start of the 1-hour bucket
|
/// Start of the 1-hour bucket
|
||||||
@@ -468,7 +468,7 @@ impl AnalyticsRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// =======================================================================
|
// =======================================================================
|
||||||
// Execution volume (from execution hypertable directly)
|
// Execution volume (from the execution table directly)
|
||||||
// =======================================================================
|
// =======================================================================
|
||||||
|
|
||||||
/// Query the `execution_volume_hourly` continuous aggregate for execution
|
/// Query the `execution_volume_hourly` continuous aggregate for execution
|
||||||
|
|||||||
@@ -577,6 +577,14 @@ pub struct CreateArtifactVersionInput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ArtifactVersionRepository {
|
impl ArtifactVersionRepository {
|
||||||
|
fn select_columns_with_alias(alias: &str) -> String {
|
||||||
|
format!(
|
||||||
|
"{alias}.id, {alias}.artifact, {alias}.version, {alias}.content_type, \
|
||||||
|
{alias}.size_bytes, NULL::bytea AS content, {alias}.content_json, \
|
||||||
|
{alias}.file_path, {alias}.meta, {alias}.created_by, {alias}.created"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Find a version by ID (without binary content for performance)
|
/// Find a version by ID (without binary content for performance)
|
||||||
pub async fn find_by_id<'e, E>(executor: E, id: i64) -> Result<Option<ArtifactVersion>>
|
pub async fn find_by_id<'e, E>(executor: E, id: i64) -> Result<Option<ArtifactVersion>>
|
||||||
where
|
where
|
||||||
@@ -812,14 +820,11 @@ impl ArtifactVersionRepository {
|
|||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
let query = format!(
|
let query = format!(
|
||||||
"SELECT av.{} \
|
"SELECT {} \
|
||||||
FROM artifact_version av \
|
FROM artifact_version av \
|
||||||
JOIN artifact a ON av.artifact = a.id \
|
JOIN artifact a ON av.artifact = a.id \
|
||||||
WHERE a.execution = $1 AND av.file_path IS NOT NULL",
|
WHERE a.execution = $1 AND av.file_path IS NOT NULL",
|
||||||
artifact_version::SELECT_COLUMNS
|
Self::select_columns_with_alias("av")
|
||||||
.split(", ")
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(", av.")
|
|
||||||
);
|
);
|
||||||
sqlx::query_as::<_, ArtifactVersion>(&query)
|
sqlx::query_as::<_, ArtifactVersion>(&query)
|
||||||
.bind(execution_id)
|
.bind(execution_id)
|
||||||
@@ -847,3 +852,18 @@ impl ArtifactVersionRepository {
|
|||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::ArtifactVersionRepository;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn aliased_select_columns_keep_null_content_expression_unqualified() {
|
||||||
|
let columns = ArtifactVersionRepository::select_columns_with_alias("av");
|
||||||
|
|
||||||
|
assert!(columns.contains("av.id"));
|
||||||
|
assert!(columns.contains("av.file_path"));
|
||||||
|
assert!(columns.contains("NULL::bytea AS content"));
|
||||||
|
assert!(!columns.contains("av.NULL::bytea AS content"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -65,6 +65,12 @@ pub struct EnforcementSearchResult {
|
|||||||
pub total: u64,
|
pub total: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct EnforcementCreateOrGetResult {
|
||||||
|
pub enforcement: Enforcement,
|
||||||
|
pub created: bool,
|
||||||
|
}
|
||||||
|
|
||||||
/// Repository for Event operations
|
/// Repository for Event operations
|
||||||
pub struct EventRepository;
|
pub struct EventRepository;
|
||||||
|
|
||||||
@@ -416,7 +422,115 @@ impl Update for EnforcementRepository {
|
|||||||
where
|
where
|
||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
// Build update query
|
if input.status.is_none() && input.payload.is_none() && input.resolved_at.is_none() {
|
||||||
|
return Self::get_by_id(executor, id).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
Self::update_with_locator(executor, input, |query| {
|
||||||
|
query.push(" WHERE id = ");
|
||||||
|
query.push_bind(id);
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl Delete for EnforcementRepository {
|
||||||
|
async fn delete<'e, E>(executor: E, id: i64) -> Result<bool>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
let result = sqlx::query("DELETE FROM enforcement WHERE id = $1")
|
||||||
|
.bind(id)
|
||||||
|
.execute(executor)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(result.rows_affected() > 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EnforcementRepository {
|
||||||
|
async fn update_with_locator<'e, E, F>(
|
||||||
|
executor: E,
|
||||||
|
input: UpdateEnforcementInput,
|
||||||
|
where_clause: F,
|
||||||
|
) -> Result<Enforcement>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
F: FnOnce(&mut QueryBuilder<'_, Postgres>),
|
||||||
|
{
|
||||||
|
let mut query = QueryBuilder::new("UPDATE enforcement SET ");
|
||||||
|
let mut has_updates = false;
|
||||||
|
|
||||||
|
if let Some(status) = input.status {
|
||||||
|
query.push("status = ");
|
||||||
|
query.push_bind(status);
|
||||||
|
has_updates = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(payload) = &input.payload {
|
||||||
|
if has_updates {
|
||||||
|
query.push(", ");
|
||||||
|
}
|
||||||
|
query.push("payload = ");
|
||||||
|
query.push_bind(payload);
|
||||||
|
has_updates = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(resolved_at) = input.resolved_at {
|
||||||
|
if has_updates {
|
||||||
|
query.push(", ");
|
||||||
|
}
|
||||||
|
query.push("resolved_at = ");
|
||||||
|
query.push_bind(resolved_at);
|
||||||
|
}
|
||||||
|
|
||||||
|
where_clause(&mut query);
|
||||||
|
query.push(
|
||||||
|
" RETURNING id, rule, rule_ref, trigger_ref, config, event, status, payload, \
|
||||||
|
condition, conditions, created, resolved_at",
|
||||||
|
);
|
||||||
|
|
||||||
|
let enforcement = query
|
||||||
|
.build_query_as::<Enforcement>()
|
||||||
|
.fetch_one(executor)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(enforcement)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update an enforcement using the loaded row's primary key.
|
||||||
|
pub async fn update_loaded<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
enforcement: &Enforcement,
|
||||||
|
input: UpdateEnforcementInput,
|
||||||
|
) -> Result<Enforcement>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
if input.status.is_none() && input.payload.is_none() && input.resolved_at.is_none() {
|
||||||
|
return Ok(enforcement.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
Self::update_with_locator(executor, input, |query| {
|
||||||
|
query.push(" WHERE id = ");
|
||||||
|
query.push_bind(enforcement.id);
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_loaded_if_status<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
enforcement: &Enforcement,
|
||||||
|
expected_status: EnforcementStatus,
|
||||||
|
input: UpdateEnforcementInput,
|
||||||
|
) -> Result<Option<Enforcement>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
if input.status.is_none() && input.payload.is_none() && input.resolved_at.is_none() {
|
||||||
|
return Ok(Some(enforcement.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
let mut query = QueryBuilder::new("UPDATE enforcement SET ");
|
let mut query = QueryBuilder::new("UPDATE enforcement SET ");
|
||||||
let mut has_updates = false;
|
let mut has_updates = false;
|
||||||
@@ -446,39 +560,25 @@ impl Update for EnforcementRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !has_updates {
|
if !has_updates {
|
||||||
// No updates requested, fetch and return existing entity
|
return Ok(Some(enforcement.clone()));
|
||||||
return Self::get_by_id(executor, id).await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
query.push(" WHERE id = ");
|
query.push(" WHERE id = ");
|
||||||
query.push_bind(id);
|
query.push_bind(enforcement.id);
|
||||||
query.push(" RETURNING id, rule, rule_ref, trigger_ref, config, event, status, payload, condition, conditions, created, resolved_at");
|
query.push(" AND status = ");
|
||||||
|
query.push_bind(expected_status);
|
||||||
|
query.push(
|
||||||
|
" RETURNING id, rule, rule_ref, trigger_ref, config, event, status, payload, \
|
||||||
|
condition, conditions, created, resolved_at",
|
||||||
|
);
|
||||||
|
|
||||||
let enforcement = query
|
query
|
||||||
.build_query_as::<Enforcement>()
|
.build_query_as::<Enforcement>()
|
||||||
.fetch_one(executor)
|
.fetch_optional(executor)
|
||||||
.await?;
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
Ok(enforcement)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
|
||||||
impl Delete for EnforcementRepository {
|
|
||||||
async fn delete<'e, E>(executor: E, id: i64) -> Result<bool>
|
|
||||||
where
|
|
||||||
E: Executor<'e, Database = Postgres> + 'e,
|
|
||||||
{
|
|
||||||
let result = sqlx::query("DELETE FROM enforcement WHERE id = $1")
|
|
||||||
.bind(id)
|
|
||||||
.execute(executor)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(result.rows_affected() > 0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EnforcementRepository {
|
|
||||||
/// Find enforcements by rule ID
|
/// Find enforcements by rule ID
|
||||||
pub async fn find_by_rule<'e, E>(executor: E, rule_id: Id) -> Result<Vec<Enforcement>>
|
pub async fn find_by_rule<'e, E>(executor: E, rule_id: Id) -> Result<Vec<Enforcement>>
|
||||||
where
|
where
|
||||||
@@ -545,6 +645,90 @@ impl EnforcementRepository {
|
|||||||
Ok(enforcements)
|
Ok(enforcements)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn find_by_rule_and_event<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
rule_id: Id,
|
||||||
|
event_id: Id,
|
||||||
|
) -> Result<Option<Enforcement>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
sqlx::query_as::<_, Enforcement>(
|
||||||
|
r#"
|
||||||
|
SELECT id, rule, rule_ref, trigger_ref, config, event, status, payload,
|
||||||
|
condition, conditions, created, resolved_at
|
||||||
|
FROM enforcement
|
||||||
|
WHERE rule = $1 AND event = $2
|
||||||
|
LIMIT 1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(rule_id)
|
||||||
|
.bind(event_id)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_or_get_by_rule_event<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
input: CreateEnforcementInput,
|
||||||
|
) -> Result<EnforcementCreateOrGetResult>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + Copy + 'e,
|
||||||
|
{
|
||||||
|
let (Some(rule_id), Some(event_id)) = (input.rule, input.event) else {
|
||||||
|
let enforcement = Self::create(executor, input).await?;
|
||||||
|
return Ok(EnforcementCreateOrGetResult {
|
||||||
|
enforcement,
|
||||||
|
created: true,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
let inserted = sqlx::query_as::<_, Enforcement>(
|
||||||
|
r#"
|
||||||
|
INSERT INTO enforcement (rule, rule_ref, trigger_ref, config, event, status,
|
||||||
|
payload, condition, conditions)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||||
|
ON CONFLICT (rule, event) WHERE rule IS NOT NULL AND event IS NOT NULL DO NOTHING
|
||||||
|
RETURNING id, rule, rule_ref, trigger_ref, config, event, status, payload,
|
||||||
|
condition, conditions, created, resolved_at
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(input.rule)
|
||||||
|
.bind(&input.rule_ref)
|
||||||
|
.bind(&input.trigger_ref)
|
||||||
|
.bind(&input.config)
|
||||||
|
.bind(input.event)
|
||||||
|
.bind(input.status)
|
||||||
|
.bind(&input.payload)
|
||||||
|
.bind(input.condition)
|
||||||
|
.bind(&input.conditions)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if let Some(enforcement) = inserted {
|
||||||
|
return Ok(EnforcementCreateOrGetResult {
|
||||||
|
enforcement,
|
||||||
|
created: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let enforcement = Self::find_by_rule_and_event(executor, rule_id, event_id)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| {
|
||||||
|
anyhow::anyhow!(
|
||||||
|
"enforcement for rule {} and event {} disappeared after dedupe conflict",
|
||||||
|
rule_id,
|
||||||
|
event_id
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(EnforcementCreateOrGetResult {
|
||||||
|
enforcement,
|
||||||
|
created: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Search enforcements with all filters pushed into SQL.
|
/// Search enforcements with all filters pushed into SQL.
|
||||||
///
|
///
|
||||||
/// All filter fields are combinable (AND). Pagination is server-side.
|
/// All filter fields are combinable (AND). Pagination is server-side.
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ use chrono::{DateTime, Utc};
|
|||||||
|
|
||||||
use crate::models::{enums::ExecutionStatus, execution::*, Id, JsonDict};
|
use crate::models::{enums::ExecutionStatus, execution::*, Id, JsonDict};
|
||||||
use crate::Result;
|
use crate::Result;
|
||||||
use sqlx::{Executor, Postgres, QueryBuilder};
|
use sqlx::{Executor, PgConnection, PgPool, Postgres, QueryBuilder};
|
||||||
|
use tokio::time::{sleep, Duration};
|
||||||
|
|
||||||
use super::{Create, Delete, FindById, List, Repository, Update};
|
use super::{Create, Delete, FindById, List, Repository, Update};
|
||||||
|
|
||||||
@@ -41,6 +42,18 @@ pub struct ExecutionSearchResult {
|
|||||||
pub total: u64,
|
pub total: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct WorkflowTaskExecutionCreateOrGetResult {
|
||||||
|
pub execution: Execution,
|
||||||
|
pub created: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct EnforcementExecutionCreateOrGetResult {
|
||||||
|
pub execution: Execution,
|
||||||
|
pub created: bool,
|
||||||
|
}
|
||||||
|
|
||||||
/// An execution row with optional `rule_ref` / `trigger_ref` populated from
|
/// An execution row with optional `rule_ref` / `trigger_ref` populated from
|
||||||
/// the joined `enforcement` table. This avoids a separate in-memory lookup.
|
/// the joined `enforcement` table. This avoids a separate in-memory lookup.
|
||||||
#[derive(Debug, Clone, sqlx::FromRow)]
|
#[derive(Debug, Clone, sqlx::FromRow)]
|
||||||
@@ -191,7 +204,577 @@ impl Update for ExecutionRepository {
|
|||||||
where
|
where
|
||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
// Build update query
|
if input.status.is_none()
|
||||||
|
&& input.result.is_none()
|
||||||
|
&& input.executor.is_none()
|
||||||
|
&& input.worker.is_none()
|
||||||
|
&& input.started_at.is_none()
|
||||||
|
&& input.workflow_task.is_none()
|
||||||
|
{
|
||||||
|
return Self::get_by_id(executor, id).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
Self::update_with_locator(executor, input, |query| {
|
||||||
|
query.push(" WHERE id = ").push_bind(id);
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecutionRepository {
|
||||||
|
pub async fn find_top_level_by_enforcement<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
enforcement_id: Id,
|
||||||
|
) -> Result<Option<Execution>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
let sql = format!(
|
||||||
|
"SELECT {SELECT_COLUMNS} \
|
||||||
|
FROM execution \
|
||||||
|
WHERE enforcement = $1
|
||||||
|
AND parent IS NULL
|
||||||
|
AND (config IS NULL OR NOT (config ? 'retry_of')) \
|
||||||
|
ORDER BY created ASC \
|
||||||
|
LIMIT 1"
|
||||||
|
);
|
||||||
|
|
||||||
|
sqlx::query_as::<_, Execution>(&sql)
|
||||||
|
.bind(enforcement_id)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_top_level_for_enforcement_if_absent<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
input: CreateExecutionInput,
|
||||||
|
enforcement_id: Id,
|
||||||
|
) -> Result<EnforcementExecutionCreateOrGetResult>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + Copy + 'e,
|
||||||
|
{
|
||||||
|
let inserted = sqlx::query_as::<_, Execution>(&format!(
|
||||||
|
"INSERT INTO execution \
|
||||||
|
(action, action_ref, config, env_vars, parent, enforcement, executor, worker, status, result, workflow_task) \
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) \
|
||||||
|
ON CONFLICT (enforcement)
|
||||||
|
WHERE enforcement IS NOT NULL
|
||||||
|
AND parent IS NULL
|
||||||
|
AND (config IS NULL OR NOT (config ? 'retry_of'))
|
||||||
|
DO NOTHING \
|
||||||
|
RETURNING {SELECT_COLUMNS}"
|
||||||
|
))
|
||||||
|
.bind(input.action)
|
||||||
|
.bind(&input.action_ref)
|
||||||
|
.bind(&input.config)
|
||||||
|
.bind(&input.env_vars)
|
||||||
|
.bind(input.parent)
|
||||||
|
.bind(input.enforcement)
|
||||||
|
.bind(input.executor)
|
||||||
|
.bind(input.worker)
|
||||||
|
.bind(input.status)
|
||||||
|
.bind(&input.result)
|
||||||
|
.bind(sqlx::types::Json(&input.workflow_task))
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if let Some(execution) = inserted {
|
||||||
|
return Ok(EnforcementExecutionCreateOrGetResult {
|
||||||
|
execution,
|
||||||
|
created: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let execution = Self::find_top_level_by_enforcement(executor, enforcement_id)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| {
|
||||||
|
anyhow::anyhow!(
|
||||||
|
"top-level execution for enforcement {} disappeared after dedupe conflict",
|
||||||
|
enforcement_id
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(EnforcementExecutionCreateOrGetResult {
|
||||||
|
execution,
|
||||||
|
created: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn claim_workflow_task_dispatch<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
workflow_execution_id: Id,
|
||||||
|
task_name: &str,
|
||||||
|
task_index: Option<i32>,
|
||||||
|
) -> Result<bool>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
let inserted: Option<(i64,)> = sqlx::query_as(
|
||||||
|
"INSERT INTO workflow_task_dispatch (workflow_execution, task_name, task_index)
|
||||||
|
VALUES ($1, $2, $3)
|
||||||
|
ON CONFLICT (workflow_execution, task_name, COALESCE(task_index, -1)) DO NOTHING
|
||||||
|
RETURNING id",
|
||||||
|
)
|
||||||
|
.bind(workflow_execution_id)
|
||||||
|
.bind(task_name)
|
||||||
|
.bind(task_index)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(inserted.is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn assign_workflow_task_dispatch_execution<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
workflow_execution_id: Id,
|
||||||
|
task_name: &str,
|
||||||
|
task_index: Option<i32>,
|
||||||
|
execution_id: Id,
|
||||||
|
) -> Result<()>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
sqlx::query(
|
||||||
|
"UPDATE workflow_task_dispatch
|
||||||
|
SET execution_id = COALESCE(execution_id, $4)
|
||||||
|
WHERE workflow_execution = $1
|
||||||
|
AND task_name = $2
|
||||||
|
AND task_index IS NOT DISTINCT FROM $3",
|
||||||
|
)
|
||||||
|
.bind(workflow_execution_id)
|
||||||
|
.bind(task_name)
|
||||||
|
.bind(task_index)
|
||||||
|
.bind(execution_id)
|
||||||
|
.execute(executor)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn lock_workflow_task_dispatch<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
workflow_execution_id: Id,
|
||||||
|
task_name: &str,
|
||||||
|
task_index: Option<i32>,
|
||||||
|
) -> Result<Option<Option<Id>>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
let row: Option<(Option<i64>,)> = sqlx::query_as(
|
||||||
|
"SELECT execution_id
|
||||||
|
FROM workflow_task_dispatch
|
||||||
|
WHERE workflow_execution = $1
|
||||||
|
AND task_name = $2
|
||||||
|
AND task_index IS NOT DISTINCT FROM $3
|
||||||
|
FOR UPDATE",
|
||||||
|
)
|
||||||
|
.bind(workflow_execution_id)
|
||||||
|
.bind(task_name)
|
||||||
|
.bind(task_index)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
// Map the outer Option to distinguish three cases:
|
||||||
|
// - None → no row exists
|
||||||
|
// - Some(None) → row exists but execution_id is still NULL (mid-creation)
|
||||||
|
// - Some(Some(id)) → row exists with a completed execution_id
|
||||||
|
Ok(row.map(|(execution_id,)| execution_id))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create_workflow_task_if_absent_in_conn(
|
||||||
|
conn: &mut PgConnection,
|
||||||
|
input: CreateExecutionInput,
|
||||||
|
workflow_execution_id: Id,
|
||||||
|
task_name: &str,
|
||||||
|
task_index: Option<i32>,
|
||||||
|
) -> Result<WorkflowTaskExecutionCreateOrGetResult> {
|
||||||
|
let claimed = Self::claim_workflow_task_dispatch(
|
||||||
|
&mut *conn,
|
||||||
|
workflow_execution_id,
|
||||||
|
task_name,
|
||||||
|
task_index,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if claimed {
|
||||||
|
let execution = Self::create(&mut *conn, input).await?;
|
||||||
|
Self::assign_workflow_task_dispatch_execution(
|
||||||
|
&mut *conn,
|
||||||
|
workflow_execution_id,
|
||||||
|
task_name,
|
||||||
|
task_index,
|
||||||
|
execution.id,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
return Ok(WorkflowTaskExecutionCreateOrGetResult {
|
||||||
|
execution,
|
||||||
|
created: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let dispatch_state = Self::lock_workflow_task_dispatch(
|
||||||
|
&mut *conn,
|
||||||
|
workflow_execution_id,
|
||||||
|
task_name,
|
||||||
|
task_index,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
match dispatch_state {
|
||||||
|
Some(Some(existing_execution_id)) => {
|
||||||
|
// Row exists with execution_id — return the existing execution.
|
||||||
|
let execution = Self::find_by_id(&mut *conn, existing_execution_id)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| {
|
||||||
|
anyhow::anyhow!(
|
||||||
|
"workflow child execution {} missing for workflow_execution {} task '{}' index {:?}",
|
||||||
|
existing_execution_id,
|
||||||
|
workflow_execution_id,
|
||||||
|
task_name,
|
||||||
|
task_index
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(WorkflowTaskExecutionCreateOrGetResult {
|
||||||
|
execution,
|
||||||
|
created: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(None) => {
|
||||||
|
// Row exists but execution_id is still NULL: another transaction is
|
||||||
|
// mid-creation (between claim and assign). Retry until it's filled in.
|
||||||
|
// If the original creator's transaction rolled back, the row also
|
||||||
|
// disappears — handled by the `None` branch inside the loop.
|
||||||
|
'wait: {
|
||||||
|
for _ in 0..20_u32 {
|
||||||
|
sleep(Duration::from_millis(50)).await;
|
||||||
|
match Self::lock_workflow_task_dispatch(
|
||||||
|
&mut *conn,
|
||||||
|
workflow_execution_id,
|
||||||
|
task_name,
|
||||||
|
task_index,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
{
|
||||||
|
Some(Some(execution_id)) => {
|
||||||
|
let execution =
|
||||||
|
Self::find_by_id(&mut *conn, execution_id).await?.ok_or_else(
|
||||||
|
|| {
|
||||||
|
anyhow::anyhow!(
|
||||||
|
"workflow child execution {} missing for workflow_execution {} task '{}' index {:?}",
|
||||||
|
execution_id,
|
||||||
|
workflow_execution_id,
|
||||||
|
task_name,
|
||||||
|
task_index
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
return Ok(WorkflowTaskExecutionCreateOrGetResult {
|
||||||
|
execution,
|
||||||
|
created: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Some(None) => {} // still NULL, keep waiting
|
||||||
|
None => break 'wait, // row rolled back; fall through to re-claim
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Exhausted all retries without the execution_id being set.
|
||||||
|
return Err(anyhow::anyhow!(
|
||||||
|
"Timed out waiting for workflow task dispatch execution_id to be set \
|
||||||
|
for workflow_execution {} task '{}' index {:?}",
|
||||||
|
workflow_execution_id,
|
||||||
|
task_name,
|
||||||
|
task_index
|
||||||
|
)
|
||||||
|
.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Row disappeared (original creator rolled back) — re-claim and create.
|
||||||
|
let re_claimed = Self::claim_workflow_task_dispatch(
|
||||||
|
&mut *conn,
|
||||||
|
workflow_execution_id,
|
||||||
|
task_name,
|
||||||
|
task_index,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
if !re_claimed {
|
||||||
|
return Err(anyhow::anyhow!(
|
||||||
|
"Workflow task dispatch for workflow_execution {} task '{}' index {:?} \
|
||||||
|
was reclaimed by another executor after rollback",
|
||||||
|
workflow_execution_id,
|
||||||
|
task_name,
|
||||||
|
task_index
|
||||||
|
)
|
||||||
|
.into());
|
||||||
|
}
|
||||||
|
let execution = Self::create(&mut *conn, input).await?;
|
||||||
|
Self::assign_workflow_task_dispatch_execution(
|
||||||
|
&mut *conn,
|
||||||
|
workflow_execution_id,
|
||||||
|
task_name,
|
||||||
|
task_index,
|
||||||
|
execution.id,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
Ok(WorkflowTaskExecutionCreateOrGetResult {
|
||||||
|
execution,
|
||||||
|
created: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
None => {
|
||||||
|
// No row at all — the original INSERT was rolled back before we arrived.
|
||||||
|
// Attempt to re-claim and create as if this were a fresh dispatch.
|
||||||
|
let re_claimed = Self::claim_workflow_task_dispatch(
|
||||||
|
&mut *conn,
|
||||||
|
workflow_execution_id,
|
||||||
|
task_name,
|
||||||
|
task_index,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
if !re_claimed {
|
||||||
|
return Err(anyhow::anyhow!(
|
||||||
|
"Workflow task dispatch for workflow_execution {} task '{}' index {:?} \
|
||||||
|
was claimed by another executor",
|
||||||
|
workflow_execution_id,
|
||||||
|
task_name,
|
||||||
|
task_index
|
||||||
|
)
|
||||||
|
.into());
|
||||||
|
}
|
||||||
|
let execution = Self::create(&mut *conn, input).await?;
|
||||||
|
Self::assign_workflow_task_dispatch_execution(
|
||||||
|
&mut *conn,
|
||||||
|
workflow_execution_id,
|
||||||
|
task_name,
|
||||||
|
task_index,
|
||||||
|
execution.id,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
Ok(WorkflowTaskExecutionCreateOrGetResult {
|
||||||
|
execution,
|
||||||
|
created: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_workflow_task_if_absent(
|
||||||
|
pool: &PgPool,
|
||||||
|
input: CreateExecutionInput,
|
||||||
|
workflow_execution_id: Id,
|
||||||
|
task_name: &str,
|
||||||
|
task_index: Option<i32>,
|
||||||
|
) -> Result<WorkflowTaskExecutionCreateOrGetResult> {
|
||||||
|
let mut conn = pool.acquire().await?;
|
||||||
|
sqlx::query("BEGIN").execute(&mut *conn).await?;
|
||||||
|
|
||||||
|
let result = Self::create_workflow_task_if_absent_in_conn(
|
||||||
|
&mut conn,
|
||||||
|
input,
|
||||||
|
workflow_execution_id,
|
||||||
|
task_name,
|
||||||
|
task_index,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(result) => {
|
||||||
|
sqlx::query("COMMIT").execute(&mut *conn).await?;
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
sqlx::query("ROLLBACK").execute(&mut *conn).await?;
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_workflow_task_if_absent_with_conn(
|
||||||
|
conn: &mut PgConnection,
|
||||||
|
input: CreateExecutionInput,
|
||||||
|
workflow_execution_id: Id,
|
||||||
|
task_name: &str,
|
||||||
|
task_index: Option<i32>,
|
||||||
|
) -> Result<WorkflowTaskExecutionCreateOrGetResult> {
|
||||||
|
Self::create_workflow_task_if_absent_in_conn(
|
||||||
|
conn,
|
||||||
|
input,
|
||||||
|
workflow_execution_id,
|
||||||
|
task_name,
|
||||||
|
task_index,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn claim_for_scheduling<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
id: Id,
|
||||||
|
claiming_executor: Option<Id>,
|
||||||
|
) -> Result<Option<Execution>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
let sql = format!(
|
||||||
|
"UPDATE execution \
|
||||||
|
SET status = $2, executor = COALESCE($3, executor), updated = NOW() \
|
||||||
|
WHERE id = $1 AND status = $4 \
|
||||||
|
RETURNING {SELECT_COLUMNS}"
|
||||||
|
);
|
||||||
|
|
||||||
|
sqlx::query_as::<_, Execution>(&sql)
|
||||||
|
.bind(id)
|
||||||
|
.bind(ExecutionStatus::Scheduling)
|
||||||
|
.bind(claiming_executor)
|
||||||
|
.bind(ExecutionStatus::Requested)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn reclaim_stale_scheduling<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
id: Id,
|
||||||
|
claiming_executor: Option<Id>,
|
||||||
|
stale_before: DateTime<Utc>,
|
||||||
|
) -> Result<Option<Execution>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
let sql = format!(
|
||||||
|
"UPDATE execution \
|
||||||
|
SET executor = COALESCE($2, executor), updated = NOW() \
|
||||||
|
WHERE id = $1 AND status = $3 AND updated <= $4 \
|
||||||
|
RETURNING {SELECT_COLUMNS}"
|
||||||
|
);
|
||||||
|
|
||||||
|
sqlx::query_as::<_, Execution>(&sql)
|
||||||
|
.bind(id)
|
||||||
|
.bind(claiming_executor)
|
||||||
|
.bind(ExecutionStatus::Scheduling)
|
||||||
|
.bind(stale_before)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_if_status<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
id: Id,
|
||||||
|
expected_status: ExecutionStatus,
|
||||||
|
input: UpdateExecutionInput,
|
||||||
|
) -> Result<Option<Execution>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
if input.status.is_none()
|
||||||
|
&& input.result.is_none()
|
||||||
|
&& input.executor.is_none()
|
||||||
|
&& input.worker.is_none()
|
||||||
|
&& input.started_at.is_none()
|
||||||
|
&& input.workflow_task.is_none()
|
||||||
|
{
|
||||||
|
return Self::find_by_id(executor, id).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
Self::update_with_locator_optional(executor, input, |query| {
|
||||||
|
query.push(" WHERE id = ").push_bind(id);
|
||||||
|
query.push(" AND status = ").push_bind(expected_status);
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_if_status_and_updated_before<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
id: Id,
|
||||||
|
expected_status: ExecutionStatus,
|
||||||
|
stale_before: DateTime<Utc>,
|
||||||
|
input: UpdateExecutionInput,
|
||||||
|
) -> Result<Option<Execution>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
if input.status.is_none()
|
||||||
|
&& input.result.is_none()
|
||||||
|
&& input.executor.is_none()
|
||||||
|
&& input.worker.is_none()
|
||||||
|
&& input.started_at.is_none()
|
||||||
|
&& input.workflow_task.is_none()
|
||||||
|
{
|
||||||
|
return Self::find_by_id(executor, id).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
Self::update_with_locator_optional(executor, input, |query| {
|
||||||
|
query.push(" WHERE id = ").push_bind(id);
|
||||||
|
query.push(" AND status = ").push_bind(expected_status);
|
||||||
|
query.push(" AND updated < ").push_bind(stale_before);
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_if_status_and_updated_at<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
id: Id,
|
||||||
|
expected_status: ExecutionStatus,
|
||||||
|
expected_updated: DateTime<Utc>,
|
||||||
|
input: UpdateExecutionInput,
|
||||||
|
) -> Result<Option<Execution>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
if input.status.is_none()
|
||||||
|
&& input.result.is_none()
|
||||||
|
&& input.executor.is_none()
|
||||||
|
&& input.worker.is_none()
|
||||||
|
&& input.started_at.is_none()
|
||||||
|
&& input.workflow_task.is_none()
|
||||||
|
{
|
||||||
|
return Self::find_by_id(executor, id).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
Self::update_with_locator_optional(executor, input, |query| {
|
||||||
|
query.push(" WHERE id = ").push_bind(id);
|
||||||
|
query.push(" AND status = ").push_bind(expected_status);
|
||||||
|
query.push(" AND updated = ").push_bind(expected_updated);
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn revert_scheduled_to_requested<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
id: Id,
|
||||||
|
) -> Result<Option<Execution>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
let sql = format!(
|
||||||
|
"UPDATE execution \
|
||||||
|
SET status = $2, worker = NULL, executor = NULL, updated = NOW() \
|
||||||
|
WHERE id = $1 AND status = $3 \
|
||||||
|
RETURNING {SELECT_COLUMNS}"
|
||||||
|
);
|
||||||
|
|
||||||
|
sqlx::query_as::<_, Execution>(&sql)
|
||||||
|
.bind(id)
|
||||||
|
.bind(ExecutionStatus::Requested)
|
||||||
|
.bind(ExecutionStatus::Scheduled)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_with_locator<'e, E, F>(
|
||||||
|
executor: E,
|
||||||
|
input: UpdateExecutionInput,
|
||||||
|
where_clause: F,
|
||||||
|
) -> Result<Execution>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
F: FnOnce(&mut QueryBuilder<'_, Postgres>),
|
||||||
|
{
|
||||||
let mut query = QueryBuilder::new("UPDATE execution SET ");
|
let mut query = QueryBuilder::new("UPDATE execution SET ");
|
||||||
let mut has_updates = false;
|
let mut has_updates = false;
|
||||||
|
|
||||||
@@ -234,15 +817,10 @@ impl Update for ExecutionRepository {
|
|||||||
query
|
query
|
||||||
.push("workflow_task = ")
|
.push("workflow_task = ")
|
||||||
.push_bind(sqlx::types::Json(workflow_task));
|
.push_bind(sqlx::types::Json(workflow_task));
|
||||||
has_updates = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !has_updates {
|
query.push(", updated = NOW()");
|
||||||
// No updates requested, fetch and return existing entity
|
where_clause(&mut query);
|
||||||
return Self::get_by_id(executor, id).await;
|
|
||||||
}
|
|
||||||
|
|
||||||
query.push(", updated = NOW() WHERE id = ").push_bind(id);
|
|
||||||
query.push(" RETURNING ");
|
query.push(" RETURNING ");
|
||||||
query.push(SELECT_COLUMNS);
|
query.push(SELECT_COLUMNS);
|
||||||
|
|
||||||
@@ -252,6 +830,96 @@ impl Update for ExecutionRepository {
|
|||||||
.await
|
.await
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn update_with_locator_optional<'e, E, F>(
|
||||||
|
executor: E,
|
||||||
|
input: UpdateExecutionInput,
|
||||||
|
where_clause: F,
|
||||||
|
) -> Result<Option<Execution>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
F: FnOnce(&mut QueryBuilder<'_, Postgres>),
|
||||||
|
{
|
||||||
|
let mut query = QueryBuilder::new("UPDATE execution SET ");
|
||||||
|
let mut has_updates = false;
|
||||||
|
|
||||||
|
if let Some(status) = input.status {
|
||||||
|
query.push("status = ").push_bind(status);
|
||||||
|
has_updates = true;
|
||||||
|
}
|
||||||
|
if let Some(result) = &input.result {
|
||||||
|
if has_updates {
|
||||||
|
query.push(", ");
|
||||||
|
}
|
||||||
|
query.push("result = ").push_bind(result);
|
||||||
|
has_updates = true;
|
||||||
|
}
|
||||||
|
if let Some(executor_id) = input.executor {
|
||||||
|
if has_updates {
|
||||||
|
query.push(", ");
|
||||||
|
}
|
||||||
|
query.push("executor = ").push_bind(executor_id);
|
||||||
|
has_updates = true;
|
||||||
|
}
|
||||||
|
if let Some(worker_id) = input.worker {
|
||||||
|
if has_updates {
|
||||||
|
query.push(", ");
|
||||||
|
}
|
||||||
|
query.push("worker = ").push_bind(worker_id);
|
||||||
|
has_updates = true;
|
||||||
|
}
|
||||||
|
if let Some(started_at) = input.started_at {
|
||||||
|
if has_updates {
|
||||||
|
query.push(", ");
|
||||||
|
}
|
||||||
|
query.push("started_at = ").push_bind(started_at);
|
||||||
|
has_updates = true;
|
||||||
|
}
|
||||||
|
if let Some(workflow_task) = &input.workflow_task {
|
||||||
|
if has_updates {
|
||||||
|
query.push(", ");
|
||||||
|
}
|
||||||
|
query
|
||||||
|
.push("workflow_task = ")
|
||||||
|
.push_bind(sqlx::types::Json(workflow_task));
|
||||||
|
}
|
||||||
|
|
||||||
|
query.push(", updated = NOW()");
|
||||||
|
where_clause(&mut query);
|
||||||
|
query.push(" RETURNING ");
|
||||||
|
query.push(SELECT_COLUMNS);
|
||||||
|
|
||||||
|
query
|
||||||
|
.build_query_as::<Execution>()
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update an execution using the loaded row's primary key.
|
||||||
|
pub async fn update_loaded<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
execution: &Execution,
|
||||||
|
input: UpdateExecutionInput,
|
||||||
|
) -> Result<Execution>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
if input.status.is_none()
|
||||||
|
&& input.result.is_none()
|
||||||
|
&& input.executor.is_none()
|
||||||
|
&& input.worker.is_none()
|
||||||
|
&& input.started_at.is_none()
|
||||||
|
&& input.workflow_task.is_none()
|
||||||
|
{
|
||||||
|
return Ok(execution.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
Self::update_with_locator(executor, input, |query| {
|
||||||
|
query.push(" WHERE id = ").push_bind(execution.id);
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
@@ -303,6 +971,34 @@ impl ExecutionRepository {
|
|||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn find_by_workflow_task<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
workflow_execution_id: Id,
|
||||||
|
task_name: &str,
|
||||||
|
task_index: Option<i32>,
|
||||||
|
) -> Result<Option<Execution>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
let sql = format!(
|
||||||
|
"SELECT {SELECT_COLUMNS} \
|
||||||
|
FROM execution \
|
||||||
|
WHERE workflow_task->>'workflow_execution' = $1::text \
|
||||||
|
AND workflow_task->>'task_name' = $2 \
|
||||||
|
AND (workflow_task->>'task_index')::int IS NOT DISTINCT FROM $3 \
|
||||||
|
ORDER BY created ASC \
|
||||||
|
LIMIT 1"
|
||||||
|
);
|
||||||
|
|
||||||
|
sqlx::query_as::<_, Execution>(&sql)
|
||||||
|
.bind(workflow_execution_id.to_string())
|
||||||
|
.bind(task_name)
|
||||||
|
.bind(task_index)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
/// Find all child executions for a given parent execution ID.
|
/// Find all child executions for a given parent execution ID.
|
||||||
///
|
///
|
||||||
/// Returns child executions ordered by creation time (ascending),
|
/// Returns child executions ordered by creation time (ascending),
|
||||||
|
|||||||
909
crates/common/src/repositories/execution_admission.rs
Normal file
909
crates/common/src/repositories/execution_admission.rs
Normal file
@@ -0,0 +1,909 @@
|
|||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use sqlx::{PgPool, Postgres, Row, Transaction};
|
||||||
|
|
||||||
|
use crate::error::Result;
|
||||||
|
use crate::models::Id;
|
||||||
|
use crate::repositories::queue_stats::{QueueStatsRepository, UpsertQueueStatsInput};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct AdmissionSlotAcquireOutcome {
|
||||||
|
pub acquired: bool,
|
||||||
|
pub current_count: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum AdmissionEnqueueOutcome {
|
||||||
|
Acquired,
|
||||||
|
Enqueued,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct AdmissionSlotReleaseOutcome {
|
||||||
|
pub action_id: Id,
|
||||||
|
pub group_key: Option<String>,
|
||||||
|
pub next_execution_id: Option<Id>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct AdmissionQueuedRemovalOutcome {
|
||||||
|
pub action_id: Id,
|
||||||
|
pub group_key: Option<String>,
|
||||||
|
pub next_execution_id: Option<Id>,
|
||||||
|
pub execution_id: Id,
|
||||||
|
pub queue_order: i64,
|
||||||
|
pub enqueued_at: DateTime<Utc>,
|
||||||
|
pub removed_index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct AdmissionQueueStats {
|
||||||
|
pub action_id: Id,
|
||||||
|
pub queue_length: usize,
|
||||||
|
pub active_count: u32,
|
||||||
|
pub max_concurrent: u32,
|
||||||
|
pub oldest_enqueued_at: Option<DateTime<Utc>>,
|
||||||
|
pub total_enqueued: u64,
|
||||||
|
pub total_completed: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct AdmissionState {
|
||||||
|
id: Id,
|
||||||
|
action_id: Id,
|
||||||
|
group_key: Option<String>,
|
||||||
|
max_concurrent: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct ExecutionEntry {
|
||||||
|
state_id: Id,
|
||||||
|
action_id: Id,
|
||||||
|
group_key: Option<String>,
|
||||||
|
status: String,
|
||||||
|
queue_order: i64,
|
||||||
|
enqueued_at: DateTime<Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ExecutionAdmissionRepository;
|
||||||
|
|
||||||
|
impl ExecutionAdmissionRepository {
|
||||||
|
pub async fn enqueue(
|
||||||
|
pool: &PgPool,
|
||||||
|
max_queue_length: usize,
|
||||||
|
action_id: Id,
|
||||||
|
execution_id: Id,
|
||||||
|
max_concurrent: u32,
|
||||||
|
group_key: Option<String>,
|
||||||
|
) -> Result<AdmissionEnqueueOutcome> {
|
||||||
|
let mut tx = pool.begin().await?;
|
||||||
|
let state = Self::lock_state(&mut tx, action_id, group_key, max_concurrent).await?;
|
||||||
|
let outcome =
|
||||||
|
Self::enqueue_in_state(&mut tx, &state, max_queue_length, execution_id, true).await?;
|
||||||
|
Self::refresh_queue_stats(&mut tx, action_id).await?;
|
||||||
|
tx.commit().await?;
|
||||||
|
Ok(outcome)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn wait_status(pool: &PgPool, execution_id: Id) -> Result<Option<bool>> {
|
||||||
|
let row = sqlx::query_scalar::<Postgres, bool>(
|
||||||
|
r#"
|
||||||
|
SELECT status = 'active'
|
||||||
|
FROM execution_admission_entry
|
||||||
|
WHERE execution_id = $1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(execution_id)
|
||||||
|
.fetch_optional(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(row)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn try_acquire(
|
||||||
|
pool: &PgPool,
|
||||||
|
action_id: Id,
|
||||||
|
execution_id: Id,
|
||||||
|
max_concurrent: u32,
|
||||||
|
group_key: Option<String>,
|
||||||
|
) -> Result<AdmissionSlotAcquireOutcome> {
|
||||||
|
let mut tx = pool.begin().await?;
|
||||||
|
let state = Self::lock_state(&mut tx, action_id, group_key, max_concurrent).await?;
|
||||||
|
let active_count = Self::active_count(&mut tx, state.id).await? as u32;
|
||||||
|
|
||||||
|
let outcome = match Self::find_execution_entry(&mut tx, execution_id).await? {
|
||||||
|
Some(entry) if entry.status == "active" => AdmissionSlotAcquireOutcome {
|
||||||
|
acquired: true,
|
||||||
|
current_count: active_count,
|
||||||
|
},
|
||||||
|
Some(entry) if entry.status == "queued" && entry.state_id == state.id => {
|
||||||
|
let promoted =
|
||||||
|
Self::maybe_promote_existing_queued(&mut tx, &state, execution_id).await?;
|
||||||
|
AdmissionSlotAcquireOutcome {
|
||||||
|
acquired: promoted,
|
||||||
|
current_count: active_count,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(_) => AdmissionSlotAcquireOutcome {
|
||||||
|
acquired: false,
|
||||||
|
current_count: active_count,
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
if active_count < max_concurrent
|
||||||
|
&& Self::queued_count(&mut tx, state.id).await? == 0
|
||||||
|
{
|
||||||
|
let queue_order = Self::allocate_queue_order(&mut tx, state.id).await?;
|
||||||
|
Self::insert_entry(
|
||||||
|
&mut tx,
|
||||||
|
state.id,
|
||||||
|
execution_id,
|
||||||
|
"active",
|
||||||
|
queue_order,
|
||||||
|
Utc::now(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
Self::increment_total_enqueued(&mut tx, state.id).await?;
|
||||||
|
Self::refresh_queue_stats(&mut tx, action_id).await?;
|
||||||
|
AdmissionSlotAcquireOutcome {
|
||||||
|
acquired: true,
|
||||||
|
current_count: active_count,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
AdmissionSlotAcquireOutcome {
|
||||||
|
acquired: false,
|
||||||
|
current_count: active_count,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
tx.commit().await?;
|
||||||
|
Ok(outcome)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn release_active_slot(
|
||||||
|
pool: &PgPool,
|
||||||
|
execution_id: Id,
|
||||||
|
) -> Result<Option<AdmissionSlotReleaseOutcome>> {
|
||||||
|
let mut tx = pool.begin().await?;
|
||||||
|
let Some(entry) = Self::find_execution_entry_for_update(&mut tx, execution_id).await?
|
||||||
|
else {
|
||||||
|
tx.commit().await?;
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
|
||||||
|
if entry.status != "active" {
|
||||||
|
tx.commit().await?;
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let state = Self::lock_existing_state(&mut tx, entry.action_id, entry.group_key.clone())
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| {
|
||||||
|
crate::Error::internal("missing execution_admission_state for active execution")
|
||||||
|
})?;
|
||||||
|
|
||||||
|
sqlx::query("DELETE FROM execution_admission_entry WHERE execution_id = $1")
|
||||||
|
.bind(execution_id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Self::increment_total_completed(&mut tx, state.id).await?;
|
||||||
|
|
||||||
|
let next_execution_id = Self::promote_next_queued(&mut tx, &state).await?;
|
||||||
|
Self::refresh_queue_stats(&mut tx, state.action_id).await?;
|
||||||
|
tx.commit().await?;
|
||||||
|
|
||||||
|
Ok(Some(AdmissionSlotReleaseOutcome {
|
||||||
|
action_id: state.action_id,
|
||||||
|
group_key: state.group_key,
|
||||||
|
next_execution_id,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn restore_active_slot(
|
||||||
|
pool: &PgPool,
|
||||||
|
execution_id: Id,
|
||||||
|
outcome: &AdmissionSlotReleaseOutcome,
|
||||||
|
) -> Result<()> {
|
||||||
|
let mut tx = pool.begin().await?;
|
||||||
|
let state =
|
||||||
|
Self::lock_existing_state(&mut tx, outcome.action_id, outcome.group_key.clone())
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| {
|
||||||
|
crate::Error::internal("missing execution_admission_state on restore")
|
||||||
|
})?;
|
||||||
|
|
||||||
|
if let Some(next_execution_id) = outcome.next_execution_id {
|
||||||
|
sqlx::query(
|
||||||
|
r#"
|
||||||
|
UPDATE execution_admission_entry
|
||||||
|
SET status = 'queued', activated_at = NULL
|
||||||
|
WHERE execution_id = $1
|
||||||
|
AND state_id = $2
|
||||||
|
AND status = 'active'
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(next_execution_id)
|
||||||
|
.bind(state.id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlx::query(
|
||||||
|
r#"
|
||||||
|
INSERT INTO execution_admission_entry (
|
||||||
|
state_id, execution_id, status, queue_order, enqueued_at, activated_at
|
||||||
|
) VALUES ($1, $2, 'active', $3, NOW(), NOW())
|
||||||
|
ON CONFLICT (execution_id) DO UPDATE
|
||||||
|
SET state_id = EXCLUDED.state_id,
|
||||||
|
status = 'active',
|
||||||
|
activated_at = EXCLUDED.activated_at
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(state.id)
|
||||||
|
.bind(execution_id)
|
||||||
|
.bind(Self::allocate_queue_order(&mut tx, state.id).await?)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
sqlx::query(
|
||||||
|
r#"
|
||||||
|
UPDATE execution_admission_state
|
||||||
|
SET total_completed = GREATEST(total_completed - 1, 0)
|
||||||
|
WHERE id = $1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(state.id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Self::refresh_queue_stats(&mut tx, state.action_id).await?;
|
||||||
|
tx.commit().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn remove_queued_execution(
|
||||||
|
pool: &PgPool,
|
||||||
|
execution_id: Id,
|
||||||
|
) -> Result<Option<AdmissionQueuedRemovalOutcome>> {
|
||||||
|
let mut tx = pool.begin().await?;
|
||||||
|
let Some(entry) = Self::find_execution_entry_for_update(&mut tx, execution_id).await?
|
||||||
|
else {
|
||||||
|
tx.commit().await?;
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
|
||||||
|
if entry.status != "queued" {
|
||||||
|
tx.commit().await?;
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let state = Self::lock_existing_state(&mut tx, entry.action_id, entry.group_key.clone())
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| {
|
||||||
|
crate::Error::internal("missing execution_admission_state for queued execution")
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let removed_index = sqlx::query_scalar::<Postgres, i64>(
|
||||||
|
r#"
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM execution_admission_entry
|
||||||
|
WHERE state_id = $1
|
||||||
|
AND status = 'queued'
|
||||||
|
AND (enqueued_at, id) < (
|
||||||
|
SELECT enqueued_at, id
|
||||||
|
FROM execution_admission_entry
|
||||||
|
WHERE execution_id = $2
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(state.id)
|
||||||
|
.bind(execution_id)
|
||||||
|
.fetch_one(&mut *tx)
|
||||||
|
.await? as usize;
|
||||||
|
|
||||||
|
sqlx::query("DELETE FROM execution_admission_entry WHERE execution_id = $1")
|
||||||
|
.bind(execution_id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let next_execution_id =
|
||||||
|
if Self::active_count(&mut tx, state.id).await? < state.max_concurrent as i64 {
|
||||||
|
Self::promote_next_queued(&mut tx, &state).await?
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
Self::refresh_queue_stats(&mut tx, state.action_id).await?;
|
||||||
|
tx.commit().await?;
|
||||||
|
|
||||||
|
Ok(Some(AdmissionQueuedRemovalOutcome {
|
||||||
|
action_id: state.action_id,
|
||||||
|
group_key: state.group_key,
|
||||||
|
next_execution_id,
|
||||||
|
execution_id,
|
||||||
|
queue_order: entry.queue_order,
|
||||||
|
enqueued_at: entry.enqueued_at,
|
||||||
|
removed_index,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn restore_queued_execution(
|
||||||
|
pool: &PgPool,
|
||||||
|
outcome: &AdmissionQueuedRemovalOutcome,
|
||||||
|
) -> Result<()> {
|
||||||
|
let mut tx = pool.begin().await?;
|
||||||
|
let state =
|
||||||
|
Self::lock_existing_state(&mut tx, outcome.action_id, outcome.group_key.clone())
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| {
|
||||||
|
crate::Error::internal("missing execution_admission_state on queued restore")
|
||||||
|
})?;
|
||||||
|
|
||||||
|
if let Some(next_execution_id) = outcome.next_execution_id {
|
||||||
|
sqlx::query(
|
||||||
|
r#"
|
||||||
|
UPDATE execution_admission_entry
|
||||||
|
SET status = 'queued', activated_at = NULL
|
||||||
|
WHERE execution_id = $1
|
||||||
|
AND state_id = $2
|
||||||
|
AND status = 'active'
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(next_execution_id)
|
||||||
|
.bind(state.id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlx::query(
|
||||||
|
r#"
|
||||||
|
INSERT INTO execution_admission_entry (
|
||||||
|
state_id, execution_id, status, queue_order, enqueued_at, activated_at
|
||||||
|
) VALUES ($1, $2, 'queued', $3, $4, NULL)
|
||||||
|
ON CONFLICT (execution_id) DO NOTHING
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(state.id)
|
||||||
|
.bind(outcome.execution_id)
|
||||||
|
.bind(outcome.queue_order)
|
||||||
|
.bind(outcome.enqueued_at)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Self::refresh_queue_stats(&mut tx, state.action_id).await?;
|
||||||
|
tx.commit().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_queue_stats(
|
||||||
|
pool: &PgPool,
|
||||||
|
action_id: Id,
|
||||||
|
) -> Result<Option<AdmissionQueueStats>> {
|
||||||
|
let row = sqlx::query(
|
||||||
|
r#"
|
||||||
|
WITH state_rows AS (
|
||||||
|
SELECT
|
||||||
|
COUNT(*) AS state_count,
|
||||||
|
COALESCE(SUM(max_concurrent), 0) AS max_concurrent,
|
||||||
|
COALESCE(SUM(total_enqueued), 0) AS total_enqueued,
|
||||||
|
COALESCE(SUM(total_completed), 0) AS total_completed
|
||||||
|
FROM execution_admission_state
|
||||||
|
WHERE action_id = $1
|
||||||
|
),
|
||||||
|
entry_rows AS (
|
||||||
|
SELECT
|
||||||
|
COUNT(*) FILTER (WHERE e.status = 'queued') AS queue_length,
|
||||||
|
COUNT(*) FILTER (WHERE e.status = 'active') AS active_count,
|
||||||
|
MIN(e.enqueued_at) FILTER (WHERE e.status = 'queued') AS oldest_enqueued_at
|
||||||
|
FROM execution_admission_state s
|
||||||
|
LEFT JOIN execution_admission_entry e ON e.state_id = s.id
|
||||||
|
WHERE s.action_id = $1
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
sr.state_count,
|
||||||
|
er.queue_length,
|
||||||
|
er.active_count,
|
||||||
|
sr.max_concurrent,
|
||||||
|
er.oldest_enqueued_at,
|
||||||
|
sr.total_enqueued,
|
||||||
|
sr.total_completed
|
||||||
|
FROM state_rows sr
|
||||||
|
CROSS JOIN entry_rows er
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(action_id)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let state_count: i64 = row.try_get("state_count")?;
|
||||||
|
if state_count == 0 {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Some(AdmissionQueueStats {
|
||||||
|
action_id,
|
||||||
|
queue_length: row.try_get::<i64, _>("queue_length")? as usize,
|
||||||
|
active_count: row.try_get::<i64, _>("active_count")? as u32,
|
||||||
|
max_concurrent: row.try_get::<i64, _>("max_concurrent")? as u32,
|
||||||
|
oldest_enqueued_at: row.try_get("oldest_enqueued_at")?,
|
||||||
|
total_enqueued: row.try_get::<i64, _>("total_enqueued")? as u64,
|
||||||
|
total_completed: row.try_get::<i64, _>("total_completed")? as u64,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn enqueue_in_state(
|
||||||
|
tx: &mut Transaction<'_, Postgres>,
|
||||||
|
state: &AdmissionState,
|
||||||
|
max_queue_length: usize,
|
||||||
|
execution_id: Id,
|
||||||
|
allow_queue: bool,
|
||||||
|
) -> Result<AdmissionEnqueueOutcome> {
|
||||||
|
if let Some(entry) = Self::find_execution_entry(tx, execution_id).await? {
|
||||||
|
if entry.status == "active" {
|
||||||
|
return Ok(AdmissionEnqueueOutcome::Acquired);
|
||||||
|
}
|
||||||
|
|
||||||
|
if entry.status == "queued" && entry.state_id == state.id {
|
||||||
|
if Self::maybe_promote_existing_queued(tx, state, execution_id).await? {
|
||||||
|
return Ok(AdmissionEnqueueOutcome::Acquired);
|
||||||
|
}
|
||||||
|
return Ok(AdmissionEnqueueOutcome::Enqueued);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(AdmissionEnqueueOutcome::Enqueued);
|
||||||
|
}
|
||||||
|
|
||||||
|
let active_count = Self::active_count(tx, state.id).await?;
|
||||||
|
let queued_count = Self::queued_count(tx, state.id).await?;
|
||||||
|
|
||||||
|
if active_count < state.max_concurrent as i64 && queued_count == 0 {
|
||||||
|
let queue_order = Self::allocate_queue_order(tx, state.id).await?;
|
||||||
|
Self::insert_entry(
|
||||||
|
tx,
|
||||||
|
state.id,
|
||||||
|
execution_id,
|
||||||
|
"active",
|
||||||
|
queue_order,
|
||||||
|
Utc::now(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
Self::increment_total_enqueued(tx, state.id).await?;
|
||||||
|
return Ok(AdmissionEnqueueOutcome::Acquired);
|
||||||
|
}
|
||||||
|
|
||||||
|
if !allow_queue {
|
||||||
|
return Ok(AdmissionEnqueueOutcome::Enqueued);
|
||||||
|
}
|
||||||
|
|
||||||
|
if queued_count >= max_queue_length as i64 {
|
||||||
|
return Err(anyhow::anyhow!(
|
||||||
|
"Queue full for action {}: maximum {} entries",
|
||||||
|
state.action_id,
|
||||||
|
max_queue_length
|
||||||
|
)
|
||||||
|
.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
let queue_order = Self::allocate_queue_order(tx, state.id).await?;
|
||||||
|
Self::insert_entry(
|
||||||
|
tx,
|
||||||
|
state.id,
|
||||||
|
execution_id,
|
||||||
|
"queued",
|
||||||
|
queue_order,
|
||||||
|
Utc::now(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
Self::increment_total_enqueued(tx, state.id).await?;
|
||||||
|
Ok(AdmissionEnqueueOutcome::Enqueued)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn maybe_promote_existing_queued(
|
||||||
|
tx: &mut Transaction<'_, Postgres>,
|
||||||
|
state: &AdmissionState,
|
||||||
|
execution_id: Id,
|
||||||
|
) -> Result<bool> {
|
||||||
|
let active_count = Self::active_count(tx, state.id).await?;
|
||||||
|
if active_count >= state.max_concurrent as i64 {
|
||||||
|
return Ok(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
let front_execution_id = sqlx::query_scalar::<Postgres, Id>(
|
||||||
|
r#"
|
||||||
|
SELECT execution_id
|
||||||
|
FROM execution_admission_entry
|
||||||
|
WHERE state_id = $1
|
||||||
|
AND status = 'queued'
|
||||||
|
ORDER BY queue_order ASC
|
||||||
|
LIMIT 1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(state.id)
|
||||||
|
.fetch_optional(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if front_execution_id != Some(execution_id) {
|
||||||
|
return Ok(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlx::query(
|
||||||
|
r#"
|
||||||
|
UPDATE execution_admission_entry
|
||||||
|
SET status = 'active',
|
||||||
|
activated_at = NOW()
|
||||||
|
WHERE execution_id = $1
|
||||||
|
AND state_id = $2
|
||||||
|
AND status = 'queued'
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(execution_id)
|
||||||
|
.bind(state.id)
|
||||||
|
.execute(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn promote_next_queued(
|
||||||
|
tx: &mut Transaction<'_, Postgres>,
|
||||||
|
state: &AdmissionState,
|
||||||
|
) -> Result<Option<Id>> {
|
||||||
|
let next_execution_id = sqlx::query_scalar::<Postgres, Id>(
|
||||||
|
r#"
|
||||||
|
SELECT execution_id
|
||||||
|
FROM execution_admission_entry
|
||||||
|
WHERE state_id = $1
|
||||||
|
AND status = 'queued'
|
||||||
|
ORDER BY queue_order ASC
|
||||||
|
LIMIT 1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(state.id)
|
||||||
|
.fetch_optional(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if let Some(next_execution_id) = next_execution_id {
|
||||||
|
sqlx::query(
|
||||||
|
r#"
|
||||||
|
UPDATE execution_admission_entry
|
||||||
|
SET status = 'active',
|
||||||
|
activated_at = NOW()
|
||||||
|
WHERE execution_id = $1
|
||||||
|
AND state_id = $2
|
||||||
|
AND status = 'queued'
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(next_execution_id)
|
||||||
|
.bind(state.id)
|
||||||
|
.execute(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(next_execution_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn lock_state(
|
||||||
|
tx: &mut Transaction<'_, Postgres>,
|
||||||
|
action_id: Id,
|
||||||
|
group_key: Option<String>,
|
||||||
|
max_concurrent: u32,
|
||||||
|
) -> Result<AdmissionState> {
|
||||||
|
sqlx::query(
|
||||||
|
r#"
|
||||||
|
INSERT INTO execution_admission_state (action_id, group_key, max_concurrent)
|
||||||
|
VALUES ($1, $2, $3)
|
||||||
|
ON CONFLICT (action_id, group_key_normalized)
|
||||||
|
DO UPDATE SET max_concurrent = EXCLUDED.max_concurrent
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(action_id)
|
||||||
|
.bind(group_key.clone())
|
||||||
|
.bind(max_concurrent as i32)
|
||||||
|
.execute(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let state = sqlx::query(
|
||||||
|
r#"
|
||||||
|
SELECT id, action_id, group_key, max_concurrent
|
||||||
|
FROM execution_admission_state
|
||||||
|
WHERE action_id = $1
|
||||||
|
AND group_key_normalized = COALESCE($2, '')
|
||||||
|
FOR UPDATE
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(action_id)
|
||||||
|
.bind(group_key)
|
||||||
|
.fetch_one(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(AdmissionState {
|
||||||
|
id: state.try_get("id")?,
|
||||||
|
action_id: state.try_get("action_id")?,
|
||||||
|
group_key: state.try_get("group_key")?,
|
||||||
|
max_concurrent: state.try_get("max_concurrent")?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn lock_existing_state(
|
||||||
|
tx: &mut Transaction<'_, Postgres>,
|
||||||
|
action_id: Id,
|
||||||
|
group_key: Option<String>,
|
||||||
|
) -> Result<Option<AdmissionState>> {
|
||||||
|
let row = sqlx::query(
|
||||||
|
r#"
|
||||||
|
SELECT id, action_id, group_key, max_concurrent
|
||||||
|
FROM execution_admission_state
|
||||||
|
WHERE action_id = $1
|
||||||
|
AND group_key_normalized = COALESCE($2, '')
|
||||||
|
FOR UPDATE
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(action_id)
|
||||||
|
.bind(group_key)
|
||||||
|
.fetch_optional(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(row.map(|state| AdmissionState {
|
||||||
|
id: state.try_get("id").expect("state.id"),
|
||||||
|
action_id: state.try_get("action_id").expect("state.action_id"),
|
||||||
|
group_key: state.try_get("group_key").expect("state.group_key"),
|
||||||
|
max_concurrent: state
|
||||||
|
.try_get("max_concurrent")
|
||||||
|
.expect("state.max_concurrent"),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn find_execution_entry(
|
||||||
|
tx: &mut Transaction<'_, Postgres>,
|
||||||
|
execution_id: Id,
|
||||||
|
) -> Result<Option<ExecutionEntry>> {
|
||||||
|
let row = sqlx::query(
|
||||||
|
r#"
|
||||||
|
SELECT
|
||||||
|
e.state_id,
|
||||||
|
s.action_id,
|
||||||
|
s.group_key,
|
||||||
|
e.execution_id,
|
||||||
|
e.status,
|
||||||
|
e.queue_order,
|
||||||
|
e.enqueued_at
|
||||||
|
FROM execution_admission_entry e
|
||||||
|
JOIN execution_admission_state s ON s.id = e.state_id
|
||||||
|
WHERE e.execution_id = $1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(execution_id)
|
||||||
|
.fetch_optional(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(row.map(|entry| ExecutionEntry {
|
||||||
|
state_id: entry.try_get("state_id").expect("entry.state_id"),
|
||||||
|
action_id: entry.try_get("action_id").expect("entry.action_id"),
|
||||||
|
group_key: entry.try_get("group_key").expect("entry.group_key"),
|
||||||
|
status: entry.try_get("status").expect("entry.status"),
|
||||||
|
queue_order: entry.try_get("queue_order").expect("entry.queue_order"),
|
||||||
|
enqueued_at: entry.try_get("enqueued_at").expect("entry.enqueued_at"),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn find_execution_entry_for_update(
|
||||||
|
tx: &mut Transaction<'_, Postgres>,
|
||||||
|
execution_id: Id,
|
||||||
|
) -> Result<Option<ExecutionEntry>> {
|
||||||
|
let row = sqlx::query(
|
||||||
|
r#"
|
||||||
|
SELECT
|
||||||
|
e.state_id,
|
||||||
|
s.action_id,
|
||||||
|
s.group_key,
|
||||||
|
e.execution_id,
|
||||||
|
e.status,
|
||||||
|
e.queue_order,
|
||||||
|
e.enqueued_at
|
||||||
|
FROM execution_admission_entry e
|
||||||
|
JOIN execution_admission_state s ON s.id = e.state_id
|
||||||
|
WHERE e.execution_id = $1
|
||||||
|
FOR UPDATE OF e, s
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(execution_id)
|
||||||
|
.fetch_optional(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(row.map(|entry| ExecutionEntry {
|
||||||
|
state_id: entry.try_get("state_id").expect("entry.state_id"),
|
||||||
|
action_id: entry.try_get("action_id").expect("entry.action_id"),
|
||||||
|
group_key: entry.try_get("group_key").expect("entry.group_key"),
|
||||||
|
status: entry.try_get("status").expect("entry.status"),
|
||||||
|
queue_order: entry.try_get("queue_order").expect("entry.queue_order"),
|
||||||
|
enqueued_at: entry.try_get("enqueued_at").expect("entry.enqueued_at"),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn active_count(tx: &mut Transaction<'_, Postgres>, state_id: Id) -> Result<i64> {
|
||||||
|
Ok(sqlx::query_scalar::<Postgres, i64>(
|
||||||
|
r#"
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM execution_admission_entry
|
||||||
|
WHERE state_id = $1
|
||||||
|
AND status = 'active'
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(state_id)
|
||||||
|
.fetch_one(&mut **tx)
|
||||||
|
.await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn queued_count(tx: &mut Transaction<'_, Postgres>, state_id: Id) -> Result<i64> {
|
||||||
|
Ok(sqlx::query_scalar::<Postgres, i64>(
|
||||||
|
r#"
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM execution_admission_entry
|
||||||
|
WHERE state_id = $1
|
||||||
|
AND status = 'queued'
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(state_id)
|
||||||
|
.fetch_one(&mut **tx)
|
||||||
|
.await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn insert_entry(
|
||||||
|
tx: &mut Transaction<'_, Postgres>,
|
||||||
|
state_id: Id,
|
||||||
|
execution_id: Id,
|
||||||
|
status: &str,
|
||||||
|
queue_order: i64,
|
||||||
|
enqueued_at: DateTime<Utc>,
|
||||||
|
) -> Result<()> {
|
||||||
|
sqlx::query(
|
||||||
|
r#"
|
||||||
|
INSERT INTO execution_admission_entry (
|
||||||
|
state_id, execution_id, status, queue_order, enqueued_at, activated_at
|
||||||
|
) VALUES (
|
||||||
|
$1, $2, $3, $4, $5,
|
||||||
|
CASE WHEN $3 = 'active' THEN NOW() ELSE NULL END
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(state_id)
|
||||||
|
.bind(execution_id)
|
||||||
|
.bind(status)
|
||||||
|
.bind(queue_order)
|
||||||
|
.bind(enqueued_at)
|
||||||
|
.execute(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn allocate_queue_order(tx: &mut Transaction<'_, Postgres>, state_id: Id) -> Result<i64> {
|
||||||
|
let queue_order = sqlx::query_scalar::<Postgres, i64>(
|
||||||
|
r#"
|
||||||
|
UPDATE execution_admission_state
|
||||||
|
SET next_queue_order = next_queue_order + 1
|
||||||
|
WHERE id = $1
|
||||||
|
RETURNING next_queue_order - 1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(state_id)
|
||||||
|
.fetch_one(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(queue_order)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn increment_total_enqueued(
|
||||||
|
tx: &mut Transaction<'_, Postgres>,
|
||||||
|
state_id: Id,
|
||||||
|
) -> Result<()> {
|
||||||
|
sqlx::query(
|
||||||
|
r#"
|
||||||
|
UPDATE execution_admission_state
|
||||||
|
SET total_enqueued = total_enqueued + 1
|
||||||
|
WHERE id = $1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(state_id)
|
||||||
|
.execute(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn increment_total_completed(
|
||||||
|
tx: &mut Transaction<'_, Postgres>,
|
||||||
|
state_id: Id,
|
||||||
|
) -> Result<()> {
|
||||||
|
sqlx::query(
|
||||||
|
r#"
|
||||||
|
UPDATE execution_admission_state
|
||||||
|
SET total_completed = total_completed + 1
|
||||||
|
WHERE id = $1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(state_id)
|
||||||
|
.execute(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn refresh_queue_stats(tx: &mut Transaction<'_, Postgres>, action_id: Id) -> Result<()> {
|
||||||
|
let Some(stats) = Self::get_queue_stats_from_tx(tx, action_id).await? else {
|
||||||
|
QueueStatsRepository::delete(&mut **tx, action_id).await?;
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
QueueStatsRepository::upsert(
|
||||||
|
&mut **tx,
|
||||||
|
UpsertQueueStatsInput {
|
||||||
|
action_id,
|
||||||
|
queue_length: stats.queue_length as i32,
|
||||||
|
active_count: stats.active_count as i32,
|
||||||
|
max_concurrent: stats.max_concurrent as i32,
|
||||||
|
oldest_enqueued_at: stats.oldest_enqueued_at,
|
||||||
|
total_enqueued: stats.total_enqueued as i64,
|
||||||
|
total_completed: stats.total_completed as i64,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_queue_stats_from_tx(
|
||||||
|
tx: &mut Transaction<'_, Postgres>,
|
||||||
|
action_id: Id,
|
||||||
|
) -> Result<Option<AdmissionQueueStats>> {
|
||||||
|
let row = sqlx::query(
|
||||||
|
r#"
|
||||||
|
WITH state_rows AS (
|
||||||
|
SELECT
|
||||||
|
COUNT(*) AS state_count,
|
||||||
|
COALESCE(SUM(max_concurrent), 0) AS max_concurrent,
|
||||||
|
COALESCE(SUM(total_enqueued), 0) AS total_enqueued,
|
||||||
|
COALESCE(SUM(total_completed), 0) AS total_completed
|
||||||
|
FROM execution_admission_state
|
||||||
|
WHERE action_id = $1
|
||||||
|
),
|
||||||
|
entry_rows AS (
|
||||||
|
SELECT
|
||||||
|
COUNT(*) FILTER (WHERE e.status = 'queued') AS queue_length,
|
||||||
|
COUNT(*) FILTER (WHERE e.status = 'active') AS active_count,
|
||||||
|
MIN(e.enqueued_at) FILTER (WHERE e.status = 'queued') AS oldest_enqueued_at
|
||||||
|
FROM execution_admission_state s
|
||||||
|
LEFT JOIN execution_admission_entry e ON e.state_id = s.id
|
||||||
|
WHERE s.action_id = $1
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
sr.state_count,
|
||||||
|
er.queue_length,
|
||||||
|
er.active_count,
|
||||||
|
sr.max_concurrent,
|
||||||
|
er.oldest_enqueued_at,
|
||||||
|
sr.total_enqueued,
|
||||||
|
sr.total_completed
|
||||||
|
FROM state_rows sr
|
||||||
|
CROSS JOIN entry_rows er
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(action_id)
|
||||||
|
.fetch_one(&mut **tx)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let state_count: i64 = row.try_get("state_count")?;
|
||||||
|
if state_count == 0 {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Some(AdmissionQueueStats {
|
||||||
|
action_id,
|
||||||
|
queue_length: row.try_get::<i64, _>("queue_length")? as usize,
|
||||||
|
active_count: row.try_get::<i64, _>("active_count")? as u32,
|
||||||
|
max_concurrent: row.try_get::<i64, _>("max_concurrent")? as u32,
|
||||||
|
oldest_enqueued_at: row.try_get("oldest_enqueued_at")?,
|
||||||
|
total_enqueued: row.try_get::<i64, _>("total_enqueued")? as u64,
|
||||||
|
total_completed: row.try_get::<i64, _>("total_completed")? as u64,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,6 +28,7 @@ pub struct UpdateIdentityInput {
|
|||||||
pub display_name: Option<String>,
|
pub display_name: Option<String>,
|
||||||
pub password_hash: Option<String>,
|
pub password_hash: Option<String>,
|
||||||
pub attributes: Option<JsonDict>,
|
pub attributes: Option<JsonDict>,
|
||||||
|
pub frozen: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
@@ -37,7 +38,7 @@ impl FindById for IdentityRepository {
|
|||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
sqlx::query_as::<_, Identity>(
|
sqlx::query_as::<_, Identity>(
|
||||||
"SELECT id, login, display_name, password_hash, attributes, created, updated FROM identity WHERE id = $1"
|
"SELECT id, login, display_name, password_hash, attributes, frozen, created, updated FROM identity WHERE id = $1"
|
||||||
).bind(id).fetch_optional(executor).await.map_err(Into::into)
|
).bind(id).fetch_optional(executor).await.map_err(Into::into)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -49,7 +50,7 @@ impl List for IdentityRepository {
|
|||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
sqlx::query_as::<_, Identity>(
|
sqlx::query_as::<_, Identity>(
|
||||||
"SELECT id, login, display_name, password_hash, attributes, created, updated FROM identity ORDER BY login ASC"
|
"SELECT id, login, display_name, password_hash, attributes, frozen, created, updated FROM identity ORDER BY login ASC"
|
||||||
).fetch_all(executor).await.map_err(Into::into)
|
).fetch_all(executor).await.map_err(Into::into)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -62,7 +63,7 @@ impl Create for IdentityRepository {
|
|||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
sqlx::query_as::<_, Identity>(
|
sqlx::query_as::<_, Identity>(
|
||||||
"INSERT INTO identity (login, display_name, password_hash, attributes) VALUES ($1, $2, $3, $4) RETURNING id, login, display_name, password_hash, attributes, created, updated"
|
"INSERT INTO identity (login, display_name, password_hash, attributes) VALUES ($1, $2, $3, $4) RETURNING id, login, display_name, password_hash, attributes, frozen, created, updated"
|
||||||
)
|
)
|
||||||
.bind(&input.login)
|
.bind(&input.login)
|
||||||
.bind(&input.display_name)
|
.bind(&input.display_name)
|
||||||
@@ -111,6 +112,13 @@ impl Update for IdentityRepository {
|
|||||||
query.push("attributes = ").push_bind(attributes);
|
query.push("attributes = ").push_bind(attributes);
|
||||||
has_updates = true;
|
has_updates = true;
|
||||||
}
|
}
|
||||||
|
if let Some(frozen) = input.frozen {
|
||||||
|
if has_updates {
|
||||||
|
query.push(", ");
|
||||||
|
}
|
||||||
|
query.push("frozen = ").push_bind(frozen);
|
||||||
|
has_updates = true;
|
||||||
|
}
|
||||||
|
|
||||||
if !has_updates {
|
if !has_updates {
|
||||||
// No updates requested, fetch and return existing entity
|
// No updates requested, fetch and return existing entity
|
||||||
@@ -119,7 +127,7 @@ impl Update for IdentityRepository {
|
|||||||
|
|
||||||
query.push(", updated = NOW() WHERE id = ").push_bind(id);
|
query.push(", updated = NOW() WHERE id = ").push_bind(id);
|
||||||
query.push(
|
query.push(
|
||||||
" RETURNING id, login, display_name, password_hash, attributes, created, updated",
|
" RETURNING id, login, display_name, password_hash, attributes, frozen, created, updated",
|
||||||
);
|
);
|
||||||
|
|
||||||
query
|
query
|
||||||
@@ -156,7 +164,7 @@ impl IdentityRepository {
|
|||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
sqlx::query_as::<_, Identity>(
|
sqlx::query_as::<_, Identity>(
|
||||||
"SELECT id, login, display_name, password_hash, attributes, created, updated FROM identity WHERE login = $1"
|
"SELECT id, login, display_name, password_hash, attributes, frozen, created, updated FROM identity WHERE login = $1"
|
||||||
).bind(login).fetch_optional(executor).await.map_err(Into::into)
|
).bind(login).fetch_optional(executor).await.map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,7 +177,7 @@ impl IdentityRepository {
|
|||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
sqlx::query_as::<_, Identity>(
|
sqlx::query_as::<_, Identity>(
|
||||||
"SELECT id, login, display_name, password_hash, attributes, created, updated
|
"SELECT id, login, display_name, password_hash, attributes, frozen, created, updated
|
||||||
FROM identity
|
FROM identity
|
||||||
WHERE attributes->'oidc'->>'issuer' = $1
|
WHERE attributes->'oidc'->>'issuer' = $1
|
||||||
AND attributes->'oidc'->>'sub' = $2",
|
AND attributes->'oidc'->>'sub' = $2",
|
||||||
@@ -190,7 +198,7 @@ impl IdentityRepository {
|
|||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
sqlx::query_as::<_, Identity>(
|
sqlx::query_as::<_, Identity>(
|
||||||
"SELECT id, login, display_name, password_hash, attributes, created, updated
|
"SELECT id, login, display_name, password_hash, attributes, frozen, created, updated
|
||||||
FROM identity
|
FROM identity
|
||||||
WHERE attributes->'ldap'->>'server_url' = $1
|
WHERE attributes->'ldap'->>'server_url' = $1
|
||||||
AND attributes->'ldap'->>'dn' = $2",
|
AND attributes->'ldap'->>'dn' = $2",
|
||||||
@@ -363,6 +371,27 @@ impl PermissionSetRepository {
|
|||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn find_by_roles<'e, E>(executor: E, roles: &[String]) -> Result<Vec<PermissionSet>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
if roles.is_empty() {
|
||||||
|
return Ok(Vec::new());
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlx::query_as::<_, PermissionSet>(
|
||||||
|
"SELECT DISTINCT ps.id, ps.ref, ps.pack, ps.pack_ref, ps.label, ps.description, ps.grants, ps.created, ps.updated
|
||||||
|
FROM permission_set ps
|
||||||
|
INNER JOIN permission_set_role_assignment psra ON psra.permset = ps.id
|
||||||
|
WHERE psra.role = ANY($1)
|
||||||
|
ORDER BY ps.ref ASC",
|
||||||
|
)
|
||||||
|
.bind(roles)
|
||||||
|
.fetch_all(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
/// Delete permission sets belonging to a pack whose refs are NOT in the given set.
|
/// Delete permission sets belonging to a pack whose refs are NOT in the given set.
|
||||||
///
|
///
|
||||||
/// Used during pack reinstallation to clean up permission sets that were
|
/// Used during pack reinstallation to clean up permission sets that were
|
||||||
@@ -481,3 +510,231 @@ impl PermissionAssignmentRepository {
|
|||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct IdentityRoleAssignmentRepository;
|
||||||
|
|
||||||
|
impl Repository for IdentityRoleAssignmentRepository {
|
||||||
|
type Entity = IdentityRoleAssignment;
|
||||||
|
fn table_name() -> &'static str {
|
||||||
|
"identity_role_assignment"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct CreateIdentityRoleAssignmentInput {
|
||||||
|
pub identity: Id,
|
||||||
|
pub role: String,
|
||||||
|
pub source: String,
|
||||||
|
pub managed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl FindById for IdentityRoleAssignmentRepository {
|
||||||
|
async fn find_by_id<'e, E>(executor: E, id: i64) -> Result<Option<Self::Entity>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
sqlx::query_as::<_, IdentityRoleAssignment>(
|
||||||
|
"SELECT id, identity, role, source, managed, created, updated FROM identity_role_assignment WHERE id = $1"
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl Create for IdentityRoleAssignmentRepository {
|
||||||
|
type CreateInput = CreateIdentityRoleAssignmentInput;
|
||||||
|
async fn create<'e, E>(executor: E, input: Self::CreateInput) -> Result<Self::Entity>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
sqlx::query_as::<_, IdentityRoleAssignment>(
|
||||||
|
"INSERT INTO identity_role_assignment (identity, role, source, managed)
|
||||||
|
VALUES ($1, $2, $3, $4)
|
||||||
|
RETURNING id, identity, role, source, managed, created, updated",
|
||||||
|
)
|
||||||
|
.bind(input.identity)
|
||||||
|
.bind(&input.role)
|
||||||
|
.bind(&input.source)
|
||||||
|
.bind(input.managed)
|
||||||
|
.fetch_one(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl Delete for IdentityRoleAssignmentRepository {
|
||||||
|
async fn delete<'e, E>(executor: E, id: i64) -> Result<bool>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
let result = sqlx::query("DELETE FROM identity_role_assignment WHERE id = $1")
|
||||||
|
.bind(id)
|
||||||
|
.execute(executor)
|
||||||
|
.await?;
|
||||||
|
Ok(result.rows_affected() > 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IdentityRoleAssignmentRepository {
|
||||||
|
pub async fn find_by_identity<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
identity_id: Id,
|
||||||
|
) -> Result<Vec<IdentityRoleAssignment>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
sqlx::query_as::<_, IdentityRoleAssignment>(
|
||||||
|
"SELECT id, identity, role, source, managed, created, updated
|
||||||
|
FROM identity_role_assignment
|
||||||
|
WHERE identity = $1
|
||||||
|
ORDER BY role ASC",
|
||||||
|
)
|
||||||
|
.bind(identity_id)
|
||||||
|
.fetch_all(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn find_role_names_by_identity<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
identity_id: Id,
|
||||||
|
) -> Result<Vec<String>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
sqlx::query_scalar::<_, String>(
|
||||||
|
"SELECT role FROM identity_role_assignment WHERE identity = $1 ORDER BY role ASC",
|
||||||
|
)
|
||||||
|
.bind(identity_id)
|
||||||
|
.fetch_all(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn replace_managed_roles<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
identity_id: Id,
|
||||||
|
source: &str,
|
||||||
|
roles: &[String],
|
||||||
|
) -> Result<()>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + Copy + 'e,
|
||||||
|
{
|
||||||
|
sqlx::query(
|
||||||
|
"DELETE FROM identity_role_assignment WHERE identity = $1 AND source = $2 AND managed = true",
|
||||||
|
)
|
||||||
|
.bind(identity_id)
|
||||||
|
.bind(source)
|
||||||
|
.execute(executor)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
for role in roles {
|
||||||
|
sqlx::query(
|
||||||
|
"INSERT INTO identity_role_assignment (identity, role, source, managed)
|
||||||
|
VALUES ($1, $2, $3, true)
|
||||||
|
ON CONFLICT (identity, role) DO UPDATE
|
||||||
|
SET source = EXCLUDED.source,
|
||||||
|
managed = EXCLUDED.managed,
|
||||||
|
updated = NOW()",
|
||||||
|
)
|
||||||
|
.bind(identity_id)
|
||||||
|
.bind(role)
|
||||||
|
.bind(source)
|
||||||
|
.execute(executor)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PermissionSetRoleAssignmentRepository;
|
||||||
|
|
||||||
|
impl Repository for PermissionSetRoleAssignmentRepository {
|
||||||
|
type Entity = PermissionSetRoleAssignment;
|
||||||
|
fn table_name() -> &'static str {
|
||||||
|
"permission_set_role_assignment"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct CreatePermissionSetRoleAssignmentInput {
|
||||||
|
pub permset: Id,
|
||||||
|
pub role: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl FindById for PermissionSetRoleAssignmentRepository {
|
||||||
|
async fn find_by_id<'e, E>(executor: E, id: i64) -> Result<Option<Self::Entity>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
sqlx::query_as::<_, PermissionSetRoleAssignment>(
|
||||||
|
"SELECT id, permset, role, created FROM permission_set_role_assignment WHERE id = $1",
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl Create for PermissionSetRoleAssignmentRepository {
|
||||||
|
type CreateInput = CreatePermissionSetRoleAssignmentInput;
|
||||||
|
async fn create<'e, E>(executor: E, input: Self::CreateInput) -> Result<Self::Entity>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
sqlx::query_as::<_, PermissionSetRoleAssignment>(
|
||||||
|
"INSERT INTO permission_set_role_assignment (permset, role)
|
||||||
|
VALUES ($1, $2)
|
||||||
|
RETURNING id, permset, role, created",
|
||||||
|
)
|
||||||
|
.bind(input.permset)
|
||||||
|
.bind(&input.role)
|
||||||
|
.fetch_one(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl Delete for PermissionSetRoleAssignmentRepository {
|
||||||
|
async fn delete<'e, E>(executor: E, id: i64) -> Result<bool>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
let result = sqlx::query("DELETE FROM permission_set_role_assignment WHERE id = $1")
|
||||||
|
.bind(id)
|
||||||
|
.execute(executor)
|
||||||
|
.await?;
|
||||||
|
Ok(result.rows_affected() > 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PermissionSetRoleAssignmentRepository {
|
||||||
|
pub async fn find_by_permission_set<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
permset_id: Id,
|
||||||
|
) -> Result<Vec<PermissionSetRoleAssignment>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
sqlx::query_as::<_, PermissionSetRoleAssignment>(
|
||||||
|
"SELECT id, permset, role, created
|
||||||
|
FROM permission_set_role_assignment
|
||||||
|
WHERE permset = $1
|
||||||
|
ORDER BY role ASC",
|
||||||
|
)
|
||||||
|
.bind(permset_id)
|
||||||
|
.fetch_all(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ pub mod artifact;
|
|||||||
pub mod entity_history;
|
pub mod entity_history;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
pub mod execution;
|
pub mod execution;
|
||||||
|
pub mod execution_admission;
|
||||||
pub mod identity;
|
pub mod identity;
|
||||||
pub mod inquiry;
|
pub mod inquiry;
|
||||||
pub mod key;
|
pub mod key;
|
||||||
@@ -53,6 +54,7 @@ pub use artifact::{ArtifactRepository, ArtifactVersionRepository};
|
|||||||
pub use entity_history::EntityHistoryRepository;
|
pub use entity_history::EntityHistoryRepository;
|
||||||
pub use event::{EnforcementRepository, EventRepository};
|
pub use event::{EnforcementRepository, EventRepository};
|
||||||
pub use execution::ExecutionRepository;
|
pub use execution::ExecutionRepository;
|
||||||
|
pub use execution_admission::ExecutionAdmissionRepository;
|
||||||
pub use identity::{IdentityRepository, PermissionAssignmentRepository, PermissionSetRepository};
|
pub use identity::{IdentityRepository, PermissionAssignmentRepository, PermissionSetRepository};
|
||||||
pub use inquiry::InquiryRepository;
|
pub use inquiry::InquiryRepository;
|
||||||
pub use key::KeyRepository;
|
pub use key::KeyRepository;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
//! Provides database operations for queue statistics persistence.
|
//! Provides database operations for queue statistics persistence.
|
||||||
|
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use sqlx::{PgPool, Postgres, QueryBuilder};
|
use sqlx::{Executor, PgPool, Postgres, QueryBuilder};
|
||||||
|
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::models::Id;
|
use crate::models::Id;
|
||||||
@@ -38,7 +38,10 @@ pub struct QueueStatsRepository;
|
|||||||
|
|
||||||
impl QueueStatsRepository {
|
impl QueueStatsRepository {
|
||||||
/// Upsert queue statistics (insert or update)
|
/// Upsert queue statistics (insert or update)
|
||||||
pub async fn upsert(pool: &PgPool, input: UpsertQueueStatsInput) -> Result<QueueStats> {
|
pub async fn upsert<'e, E>(executor: E, input: UpsertQueueStatsInput) -> Result<QueueStats>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
let stats = sqlx::query_as::<Postgres, QueueStats>(
|
let stats = sqlx::query_as::<Postgres, QueueStats>(
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO queue_stats (
|
INSERT INTO queue_stats (
|
||||||
@@ -69,14 +72,17 @@ impl QueueStatsRepository {
|
|||||||
.bind(input.oldest_enqueued_at)
|
.bind(input.oldest_enqueued_at)
|
||||||
.bind(input.total_enqueued)
|
.bind(input.total_enqueued)
|
||||||
.bind(input.total_completed)
|
.bind(input.total_completed)
|
||||||
.fetch_one(pool)
|
.fetch_one(executor)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(stats)
|
Ok(stats)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get queue statistics for a specific action
|
/// Get queue statistics for a specific action
|
||||||
pub async fn find_by_action(pool: &PgPool, action_id: Id) -> Result<Option<QueueStats>> {
|
pub async fn find_by_action<'e, E>(executor: E, action_id: Id) -> Result<Option<QueueStats>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
let stats = sqlx::query_as::<Postgres, QueueStats>(
|
let stats = sqlx::query_as::<Postgres, QueueStats>(
|
||||||
r#"
|
r#"
|
||||||
SELECT
|
SELECT
|
||||||
@@ -93,14 +99,17 @@ impl QueueStatsRepository {
|
|||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(action_id)
|
.bind(action_id)
|
||||||
.fetch_optional(pool)
|
.fetch_optional(executor)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(stats)
|
Ok(stats)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// List all queue statistics with active queues (queue_length > 0 or active_count > 0)
|
/// List all queue statistics with active queues (queue_length > 0 or active_count > 0)
|
||||||
pub async fn list_active(pool: &PgPool) -> Result<Vec<QueueStats>> {
|
pub async fn list_active<'e, E>(executor: E) -> Result<Vec<QueueStats>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
let stats = sqlx::query_as::<Postgres, QueueStats>(
|
let stats = sqlx::query_as::<Postgres, QueueStats>(
|
||||||
r#"
|
r#"
|
||||||
SELECT
|
SELECT
|
||||||
@@ -117,14 +126,17 @@ impl QueueStatsRepository {
|
|||||||
ORDER BY last_updated DESC
|
ORDER BY last_updated DESC
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.fetch_all(pool)
|
.fetch_all(executor)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(stats)
|
Ok(stats)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// List all queue statistics
|
/// List all queue statistics
|
||||||
pub async fn list_all(pool: &PgPool) -> Result<Vec<QueueStats>> {
|
pub async fn list_all<'e, E>(executor: E) -> Result<Vec<QueueStats>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
let stats = sqlx::query_as::<Postgres, QueueStats>(
|
let stats = sqlx::query_as::<Postgres, QueueStats>(
|
||||||
r#"
|
r#"
|
||||||
SELECT
|
SELECT
|
||||||
@@ -140,14 +152,17 @@ impl QueueStatsRepository {
|
|||||||
ORDER BY last_updated DESC
|
ORDER BY last_updated DESC
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.fetch_all(pool)
|
.fetch_all(executor)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(stats)
|
Ok(stats)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Delete queue statistics for a specific action
|
/// Delete queue statistics for a specific action
|
||||||
pub async fn delete(pool: &PgPool, action_id: Id) -> Result<bool> {
|
pub async fn delete<'e, E>(executor: E, action_id: Id) -> Result<bool>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
let result = sqlx::query(
|
let result = sqlx::query(
|
||||||
r#"
|
r#"
|
||||||
DELETE FROM queue_stats
|
DELETE FROM queue_stats
|
||||||
@@ -155,7 +170,7 @@ impl QueueStatsRepository {
|
|||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(action_id)
|
.bind(action_id)
|
||||||
.execute(pool)
|
.execute(executor)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(result.rows_affected() > 0)
|
Ok(result.rows_affected() > 0)
|
||||||
@@ -163,7 +178,7 @@ impl QueueStatsRepository {
|
|||||||
|
|
||||||
/// Batch upsert multiple queue statistics
|
/// Batch upsert multiple queue statistics
|
||||||
pub async fn batch_upsert(
|
pub async fn batch_upsert(
|
||||||
pool: &PgPool,
|
executor: &PgPool,
|
||||||
inputs: Vec<UpsertQueueStatsInput>,
|
inputs: Vec<UpsertQueueStatsInput>,
|
||||||
) -> Result<Vec<QueueStats>> {
|
) -> Result<Vec<QueueStats>> {
|
||||||
if inputs.is_empty() {
|
if inputs.is_empty() {
|
||||||
@@ -213,14 +228,17 @@ impl QueueStatsRepository {
|
|||||||
|
|
||||||
let stats = query_builder
|
let stats = query_builder
|
||||||
.build_query_as::<QueueStats>()
|
.build_query_as::<QueueStats>()
|
||||||
.fetch_all(pool)
|
.fetch_all(executor)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(stats)
|
Ok(stats)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clear stale statistics (older than specified duration)
|
/// Clear stale statistics (older than specified duration)
|
||||||
pub async fn clear_stale(pool: &PgPool, older_than_seconds: i64) -> Result<u64> {
|
pub async fn clear_stale<'e, E>(executor: E, older_than_seconds: i64) -> Result<u64>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
let result = sqlx::query(
|
let result = sqlx::query(
|
||||||
r#"
|
r#"
|
||||||
DELETE FROM queue_stats
|
DELETE FROM queue_stats
|
||||||
@@ -230,7 +248,7 @@ impl QueueStatsRepository {
|
|||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(older_than_seconds)
|
.bind(older_than_seconds)
|
||||||
.execute(pool)
|
.execute(executor)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(result.rows_affected())
|
Ok(result.rows_affected())
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use crate::models::{rule::*, Id};
|
|||||||
use crate::{Error, Result};
|
use crate::{Error, Result};
|
||||||
use sqlx::{Executor, Postgres, QueryBuilder};
|
use sqlx::{Executor, Postgres, QueryBuilder};
|
||||||
|
|
||||||
use super::{Create, Delete, FindById, FindByRef, List, Repository, Update};
|
use super::{Create, Delete, FindById, FindByRef, List, Patch, Repository, Update};
|
||||||
|
|
||||||
/// Filters for [`RuleRepository::list_search`].
|
/// Filters for [`RuleRepository::list_search`].
|
||||||
///
|
///
|
||||||
@@ -41,7 +41,7 @@ pub struct RestoreRuleInput {
|
|||||||
pub pack: Id,
|
pub pack: Id,
|
||||||
pub pack_ref: String,
|
pub pack_ref: String,
|
||||||
pub label: String,
|
pub label: String,
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
pub action: Option<Id>,
|
pub action: Option<Id>,
|
||||||
pub action_ref: String,
|
pub action_ref: String,
|
||||||
pub trigger: Option<Id>,
|
pub trigger: Option<Id>,
|
||||||
@@ -70,7 +70,7 @@ pub struct CreateRuleInput {
|
|||||||
pub pack: Id,
|
pub pack: Id,
|
||||||
pub pack_ref: String,
|
pub pack_ref: String,
|
||||||
pub label: String,
|
pub label: String,
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
pub action: Id,
|
pub action: Id,
|
||||||
pub action_ref: String,
|
pub action_ref: String,
|
||||||
pub trigger: Id,
|
pub trigger: Id,
|
||||||
@@ -86,7 +86,7 @@ pub struct CreateRuleInput {
|
|||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct UpdateRuleInput {
|
pub struct UpdateRuleInput {
|
||||||
pub label: Option<String>,
|
pub label: Option<String>,
|
||||||
pub description: Option<String>,
|
pub description: Option<Patch<String>>,
|
||||||
pub conditions: Option<serde_json::Value>,
|
pub conditions: Option<serde_json::Value>,
|
||||||
pub action_params: Option<serde_json::Value>,
|
pub action_params: Option<serde_json::Value>,
|
||||||
pub trigger_params: Option<serde_json::Value>,
|
pub trigger_params: Option<serde_json::Value>,
|
||||||
@@ -228,7 +228,10 @@ impl Update for RuleRepository {
|
|||||||
query.push(", ");
|
query.push(", ");
|
||||||
}
|
}
|
||||||
query.push("description = ");
|
query.push("description = ");
|
||||||
query.push_bind(description);
|
match description {
|
||||||
|
Patch::Set(value) => query.push_bind(value),
|
||||||
|
Patch::Clear => query.push_bind(Option::<String>::None),
|
||||||
|
};
|
||||||
has_updates = true;
|
has_updates = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,13 @@ impl Repository for RuntimeRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Columns selected for all Runtime queries. Centralised here so that
|
||||||
|
/// schema changes only need one update.
|
||||||
|
pub const SELECT_COLUMNS: &str = "id, ref, pack, pack_ref, description, name, aliases, \
|
||||||
|
distributions, installation, installers, execution_config, \
|
||||||
|
auto_detected, detection_config, \
|
||||||
|
created, updated";
|
||||||
|
|
||||||
/// Input for creating a new runtime
|
/// Input for creating a new runtime
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct CreateRuntimeInput {
|
pub struct CreateRuntimeInput {
|
||||||
@@ -31,9 +38,12 @@ pub struct CreateRuntimeInput {
|
|||||||
pub pack_ref: Option<String>,
|
pub pack_ref: Option<String>,
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub aliases: Vec<String>,
|
||||||
pub distributions: JsonDict,
|
pub distributions: JsonDict,
|
||||||
pub installation: Option<JsonDict>,
|
pub installation: Option<JsonDict>,
|
||||||
pub execution_config: JsonDict,
|
pub execution_config: JsonDict,
|
||||||
|
pub auto_detected: bool,
|
||||||
|
pub detection_config: JsonDict,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Input for updating a runtime
|
/// Input for updating a runtime
|
||||||
@@ -41,9 +51,12 @@ pub struct CreateRuntimeInput {
|
|||||||
pub struct UpdateRuntimeInput {
|
pub struct UpdateRuntimeInput {
|
||||||
pub description: Option<Patch<String>>,
|
pub description: Option<Patch<String>>,
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
|
pub aliases: Option<Vec<String>>,
|
||||||
pub distributions: Option<JsonDict>,
|
pub distributions: Option<JsonDict>,
|
||||||
pub installation: Option<Patch<JsonDict>>,
|
pub installation: Option<Patch<JsonDict>>,
|
||||||
pub execution_config: Option<JsonDict>,
|
pub execution_config: Option<JsonDict>,
|
||||||
|
pub auto_detected: Option<bool>,
|
||||||
|
pub detection_config: Option<JsonDict>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
@@ -52,15 +65,8 @@ impl FindById for RuntimeRepository {
|
|||||||
where
|
where
|
||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
let runtime = sqlx::query_as::<_, Runtime>(
|
let query = format!("SELECT {} FROM runtime WHERE id = $1", SELECT_COLUMNS);
|
||||||
r#"
|
let runtime = sqlx::query_as::<_, Runtime>(&query)
|
||||||
SELECT id, ref, pack, pack_ref, description, name,
|
|
||||||
distributions, installation, installers, execution_config,
|
|
||||||
created, updated
|
|
||||||
FROM runtime
|
|
||||||
WHERE id = $1
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.bind(id)
|
.bind(id)
|
||||||
.fetch_optional(executor)
|
.fetch_optional(executor)
|
||||||
.await?;
|
.await?;
|
||||||
@@ -75,15 +81,8 @@ impl FindByRef for RuntimeRepository {
|
|||||||
where
|
where
|
||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
let runtime = sqlx::query_as::<_, Runtime>(
|
let query = format!("SELECT {} FROM runtime WHERE ref = $1", SELECT_COLUMNS);
|
||||||
r#"
|
let runtime = sqlx::query_as::<_, Runtime>(&query)
|
||||||
SELECT id, ref, pack, pack_ref, description, name,
|
|
||||||
distributions, installation, installers, execution_config,
|
|
||||||
created, updated
|
|
||||||
FROM runtime
|
|
||||||
WHERE ref = $1
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.bind(ref_str)
|
.bind(ref_str)
|
||||||
.fetch_optional(executor)
|
.fetch_optional(executor)
|
||||||
.await?;
|
.await?;
|
||||||
@@ -98,15 +97,8 @@ impl List for RuntimeRepository {
|
|||||||
where
|
where
|
||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
let runtimes = sqlx::query_as::<_, Runtime>(
|
let query = format!("SELECT {} FROM runtime ORDER BY ref ASC", SELECT_COLUMNS);
|
||||||
r#"
|
let runtimes = sqlx::query_as::<_, Runtime>(&query)
|
||||||
SELECT id, ref, pack, pack_ref, description, name,
|
|
||||||
distributions, installation, installers, execution_config,
|
|
||||||
created, updated
|
|
||||||
FROM runtime
|
|
||||||
ORDER BY ref ASC
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.fetch_all(executor)
|
.fetch_all(executor)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -122,25 +114,27 @@ impl Create for RuntimeRepository {
|
|||||||
where
|
where
|
||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
let runtime = sqlx::query_as::<_, Runtime>(
|
let query = format!(
|
||||||
r#"
|
"INSERT INTO runtime (ref, pack, pack_ref, description, name, aliases, \
|
||||||
INSERT INTO runtime (ref, pack, pack_ref, description, name,
|
distributions, installation, installers, execution_config, \
|
||||||
distributions, installation, installers, execution_config)
|
auto_detected, detection_config) \
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) \
|
||||||
RETURNING id, ref, pack, pack_ref, description, name,
|
RETURNING {}",
|
||||||
distributions, installation, installers, execution_config,
|
SELECT_COLUMNS
|
||||||
created, updated
|
);
|
||||||
"#,
|
let runtime = sqlx::query_as::<_, Runtime>(&query)
|
||||||
)
|
|
||||||
.bind(&input.r#ref)
|
.bind(&input.r#ref)
|
||||||
.bind(input.pack)
|
.bind(input.pack)
|
||||||
.bind(&input.pack_ref)
|
.bind(&input.pack_ref)
|
||||||
.bind(&input.description)
|
.bind(&input.description)
|
||||||
.bind(&input.name)
|
.bind(&input.name)
|
||||||
|
.bind(&input.aliases)
|
||||||
.bind(&input.distributions)
|
.bind(&input.distributions)
|
||||||
.bind(&input.installation)
|
.bind(&input.installation)
|
||||||
.bind(serde_json::json!({}))
|
.bind(serde_json::json!({}))
|
||||||
.bind(&input.execution_config)
|
.bind(&input.execution_config)
|
||||||
|
.bind(input.auto_detected)
|
||||||
|
.bind(&input.detection_config)
|
||||||
.fetch_one(executor)
|
.fetch_one(executor)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -179,6 +173,15 @@ impl Update for RuntimeRepository {
|
|||||||
has_updates = true;
|
has_updates = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(aliases) = &input.aliases {
|
||||||
|
if has_updates {
|
||||||
|
query.push(", ");
|
||||||
|
}
|
||||||
|
query.push("aliases = ");
|
||||||
|
query.push_bind(aliases.as_slice());
|
||||||
|
has_updates = true;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(distributions) = &input.distributions {
|
if let Some(distributions) = &input.distributions {
|
||||||
if has_updates {
|
if has_updates {
|
||||||
query.push(", ");
|
query.push(", ");
|
||||||
@@ -209,6 +212,24 @@ impl Update for RuntimeRepository {
|
|||||||
has_updates = true;
|
has_updates = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(auto_detected) = input.auto_detected {
|
||||||
|
if has_updates {
|
||||||
|
query.push(", ");
|
||||||
|
}
|
||||||
|
query.push("auto_detected = ");
|
||||||
|
query.push_bind(auto_detected);
|
||||||
|
has_updates = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(detection_config) = &input.detection_config {
|
||||||
|
if has_updates {
|
||||||
|
query.push(", ");
|
||||||
|
}
|
||||||
|
query.push("detection_config = ");
|
||||||
|
query.push_bind(detection_config);
|
||||||
|
has_updates = true;
|
||||||
|
}
|
||||||
|
|
||||||
if !has_updates {
|
if !has_updates {
|
||||||
// No updates requested, fetch and return existing entity
|
// No updates requested, fetch and return existing entity
|
||||||
return Self::get_by_id(executor, id).await;
|
return Self::get_by_id(executor, id).await;
|
||||||
@@ -216,10 +237,7 @@ impl Update for RuntimeRepository {
|
|||||||
|
|
||||||
query.push(", updated = NOW() WHERE id = ");
|
query.push(", updated = NOW() WHERE id = ");
|
||||||
query.push_bind(id);
|
query.push_bind(id);
|
||||||
query.push(
|
query.push(format!(" RETURNING {}", SELECT_COLUMNS));
|
||||||
" RETURNING id, ref, pack, pack_ref, description, name, \
|
|
||||||
distributions, installation, installers, execution_config, created, updated",
|
|
||||||
);
|
|
||||||
|
|
||||||
let runtime = query
|
let runtime = query
|
||||||
.build_query_as::<Runtime>()
|
.build_query_as::<Runtime>()
|
||||||
@@ -251,16 +269,11 @@ impl RuntimeRepository {
|
|||||||
where
|
where
|
||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
let runtimes = sqlx::query_as::<_, Runtime>(
|
let query = format!(
|
||||||
r#"
|
"SELECT {} FROM runtime WHERE pack = $1 ORDER BY ref ASC",
|
||||||
SELECT id, ref, pack, pack_ref, description, name,
|
SELECT_COLUMNS
|
||||||
distributions, installation, installers, execution_config,
|
);
|
||||||
created, updated
|
let runtimes = sqlx::query_as::<_, Runtime>(&query)
|
||||||
FROM runtime
|
|
||||||
WHERE pack = $1
|
|
||||||
ORDER BY ref ASC
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.bind(pack_id)
|
.bind(pack_id)
|
||||||
.fetch_all(executor)
|
.fetch_all(executor)
|
||||||
.await?;
|
.await?;
|
||||||
@@ -273,16 +286,11 @@ impl RuntimeRepository {
|
|||||||
where
|
where
|
||||||
E: Executor<'e, Database = Postgres> + 'e,
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
{
|
{
|
||||||
let runtime = sqlx::query_as::<_, Runtime>(
|
let query = format!(
|
||||||
r#"
|
"SELECT {} FROM runtime WHERE LOWER(name) = LOWER($1) LIMIT 1",
|
||||||
SELECT id, ref, pack, pack_ref, description, name,
|
SELECT_COLUMNS
|
||||||
distributions, installation, installers, execution_config,
|
);
|
||||||
created, updated
|
let runtime = sqlx::query_as::<_, Runtime>(&query)
|
||||||
FROM runtime
|
|
||||||
WHERE LOWER(name) = LOWER($1)
|
|
||||||
LIMIT 1
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.bind(name)
|
.bind(name)
|
||||||
.fetch_optional(executor)
|
.fetch_optional(executor)
|
||||||
.await?;
|
.await?;
|
||||||
@@ -290,6 +298,23 @@ impl RuntimeRepository {
|
|||||||
Ok(runtime)
|
Ok(runtime)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Find a runtime where the given alias appears in its `aliases` array.
|
||||||
|
/// Uses PostgreSQL's `@>` (array contains) operator with a GIN index.
|
||||||
|
pub async fn find_by_alias<'e, E>(executor: E, alias: &str) -> Result<Option<Runtime>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
let query = format!(
|
||||||
|
"SELECT {} FROM runtime WHERE aliases @> ARRAY[$1]::text[] LIMIT 1",
|
||||||
|
SELECT_COLUMNS
|
||||||
|
);
|
||||||
|
let runtime = sqlx::query_as::<_, Runtime>(&query)
|
||||||
|
.bind(alias)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await?;
|
||||||
|
Ok(runtime)
|
||||||
|
}
|
||||||
|
|
||||||
/// Delete runtimes belonging to a pack whose refs are NOT in the given set.
|
/// Delete runtimes belonging to a pack whose refs are NOT in the given set.
|
||||||
///
|
///
|
||||||
/// Used during pack reinstallation to clean up runtimes that were removed
|
/// Used during pack reinstallation to clean up runtimes that were removed
|
||||||
|
|||||||
@@ -665,7 +665,7 @@ pub struct CreateSensorInput {
|
|||||||
pub pack: Option<Id>,
|
pub pack: Option<Id>,
|
||||||
pub pack_ref: Option<String>,
|
pub pack_ref: Option<String>,
|
||||||
pub label: String,
|
pub label: String,
|
||||||
pub description: String,
|
pub description: Option<String>,
|
||||||
pub entrypoint: String,
|
pub entrypoint: String,
|
||||||
pub runtime: Id,
|
pub runtime: Id,
|
||||||
pub runtime_ref: String,
|
pub runtime_ref: String,
|
||||||
@@ -681,7 +681,7 @@ pub struct CreateSensorInput {
|
|||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct UpdateSensorInput {
|
pub struct UpdateSensorInput {
|
||||||
pub label: Option<String>,
|
pub label: Option<String>,
|
||||||
pub description: Option<String>,
|
pub description: Option<Patch<String>>,
|
||||||
pub entrypoint: Option<String>,
|
pub entrypoint: Option<String>,
|
||||||
pub runtime: Option<Id>,
|
pub runtime: Option<Id>,
|
||||||
pub runtime_ref: Option<String>,
|
pub runtime_ref: Option<String>,
|
||||||
@@ -830,7 +830,10 @@ impl Update for SensorRepository {
|
|||||||
query.push(", ");
|
query.push(", ");
|
||||||
}
|
}
|
||||||
query.push("description = ");
|
query.push("description = ");
|
||||||
query.push_bind(description);
|
match description {
|
||||||
|
Patch::Set(value) => query.push_bind(value),
|
||||||
|
Patch::Clear => query.push_bind(Option::<String>::None),
|
||||||
|
};
|
||||||
has_updates = true;
|
has_updates = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -411,6 +411,12 @@ impl WorkflowDefinitionRepository {
|
|||||||
|
|
||||||
pub struct WorkflowExecutionRepository;
|
pub struct WorkflowExecutionRepository;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct WorkflowExecutionCreateOrGetResult {
|
||||||
|
pub workflow_execution: WorkflowExecution,
|
||||||
|
pub created: bool,
|
||||||
|
}
|
||||||
|
|
||||||
impl Repository for WorkflowExecutionRepository {
|
impl Repository for WorkflowExecutionRepository {
|
||||||
type Entity = WorkflowExecution;
|
type Entity = WorkflowExecution;
|
||||||
fn table_name() -> &'static str {
|
fn table_name() -> &'static str {
|
||||||
@@ -606,6 +612,71 @@ impl Delete for WorkflowExecutionRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl WorkflowExecutionRepository {
|
impl WorkflowExecutionRepository {
|
||||||
|
pub async fn find_by_id_for_update<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
id: Id,
|
||||||
|
) -> Result<Option<WorkflowExecution>>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + 'e,
|
||||||
|
{
|
||||||
|
sqlx::query_as::<_, WorkflowExecution>(
|
||||||
|
"SELECT id, execution, workflow_def, current_tasks, completed_tasks, failed_tasks, skipped_tasks,
|
||||||
|
variables, task_graph, status, error_message, paused, pause_reason, created, updated
|
||||||
|
FROM workflow_execution
|
||||||
|
WHERE id = $1
|
||||||
|
FOR UPDATE"
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_or_get_by_execution<'e, E>(
|
||||||
|
executor: E,
|
||||||
|
input: CreateWorkflowExecutionInput,
|
||||||
|
) -> Result<WorkflowExecutionCreateOrGetResult>
|
||||||
|
where
|
||||||
|
E: Executor<'e, Database = Postgres> + Copy + 'e,
|
||||||
|
{
|
||||||
|
let inserted = sqlx::query_as::<_, WorkflowExecution>(
|
||||||
|
"INSERT INTO workflow_execution
|
||||||
|
(execution, workflow_def, task_graph, variables, status)
|
||||||
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
|
ON CONFLICT (execution) DO NOTHING
|
||||||
|
RETURNING id, execution, workflow_def, current_tasks, completed_tasks, failed_tasks, skipped_tasks,
|
||||||
|
variables, task_graph, status, error_message, paused, pause_reason, created, updated"
|
||||||
|
)
|
||||||
|
.bind(input.execution)
|
||||||
|
.bind(input.workflow_def)
|
||||||
|
.bind(&input.task_graph)
|
||||||
|
.bind(&input.variables)
|
||||||
|
.bind(input.status)
|
||||||
|
.fetch_optional(executor)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if let Some(workflow_execution) = inserted {
|
||||||
|
return Ok(WorkflowExecutionCreateOrGetResult {
|
||||||
|
workflow_execution,
|
||||||
|
created: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let workflow_execution = Self::find_by_execution(executor, input.execution)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| {
|
||||||
|
anyhow::anyhow!(
|
||||||
|
"workflow_execution for parent execution {} disappeared after conflict",
|
||||||
|
input.execution
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(WorkflowExecutionCreateOrGetResult {
|
||||||
|
workflow_execution,
|
||||||
|
created: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Find workflow execution by the parent execution ID
|
/// Find workflow execution by the parent execution ID
|
||||||
pub async fn find_by_execution<'e, E>(
|
pub async fn find_by_execution<'e, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
|
|||||||
@@ -6,59 +6,41 @@
|
|||||||
//! 2. Config file specification (medium priority)
|
//! 2. Config file specification (medium priority)
|
||||||
//! 3. Database-driven detection with verification (lowest priority)
|
//! 3. Database-driven detection with verification (lowest priority)
|
||||||
//!
|
//!
|
||||||
//! Also provides [`normalize_runtime_name`] for alias-aware runtime name
|
//! Also provides alias-based matching functions ([`runtime_aliases_match_filter`]
|
||||||
//! comparison across the codebase (worker filters, env setup, etc.).
|
//! and [`runtime_aliases_contain`]) for comparing runtime alias lists against
|
||||||
|
//! worker filters and capability strings. Aliases are declared per-runtime in
|
||||||
|
//! pack manifests, so no hardcoded alias table is needed here.
|
||||||
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::models::Runtime;
|
use crate::models::Runtime;
|
||||||
|
use crate::repositories::runtime::SELECT_COLUMNS;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use tracing::{debug, info, warn};
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
/// Normalize a runtime name to its canonical short form.
|
/// Check if a runtime's aliases overlap with a filter list.
|
||||||
///
|
///
|
||||||
/// This ensures that different ways of referring to the same runtime
|
/// The filter list comes from `ATTUNE_WORKER_RUNTIMES` (e.g., `["python", "shell"]`).
|
||||||
/// (e.g., "node", "nodejs", "node.js") all resolve to a single canonical
|
/// A runtime matches if any of its declared aliases appear in the filter list.
|
||||||
/// name. Used by worker runtime filters and environment setup to match
|
/// Comparison is case-insensitive.
|
||||||
/// database runtime names against short filter values.
|
pub fn runtime_aliases_match_filter(aliases: &[String], filter: &[String]) -> bool {
|
||||||
///
|
aliases.iter().any(|alias| {
|
||||||
/// The canonical names mirror the alias groups in
|
let lower_alias = alias.to_ascii_lowercase();
|
||||||
/// `PackComponentLoader::resolve_runtime`.
|
filter.iter().any(|f| f.to_ascii_lowercase() == lower_alias)
|
||||||
///
|
})
|
||||||
/// # Examples
|
|
||||||
/// ```
|
|
||||||
/// use attune_common::runtime_detection::normalize_runtime_name;
|
|
||||||
/// assert_eq!(normalize_runtime_name("node.js"), "node");
|
|
||||||
/// assert_eq!(normalize_runtime_name("nodejs"), "node");
|
|
||||||
/// assert_eq!(normalize_runtime_name("python3"), "python");
|
|
||||||
/// assert_eq!(normalize_runtime_name("shell"), "shell");
|
|
||||||
/// ```
|
|
||||||
pub fn normalize_runtime_name(name: &str) -> &str {
|
|
||||||
match name {
|
|
||||||
"node" | "nodejs" | "node.js" => "node",
|
|
||||||
"python" | "python3" => "python",
|
|
||||||
"bash" | "sh" | "shell" => "shell",
|
|
||||||
"native" | "builtin" | "standalone" => "native",
|
|
||||||
other => other,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if a runtime name matches a filter entry, supporting common aliases.
|
/// Check if a runtime's aliases contain a specific name.
|
||||||
///
|
///
|
||||||
/// Both sides are lowercased and then normalized before comparison so that,
|
/// Used by the scheduler to check if a worker's capability string
|
||||||
/// e.g., a filter value of `"node"` matches a database runtime name `"Node.js"`.
|
/// (e.g., "python") matches a runtime's aliases (e.g., ["python", "python3"]).
|
||||||
pub fn runtime_matches_filter(rt_name: &str, filter_entry: &str) -> bool {
|
/// Comparison is case-insensitive.
|
||||||
let rt_lower = rt_name.to_ascii_lowercase();
|
pub fn runtime_aliases_contain(aliases: &[String], name: &str) -> bool {
|
||||||
let filter_lower = filter_entry.to_ascii_lowercase();
|
let lower = name.to_ascii_lowercase();
|
||||||
normalize_runtime_name(&rt_lower) == normalize_runtime_name(&filter_lower)
|
aliases.iter().any(|a| a.to_ascii_lowercase() == lower)
|
||||||
}
|
|
||||||
|
|
||||||
/// Check if a runtime name matches any entry in a filter list.
|
|
||||||
pub fn runtime_in_filter(rt_name: &str, filter: &[String]) -> bool {
|
|
||||||
filter.iter().any(|f| runtime_matches_filter(rt_name, f))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Runtime detection service
|
/// Runtime detection service
|
||||||
@@ -156,15 +138,8 @@ impl RuntimeDetector {
|
|||||||
info!("Querying database for runtime definitions...");
|
info!("Querying database for runtime definitions...");
|
||||||
|
|
||||||
// Query all runtimes from database
|
// Query all runtimes from database
|
||||||
let runtimes = sqlx::query_as::<_, Runtime>(
|
let query = format!("SELECT {} FROM runtime ORDER BY ref", SELECT_COLUMNS);
|
||||||
r#"
|
let runtimes = sqlx::query_as::<_, Runtime>(&query)
|
||||||
SELECT id, ref, pack, pack_ref, description, name,
|
|
||||||
distributions, installation, installers, execution_config,
|
|
||||||
created, updated
|
|
||||||
FROM runtime
|
|
||||||
ORDER BY ref
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -337,69 +312,46 @@ mod tests {
|
|||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_normalize_runtime_name_node_variants() {
|
fn test_runtime_aliases_match_filter() {
|
||||||
assert_eq!(normalize_runtime_name("node"), "node");
|
let aliases = vec!["python".to_string(), "python3".to_string()];
|
||||||
assert_eq!(normalize_runtime_name("nodejs"), "node");
|
let filter = vec!["python".to_string(), "shell".to_string()];
|
||||||
assert_eq!(normalize_runtime_name("node.js"), "node");
|
assert!(runtime_aliases_match_filter(&aliases, &filter));
|
||||||
|
|
||||||
|
let filter_no_match = vec!["node".to_string(), "ruby".to_string()];
|
||||||
|
assert!(!runtime_aliases_match_filter(&aliases, &filter_no_match));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_normalize_runtime_name_python_variants() {
|
fn test_runtime_aliases_match_filter_case_insensitive() {
|
||||||
assert_eq!(normalize_runtime_name("python"), "python");
|
let aliases = vec!["Python".to_string(), "python3".to_string()];
|
||||||
assert_eq!(normalize_runtime_name("python3"), "python");
|
let filter = vec!["python".to_string()];
|
||||||
|
assert!(runtime_aliases_match_filter(&aliases, &filter));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_normalize_runtime_name_shell_variants() {
|
fn test_runtime_aliases_match_filter_empty() {
|
||||||
assert_eq!(normalize_runtime_name("shell"), "shell");
|
let aliases: Vec<String> = vec![];
|
||||||
assert_eq!(normalize_runtime_name("bash"), "shell");
|
let filter = vec!["python".to_string()];
|
||||||
assert_eq!(normalize_runtime_name("sh"), "shell");
|
assert!(!runtime_aliases_match_filter(&aliases, &filter));
|
||||||
|
|
||||||
|
let aliases = vec!["python".to_string()];
|
||||||
|
let filter: Vec<String> = vec![];
|
||||||
|
assert!(!runtime_aliases_match_filter(&aliases, &filter));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_normalize_runtime_name_native_variants() {
|
fn test_runtime_aliases_contain() {
|
||||||
assert_eq!(normalize_runtime_name("native"), "native");
|
let aliases = vec!["ruby".to_string(), "rb".to_string()];
|
||||||
assert_eq!(normalize_runtime_name("builtin"), "native");
|
assert!(runtime_aliases_contain(&aliases, "ruby"));
|
||||||
assert_eq!(normalize_runtime_name("standalone"), "native");
|
assert!(runtime_aliases_contain(&aliases, "rb"));
|
||||||
|
assert!(!runtime_aliases_contain(&aliases, "python"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_normalize_runtime_name_passthrough() {
|
fn test_runtime_aliases_contain_case_insensitive() {
|
||||||
assert_eq!(normalize_runtime_name("custom_runtime"), "custom_runtime");
|
let aliases = vec!["ruby".to_string(), "rb".to_string()];
|
||||||
}
|
assert!(runtime_aliases_contain(&aliases, "Ruby"));
|
||||||
|
assert!(runtime_aliases_contain(&aliases, "RB"));
|
||||||
#[test]
|
|
||||||
fn test_runtime_matches_filter() {
|
|
||||||
// Node.js DB name lowercased vs worker filter "node"
|
|
||||||
assert!(runtime_matches_filter("node.js", "node"));
|
|
||||||
assert!(runtime_matches_filter("node", "nodejs"));
|
|
||||||
assert!(runtime_matches_filter("nodejs", "node.js"));
|
|
||||||
// Exact match
|
|
||||||
assert!(runtime_matches_filter("shell", "shell"));
|
|
||||||
// No match
|
|
||||||
assert!(!runtime_matches_filter("python", "node"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_runtime_matches_filter_case_insensitive() {
|
|
||||||
// Database stores capitalized names (e.g., "Node.js", "Python")
|
|
||||||
// Worker capabilities store lowercase (e.g., "node", "python")
|
|
||||||
assert!(runtime_matches_filter("Node.js", "node"));
|
|
||||||
assert!(runtime_matches_filter("node", "Node.js"));
|
|
||||||
assert!(runtime_matches_filter("Python", "python"));
|
|
||||||
assert!(runtime_matches_filter("python", "Python"));
|
|
||||||
assert!(runtime_matches_filter("Shell", "shell"));
|
|
||||||
assert!(runtime_matches_filter("NODEJS", "node"));
|
|
||||||
assert!(!runtime_matches_filter("Python", "node"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_runtime_in_filter() {
|
|
||||||
let filter = vec!["shell".to_string(), "node".to_string()];
|
|
||||||
assert!(runtime_in_filter("shell", &filter));
|
|
||||||
assert!(runtime_in_filter("node.js", &filter));
|
|
||||||
assert!(runtime_in_filter("nodejs", &filter));
|
|
||||||
assert!(!runtime_in_filter("python", &filter));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -172,6 +172,7 @@ impl WorkflowLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read and parse YAML
|
// Read and parse YAML
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Workflow files come from previously discovered pack directories under packs_base_dir.
|
||||||
let content = fs::read_to_string(&file.path)
|
let content = fs::read_to_string(&file.path)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| Error::validation(format!("Failed to read workflow file: {}", e)))?;
|
.map_err(|e| Error::validation(format!("Failed to read workflow file: {}", e)))?;
|
||||||
@@ -292,6 +293,7 @@ impl WorkflowLoader {
|
|||||||
pack_name: &str,
|
pack_name: &str,
|
||||||
) -> Result<Vec<WorkflowFile>> {
|
) -> Result<Vec<WorkflowFile>> {
|
||||||
let mut workflow_files = Vec::new();
|
let mut workflow_files = Vec::new();
|
||||||
|
// nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path -- Workflow scanning only traverses pack workflow directories derived from packs_base_dir.
|
||||||
let mut entries = fs::read_dir(workflows_dir)
|
let mut entries = fs::read_dir(workflows_dir)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| Error::validation(format!("Failed to read workflows directory: {}", e)))?;
|
.map_err(|e| Error::validation(format!("Failed to read workflows directory: {}", e)))?;
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
use crate::error::{Error, Result};
|
use crate::error::{Error, Result};
|
||||||
use crate::repositories::action::{ActionRepository, CreateActionInput, UpdateActionInput};
|
use crate::repositories::action::{ActionRepository, CreateActionInput, UpdateActionInput};
|
||||||
use crate::repositories::workflow::{CreateWorkflowDefinitionInput, UpdateWorkflowDefinitionInput};
|
use crate::repositories::workflow::{CreateWorkflowDefinitionInput, UpdateWorkflowDefinitionInput};
|
||||||
|
use crate::repositories::Patch;
|
||||||
use crate::repositories::{
|
use crate::repositories::{
|
||||||
Create, Delete, FindByRef, PackRepository, Update, WorkflowDefinitionRepository,
|
Create, Delete, FindByRef, PackRepository, Update, WorkflowDefinitionRepository,
|
||||||
};
|
};
|
||||||
@@ -270,7 +271,7 @@ impl WorkflowRegistrar {
|
|||||||
pack: pack_id,
|
pack: pack_id,
|
||||||
pack_ref: pack_ref.to_string(),
|
pack_ref: pack_ref.to_string(),
|
||||||
label: effective_label.to_string(),
|
label: effective_label.to_string(),
|
||||||
description: workflow.description.clone().unwrap_or_default(),
|
description: workflow.description.clone(),
|
||||||
entrypoint,
|
entrypoint,
|
||||||
runtime: None,
|
runtime: None,
|
||||||
runtime_version_constraint: None,
|
runtime_version_constraint: None,
|
||||||
@@ -317,7 +318,10 @@ impl WorkflowRegistrar {
|
|||||||
// Update the existing companion action to stay in sync
|
// Update the existing companion action to stay in sync
|
||||||
let update_input = UpdateActionInput {
|
let update_input = UpdateActionInput {
|
||||||
label: Some(effective_label.to_string()),
|
label: Some(effective_label.to_string()),
|
||||||
description: workflow.description.clone(),
|
description: Some(match workflow.description.clone() {
|
||||||
|
Some(description) => Patch::Set(description),
|
||||||
|
None => Patch::Clear,
|
||||||
|
}),
|
||||||
entrypoint: Some(format!("workflows/{}.workflow.yaml", workflow_name)),
|
entrypoint: Some(format!("workflows/{}.workflow.yaml", workflow_name)),
|
||||||
runtime: None,
|
runtime: None,
|
||||||
runtime_version_constraint: None,
|
runtime_version_constraint: None,
|
||||||
|
|||||||
@@ -66,7 +66,10 @@ async fn test_create_action_with_optional_fields() {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(action.label, "Full Test Action");
|
assert_eq!(action.label, "Full Test Action");
|
||||||
assert_eq!(action.description, "Action with all optional fields");
|
assert_eq!(
|
||||||
|
action.description,
|
||||||
|
Some("Action with all optional fields".to_string())
|
||||||
|
);
|
||||||
assert_eq!(action.entrypoint, "custom.py");
|
assert_eq!(action.entrypoint, "custom.py");
|
||||||
assert!(action.param_schema.is_some());
|
assert!(action.param_schema.is_some());
|
||||||
assert!(action.out_schema.is_some());
|
assert!(action.out_schema.is_some());
|
||||||
@@ -204,7 +207,9 @@ async fn test_update_action() {
|
|||||||
|
|
||||||
let update = UpdateActionInput {
|
let update = UpdateActionInput {
|
||||||
label: Some("Updated Label".to_string()),
|
label: Some("Updated Label".to_string()),
|
||||||
description: Some("Updated description".to_string()),
|
description: Some(attune_common::repositories::Patch::Set(
|
||||||
|
"Updated description".to_string(),
|
||||||
|
)),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -214,7 +219,7 @@ async fn test_update_action() {
|
|||||||
|
|
||||||
assert_eq!(updated.id, action.id);
|
assert_eq!(updated.id, action.id);
|
||||||
assert_eq!(updated.label, "Updated Label");
|
assert_eq!(updated.label, "Updated Label");
|
||||||
assert_eq!(updated.description, "Updated description");
|
assert_eq!(updated.description, Some("Updated description".to_string()));
|
||||||
assert_eq!(updated.entrypoint, action.entrypoint); // Unchanged
|
assert_eq!(updated.entrypoint, action.entrypoint); // Unchanged
|
||||||
assert!(updated.updated > original_updated);
|
assert!(updated.updated > original_updated);
|
||||||
}
|
}
|
||||||
@@ -338,7 +343,7 @@ async fn test_action_foreign_key_constraint() {
|
|||||||
pack: 99999,
|
pack: 99999,
|
||||||
pack_ref: "nonexistent.pack".to_string(),
|
pack_ref: "nonexistent.pack".to_string(),
|
||||||
label: "Test Action".to_string(),
|
label: "Test Action".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
entrypoint: "main.py".to_string(),
|
entrypoint: "main.py".to_string(),
|
||||||
runtime: None,
|
runtime: None,
|
||||||
runtime_version_constraint: None,
|
runtime_version_constraint: None,
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ async fn test_create_enforcement_minimal() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -121,7 +121,7 @@ async fn test_create_enforcement_with_event() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -189,7 +189,7 @@ async fn test_create_enforcement_with_conditions() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -255,7 +255,7 @@ async fn test_create_enforcement_with_any_condition() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -397,7 +397,7 @@ async fn test_find_enforcement_by_id() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -471,7 +471,7 @@ async fn test_get_enforcement_by_id() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -552,7 +552,7 @@ async fn test_list_enforcements() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -624,7 +624,7 @@ async fn test_update_enforcement_status() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -690,7 +690,7 @@ async fn test_update_enforcement_status_transitions() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -769,7 +769,7 @@ async fn test_update_enforcement_payload() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -832,7 +832,7 @@ async fn test_update_enforcement_both_fields() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -896,7 +896,7 @@ async fn test_update_enforcement_no_changes() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -981,7 +981,7 @@ async fn test_delete_enforcement() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -1056,7 +1056,7 @@ async fn test_find_enforcements_by_rule() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Rule 1".to_string(),
|
label: "Rule 1".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -1078,7 +1078,7 @@ async fn test_find_enforcements_by_rule() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Rule 2".to_string(),
|
label: "Rule 2".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -1149,7 +1149,7 @@ async fn test_find_enforcements_by_status() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -1239,7 +1239,7 @@ async fn test_find_enforcements_by_event() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -1324,7 +1324,7 @@ async fn test_delete_rule_sets_enforcement_rule_to_null() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -1390,7 +1390,7 @@ async fn test_enforcement_resolved_at_lifecycle() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -1430,3 +1430,70 @@ async fn test_enforcement_resolved_at_lifecycle() {
|
|||||||
assert!(updated.resolved_at.is_some());
|
assert!(updated.resolved_at.is_some());
|
||||||
assert!(updated.resolved_at.unwrap() >= enforcement.created);
|
assert!(updated.resolved_at.unwrap() >= enforcement.created);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "integration test — requires database"]
|
||||||
|
async fn test_update_loaded_enforcement_uses_loaded_locator() {
|
||||||
|
let pool = create_test_pool().await.unwrap();
|
||||||
|
|
||||||
|
let pack = PackFixture::new_unique("targeted_update_pack")
|
||||||
|
.create(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let trigger = TriggerFixture::new_unique(Some(pack.id), Some(pack.r#ref.clone()), "webhook")
|
||||||
|
.create(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let action = ActionFixture::new_unique(pack.id, &pack.r#ref, "action")
|
||||||
|
.create(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
use attune_common::repositories::rule::{CreateRuleInput, RuleRepository};
|
||||||
|
let rule = RuleRepository::create(
|
||||||
|
&pool,
|
||||||
|
CreateRuleInput {
|
||||||
|
r#ref: format!("{}.test_rule", pack.r#ref),
|
||||||
|
pack: pack.id,
|
||||||
|
pack_ref: pack.r#ref.clone(),
|
||||||
|
label: "Test Rule".to_string(),
|
||||||
|
description: Some("Test".to_string()),
|
||||||
|
action: action.id,
|
||||||
|
action_ref: action.r#ref.clone(),
|
||||||
|
trigger: trigger.id,
|
||||||
|
trigger_ref: trigger.r#ref.clone(),
|
||||||
|
conditions: json!({}),
|
||||||
|
action_params: json!({}),
|
||||||
|
trigger_params: json!({}),
|
||||||
|
enabled: true,
|
||||||
|
is_adhoc: false,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let enforcement = EnforcementFixture::new_unique(Some(rule.id), &rule.r#ref, &trigger.r#ref)
|
||||||
|
.create(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let updated = EnforcementRepository::update_loaded(
|
||||||
|
&pool,
|
||||||
|
&enforcement,
|
||||||
|
UpdateEnforcementInput {
|
||||||
|
status: Some(EnforcementStatus::Processed),
|
||||||
|
payload: None,
|
||||||
|
resolved_at: Some(chrono::Utc::now()),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(updated.id, enforcement.id);
|
||||||
|
assert_eq!(updated.created, enforcement.created);
|
||||||
|
assert_eq!(updated.rule_ref, enforcement.rule_ref);
|
||||||
|
assert_eq!(updated.status, EnforcementStatus::Processed);
|
||||||
|
assert!(updated.resolved_at.is_some());
|
||||||
|
}
|
||||||
|
|||||||
@@ -449,7 +449,7 @@ async fn test_delete_event_enforcement_retains_event_id() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
|
|||||||
@@ -1153,3 +1153,108 @@ async fn test_execution_result_json() {
|
|||||||
|
|
||||||
assert_eq!(updated.result, Some(complex_result));
|
assert_eq!(updated.result, Some(complex_result));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "integration test — requires database"]
|
||||||
|
async fn test_claim_for_scheduling_succeeds_once() {
|
||||||
|
let pool = create_test_pool().await.unwrap();
|
||||||
|
|
||||||
|
let pack = PackFixture::new_unique("claim_pack")
|
||||||
|
.create(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let action = ActionFixture::new_unique(pack.id, &pack.r#ref, "claim_action")
|
||||||
|
.create(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let created = ExecutionRepository::create(
|
||||||
|
&pool,
|
||||||
|
CreateExecutionInput {
|
||||||
|
action: Some(action.id),
|
||||||
|
action_ref: action.r#ref.clone(),
|
||||||
|
config: None,
|
||||||
|
env_vars: None,
|
||||||
|
parent: None,
|
||||||
|
enforcement: None,
|
||||||
|
executor: None,
|
||||||
|
worker: None,
|
||||||
|
status: ExecutionStatus::Requested,
|
||||||
|
result: None,
|
||||||
|
workflow_task: None,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let first = ExecutionRepository::claim_for_scheduling(&pool, created.id, None)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let second = ExecutionRepository::claim_for_scheduling(&pool, created.id, None)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(first.unwrap().status, ExecutionStatus::Scheduling);
|
||||||
|
assert!(second.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "integration test — requires database"]
|
||||||
|
async fn test_update_if_status_only_updates_matching_row() {
|
||||||
|
let pool = create_test_pool().await.unwrap();
|
||||||
|
|
||||||
|
let pack = PackFixture::new_unique("conditional_pack")
|
||||||
|
.create(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let action = ActionFixture::new_unique(pack.id, &pack.r#ref, "conditional_action")
|
||||||
|
.create(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let created = ExecutionRepository::create(
|
||||||
|
&pool,
|
||||||
|
CreateExecutionInput {
|
||||||
|
action: Some(action.id),
|
||||||
|
action_ref: action.r#ref.clone(),
|
||||||
|
config: None,
|
||||||
|
env_vars: None,
|
||||||
|
parent: None,
|
||||||
|
enforcement: None,
|
||||||
|
executor: None,
|
||||||
|
worker: None,
|
||||||
|
status: ExecutionStatus::Scheduling,
|
||||||
|
result: None,
|
||||||
|
workflow_task: None,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let updated = ExecutionRepository::update_if_status(
|
||||||
|
&pool,
|
||||||
|
created.id,
|
||||||
|
ExecutionStatus::Scheduling,
|
||||||
|
UpdateExecutionInput {
|
||||||
|
status: Some(ExecutionStatus::Scheduled),
|
||||||
|
worker: Some(77),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let skipped = ExecutionRepository::update_if_status(
|
||||||
|
&pool,
|
||||||
|
created.id,
|
||||||
|
ExecutionStatus::Scheduling,
|
||||||
|
UpdateExecutionInput {
|
||||||
|
status: Some(ExecutionStatus::Failed),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(updated.unwrap().status, ExecutionStatus::Scheduled);
|
||||||
|
assert!(skipped.is_none());
|
||||||
|
}
|
||||||
|
|||||||
@@ -454,7 +454,7 @@ impl ActionFixture {
|
|||||||
pack_ref: self.pack_ref,
|
pack_ref: self.pack_ref,
|
||||||
r#ref: self.r#ref,
|
r#ref: self.r#ref,
|
||||||
label: self.label,
|
label: self.label,
|
||||||
description: self.description,
|
description: Some(self.description),
|
||||||
entrypoint: self.entrypoint,
|
entrypoint: self.entrypoint,
|
||||||
runtime: self.runtime,
|
runtime: self.runtime,
|
||||||
runtime_version_constraint: None,
|
runtime_version_constraint: None,
|
||||||
@@ -961,9 +961,12 @@ impl RuntimeFixture {
|
|||||||
pack_ref: self.pack_ref,
|
pack_ref: self.pack_ref,
|
||||||
description: self.description,
|
description: self.description,
|
||||||
name: self.name,
|
name: self.name,
|
||||||
|
aliases: vec![],
|
||||||
distributions: self.distributions,
|
distributions: self.distributions,
|
||||||
installation: self.installation,
|
installation: self.installation,
|
||||||
execution_config: self.execution_config,
|
execution_config: self.execution_config,
|
||||||
|
auto_detected: false,
|
||||||
|
detection_config: serde_json::json!({}),
|
||||||
};
|
};
|
||||||
|
|
||||||
RuntimeRepository::create(pool, input).await
|
RuntimeRepository::create(pool, input).await
|
||||||
@@ -1085,7 +1088,7 @@ impl SensorFixture {
|
|||||||
pack: self.pack_id,
|
pack: self.pack_id,
|
||||||
pack_ref: self.pack_ref,
|
pack_ref: self.pack_ref,
|
||||||
label: self.label,
|
label: self.label,
|
||||||
description: self.description,
|
description: Some(self.description),
|
||||||
entrypoint: self.entrypoint,
|
entrypoint: self.entrypoint,
|
||||||
runtime: self.runtime_id,
|
runtime: self.runtime_id,
|
||||||
runtime_ref: self.runtime_ref,
|
runtime_ref: self.runtime_ref,
|
||||||
|
|||||||
@@ -219,6 +219,7 @@ async fn test_update_identity() {
|
|||||||
display_name: Some("Updated Name".to_string()),
|
display_name: Some("Updated Name".to_string()),
|
||||||
password_hash: None,
|
password_hash: None,
|
||||||
attributes: Some(json!({"key": "updated", "new_key": "new_value"})),
|
attributes: Some(json!({"key": "updated", "new_key": "new_value"})),
|
||||||
|
frozen: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let updated = IdentityRepository::update(&pool, identity.id, update_input)
|
let updated = IdentityRepository::update(&pool, identity.id, update_input)
|
||||||
@@ -252,6 +253,7 @@ async fn test_update_identity_partial() {
|
|||||||
display_name: Some("Only Display Name Changed".to_string()),
|
display_name: Some("Only Display Name Changed".to_string()),
|
||||||
password_hash: None,
|
password_hash: None,
|
||||||
attributes: None,
|
attributes: None,
|
||||||
|
frozen: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let updated = IdentityRepository::update(&pool, identity.id, update_input)
|
let updated = IdentityRepository::update(&pool, identity.id, update_input)
|
||||||
@@ -274,6 +276,7 @@ async fn test_update_identity_not_found() {
|
|||||||
display_name: Some("Updated Name".to_string()),
|
display_name: Some("Updated Name".to_string()),
|
||||||
password_hash: None,
|
password_hash: None,
|
||||||
attributes: None,
|
attributes: None,
|
||||||
|
frozen: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = IdentityRepository::update(&pool, 999999, update_input).await;
|
let result = IdentityRepository::update(&pool, 999999, update_input).await;
|
||||||
@@ -380,6 +383,7 @@ async fn test_identity_updated_changes_on_update() {
|
|||||||
display_name: Some("Updated".to_string()),
|
display_name: Some("Updated".to_string()),
|
||||||
password_hash: None,
|
password_hash: None,
|
||||||
attributes: None,
|
attributes: None,
|
||||||
|
frozen: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let updated = IdentityRepository::update(&pool, identity.id, update_input)
|
let updated = IdentityRepository::update(&pool, identity.id, update_input)
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ impl RuntimeFixture {
|
|||||||
pack_ref: None,
|
pack_ref: None,
|
||||||
description: Some(format!("Test runtime {}", seq)),
|
description: Some(format!("Test runtime {}", seq)),
|
||||||
name,
|
name,
|
||||||
|
aliases: vec![],
|
||||||
distributions: json!({
|
distributions: json!({
|
||||||
"linux": { "supported": true, "versions": ["ubuntu20.04", "ubuntu22.04"] },
|
"linux": { "supported": true, "versions": ["ubuntu20.04", "ubuntu22.04"] },
|
||||||
"darwin": { "supported": true, "versions": ["12", "13"] }
|
"darwin": { "supported": true, "versions": ["12", "13"] }
|
||||||
@@ -79,6 +80,8 @@ impl RuntimeFixture {
|
|||||||
"file_extension": ".py"
|
"file_extension": ".py"
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
auto_detected: false,
|
||||||
|
detection_config: json!({}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +96,7 @@ impl RuntimeFixture {
|
|||||||
pack_ref: None,
|
pack_ref: None,
|
||||||
description: None,
|
description: None,
|
||||||
name,
|
name,
|
||||||
|
aliases: vec![],
|
||||||
distributions: json!({}),
|
distributions: json!({}),
|
||||||
installation: None,
|
installation: None,
|
||||||
execution_config: json!({
|
execution_config: json!({
|
||||||
@@ -102,6 +106,8 @@ impl RuntimeFixture {
|
|||||||
"file_extension": ".sh"
|
"file_extension": ".sh"
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
auto_detected: false,
|
||||||
|
detection_config: json!({}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -268,6 +274,7 @@ async fn test_update_runtime() {
|
|||||||
"method": "npm"
|
"method": "npm"
|
||||||
}))),
|
}))),
|
||||||
execution_config: None,
|
execution_config: None,
|
||||||
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let updated = RuntimeRepository::update(&pool, created.id, update_input.clone())
|
let updated = RuntimeRepository::update(&pool, created.id, update_input.clone())
|
||||||
@@ -299,6 +306,7 @@ async fn test_update_runtime_partial() {
|
|||||||
distributions: None,
|
distributions: None,
|
||||||
installation: None,
|
installation: None,
|
||||||
execution_config: None,
|
execution_config: None,
|
||||||
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let updated = RuntimeRepository::update(&pool, created.id, update_input.clone())
|
let updated = RuntimeRepository::update(&pool, created.id, update_input.clone())
|
||||||
|
|||||||
@@ -574,6 +574,7 @@ async fn test_worker_with_runtime() {
|
|||||||
pack_ref: None,
|
pack_ref: None,
|
||||||
description: Some("Test runtime".to_string()),
|
description: Some("Test runtime".to_string()),
|
||||||
name: "test_runtime".to_string(),
|
name: "test_runtime".to_string(),
|
||||||
|
aliases: vec![],
|
||||||
distributions: json!({}),
|
distributions: json!({}),
|
||||||
installation: None,
|
installation: None,
|
||||||
execution_config: json!({
|
execution_config: json!({
|
||||||
@@ -583,6 +584,8 @@ async fn test_worker_with_runtime() {
|
|||||||
"file_extension": ".sh"
|
"file_extension": ".sh"
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
auto_detected: false,
|
||||||
|
detection_config: json!({}),
|
||||||
};
|
};
|
||||||
|
|
||||||
let runtime = RuntimeRepository::create(&pool, runtime_input)
|
let runtime = RuntimeRepository::create(&pool, runtime_input)
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ mod helpers;
|
|||||||
use attune_common::{
|
use attune_common::{
|
||||||
repositories::{
|
repositories::{
|
||||||
rule::{CreateRuleInput, RuleRepository, UpdateRuleInput},
|
rule::{CreateRuleInput, RuleRepository, UpdateRuleInput},
|
||||||
Create, Delete, FindById, FindByRef, List, Update,
|
Create, Delete, FindById, FindByRef, List, Patch, Update,
|
||||||
},
|
},
|
||||||
Error,
|
Error,
|
||||||
};
|
};
|
||||||
@@ -48,7 +48,7 @@ async fn test_create_rule() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "A test rule".to_string(),
|
description: Some("A test rule".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -66,7 +66,7 @@ async fn test_create_rule() {
|
|||||||
assert_eq!(rule.pack, pack.id);
|
assert_eq!(rule.pack, pack.id);
|
||||||
assert_eq!(rule.pack_ref, pack.r#ref);
|
assert_eq!(rule.pack_ref, pack.r#ref);
|
||||||
assert_eq!(rule.label, "Test Rule");
|
assert_eq!(rule.label, "Test Rule");
|
||||||
assert_eq!(rule.description, "A test rule");
|
assert_eq!(rule.description, Some("A test rule".to_string()));
|
||||||
assert_eq!(rule.action, Some(action.id));
|
assert_eq!(rule.action, Some(action.id));
|
||||||
assert_eq!(rule.action_ref, action.r#ref);
|
assert_eq!(rule.action_ref, action.r#ref);
|
||||||
assert_eq!(rule.trigger, Some(trigger.id));
|
assert_eq!(rule.trigger, Some(trigger.id));
|
||||||
@@ -105,7 +105,7 @@ async fn test_create_rule_disabled() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Disabled Rule".to_string(),
|
label: "Disabled Rule".to_string(),
|
||||||
description: "A disabled rule".to_string(),
|
description: Some("A disabled rule".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -155,7 +155,7 @@ async fn test_create_rule_with_complex_conditions() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Complex Rule".to_string(),
|
label: "Complex Rule".to_string(),
|
||||||
description: "Rule with complex conditions".to_string(),
|
description: Some("Rule with complex conditions".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -200,7 +200,7 @@ async fn test_create_rule_duplicate_ref() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "First Rule".to_string(),
|
label: "First Rule".to_string(),
|
||||||
description: "First".to_string(),
|
description: Some("First".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -220,7 +220,7 @@ async fn test_create_rule_duplicate_ref() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Second Rule".to_string(),
|
label: "Second Rule".to_string(),
|
||||||
description: "Second".to_string(),
|
description: Some("Second".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -274,7 +274,7 @@ async fn test_create_rule_invalid_ref_format_uppercase() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Upper Rule".to_string(),
|
label: "Upper Rule".to_string(),
|
||||||
description: "Invalid uppercase ref".to_string(),
|
description: Some("Invalid uppercase ref".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -316,7 +316,7 @@ async fn test_create_rule_invalid_ref_format_no_dot() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "No Dot Rule".to_string(),
|
label: "No Dot Rule".to_string(),
|
||||||
description: "Invalid ref without dot".to_string(),
|
description: Some("Invalid ref without dot".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -362,7 +362,7 @@ async fn test_find_rule_by_id() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Find Rule".to_string(),
|
label: "Find Rule".to_string(),
|
||||||
description: "Rule to find".to_string(),
|
description: Some("Rule to find".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -422,7 +422,7 @@ async fn test_find_rule_by_ref() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Find By Ref Rule".to_string(),
|
label: "Find By Ref Rule".to_string(),
|
||||||
description: "Find by ref".to_string(),
|
description: Some("Find by ref".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -484,7 +484,7 @@ async fn test_list_rules() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: format!("List Rule {}", i),
|
label: format!("List Rule {}", i),
|
||||||
description: format!("Rule {}", i),
|
description: Some(format!("Rule {}", i)),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -538,7 +538,7 @@ async fn test_list_rules_ordered_by_ref() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: name.to_string(),
|
label: name.to_string(),
|
||||||
description: name.to_string(),
|
description: Some(name.to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -594,7 +594,7 @@ async fn test_update_rule_label() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Original Label".to_string(),
|
label: "Original Label".to_string(),
|
||||||
description: "Original".to_string(),
|
description: Some("Original".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -618,7 +618,7 @@ async fn test_update_rule_label() {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(updated.label, "Updated Label");
|
assert_eq!(updated.label, "Updated Label");
|
||||||
assert_eq!(updated.description, "Original"); // unchanged
|
assert_eq!(updated.description, Some("Original".to_string())); // unchanged
|
||||||
assert!(updated.updated > created.updated);
|
assert!(updated.updated > created.updated);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -647,7 +647,7 @@ async fn test_update_rule_description() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test".to_string(),
|
label: "Test".to_string(),
|
||||||
description: "Old description".to_string(),
|
description: Some("Old description".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -662,7 +662,7 @@ async fn test_update_rule_description() {
|
|||||||
let created = RuleRepository::create(&pool, input).await.unwrap();
|
let created = RuleRepository::create(&pool, input).await.unwrap();
|
||||||
|
|
||||||
let update = UpdateRuleInput {
|
let update = UpdateRuleInput {
|
||||||
description: Some("New description".to_string()),
|
description: Some(Patch::Set("New description".to_string())),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -670,7 +670,7 @@ async fn test_update_rule_description() {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(updated.description, "New description");
|
assert_eq!(updated.description, Some("New description".to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -698,7 +698,7 @@ async fn test_update_rule_conditions() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test".to_string(),
|
label: "Test".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -750,7 +750,7 @@ async fn test_update_rule_enabled() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test".to_string(),
|
label: "Test".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -803,7 +803,7 @@ async fn test_update_rule_multiple_fields() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Old".to_string(),
|
label: "Old".to_string(),
|
||||||
description: "Old".to_string(),
|
description: Some("Old".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -819,7 +819,7 @@ async fn test_update_rule_multiple_fields() {
|
|||||||
|
|
||||||
let update = UpdateRuleInput {
|
let update = UpdateRuleInput {
|
||||||
label: Some("New Label".to_string()),
|
label: Some("New Label".to_string()),
|
||||||
description: Some("New Description".to_string()),
|
description: Some(Patch::Set("New Description".to_string())),
|
||||||
conditions: Some(json!({"updated": true})),
|
conditions: Some(json!({"updated": true})),
|
||||||
action_params: None,
|
action_params: None,
|
||||||
trigger_params: None,
|
trigger_params: None,
|
||||||
@@ -831,7 +831,7 @@ async fn test_update_rule_multiple_fields() {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(updated.label, "New Label");
|
assert_eq!(updated.label, "New Label");
|
||||||
assert_eq!(updated.description, "New Description");
|
assert_eq!(updated.description, Some("New Description".to_string()));
|
||||||
assert_eq!(updated.conditions, json!({"updated": true}));
|
assert_eq!(updated.conditions, json!({"updated": true}));
|
||||||
assert!(!updated.enabled);
|
assert!(!updated.enabled);
|
||||||
}
|
}
|
||||||
@@ -861,7 +861,7 @@ async fn test_update_rule_no_changes() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Test".to_string(),
|
label: "Test".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -914,7 +914,7 @@ async fn test_delete_rule() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "To Delete".to_string(),
|
label: "To Delete".to_string(),
|
||||||
description: "Will be deleted".to_string(),
|
description: Some("Will be deleted".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -995,7 +995,7 @@ async fn test_find_rules_by_pack() {
|
|||||||
pack: pack1.id,
|
pack: pack1.id,
|
||||||
pack_ref: pack1.r#ref.clone(),
|
pack_ref: pack1.r#ref.clone(),
|
||||||
label: format!("Rule {}", i),
|
label: format!("Rule {}", i),
|
||||||
description: format!("Rule {}", i),
|
description: Some(format!("Rule {}", i)),
|
||||||
action: action1.id,
|
action: action1.id,
|
||||||
action_ref: action1.r#ref.clone(),
|
action_ref: action1.r#ref.clone(),
|
||||||
trigger: trigger1.id,
|
trigger: trigger1.id,
|
||||||
@@ -1016,7 +1016,7 @@ async fn test_find_rules_by_pack() {
|
|||||||
pack: pack2.id,
|
pack: pack2.id,
|
||||||
pack_ref: pack2.r#ref.clone(),
|
pack_ref: pack2.r#ref.clone(),
|
||||||
label: "Pack2 Rule".to_string(),
|
label: "Pack2 Rule".to_string(),
|
||||||
description: "Pack2".to_string(),
|
description: Some("Pack2".to_string()),
|
||||||
action: action2.id,
|
action: action2.id,
|
||||||
action_ref: action2.r#ref.clone(),
|
action_ref: action2.r#ref.clone(),
|
||||||
trigger: trigger2.id,
|
trigger: trigger2.id,
|
||||||
@@ -1073,7 +1073,7 @@ async fn test_find_rules_by_action() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: format!("Action1 Rule {}", i),
|
label: format!("Action1 Rule {}", i),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action1.id,
|
action: action1.id,
|
||||||
action_ref: action1.r#ref.clone(),
|
action_ref: action1.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -1094,7 +1094,7 @@ async fn test_find_rules_by_action() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Action2 Rule".to_string(),
|
label: "Action2 Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action2.id,
|
action: action2.id,
|
||||||
action_ref: action2.r#ref.clone(),
|
action_ref: action2.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -1155,7 +1155,7 @@ async fn test_find_rules_by_trigger() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: format!("Trigger1 Rule {}", i),
|
label: format!("Trigger1 Rule {}", i),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger1.id,
|
trigger: trigger1.id,
|
||||||
@@ -1176,7 +1176,7 @@ async fn test_find_rules_by_trigger() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Trigger2 Rule".to_string(),
|
label: "Trigger2 Rule".to_string(),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger2.id,
|
trigger: trigger2.id,
|
||||||
@@ -1234,7 +1234,7 @@ async fn test_find_enabled_rules() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: format!("Enabled {}", i),
|
label: format!("Enabled {}", i),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -1256,7 +1256,7 @@ async fn test_find_enabled_rules() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: format!("Disabled {}", i),
|
label: format!("Disabled {}", i),
|
||||||
description: "Test".to_string(),
|
description: Some("Test".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -1312,7 +1312,7 @@ async fn test_cascade_delete_pack_deletes_rules() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Cascade Rule".to_string(),
|
label: "Cascade Rule".to_string(),
|
||||||
description: "Will be cascade deleted".to_string(),
|
description: Some("Will be cascade deleted".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
@@ -1368,7 +1368,7 @@ async fn test_rule_timestamps() {
|
|||||||
pack: pack.id,
|
pack: pack.id,
|
||||||
pack_ref: pack.r#ref.clone(),
|
pack_ref: pack.r#ref.clone(),
|
||||||
label: "Timestamp Rule".to_string(),
|
label: "Timestamp Rule".to_string(),
|
||||||
description: "Test timestamps".to_string(),
|
description: Some("Test timestamps".to_string()),
|
||||||
action: action.id,
|
action: action.id,
|
||||||
action_ref: action.r#ref.clone(),
|
action_ref: action.r#ref.clone(),
|
||||||
trigger: trigger.id,
|
trigger: trigger.id,
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ async fn test_create_sensor_duplicate_ref_fails() {
|
|||||||
pack: Some(pack.id),
|
pack: Some(pack.id),
|
||||||
pack_ref: Some(pack.r#ref.clone()),
|
pack_ref: Some(pack.r#ref.clone()),
|
||||||
label: "Duplicate Sensor".to_string(),
|
label: "Duplicate Sensor".to_string(),
|
||||||
description: "Test sensor".to_string(),
|
description: Some("Test sensor".to_string()),
|
||||||
entrypoint: "sensors/dup.py".to_string(),
|
entrypoint: "sensors/dup.py".to_string(),
|
||||||
runtime: runtime.id,
|
runtime: runtime.id,
|
||||||
runtime_ref: runtime.r#ref.clone(),
|
runtime_ref: runtime.r#ref.clone(),
|
||||||
@@ -235,7 +235,7 @@ async fn test_create_sensor_invalid_ref_format_fails() {
|
|||||||
pack: Some(pack.id),
|
pack: Some(pack.id),
|
||||||
pack_ref: Some(pack.r#ref.clone()),
|
pack_ref: Some(pack.r#ref.clone()),
|
||||||
label: "Invalid Sensor".to_string(),
|
label: "Invalid Sensor".to_string(),
|
||||||
description: "Test sensor".to_string(),
|
description: Some("Test sensor".to_string()),
|
||||||
entrypoint: "sensors/invalid.py".to_string(),
|
entrypoint: "sensors/invalid.py".to_string(),
|
||||||
runtime: runtime.id,
|
runtime: runtime.id,
|
||||||
runtime_ref: runtime.r#ref.clone(),
|
runtime_ref: runtime.r#ref.clone(),
|
||||||
@@ -276,7 +276,7 @@ async fn test_create_sensor_invalid_pack_fails() {
|
|||||||
pack: Some(99999), // Non-existent pack
|
pack: Some(99999), // Non-existent pack
|
||||||
pack_ref: Some("invalid".to_string()),
|
pack_ref: Some("invalid".to_string()),
|
||||||
label: "Invalid Pack Sensor".to_string(),
|
label: "Invalid Pack Sensor".to_string(),
|
||||||
description: "Test sensor".to_string(),
|
description: Some("Test sensor".to_string()),
|
||||||
entrypoint: "sensors/invalid.py".to_string(),
|
entrypoint: "sensors/invalid.py".to_string(),
|
||||||
runtime: runtime.id,
|
runtime: runtime.id,
|
||||||
runtime_ref: runtime.r#ref.clone(),
|
runtime_ref: runtime.r#ref.clone(),
|
||||||
@@ -308,7 +308,7 @@ async fn test_create_sensor_invalid_trigger_fails() {
|
|||||||
pack: None,
|
pack: None,
|
||||||
pack_ref: None,
|
pack_ref: None,
|
||||||
label: "Invalid Trigger Sensor".to_string(),
|
label: "Invalid Trigger Sensor".to_string(),
|
||||||
description: "Test sensor".to_string(),
|
description: Some("Test sensor".to_string()),
|
||||||
entrypoint: "sensors/invalid.py".to_string(),
|
entrypoint: "sensors/invalid.py".to_string(),
|
||||||
runtime: runtime.id,
|
runtime: runtime.id,
|
||||||
runtime_ref: runtime.r#ref.clone(),
|
runtime_ref: runtime.r#ref.clone(),
|
||||||
@@ -340,7 +340,7 @@ async fn test_create_sensor_invalid_runtime_fails() {
|
|||||||
pack: None,
|
pack: None,
|
||||||
pack_ref: None,
|
pack_ref: None,
|
||||||
label: "Invalid Runtime Sensor".to_string(),
|
label: "Invalid Runtime Sensor".to_string(),
|
||||||
description: "Test sensor".to_string(),
|
description: Some("Test sensor".to_string()),
|
||||||
entrypoint: "sensors/invalid.py".to_string(),
|
entrypoint: "sensors/invalid.py".to_string(),
|
||||||
runtime: 99999, // Non-existent runtime
|
runtime: 99999, // Non-existent runtime
|
||||||
runtime_ref: "invalid.runtime".to_string(),
|
runtime_ref: "invalid.runtime".to_string(),
|
||||||
@@ -728,7 +728,7 @@ async fn test_update_description() {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let input = UpdateSensorInput {
|
let input = UpdateSensorInput {
|
||||||
description: Some("New description for the sensor".to_string()),
|
description: Some(Patch::Set("New description for the sensor".to_string())),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -736,7 +736,10 @@ async fn test_update_description() {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(updated.description, "New description for the sensor");
|
assert_eq!(
|
||||||
|
updated.description,
|
||||||
|
Some("New description for the sensor".to_string())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -934,7 +937,7 @@ async fn test_update_multiple_fields() {
|
|||||||
|
|
||||||
let input = UpdateSensorInput {
|
let input = UpdateSensorInput {
|
||||||
label: Some("Multi Update".to_string()),
|
label: Some("Multi Update".to_string()),
|
||||||
description: Some("Updated multiple fields".to_string()),
|
description: Some(Patch::Set("Updated multiple fields".to_string())),
|
||||||
entrypoint: Some("sensors/multi.py".to_string()),
|
entrypoint: Some("sensors/multi.py".to_string()),
|
||||||
enabled: Some(false),
|
enabled: Some(false),
|
||||||
param_schema: Some(Patch::Set(json!({"type": "object"}))),
|
param_schema: Some(Patch::Set(json!({"type": "object"}))),
|
||||||
@@ -946,7 +949,10 @@ async fn test_update_multiple_fields() {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(updated.label, "Multi Update");
|
assert_eq!(updated.label, "Multi Update");
|
||||||
assert_eq!(updated.description, "Updated multiple fields");
|
assert_eq!(
|
||||||
|
updated.description,
|
||||||
|
Some("Updated multiple fields".to_string())
|
||||||
|
);
|
||||||
assert_eq!(updated.entrypoint, "sensors/multi.py");
|
assert_eq!(updated.entrypoint, "sensors/multi.py");
|
||||||
assert!(!updated.enabled);
|
assert!(!updated.enabled);
|
||||||
assert_eq!(updated.param_schema, Some(json!({"type": "object"})));
|
assert_eq!(updated.param_schema, Some(json!({"type": "object"})));
|
||||||
|
|||||||
@@ -182,6 +182,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_decode_valid_token() {
|
fn test_decode_valid_token() {
|
||||||
// Valid JWT with exp and iat claims
|
// Valid JWT with exp and iat claims
|
||||||
|
// nosemgrep: generic.secrets.security.detected-jwt-token.detected-jwt-token -- This is a non-secret test fixture with a dummy signature used only for JWT parsing tests.
|
||||||
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzZW5zb3I6Y29yZS50aW1lciIsImlhdCI6MTcwNjM1NjQ5NiwiZXhwIjoxNzE0MTMyNDk2fQ.signature";
|
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzZW5zb3I6Y29yZS50aW1lciIsImlhdCI6MTcwNjM1NjQ5NiwiZXhwIjoxNzE0MTMyNDk2fQ.signature";
|
||||||
|
|
||||||
let manager = TokenRefreshManager::new(
|
let manager = TokenRefreshManager::new(
|
||||||
|
|||||||
@@ -11,7 +11,10 @@
|
|||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use attune_common::{
|
use attune_common::{
|
||||||
mq::{Consumer, ExecutionCompletedPayload, MessageEnvelope, Publisher},
|
mq::{
|
||||||
|
Consumer, ExecutionCompletedPayload, ExecutionRequestedPayload, MessageEnvelope,
|
||||||
|
MessageType, MqError, Publisher,
|
||||||
|
},
|
||||||
repositories::{execution::ExecutionRepository, FindById},
|
repositories::{execution::ExecutionRepository, FindById},
|
||||||
};
|
};
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
@@ -36,6 +39,19 @@ pub struct CompletionListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl CompletionListener {
|
impl CompletionListener {
|
||||||
|
fn retryable_mq_error(error: &anyhow::Error) -> Option<MqError> {
|
||||||
|
let mq_error = error.downcast_ref::<MqError>()?;
|
||||||
|
Some(match mq_error {
|
||||||
|
MqError::Connection(msg) => MqError::Connection(msg.clone()),
|
||||||
|
MqError::Channel(msg) => MqError::Channel(msg.clone()),
|
||||||
|
MqError::Publish(msg) => MqError::Publish(msg.clone()),
|
||||||
|
MqError::Timeout(msg) => MqError::Timeout(msg.clone()),
|
||||||
|
MqError::Pool(msg) => MqError::Pool(msg.clone()),
|
||||||
|
MqError::Lapin(err) => MqError::Connection(err.to_string()),
|
||||||
|
_ => return None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Create a new completion listener
|
/// Create a new completion listener
|
||||||
pub fn new(
|
pub fn new(
|
||||||
pool: PgPool,
|
pool: PgPool,
|
||||||
@@ -82,6 +98,9 @@ impl CompletionListener {
|
|||||||
{
|
{
|
||||||
error!("Error processing execution completion: {}", e);
|
error!("Error processing execution completion: {}", e);
|
||||||
// Return error to trigger nack with requeue
|
// Return error to trigger nack with requeue
|
||||||
|
if let Some(mq_err) = Self::retryable_mq_error(&e) {
|
||||||
|
return Err(mq_err);
|
||||||
|
}
|
||||||
return Err(
|
return Err(
|
||||||
format!("Failed to process execution completion: {}", e).into()
|
format!("Failed to process execution completion: {}", e).into()
|
||||||
);
|
);
|
||||||
@@ -138,7 +157,11 @@ impl CompletionListener {
|
|||||||
"Failed to advance workflow for execution {}: {}",
|
"Failed to advance workflow for execution {}: {}",
|
||||||
execution_id, e
|
execution_id, e
|
||||||
);
|
);
|
||||||
// Continue processing — don't fail the entire completion
|
if let Some(mq_err) = Self::retryable_mq_error(&e) {
|
||||||
|
return Err(mq_err.into());
|
||||||
|
}
|
||||||
|
// Non-retryable workflow advancement errors are logged but
|
||||||
|
// do not fail the entire completion processing path.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,19 +210,39 @@ impl CompletionListener {
|
|||||||
action_id, execution_id
|
action_id, execution_id
|
||||||
);
|
);
|
||||||
|
|
||||||
match queue_manager.notify_completion(action_id).await {
|
match queue_manager.release_active_slot(execution_id).await {
|
||||||
Ok(notified) => {
|
Ok(release) => {
|
||||||
if notified {
|
if let Some(release) = release {
|
||||||
|
if let Some(next_execution_id) = release.next_execution_id {
|
||||||
info!(
|
info!(
|
||||||
"Queue slot released for action {}, next execution notified",
|
"Queue slot released for action {}, next execution {} can proceed",
|
||||||
action_id
|
action_id, next_execution_id
|
||||||
);
|
);
|
||||||
|
if let Err(republish_err) = Self::publish_execution_requested(
|
||||||
|
pool,
|
||||||
|
publisher,
|
||||||
|
action_id,
|
||||||
|
next_execution_id,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
queue_manager
|
||||||
|
.restore_active_slot(execution_id, &release)
|
||||||
|
.await?;
|
||||||
|
return Err(republish_err);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
debug!(
|
debug!(
|
||||||
"Queue slot released for action {}, no executions waiting",
|
"Queue slot released for action {}, no executions waiting",
|
||||||
action_id
|
action_id
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
debug!(
|
||||||
|
"Execution {} had no active queue slot to release",
|
||||||
|
execution_id
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!(
|
error!(
|
||||||
@@ -225,6 +268,38 @@ impl CompletionListener {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn publish_execution_requested(
|
||||||
|
pool: &PgPool,
|
||||||
|
publisher: &Publisher,
|
||||||
|
action_id: i64,
|
||||||
|
execution_id: i64,
|
||||||
|
) -> Result<()> {
|
||||||
|
let execution = ExecutionRepository::find_by_id(pool, execution_id)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("Execution {} not found", execution_id))?;
|
||||||
|
|
||||||
|
let payload = ExecutionRequestedPayload {
|
||||||
|
execution_id,
|
||||||
|
action_id: Some(action_id),
|
||||||
|
action_ref: execution.action_ref.clone(),
|
||||||
|
parent_id: execution.parent,
|
||||||
|
enforcement_id: execution.enforcement,
|
||||||
|
config: execution.config.clone(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let envelope = MessageEnvelope::new(MessageType::ExecutionRequested, payload)
|
||||||
|
.with_source("executor-completion-listener");
|
||||||
|
|
||||||
|
publisher.publish_envelope(&envelope).await?;
|
||||||
|
|
||||||
|
debug!(
|
||||||
|
"Republished deferred ExecutionRequested for execution {}",
|
||||||
|
execution_id
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -233,13 +308,13 @@ mod tests {
|
|||||||
use crate::queue_manager::ExecutionQueueManager;
|
use crate::queue_manager::ExecutionQueueManager;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_notify_completion_releases_slot() {
|
async fn test_release_active_slot_releases_slot() {
|
||||||
let queue_manager = Arc::new(ExecutionQueueManager::with_defaults());
|
let queue_manager = Arc::new(ExecutionQueueManager::with_defaults());
|
||||||
let action_id = 1;
|
let action_id = 1;
|
||||||
|
|
||||||
// Simulate acquiring a slot
|
// Simulate acquiring a slot
|
||||||
queue_manager
|
queue_manager
|
||||||
.enqueue_and_wait(action_id, 100, 1)
|
.enqueue_and_wait(action_id, 100, 1, None)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -249,8 +324,9 @@ mod tests {
|
|||||||
assert_eq!(stats.queue_length, 0);
|
assert_eq!(stats.queue_length, 0);
|
||||||
|
|
||||||
// Simulate completion notification
|
// Simulate completion notification
|
||||||
let notified = queue_manager.notify_completion(action_id).await.unwrap();
|
let release = queue_manager.release_active_slot(100).await.unwrap();
|
||||||
assert!(!notified); // No one waiting
|
assert!(release.is_some());
|
||||||
|
assert_eq!(release.unwrap().next_execution_id, None);
|
||||||
|
|
||||||
// Verify slot is released
|
// Verify slot is released
|
||||||
let stats = queue_manager.get_queue_stats(action_id).await.unwrap();
|
let stats = queue_manager.get_queue_stats(action_id).await.unwrap();
|
||||||
@@ -258,13 +334,13 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_notify_completion_wakes_waiting() {
|
async fn test_release_active_slot_wakes_waiting() {
|
||||||
let queue_manager = Arc::new(ExecutionQueueManager::with_defaults());
|
let queue_manager = Arc::new(ExecutionQueueManager::with_defaults());
|
||||||
let action_id = 1;
|
let action_id = 1;
|
||||||
|
|
||||||
// Fill capacity
|
// Fill capacity
|
||||||
queue_manager
|
queue_manager
|
||||||
.enqueue_and_wait(action_id, 100, 1)
|
.enqueue_and_wait(action_id, 100, 1, None)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -272,7 +348,7 @@ mod tests {
|
|||||||
let queue_manager_clone = queue_manager.clone();
|
let queue_manager_clone = queue_manager.clone();
|
||||||
let handle = tokio::spawn(async move {
|
let handle = tokio::spawn(async move {
|
||||||
queue_manager_clone
|
queue_manager_clone
|
||||||
.enqueue_and_wait(action_id, 101, 1)
|
.enqueue_and_wait(action_id, 101, 1, None)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
});
|
});
|
||||||
@@ -286,8 +362,8 @@ mod tests {
|
|||||||
assert_eq!(stats.queue_length, 1);
|
assert_eq!(stats.queue_length, 1);
|
||||||
|
|
||||||
// Notify completion
|
// Notify completion
|
||||||
let notified = queue_manager.notify_completion(action_id).await.unwrap();
|
let release = queue_manager.release_active_slot(100).await.unwrap();
|
||||||
assert!(notified); // Should wake the waiting execution
|
assert_eq!(release.unwrap().next_execution_id, Some(101));
|
||||||
|
|
||||||
// Wait for queued execution to proceed
|
// Wait for queued execution to proceed
|
||||||
handle.await.unwrap();
|
handle.await.unwrap();
|
||||||
@@ -306,7 +382,7 @@ mod tests {
|
|||||||
|
|
||||||
// Fill capacity
|
// Fill capacity
|
||||||
queue_manager
|
queue_manager
|
||||||
.enqueue_and_wait(action_id, 100, 1)
|
.enqueue_and_wait(action_id, 100, 1, None)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -320,7 +396,7 @@ mod tests {
|
|||||||
|
|
||||||
let handle = tokio::spawn(async move {
|
let handle = tokio::spawn(async move {
|
||||||
queue_manager
|
queue_manager
|
||||||
.enqueue_and_wait(action_id, exec_id, 1)
|
.enqueue_and_wait(action_id, exec_id, 1, None)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
order.lock().await.push(exec_id);
|
order.lock().await.push(exec_id);
|
||||||
@@ -333,9 +409,13 @@ mod tests {
|
|||||||
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
|
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
|
||||||
|
|
||||||
// Release them one by one
|
// Release them one by one
|
||||||
for _ in 0..3 {
|
for execution_id in 100..103 {
|
||||||
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
|
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
|
||||||
queue_manager.notify_completion(action_id).await.unwrap();
|
let release = queue_manager
|
||||||
|
.release_active_slot(execution_id)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert!(release.is_some());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for all to complete
|
// Wait for all to complete
|
||||||
@@ -351,11 +431,11 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_completion_with_no_queue() {
|
async fn test_completion_with_no_queue() {
|
||||||
let queue_manager = Arc::new(ExecutionQueueManager::with_defaults());
|
let queue_manager = Arc::new(ExecutionQueueManager::with_defaults());
|
||||||
let action_id = 999; // Non-existent action
|
let execution_id = 999; // Non-existent execution
|
||||||
|
|
||||||
// Should succeed but not notify anyone
|
// Should succeed but not notify anyone
|
||||||
let result = queue_manager.notify_completion(action_id).await;
|
let result = queue_manager.release_active_slot(execution_id).await;
|
||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
assert!(!result.unwrap());
|
assert!(result.unwrap().is_none());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ use attune_common::{
|
|||||||
error::Error,
|
error::Error,
|
||||||
models::ExecutionStatus,
|
models::ExecutionStatus,
|
||||||
mq::{Consumer, ConsumerConfig, MessageEnvelope, MessageType, MqResult},
|
mq::{Consumer, ConsumerConfig, MessageEnvelope, MessageType, MqResult},
|
||||||
repositories::{execution::UpdateExecutionInput, ExecutionRepository, FindById, Update},
|
repositories::{execution::UpdateExecutionInput, ExecutionRepository, FindById},
|
||||||
};
|
};
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
@@ -179,13 +179,12 @@ async fn handle_execution_requested(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Only fail if still in a non-terminal state
|
// Only scheduled executions are still legitimately owned by the scheduler.
|
||||||
if !matches!(
|
// If the execution already moved to running or a terminal state, this DLQ
|
||||||
execution.status,
|
// delivery is stale and must not overwrite newer state.
|
||||||
ExecutionStatus::Scheduled | ExecutionStatus::Running
|
if execution.status != ExecutionStatus::Scheduled {
|
||||||
) {
|
|
||||||
info!(
|
info!(
|
||||||
"Execution {} already in terminal state {:?}, skipping",
|
"Execution {} already left Scheduled state ({:?}), skipping stale DLQ handling",
|
||||||
execution_id, execution.status
|
execution_id, execution.status
|
||||||
);
|
);
|
||||||
return Ok(()); // Acknowledge to remove from queue
|
return Ok(()); // Acknowledge to remove from queue
|
||||||
@@ -193,6 +192,12 @@ async fn handle_execution_requested(
|
|||||||
|
|
||||||
// Get worker info from payload for better error message
|
// Get worker info from payload for better error message
|
||||||
let worker_id = envelope.payload.get("worker_id").and_then(|v| v.as_i64());
|
let worker_id = envelope.payload.get("worker_id").and_then(|v| v.as_i64());
|
||||||
|
let scheduled_attempt_updated_at = envelope
|
||||||
|
.payload
|
||||||
|
.get("scheduled_attempt_updated_at")
|
||||||
|
.and_then(|v| v.as_str())
|
||||||
|
.and_then(|s| chrono::DateTime::parse_from_rfc3339(s).ok())
|
||||||
|
.map(|dt| dt.with_timezone(&Utc));
|
||||||
|
|
||||||
let error_message = if let Some(wid) = worker_id {
|
let error_message = if let Some(wid) = worker_id {
|
||||||
format!(
|
format!(
|
||||||
@@ -214,26 +219,87 @@ async fn handle_execution_requested(
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
match ExecutionRepository::update(pool, execution_id, update_input).await {
|
if let Some(timestamp) = scheduled_attempt_updated_at {
|
||||||
Ok(_) => {
|
// Guard on both status and the exact updated_at from when the execution was
|
||||||
|
// scheduled — prevents overwriting state that changed after this DLQ message
|
||||||
|
// was enqueued.
|
||||||
|
match ExecutionRepository::update_if_status_and_updated_at(
|
||||||
|
pool,
|
||||||
|
execution_id,
|
||||||
|
ExecutionStatus::Scheduled,
|
||||||
|
timestamp,
|
||||||
|
update_input,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(Some(_)) => {
|
||||||
info!(
|
info!(
|
||||||
"Successfully failed execution {} due to worker queue expiration",
|
"Successfully failed execution {} due to worker queue expiration",
|
||||||
execution_id
|
execution_id
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Ok(None) => {
|
||||||
|
info!(
|
||||||
|
"Skipping DLQ failure for execution {} because it already left Scheduled state",
|
||||||
|
execution_id
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!(
|
error!(
|
||||||
"Failed to update execution {} to failed state: {}",
|
"Failed to update execution {} to failed state: {}",
|
||||||
execution_id, e
|
execution_id, e
|
||||||
);
|
);
|
||||||
// Return error to nack and potentially retry
|
|
||||||
Err(attune_common::mq::MqError::Consume(format!(
|
Err(attune_common::mq::MqError::Consume(format!(
|
||||||
"Failed to update execution: {}",
|
"Failed to update execution: {}",
|
||||||
e
|
e
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Fallback for DLQ messages that predate the scheduled_attempt_updated_at
|
||||||
|
// field. Use a status-only guard — same safety guarantee as the original code
|
||||||
|
// (never overwrites terminal or running state).
|
||||||
|
warn!(
|
||||||
|
"DLQ message for execution {} lacks scheduled_attempt_updated_at; \
|
||||||
|
falling back to status-only guard",
|
||||||
|
execution_id
|
||||||
|
);
|
||||||
|
match ExecutionRepository::update_if_status(
|
||||||
|
pool,
|
||||||
|
execution_id,
|
||||||
|
ExecutionStatus::Scheduled,
|
||||||
|
update_input,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(Some(_)) => {
|
||||||
|
info!(
|
||||||
|
"Successfully failed execution {} due to worker queue expiration (status-only guard)",
|
||||||
|
execution_id
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Ok(None) => {
|
||||||
|
info!(
|
||||||
|
"Skipping DLQ failure for execution {} because it already left Scheduled state",
|
||||||
|
execution_id
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!(
|
||||||
|
"Failed to update execution {} to failed state: {}",
|
||||||
|
execution_id, e
|
||||||
|
);
|
||||||
|
Err(attune_common::mq::MqError::Consume(format!(
|
||||||
|
"Failed to update execution: {}",
|
||||||
|
e
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a dead letter consumer configuration
|
/// Create a dead letter consumer configuration
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ use attune_common::{
|
|||||||
event::{EnforcementRepository, EventRepository, UpdateEnforcementInput},
|
event::{EnforcementRepository, EventRepository, UpdateEnforcementInput},
|
||||||
execution::{CreateExecutionInput, ExecutionRepository},
|
execution::{CreateExecutionInput, ExecutionRepository},
|
||||||
rule::RuleRepository,
|
rule::RuleRepository,
|
||||||
Create, FindById, Update,
|
FindById,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -116,6 +116,14 @@ impl EnforcementProcessor {
|
|||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| anyhow::anyhow!("Enforcement not found: {}", enforcement_id))?;
|
.ok_or_else(|| anyhow::anyhow!("Enforcement not found: {}", enforcement_id))?;
|
||||||
|
|
||||||
|
if enforcement.status != EnforcementStatus::Created {
|
||||||
|
debug!(
|
||||||
|
"Enforcement {} already left Created state ({:?}), skipping duplicate processing",
|
||||||
|
enforcement_id, enforcement.status
|
||||||
|
);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
// Fetch associated rule
|
// Fetch associated rule
|
||||||
let rule = RuleRepository::find_by_id(
|
let rule = RuleRepository::find_by_id(
|
||||||
pool,
|
pool,
|
||||||
@@ -135,7 +143,7 @@ impl EnforcementProcessor {
|
|||||||
|
|
||||||
// Evaluate whether to create execution
|
// Evaluate whether to create execution
|
||||||
if Self::should_create_execution(&enforcement, &rule, event.as_ref())? {
|
if Self::should_create_execution(&enforcement, &rule, event.as_ref())? {
|
||||||
Self::create_execution(
|
let execution_created = Self::create_execution(
|
||||||
pool,
|
pool,
|
||||||
publisher,
|
publisher,
|
||||||
policy_enforcer,
|
policy_enforcer,
|
||||||
@@ -145,10 +153,10 @@ impl EnforcementProcessor {
|
|||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Update enforcement status to Processed after successful execution creation
|
let updated = EnforcementRepository::update_loaded_if_status(
|
||||||
EnforcementRepository::update(
|
|
||||||
pool,
|
pool,
|
||||||
enforcement_id,
|
&enforcement,
|
||||||
|
EnforcementStatus::Created,
|
||||||
UpdateEnforcementInput {
|
UpdateEnforcementInput {
|
||||||
status: Some(EnforcementStatus::Processed),
|
status: Some(EnforcementStatus::Processed),
|
||||||
payload: None,
|
payload: None,
|
||||||
@@ -157,17 +165,27 @@ impl EnforcementProcessor {
|
|||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
debug!("Updated enforcement {} status to Processed", enforcement_id);
|
if updated.is_some() {
|
||||||
|
debug!(
|
||||||
|
"Updated enforcement {} status to Processed after {} execution path",
|
||||||
|
enforcement_id,
|
||||||
|
if execution_created {
|
||||||
|
"new"
|
||||||
|
} else {
|
||||||
|
"idempotent"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
info!(
|
info!(
|
||||||
"Skipping execution creation for enforcement: {}",
|
"Skipping execution creation for enforcement: {}",
|
||||||
enforcement_id
|
enforcement_id
|
||||||
);
|
);
|
||||||
|
|
||||||
// Update enforcement status to Disabled since it was not actionable
|
let updated = EnforcementRepository::update_loaded_if_status(
|
||||||
EnforcementRepository::update(
|
|
||||||
pool,
|
pool,
|
||||||
enforcement_id,
|
&enforcement,
|
||||||
|
EnforcementStatus::Created,
|
||||||
UpdateEnforcementInput {
|
UpdateEnforcementInput {
|
||||||
status: Some(EnforcementStatus::Disabled),
|
status: Some(EnforcementStatus::Disabled),
|
||||||
payload: None,
|
payload: None,
|
||||||
@@ -176,11 +194,13 @@ impl EnforcementProcessor {
|
|||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
if updated.is_some() {
|
||||||
debug!(
|
debug!(
|
||||||
"Updated enforcement {} status to Disabled (skipped)",
|
"Updated enforcement {} status to Disabled (skipped)",
|
||||||
enforcement_id
|
enforcement_id
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -230,11 +250,11 @@ impl EnforcementProcessor {
|
|||||||
async fn create_execution(
|
async fn create_execution(
|
||||||
pool: &PgPool,
|
pool: &PgPool,
|
||||||
publisher: &Publisher,
|
publisher: &Publisher,
|
||||||
policy_enforcer: &PolicyEnforcer,
|
_policy_enforcer: &PolicyEnforcer,
|
||||||
_queue_manager: &ExecutionQueueManager,
|
_queue_manager: &ExecutionQueueManager,
|
||||||
enforcement: &Enforcement,
|
enforcement: &Enforcement,
|
||||||
rule: &Rule,
|
rule: &Rule,
|
||||||
) -> Result<()> {
|
) -> Result<bool> {
|
||||||
// Extract action ID — should_create_execution already verified it's Some,
|
// Extract action ID — should_create_execution already verified it's Some,
|
||||||
// but guard defensively here as well.
|
// but guard defensively here as well.
|
||||||
let action_id = match rule.action {
|
let action_id = match rule.action {
|
||||||
@@ -257,33 +277,10 @@ impl EnforcementProcessor {
|
|||||||
enforcement.id, rule.id, action_id
|
enforcement.id, rule.id, action_id
|
||||||
);
|
);
|
||||||
|
|
||||||
let pack_id = rule.pack;
|
|
||||||
let action_ref = &rule.action_ref;
|
let action_ref = &rule.action_ref;
|
||||||
|
|
||||||
// Enforce policies and wait for queue slot if needed
|
// Create the execution row first; scheduler-side policy enforcement
|
||||||
info!(
|
// now handles both rule-triggered and manual executions uniformly.
|
||||||
"Enforcing policies for action {} (enforcement: {})",
|
|
||||||
action_id, enforcement.id
|
|
||||||
);
|
|
||||||
|
|
||||||
// Use enforcement ID for queue tracking (execution doesn't exist yet)
|
|
||||||
if let Err(e) = policy_enforcer
|
|
||||||
.enforce_and_wait(action_id, Some(pack_id), enforcement.id)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
error!(
|
|
||||||
"Policy enforcement failed for enforcement {}: {}",
|
|
||||||
enforcement.id, e
|
|
||||||
);
|
|
||||||
return Err(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
info!(
|
|
||||||
"Policy check passed and queue slot obtained for enforcement: {}",
|
|
||||||
enforcement.id
|
|
||||||
);
|
|
||||||
|
|
||||||
// Now create execution in database (we have a queue slot)
|
|
||||||
let execution_input = CreateExecutionInput {
|
let execution_input = CreateExecutionInput {
|
||||||
action: Some(action_id),
|
action: Some(action_id),
|
||||||
action_ref: action_ref.clone(),
|
action_ref: action_ref.clone(),
|
||||||
@@ -298,21 +295,36 @@ impl EnforcementProcessor {
|
|||||||
workflow_task: None, // Non-workflow execution
|
workflow_task: None, // Non-workflow execution
|
||||||
};
|
};
|
||||||
|
|
||||||
let execution = ExecutionRepository::create(pool, execution_input).await?;
|
let execution_result = ExecutionRepository::create_top_level_for_enforcement_if_absent(
|
||||||
|
pool,
|
||||||
|
execution_input,
|
||||||
|
enforcement.id,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
let execution = execution_result.execution;
|
||||||
|
|
||||||
|
if execution_result.created {
|
||||||
info!(
|
info!(
|
||||||
"Created execution: {} for enforcement: {}",
|
"Created execution: {} for enforcement: {}",
|
||||||
execution.id, enforcement.id
|
execution.id, enforcement.id
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
info!(
|
||||||
|
"Reusing execution: {} for enforcement: {}",
|
||||||
|
execution.id, enforcement.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Publish ExecutionRequested message
|
if execution_result.created
|
||||||
|
|| execution.status == attune_common::models::enums::ExecutionStatus::Requested
|
||||||
|
{
|
||||||
let payload = ExecutionRequestedPayload {
|
let payload = ExecutionRequestedPayload {
|
||||||
execution_id: execution.id,
|
execution_id: execution.id,
|
||||||
action_id: Some(action_id),
|
action_id: Some(action_id),
|
||||||
action_ref: action_ref.clone(),
|
action_ref: action_ref.clone(),
|
||||||
parent_id: None,
|
parent_id: None,
|
||||||
enforcement_id: Some(enforcement.id),
|
enforcement_id: Some(enforcement.id),
|
||||||
config: enforcement.config.clone(),
|
config: execution.config.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let envelope =
|
let envelope =
|
||||||
@@ -331,11 +343,12 @@ impl EnforcementProcessor {
|
|||||||
"Published execution.requested message for execution: {} (enforcement: {}, action: {})",
|
"Published execution.requested message for execution: {} (enforcement: {}, action: {})",
|
||||||
execution.id, enforcement.id, action_id
|
execution.id, enforcement.id, action_id
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// NOTE: Queue slot will be released when worker publishes execution.completed
|
// NOTE: Queue slot will be released when worker publishes execution.completed
|
||||||
// and CompletionListener calls queue_manager.notify_completion(action_id)
|
// and CompletionListener calls queue_manager.notify_completion(action_id)
|
||||||
|
|
||||||
Ok(())
|
Ok(execution_result.created)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -368,7 +381,7 @@ mod tests {
|
|||||||
pack: 1,
|
pack: 1,
|
||||||
pack_ref: "test".to_string(),
|
pack_ref: "test".to_string(),
|
||||||
label: "Test Rule".to_string(),
|
label: "Test Rule".to_string(),
|
||||||
description: "Test rule description".to_string(),
|
description: Some("Test rule description".to_string()),
|
||||||
trigger_ref: "test.trigger".to_string(),
|
trigger_ref: "test.trigger".to_string(),
|
||||||
trigger: Some(1),
|
trigger: Some(1),
|
||||||
action_ref: "test.action".to_string(),
|
action_ref: "test.action".to_string(),
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ use attune_common::{
|
|||||||
event::{CreateEnforcementInput, EnforcementRepository, EventRepository},
|
event::{CreateEnforcementInput, EnforcementRepository, EventRepository},
|
||||||
pack::PackRepository,
|
pack::PackRepository,
|
||||||
rule::RuleRepository,
|
rule::RuleRepository,
|
||||||
Create, FindById, List,
|
FindById, List,
|
||||||
},
|
},
|
||||||
template_resolver::{resolve_templates, TemplateContext},
|
template_resolver::{resolve_templates, TemplateContext},
|
||||||
};
|
};
|
||||||
@@ -206,14 +206,23 @@ impl EventProcessor {
|
|||||||
conditions: rule.conditions.clone(),
|
conditions: rule.conditions.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let enforcement = EnforcementRepository::create(pool, create_input).await?;
|
let enforcement_result =
|
||||||
|
EnforcementRepository::create_or_get_by_rule_event(pool, create_input).await?;
|
||||||
|
let enforcement = enforcement_result.enforcement;
|
||||||
|
|
||||||
|
if enforcement_result.created {
|
||||||
info!(
|
info!(
|
||||||
"Enforcement {} created for rule {} (event: {})",
|
"Enforcement {} created for rule {} (event: {})",
|
||||||
enforcement.id, rule.r#ref, event.id
|
enforcement.id, rule.r#ref, event.id
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
info!(
|
||||||
|
"Reusing enforcement {} for rule {} (event: {})",
|
||||||
|
enforcement.id, rule.r#ref, event.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Publish EnforcementCreated message
|
if enforcement_result.created || enforcement.status == EnforcementStatus::Created {
|
||||||
let enforcement_payload = EnforcementCreatedPayload {
|
let enforcement_payload = EnforcementCreatedPayload {
|
||||||
enforcement_id: enforcement.id,
|
enforcement_id: enforcement.id,
|
||||||
rule_id: Some(rule.id),
|
rule_id: Some(rule.id),
|
||||||
@@ -223,7 +232,8 @@ impl EventProcessor {
|
|||||||
payload: payload.clone(),
|
payload: payload.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let envelope = MessageEnvelope::new(MessageType::EnforcementCreated, enforcement_payload)
|
let envelope =
|
||||||
|
MessageEnvelope::new(MessageType::EnforcementCreated, enforcement_payload)
|
||||||
.with_source("event-processor");
|
.with_source("event-processor");
|
||||||
|
|
||||||
publisher.publish_envelope(&envelope).await?;
|
publisher.publish_envelope(&envelope).await?;
|
||||||
@@ -232,6 +242,7 @@ impl EventProcessor {
|
|||||||
"Published EnforcementCreated message for enforcement {}",
|
"Published EnforcementCreated message for enforcement {}",
|
||||||
enforcement.id
|
enforcement.id
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user