ЛР 7 — БВТ2303 — Пластинин — на 5 #5
@@ -3,6 +3,7 @@
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
.runtime
|
||||
secrets
|
||||
certs
|
||||
volumes
|
||||
@@ -17,4 +18,5 @@ backups
|
||||
__pycache__
|
||||
node_modules
|
||||
evidence
|
||||
/messenger
|
||||
.DS_Store
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [main]
|
||||
push:
|
||||
branches: [main]
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: ci-${{ github.repository }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
verify:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 60
|
||||
env:
|
||||
COMPOSE_PROJECT_NAME: mtusi2026lab7-${{ github.run_id }}
|
||||
API_BIND_ADDRESS: 127.0.0.1:0
|
||||
SWAGGER_BIND_ADDRESS: 127.0.0.1:0
|
||||
PROMETHEUS_BIND_ADDRESS: 127.0.0.1:0
|
||||
GRAFANA_BIND_ADDRESS: 127.0.0.1:0
|
||||
LOKI_BIND_ADDRESS: 127.0.0.1:0
|
||||
TEMPO_BIND_ADDRESS: 127.0.0.1:0
|
||||
OPENBAO_BIND_ADDRESS: 127.0.0.1:0
|
||||
BAO_ADDR: https://openbao:8200
|
||||
DOCKER_WORKSPACE_MIRROR: "1"
|
||||
CI_WORKSPACE: /tmp/mtusi2026lab7-${{ github.run_id }}
|
||||
steps:
|
||||
- name: Checkout exact commit
|
||||
run: |
|
||||
git init .
|
||||
git remote add origin https://gitea.privcloud.tech/${{ github.repository }}.git
|
||||
git fetch --depth=1 origin ${{ github.sha }}
|
||||
git checkout --detach FETCH_HEAD
|
||||
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: "1.26.10"
|
||||
cache: false
|
||||
|
||||
- name: Check contract
|
||||
run: make check
|
||||
|
||||
- name: Validate OpenAPI
|
||||
run: |
|
||||
python3 -m venv .venv
|
||||
.venv/bin/python -m pip install -r requirements-dev.txt
|
||||
make validate PYTHON=.venv/bin/python
|
||||
|
||||
- name: Check formatting and shell scripts
|
||||
run: |
|
||||
test -z "$(gofmt -l cmd internal migrations)"
|
||||
for file in infra/minio/*.sh infra/pki/*.sh infra/postgres/*.sh infra/redis/*.sh infra/security/*.sh infra/openbao/*.sh scripts/*.sh; do sh -n "$file"; done
|
||||
python3 -m py_compile scripts/lab6-traffic.py scripts/lab6-observe.py scripts/lab6-alert.py scripts/lab6-correlation.py scripts/validate-lab6-evidence.py scripts/test-login-rate-limit.py
|
||||
|
||||
- name: Lint
|
||||
uses: golangci/golangci-lint-action@v7
|
||||
with:
|
||||
version: v2.13.2
|
||||
args: --timeout=5m
|
||||
skip-cache: true
|
||||
|
||||
- name: Unit tests and build
|
||||
run: |
|
||||
go test -race ./...
|
||||
go build ./cmd/...
|
||||
|
||||
- name: Prepare local stack
|
||||
run: |
|
||||
mkdir -p "$CI_WORKSPACE"
|
||||
git archive HEAD | tar -x -C "$CI_WORKSPACE"
|
||||
cd "$CI_WORKSPACE"
|
||||
cp lab7.env.example .env
|
||||
docker compose config --quiet
|
||||
./infra/pki/bootstrap.sh
|
||||
./scripts/openbao-seed.sh .env
|
||||
./scripts/sync-docker-workspace.sh
|
||||
docker compose up -d security-bootstrap openbao-permissions openbao
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_secrets" "$HOSTNAME"
|
||||
./scripts/bootstrap-local.sh .env
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME"
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME"
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_data_backend" "$HOSTNAME"
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_monitoring" "$HOSTNAME"
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_secure_clients" "$HOSTNAME"
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_telemetry_storage" "$HOSTNAME"
|
||||
|
||||
- name: Integration checks
|
||||
run: |
|
||||
cd "$CI_WORKSPACE"
|
||||
test "$(curl --noproxy '*' --cacert .runtime/security/internal-ca/ca.crt -s -o /dev/null -w '%{http_code}' "https://s3:9000/messenger-attachments")" = "403"
|
||||
make test BASE_URL=https://nginx:8443 CA_FILE=.runtime/security/public-ca/ca.crt
|
||||
make test-security BASE_URL=https://nginx:8443 CA_FILE=.runtime/security/public-ca/ca.crt
|
||||
make test-tls-matrix ENV_FILE=.env
|
||||
ENV_FILE=.env BASE_URL=https://nginx:8443 ./scripts/test-leaf-rotation.sh
|
||||
docker compose --profile security run --rm dependency-tests
|
||||
ENV_FILE=.env BAO_ADDR=https://openbao:8200 BAO_CACERT="$PWD/.runtime/security/openbao/ca.crt" ./scripts/openbao-test-policy.sh
|
||||
ENV_FILE=.env BAO_ADDR=https://openbao:8200 BAO_CACERT="$PWD/.runtime/security/openbao/ca.crt" ./scripts/test-openbao-lifecycle.sh
|
||||
make test-login-rate-limit BASE_URL=https://nginx:8443 CA_FILE=.runtime/security/public-ca/ca.crt
|
||||
docker compose --profile tests run --rm --build tests
|
||||
go run ./cmd/dependencycheck -base-url=https://nginx:8443 -ca-file=.runtime/security/public-ca/ca.crt
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=https://nginx:8443 CA_FILE=.runtime/security/public-ca/ca.crt
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL=https://nginx:8443 CA_FILE=.runtime/security/public-ca/ca.crt
|
||||
docker compose up -d --scale api=2 --wait --wait-timeout 90
|
||||
make test-lab5-acceptance
|
||||
make test-worker-compromise ENV_FILE=.env
|
||||
make scan-security
|
||||
ENV_FILE=.env CA_FILE=.runtime/security/public-ca/ca.crt PROMETHEUS_URL=http://prometheus:9090 TEMPO_URL=http://tempo:3200 LOKI_URL=http://loki:3100 make test-lab6-smoke BASE_URL=https://nginx:8443
|
||||
mkdir -p evidence/runtime
|
||||
api_restart_counts=$(docker inspect --format '{{.RestartCount}}' $(docker compose ps -q api))
|
||||
go run ./cmd/loginburst -base-url=https://nginx:8443 -ca-file=.runtime/security/public-ca/ca.crt > evidence/runtime/login-burst.json
|
||||
test "$(docker inspect --format '{{.RestartCount}}' $(docker compose ps -q api))" = "$api_restart_counts"
|
||||
|
||||
- name: Container logs
|
||||
if: failure()
|
||||
run: if [ -f "$CI_WORKSPACE/.env" ]; then cd "$CI_WORKSPACE"; docker compose logs --no-color --tail=200; fi
|
||||
|
||||
- name: Upload security evidence
|
||||
if: always()
|
||||
uses: actions/upload-artifact@v3
|
||||
env:
|
||||
ACTIONS_RUNTIME_URL: https://gitea.privcloud.tech/api/actions_pipeline/
|
||||
with:
|
||||
name: lab7-security-${{ github.sha }}
|
||||
path: |
|
||||
${{ env.CI_WORKSPACE }}/evidence/lab7-*.json
|
||||
${{ env.CI_WORKSPACE }}/evidence/runtime/security-scan
|
||||
${{ env.CI_WORKSPACE }}/evidence/runtime/login-burst.json
|
||||
if-no-files-found: warn
|
||||
retention-days: 14
|
||||
|
||||
- name: Stop local stack
|
||||
if: always()
|
||||
run: |
|
||||
test -d "$CI_WORKSPACE" || exit 0
|
||||
cd "$CI_WORKSPACE"
|
||||
trap './scripts/sync-docker-workspace.sh --clean' EXIT
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_secrets" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_data_backend" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_monitoring" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_secure_clients" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_telemetry_storage" "$HOSTNAME" 2>/dev/null || true
|
||||
if [ -f .env ]; then docker compose --profile tests down -v --remove-orphans; fi
|
||||
@@ -0,0 +1,231 @@
|
||||
name: Deploy production
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: production
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
verify:
|
||||
name: Verify selected commit
|
||||
if: github.ref == 'refs/heads/main'
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 60
|
||||
outputs:
|
||||
api_image: ${{ steps.publish.outputs.api_image }}
|
||||
nginx_image: ${{ steps.publish.outputs.nginx_image }}
|
||||
swagger_image: ${{ steps.publish.outputs.swagger_image }}
|
||||
prometheus_image: ${{ steps.publish.outputs.prometheus_image }}
|
||||
grafana_image: ${{ steps.publish.outputs.grafana_image }}
|
||||
s3_image: ${{ steps.publish.outputs.s3_image }}
|
||||
s3_bootstrap_image: ${{ steps.publish.outputs.s3_bootstrap_image }}
|
||||
env:
|
||||
COMPOSE_PROJECT_NAME: mtusi2026lab7-deploy-${{ github.run_id }}
|
||||
COMPOSE_FILE: compose.yaml:deploy/compose.images.yaml
|
||||
API_BIND_ADDRESS: 127.0.0.1:0
|
||||
SWAGGER_BIND_ADDRESS: 127.0.0.1:0
|
||||
PROMETHEUS_BIND_ADDRESS: 127.0.0.1:0
|
||||
GRAFANA_BIND_ADDRESS: 127.0.0.1:0
|
||||
LOKI_BIND_ADDRESS: 127.0.0.1:0
|
||||
TEMPO_BIND_ADDRESS: 127.0.0.1:0
|
||||
OPENBAO_BIND_ADDRESS: 127.0.0.1:0
|
||||
BAO_ADDR: https://openbao:8200
|
||||
DOCKER_WORKSPACE_MIRROR: "1"
|
||||
CI_WORKSPACE: /tmp/mtusi2026lab7-${{ github.run_id }}
|
||||
API_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab7-api:${{ github.sha }}
|
||||
NGINX_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab7-nginx:${{ github.sha }}
|
||||
SWAGGER_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab7-swagger:${{ github.sha }}
|
||||
PROMETHEUS_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab7-prometheus:${{ github.sha }}
|
||||
GRAFANA_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab7-grafana:${{ github.sha }}
|
||||
S3_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab7-minio:${{ github.sha }}
|
||||
S3_BOOTSTRAP_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab7-minio-client:${{ github.sha }}
|
||||
steps:
|
||||
- name: Checkout exact commit
|
||||
run: |
|
||||
git init .
|
||||
git remote add origin https://gitea.privcloud.tech/${{ github.repository }}.git
|
||||
git fetch --depth=1 origin ${{ github.sha }}
|
||||
git checkout --detach FETCH_HEAD
|
||||
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: "1.26.10"
|
||||
cache: false
|
||||
|
||||
- name: Check contract
|
||||
run: make check
|
||||
|
||||
- name: Validate OpenAPI
|
||||
run: |
|
||||
python3 -m venv .venv
|
||||
.venv/bin/python -m pip install -r requirements-dev.txt
|
||||
make validate PYTHON=.venv/bin/python
|
||||
|
||||
- name: Check formatting and shell scripts
|
||||
run: |
|
||||
test -z "$(gofmt -l cmd internal migrations)"
|
||||
for file in infra/minio/*.sh infra/pki/*.sh infra/postgres/*.sh infra/redis/*.sh infra/security/*.sh infra/openbao/*.sh scripts/*.sh; do sh -n "$file"; done
|
||||
python3 -m py_compile scripts/lab6-traffic.py scripts/lab6-observe.py scripts/lab6-alert.py scripts/lab6-correlation.py scripts/validate-lab6-evidence.py scripts/test-login-rate-limit.py
|
||||
|
||||
- name: Lint
|
||||
uses: golangci/golangci-lint-action@v7
|
||||
with:
|
||||
version: v2.13.2
|
||||
args: --timeout=5m
|
||||
skip-cache: true
|
||||
|
||||
- name: Unit tests and build
|
||||
run: |
|
||||
go test -race ./...
|
||||
go build ./cmd/...
|
||||
|
||||
- name: Prepare local stack
|
||||
run: |
|
||||
mkdir -p "$CI_WORKSPACE"
|
||||
git archive HEAD | tar -x -C "$CI_WORKSPACE"
|
||||
cd "$CI_WORKSPACE"
|
||||
cp lab7.env.example .env
|
||||
docker compose config --quiet
|
||||
./infra/pki/bootstrap.sh
|
||||
./scripts/openbao-seed.sh .env
|
||||
./scripts/sync-docker-workspace.sh
|
||||
docker compose up -d security-bootstrap openbao-permissions openbao
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_secrets" "$HOSTNAME"
|
||||
./scripts/bootstrap-local.sh .env
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME"
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME"
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_data_backend" "$HOSTNAME"
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_monitoring" "$HOSTNAME"
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_secure_clients" "$HOSTNAME"
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_telemetry_storage" "$HOSTNAME"
|
||||
|
||||
- name: Integration checks
|
||||
run: |
|
||||
cd "$CI_WORKSPACE"
|
||||
test "$(curl --noproxy '*' --cacert .runtime/security/internal-ca/ca.crt -s -o /dev/null -w '%{http_code}' "https://s3:9000/messenger-attachments")" = "403"
|
||||
make test BASE_URL=https://nginx:8443 CA_FILE=.runtime/security/public-ca/ca.crt
|
||||
make test-security BASE_URL=https://nginx:8443 CA_FILE=.runtime/security/public-ca/ca.crt
|
||||
make test-tls-matrix ENV_FILE=.env
|
||||
ENV_FILE=.env BASE_URL=https://nginx:8443 ./scripts/test-leaf-rotation.sh
|
||||
docker compose --profile security run --rm dependency-tests
|
||||
ENV_FILE=.env BAO_ADDR=https://openbao:8200 BAO_CACERT="$PWD/.runtime/security/openbao/ca.crt" ./scripts/openbao-test-policy.sh
|
||||
ENV_FILE=.env BAO_ADDR=https://openbao:8200 BAO_CACERT="$PWD/.runtime/security/openbao/ca.crt" ./scripts/test-openbao-lifecycle.sh
|
||||
make test-login-rate-limit BASE_URL=https://nginx:8443 CA_FILE=.runtime/security/public-ca/ca.crt
|
||||
docker compose --profile tests run --rm --build tests
|
||||
go run ./cmd/dependencycheck -base-url=https://nginx:8443 -ca-file=.runtime/security/public-ca/ca.crt
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=https://nginx:8443 CA_FILE=.runtime/security/public-ca/ca.crt
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL=https://nginx:8443 CA_FILE=.runtime/security/public-ca/ca.crt
|
||||
make test-lab5-acceptance
|
||||
make test-worker-compromise ENV_FILE=.env
|
||||
ENV_FILE=.env CA_FILE=.runtime/security/public-ca/ca.crt PROMETHEUS_URL=http://prometheus:9090 TEMPO_URL=http://tempo:3200 LOKI_URL=http://loki:3100 make test-lab6-smoke BASE_URL=https://nginx:8443
|
||||
mkdir -p evidence/runtime
|
||||
api_restart_counts=$(docker inspect --format '{{.RestartCount}}' $(docker compose ps -q api))
|
||||
go run ./cmd/loginburst -base-url=https://nginx:8443 -ca-file=.runtime/security/public-ca/ca.crt > evidence/runtime/login-burst.json
|
||||
test "$(docker inspect --format '{{.RestartCount}}' $(docker compose ps -q api))" = "$api_restart_counts"
|
||||
|
||||
- name: Publish verified images
|
||||
id: publish
|
||||
env:
|
||||
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
|
||||
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
|
||||
run: |
|
||||
printf '%s' "$REGISTRY_PASSWORD" | docker login gitea.privcloud.tech --username "$REGISTRY_USERNAME" --password-stdin
|
||||
|
||||
publish() {
|
||||
output=$1
|
||||
image=$2
|
||||
push_output=$(docker push "$image")
|
||||
printf '%s\n' "$push_output"
|
||||
digest=$(printf '%s\n' "$push_output" | sed -n 's/^.*digest: \(sha256:[0-9a-f]*\).*$/\1/p' | tail -n 1)
|
||||
test -n "$digest"
|
||||
repository=${image%:*}
|
||||
printf '%s=%s@%s\n' "$output" "$repository" "$digest" >> "$GITHUB_OUTPUT"
|
||||
}
|
||||
|
||||
publish api_image "$API_IMAGE"
|
||||
publish nginx_image "$NGINX_IMAGE"
|
||||
publish swagger_image "$SWAGGER_IMAGE"
|
||||
publish prometheus_image "$PROMETHEUS_IMAGE"
|
||||
publish grafana_image "$GRAFANA_IMAGE"
|
||||
publish s3_image "$S3_IMAGE"
|
||||
publish s3_bootstrap_image "$S3_BOOTSTRAP_IMAGE"
|
||||
|
||||
- name: Container logs
|
||||
if: failure()
|
||||
run: if [ -f "$CI_WORKSPACE/.env" ]; then cd "$CI_WORKSPACE"; docker compose logs --no-color --tail=200; fi
|
||||
|
||||
- name: Stop local stack
|
||||
if: always()
|
||||
run: |
|
||||
test -d "$CI_WORKSPACE" || exit 0
|
||||
cd "$CI_WORKSPACE"
|
||||
trap './scripts/sync-docker-workspace.sh --clean' EXIT
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_secrets" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_data_backend" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_monitoring" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_secure_clients" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_telemetry_storage" "$HOSTNAME" 2>/dev/null || true
|
||||
if [ -f .env ]; then docker compose --profile tests down -v --remove-orphans; fi
|
||||
|
||||
deploy:
|
||||
name: Deploy selected commit
|
||||
needs: verify
|
||||
runs-on: ubuntu-latest
|
||||
environment: production
|
||||
env:
|
||||
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
|
||||
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
|
||||
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
|
||||
DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
|
||||
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
|
||||
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
|
||||
DEPLOY_SHA: ${{ github.sha }}
|
||||
steps:
|
||||
- name: Configure SSH
|
||||
run: |
|
||||
umask 077
|
||||
mkdir -p ~/.ssh
|
||||
printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/id_ed25519
|
||||
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > ~/.ssh/known_hosts
|
||||
|
||||
- name: Prepare immutable image manifest
|
||||
run: |
|
||||
cat > deploy-images.env <<EOF
|
||||
API_IMAGE=${{ needs.verify.outputs.api_image }}
|
||||
NGINX_IMAGE=${{ needs.verify.outputs.nginx_image }}
|
||||
SWAGGER_IMAGE=${{ needs.verify.outputs.swagger_image }}
|
||||
PROMETHEUS_IMAGE=${{ needs.verify.outputs.prometheus_image }}
|
||||
GRAFANA_IMAGE=${{ needs.verify.outputs.grafana_image }}
|
||||
S3_IMAGE=${{ needs.verify.outputs.s3_image }}
|
||||
S3_BOOTSTRAP_IMAGE=${{ needs.verify.outputs.s3_bootstrap_image }}
|
||||
EOF
|
||||
|
||||
- name: Authenticate registry on deployment host
|
||||
run: printf '%s' "$REGISTRY_PASSWORD" | ssh "$DEPLOY_USER@$DEPLOY_HOST" "docker login gitea.privcloud.tech --username '$REGISTRY_USERNAME' --password-stdin"
|
||||
|
||||
- name: Upload image manifest
|
||||
run: scp deploy-images.env "$DEPLOY_USER@$DEPLOY_HOST:/tmp/mtusi2026lab7-${{ github.run_id }}.env"
|
||||
|
||||
- name: Deploy and verify
|
||||
run: |
|
||||
ssh "$DEPLOY_USER@$DEPLOY_HOST" "
|
||||
set -eu
|
||||
cd ~/mtusi2026lab7
|
||||
git fetch origin main
|
||||
git cat-file -e '$DEPLOY_SHA^{commit}'
|
||||
git reset --hard '$DEPLOY_SHA'
|
||||
manifest='/tmp/mtusi2026lab7-${{ github.run_id }}.env'
|
||||
if DEPLOY_GENERATION='${{ github.run_id }}' scripts/rolling-update.sh \"\$manifest\"; then
|
||||
rm -f \"\$manifest\"
|
||||
else
|
||||
status=\$?
|
||||
rm -f \"\$manifest\"
|
||||
exit \"\$status\"
|
||||
fi
|
||||
test \"\$(git rev-parse HEAD)\" = '$DEPLOY_SHA'
|
||||
docker compose ps
|
||||
"
|
||||
@@ -0,0 +1,99 @@
|
||||
name: Lab 7 inherited incidents
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: lab7-incidents-${{ github.repository }}-${{ github.ref }}
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
incidents:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 35
|
||||
env:
|
||||
COMPOSE_PROJECT_NAME: mtusi2026lab7-incidents-${{ github.run_id }}
|
||||
API_BIND_ADDRESS: 127.0.0.1:0
|
||||
SWAGGER_BIND_ADDRESS: 127.0.0.1:0
|
||||
PROMETHEUS_BIND_ADDRESS: 127.0.0.1:0
|
||||
GRAFANA_BIND_ADDRESS: 127.0.0.1:0
|
||||
LOKI_BIND_ADDRESS: 127.0.0.1:0
|
||||
TEMPO_BIND_ADDRESS: 127.0.0.1:0
|
||||
OPENBAO_BIND_ADDRESS: 127.0.0.1:0
|
||||
BAO_ADDR: https://openbao:8200
|
||||
DOCKER_WORKSPACE_MIRROR: "1"
|
||||
CI_WORKSPACE: /tmp/mtusi2026lab7-${{ github.run_id }}
|
||||
steps:
|
||||
- name: Checkout exact commit
|
||||
run: |
|
||||
git init .
|
||||
git remote add origin https://gitea.privcloud.tech/${{ github.repository }}.git
|
||||
git fetch --depth=1 origin ${{ github.sha }}
|
||||
git checkout --detach FETCH_HEAD
|
||||
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: "1.26.10"
|
||||
cache: false
|
||||
|
||||
- name: Prepare stack
|
||||
run: |
|
||||
mkdir -p "$CI_WORKSPACE"
|
||||
git archive HEAD | tar -x -C "$CI_WORKSPACE"
|
||||
cd "$CI_WORKSPACE"
|
||||
cp lab7.env.example .env
|
||||
docker compose config --quiet
|
||||
./infra/pki/bootstrap.sh
|
||||
./scripts/openbao-seed.sh .env
|
||||
./scripts/sync-docker-workspace.sh
|
||||
docker compose up -d security-bootstrap openbao-permissions openbao
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_secrets" "$HOSTNAME"
|
||||
./scripts/bootstrap-local.sh .env
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME"
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME"
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_data_backend" "$HOSTNAME"
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_monitoring" "$HOSTNAME"
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_secure_clients" "$HOSTNAME"
|
||||
docker network connect "${COMPOSE_PROJECT_NAME}_telemetry_storage" "$HOSTNAME"
|
||||
|
||||
- name: Reproduce SLO incidents
|
||||
run: |
|
||||
cd "$CI_WORKSPACE"
|
||||
ENV_FILE=.env \
|
||||
BASE_URL=https://nginx:8443 \
|
||||
CA_FILE=.runtime/security/public-ca/ca.crt \
|
||||
PROMETHEUS_URL=http://prometheus:9090 \
|
||||
TEMPO_URL=http://tempo:3200 \
|
||||
LOKI_URL=http://loki:3100 \
|
||||
EVIDENCE_DIR=evidence/runtime/ci \
|
||||
make test-lab6
|
||||
|
||||
- name: Upload sanitized evidence
|
||||
if: always()
|
||||
uses: actions/upload-artifact@v3
|
||||
env:
|
||||
ACTIONS_RUNTIME_URL: https://gitea.privcloud.tech/api/actions_pipeline/
|
||||
with:
|
||||
name: lab6-incidents-${{ github.sha }}
|
||||
path: ${{ env.CI_WORKSPACE }}/evidence/runtime/ci
|
||||
if-no-files-found: warn
|
||||
retention-days: 14
|
||||
|
||||
- name: Container logs
|
||||
if: failure()
|
||||
run: if [ -f "$CI_WORKSPACE/.env" ]; then cd "$CI_WORKSPACE"; docker compose logs --no-color --tail=200; fi
|
||||
|
||||
- name: Stop local stack
|
||||
if: always()
|
||||
run: |
|
||||
test -d "$CI_WORKSPACE" || exit 0
|
||||
cd "$CI_WORKSPACE"
|
||||
trap './scripts/sync-docker-workspace.sh --clean' EXIT
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_secrets" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_data_backend" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_monitoring" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_secure_clients" "$HOSTNAME" 2>/dev/null || true
|
||||
docker network disconnect "${COMPOSE_PROJECT_NAME}_telemetry_storage" "$HOSTNAME" 2>/dev/null || true
|
||||
docker compose down -v --remove-orphans
|
||||
@@ -2,6 +2,7 @@
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
.runtime/
|
||||
secrets/
|
||||
certs/private/
|
||||
*.key
|
||||
@@ -30,3 +31,5 @@ coverage/
|
||||
.vscode/
|
||||
# Keep small, sanitized evidence summaries in evidence/ under version control.
|
||||
# Inspect git diff --cached: ignore rules are not a secret scanner.
|
||||
|
||||
evidence/runtime/
|
||||
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
FROM alpine:3.24.2 AS security-test
|
||||
|
||||
RUN apk add --no-cache curl openssl
|
||||
WORKDIR /src
|
||||
COPY scripts/test-tls-matrix.sh /src/scripts/test-tls-matrix.sh
|
||||
CMD ["/bin/sh", "/src/scripts/test-tls-matrix.sh"]
|
||||
|
||||
FROM golang:1.26.10-alpine3.24 AS build
|
||||
|
||||
ARG GOPROXY=https://proxy.golang.org
|
||||
|
||||
WORKDIR /src
|
||||
ENV GOMAXPROCS=2 GOMEMLIMIT=1GiB
|
||||
RUN apk add --no-cache git
|
||||
COPY go.mod go.sum ./
|
||||
RUN GOPROXY="$GOPROXY" go mod download
|
||||
COPY . .
|
||||
RUN go build -p=1 -o /out/messenger ./cmd/messenger
|
||||
RUN go build -p=1 -o /out/lab5experiment ./cmd/lab5experiment
|
||||
RUN go build -p=1 -o /out/mtlsproxy ./cmd/mtlsproxy
|
||||
|
||||
FROM build AS test
|
||||
|
||||
CMD ["go", "test", "./internal/postgres", "./internal/session"]
|
||||
|
||||
FROM swaggerapi/swagger-ui:v5.31.2 AS swagger
|
||||
|
||||
COPY contracts/openapi.json /spec/openapi.json
|
||||
ENV SWAGGER_JSON=/spec/openapi.json
|
||||
|
||||
FROM prom/prometheus:v3.13.3 AS prometheus
|
||||
|
||||
COPY monitoring/prometheus.yml /etc/prometheus/prometheus.yml
|
||||
COPY monitoring/rules /etc/prometheus/rules
|
||||
|
||||
FROM grafana/grafana:13.2.4 AS grafana
|
||||
|
||||
COPY monitoring/grafana/provisioning /etc/grafana/provisioning
|
||||
COPY monitoring/grafana/dashboards /etc/grafana/dashboards
|
||||
|
||||
FROM nginx:1.30.6-alpine AS loadbalancer
|
||||
|
||||
RUN apk upgrade --no-cache
|
||||
|
||||
COPY nginx/nginx.conf /etc/nginx/nginx.conf
|
||||
COPY nginx/upstream.conf /etc/nginx/conf.d/upstream.conf
|
||||
|
||||
FROM alpine:3.24.2
|
||||
|
||||
RUN apk upgrade --no-cache && addgroup -S app && adduser -S -G app -u 10001 app
|
||||
COPY --from=build --chown=app:app /out/messenger /app/messenger
|
||||
COPY --from=build --chown=app:app /out/lab5experiment /app/lab5experiment
|
||||
COPY --from=build --chown=app:app /out/mtlsproxy /app/mtlsproxy
|
||||
|
||||
USER app
|
||||
WORKDIR /app
|
||||
HEALTHCHECK --interval=10s --timeout=2s --start-period=5s --retries=3 \
|
||||
CMD wget -q -O /dev/null "http://127.0.0.1:8082/health/ready" || exit 1
|
||||
CMD ["/app/messenger"]
|
||||
@@ -2,9 +2,10 @@ PYTHON ?= python3
|
||||
BASE_URL ?= https://localhost:8443
|
||||
CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
ENV_FILE ?= $(if $(wildcard .env),.env,lab7.env.example)
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover test-security
|
||||
.PHONY: help check validate test load test-persistence test-failover test-security test-tls-matrix test-leaf-rotation test-openbao-lifecycle test-login-rate-limit test-worker-compromise scan-security test-lab5 test-lab5-acceptance lab6-load test-lab6-smoke test-lab6
|
||||
|
||||
help:
|
||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
||||
@@ -31,3 +32,37 @@ test-failover:
|
||||
|
||||
test-security:
|
||||
$(PYTHON) tests/security.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
test-tls-matrix:
|
||||
docker compose --env-file "$(ENV_FILE)" --profile security run --rm security-tests
|
||||
|
||||
test-leaf-rotation:
|
||||
ENV_FILE="$(ENV_FILE)" BASE_URL="$(BASE_URL)" ./scripts/test-leaf-rotation.sh
|
||||
|
||||
test-openbao-lifecycle:
|
||||
ENV_FILE="$(ENV_FILE)" ./scripts/test-openbao-lifecycle.sh
|
||||
|
||||
test-login-rate-limit:
|
||||
@test -n "$(CA_FILE)" || (echo "Set CA_FILE"; exit 2)
|
||||
$(PYTHON) scripts/test-login-rate-limit.py --base-url "$(BASE_URL)" --ca-file "$(CA_FILE)"
|
||||
|
||||
test-worker-compromise:
|
||||
ENV_FILE="$(ENV_FILE)" ./scripts/lab7-worker-compromise.sh
|
||||
|
||||
scan-security:
|
||||
./scripts/scan-security.sh
|
||||
|
||||
test-lab5:
|
||||
./scripts/test-lab5.sh
|
||||
|
||||
test-lab5-acceptance:
|
||||
./scripts/test-lab5-acceptance.sh
|
||||
|
||||
lab6-load:
|
||||
$(PYTHON) scripts/lab6-traffic.py --base-url "$(BASE_URL)" $(TLS_ARGS) --mode mixed --duration 60 --concurrency 4 --wait-ready
|
||||
|
||||
test-lab6-smoke:
|
||||
ENV_FILE="$(ENV_FILE)" BASE_URL="$(BASE_URL)" CA_FILE="$(CA_FILE)" ./scripts/test-lab6.sh smoke
|
||||
|
||||
test-lab6:
|
||||
ENV_FILE="$(ENV_FILE)" BASE_URL="$(BASE_URL)" CA_FILE="$(CA_FILE)" ./scripts/test-lab6.sh full
|
||||
|
||||
@@ -1,37 +1,186 @@
|
||||
# Отчёт — лабораторная 7
|
||||
# Отчёт — лабораторная работа № 7
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
**Автор:** Пластинин Денис, БВТ2303
|
||||
|
||||
**Целевая оценка:** 5
|
||||
|
||||
**Выбранный трек:** 1 — компрометация worker
|
||||
|
||||
## Цель работы
|
||||
|
||||
Цель работы — провести границы доверия в ранее реализованном мессенджере, включить проверяемое TLS/mTLS взаимодействие, разделить права сервисов и вынести runtime secrets в OpenBao. За основу взята лабораторная работа № 6: сохранены API, обработка вложений через Redis Streams, PostgreSQL, MinIO и стек наблюдаемости.
|
||||
|
||||
Внешний клиент подключается только к Nginx по HTTPS. Nginx обращается к двум API-репликам по mTLS, причём API принимает только сертификат с разрешённым URI SAN балансировщика. Внутренние зависимости используют TLS, отдельные service identities и минимальные ACL. Наличие сертификата, подписанного доверенным CA, само по себе не даёт доступ: identity дополнительно сопоставляется с разрешённой ролью.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Требуются Docker с Compose v2, `make`, Python 3 и `curl`. Все используемые образы закреплены по версиям в `compose.yaml`. Локальные ключи и operator material создаются в игнорируемой директории `.runtime`.
|
||||
|
||||
## Архитектура и решения
|
||||
```sh
|
||||
cp lab7.env.example .env
|
||||
./scripts/bootstrap-local.sh .env
|
||||
```
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab6, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
Скрипт создаёт локальную PKI, запускает OpenBao с Raft storage, при первом запуске выполняет `init`/`unseal`, создаёт policies и AppRole, затем поднимает стенд с двумя API-репликами. Повторный запуск использует сохранённое состояние OpenBao и выполняет только необходимый bootstrap.
|
||||
|
||||
Основные интерфейсы:
|
||||
|
||||
- API и Swagger через HTTPS: `https://localhost:8443` и `http://localhost:8081`;
|
||||
- Grafana: `https://localhost:3000`;
|
||||
- Prometheus: `http://localhost:9090`;
|
||||
- OpenBao operator endpoint: `https://localhost:8200`.
|
||||
|
||||
Публичный CA создаётся в `.runtime/security/public-ca/ca.crt`. Проверка API:
|
||||
|
||||
```sh
|
||||
make test BASE_URL=https://localhost:8443 CA_FILE="$PWD/.runtime/security/public-ca/ca.crt"
|
||||
make test-security BASE_URL=https://localhost:8443 CA_FILE="$PWD/.runtime/security/public-ca/ca.crt"
|
||||
```
|
||||
|
||||
Безопасная остановка сохраняет volumes:
|
||||
|
||||
```sh
|
||||
docker compose --env-file .env stop
|
||||
```
|
||||
|
||||
Намеренное удаление учебного стенда и его данных:
|
||||
|
||||
```sh
|
||||
docker compose --env-file .env down --volumes --remove-orphans
|
||||
rm -rf .runtime
|
||||
```
|
||||
|
||||
Root token и unseal key находятся только в `.runtime/openbao/operator/init.json` с правами `0600`. Их хранит оператор; приложения и CI runtime их не получают. В production этот файл должен находиться во внешнем защищённом хранилище, а unseal shares должны быть разделены между операторами.
|
||||
|
||||
## Архитектура
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
C[Client] -->|HTTPS + Bearer| N[Nginx]
|
||||
N -->|mTLS| A1[API 1]
|
||||
N -->|mTLS| A2[API 2]
|
||||
A1 & A2 -->|TLS + api role| P[(PostgreSQL)]
|
||||
A1 & A2 -->|mTLS + ACL| V[(Valkey)]
|
||||
A1 & A2 -->|HTTPS + staging policy| S[(MinIO)]
|
||||
D[Dispatcher] -->|mTLS producer| R[(Redis Streams)]
|
||||
R -->|mTLS consumer| W[Worker]
|
||||
W -->|HTTPS + attachment policy| S
|
||||
A1 & A2 & D & W -->|mTLS| O[OTel gateway]
|
||||
BA[OpenBao Agents] -->|TLS + AppRole| B[(OpenBao Raft)]
|
||||
BA -->|secret file 0600| A1 & A2 & W
|
||||
```
|
||||
|
||||
PostgreSQL остаётся источником истины для пользователей, чатов, сообщений, metadata вложений, outbox и состояния заданий. MinIO хранит содержимое вложений. Redis Streams переносит задания с гарантией at-least-once, а Valkey хранит session token hashes и общие счётчики login rate limit. Дедупликация и переходы состояний выполняются транзакционно в PostgreSQL.
|
||||
|
||||
OpenBao работает в non-dev режиме на persistent Raft storage. API и worker имеют разные AppRole и KV policies. Agent аутентифицируется короткоживущим SecretID, обновляет renewable token и рендерит JSON secret в защищённый Docker volume; приложение получает путь через `MESSENGER_SECRETS_FILE`. Token Agent хранится в tmpfs, root token не используется приложением. SecretID действует один час и позволяет повторную аутентификацию после рестарта; затем оператор выдаёт новый.
|
||||
|
||||
PostgreSQL использует `verify-full` и отдельные роли API, dispatcher, worker, reconciler, exporter и migrator. Valkey и Redis Streams защищены native TLS, ACL и Envoy gateway, который проверяет точный URI SAN клиента. MinIO использует HTTPS и отдельные scoped users. OTel ingest и metrics endpoints требуют mTLS с allowlist identities. Полная схема связей приведена в `contracts/trust-matrix.md`.
|
||||
|
||||
Контейнеры приложения запускаются non-root, с `read_only`, `no-new-privileges`, `cap_drop: ALL`, отдельным `tmpfs` и лимитами CPU/RAM. Stateful компоненты сохраняют данные в named volumes; их исключения из `read_only` ограничены каталогами данных.
|
||||
|
||||
## Пароли, сессии и защита login
|
||||
|
||||
Пароли хешируются Argon2id с индивидуальной солью и сохранёнными параметрами. При успешном входе `NeedsRehash` сравнивает параметры хеша с текущей политикой; устаревший хеш обновляется compare-and-swap запросом, поэтому параллельный login не перезаписывает более новый пароль.
|
||||
|
||||
Параметры `m=65536 KiB`, `t=3`, `p=2` измерены в Linux-контейнере с лимитом 2 CPU и 512 MiB. Пять операций дали около 60,0 мс на hash и 64,8 мс на verify при примерно 64 MiB памяти на операцию. Частотный лимит ограничивает login, а общий semaphore внутри Hasher допускает не более двух `Hash`/`Verify` на API-реплику, включая регистрацию и rehash. Поэтому одновременно занятая Argon2 память ограничена примерно 128 MiB на реплику; лишние операции сразу получают `429 Retry-After: 1`. Лимит задаётся `server.password.max_concurrent_operations`. Raw summary сохранён в `evidence/lab7-argon2-budget.json`.
|
||||
|
||||
`cmd/loginburst` создаёт одновременные волны по 20 регистраций и login-запросов через Nginx, проверяет ответы `429` с положительным `Retry-After`, отсутствие `5xx` и readiness после нагрузки. CI дополнительно проверяет, что API containers не перезапускались. `X-Instance-Id` — случайный alias на время жизни процесса; hostname и IP в него не входят.
|
||||
|
||||
Session token является случайным opaque bearer token. В Valkey сохраняется только его SHA-256 hash с TTL; logout удаляет запись. Login limiter общий для API-реплик: Lua script атомарно увеличивает счётчики по нормализованному login и адресу источника, выставляет TTL и возвращает время до повторной попытки. При превышении лимита API отвечает `429` и положительным `Retry-After`.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| Критерий | Команда/сценарий | Наблюдаемый результат | Доказательство |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
| Контракт и OpenAPI | `make check`, `make validate` | структура и OpenAPI 3.1 проходят проверку | CI `verify` |
|
||||
| Unit и integration | `go test ./...`, PostgreSQL/Valkey integration tests | тесты проходят по TLS connections | CI и test output |
|
||||
| Публичный API через HTTPS | `make test ... CA_FILE=...` | полный smoke проходит через Nginx | public smoke output |
|
||||
| Недоверенный внешний TLS | `make test-security` | правильный CA проходит, пустой/чужой CA отклонён | security test output |
|
||||
| LB→API mTLS | `make test-tls-matrix` | 13 положительных и отрицательных cases проходят | TLS matrix output |
|
||||
| DB/S3/broker/OTel least privilege | `infra/security/test-dependency-matrix.sh` | разрешённые операции проходят, cross-role операции отклонены | dependency matrix output |
|
||||
| OpenBao scoped policy | `scripts/openbao-test-policy.sh` | own read `200`, cross/write/policy `403`, token renew `200` | policy test output |
|
||||
| OpenBao expiry/revoke/seal | `make test-openbao-lifecycle` | revoked/expired token `403`, sealed login `503`, Agent восстановил аутентификацию после unseal | lifecycle test output |
|
||||
| Ротация leaf и отзыв identity | `make test-leaf-rotation` | новая LB identity проходит, прежняя same-CA identity и expired leaf отклонены на новых connections | rotation test output |
|
||||
| Общий login limiter | `make test-login-rate-limit ...` | запросы через обе replicas завершаются `429` с `Retry-After` | rate-limit test output |
|
||||
| Rehash Argon2id | `go test ./internal/password ./internal/postgres ./internal/messenger/httpapi` | проверены `NeedsRehash` и конкурентное CAS update | unit/integration tests |
|
||||
| Argon2/login DoS budget | `go test ./internal/password -run '^$' -bench 'BenchmarkArgon2id(Hash\|Verify)$' -benchtime=5x -benchmem` | hash 60,0 мс, verify 64,8 мс, около 64 MiB/op при 2 CPU | `evidence/lab7-argon2-budget.json` |
|
||||
| Dependency/image/security scan | `make scan-security` | secrets не найдены; исправляемые high/critical Go CVE обновлены | `evidence/lab7-security-scan.json` |
|
||||
| Компрометация worker | `make test-worker-compromise` | старый broker credential отклонён, после rotation queued attachment обработан без потери | `evidence/lab7-worker-compromise.json` |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
## Эксперимент: компрометация worker
|
||||
|
||||
## Эксперименты и отказы
|
||||
### Гипотеза
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Если worker credential будет скомпрометирован, злоумышленник получит только consumer operations своего stream, ограниченные таблицы PostgreSQL и два MinIO prefix. После смены broker password и worker secret старая identity перестанет работать, а уже поставленное задание сохранится и завершится после восстановления worker.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
### Методика
|
||||
|
||||
Стенд запускался в контейнерах с лимитом worker `2 CPU / 1 GiB`. Скрипт `scripts/lab7-worker-compromise.sh` останавливает worker, создаёт реальное задание загрузки через API, меняет Redis ACL password, не обновляя secret работающего worker, и проверяет отказ старой identity. Затем он записывает новый secret в OpenBao, пересоздаёт OpenBao Agent и worker и проверяет созданное вложение через API. В `trap` всегда восстанавливается исходный учебный secret.
|
||||
|
||||
Отдельная dependency matrix моделирует возможности скомпрометированного worker: ему разрешены чтение/изменение attachment job state, consumer/ack операции stream и работа с `staging/`/`attachments/`; запрещены таблица `users`, unrelated Redis streams и произвольный S3 prefix.
|
||||
|
||||
### Изменение blast radius относительно lab6
|
||||
|
||||
| Ресурс | До разделения прав в lab6 | После least privilege в lab7 |
|
||||
| --- | --- | --- |
|
||||
| PostgreSQL | API, dispatcher, worker и reconciler получали общий `DATABASE_URL`; worker мог использовать права основной роли на пользователей, чаты и сообщения | `messenger_worker` читает/обновляет только attachments/jobs/variants, создаёт audit/outbox; чтение `users` запрещено |
|
||||
| MinIO | один `S3_ACCESS_KEY` имел `Get/Put/Delete` на весь bucket | worker видит только `staging/*` и `attachments/*`; `private/*` и управление users/policies запрещены |
|
||||
| Redis Streams | dispatcher и worker использовали общий `BROKER_URL` | dispatcher может только `XADD` в `attachment-jobs`; worker может consumer/ack/recovery команды только для job/DLQ streams |
|
||||
| TLS identity | внутренние зависимости принимали сетевое соединение без service URI проверки | gateway сопоставляет worker certificate с `spiffe://mtusi/lab7/worker`; same-CA чужая identity отклоняется |
|
||||
| Secrets | все static credentials находились в общем `.env` процесса | worker получает отдельный JSON через scoped OpenBao AppRole; API policy не читает worker path |
|
||||
|
||||
Таким образом, до разделения кража worker environment давала общий доступ к данным приложения и всему bucket. После разделения она ограничена обработкой вложений. Ротация в эксперименте меняет broker, PostgreSQL и MinIO credentials внутри worker secret; старые credentials не открывают новое соединение даже при наличии прежнего leaf certificate.
|
||||
|
||||
### Результат
|
||||
|
||||
| Воздействие | Ожидание | Наблюдение | Потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
| Старый broker password после ротации | authentication error | worker получил `WRONGPASS/invalid username-password` | нет |
|
||||
| Cross-role DB/S3/stream operations | ACL denial | все отрицательные cases dependency matrix отклонены | нет |
|
||||
| Обновление worker secret в OpenBao | Agent рендерит новый secret, worker становится healthy | от начала сценария до проверки вложения — 21 секунда; возврат исходных паролей измеряется отдельно | нет |
|
||||
| Проверка задания, созданного до отзыва | вложение обработано после восстановления | API verification прошла | queued attachment сохранён |
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
Эксперимент доказывает ограничение blast radius и восстановление задания, но не моделирует кражу root-доступа к Docker host. Сценарий разрывает broker connections перезапуском gateway и завершает PostgreSQL sessions роли worker через `pg_terminate_backend`; затем проверяет новые подключения со старыми паролями и отказ старому S3 key. Для production потребуются внешний KMS/HSM для CA, response-wrapped SecretID, автоматизированный revoke и несколько независимых повторов с измерением времени восстановления.
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
## Scan triage
|
||||
|
||||
`scripts/scan-security.sh` запускает Trivy filesystem scan и image scan всех образов работающего Compose-стенда. Raw JSON сохраняется в игнорируемом `evidence/runtime/security-scan` и CI artifact, компактная сводка — в `evidence/lab7-security-scan.json`. Найденные у приложения CVE устранены обновлением OpenTelemetry, gRPC, `x/net`, `klauspost/compress`, Go toolchain и Nginx. Весь стенд не объявляется свободным от CVE: сканирование сторонних образов находит отдельные уязвимые компоненты. Secrets в repository/images не найдены.
|
||||
|
||||
| Finding / класс | Применимость и решение |
|
||||
| --- | --- |
|
||||
| `CVE-2026-81871`, `CVE-2026-81872`, `CVE-2026-81870` в OTLP | Затрагивают используемые приложением exporters/log SDK. Обновлены до OTLP `1.45.0` / log `0.21.0`; отключение TLS verification не используется |
|
||||
| `CVE-2026-33322`, `CVE-2026-33419` в MinIO | Требуют OIDC/LDAP auth, которые в стенде не настроены; используется локальная scoped access-key auth. Образ не опубликован наружу. Обновление MinIO остаётся задачей перед production |
|
||||
| `CVE-2026-77405`, `CVE-2026-77408`, `CVE-2026-77411` в AMQP library сторонних images | AMQP receivers/exporters и MinIO AMQP notifications не настроены; брокер стенда — Redis Streams. Пакет присутствует в бинарнике, но этот путь не используется конфигурацией |
|
||||
| `CVE-2026-31789` в OpenSSL | Advisory относится к 32-bit обработке X.509; измеренный стенд `linux/arm64`. Не экстраполируем это исключение на остальные CVE OpenSSL |
|
||||
| `CVE-2026-111497` (Brotli), `CVE-2026-48930` (Node.js) в UI images | Grafana/Swagger доступны только на loopback; приложение Go не использует эти пакеты. Достижимость уязвимых UI-путей не доказана; фиксированные upstream images нужны перед внешней публикацией |
|
||||
| `CVE-2025-68121` и остальные stdlib/OS findings сторонних images | Собственный API пересобран patched Go. Для сторонних бинарников пересборка не выполнялась; это остаточный риск, который не снимается одной сетевой изоляцией. Ограничиваем доступ и ресурсы, сохраняем raw findings для обновления images |
|
||||
| `UNKNOWN`, например `CVE-2026-39824` в Windows API | Severity не определена источником. Windows-only путь не выполняется в Linux; остальные UNKNOWN требуют отдельной проверки, а не считаются автоматически безопасными |
|
||||
|
||||
Данный scan triage подтверждает применимость конкретных ограничений учебного стенда. Он не заменяет обновление сторонних образов и не является разрешением на production deployment.
|
||||
|
||||
Замечания к отсутствию `HEALTHCHECK` внутри отдельных toolbox/bootstrap images не дают обхода runtime health: Compose задаёт healthchecks долговечным сервисам, а bootstrap containers одноразовые и должны завершиться с кодом 0. Это остаётся осознанным ограничением, а не исключением проверки приложения.
|
||||
|
||||
## Ограничения
|
||||
|
||||
- PKI локальная и не содержит CRL/OCSP. Отзыв leaf выполняется удалением доверия/сменой allowlist и пересозданием соединений.
|
||||
- PostgreSQL, Valkey, Redis, MinIO и OpenBao запущены по одному экземпляру; работа проверяет безопасность границ, а не HA этих хранилищ.
|
||||
- Operator init/unseal ручной. Это допустимо для учебного стенда, но production требует auto-unseal через KMS/HSM и раздельного хранения shares.
|
||||
- Static S3/DB/broker credentials выдаются через OpenBao KV и ротируются оператором. Dynamic database credentials были бы следующим улучшением.
|
||||
- Rate limiter хранится в Valkey и при его отказе блокирует login; уже выданные сессии также зависят от Valkey.
|
||||
|
||||
## Источники
|
||||
|
||||
- [OWASP Password Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html)
|
||||
- [OWASP File Upload Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/File_Upload_Cheat_Sheet.html)
|
||||
- [OpenBao AppRole](https://openbao.org/docs/auth/approle/)
|
||||
- [OpenBao policies](https://openbao.org/docs/concepts/policies/)
|
||||
- [OpenBao production hardening](https://openbao.org/docs/internals/security/)
|
||||
- [PostgreSQL SSL support](https://www.postgresql.org/docs/current/libpq-ssl.html)
|
||||
- [Redis TLS](https://redis.io/docs/latest/operate/oss_and_stack/management/security/encryption/)
|
||||
- [Redis ACL](https://redis.io/docs/latest/operate/oss_and_stack/management/security/acl/)
|
||||
- [MinIO policy based access control](https://min.io/docs/minio/linux/administration/identity-access-management/policy-based-access-control.html)
|
||||
- [OpenTelemetry Collector security](https://opentelemetry.io/docs/security/config-best-practices/)
|
||||
- [Docker Engine security](https://docs.docker.com/engine/security/)
|
||||
- [Trivy filesystem scanning](https://trivy.dev/latest/docs/target/filesystem/)
|
||||
|
||||
OpenAI Codex использовался для подготовки и ревью реализации; результаты проверены тестами, CI-сценариями и воспроизводимыми отрицательными экспериментами.
|
||||
|
||||
@@ -0,0 +1,349 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type apiClient struct {
|
||||
baseURL string
|
||||
client *http.Client
|
||||
transport *http.Transport
|
||||
token string
|
||||
userID string
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("PASS: replica idempotency and dependency recovery")
|
||||
}
|
||||
|
||||
func run() error {
|
||||
baseURL := flag.String("base-url", "https://localhost:8443", "API base URL")
|
||||
caFile := flag.String("ca-file", ".runtime/security/public-ca/ca.crt", "trusted CA certificate")
|
||||
flag.Parse()
|
||||
caPEM, err := os.ReadFile(*caFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read trusted CA: %w", err)
|
||||
}
|
||||
roots := x509.NewCertPool()
|
||||
if !roots.AppendCertsFromPEM(caPEM) {
|
||||
return fmt.Errorf("trusted CA does not contain a certificate")
|
||||
}
|
||||
transport := &http.Transport{TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12, RootCAs: roots}}
|
||||
defer transport.CloseIdleConnections()
|
||||
client := &apiClient{
|
||||
baseURL: *baseURL, client: &http.Client{Transport: transport, Timeout: 10 * time.Second},
|
||||
transport: transport,
|
||||
}
|
||||
if err := client.registerAndLogin(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := client.expect(http.MethodGet, "/api/v1/users/me", http.StatusOK, true); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := client.verifyConcurrentIdempotency(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := verifyOutage(client, "valkey"); err != nil {
|
||||
return err
|
||||
}
|
||||
return verifyOutage(client, "postgres")
|
||||
}
|
||||
|
||||
func (c *apiClient) registerAndLogin() error {
|
||||
username := "dep_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:16]
|
||||
password := "T3st-" + uuid.NewString()
|
||||
body := map[string]string{"username": username, "display_name": "Dependency check", "password": password}
|
||||
payload, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request, err := http.NewRequest(http.MethodPost, c.baseURL+"/api/v1/users", bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
response, err := c.client.Do(request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("register: %w", err)
|
||||
}
|
||||
registrationBody := response.Body
|
||||
defer func() { _ = registrationBody.Close() }()
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
return fmt.Errorf("register status = %d", response.StatusCode)
|
||||
}
|
||||
var user struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&user); err != nil {
|
||||
return fmt.Errorf("decode user: %w", err)
|
||||
}
|
||||
if user.ID == "" {
|
||||
return fmt.Errorf("register returned an empty user id")
|
||||
}
|
||||
c.userID = user.ID
|
||||
|
||||
request, err = http.NewRequest(http.MethodPost, c.baseURL+"/api/v1/auth/sessions", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request.SetBasicAuth(username, password)
|
||||
response, err = c.client.Do(request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("login: %w", err)
|
||||
}
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
return fmt.Errorf("login status = %d", response.StatusCode)
|
||||
}
|
||||
var session struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&session); err != nil {
|
||||
return fmt.Errorf("decode session: %w", err)
|
||||
}
|
||||
if session.Token == "" {
|
||||
return fmt.Errorf("login returned an empty token")
|
||||
}
|
||||
c.token = session.Token
|
||||
return nil
|
||||
}
|
||||
|
||||
type messageResult struct {
|
||||
messageID string
|
||||
instanceID string
|
||||
err error
|
||||
}
|
||||
|
||||
func (c *apiClient) verifyConcurrentIdempotency() error {
|
||||
chatID, err := c.createChat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
const workers = 16
|
||||
var (
|
||||
key = uuid.NewString()
|
||||
start = make(chan struct{})
|
||||
results = make(chan messageResult, workers)
|
||||
group sync.WaitGroup
|
||||
instances = make(map[string]struct{})
|
||||
messageID string
|
||||
)
|
||||
transport := c.transport.Clone()
|
||||
transport.DisableKeepAlives = true
|
||||
transport.MaxConnsPerHost = workers
|
||||
client := &http.Client{Transport: transport, Timeout: 10 * time.Second}
|
||||
defer transport.CloseIdleConnections()
|
||||
|
||||
for range workers {
|
||||
group.Add(1)
|
||||
go func() {
|
||||
defer group.Done()
|
||||
<-start
|
||||
id, instance, requestErr := c.createMessage(client, chatID, key, "concurrent message")
|
||||
results <- messageResult{messageID: id, instanceID: instance, err: requestErr}
|
||||
}()
|
||||
}
|
||||
close(start)
|
||||
group.Wait()
|
||||
close(results)
|
||||
|
||||
for result := range results {
|
||||
if result.err != nil {
|
||||
return result.err
|
||||
}
|
||||
if result.instanceID == "" {
|
||||
return fmt.Errorf("message response has an empty X-Instance-Id")
|
||||
}
|
||||
instances[result.instanceID] = struct{}{}
|
||||
if messageID == "" {
|
||||
messageID = result.messageID
|
||||
}
|
||||
if result.messageID != messageID {
|
||||
return fmt.Errorf("idempotent responses returned different message ids")
|
||||
}
|
||||
}
|
||||
if len(instances) < 2 {
|
||||
return fmt.Errorf("requests reached %d API instance, want at least 2", len(instances))
|
||||
}
|
||||
return c.verifyMessageHistory(chatID, messageID)
|
||||
}
|
||||
|
||||
func (c *apiClient) createChat() (string, error) {
|
||||
payload, err := json.Marshal(map[string]any{
|
||||
"title": "Replica check", "member_ids": []string{c.userID},
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
request, err := http.NewRequest(http.MethodPost, c.baseURL+"/api/v1/chats", bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
request.Header.Set("Authorization", "Bearer "+c.token)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
response, err := c.client.Do(request)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create chat: %w", err)
|
||||
}
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
return "", fmt.Errorf("create chat status = %d", response.StatusCode)
|
||||
}
|
||||
var chat struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&chat); err != nil {
|
||||
return "", fmt.Errorf("decode chat: %w", err)
|
||||
}
|
||||
return chat.ID, nil
|
||||
}
|
||||
|
||||
func (c *apiClient) createMessage(client *http.Client, chatID, key, text string) (string, string, error) {
|
||||
payload, err := json.Marshal(map[string]string{"text": text})
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
request, err := http.NewRequest(
|
||||
http.MethodPost, c.baseURL+"/api/v1/chats/"+chatID+"/messages", bytes.NewReader(payload),
|
||||
)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
request.Header.Set("Authorization", "Bearer "+c.token)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set("Idempotency-Key", key)
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("create message: %w", err)
|
||||
}
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
return "", response.Header.Get("X-Instance-Id"), fmt.Errorf("create message status = %d", response.StatusCode)
|
||||
}
|
||||
var message struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&message); err != nil {
|
||||
return "", response.Header.Get("X-Instance-Id"), fmt.Errorf("decode message: %w", err)
|
||||
}
|
||||
return message.ID, response.Header.Get("X-Instance-Id"), nil
|
||||
}
|
||||
|
||||
func (c *apiClient) verifyMessageHistory(chatID, messageID string) error {
|
||||
request, err := http.NewRequest(http.MethodGet, c.baseURL+"/api/v1/chats/"+chatID+"/messages?limit=100", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request.Header.Set("Authorization", "Bearer "+c.token)
|
||||
response, err := c.client.Do(request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("list messages: %w", err)
|
||||
}
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("list messages status = %d", response.StatusCode)
|
||||
}
|
||||
var page struct {
|
||||
Items []struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"items"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&page); err != nil {
|
||||
return fmt.Errorf("decode messages: %w", err)
|
||||
}
|
||||
if len(page.Items) != 1 || page.Items[0].ID != messageID {
|
||||
return fmt.Errorf("message history contains %d items with an unexpected id", len(page.Items))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func verifyOutage(client *apiClient, service string) (returnErr error) {
|
||||
if err := compose("stop", service); err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if err := compose("up", "-d", "--wait", "--wait-timeout", "90", service); returnErr == nil && err != nil {
|
||||
returnErr = err
|
||||
}
|
||||
}()
|
||||
if err := client.expectEventually("/health/ready", http.StatusServiceUnavailable, false); err != nil {
|
||||
return fmt.Errorf("%s readiness outage: %w", service, err)
|
||||
}
|
||||
if err := client.expectEventually("/api/v1/users/me", http.StatusServiceUnavailable, true); err != nil {
|
||||
return fmt.Errorf("%s business outage: %w", service, err)
|
||||
}
|
||||
if err := compose("up", "-d", "--wait", "--wait-timeout", "90", service); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := client.expectEventually("/health/ready", http.StatusOK, false); err != nil {
|
||||
return fmt.Errorf("%s readiness recovery: %w", service, err)
|
||||
}
|
||||
if err := client.expectEventually("/api/v1/users/me", http.StatusOK, true); err != nil {
|
||||
return fmt.Errorf("%s session recovery: %w", service, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func compose(arguments ...string) error {
|
||||
command := exec.Command("docker", append([]string{"compose"}, arguments...)...)
|
||||
if output, err := command.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("docker compose %v: %w: %s", arguments, err, bytes.TrimSpace(output))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *apiClient) expectEventually(path string, status int, authenticated bool) error {
|
||||
deadline := time.Now().Add(20 * time.Second)
|
||||
var lastErr error
|
||||
for time.Now().Before(deadline) {
|
||||
lastErr = c.expect(http.MethodGet, path, status, authenticated)
|
||||
if lastErr == nil {
|
||||
return nil
|
||||
}
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
return lastErr
|
||||
}
|
||||
|
||||
func (c *apiClient) expect(method, path string, status int, authenticated bool) error {
|
||||
request, err := http.NewRequest(method, c.baseURL+path, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if authenticated {
|
||||
request.Header.Set("Authorization", "Bearer "+c.token)
|
||||
}
|
||||
response, err := c.client.Do(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return closeResponse(response, status)
|
||||
}
|
||||
|
||||
func closeResponse(response *http.Response, status int) error {
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
_, _ = io.Copy(io.Discard, io.LimitReader(response.Body, 1<<20))
|
||||
if response.StatusCode != status {
|
||||
return fmt.Errorf("status = %d, want %d", response.StatusCode, status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,324 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/model"
|
||||
)
|
||||
|
||||
type apiState struct {
|
||||
BaseURL string `json:"base_url"`
|
||||
CAFile string `json:"ca_file"`
|
||||
Token string `json:"token"`
|
||||
AttachmentID string `json:"attachment_id"`
|
||||
}
|
||||
|
||||
type apiUser struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
type apiSession struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
type apiChat struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
func runAPIProduce(arguments []string, output io.Writer) error {
|
||||
flags := flag.NewFlagSet("api-produce", flag.ContinueOnError)
|
||||
flags.SetOutput(io.Discard)
|
||||
baseURL := flags.String("base-url", "https://nginx:8443", "messenger base URL")
|
||||
caFile := flags.String("ca-file", "/run/security/public-ca/ca.crt", "trusted CA certificate")
|
||||
if err := flags.Parse(arguments); err != nil || flags.NArg() != 0 {
|
||||
return errors.New("usage: lab5experiment api-produce [-base-url URL] [-ca-file PATH]")
|
||||
}
|
||||
client, err := newAPIClient(*caFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
suffix := strings.ReplaceAll(uuid.NewString(), "-", "")[:12]
|
||||
password := "Lab5-" + uuid.NewString()
|
||||
owner, err := registerAPIUser(client, *baseURL, "qa_"+suffix, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
member, err := registerAPIUser(client, *baseURL, "qb_"+suffix, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
token, err := createAPISession(client, *baseURL, "qa_"+suffix, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
chat, err := createAPIChat(client, *baseURL, token, owner.ID, member.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fixture, err := pngFixture(320, 200)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var attachment model.Attachment
|
||||
response, err := apiRequest(
|
||||
client, http.MethodPost,
|
||||
*baseURL+"/api/v1/chats/"+chat.ID+"/attachments?filename=queued.png",
|
||||
token, "image/png", fixture, &attachment,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.StatusCode != http.StatusAccepted || attachment.Status != model.AttachmentQueued {
|
||||
return fmt.Errorf("upload returned status=%d attachment_status=%s", response.StatusCode, attachment.Status)
|
||||
}
|
||||
messageBody, err := json.Marshal(map[string]any{"text": "", "attachment_ids": []string{attachment.ID}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response, err = apiRequestWithHeaders(
|
||||
client, http.MethodPost, *baseURL+"/api/v1/chats/"+chat.ID+"/messages",
|
||||
token, "application/json", messageBody, nil,
|
||||
map[string]string{"Idempotency-Key": uuid.NewString()},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
return fmt.Errorf("link queued attachment returned %d", response.StatusCode)
|
||||
}
|
||||
response, err = apiRequest(client, http.MethodGet, *baseURL+"/api/v1/attachments/"+attachment.ID+"/content", token, "", nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.StatusCode != http.StatusConflict {
|
||||
return fmt.Errorf("queued content returned %d, expected 409", response.StatusCode)
|
||||
}
|
||||
return json.NewEncoder(output).Encode(apiState{
|
||||
BaseURL: *baseURL, CAFile: *caFile, Token: token, AttachmentID: attachment.ID,
|
||||
})
|
||||
}
|
||||
|
||||
func runAPIVerify(arguments []string, input io.Reader, output io.Writer) error {
|
||||
flags := flag.NewFlagSet("api-verify", flag.ContinueOnError)
|
||||
flags.SetOutput(io.Discard)
|
||||
waitTimeout := flags.Duration("wait-timeout", time.Minute, "ready wait timeout")
|
||||
if err := flags.Parse(arguments); err != nil || flags.NArg() != 0 {
|
||||
return errors.New("usage: lab5experiment api-verify [-wait-timeout 1m]")
|
||||
}
|
||||
var state apiState
|
||||
if err := json.NewDecoder(input).Decode(&state); err != nil {
|
||||
return fmt.Errorf("decode API scenario state: %w", err)
|
||||
}
|
||||
client, err := newAPIClient(state.CAFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), *waitTimeout)
|
||||
defer cancel()
|
||||
var attachment model.Attachment
|
||||
for {
|
||||
response, err := apiRequest(client, http.MethodGet, state.BaseURL+"/api/v1/attachments/"+state.AttachmentID, state.Token, "", nil, &attachment)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("attachment metadata returned %d", response.StatusCode)
|
||||
}
|
||||
if attachment.Status == model.AttachmentReady {
|
||||
break
|
||||
}
|
||||
if attachment.Status == model.AttachmentFailed {
|
||||
return fmt.Errorf("attachment failed with %v", attachment.ErrorCode)
|
||||
}
|
||||
if err := wait(ctx, 250*time.Millisecond); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(attachment.Variants) != 3 {
|
||||
return fmt.Errorf("public variants = %d, expected 3", len(attachment.Variants))
|
||||
}
|
||||
response, original, err := apiBytes(client, state.BaseURL+"/api/v1/attachments/"+state.AttachmentID+"/content", state.Token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fixture, err := pngFixture(320, 200)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.StatusCode != http.StatusOK || !bytes.Equal(original, fixture) {
|
||||
return errors.New("original changed after worker and broker restart")
|
||||
}
|
||||
response, thumbnail, err := apiBytes(
|
||||
client, state.BaseURL+"/api/v1/attachments/"+state.AttachmentID+"/content?variant=thumbnail", state.Token,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.StatusCode != http.StatusOK || response.Header.Get("Content-Type") != "image/jpeg" {
|
||||
return errors.New("thumbnail response is invalid")
|
||||
}
|
||||
imageConfig, format, err := image.DecodeConfig(bytes.NewReader(thumbnail))
|
||||
if err != nil || format != "jpeg" || imageConfig.Width != 256 || imageConfig.Height != 160 {
|
||||
return errors.New("thumbnail dimensions are invalid")
|
||||
}
|
||||
return json.NewEncoder(output).Encode(map[string]any{
|
||||
"accepted_while_worker_stopped": true,
|
||||
"accepted_before_outbox_publish": true,
|
||||
"linked_while_queued": true,
|
||||
"content_conflict_while_queued": true,
|
||||
"ready_after_api_broker_restart": true,
|
||||
"original_preserved": true,
|
||||
"thumbnail_dimensions": "256x160",
|
||||
})
|
||||
}
|
||||
|
||||
func newAPIClient(caFile string) (*http.Client, error) {
|
||||
caPEM, err := os.ReadFile(caFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read trusted CA: %w", err)
|
||||
}
|
||||
roots := x509.NewCertPool()
|
||||
if !roots.AppendCertsFromPEM(caPEM) {
|
||||
return nil, errors.New("trusted CA does not contain a certificate")
|
||||
}
|
||||
transport := &http.Transport{TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12, RootCAs: roots}}
|
||||
return &http.Client{Transport: transport, Timeout: 10 * time.Second}, nil
|
||||
}
|
||||
|
||||
func registerAPIUser(client *http.Client, baseURL, username, password string) (apiUser, error) {
|
||||
body, err := json.Marshal(map[string]string{
|
||||
"username": username, "display_name": username, "password": password,
|
||||
})
|
||||
if err != nil {
|
||||
return apiUser{}, err
|
||||
}
|
||||
var user apiUser
|
||||
response, err := apiRequest(client, http.MethodPost, baseURL+"/api/v1/users", "", "application/json", body, &user)
|
||||
if err != nil {
|
||||
return apiUser{}, err
|
||||
}
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
return apiUser{}, fmt.Errorf("register user returned %d", response.StatusCode)
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func createAPISession(client *http.Client, baseURL, username, password string) (string, error) {
|
||||
credentials := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
|
||||
request, err := http.NewRequest(http.MethodPost, baseURL+"/api/v1/auth/sessions", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
request.Header.Set("Authorization", "Basic "+credentials)
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
return "", fmt.Errorf("create session returned %d", response.StatusCode)
|
||||
}
|
||||
var session apiSession
|
||||
if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&session); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return session.Token, nil
|
||||
}
|
||||
|
||||
func createAPIChat(client *http.Client, baseURL, token, ownerID, memberID string) (apiChat, error) {
|
||||
body, err := json.Marshal(map[string]any{
|
||||
"title": "Lab 5 resilience", "member_ids": []string{ownerID, memberID},
|
||||
})
|
||||
if err != nil {
|
||||
return apiChat{}, err
|
||||
}
|
||||
var chat apiChat
|
||||
response, err := apiRequest(client, http.MethodPost, baseURL+"/api/v1/chats", token, "application/json", body, &chat)
|
||||
if err != nil {
|
||||
return apiChat{}, err
|
||||
}
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
return apiChat{}, fmt.Errorf("create chat returned %d", response.StatusCode)
|
||||
}
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func apiRequest(
|
||||
client *http.Client,
|
||||
method string,
|
||||
requestURL string,
|
||||
token string,
|
||||
contentType string,
|
||||
body []byte,
|
||||
result any,
|
||||
) (*http.Response, error) {
|
||||
return apiRequestWithHeaders(client, method, requestURL, token, contentType, body, result, nil)
|
||||
}
|
||||
|
||||
func apiRequestWithHeaders(
|
||||
client *http.Client,
|
||||
method string,
|
||||
requestURL string,
|
||||
token string,
|
||||
contentType string,
|
||||
body []byte,
|
||||
result any,
|
||||
headers map[string]string,
|
||||
) (*http.Response, error) {
|
||||
request, err := http.NewRequest(method, requestURL, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if token != "" {
|
||||
request.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
if contentType != "" {
|
||||
request.Header.Set("Content-Type", contentType)
|
||||
}
|
||||
for name, value := range headers {
|
||||
request.Header.Set(name, value)
|
||||
}
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
if result != nil && response.StatusCode < http.StatusBadRequest {
|
||||
if err := json.NewDecoder(io.LimitReader(response.Body, 16<<20)).Decode(result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func apiBytes(client *http.Client, requestURL, token string) (*http.Response, []byte, error) {
|
||||
request, err := http.NewRequest(http.MethodGet, requestURL, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
request.Header.Set("Authorization", "Bearer "+token)
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
body, err := io.ReadAll(io.LimitReader(response.Body, 16<<20))
|
||||
return response, body, err
|
||||
}
|
||||
@@ -0,0 +1,617 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/broker/redisstream"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/config"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/database"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/messenger/attachment/transform"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/messenger/attachment/worker"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/model"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/objectstore"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/outbox"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/postgres"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/security/tlsconfig"
|
||||
)
|
||||
|
||||
const (
|
||||
experimentLease = 2 * time.Second
|
||||
maxAttempts = 5
|
||||
)
|
||||
|
||||
type result struct {
|
||||
StartedAt string `json:"started_at"`
|
||||
DurationMillis int64 `json:"duration_ms"`
|
||||
PipelineVersion int `json:"pipeline_version"`
|
||||
FaultScenarios []scenario `json:"fault_scenarios"`
|
||||
DuplicateDeliveries int `json:"duplicate_deliveries"`
|
||||
V1ObjectCount int `json:"v1_object_count"`
|
||||
V2ObjectCount int `json:"v2_object_count"`
|
||||
AcceptedJobs int `json:"accepted_jobs"`
|
||||
TerminalJobs int `json:"terminal_jobs"`
|
||||
ReadyJobs int `json:"ready_jobs"`
|
||||
FailedJobs int `json:"failed_jobs"`
|
||||
PermanentErrorCode string `json:"permanent_error_code"`
|
||||
ReplayAttempt int `json:"replay_attempt"`
|
||||
InvariantResults map[string]bool `json:"invariant_results"`
|
||||
}
|
||||
|
||||
type scenario struct {
|
||||
Name string `json:"name"`
|
||||
AttemptAfterRecovery int `json:"attempt_after_recovery"`
|
||||
ObjectsBeforeRecovery int `json:"objects_before_recovery"`
|
||||
ObjectsAfterRecovery int `json:"objects_after_recovery"`
|
||||
FinalStatus string `json:"final_status"`
|
||||
}
|
||||
|
||||
type experiment struct {
|
||||
settings config.WorkerConfig
|
||||
repository *postgres.Store
|
||||
objects *objectstore.Store
|
||||
broker *redisstream.Broker
|
||||
dispatcher *outbox.Dispatcher
|
||||
worker *worker.Worker
|
||||
ownerID string
|
||||
chatID string
|
||||
fixture []byte
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := run(os.Args[1:], os.Stdout); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(arguments []string, output io.Writer) error {
|
||||
if len(arguments) > 0 {
|
||||
switch arguments[0] {
|
||||
case "api-produce":
|
||||
return runAPIProduce(arguments[1:], output)
|
||||
case "api-verify":
|
||||
return runAPIVerify(arguments[1:], os.Stdin, output)
|
||||
}
|
||||
}
|
||||
flags := flag.NewFlagSet("lab5experiment", flag.ContinueOnError)
|
||||
flags.SetOutput(io.Discard)
|
||||
timeout := flags.Duration("timeout", 2*time.Minute, "experiment timeout")
|
||||
if err := flags.Parse(arguments); err != nil || flags.NArg() != 0 || *timeout <= 0 {
|
||||
return errors.New("usage: lab5experiment [-timeout 2m]")
|
||||
}
|
||||
started := time.Now()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
|
||||
defer cancel()
|
||||
experiment, closeExperiment, err := newExperiment(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer closeExperiment()
|
||||
|
||||
report, err := experiment.run(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
report.StartedAt = started.UTC().Format(time.RFC3339)
|
||||
report.DurationMillis = time.Since(started).Milliseconds()
|
||||
encoder := json.NewEncoder(output)
|
||||
encoder.SetIndent("", " ")
|
||||
return encoder.Encode(report)
|
||||
}
|
||||
|
||||
func newExperiment(ctx context.Context) (*experiment, func(), error) {
|
||||
settings, err := config.LoadWorker()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
pool, err := database.Open(ctx, database.Config{
|
||||
URL: settings.DatabaseURL, MinConnections: settings.DBMinConns, MaxConnections: settings.DBMaxConns,
|
||||
MaxConnectionAge: settings.DBMaxConnectionAge, MaxConnectionIdle: settings.DBMaxConnectionIdle,
|
||||
HealthCheckPeriod: settings.DBHealthCheckPeriod, ConnectTimeout: settings.DependencyTimeout,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
objects, err := objectstore.New(objectstore.Config{
|
||||
Endpoint: settings.S3Endpoint, Region: settings.S3Region, Bucket: settings.S3Bucket,
|
||||
AccessKey: settings.S3AccessKey, SecretKey: settings.S3SecretKey,
|
||||
UseTLS: settings.S3UseTLS, CAPath: settings.S3CAPath, Timeout: settings.S3Timeout,
|
||||
})
|
||||
if err != nil {
|
||||
pool.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
redisOptions, err := redis.ParseURL(settings.BrokerURL)
|
||||
if err != nil {
|
||||
pool.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
brokerTLS, err := tlsconfig.LoadClient(tlsconfig.ClientConfig{
|
||||
CAPath: settings.BrokerTLS.CAPath, CertificatePath: settings.BrokerTLS.CertificatePath,
|
||||
PrivateKeyPath: settings.BrokerTLS.PrivateKeyPath, ServerName: settings.BrokerTLS.ServerName,
|
||||
})
|
||||
if err != nil {
|
||||
pool.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
redisOptions.TLSConfig = brokerTLS
|
||||
client := redis.NewClient(redisOptions)
|
||||
if err := client.Ping(ctx).Err(); err != nil {
|
||||
pool.Close()
|
||||
_ = client.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
consumer := "lab5experiment-" + uuid.NewString()
|
||||
broker, err := redisstream.New(client, redisstream.Config{
|
||||
Stream: settings.Stream, Group: settings.ConsumerGroup, Consumer: consumer,
|
||||
FailedStream: settings.FailedStream, FailedMaxLen: settings.FailedStreamMaxLength, Block: time.Second,
|
||||
ClaimMinIdle: experimentLease, ReadCount: 32,
|
||||
})
|
||||
if err != nil {
|
||||
pool.Close()
|
||||
_ = client.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := broker.Ensure(ctx); err != nil {
|
||||
pool.Close()
|
||||
_ = client.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
repository := postgres.New(pool)
|
||||
dispatcher, err := outbox.New(repository, broker, nil, outbox.Config{
|
||||
BatchSize: 32, LeaseDuration: 10 * time.Second, PollInterval: time.Second,
|
||||
RecoveryInterval: 10 * time.Second, RetryDelay: time.Second,
|
||||
Retention: 24 * time.Hour, CleanupInterval: time.Hour, CleanupBatchSize: 1000,
|
||||
})
|
||||
if err != nil {
|
||||
pool.Close()
|
||||
_ = client.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
processor, err := transform.NewProcessor(transform.DefaultConfig(settings.AttachmentMaxBytes, settings.AttachmentMaxPixels))
|
||||
if err != nil {
|
||||
pool.Close()
|
||||
_ = client.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
processorWorker, err := worker.New(
|
||||
slog.New(slog.NewTextHandler(io.Discard, nil)), repository, broker, objects, processor, nil,
|
||||
worker.Config{
|
||||
WorkerID: consumer, Concurrency: 1,
|
||||
LeaseDuration: experimentLease, HeartbeatInterval: 500 * time.Millisecond,
|
||||
ProcessingTimeout: time.Second,
|
||||
ReclaimInterval: experimentLease,
|
||||
MaxAttempts: maxAttempts, RetryBase: time.Second, RetryMax: 4 * time.Second,
|
||||
MaxBytes: settings.AttachmentMaxBytes, MaxPixels: settings.AttachmentMaxPixels,
|
||||
MemoryBytes: settings.MemoryBytes,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
pool.Close()
|
||||
_ = client.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
fixture, err := pngFixture(320, 200)
|
||||
if err != nil {
|
||||
pool.Close()
|
||||
_ = client.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
value := &experiment{
|
||||
settings: settings, repository: repository, objects: objects, broker: broker,
|
||||
dispatcher: dispatcher, worker: processorWorker, fixture: fixture,
|
||||
}
|
||||
if err := value.seed(ctx); err != nil {
|
||||
pool.Close()
|
||||
_ = client.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
closeExperiment := func() {
|
||||
pool.Close()
|
||||
_ = client.Close()
|
||||
}
|
||||
return value, closeExperiment, nil
|
||||
}
|
||||
|
||||
func (e *experiment) seed(ctx context.Context) error {
|
||||
suffix := strings.ReplaceAll(uuid.NewString(), "-", "")[:12]
|
||||
owner, err := e.repository.CreateUser(ctx, "l5a_"+suffix, "Lab 5 A", "$argon2id$experiment")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
member, err := e.repository.CreateUser(ctx, "l5b_"+suffix, "Lab 5 B", "$argon2id$experiment")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
chat, err := e.repository.CreateChat(ctx, "Lab 5 experiment", []string{owner.ID, member.ID}, owner.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.ownerID = owner.ID
|
||||
e.chatID = chat.ID
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *experiment) run(ctx context.Context) (result, error) {
|
||||
report := result{
|
||||
PipelineVersion: 1,
|
||||
InvariantResults: map[string]bool{
|
||||
"accepted_jobs_reach_terminal_state": false,
|
||||
"duplicate_effect_is_unique": false,
|
||||
"old_pipeline_cannot_replace_new": false,
|
||||
},
|
||||
}
|
||||
for _, fault := range []worker.FaultPoint{worker.FaultBeforeSideEffect, worker.FaultAfterSideEffect} {
|
||||
observed, err := e.runLeaseRecovery(ctx, fault)
|
||||
if err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
report.FaultScenarios = append(report.FaultScenarios, observed)
|
||||
report.AcceptedJobs++
|
||||
if observed.FinalStatus == model.AttachmentReady {
|
||||
report.ReadyJobs++
|
||||
report.TerminalJobs++
|
||||
}
|
||||
}
|
||||
permanentCode, replayAttempt, err := e.runPermanentFailureReplay(ctx)
|
||||
if err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
report.AcceptedJobs++
|
||||
report.TerminalJobs++
|
||||
report.FailedJobs++
|
||||
report.PermanentErrorCode = permanentCode
|
||||
report.ReplayAttempt = replayAttempt
|
||||
|
||||
commitScenario, envelope, attachmentID, err := e.runAfterCommit(ctx)
|
||||
if err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
report.FaultScenarios = append(report.FaultScenarios, commitScenario)
|
||||
report.AcceptedJobs++
|
||||
if commitScenario.FinalStatus == model.AttachmentReady {
|
||||
report.ReadyJobs++
|
||||
report.TerminalJobs++
|
||||
}
|
||||
|
||||
payload, err := redisstream.EncodeEnvelope(envelope)
|
||||
if err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
for range 2 {
|
||||
if _, err := e.broker.PublishConfirmed(ctx, payload); err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
}
|
||||
duplicateDeliveries, err := e.readDeliveries(ctx, 2)
|
||||
if err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
for _, delivery := range duplicateDeliveries {
|
||||
if err := e.worker.ProcessDelivery(ctx, delivery, worker.FaultNone); err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
}
|
||||
report.DuplicateDeliveries = len(duplicateDeliveries)
|
||||
report.V1ObjectCount, err = e.objectCount(ctx, attachmentID)
|
||||
if err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
if report.V1ObjectCount != 4 {
|
||||
return result{}, fmt.Errorf("duplicate delivery produced %d objects, expected 4", report.V1ObjectCount)
|
||||
}
|
||||
report.InvariantResults["duplicate_effect_is_unique"] = true
|
||||
|
||||
oldPayload := payload
|
||||
v2Job := model.AttachmentJob{ID: uuid.NewString(), PipelineVersion: envelope.PipelineVersion + 1}
|
||||
if _, err := e.repository.QueueAttachmentPipelineVersion(ctx, attachmentID, v2Job, "", ""); err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
if _, err := e.dispatcher.DispatchOnce(ctx); err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
v2Delivery, err := e.readDelivery(ctx)
|
||||
if err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
if err := e.worker.ProcessDelivery(ctx, v2Delivery, worker.FaultNone); err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
if _, err := e.broker.PublishConfirmed(ctx, oldPayload); err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
staleDelivery, err := e.readDelivery(ctx)
|
||||
if err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
if err := e.worker.ProcessDelivery(ctx, staleDelivery, worker.FaultNone); err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
metadata, err := e.repository.AttachmentForUser(ctx, attachmentID, e.ownerID)
|
||||
if err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
if metadata.Status != model.AttachmentReady || metadata.PipelineVersion != v2Job.PipelineVersion || len(metadata.Variants) != 3 {
|
||||
return result{}, errors.New("new pipeline version was not preserved after stale delivery")
|
||||
}
|
||||
report.V2ObjectCount, err = e.objectCount(ctx, attachmentID)
|
||||
if err != nil {
|
||||
return result{}, err
|
||||
}
|
||||
if report.V2ObjectCount != 8 {
|
||||
return result{}, fmt.Errorf("versioned pipeline produced %d objects, expected 8", report.V2ObjectCount)
|
||||
}
|
||||
report.InvariantResults["old_pipeline_cannot_replace_new"] = true
|
||||
report.AcceptedJobs++
|
||||
report.ReadyJobs++
|
||||
report.TerminalJobs++
|
||||
report.InvariantResults["accepted_jobs_reach_terminal_state"] = report.AcceptedJobs == report.TerminalJobs
|
||||
return report, nil
|
||||
}
|
||||
|
||||
func (e *experiment) runPermanentFailureReplay(ctx context.Context) (string, int, error) {
|
||||
attachmentID, _, _, err := e.queueAttachmentData(ctx, []byte("corrupted image"), "image/png")
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
delivery, err := e.readDelivery(ctx)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
if err := e.worker.ProcessDelivery(ctx, delivery, worker.FaultNone); err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
metadata, err := e.repository.AttachmentForUser(ctx, attachmentID, e.ownerID)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
if metadata.Status != model.AttachmentFailed || metadata.ErrorCode == nil {
|
||||
return "", 0, errors.New("corrupted staging object did not reach failed")
|
||||
}
|
||||
firstCode := *metadata.ErrorCode
|
||||
replayed, err := e.repository.ReplayFailedAttachment(
|
||||
ctx, attachmentID, "lab5experiment", "verify explicit replay fencing",
|
||||
)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
if _, err := e.dispatcher.DispatchOnce(ctx); err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
replayDelivery, err := e.readDelivery(ctx)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
if err := e.worker.ProcessDelivery(ctx, replayDelivery, worker.FaultNone); err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
metadata, err = e.repository.AttachmentForUser(ctx, attachmentID, e.ownerID)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
if metadata.Status != model.AttachmentFailed || metadata.ErrorCode == nil || *metadata.ErrorCode != firstCode {
|
||||
return "", 0, errors.New("replayed permanent failure changed terminal classification")
|
||||
}
|
||||
return firstCode, replayed.Attempt, nil
|
||||
}
|
||||
|
||||
func (e *experiment) runLeaseRecovery(ctx context.Context, fault worker.FaultPoint) (scenario, error) {
|
||||
attachmentID, _, err := e.queueAttachment(ctx)
|
||||
if err != nil {
|
||||
return scenario{}, err
|
||||
}
|
||||
delivery, err := e.readDelivery(ctx)
|
||||
if err != nil {
|
||||
return scenario{}, err
|
||||
}
|
||||
if err := e.worker.ProcessDelivery(ctx, delivery, fault); !errors.Is(err, worker.ErrInjectedFailure) {
|
||||
return scenario{}, fmt.Errorf("fault %s returned %v", fault, err)
|
||||
}
|
||||
before, err := e.objectCount(ctx, attachmentID)
|
||||
if err != nil {
|
||||
return scenario{}, err
|
||||
}
|
||||
if fault == worker.FaultBeforeSideEffect && before != 0 {
|
||||
return scenario{}, errors.New("objects were written before the injected side effect")
|
||||
}
|
||||
if fault == worker.FaultAfterSideEffect && before != 4 {
|
||||
return scenario{}, fmt.Errorf("side effect wrote %d objects, expected 4", before)
|
||||
}
|
||||
if err := wait(ctx, experimentLease+250*time.Millisecond); err != nil {
|
||||
return scenario{}, err
|
||||
}
|
||||
recovered, err := e.repository.RecoverExpiredAttachmentJobs(ctx, 10, maxAttempts, time.Now())
|
||||
if err != nil {
|
||||
return scenario{}, err
|
||||
}
|
||||
if recovered.Requeued != 1 {
|
||||
return scenario{}, fmt.Errorf("requeued jobs = %d, expected 1", recovered.Requeued)
|
||||
}
|
||||
if _, err := e.dispatcher.DispatchOnce(ctx); err != nil {
|
||||
return scenario{}, err
|
||||
}
|
||||
retry, err := e.readDelivery(ctx)
|
||||
if err != nil {
|
||||
return scenario{}, err
|
||||
}
|
||||
if err := e.worker.ProcessDelivery(ctx, retry, worker.FaultNone); err != nil {
|
||||
return scenario{}, err
|
||||
}
|
||||
if err := e.ackReclaimed(ctx); err != nil {
|
||||
return scenario{}, err
|
||||
}
|
||||
metadata, err := e.repository.AttachmentForUser(ctx, attachmentID, e.ownerID)
|
||||
if err != nil {
|
||||
return scenario{}, err
|
||||
}
|
||||
after, err := e.objectCount(ctx, attachmentID)
|
||||
if err != nil {
|
||||
return scenario{}, err
|
||||
}
|
||||
job, err := e.repository.AttachmentJob(ctx, retry.Envelope.JobID.String())
|
||||
if err != nil {
|
||||
return scenario{}, err
|
||||
}
|
||||
return scenario{
|
||||
Name: string(fault), AttemptAfterRecovery: job.Attempt,
|
||||
ObjectsBeforeRecovery: before, ObjectsAfterRecovery: after, FinalStatus: metadata.Status,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (e *experiment) runAfterCommit(ctx context.Context) (scenario, redisstream.Envelope, string, error) {
|
||||
attachmentID, _, err := e.queueAttachment(ctx)
|
||||
if err != nil {
|
||||
return scenario{}, redisstream.Envelope{}, "", err
|
||||
}
|
||||
delivery, err := e.readDelivery(ctx)
|
||||
if err != nil {
|
||||
return scenario{}, redisstream.Envelope{}, "", err
|
||||
}
|
||||
if err := e.worker.ProcessDelivery(ctx, delivery, worker.FaultAfterCommit); !errors.Is(err, worker.ErrInjectedFailure) {
|
||||
return scenario{}, redisstream.Envelope{}, "", fmt.Errorf("after_commit returned %v", err)
|
||||
}
|
||||
metadata, err := e.repository.AttachmentForUser(ctx, attachmentID, e.ownerID)
|
||||
if err != nil {
|
||||
return scenario{}, redisstream.Envelope{}, "", err
|
||||
}
|
||||
before, err := e.objectCount(ctx, attachmentID)
|
||||
if err != nil {
|
||||
return scenario{}, redisstream.Envelope{}, "", err
|
||||
}
|
||||
if err := wait(ctx, experimentLease+250*time.Millisecond); err != nil {
|
||||
return scenario{}, redisstream.Envelope{}, "", err
|
||||
}
|
||||
if err := e.ackReclaimed(ctx); err != nil {
|
||||
return scenario{}, redisstream.Envelope{}, "", err
|
||||
}
|
||||
after, err := e.objectCount(ctx, attachmentID)
|
||||
if err != nil {
|
||||
return scenario{}, redisstream.Envelope{}, "", err
|
||||
}
|
||||
return scenario{
|
||||
Name: string(worker.FaultAfterCommit), AttemptAfterRecovery: delivery.Envelope.Attempt,
|
||||
ObjectsBeforeRecovery: before, ObjectsAfterRecovery: after, FinalStatus: metadata.Status,
|
||||
}, delivery.Envelope, attachmentID, nil
|
||||
}
|
||||
|
||||
func (e *experiment) queueAttachment(ctx context.Context) (string, model.AttachmentJob, error) {
|
||||
attachmentID, _, job, err := e.queueAttachmentData(ctx, e.fixture, "image/png")
|
||||
return attachmentID, job, err
|
||||
}
|
||||
|
||||
func (e *experiment) queueAttachmentData(
|
||||
ctx context.Context,
|
||||
data []byte,
|
||||
contentType string,
|
||||
) (string, string, model.AttachmentJob, error) {
|
||||
attachmentID := uuid.NewString()
|
||||
stagingKey := "staging/" + attachmentID + "/experiment"
|
||||
digest := sha256.Sum256(data)
|
||||
_, err := e.repository.CreatePendingAttachment(ctx, model.Attachment{
|
||||
ID: attachmentID, ChatID: e.chatID, OwnerID: e.ownerID, Filename: "experiment.png",
|
||||
ContentType: contentType, SizeBytes: int64(len(data)), SHA256: hex.EncodeToString(digest[:]),
|
||||
ObjectKey: stagingKey, StagingKey: stagingKey, Status: model.AttachmentPending, PipelineVersion: 1,
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", model.AttachmentJob{}, err
|
||||
}
|
||||
if err := e.objects.Put(ctx, stagingKey, contentType, data); err != nil {
|
||||
return "", "", model.AttachmentJob{}, err
|
||||
}
|
||||
job := model.AttachmentJob{ID: uuid.NewString(), PipelineVersion: 1, Attempt: 1}
|
||||
if _, err := e.repository.QueueAttachmentWithJobAndOutbox(ctx, attachmentID, stagingKey, job, "", ""); err != nil {
|
||||
return "", "", model.AttachmentJob{}, err
|
||||
}
|
||||
if _, err := e.dispatcher.DispatchOnce(ctx); err != nil {
|
||||
return "", "", model.AttachmentJob{}, err
|
||||
}
|
||||
return attachmentID, stagingKey, job, nil
|
||||
}
|
||||
|
||||
func (e *experiment) readDelivery(ctx context.Context) (redisstream.Delivery, error) {
|
||||
deliveries, err := e.readDeliveries(ctx, 1)
|
||||
if err != nil {
|
||||
return redisstream.Delivery{}, err
|
||||
}
|
||||
return deliveries[0], nil
|
||||
}
|
||||
|
||||
func (e *experiment) readDeliveries(ctx context.Context, expected int) ([]redisstream.Delivery, error) {
|
||||
var result []redisstream.Delivery
|
||||
for len(result) < expected {
|
||||
deliveries, err := e.broker.Read(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, deliveries...)
|
||||
}
|
||||
if len(result) != expected {
|
||||
return nil, fmt.Errorf("deliveries = %d, expected %d", len(result), expected)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (e *experiment) ackReclaimed(ctx context.Context) error {
|
||||
deliveries, _, err := e.broker.Reclaim(ctx, "0-0")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(deliveries) != 1 {
|
||||
return fmt.Errorf("reclaimed deliveries = %d, expected 1", len(deliveries))
|
||||
}
|
||||
return e.worker.ProcessDelivery(ctx, deliveries[0], worker.FaultNone)
|
||||
}
|
||||
|
||||
func (e *experiment) objectCount(ctx context.Context, attachmentID string) (int, error) {
|
||||
objects, next, err := e.objects.List(ctx, "attachments/"+attachmentID+"/", "", 100)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if next != "" {
|
||||
return 0, errors.New("unexpected object pagination")
|
||||
}
|
||||
return len(objects), nil
|
||||
}
|
||||
|
||||
func pngFixture(width, height int) ([]byte, error) {
|
||||
value := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||
for y := range height {
|
||||
for x := range width {
|
||||
value.SetNRGBA(x, y, color.NRGBA{R: uint8(x), G: uint8(y), B: uint8(x + y), A: 255})
|
||||
}
|
||||
}
|
||||
var output bytes.Buffer
|
||||
if err := png.Encode(&output, value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return output.Bytes(), nil
|
||||
}
|
||||
|
||||
func wait(ctx context.Context, duration time.Duration) error {
|
||||
timer := time.NewTimer(duration)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type result struct {
|
||||
status int
|
||||
retryAfter string
|
||||
instance string
|
||||
duration time.Duration
|
||||
err error
|
||||
}
|
||||
|
||||
type phase struct {
|
||||
Name string `json:"name"`
|
||||
Requests int `json:"requests"`
|
||||
Statuses map[int]int `json:"statuses"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
P95MS int64 `json:"p95_ms"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
var (
|
||||
baseURL = flag.String("base-url", "https://localhost:8443", "API endpoint")
|
||||
caFile = flag.String("ca-file", ".runtime/security/public-ca/ca.crt", "trusted public CA")
|
||||
count = flag.Int("requests", 20, "concurrent requests per phase")
|
||||
)
|
||||
flag.Parse()
|
||||
if *count < 8 || *count > 100 {
|
||||
return errors.New("requests must be between 8 and 100")
|
||||
}
|
||||
ca, err := os.ReadFile(*caFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
roots := x509.NewCertPool()
|
||||
if !roots.AppendCertsFromPEM(ca) {
|
||||
return errors.New("invalid public CA")
|
||||
}
|
||||
transport := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS13, RootCAs: roots},
|
||||
DisableKeepAlives: true, MaxConnsPerHost: *count,
|
||||
}
|
||||
defer transport.CloseIdleConnections()
|
||||
client := &http.Client{Transport: transport, Timeout: 10 * time.Second}
|
||||
username := fmt.Sprintf("burst_%d", time.Now().UnixNano())
|
||||
body, err := json.Marshal(map[string]string{
|
||||
"username": username, "display_name": "Login burst", "password": "T3st-burst-password-123",
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
register, err := burst(client, *count, "registration", func(index int) (*http.Request, error) {
|
||||
request, err := http.NewRequest(http.MethodPost, *baseURL+"/api/v1/users", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
return request, nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
login, err := burst(client, *count, "login", func(index int) (*http.Request, error) {
|
||||
request, err := http.NewRequest(http.MethodPost, *baseURL+"/api/v1/auth/sessions", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
request.SetBasicAuth(fmt.Sprintf("%s_%d", username, index), "wrong-password")
|
||||
return request, nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response, err := client.Get(*baseURL + "/health/ready")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("readiness after burst: HTTP %d", response.StatusCode)
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(struct {
|
||||
Phases []phase `json:"phases"`
|
||||
Ready bool `json:"ready_after_burst"`
|
||||
}{[]phase{register, login}, true})
|
||||
}
|
||||
|
||||
func burst(client *http.Client, count int, name string, request func(int) (*http.Request, error)) (phase, error) {
|
||||
var (
|
||||
start = make(chan struct{})
|
||||
results = make(chan result, count)
|
||||
group sync.WaitGroup
|
||||
instances = make(map[string]struct{})
|
||||
statuses = make(map[int]int)
|
||||
durations = make([]time.Duration, 0, count)
|
||||
started = time.Now()
|
||||
)
|
||||
for index := range count {
|
||||
group.Add(1)
|
||||
go func() {
|
||||
defer group.Done()
|
||||
<-start
|
||||
began := time.Now()
|
||||
value, err := request(index)
|
||||
if err != nil {
|
||||
results <- result{err: err}
|
||||
return
|
||||
}
|
||||
response, err := client.Do(value)
|
||||
if err != nil {
|
||||
results <- result{err: err}
|
||||
return
|
||||
}
|
||||
_, copyErr := io.Copy(io.Discard, response.Body)
|
||||
_ = response.Body.Close()
|
||||
results <- result{response.StatusCode, response.Header.Get("Retry-After"), response.Header.Get("X-Instance-Id"), time.Since(began), copyErr}
|
||||
}()
|
||||
}
|
||||
close(start)
|
||||
group.Wait()
|
||||
close(results)
|
||||
for value := range results {
|
||||
if value.err != nil {
|
||||
return phase{}, fmt.Errorf("%s burst: %w", name, value.err)
|
||||
}
|
||||
if value.status == http.StatusTooManyRequests {
|
||||
retryAfter, err := strconv.Atoi(value.retryAfter)
|
||||
if err != nil || retryAfter < 1 {
|
||||
return phase{}, errors.New("overload response has no positive Retry-After")
|
||||
}
|
||||
}
|
||||
allowed := value.status == http.StatusTooManyRequests
|
||||
if name == "registration" {
|
||||
allowed = allowed || value.status == http.StatusCreated || value.status == http.StatusConflict
|
||||
} else {
|
||||
allowed = allowed || value.status == http.StatusUnauthorized
|
||||
}
|
||||
if !allowed {
|
||||
return phase{}, fmt.Errorf("%s burst: unexpected HTTP %d", name, value.status)
|
||||
}
|
||||
statuses[value.status]++
|
||||
durations = append(durations, value.duration)
|
||||
if strings.TrimSpace(value.instance) != "" {
|
||||
instances[value.instance] = struct{}{}
|
||||
}
|
||||
}
|
||||
if statuses[http.StatusTooManyRequests] == 0 || len(instances) < 2 {
|
||||
return phase{}, fmt.Errorf("%s burst: expected overload across two replicas, statuses=%v instances=%d", name, statuses, len(instances))
|
||||
}
|
||||
sort.Slice(durations, func(i, j int) bool { return durations[i] < durations[j] })
|
||||
return phase{name, count, statuses, time.Since(started).Milliseconds(), durations[(count*95+99)/100-1].Milliseconds()}, nil
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/bootstrap"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/logger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
appLogger := logger.New()
|
||||
if err := run(os.Args[1:]); err != nil {
|
||||
appLogger.Error("application stopped", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(arguments []string) error {
|
||||
if len(arguments) == 0 {
|
||||
return bootstrap.RunServer()
|
||||
}
|
||||
if len(arguments) == 1 && arguments[0] == "migrate" {
|
||||
return bootstrap.RunMigrations()
|
||||
}
|
||||
if len(arguments) == 1 && arguments[0] == "dispatch" {
|
||||
return bootstrap.RunDispatcher()
|
||||
}
|
||||
if len(arguments) == 1 && arguments[0] == "worker" {
|
||||
return bootstrap.RunWorker()
|
||||
}
|
||||
if arguments[0] == "reconcile-attachments" {
|
||||
return bootstrap.RunReconciliation(arguments[1:], os.Stdout)
|
||||
}
|
||||
if arguments[0] == "replay-attachment" {
|
||||
return bootstrap.RunAttachmentReplay(arguments[1:], os.Stdout)
|
||||
}
|
||||
return errors.New("usage: messenger [migrate|dispatch|worker|reconcile-attachments|replay-attachment]")
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/security/mtlsproxy"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
config := mtlsproxy.Config{
|
||||
ListenAddress: required("PROXY_LISTEN_ADDRESS"), TargetAddress: required("PROXY_TARGET_ADDRESS"),
|
||||
CAPath: required("PROXY_TLS_CA_PATH"), CertificatePath: required("PROXY_TLS_CERTIFICATE_PATH"),
|
||||
PrivateKeyPath: required("PROXY_TLS_PRIVATE_KEY_PATH"), AllowedClientURI: required("PROXY_ALLOWED_CLIENT_URI"),
|
||||
ALPNProtocols: strings.Split(required("PROXY_ALPN_PROTOCOLS"), ","),
|
||||
DialTimeout: 5 * time.Second,
|
||||
}
|
||||
if config.ListenAddress == "" || config.TargetAddress == "" || config.CAPath == "" ||
|
||||
config.CertificatePath == "" || config.PrivateKeyPath == "" || config.AllowedClientURI == "" ||
|
||||
len(config.ALPNProtocols) == 0 || config.ALPNProtocols[0] == "" {
|
||||
return errors.New("mTLS proxy environment is incomplete")
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
return mtlsproxy.Run(ctx, config)
|
||||
}
|
||||
|
||||
func required(name string) string {
|
||||
return os.Getenv(name)
|
||||
}
|
||||
+1118
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,108 @@
|
||||
version: 1
|
||||
|
||||
server:
|
||||
health_address: 127.0.0.1:8082
|
||||
metrics_address: 0.0.0.0:9091
|
||||
tls:
|
||||
ca_path: /run/security/internal-ca/ca.crt
|
||||
certificate_path: /run/security/api/tls.crt
|
||||
private_key_path: /run/security/api/tls.key
|
||||
allowed_client_uri: spiffe://mtusi/lab7/load-balancer
|
||||
allowed_metrics_client_uri: spiffe://mtusi/lab7/prometheus
|
||||
session_store: valkey
|
||||
session_ttl: 1h
|
||||
idempotency_ttl: 24h
|
||||
login_rate_limit:
|
||||
account_attempts: 5
|
||||
source_attempts: 20
|
||||
window: 1m
|
||||
request_timeout: 5s
|
||||
http:
|
||||
read_header_timeout: 5s
|
||||
read_timeout: 10s
|
||||
write_timeout: 10s
|
||||
idle_timeout: 1m
|
||||
shutdown_timeout: 5s
|
||||
password:
|
||||
memory_kib: 65536
|
||||
iterations: 3
|
||||
parallelism: 2
|
||||
salt_length: 16
|
||||
key_length: 32
|
||||
max_concurrent_operations: 2
|
||||
|
||||
database:
|
||||
min_connections: 1
|
||||
max_connections: 10
|
||||
max_connection_age: 30m
|
||||
max_connection_idle: 5m
|
||||
health_check_period: 30s
|
||||
dependency_timeout: 5s
|
||||
|
||||
cache:
|
||||
tls:
|
||||
ca_path: /run/security/internal-ca/ca.crt
|
||||
certificate_path: /run/security/client/tls.crt
|
||||
private_key_path: /run/security/client/tls.key
|
||||
server_name: redis-gateway
|
||||
|
||||
migration:
|
||||
timeout: 30s
|
||||
|
||||
object_storage:
|
||||
use_tls: true
|
||||
ca_path: /run/security/internal-ca/ca.crt
|
||||
timeout: 5s
|
||||
|
||||
attachments:
|
||||
max_bytes: 10485760
|
||||
max_pixels: 40000000
|
||||
body_memory_bytes: 67108864
|
||||
decode_memory_bytes: 402653184
|
||||
|
||||
pipeline:
|
||||
version: 1
|
||||
|
||||
broker:
|
||||
tls:
|
||||
ca_path: /run/security/internal-ca/ca.crt
|
||||
certificate_path: /run/security/client/tls.crt
|
||||
private_key_path: /run/security/client/tls.key
|
||||
server_name: redis-gateway
|
||||
stream: attachment-jobs
|
||||
consumer_group: attachment-workers
|
||||
failed_stream: attachment-jobs-failed
|
||||
failed_stream_max_length: 10000
|
||||
|
||||
outbox:
|
||||
batch_size: 32
|
||||
lease: 15s
|
||||
poll_interval: 1s
|
||||
recovery_interval: 10s
|
||||
retry_delay: 2s
|
||||
retention: 168h
|
||||
cleanup_interval: 1m
|
||||
cleanup_batch_size: 1000
|
||||
|
||||
worker:
|
||||
concurrency: 2
|
||||
prefetch: 4
|
||||
lease: 30s
|
||||
heartbeat_interval: 5s
|
||||
processing_timeout: 20s
|
||||
read_block: 2s
|
||||
reclaim_interval: 5s
|
||||
max_attempts: 5
|
||||
retry:
|
||||
base: 1s
|
||||
max: 30s
|
||||
memory_bytes: 738197504
|
||||
|
||||
monitoring:
|
||||
metrics_address: 0.0.0.0:9091
|
||||
health_address: 127.0.0.1:9092
|
||||
tls:
|
||||
ca_path: /run/security/internal-ca/ca.crt
|
||||
certificate_path: /run/security/metrics/tls.crt
|
||||
private_key_path: /run/security/metrics/tls.key
|
||||
allowed_client_uri: spiffe://mtusi/lab7/prometheus
|
||||
+24
-34
@@ -1,37 +1,27 @@
|
||||
# Матрица доверия — заполнить в решении lab7
|
||||
# Матрица доверия
|
||||
|
||||
Это обязательная матрица покрытия. Для каждой строки укажите реальный порт, endpoint, CA, service identity, проверку SAN/ACL, срок жизни credentials и команду положительного/отрицательного теста. TLS шифрует и подтверждает сервер; mTLS также проверяет клиента. При доверенном CA всё равно нужно ограничивать разрешённую identity. Plaintext hop допустим только внутри закрытого локального transport/proxy boundary, без опубликованного обхода; границу и rationale нужно доказать.
|
||||
| Связь | Порт и протокол | Server trust и client identity | Минимальные права / lifecycle | Доказательство |
|
||||
| --- | --- | --- | --- | --- |
|
||||
| Клиент → Nginx | `8443`, TLS 1.3 | Public CA, SAN `localhost`; Bearer session | Сессия хранится как hash с TTL; общий login limiter | `make test-security`; `make test`; `make test-login-rate-limit` |
|
||||
| Nginx → API replicas | `8443`, mTLS | Internal CA; server SAN `api`; client URI `spiffe://mtusi/lab7/load-balancer` | Leaf выпускается bootstrap и меняется без rebuild | `make test-tls-matrix`: allow LB, deny no-cert/foreign/forbidden |
|
||||
| Prometheus → API metrics | `9091`, mTLS | Internal CA; URI `spiffe://mtusi/lab7/prometheus` | Только `/metrics`; business route отсутствует | `make test-tls-matrix` |
|
||||
| API/worker/dispatcher/reconciler → PostgreSQL | `5432`, TLS 1.3 `verify-full` + client cert | SAN `postgres`; отдельные API, worker, dispatcher, reconciler certs | Разные DB roles/grants; migrator отдельный one-shot | `infra/security/test-dependency-matrix.sh` |
|
||||
| API → Valkey | gateway `16379`, mTLS; backend TLS | SAN `redis-gateway`; API URI SAN; Valkey ACL user `messenger_api` | Только session/rate-limit prefixes; password приходит из OpenBao | dependency matrix: разрешён session key, запрещён arbitrary key |
|
||||
| Dispatcher → Redis Streams | gateway `26379`, mTLS; backend TLS | Dispatcher URI SAN | Producer ACL без consume | dependency matrix: `XADD` разрешён, `XREAD` запрещён |
|
||||
| Worker → Redis Streams | gateway `36379`, mTLS; backend TLS | Worker URI SAN | Consumer/ack ACL, unrelated stream запрещён; credential ротируется | dependency matrix и `make test-worker-compromise` |
|
||||
| API/worker/reconciler → MinIO | `9000`, HTTPS | Internal CA, SAN `s3` | API: staging; worker: staging+attachments; reconciler: cleanup; разные keys | dependency matrix проверяет разрешённые и запрещённые prefixes |
|
||||
| API/worker/dispatcher/reconciler → OTel gateway | `4317/4318`, mTLS | Internal CA; exact allowlist service URI SAN | Только telemetry ingest | dependency matrix отклоняет same-CA Prometheus identity |
|
||||
| OTel Collector → Loki/Tempo/Prometheus | локальный TLS gateway → backend network | Collector identity и Internal CA | Backend ports не опубликованы; plaintext hop только внутри выделенной `telemetry_storage` network | `infra/otel/collector.yaml`, Prometheus targets, TLS matrix |
|
||||
| Grafana → Prometheus/Loki/Tempo | отдельные HTTPS gateways | Grafana client certificate, Internal CA, exact URI SAN | Read-only datasource access; UI опубликован только на `127.0.0.1:3000` по HTTPS | provisioned datasource health API и gateway config |
|
||||
| OpenBao Agent API/worker → OpenBao | `8200`, TLS 1.3 | OpenBao SAN; AppRole RoleID+SecretID с TTL 1 час | Раздельные KV policies; renewable token; rendered file `0600` в защищённом volume, Agent token в tmpfs | `scripts/openbao-test-policy.sh`, `scripts/openbao-rotate.sh` |
|
||||
| Оператор → OpenBao | `127.0.0.1:8200`, TLS 1.3 | CA; root token только в operator runtime file | Init/unseal/bootstrap/rotation; Raft volume persistent | `scripts/bootstrap-local.sh`, `infra/openbao/README.md` |
|
||||
| Оператор → Grafana | `127.0.0.1:3000`, HTTPS | Internal CA, login/password | Административный UI не слушает внешний интерфейс | `docker compose port grafana 3000` и HTTPS health |
|
||||
| Replication/cluster channels | N/A | PostgreSQL, Valkey, Redis, MinIO и OpenBao запущены по одному экземпляру | Репликация не заявлена; отказ зависимости обнаруживается readiness | Документированное ограничение стенда |
|
||||
|
||||
| Связь | Минимум на 3 | Дополнительно на 4 | Доказательство / конфиг |
|
||||
| --- | --- | --- | --- |
|
||||
| Клиент → ingress/LB | TLS, CA + hostname, Bearer | Проверка expiry/revocation сессии и общего login rate limit | TODO |
|
||||
| LB → API (все реплики) | mTLS + allowlist LB identity | Ротация leaf без обхода проверки | TODO |
|
||||
| API/worker → PostgreSQL | TLS verify-full + раздельные ограниченные DB roles | Ротация credentials; mTLS — допустимое усиление | TODO |
|
||||
| API/worker → S3 | TLS + разные scoped credentials/prefix rights | Проверка denied prefix и ротация | TODO |
|
||||
| API/worker → broker | TLS + разные producer/consumer identities и ACL | mTLS + ограничение допустимых SAN/ролей | TODO |
|
||||
| API/worker → Collector | TLS; сеть/аутентификация запрещает произвольный ingest | mTLS с раздельными identities | TODO |
|
||||
| Collector → telemetry backends | TLS + scoped backend credentials или mTLS | Ротация и проверка least privilege | TODO |
|
||||
| Grafana → data sources | TLS с проверкой сервера, ограниченная identity | Отрицательные проверки доступа | TODO |
|
||||
| API/worker/agent → OpenBao | TLS + сервисный auth + scoped policy | Renewal/expiry/revocation | TODO |
|
||||
| Оператор → OpenBao/Grafana/S3 admin | TLS + authentication, доступ локальный/через защищённый канал | Разделение operator/application roles | TODO |
|
||||
| API → Redis/Valkey, если используется | TLS + ACL/пароль с минимальными правами | Ротация; mTLS по возможности | TODO |
|
||||
| Replication/кластерные каналы, если добавлены | TLS/mTLS либо подтверждённый шифрованный transport | Проверка заявленной модели отказа | TODO |
|
||||
## Каналы bootstrap, health и scrape
|
||||
|
||||
Нет выбранного компонента — отметьте N/A с причиной, а не оставляйте пустую строку. Внутренний публичный health listener, если нужен, минимален и изолирован; через него нельзя обращаться к бизнес-API без mTLS. Отдельно задокументируйте scrape/health/bootstrap каналы: они тоже не должны создавать обход.
|
||||
|
||||
## Обязательные отрицательные проверки
|
||||
|
||||
1. HTTPS с правильным CA/hostname проходит; с пустым/чужим CA и неправильным hostname — ошибка верификации, не «сервер недоступен».
|
||||
2. LB→API: без client cert, с cert чужого CA, с запрещённой identity от доверенного CA — отказ; с разрешённой LB identity — успех.
|
||||
3. На 4 повторите набор для broker и Collector; ограничение на network policy не заменяет проверку identity.
|
||||
4. Пользователь вне чата не читает metadata/content и не прикрепляет чужой файл; поддельный X-User-Id не меняет Bearer identity.
|
||||
5. OpenBao app policy читает только свой secret/path; запись policy и чтение чужого path запрещены. Worker не получает admin credentials API/БД/S3.
|
||||
6. На 4 отзовите secret/cert и дождитесь контролируемого эффекта с учётом действующих соединений, TLS resumption и cache/TTL. Покажите новое подключение, иначе тест отзыва может ничего не проверить.
|
||||
|
||||
## Bootstrap и эксплуатация
|
||||
|
||||
В репозитории только открытые конфиги, policy templates и scripts. CA private key, leaf keys, root token, unseal shares и SecretID не коммитятся, не запекаются в образ и не печатаются в отчёте. Для локального стенда оператор создаёт их вне Git и передаёт через files/secrets с ограниченными правами. Опишите, кто имеет доступ к CA/unseal, что нужно после рестарта OpenBao и как восстановить работу.
|
||||
|
||||
OpenBao не использует dev-mode. Выберите persistent storage и auth метод (например AppRole с короткоживущим SecretID/response wrapping, cert auth или иная обоснованная identity). Не подменяйте root token постоянной переменной BAO_TOKEN в приложении. Рабочие tokens имеют policy, TTL и renewal/re-auth сценарий. Статические KV secrets на 3 допустимы при реальном чтении через сервисную identity; динамические DB credentials — хорошее расширение, но не обязательное название технологии.
|
||||
|
||||
Разрешённый компромисс учебного стенда: ручной operator init/unseal, если он безопасно документирован и не нужен при каждом обычном рестарте API. Автоматическое хранение всех unseal shares рядом с OpenBao без защиты не считается решённым bootstrap.
|
||||
- `127.0.0.1:8082` существует только внутри каждого app container и используется Docker healthcheck; business router на нём отсутствует.
|
||||
- Публичный `/health/ready` проходит через ingress TLS, но не раскрывает credentials или содержимое зависимостей.
|
||||
- Metrics listener требует Prometheus URI SAN; сертификат LB и сертификаты приложений отклоняются.
|
||||
- Bootstrap jobs подключены только к нужным внутренним сетям, не публикуют порты и завершаются после установки ACL/roles/secrets.
|
||||
- Private keys, root token, unseal shares, RoleID/SecretID и rendered secrets хранятся в `.runtime`/Docker volumes и отсутствуют в Git.
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
services:
|
||||
api:
|
||||
image: ${API_IMAGE:?API_IMAGE is required}
|
||||
|
||||
migrate:
|
||||
image: ${API_IMAGE:?API_IMAGE is required}
|
||||
|
||||
dispatcher:
|
||||
image: ${API_IMAGE:?API_IMAGE is required}
|
||||
|
||||
worker:
|
||||
image: ${API_IMAGE:?API_IMAGE is required}
|
||||
|
||||
nginx:
|
||||
image: ${NGINX_IMAGE:?NGINX_IMAGE is required}
|
||||
|
||||
swagger:
|
||||
image: ${SWAGGER_IMAGE:?SWAGGER_IMAGE is required}
|
||||
|
||||
prometheus:
|
||||
image: ${PROMETHEUS_IMAGE:?PROMETHEUS_IMAGE is required}
|
||||
|
||||
grafana:
|
||||
image: ${GRAFANA_IMAGE:?GRAFANA_IMAGE is required}
|
||||
|
||||
s3:
|
||||
image: ${S3_IMAGE:?S3_IMAGE is required}
|
||||
|
||||
s3-bootstrap:
|
||||
image: ${S3_BOOTSTRAP_IMAGE:?S3_BOOTSTRAP_IMAGE is required}
|
||||
@@ -0,0 +1,4 @@
|
||||
services:
|
||||
nginx:
|
||||
volumes:
|
||||
- ${NGINX_UPSTREAM_FILE:?NGINX_UPSTREAM_FILE is required}:/etc/nginx/conf.d/upstream.conf:ro
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"environment": {
|
||||
"os": "linux",
|
||||
"architecture": "arm64",
|
||||
"cpu_limit": 2,
|
||||
"memory_limit_mib": 512,
|
||||
"go_version": "1.26.5"
|
||||
},
|
||||
"parameters": {
|
||||
"memory_kib": 65536,
|
||||
"iterations": 3,
|
||||
"parallelism": 2,
|
||||
"salt_length": 16,
|
||||
"key_length": 32
|
||||
},
|
||||
"method": "go test benchmark, 5 iterations per operation",
|
||||
"hash": {
|
||||
"nanoseconds_per_operation": 60029195,
|
||||
"bytes_per_operation": 67114704,
|
||||
"allocations_per_operation": 59
|
||||
},
|
||||
"verify": {
|
||||
"nanoseconds_per_operation": 64816424,
|
||||
"bytes_per_operation": 67113590,
|
||||
"allocations_per_operation": 53
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"generated_at": "2026-11-06T18:18:28.005396+00:00",
|
||||
"scope": "local runtime checks on the lab7 Compose stack; remote CI is separate",
|
||||
"public_https_smoke": "PASS",
|
||||
"public_and_api_mtls_matrix_cases": 13,
|
||||
"dependency_mtls_and_acl_matrix": "PASS",
|
||||
"openbao_policy_allow_deny_and_renewal": "PASS",
|
||||
"openbao_expiry_revoke_seal_and_reauth": "PASS",
|
||||
"leaf_rotation_old_identity_and_expired_leaf_denied": "PASS",
|
||||
"shared_login_rate_limit_across_two_replicas": "PASS",
|
||||
"postgres_and_valkey_integration": "PASS",
|
||||
"go_tests_and_vet": "PASS",
|
||||
"grafana_datasource_health": {
|
||||
"prometheus": "OK",
|
||||
"loki": "OK",
|
||||
"tempo": "OK"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,331 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"scanner": "Trivy 0.76.2",
|
||||
"generated_at": "2026-11-06T18:17:33+00:00",
|
||||
"scope": "repository filesystem: vulnerabilities, misconfigurations, secrets",
|
||||
"vulnerabilities": {
|
||||
"UNKNOWN": 3
|
||||
},
|
||||
"misconfigurations": {
|
||||
"LOW": 2
|
||||
},
|
||||
"secret_findings": 0,
|
||||
"scanned_images": [
|
||||
"envoyproxy/envoy:v1.35.2",
|
||||
"grafana/loki:3.7.9",
|
||||
"grafana/tempo:2.10.10",
|
||||
"mtusi-grafana:lab7",
|
||||
"mtusi-messenger:lab7",
|
||||
"mtusi-minio:RELEASE.2025-10-15T17-29-55Z",
|
||||
"mtusi-nginx:lab7",
|
||||
"mtusi-prometheus:lab7",
|
||||
"mtusi-swagger:lab7",
|
||||
"nginx/nginx-prometheus-exporter:1.5.1",
|
||||
"oliver006/redis_exporter:v1.89.0-alpine",
|
||||
"openbao/openbao:2.7.1",
|
||||
"otel/opentelemetry-collector-contrib:0.160.0",
|
||||
"postgres:17.6-alpine3.22",
|
||||
"quay.io/prometheuscommunity/postgres-exporter:v0.20.1",
|
||||
"redis:8.2.2-alpine",
|
||||
"valkey/valkey:8.1.4-alpine"
|
||||
],
|
||||
"image_vulnerabilities": {
|
||||
"CRITICAL": 26,
|
||||
"HIGH": 479,
|
||||
"LOW": 508,
|
||||
"MEDIUM": 795,
|
||||
"UNKNOWN": 92
|
||||
},
|
||||
"image_secret_findings": 0,
|
||||
"findings": [
|
||||
{
|
||||
"id": "CVE-2026-111497",
|
||||
"package": "brotli-libs",
|
||||
"installed_version": "1.2.0-r1",
|
||||
"fixed_version": "1.2.1-r0",
|
||||
"severity": "CRITICAL",
|
||||
"title": "brotli: Out-of-Bounds Write in Google Brotli Decoder",
|
||||
"scopes": [
|
||||
"mtusi-grafana:lab7"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-111497",
|
||||
"package": "brotli-libs",
|
||||
"installed_version": "1.2.0-r0",
|
||||
"fixed_version": "1.2.1-r0",
|
||||
"severity": "CRITICAL",
|
||||
"title": "brotli: Out-of-Bounds Write in Google Brotli Decoder",
|
||||
"scopes": [
|
||||
"mtusi-swagger:lab7"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-33322",
|
||||
"package": "github.com/minio/minio",
|
||||
"installed_version": "v0.0.0-20251015172955-9e49d5e7a648",
|
||||
"fixed_version": "",
|
||||
"severity": "CRITICAL",
|
||||
"title": "MinIO has JWT Algorithm Confusion in OIDC Authentication",
|
||||
"scopes": [
|
||||
"mtusi-minio:RELEASE.2025-10-15T17-29-55Z"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-33419",
|
||||
"package": "github.com/minio/minio",
|
||||
"installed_version": "v0.0.0-20251015172955-9e49d5e7a648",
|
||||
"fixed_version": "",
|
||||
"severity": "CRITICAL",
|
||||
"title": "MinIO LDAP login brute-force via user enumeration and missing rate limit",
|
||||
"scopes": [
|
||||
"mtusi-minio:RELEASE.2025-10-15T17-29-55Z"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-77405",
|
||||
"package": "github.com/rabbitmq/amqp091-go",
|
||||
"installed_version": "v1.10.0",
|
||||
"fixed_version": "1.13.0",
|
||||
"severity": "CRITICAL",
|
||||
"title": "github.com/rabbitmq/amqp091-go: RabbitMQ amqp091-go: Missing Explicit TLS Minimum Version Configuration In URI Parser",
|
||||
"scopes": [
|
||||
"mtusi-minio:RELEASE.2025-10-15T17-29-55Z"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-77405",
|
||||
"package": "github.com/rabbitmq/amqp091-go",
|
||||
"installed_version": "v1.12.0",
|
||||
"fixed_version": "1.13.0",
|
||||
"severity": "CRITICAL",
|
||||
"title": "github.com/rabbitmq/amqp091-go: RabbitMQ amqp091-go: Missing Explicit TLS Minimum Version Configuration In URI Parser",
|
||||
"scopes": [
|
||||
"otel/opentelemetry-collector-contrib:0.160.0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-77408",
|
||||
"package": "github.com/rabbitmq/amqp091-go",
|
||||
"installed_version": "v1.10.0",
|
||||
"fixed_version": "1.13.0",
|
||||
"severity": "CRITICAL",
|
||||
"title": "github.com/rabbitmq/amqp091-go: RabbitMQ amqp091-go: Silent Data Truncation and State Corruption via Shortstr Integer Overflow",
|
||||
"scopes": [
|
||||
"mtusi-minio:RELEASE.2025-10-15T17-29-55Z"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-77408",
|
||||
"package": "github.com/rabbitmq/amqp091-go",
|
||||
"installed_version": "v1.12.0",
|
||||
"fixed_version": "1.13.0",
|
||||
"severity": "CRITICAL",
|
||||
"title": "github.com/rabbitmq/amqp091-go: RabbitMQ amqp091-go: Silent Data Truncation and State Corruption via Shortstr Integer Overflow",
|
||||
"scopes": [
|
||||
"otel/opentelemetry-collector-contrib:0.160.0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-77411",
|
||||
"package": "github.com/rabbitmq/amqp091-go",
|
||||
"installed_version": "v1.10.0",
|
||||
"fixed_version": "1.13.0",
|
||||
"severity": "CRITICAL",
|
||||
"title": "github.com/rabbitmq/amqp091-go: RabbitMQ amqp091-go: Protocol Desynchronization and Frame Injection via Integer Overflow in readLongstr",
|
||||
"scopes": [
|
||||
"mtusi-minio:RELEASE.2025-10-15T17-29-55Z"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-77411",
|
||||
"package": "github.com/rabbitmq/amqp091-go",
|
||||
"installed_version": "v1.12.0",
|
||||
"fixed_version": "1.13.0",
|
||||
"severity": "CRITICAL",
|
||||
"title": "github.com/rabbitmq/amqp091-go: RabbitMQ amqp091-go: Protocol Desynchronization and Frame Injection via Integer Overflow in readLongstr",
|
||||
"scopes": [
|
||||
"otel/opentelemetry-collector-contrib:0.160.0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "GO-2026-5932",
|
||||
"package": "golang.org/x/crypto",
|
||||
"installed_version": "v0.57.0",
|
||||
"fixed_version": "0.58.1-0.20261009181718-2f1f834330b2",
|
||||
"severity": "UNKNOWN",
|
||||
"title": "The golang.org/x/crypto/openpgp package is unmaintained, unsafe by design, and has known security issues",
|
||||
"scopes": [
|
||||
"repository",
|
||||
"grafana/tempo:2.10.10",
|
||||
"mtusi-grafana:lab7",
|
||||
"mtusi-messenger:lab7"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-78661",
|
||||
"package": "golang.org/x/net",
|
||||
"installed_version": "v0.60.0",
|
||||
"fixed_version": "0.62.0",
|
||||
"severity": "UNKNOWN",
|
||||
"title": "When coalescing adjacent text nodes in addText and fosterParent, appen ...",
|
||||
"scopes": [
|
||||
"repository",
|
||||
"grafana/tempo:2.10.10",
|
||||
"mtusi-grafana:lab7",
|
||||
"mtusi-messenger:lab7"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-97045",
|
||||
"package": "golang.org/x/net",
|
||||
"installed_version": "v0.60.0",
|
||||
"fixed_version": "0.62.0",
|
||||
"severity": "UNKNOWN",
|
||||
"title": "In childTextNodesAreLiteral, walking up the tree previously stopped at ...",
|
||||
"scopes": [
|
||||
"repository",
|
||||
"grafana/tempo:2.10.10",
|
||||
"mtusi-grafana:lab7",
|
||||
"mtusi-messenger:lab7"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-33186",
|
||||
"package": "google.golang.org/grpc",
|
||||
"installed_version": "v1.72.0",
|
||||
"fixed_version": "1.79.3",
|
||||
"severity": "CRITICAL",
|
||||
"title": "google.golang.org/grpc/grpc-go: google.golang.org/grpc/authz: gRPC-Go: Authorization bypass due to improper HTTP/2 path validation",
|
||||
"scopes": [
|
||||
"mtusi-minio:RELEASE.2025-10-15T17-29-55Z"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-31789",
|
||||
"package": "libcrypto3",
|
||||
"installed_version": "3.5.4-r0",
|
||||
"fixed_version": "3.5.6-r0",
|
||||
"severity": "CRITICAL",
|
||||
"title": "openssl: OpenSSL: Heap buffer overflow on 32-bit systems from large X.509 certificate processing",
|
||||
"scopes": [
|
||||
"mtusi-minio:RELEASE.2025-10-15T17-29-55Z",
|
||||
"postgres:17.6-alpine3.22",
|
||||
"redis:8.2.2-alpine",
|
||||
"valkey/valkey:8.1.4-alpine"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-31789",
|
||||
"package": "libcrypto3",
|
||||
"installed_version": "3.5.5-r0",
|
||||
"fixed_version": "3.5.6-r0",
|
||||
"severity": "CRITICAL",
|
||||
"title": "openssl: OpenSSL: Heap buffer overflow on 32-bit systems from large X.509 certificate processing",
|
||||
"scopes": [
|
||||
"mtusi-swagger:lab7"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-31789",
|
||||
"package": "libssl3",
|
||||
"installed_version": "3.5.4-r0",
|
||||
"fixed_version": "3.5.6-r0",
|
||||
"severity": "CRITICAL",
|
||||
"title": "openssl: OpenSSL: Heap buffer overflow on 32-bit systems from large X.509 certificate processing",
|
||||
"scopes": [
|
||||
"mtusi-minio:RELEASE.2025-10-15T17-29-55Z",
|
||||
"postgres:17.6-alpine3.22",
|
||||
"redis:8.2.2-alpine",
|
||||
"valkey/valkey:8.1.4-alpine"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-31789",
|
||||
"package": "libssl3",
|
||||
"installed_version": "3.5.5-r0",
|
||||
"fixed_version": "3.5.6-r0",
|
||||
"severity": "CRITICAL",
|
||||
"title": "openssl: OpenSSL: Heap buffer overflow on 32-bit systems from large X.509 certificate processing",
|
||||
"scopes": [
|
||||
"mtusi-swagger:lab7"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-48930",
|
||||
"package": "nodejs",
|
||||
"installed_version": "24.13.0-r1",
|
||||
"fixed_version": "24.17.0-r0",
|
||||
"severity": "CRITICAL",
|
||||
"title": "nodejs: Node.js: Silent authority rebinding due to embedded-nul hostnames in TLS handling",
|
||||
"scopes": [
|
||||
"mtusi-swagger:lab7"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2026-31789",
|
||||
"package": "openssl",
|
||||
"installed_version": "3.5.4-r0",
|
||||
"fixed_version": "3.5.6-r0",
|
||||
"severity": "CRITICAL",
|
||||
"title": "openssl: OpenSSL: Heap buffer overflow on 32-bit systems from large X.509 certificate processing",
|
||||
"scopes": [
|
||||
"valkey/valkey:8.1.4-alpine"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2025-68121",
|
||||
"package": "stdlib",
|
||||
"installed_version": "v1.24.8",
|
||||
"fixed_version": "1.24.13, 1.25.7, 1.26.0-rc.3",
|
||||
"severity": "CRITICAL",
|
||||
"title": "crypto/tls: crypto/tls: Incorrect certificate validation during TLS session resumption",
|
||||
"scopes": [
|
||||
"mtusi-minio:RELEASE.2025-10-15T17-29-55Z"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2025-68121",
|
||||
"package": "stdlib",
|
||||
"installed_version": "v1.25.3",
|
||||
"fixed_version": "1.24.13, 1.25.7, 1.26.0-rc.3",
|
||||
"severity": "CRITICAL",
|
||||
"title": "crypto/tls: crypto/tls: Incorrect certificate validation during TLS session resumption",
|
||||
"scopes": [
|
||||
"nginx/nginx-prometheus-exporter:1.5.1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "CVE-2025-68121",
|
||||
"package": "stdlib",
|
||||
"installed_version": "v1.24.6",
|
||||
"fixed_version": "1.24.13, 1.25.7, 1.26.0-rc.3",
|
||||
"severity": "CRITICAL",
|
||||
"title": "crypto/tls: crypto/tls: Incorrect certificate validation during TLS session resumption",
|
||||
"scopes": [
|
||||
"postgres:17.6-alpine3.22"
|
||||
]
|
||||
}
|
||||
],
|
||||
"findings_scope": "all repository findings and critical image findings; full findings in raw reports",
|
||||
"raw_report": "evidence/runtime/security-scan/trivy-fs.json",
|
||||
"image_raw_reports": [
|
||||
"evidence/runtime/security-scan/trivy-image-1.json",
|
||||
"evidence/runtime/security-scan/trivy-image-2.json",
|
||||
"evidence/runtime/security-scan/trivy-image-3.json",
|
||||
"evidence/runtime/security-scan/trivy-image-4.json",
|
||||
"evidence/runtime/security-scan/trivy-image-5.json",
|
||||
"evidence/runtime/security-scan/trivy-image-6.json",
|
||||
"evidence/runtime/security-scan/trivy-image-7.json",
|
||||
"evidence/runtime/security-scan/trivy-image-8.json",
|
||||
"evidence/runtime/security-scan/trivy-image-9.json",
|
||||
"evidence/runtime/security-scan/trivy-image-10.json",
|
||||
"evidence/runtime/security-scan/trivy-image-11.json",
|
||||
"evidence/runtime/security-scan/trivy-image-12.json",
|
||||
"evidence/runtime/security-scan/trivy-image-13.json",
|
||||
"evidence/runtime/security-scan/trivy-image-14.json",
|
||||
"evidence/runtime/security-scan/trivy-image-15.json",
|
||||
"evidence/runtime/security-scan/trivy-image-16.json",
|
||||
"evidence/runtime/security-scan/trivy-image-17.json"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"scenario": "worker credential compromise and rotation",
|
||||
"generated_at": "2026-11-06T18:08:45+00:00",
|
||||
"recovery_seconds": 21,
|
||||
"old_credential_rejected": true,
|
||||
"old_database_credential_rejected": true,
|
||||
"old_s3_credential_rejected": true,
|
||||
"openbao_secret_rotated": true,
|
||||
"queued_attachment_preserved": true,
|
||||
"result": "PASS"
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
module gitea.privcloud.tech/dizi/mtusi2026lab7
|
||||
|
||||
go 1.26.0
|
||||
|
||||
toolchain go1.26.10
|
||||
|
||||
require (
|
||||
github.com/deepteams/webp v1.2.8
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgx/v5 v5.11.0
|
||||
github.com/minio/minio-go/v7 v7.0.99
|
||||
github.com/prometheus/client_golang v1.23.2
|
||||
github.com/redis/go-redis/v9 v9.17.2
|
||||
github.com/spf13/viper v1.21.0
|
||||
github.com/stretchr/testify v1.11.1
|
||||
github.com/valkey-io/valkey-go v1.0.78
|
||||
go.opentelemetry.io/contrib/bridges/otelslog v0.20.0
|
||||
go.opentelemetry.io/contrib/instrumentation/runtime v0.60.0
|
||||
go.opentelemetry.io/otel v1.45.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.21.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.45.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.45.0
|
||||
go.opentelemetry.io/otel/exporters/prometheus v0.60.0
|
||||
go.opentelemetry.io/otel/metric v1.45.0
|
||||
go.opentelemetry.io/otel/sdk v1.45.0
|
||||
go.opentelemetry.io/otel/sdk/log v0.21.0
|
||||
go.opentelemetry.io/otel/sdk/metric v1.45.0
|
||||
go.opentelemetry.io/otel/trace v1.45.0
|
||||
golang.org/x/crypto v0.57.0
|
||||
golang.org/x/sync v0.23.0
|
||||
google.golang.org/grpc v1.83.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/go-logr/logr v1.4.4 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
||||
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.29.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/klauspost/compress v1.18.7 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.11 // indirect
|
||||
github.com/klauspost/crc32 v1.3.0 // indirect
|
||||
github.com/minio/crc64nvme v1.1.1 // indirect
|
||||
github.com/minio/md5-simd v1.1.2 // indirect
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||
github.com/philhofer/fwd v1.2.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/prometheus/client_model v0.6.2 // indirect
|
||||
github.com/prometheus/common v0.66.1 // indirect
|
||||
github.com/prometheus/otlptranslator v0.0.2 // indirect
|
||||
github.com/prometheus/procfs v0.17.0 // indirect
|
||||
github.com/rs/xid v1.6.0 // indirect
|
||||
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
|
||||
github.com/spf13/afero v1.15.0 // indirect
|
||||
github.com/spf13/cast v1.10.0 // indirect
|
||||
github.com/spf13/pflag v1.0.10 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
github.com/tinylib/msgp v1.6.1 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.45.0 // indirect
|
||||
go.opentelemetry.io/otel/log v0.21.0 // indirect
|
||||
go.opentelemetry.io/proto/otlp v1.11.0 // indirect
|
||||
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
||||
golang.org/x/net v0.60.0 // indirect
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
golang.org/x/text v0.42.0 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260803160001-6ac0973c030d // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260803160001-6ac0973c030d // indirect
|
||||
google.golang.org/protobuf v1.36.11 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
@@ -0,0 +1,186 @@
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||
github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM=
|
||||
github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/deepteams/webp v1.2.8 h1:iFLBxo4V34ixqay6+MTB18zujCM8VSe1ejsYh+ue1nA=
|
||||
github.com/deepteams/webp v1.2.8/go.mod h1:J8Ap+HAixxpKKRN9IpEeSKlfvhsef1v43jKTO7m3f4c=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
|
||||
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
|
||||
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8=
|
||||
github.com/go-logr/logr v1.4.4/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc h1:GN2Lv3MGO7AS6PrRoT6yV5+wkrOpcszoIsO4+4ds248=
|
||||
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc/go.mod h1:+JKpmjMGhpgPL+rXZ5nsZieVzvarn86asRlBg4uNGnk=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.29.0 h1:5VipnvEpbqr2gA2VbM+nYVbkIF28c5ZQfqCBQ5g2xfk=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.29.0/go.mod h1:Hyl3n6Twe1hvtd9XUXDec4pTvgMSEixRuQKPTMH2bNs=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg=
|
||||
github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/klauspost/compress v1.18.7 h1:aUyZsS4kH3QTKurYhAOwAHxllVPnOthb3vPfnF1Ehjw=
|
||||
github.com/klauspost/compress v1.18.7/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
||||
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.11 h1:0OwqZRYI2rFrjS4kvkDnqJkKHdHaRnCm68/DY4OxRzU=
|
||||
github.com/klauspost/cpuid/v2 v2.2.11/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/klauspost/crc32 v1.3.0 h1:sSmTt3gUt81RP655XGZPElI0PelVTZ6YwCRnPSupoFM=
|
||||
github.com/klauspost/crc32 v1.3.0/go.mod h1:D7kQaZhnkX/Y0tstFGf8VUzv2UofNGqCjnC3zdHB0Hw=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||
github.com/minio/crc64nvme v1.1.1 h1:8dwx/Pz49suywbO+auHCBpCtlW1OfpcLN7wYgVR6wAI=
|
||||
github.com/minio/crc64nvme v1.1.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg=
|
||||
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
|
||||
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
|
||||
github.com/minio/minio-go/v7 v7.0.99 h1:2vH/byrwUkIpFQFOilvTfaUpvAX3fEFhEzO+DR3DlCE=
|
||||
github.com/minio/minio-go/v7 v7.0.99/go.mod h1:EtGNKtlX20iL2yaYnxEigaIvj0G0GwSDnifnG8ClIdw=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/onsi/gomega v1.42.1 h1:iN1rCUX+44NZ1Dc97MPoeFYbFR0vh8zxoxMFwKdyZ6I=
|
||||
github.com/onsi/gomega v1.42.1/go.mod h1:REff/hsDsodHoKlWsP2mAPhu1+5/6hVYNf9rIEBpeSg=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
|
||||
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o=
|
||||
github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg=
|
||||
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
|
||||
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
|
||||
github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9ZoGs=
|
||||
github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA=
|
||||
github.com/prometheus/otlptranslator v0.0.2 h1:+1CdeLVrRQ6Psmhnobldo0kTp96Rj80DRXRd5OSnMEQ=
|
||||
github.com/prometheus/otlptranslator v0.0.2/go.mod h1:P8AwMgdD7XEr6QRUJ2QWLpiAZTgTE2UYgjlu3svompI=
|
||||
github.com/prometheus/procfs v0.17.0 h1:FuLQ+05u4ZI+SS/w9+BWEM2TXiHKsUQ9TADiRH7DuK0=
|
||||
github.com/prometheus/procfs v0.17.0/go.mod h1:oPQLaDAMRbA+u8H5Pbfq+dl3VDAvHxMUOVhe0wYB2zw=
|
||||
github.com/redis/go-redis/v9 v9.17.2 h1:P2EGsA4qVIM3Pp+aPocCJ7DguDHhqrXNhVcEp4ViluI=
|
||||
github.com/redis/go-redis/v9 v9.17.2/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=
|
||||
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
||||
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
||||
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
|
||||
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8/go.mod h1:3n1Cwaq1E1/1lhQhtRK2ts/ZwZEhjcQeJQ1RuC6Q/8U=
|
||||
github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I=
|
||||
github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg=
|
||||
github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
|
||||
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
|
||||
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
|
||||
github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||
github.com/tinylib/msgp v1.6.1 h1:ESRv8eL3u+DNHUoSAAQRE50Hm162zqAnBoGv9PzScPY=
|
||||
github.com/tinylib/msgp v1.6.1/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
|
||||
github.com/valkey-io/valkey-go v1.0.78 h1:1Ce8hRaaNXpKfqmqZQRhnbVh1eW8MGR873pdXVvE4y8=
|
||||
github.com/valkey-io/valkey-go v1.0.78/go.mod h1:gvC/r2m3eW4Hbj0YnjogTzNtFdPSM/D+NCqen+5OABM=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
|
||||
go.opentelemetry.io/contrib/bridges/otelslog v0.20.0 h1:oEl2Pw/i4OQwhAuda2pAHFAcOMivA+Xa+iTccBfab/g=
|
||||
go.opentelemetry.io/contrib/bridges/otelslog v0.20.0/go.mod h1:yMSQaiiq5dpfrSJCYLBcqFeJkFFI67seT4ngvx6jfVo=
|
||||
go.opentelemetry.io/contrib/instrumentation/runtime v0.60.0 h1:0NgN/3SYkqYJ9NBlDfl/2lzVlwos/YQLvi8sUrzJRBE=
|
||||
go.opentelemetry.io/contrib/instrumentation/runtime v0.60.0/go.mod h1:oxpUfhTkhgQaYIjtBt3T3w135dLoxq//qo3WPlPIKkE=
|
||||
go.opentelemetry.io/otel v1.45.0 h1:pdrWmLHofpubmArBv1LgFSv1Z0Ie/ppdZzu+kUN5EeU=
|
||||
go.opentelemetry.io/otel v1.45.0/go.mod h1:XZxIqPapzEYnhNSScF5DIqXhm/rYi0FzCe2XddAwZfQ=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.21.0 h1:WseeVYf5dJZTsyPiyW5L14k5qsSibqXAMTSiFEDiWr0=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.21.0/go.mod h1:SiLZnQS6Qk2eCpvr2CH/XMAOa64TWGXxEZJZCpD2Lmc=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.45.0 h1:klTViGcsvLCd1xN3rZzfZ12NslC/OimbmR+k+A006RI=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.45.0/go.mod h1:jRsK04CWmXuY8A0O+wMpSf+t90RHZ53o5Qmxn2PQPfk=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.45.0 h1:QRefszxJmfPdjXUUm3j6iDzY03mTPXMjqErFqQ67vUg=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.45.0/go.mod h1:Tiz03lTBVBrm7eWZBOidzEaYaJa8tjwGUGv6d8mlTyk=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.45.0 h1:fG5MCxGz8+2VtrN/WgqSpJFctVz24gpxj8CxkKmc8Ww=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.45.0/go.mod h1:BmAYTn+3ysbRe+IU2msxmf5Rx3g6DHvex+tWI3LdhYI=
|
||||
go.opentelemetry.io/otel/exporters/prometheus v0.60.0 h1:cGtQxGvZbnrWdC2GyjZi0PDKVSLWP/Jocix3QWfXtbo=
|
||||
go.opentelemetry.io/otel/exporters/prometheus v0.60.0/go.mod h1:hkd1EekxNo69PTV4OWFGZcKQiIqg0RfuWExcPKFvepk=
|
||||
go.opentelemetry.io/otel/log v0.21.0 h1:SLsVDGmtyBrdw8/a2Z0bOIxou/+bN4z56GebH7T0LvA=
|
||||
go.opentelemetry.io/otel/log v0.21.0/go.mod h1:iReetQrZL9Wyg84cCkOoCmqDHS5RCFfyxC7J+r8fn8g=
|
||||
go.opentelemetry.io/otel/metric v1.45.0 h1:7Eg1uH7CJ5cXv9is6tnBe1FI6rj1nwUdbFypRm3br/M=
|
||||
go.opentelemetry.io/otel/metric v1.45.0/go.mod h1:HAPbm1nd3p1PmFH7v2dR+6BjXxw+Lq4a2+pndMAm08s=
|
||||
go.opentelemetry.io/otel/metric/x v0.67.0 h1:PcicCNZFkZ4bXfSooXdo3WN7RBOVOtjVdo1wD358Uns=
|
||||
go.opentelemetry.io/otel/metric/x v0.67.0/go.mod h1:FBjCWZe6wgcqxcMtjdGiClDKXb2YxxXii0CXftE4QtI=
|
||||
go.opentelemetry.io/otel/sdk v1.45.0 h1:4VVSMgQ83dUgW2aoX5f6JgLvHwIvzcuLnF9lUdCSpCw=
|
||||
go.opentelemetry.io/otel/sdk v1.45.0/go.mod h1:Sr40LgXV7DsKMMJMKOhUWOgMWTfAaqvm2kF0g7ilwuA=
|
||||
go.opentelemetry.io/otel/sdk/log v0.21.0 h1:QsE7XSR0ktQdKmRKGnR+f1ObGF32WG+7MER/P9KgmYc=
|
||||
go.opentelemetry.io/otel/sdk/log v0.21.0/go.mod h1:m9mApjCoD2/1QuKCAptjv+BrG9WKOvQLVdNx+iBldTo=
|
||||
go.opentelemetry.io/otel/sdk/log/logtest v0.21.0 h1:X+JBBgKlswCGYsmgL0CnoUUtlE//VB345c84jYAYkdQ=
|
||||
go.opentelemetry.io/otel/sdk/log/logtest v0.21.0/go.mod h1:HD1575K8e6sIFBBDd5tZB3t9DlMytWXq9FuR+Y4rfjE=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.45.0 h1:oVFszMfyj1Am6s24Vtc7wBb8BKLcwepJjNEYILuiE3o=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.45.0/go.mod h1:vUWUxDZvu1WVRj8JA8S0AdhsPrZoDpA2DdZauIh4mDA=
|
||||
go.opentelemetry.io/otel/trace v1.45.0 h1:l/mP6Uv7oNO7/TblbhpbgMidxhq1uO/rPsikOyVhxag=
|
||||
go.opentelemetry.io/otel/trace v1.45.0/go.mod h1:qoJJA2xNMnxRrdISU/kLtfUH2wNeQbiv+jhs/CxI8bc=
|
||||
go.opentelemetry.io/proto/otlp v1.11.0 h1:5rrYs0Ykyj50sdU/JU0x8etU+LubXWb+gED6TbEdMIk=
|
||||
go.opentelemetry.io/proto/otlp v1.11.0/go.mod h1:SmVizdCOAm3XBtG1g1NnOdhW6jtddT72hLMhv8VwA8E=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
|
||||
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
||||
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
||||
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
||||
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
||||
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
||||
golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU=
|
||||
golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
|
||||
golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
|
||||
golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
|
||||
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
|
||||
gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4=
|
||||
gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260803160001-6ac0973c030d h1:FarXi840EJWSHYTN3ERkADbPWjl307+FGrA22KAVjjc=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260803160001-6ac0973c030d/go.mod h1:K/+WGbmBY7aNW1HDw1fJnKYo10i0DkAX6pows00dLig=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260803160001-6ac0973c030d h1:IL4hdHzcUv2l/gcg98/Rj3FbtE6axwqslOW8SW0C+S0=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260803160001-6ac0973c030d/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
|
||||
google.golang.org/grpc v1.83.2 h1:EManeRomTObA0BU7I8vXgg/78uE5MJ9M8B39EX2WscU=
|
||||
google.golang.org/grpc v1.83.2/go.mod h1:YPI1hK3kDked6iHvgX3tR0y+nX/qpMFKhPgFsokw1S8=
|
||||
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
|
||||
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -0,0 +1,48 @@
|
||||
auth_enabled: false
|
||||
|
||||
server:
|
||||
http_listen_address: 0.0.0.0
|
||||
http_listen_port: 3100
|
||||
grpc_listen_address: 0.0.0.0
|
||||
grpc_listen_port: 9096
|
||||
|
||||
common:
|
||||
path_prefix: /loki
|
||||
replication_factor: 1
|
||||
ring:
|
||||
kvstore:
|
||||
store: inmemory
|
||||
storage:
|
||||
filesystem:
|
||||
chunks_directory: /loki/chunks
|
||||
rules_directory: /loki/rules
|
||||
|
||||
schema_config:
|
||||
configs:
|
||||
- from: 2024-01-01
|
||||
store: tsdb
|
||||
object_store: filesystem
|
||||
schema: v13
|
||||
index:
|
||||
prefix: index_
|
||||
period: 24h
|
||||
|
||||
limits_config:
|
||||
allow_structured_metadata: true
|
||||
retention_period: 168h
|
||||
volume_enabled: true
|
||||
otlp_config:
|
||||
resource_attributes:
|
||||
attributes_config:
|
||||
- action: index_label
|
||||
attributes:
|
||||
- service.name
|
||||
- deployment.environment.name
|
||||
|
||||
compactor:
|
||||
working_directory: /loki/compactor
|
||||
retention_enabled: true
|
||||
delete_request_store: filesystem
|
||||
|
||||
analytics:
|
||||
reporting_enabled: false
|
||||
Executable
+125
@@ -0,0 +1,125 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
: "${MINIO_ROOT_USER:?MINIO_ROOT_USER is required}"
|
||||
: "${MINIO_ROOT_PASSWORD:?MINIO_ROOT_PASSWORD is required}"
|
||||
: "${S3_BUCKET:?S3_BUCKET is required}"
|
||||
: "${S3_API_ACCESS_KEY:?S3_API_ACCESS_KEY is required}"
|
||||
: "${S3_API_SECRET_KEY:?S3_API_SECRET_KEY is required}"
|
||||
: "${S3_WORKER_ACCESS_KEY:?S3_WORKER_ACCESS_KEY is required}"
|
||||
: "${S3_WORKER_SECRET_KEY:?S3_WORKER_SECRET_KEY is required}"
|
||||
: "${S3_RECONCILER_ACCESS_KEY:?S3_RECONCILER_ACCESS_KEY is required}"
|
||||
: "${S3_RECONCILER_SECRET_KEY:?S3_RECONCILER_SECRET_KEY is required}"
|
||||
|
||||
export SSL_CERT_FILE=${SSL_CERT_FILE:-/run/security/internal-ca/ca.crt}
|
||||
|
||||
alias_name=bootstrap
|
||||
policy_dir=/tmp/minio-policies
|
||||
mkdir -p "$policy_dir"
|
||||
|
||||
mc alias set "$alias_name" https://s3:9000 "$MINIO_ROOT_USER" "$MINIO_ROOT_PASSWORD"
|
||||
mc mb --ignore-existing "$alias_name/$S3_BUCKET"
|
||||
mc anonymous set none "$alias_name/$S3_BUCKET"
|
||||
|
||||
cat >"$policy_dir/messenger-api.json" <<EOF
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:GetBucketLocation"],
|
||||
"Resource": ["arn:aws:s3:::$S3_BUCKET"]
|
||||
},
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:ListBucket"],
|
||||
"Resource": ["arn:aws:s3:::$S3_BUCKET"],
|
||||
"Condition": {"StringLike": {"s3:prefix": ["staging/*", "attachments/*"]}}
|
||||
},
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:GetObject"],
|
||||
"Resource": ["arn:aws:s3:::$S3_BUCKET/attachments/*"]
|
||||
},
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
|
||||
"Resource": ["arn:aws:s3:::$S3_BUCKET/staging/*"]
|
||||
}
|
||||
]
|
||||
}
|
||||
EOF
|
||||
|
||||
cat >"$policy_dir/messenger-worker.json" <<EOF
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:GetBucketLocation"],
|
||||
"Resource": ["arn:aws:s3:::$S3_BUCKET"]
|
||||
},
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:ListBucket"],
|
||||
"Resource": ["arn:aws:s3:::$S3_BUCKET"],
|
||||
"Condition": {"StringLike": {"s3:prefix": ["staging/*", "attachments/*"]}}
|
||||
},
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:GetObject", "s3:DeleteObject"],
|
||||
"Resource": ["arn:aws:s3:::$S3_BUCKET/staging/*"]
|
||||
},
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
|
||||
"Resource": ["arn:aws:s3:::$S3_BUCKET/attachments/*"]
|
||||
}
|
||||
]
|
||||
}
|
||||
EOF
|
||||
|
||||
cat >"$policy_dir/messenger-reconciler.json" <<EOF
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:GetBucketLocation"],
|
||||
"Resource": ["arn:aws:s3:::$S3_BUCKET"]
|
||||
},
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:ListBucket"],
|
||||
"Resource": ["arn:aws:s3:::$S3_BUCKET"],
|
||||
"Condition": {"StringLike": {"s3:prefix": ["staging/*", "attachments/*"]}}
|
||||
},
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:GetObject", "s3:DeleteObject"],
|
||||
"Resource": [
|
||||
"arn:aws:s3:::$S3_BUCKET/staging/*",
|
||||
"arn:aws:s3:::$S3_BUCKET/attachments/*"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
EOF
|
||||
|
||||
create_user() {
|
||||
access_key=$1
|
||||
secret_key=$2
|
||||
policy=$3
|
||||
|
||||
mc admin policy create "$alias_name" "$policy" "$policy_dir/$policy.json"
|
||||
mc admin user add "$alias_name" "$access_key" "$secret_key"
|
||||
mc admin policy attach "$alias_name" "$policy" --user "$access_key"
|
||||
}
|
||||
|
||||
create_user "$S3_API_ACCESS_KEY" "$S3_API_SECRET_KEY" messenger-api
|
||||
create_user "$S3_WORKER_ACCESS_KEY" "$S3_WORKER_SECRET_KEY" messenger-worker
|
||||
create_user "$S3_RECONCILER_ACCESS_KEY" "$S3_RECONCILER_SECRET_KEY" messenger-reconciler
|
||||
|
||||
mc alias set application https://s3:9000 "$S3_API_ACCESS_KEY" "$S3_API_SECRET_KEY"
|
||||
mc ls "application/$S3_BUCKET/staging/" >/dev/null
|
||||
@@ -0,0 +1,20 @@
|
||||
FROM golang:1.24.8-alpine3.22 AS build
|
||||
|
||||
ARG MC_VERSION
|
||||
RUN apk add --no-cache git
|
||||
RUN git clone --depth 1 --branch "${MC_VERSION}" https://github.com/minio/mc.git /src/mc
|
||||
WORKDIR /src/mc
|
||||
RUN go mod download
|
||||
RUN LDFLAGS="$(MC_RELEASE=RELEASE go run buildscripts/gen-ldflags.go)" && \
|
||||
GOMAXPROCS=2 GOMEMLIMIT=1GiB CGO_ENABLED=0 go build -p=1 -tags kqueue -trimpath -ldflags "$LDFLAGS" -o /out/mc .
|
||||
|
||||
FROM alpine:3.22.2
|
||||
|
||||
RUN apk add --no-cache curl openssl postgresql17-client redis && \
|
||||
addgroup -g 10001 messenger && \
|
||||
adduser -D -H -u 10001 -G messenger messenger
|
||||
COPY --from=build /out/mc /usr/local/bin/mc
|
||||
COPY infra/minio/bootstrap.sh /usr/local/bin/bootstrap.sh
|
||||
|
||||
USER 10001:10001
|
||||
ENTRYPOINT ["mc"]
|
||||
@@ -0,0 +1,18 @@
|
||||
FROM golang:1.24.8-alpine3.22 AS build
|
||||
|
||||
ARG MINIO_VERSION
|
||||
RUN apk add --no-cache git
|
||||
RUN git clone --depth 1 --branch "${MINIO_VERSION}" https://github.com/minio/minio.git /src/minio
|
||||
WORKDIR /src/minio
|
||||
RUN go mod download
|
||||
RUN LDFLAGS="$(MINIO_RELEASE=RELEASE go run buildscripts/gen-ldflags.go)" && \
|
||||
GOMAXPROCS=2 GOMEMLIMIT=1GiB CGO_ENABLED=0 go build -p=1 -tags kqueue -trimpath -ldflags "$LDFLAGS" -o /out/minio .
|
||||
|
||||
FROM alpine:3.22.2
|
||||
|
||||
RUN addgroup -S minio && adduser -S -G minio minio && mkdir /data && chown minio:minio /data
|
||||
COPY --from=build /out/minio /usr/local/bin/minio
|
||||
|
||||
USER minio
|
||||
VOLUME ["/data"]
|
||||
ENTRYPOINT ["minio"]
|
||||
+42
-4
@@ -1,7 +1,45 @@
|
||||
# OpenBao — реализовать конфигурацию и политики
|
||||
# OpenBao
|
||||
|
||||
Добавьте non-dev конфигурацию listener/storage/TLS, API/worker policies, bootstrap и rotation scripts. Credentials и unseal material создаются локально; здесь нет готового root token или private key.
|
||||
OpenBao работает вне dev-mode с single-node integrated Raft. Данные сохраняются в `/openbao/data`, API принимает только TLS 1.3 на `8200`, cluster transport использует `8201`. Сертификат и ключ передаются в `/run/security/openbao`, а не входят в образ.
|
||||
|
||||
Решение должно показать реальную сервисную аутентификацию и получение нужного secret по ограниченной policy; просто работающий контейнер OpenBao не засчитывается. На 3 статический KV secret допустим. На 4 покажите expiry/renewal/rotation и allow/deny. Путь KV v2 data/metadata и ACL должен соответствовать фактическому mount; не копируйте широкое `path "*"` для приложения.
|
||||
## Идентичности
|
||||
|
||||
Документируйте шаги оператора init/unseal и восстановление после restart. Скрипт не должен печатать tokens/shares или хранить их в Git. См. contracts/trust-matrix.md и REPORT.md.
|
||||
`messenger-api` и `messenger-worker` — разные AppRole. Каждая роль:
|
||||
|
||||
- читает только свой `secret/data/messenger/<service>` в KV v2;
|
||||
- получает renewable token на 15 минут с максимальным TTL 1 час;
|
||||
- использует короткоживущий `SecretID` с TTL один час;
|
||||
- не получает policy `default`, доступ к чужому secret или управление policies.
|
||||
|
||||
OpenBao Agent читает `RoleID` и короткоживущий `SecretID`, renew-ит token и повторно аутентифицируется после его истечения или рестарта. SecretID действует один час и хранится в operator-controlled каталоге с правами `0600`; после истечения оператор выдаёт новый через `openbao-issue-approle.sh`. KV v2 secret рендерится в защищённый Docker volume `/run/openbao-secrets/secrets.json` с правами `0600`; токен Agent хранится отдельно в tmpfs `/run/openbao-agent`. Старый rendered secret не считается признаком здоровья: healthcheck также проверяет token и процесс Agent.
|
||||
|
||||
## Bootstrap оператора
|
||||
|
||||
Все команды выполняются с проверкой CA и hostname. `BAO_ADDR` по умолчанию равен `https://localhost:8200`, `BAO_CACERT` — `.runtime/security/openbao/ca.crt`.
|
||||
|
||||
```sh
|
||||
scripts/openbao-init.sh
|
||||
scripts/openbao-unseal.sh
|
||||
chmod 0600 .runtime/openbao/seed/api.json .runtime/openbao/seed/worker.json
|
||||
scripts/openbao-bootstrap.sh \
|
||||
"$PWD/.runtime/openbao/seed/api.json" \
|
||||
"$PWD/.runtime/openbao/seed/worker.json"
|
||||
scripts/openbao-test-policy.sh
|
||||
```
|
||||
|
||||
Seed-файлы — непустые JSON-объекты с фактическими credentials сервиса. Скрипт не выводит значения в консоль. `operator init` создаёт три unseal share с threshold 2 и сохраняет единственную локальную копию в `.runtime/openbao/operator/init.json` с правами `0600`. Этот каталог игнорируется Git.
|
||||
|
||||
Для учебного локального стенда оператор хранит init-файл вне контейнера. В реальном окружении shares нужно разделить между операторами либо защитить KMS/HSM, а root token отозвать после настройки recovery-доступа. API, worker и Agent не получают root token или unseal shares.
|
||||
|
||||
После обычного перезапуска данные остаются в Raft volume, но процесс остаётся sealed до ручного `scripts/openbao-unseal.sh`. Перезапуск API и worker не требует unseal.
|
||||
|
||||
## Ротация
|
||||
|
||||
```sh
|
||||
chmod 0600 .runtime/openbao/seed/api-next.json
|
||||
scripts/openbao-rotate.sh api "$PWD/.runtime/openbao/seed/api-next.json"
|
||||
```
|
||||
|
||||
Ротация создаёт новую версию KV v2 и новый короткоживущий `SecretID`; предыдущий accessor отзывается. Agent перечитывает статический secret не позднее пяти минут. Уже выданный token может жить до своего TTL, поэтому при компрометации дополнительно отзывают его accessor или отключают роль и перезапускают Agent с новой идентичностью.
|
||||
|
||||
`scripts/openbao-test-policy.sh` выдаёт временные AppRole credentials, проверяет разрешённое чтение собственного пути, запрет чужого пути, записи и управления policies, а также renewal. Временные tokens отзываются при завершении.
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
{{- with secret "secret/data/messenger/api" -}}
|
||||
{{ .Data.data | toJSON }}
|
||||
{{- end -}}
|
||||
@@ -0,0 +1,43 @@
|
||||
pid_file = "/run/openbao-agent/agent.pid"
|
||||
|
||||
vault {
|
||||
address = "https://openbao:8200"
|
||||
ca_cert = "/run/security/openbao/ca.crt"
|
||||
tls_server_name = "openbao"
|
||||
}
|
||||
|
||||
auto_auth {
|
||||
method {
|
||||
type = "approle"
|
||||
mount_path = "auth/approle"
|
||||
|
||||
config = {
|
||||
role_id_file_path = "/run/openbao-identity/role-id"
|
||||
secret_id_file_path = "/run/openbao-identity/secret-id"
|
||||
remove_secret_id_file_after_reading = false
|
||||
}
|
||||
}
|
||||
|
||||
sink {
|
||||
type = "file"
|
||||
|
||||
config = {
|
||||
path = "/run/openbao-agent/token"
|
||||
mode = 384
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template_config {
|
||||
exit_on_retry_failure = true
|
||||
static_secret_render_interval = "5m"
|
||||
}
|
||||
|
||||
template {
|
||||
source = "/etc/openbao-agent/api-secrets.ctmpl"
|
||||
destination = "/run/openbao-secrets/secrets.json"
|
||||
create_dest_dirs = true
|
||||
error_on_missing_key = true
|
||||
perms = "0600"
|
||||
backup = false
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{{- with secret "secret/data/messenger/worker" -}}
|
||||
{{ .Data.data | toJSON }}
|
||||
{{- end -}}
|
||||
@@ -0,0 +1,43 @@
|
||||
pid_file = "/run/openbao-agent/agent.pid"
|
||||
|
||||
vault {
|
||||
address = "https://openbao:8200"
|
||||
ca_cert = "/run/security/openbao/ca.crt"
|
||||
tls_server_name = "openbao"
|
||||
}
|
||||
|
||||
auto_auth {
|
||||
method {
|
||||
type = "approle"
|
||||
mount_path = "auth/approle"
|
||||
|
||||
config = {
|
||||
role_id_file_path = "/run/openbao-identity/role-id"
|
||||
secret_id_file_path = "/run/openbao-identity/secret-id"
|
||||
remove_secret_id_file_after_reading = false
|
||||
}
|
||||
}
|
||||
|
||||
sink {
|
||||
type = "file"
|
||||
|
||||
config = {
|
||||
path = "/run/openbao-agent/token"
|
||||
mode = 384
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template_config {
|
||||
exit_on_retry_failure = true
|
||||
static_secret_render_interval = "5m"
|
||||
}
|
||||
|
||||
template {
|
||||
source = "/etc/openbao-agent/worker-secrets.ctmpl"
|
||||
destination = "/run/openbao-secrets/secrets.json"
|
||||
create_dest_dirs = true
|
||||
error_on_missing_key = true
|
||||
perms = "0600"
|
||||
backup = false
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
ui = false
|
||||
|
||||
api_addr = "https://openbao:8200"
|
||||
cluster_addr = "https://openbao:8201"
|
||||
|
||||
storage "raft" {
|
||||
path = "/openbao/data"
|
||||
node_id = "messenger-openbao-1"
|
||||
}
|
||||
|
||||
listener "tcp" {
|
||||
address = "0.0.0.0:8200"
|
||||
cluster_address = "0.0.0.0:8201"
|
||||
tls_cert_file = "/run/security/openbao/tls.crt"
|
||||
tls_key_file = "/run/security/openbao/tls.key"
|
||||
tls_min_version = "tls13"
|
||||
}
|
||||
|
||||
log_level = "info"
|
||||
@@ -0,0 +1,21 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
umask 077
|
||||
|
||||
case "${SERVICE:-}" in
|
||||
api|worker) ;;
|
||||
*) echo "SERVICE must be api or worker" >&2; exit 2 ;;
|
||||
esac
|
||||
|
||||
source_dir="/source/$SERVICE"
|
||||
target_dir="/identities/$SERVICE"
|
||||
mkdir -p "$target_dir"
|
||||
for file in role-id secret-id secret-id-accessor; do
|
||||
cp "$source_dir/$file" "$target_dir/$file.next"
|
||||
chown 10001:10001 "$target_dir/$file.next"
|
||||
chmod 0600 "$target_dir/$file.next"
|
||||
mv "$target_dir/$file.next" "$target_dir/$file"
|
||||
done
|
||||
chown 10001:10001 "$target_dir"
|
||||
chmod 0700 "$target_dir"
|
||||
@@ -0,0 +1,31 @@
|
||||
path "secret/data/messenger/api" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
|
||||
path "secret/metadata/messenger/api" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
|
||||
path "secret/data/messenger/worker" {
|
||||
capabilities = ["deny"]
|
||||
}
|
||||
|
||||
path "secret/metadata/messenger/worker" {
|
||||
capabilities = ["deny"]
|
||||
}
|
||||
|
||||
path "auth/token/lookup-self" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
|
||||
path "auth/token/renew-self" {
|
||||
capabilities = ["update"]
|
||||
}
|
||||
|
||||
path "auth/token/revoke-self" {
|
||||
capabilities = ["update"]
|
||||
}
|
||||
|
||||
path "sys/policies/*" {
|
||||
capabilities = ["deny"]
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
path "secret/data/messenger/worker" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
|
||||
path "secret/metadata/messenger/worker" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
|
||||
path "secret/data/messenger/api" {
|
||||
capabilities = ["deny"]
|
||||
}
|
||||
|
||||
path "secret/metadata/messenger/api" {
|
||||
capabilities = ["deny"]
|
||||
}
|
||||
|
||||
path "auth/token/lookup-self" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
|
||||
path "auth/token/renew-self" {
|
||||
capabilities = ["update"]
|
||||
}
|
||||
|
||||
path "auth/token/revoke-self" {
|
||||
capabilities = ["update"]
|
||||
}
|
||||
|
||||
path "sys/policies/*" {
|
||||
capabilities = ["deny"]
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
extensions:
|
||||
health_check:
|
||||
endpoint: 0.0.0.0:13133
|
||||
|
||||
receivers:
|
||||
otlp:
|
||||
protocols:
|
||||
grpc:
|
||||
endpoint: 0.0.0.0:14317
|
||||
http:
|
||||
endpoint: 0.0.0.0:14318
|
||||
|
||||
processors:
|
||||
memory_limiter:
|
||||
check_interval: 1s
|
||||
limit_mib: 192
|
||||
spike_limit_mib: 48
|
||||
batch:
|
||||
send_batch_size: 1024
|
||||
send_batch_max_size: 2048
|
||||
timeout: 2s
|
||||
|
||||
exporters:
|
||||
prometheus:
|
||||
endpoint: 0.0.0.0:8889
|
||||
resource_constant_labels:
|
||||
included:
|
||||
- service.name
|
||||
- service.version
|
||||
- deployment.environment.name
|
||||
otlp_grpc/tempo:
|
||||
endpoint: tempo-collector-gateway:8443
|
||||
tls:
|
||||
ca_file: /run/security/internal-ca/ca.crt
|
||||
cert_file: /run/security/client/tls.crt
|
||||
key_file: /run/security/client/tls.key
|
||||
server_name_override: telemetry-gateway
|
||||
sending_queue:
|
||||
enabled: true
|
||||
num_consumers: 2
|
||||
queue_size: 512
|
||||
retry_on_failure:
|
||||
enabled: true
|
||||
initial_interval: 1s
|
||||
max_interval: 5s
|
||||
max_elapsed_time: 30s
|
||||
otlp_http/loki:
|
||||
endpoint: https://loki-collector-gateway:8443/otlp
|
||||
tls:
|
||||
ca_file: /run/security/internal-ca/ca.crt
|
||||
cert_file: /run/security/client/tls.crt
|
||||
key_file: /run/security/client/tls.key
|
||||
server_name_override: telemetry-gateway
|
||||
sending_queue:
|
||||
enabled: true
|
||||
num_consumers: 2
|
||||
queue_size: 512
|
||||
retry_on_failure:
|
||||
enabled: true
|
||||
initial_interval: 1s
|
||||
max_interval: 5s
|
||||
max_elapsed_time: 30s
|
||||
|
||||
service:
|
||||
extensions: [health_check]
|
||||
telemetry:
|
||||
logs:
|
||||
level: info
|
||||
metrics:
|
||||
readers:
|
||||
- pull:
|
||||
exporter:
|
||||
prometheus:
|
||||
host: 0.0.0.0
|
||||
port: 8888
|
||||
pipelines:
|
||||
metrics:
|
||||
receivers: [otlp]
|
||||
processors: [memory_limiter, batch]
|
||||
exporters: [prometheus]
|
||||
logs:
|
||||
receivers: [otlp]
|
||||
processors: [memory_limiter, batch]
|
||||
exporters: [otlp_http/loki]
|
||||
traces:
|
||||
receivers: [otlp]
|
||||
processors: [memory_limiter, batch]
|
||||
exporters: [otlp_grpc/tempo]
|
||||
@@ -0,0 +1,133 @@
|
||||
static_resources:
|
||||
listeners:
|
||||
- name: otlp_grpc_mtls
|
||||
address:
|
||||
socket_address:
|
||||
address: 0.0.0.0
|
||||
port_value: 4317
|
||||
filter_chains:
|
||||
- filters:
|
||||
- name: envoy.filters.network.http_connection_manager
|
||||
typed_config:
|
||||
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
|
||||
stat_prefix: otlp_grpc
|
||||
codec_type: HTTP2
|
||||
route_config:
|
||||
name: otlp_grpc
|
||||
virtual_hosts:
|
||||
- name: collector
|
||||
domains: ["*"]
|
||||
routes:
|
||||
- match: {prefix: "/"}
|
||||
route:
|
||||
cluster: collector_grpc
|
||||
timeout: 0s
|
||||
http_filters:
|
||||
- name: envoy.filters.http.router
|
||||
typed_config:
|
||||
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
|
||||
transport_socket:
|
||||
name: envoy.transport_sockets.tls
|
||||
typed_config:
|
||||
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.DownstreamTlsContext
|
||||
require_client_certificate: true
|
||||
common_tls_context:
|
||||
tls_params:
|
||||
tls_minimum_protocol_version: TLSv1_3
|
||||
tls_maximum_protocol_version: TLSv1_3
|
||||
alpn_protocols: [h2]
|
||||
tls_certificates:
|
||||
- certificate_chain: {filename: /run/security/otel-gateway/tls.crt}
|
||||
private_key: {filename: /run/security/otel-gateway/tls.key}
|
||||
validation_context:
|
||||
trusted_ca: {filename: /run/security/internal-ca/ca.crt}
|
||||
match_typed_subject_alt_names:
|
||||
- san_type: URI
|
||||
matcher:
|
||||
safe_regex:
|
||||
regex: "^spiffe://mtusi/lab7/(api|dispatcher|worker)$"
|
||||
|
||||
- name: otlp_http_mtls
|
||||
address:
|
||||
socket_address:
|
||||
address: 0.0.0.0
|
||||
port_value: 4318
|
||||
filter_chains:
|
||||
- filters:
|
||||
- name: envoy.filters.network.http_connection_manager
|
||||
typed_config:
|
||||
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
|
||||
stat_prefix: otlp_http
|
||||
codec_type: AUTO
|
||||
route_config:
|
||||
name: otlp_http
|
||||
virtual_hosts:
|
||||
- name: collector
|
||||
domains: ["*"]
|
||||
routes:
|
||||
- match: {prefix: "/"}
|
||||
route:
|
||||
cluster: collector_http
|
||||
timeout: 30s
|
||||
http_filters:
|
||||
- name: envoy.filters.http.router
|
||||
typed_config:
|
||||
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
|
||||
transport_socket:
|
||||
name: envoy.transport_sockets.tls
|
||||
typed_config:
|
||||
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.DownstreamTlsContext
|
||||
require_client_certificate: true
|
||||
common_tls_context:
|
||||
tls_params:
|
||||
tls_minimum_protocol_version: TLSv1_3
|
||||
tls_maximum_protocol_version: TLSv1_3
|
||||
alpn_protocols: [h2, http/1.1]
|
||||
tls_certificates:
|
||||
- certificate_chain: {filename: /run/security/otel-gateway/tls.crt}
|
||||
private_key: {filename: /run/security/otel-gateway/tls.key}
|
||||
validation_context:
|
||||
trusted_ca: {filename: /run/security/internal-ca/ca.crt}
|
||||
match_typed_subject_alt_names:
|
||||
- san_type: URI
|
||||
matcher:
|
||||
safe_regex:
|
||||
regex: "^spiffe://mtusi/lab7/(api|dispatcher|worker)$"
|
||||
|
||||
clusters:
|
||||
- name: collector_grpc
|
||||
connect_timeout: 2s
|
||||
type: STRICT_DNS
|
||||
load_assignment:
|
||||
cluster_name: collector_grpc
|
||||
endpoints:
|
||||
- lb_endpoints:
|
||||
- endpoint:
|
||||
address:
|
||||
socket_address:
|
||||
address: otel-collector
|
||||
port_value: 14317
|
||||
typed_extension_protocol_options:
|
||||
envoy.extensions.upstreams.http.v3.HttpProtocolOptions:
|
||||
"@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions
|
||||
explicit_http_config:
|
||||
http2_protocol_options: {}
|
||||
|
||||
- name: collector_http
|
||||
connect_timeout: 2s
|
||||
type: STRICT_DNS
|
||||
load_assignment:
|
||||
cluster_name: collector_http
|
||||
endpoints:
|
||||
- lb_endpoints:
|
||||
- endpoint:
|
||||
address:
|
||||
socket_address:
|
||||
address: otel-collector
|
||||
port_value: 14318
|
||||
|
||||
admin:
|
||||
address:
|
||||
socket_address:
|
||||
address: 127.0.0.1
|
||||
port_value: 9901
|
||||
Executable
+158
@@ -0,0 +1,158 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
umask 077
|
||||
|
||||
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
repo_dir=$(CDPATH= cd -- "$script_dir/../.." && pwd)
|
||||
security_dir="$repo_dir/.runtime/security"
|
||||
staging_dir="$security_dir/.bootstrap.$$"
|
||||
|
||||
cleanup() {
|
||||
rm -rf "$staging_dir"
|
||||
}
|
||||
|
||||
trap cleanup EXIT HUP INT TERM
|
||||
|
||||
if [ "${1:-}" = "--force" ]; then
|
||||
force=1
|
||||
elif [ "$#" -eq 0 ]; then
|
||||
force=0
|
||||
else
|
||||
echo "usage: $0 [--force]" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
if [ -d "$security_dir/public-ca" ] || [ -d "$security_dir/internal-ca" ]; then
|
||||
if [ "$force" -ne 1 ]; then
|
||||
echo "security material already exists; use --force to replace it" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
mkdir -p "$security_dir" "$staging_dir"
|
||||
chmod 0700 "$security_dir" "$staging_dir"
|
||||
|
||||
create_ca() {
|
||||
name=$1
|
||||
common_name=$2
|
||||
directory="$staging_dir/$name"
|
||||
|
||||
mkdir -p "$directory"
|
||||
openssl genpkey -quiet -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "$directory/ca.key"
|
||||
openssl req -new -x509 -sha256 -days 3650 \
|
||||
-key "$directory/ca.key" \
|
||||
-out "$directory/ca.crt" \
|
||||
-subj "/CN=$common_name" \
|
||||
-config "$script_dir/templates/ca.cnf" \
|
||||
-extensions v3_ca
|
||||
}
|
||||
|
||||
create_leaf() {
|
||||
name=$1
|
||||
common_name=$2
|
||||
issuer=$3
|
||||
template=$4
|
||||
directory="$staging_dir/$name"
|
||||
issuer_directory="$staging_dir/$issuer"
|
||||
|
||||
mkdir -p "$directory"
|
||||
openssl genpkey -quiet -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "$directory/tls.key"
|
||||
openssl req -new -sha256 \
|
||||
-key "$directory/tls.key" \
|
||||
-out "$directory/tls.csr" \
|
||||
-subj "/CN=$common_name"
|
||||
openssl x509 -req -sha256 -days 30 \
|
||||
-in "$directory/tls.csr" \
|
||||
-CA "$issuer_directory/ca.crt" \
|
||||
-CAkey "$issuer_directory/ca.key" \
|
||||
-CAcreateserial \
|
||||
-out "$directory/tls.crt" \
|
||||
-extfile "$script_dir/templates/$template"
|
||||
rm "$directory/tls.csr"
|
||||
}
|
||||
|
||||
create_ca public-ca "MTUSI Lab 7 Public CA"
|
||||
create_ca internal-ca "MTUSI Lab 7 Internal CA"
|
||||
create_ca foreign-ca "MTUSI Lab 7 Foreign CA"
|
||||
|
||||
create_leaf ingress localhost public-ca ingress.cnf
|
||||
create_leaf api api internal-ca api.cnf
|
||||
create_leaf load-balancer load-balancer internal-ca load-balancer.cnf
|
||||
create_leaf prometheus prometheus internal-ca prometheus.cnf
|
||||
create_leaf postgres postgres internal-ca postgres.cnf
|
||||
create_leaf valkey valkey internal-ca valkey.cnf
|
||||
create_leaf redis-broker redis-broker internal-ca redis-broker.cnf
|
||||
create_leaf redis-gateway redis-gateway internal-ca redis-gateway.cnf
|
||||
create_leaf redis-gateway-client redis-gateway internal-ca redis-gateway-client.cnf
|
||||
create_leaf minio s3 internal-ca minio.cnf
|
||||
create_leaf otel-gateway otel-gateway internal-ca otel-gateway.cnf
|
||||
create_leaf openbao openbao internal-ca openbao.cnf
|
||||
cp "$staging_dir/internal-ca/ca.crt" "$staging_dir/openbao/ca.crt"
|
||||
create_leaf telemetry-gateway telemetry-gateway internal-ca telemetry-gateway.cnf
|
||||
create_leaf collector-client collector internal-ca collector-client.cnf
|
||||
create_leaf grafana-client grafana internal-ca grafana-client.cnf
|
||||
create_leaf grafana-server grafana internal-ca grafana-server.cnf
|
||||
create_leaf pipeline-metrics pipeline-metrics internal-ca pipeline-metrics.cnf
|
||||
create_leaf api-client messenger_api internal-ca api-client.cnf
|
||||
create_leaf dispatcher-client messenger_dispatcher internal-ca dispatcher-client.cnf
|
||||
create_leaf worker-client messenger_worker internal-ca worker-client.cnf
|
||||
create_leaf migrate-client messenger_migrate internal-ca migrate-client.cnf
|
||||
create_leaf reconciler-client messenger_reconciler internal-ca reconciler-client.cnf
|
||||
create_leaf postgres-exporter messenger_exporter internal-ca postgres-exporter.cnf
|
||||
create_leaf postgres-admin-client messenger internal-ca postgres-admin-client.cnf
|
||||
create_leaf valkey-exporter valkey_exporter internal-ca valkey-exporter.cnf
|
||||
create_leaf redis-exporter redis_exporter internal-ca redis-exporter.cnf
|
||||
create_leaf forbidden-client worker internal-ca forbidden-client.cnf
|
||||
create_leaf foreign-client load-balancer foreign-ca load-balancer.cnf
|
||||
|
||||
openssl verify -purpose sslserver -verify_hostname localhost \
|
||||
-CAfile "$staging_dir/public-ca/ca.crt" "$staging_dir/ingress/tls.crt" >/dev/null
|
||||
openssl verify -purpose sslserver -verify_hostname api \
|
||||
-CAfile "$staging_dir/internal-ca/ca.crt" "$staging_dir/api/tls.crt" >/dev/null
|
||||
for server in postgres valkey redis-broker redis-gateway minio otel-gateway openbao telemetry-gateway grafana-server pipeline-metrics; do
|
||||
case "$server" in
|
||||
minio) hostname=s3 ;;
|
||||
grafana-server) hostname=grafana ;;
|
||||
*) hostname=$server ;;
|
||||
esac
|
||||
openssl verify -purpose sslserver -verify_hostname "$hostname" \
|
||||
-CAfile "$staging_dir/internal-ca/ca.crt" "$staging_dir/$server/tls.crt" >/dev/null
|
||||
done
|
||||
openssl verify -purpose sslclient \
|
||||
-CAfile "$staging_dir/internal-ca/ca.crt" \
|
||||
"$staging_dir/load-balancer/tls.crt" \
|
||||
"$staging_dir/prometheus/tls.crt" \
|
||||
"$staging_dir/api-client/tls.crt" \
|
||||
"$staging_dir/dispatcher-client/tls.crt" \
|
||||
"$staging_dir/worker-client/tls.crt" \
|
||||
"$staging_dir/migrate-client/tls.crt" \
|
||||
"$staging_dir/reconciler-client/tls.crt" \
|
||||
"$staging_dir/postgres-exporter/tls.crt" \
|
||||
"$staging_dir/postgres-admin-client/tls.crt" \
|
||||
"$staging_dir/valkey-exporter/tls.crt" \
|
||||
"$staging_dir/redis-exporter/tls.crt" \
|
||||
"$staging_dir/redis-gateway-client/tls.crt" \
|
||||
"$staging_dir/collector-client/tls.crt" \
|
||||
"$staging_dir/grafana-client/tls.crt" \
|
||||
"$staging_dir/forbidden-client/tls.crt" >/dev/null
|
||||
openssl verify -purpose sslclient \
|
||||
-CAfile "$staging_dir/foreign-ca/ca.crt" "$staging_dir/foreign-client/tls.crt" >/dev/null
|
||||
|
||||
find "$staging_dir" -type d -exec chmod 0700 {} \;
|
||||
find "$staging_dir" -type f -exec chmod 0600 {} \;
|
||||
|
||||
for directory in \
|
||||
public-ca internal-ca foreign-ca ingress api load-balancer prometheus \
|
||||
postgres valkey redis-broker redis-gateway redis-gateway-client minio otel-gateway openbao \
|
||||
telemetry-gateway collector-client grafana-client grafana-server \
|
||||
pipeline-metrics \
|
||||
api-client dispatcher-client worker-client migrate-client reconciler-client \
|
||||
postgres-exporter postgres-admin-client valkey-exporter redis-exporter \
|
||||
forbidden-client foreign-client; do
|
||||
rm -rf "$security_dir/$directory"
|
||||
mv "$staging_dir/$directory" "$security_dir/$directory"
|
||||
done
|
||||
|
||||
echo "local PKI created in .runtime/security"
|
||||
@@ -0,0 +1,141 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
umask 077
|
||||
|
||||
rm -rf /target/api/internal-ca /target/api/api /target/api/client
|
||||
mkdir -p /target/api/internal-ca /target/api/api /target/api/client
|
||||
cp /source/internal-ca/ca.crt /target/api/internal-ca/ca.crt
|
||||
cp /source/api/tls.crt /target/api/api/tls.crt
|
||||
cp /source/api/tls.key /target/api/api/tls.key
|
||||
cp /source/api-client/tls.crt /target/api/client/tls.crt
|
||||
cp /source/api-client/tls.key /target/api/client/tls.key
|
||||
chown -R 10001:10001 /target/api
|
||||
find /target/api -type d -exec chmod 0700 {} \;
|
||||
find /target/api -type f -exec chmod 0600 {} \;
|
||||
|
||||
rm -rf /target/nginx/ingress /target/nginx/public-ca /target/nginx/internal-ca /target/nginx/load-balancer
|
||||
mkdir -p /target/nginx/ingress /target/nginx/public-ca /target/nginx/internal-ca /target/nginx/load-balancer
|
||||
cp /source/ingress/tls.crt /target/nginx/ingress/tls.crt
|
||||
cp /source/ingress/tls.key /target/nginx/ingress/tls.key
|
||||
cp /source/public-ca/ca.crt /target/nginx/public-ca/ca.crt
|
||||
cp /source/internal-ca/ca.crt /target/nginx/internal-ca/ca.crt
|
||||
cp /source/load-balancer/tls.crt /target/nginx/load-balancer/tls.crt
|
||||
cp /source/load-balancer/tls.key /target/nginx/load-balancer/tls.key
|
||||
chown -R 0:0 /target/nginx
|
||||
find /target/nginx -type d -exec chmod 0700 {} \;
|
||||
find /target/nginx -type f -exec chmod 0600 {} \;
|
||||
|
||||
rm -rf /target/prometheus/internal-ca /target/prometheus/prometheus
|
||||
mkdir -p /target/prometheus/internal-ca /target/prometheus/prometheus
|
||||
cp /source/internal-ca/ca.crt /target/prometheus/internal-ca/ca.crt
|
||||
cp /source/prometheus/tls.crt /target/prometheus/prometheus/tls.crt
|
||||
cp /source/prometheus/tls.key /target/prometheus/prometheus/tls.key
|
||||
chown -R 65534:65534 /target/prometheus
|
||||
find /target/prometheus -type d -exec chmod 0700 {} \;
|
||||
find /target/prometheus -type f -exec chmod 0600 {} \;
|
||||
|
||||
rm -rf /target/client/public-ca
|
||||
mkdir -p /target/client/public-ca
|
||||
cp /source/public-ca/ca.crt /target/client/public-ca/ca.crt
|
||||
chown -R 10001:10001 /target/client
|
||||
find /target/client -type d -exec chmod 0700 {} \;
|
||||
find /target/client -type f -exec chmod 0600 {} \;
|
||||
|
||||
install_server() {
|
||||
target=$1
|
||||
identity=$2
|
||||
owner=$3
|
||||
|
||||
rm -rf "/target/$target/internal-ca" "/target/$target/$identity"
|
||||
mkdir -p "/target/$target/internal-ca" "/target/$target/$identity"
|
||||
cp /source/internal-ca/ca.crt "/target/$target/internal-ca/ca.crt"
|
||||
cp "/source/$identity/tls.crt" "/target/$target/$identity/tls.crt"
|
||||
cp "/source/$identity/tls.key" "/target/$target/$identity/tls.key"
|
||||
chown -R "$owner" "/target/$target"
|
||||
find "/target/$target" -type d -exec chmod 0700 {} \;
|
||||
find "/target/$target" -type f -exec chmod 0600 {} \;
|
||||
}
|
||||
|
||||
install_client() {
|
||||
target=$1
|
||||
identity=$2
|
||||
owner=$3
|
||||
|
||||
rm -rf "/target/$target/internal-ca" "/target/$target/public-ca" "/target/$target/client"
|
||||
mkdir -p "/target/$target/internal-ca" "/target/$target/public-ca" "/target/$target/client"
|
||||
cp /source/internal-ca/ca.crt "/target/$target/internal-ca/ca.crt"
|
||||
cp /source/public-ca/ca.crt "/target/$target/public-ca/ca.crt"
|
||||
cp "/source/$identity/tls.crt" "/target/$target/client/tls.crt"
|
||||
cp "/source/$identity/tls.key" "/target/$target/client/tls.key"
|
||||
chown -R "$owner" "/target/$target"
|
||||
find "/target/$target" -type d -exec chmod 0700 {} \;
|
||||
find "/target/$target" -type f -exec chmod 0600 {} \;
|
||||
}
|
||||
|
||||
install_server postgres postgres 70:70
|
||||
install_server valkey valkey 999:999
|
||||
install_server redis-broker redis-broker 999:999
|
||||
install_server minio minio 100:101
|
||||
cp /source/minio/tls.crt /target/minio/minio/public.crt
|
||||
cp /source/minio/tls.key /target/minio/minio/private.key
|
||||
chown 100:101 /target/minio/minio/public.crt /target/minio/minio/private.key
|
||||
chmod 0600 /target/minio/minio/public.crt /target/minio/minio/private.key
|
||||
install_server otel-gateway otel-gateway 101:101
|
||||
install_server openbao openbao 100:100
|
||||
cp /source/internal-ca/ca.crt /target/openbao/openbao/ca.crt
|
||||
chown 100:100 /target/openbao/openbao/ca.crt
|
||||
chmod 0600 /target/openbao/openbao/ca.crt
|
||||
|
||||
rm -rf /target/openbao-agent/openbao
|
||||
mkdir -p /target/openbao-agent/openbao
|
||||
cp /source/internal-ca/ca.crt /target/openbao-agent/openbao/ca.crt
|
||||
chown -R 10001:10001 /target/openbao-agent
|
||||
find /target/openbao-agent -type d -exec chmod 0700 {} \;
|
||||
find /target/openbao-agent -type f -exec chmod 0600 {} \;
|
||||
chmod 0644 /target/openbao-agent/openbao/ca.crt
|
||||
chmod 0755 /target/openbao-agent /target/openbao-agent/openbao
|
||||
|
||||
install_server telemetry-gateway telemetry-gateway 10001:10001
|
||||
install_client collector collector-client 10001:10001
|
||||
install_client grafana grafana-client 472:472
|
||||
cp /source/grafana-server/tls.crt /target/grafana/grafana-server.crt
|
||||
cp /source/grafana-server/tls.key /target/grafana/grafana-server.key
|
||||
chown 472:472 /target/grafana/grafana-server.crt /target/grafana/grafana-server.key
|
||||
chmod 0600 /target/grafana/grafana-server.crt /target/grafana/grafana-server.key
|
||||
|
||||
rm -rf /target/redis-gateway/internal-ca /target/redis-gateway/redis-gateway /target/redis-gateway/client
|
||||
mkdir -p /target/redis-gateway/internal-ca /target/redis-gateway/redis-gateway /target/redis-gateway/client
|
||||
cp /source/internal-ca/ca.crt /target/redis-gateway/internal-ca/ca.crt
|
||||
cp /source/redis-gateway/tls.crt /target/redis-gateway/redis-gateway/tls.crt
|
||||
cp /source/redis-gateway/tls.key /target/redis-gateway/redis-gateway/tls.key
|
||||
cp /source/redis-gateway-client/tls.crt /target/redis-gateway/client/tls.crt
|
||||
cp /source/redis-gateway-client/tls.key /target/redis-gateway/client/tls.key
|
||||
chown -R 101:101 /target/redis-gateway
|
||||
find /target/redis-gateway -type d -exec chmod 0700 {} \;
|
||||
find /target/redis-gateway -type f -exec chmod 0600 {} \;
|
||||
|
||||
install_client dispatcher dispatcher-client 10001:10001
|
||||
install_client worker worker-client 10001:10001
|
||||
for target in dispatcher worker; do
|
||||
mkdir -p "/target/$target/metrics"
|
||||
cp /source/pipeline-metrics/tls.crt "/target/$target/metrics/tls.crt"
|
||||
cp /source/pipeline-metrics/tls.key "/target/$target/metrics/tls.key"
|
||||
chown -R 10001:10001 "/target/$target/metrics"
|
||||
find "/target/$target/metrics" -type d -exec chmod 0700 {} \;
|
||||
find "/target/$target/metrics" -type f -exec chmod 0600 {} \;
|
||||
done
|
||||
install_client migrate migrate-client 10001:10001
|
||||
install_client reconciler reconciler-client 10001:10001
|
||||
install_client postgres-exporter postgres-exporter 65534:65534
|
||||
install_client postgres-bootstrap postgres-admin-client 0:0
|
||||
install_client valkey-exporter valkey-exporter 59000:59000
|
||||
install_client redis-exporter redis-exporter 59000:59000
|
||||
|
||||
rm -rf /target/s3-bootstrap/internal-ca
|
||||
mkdir -p /target/s3-bootstrap/internal-ca
|
||||
cp /source/internal-ca/ca.crt /target/s3-bootstrap/internal-ca/ca.crt
|
||||
chown -R 10001:10001 /target/s3-bootstrap
|
||||
find /target/s3-bootstrap -type d -exec chmod 0700 {} \;
|
||||
find /target/s3-bootstrap -type f -exec chmod 0600 {} \;
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = URI:spiffe://mtusi/lab7/api
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,10 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage = serverAuth
|
||||
subjectAltName = @subject_alt_names
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
|
||||
[subject_alt_names]
|
||||
DNS.1 = api
|
||||
URI.1 = spiffe://mtusi/lab7/api
|
||||
@@ -0,0 +1,12 @@
|
||||
[req]
|
||||
distinguished_name = distinguished_name
|
||||
prompt = no
|
||||
|
||||
[distinguished_name]
|
||||
CN = local-ca
|
||||
|
||||
[v3_ca]
|
||||
basicConstraints = critical,CA:TRUE,pathlen:0
|
||||
keyUsage = critical,keyCertSign,cRLSign
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid:always
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = URI:spiffe://mtusi/lab7/collector
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = URI:spiffe://mtusi/lab7/dispatcher
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,9 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = @subject_alt_names
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
|
||||
[subject_alt_names]
|
||||
URI.1 = spiffe://mtusi/lab7/worker
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = URI:spiffe://mtusi/lab7/grafana
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,11 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage = serverAuth
|
||||
subjectAltName = @subject_alt_names
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
|
||||
[subject_alt_names]
|
||||
DNS.1 = grafana
|
||||
DNS.2 = localhost
|
||||
IP.1 = 127.0.0.1
|
||||
@@ -0,0 +1,11 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage = serverAuth
|
||||
subjectAltName = @subject_alt_names
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
|
||||
[subject_alt_names]
|
||||
DNS.1 = localhost
|
||||
DNS.2 = nginx
|
||||
IP.1 = 127.0.0.1
|
||||
@@ -0,0 +1,9 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = @subject_alt_names
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
|
||||
[subject_alt_names]
|
||||
URI.1 = spiffe://mtusi/lab7/load-balancer
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = URI:spiffe://mtusi/lab7/migrate
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage = serverAuth
|
||||
subjectAltName = DNS:s3
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,11 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage = serverAuth
|
||||
subjectAltName = @subject_alt_names
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
|
||||
[subject_alt_names]
|
||||
DNS.1 = openbao
|
||||
DNS.2 = localhost
|
||||
IP.1 = 127.0.0.1
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage = serverAuth
|
||||
subjectAltName = DNS:otel-gateway
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage = serverAuth
|
||||
subjectAltName = DNS:pipeline-metrics
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = URI:spiffe://mtusi/lab7/postgres-bootstrap
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = URI:spiffe://mtusi/lab7/postgres-exporter
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage = serverAuth
|
||||
subjectAltName = DNS:postgres
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,4 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = URI:spiffe://mtusi/lab7/prometheus
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = URI:spiffe://mtusi/lab7/reconciler
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage = serverAuth
|
||||
subjectAltName = DNS:redis-broker
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = URI:spiffe://mtusi/lab7/redis-exporter
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = URI:spiffe://mtusi/lab7/redis-gateway
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage = serverAuth
|
||||
subjectAltName = DNS:redis-gateway
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage = serverAuth
|
||||
subjectAltName = DNS:telemetry-gateway
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = URI:spiffe://mtusi/lab7/valkey-exporter
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage = serverAuth
|
||||
subjectAltName = DNS:valkey
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
@@ -0,0 +1,6 @@
|
||||
basicConstraints = critical,CA:FALSE
|
||||
keyUsage = critical,digitalSignature
|
||||
extendedKeyUsage = clientAuth
|
||||
subjectAltName = URI:spiffe://mtusi/lab7/worker
|
||||
subjectKeyIdentifier = hash
|
||||
authorityKeyIdentifier = keyid,issuer
|
||||
Executable
+38
@@ -0,0 +1,38 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
case "${1:-}" in
|
||||
create) sql_file=/bootstrap/roles.sql ;;
|
||||
grant) sql_file=/bootstrap/grants.sql ;;
|
||||
*) echo "usage: $0 create|grant" >&2; exit 2 ;;
|
||||
esac
|
||||
|
||||
: "${POSTGRES_DB:?POSTGRES_DB is required}"
|
||||
: "${POSTGRES_USER:?POSTGRES_USER is required}"
|
||||
: "${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}"
|
||||
: "${POSTGRES_API_PASSWORD:?POSTGRES_API_PASSWORD is required}"
|
||||
: "${POSTGRES_DISPATCHER_PASSWORD:?POSTGRES_DISPATCHER_PASSWORD is required}"
|
||||
: "${POSTGRES_WORKER_PASSWORD:?POSTGRES_WORKER_PASSWORD is required}"
|
||||
: "${POSTGRES_RECONCILER_PASSWORD:?POSTGRES_RECONCILER_PASSWORD is required}"
|
||||
: "${POSTGRES_EXPORTER_PASSWORD:?POSTGRES_EXPORTER_PASSWORD is required}"
|
||||
: "${POSTGRES_MIGRATE_PASSWORD:?POSTGRES_MIGRATE_PASSWORD is required}"
|
||||
|
||||
export PGPASSWORD=$POSTGRES_PASSWORD
|
||||
export PGSSLROOTCERT=${PGSSLROOTCERT:-/run/security/internal-ca/ca.crt}
|
||||
export PGSSLCERT=${PGSSLCERT:-/run/security/client/tls.crt}
|
||||
export PGSSLKEY=${PGSSLKEY:-/run/security/client/tls.key}
|
||||
export PGSSLMODE=verify-full
|
||||
|
||||
psql \
|
||||
--host=postgres \
|
||||
--username="$POSTGRES_USER" \
|
||||
--dbname="$POSTGRES_DB" \
|
||||
--set=ON_ERROR_STOP=1 \
|
||||
--set=api_password="$POSTGRES_API_PASSWORD" \
|
||||
--set=dispatcher_password="$POSTGRES_DISPATCHER_PASSWORD" \
|
||||
--set=worker_password="$POSTGRES_WORKER_PASSWORD" \
|
||||
--set=reconciler_password="$POSTGRES_RECONCILER_PASSWORD" \
|
||||
--set=exporter_password="$POSTGRES_EXPORTER_PASSWORD" \
|
||||
--set=migrate_password="$POSTGRES_MIGRATE_PASSWORD" \
|
||||
--file="$sql_file"
|
||||
@@ -0,0 +1,25 @@
|
||||
REVOKE ALL ON ALL TABLES IN SCHEMA public FROM PUBLIC;
|
||||
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON
|
||||
users,
|
||||
chats,
|
||||
chat_members,
|
||||
messages,
|
||||
message_idempotency,
|
||||
attachments,
|
||||
message_attachments,
|
||||
attachment_jobs,
|
||||
outbox_events
|
||||
TO messenger_api;
|
||||
GRANT SELECT ON attachment_variants TO messenger_api;
|
||||
|
||||
GRANT SELECT, UPDATE, DELETE ON outbox_events TO messenger_dispatcher;
|
||||
|
||||
GRANT SELECT, UPDATE ON attachments, attachment_jobs TO messenger_worker;
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON attachment_variants TO messenger_worker;
|
||||
GRANT INSERT ON attachment_replay_audit TO messenger_worker;
|
||||
GRANT INSERT ON outbox_events TO messenger_worker;
|
||||
|
||||
GRANT SELECT, UPDATE, DELETE ON attachments TO messenger_reconciler;
|
||||
GRANT SELECT ON attachment_variants, message_attachments TO messenger_reconciler;
|
||||
GRANT pg_monitor TO messenger_exporter;
|
||||
@@ -0,0 +1,15 @@
|
||||
# Local bootstrap and health checks stay inside the PostgreSQL container.
|
||||
local all all trust
|
||||
|
||||
# TLS verifies the server. clientcert=verify-full additionally binds the
|
||||
# certificate CN to the database role; SCRAM remains a second factor.
|
||||
hostssl messenger messenger_api all scram-sha-256 clientcert=verify-full
|
||||
hostssl messenger messenger_dispatcher all scram-sha-256 clientcert=verify-full
|
||||
hostssl messenger messenger_worker all scram-sha-256 clientcert=verify-full
|
||||
hostssl messenger messenger_reconciler all scram-sha-256 clientcert=verify-full
|
||||
hostssl messenger messenger_exporter all scram-sha-256 clientcert=verify-full
|
||||
hostssl messenger messenger_migrate all scram-sha-256 clientcert=verify-full
|
||||
hostssl messenger messenger all scram-sha-256 clientcert=verify-full
|
||||
|
||||
hostnossl all all all reject
|
||||
hostssl all all all reject
|
||||
@@ -0,0 +1,11 @@
|
||||
listen_addresses = '*'
|
||||
port = 5432
|
||||
|
||||
ssl = on
|
||||
ssl_cert_file = '/run/security/postgres/tls.crt'
|
||||
ssl_key_file = '/run/security/postgres/tls.key'
|
||||
ssl_ca_file = '/run/security/internal-ca/ca.crt'
|
||||
ssl_min_protocol_version = 'TLSv1.3'
|
||||
|
||||
hba_file = '/etc/postgresql/pg_hba.conf'
|
||||
password_encryption = 'scram-sha-256'
|
||||
@@ -0,0 +1,38 @@
|
||||
SELECT format('CREATE ROLE %I LOGIN', role_name)
|
||||
FROM unnest(ARRAY[
|
||||
'messenger_api',
|
||||
'messenger_dispatcher',
|
||||
'messenger_worker',
|
||||
'messenger_reconciler',
|
||||
'messenger_exporter',
|
||||
'messenger_migrate'
|
||||
]) AS roles(role_name)
|
||||
WHERE NOT EXISTS (SELECT FROM pg_roles WHERE rolname = role_name)
|
||||
\gexec
|
||||
|
||||
ALTER ROLE messenger_api PASSWORD :'api_password';
|
||||
ALTER ROLE messenger_dispatcher PASSWORD :'dispatcher_password';
|
||||
ALTER ROLE messenger_worker PASSWORD :'worker_password';
|
||||
ALTER ROLE messenger_reconciler PASSWORD :'reconciler_password';
|
||||
ALTER ROLE messenger_exporter PASSWORD :'exporter_password';
|
||||
ALTER ROLE messenger_migrate PASSWORD :'migrate_password';
|
||||
|
||||
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
|
||||
|
||||
GRANT CONNECT ON DATABASE messenger TO
|
||||
messenger_api,
|
||||
messenger_dispatcher,
|
||||
messenger_worker,
|
||||
messenger_reconciler,
|
||||
messenger_exporter,
|
||||
messenger_migrate;
|
||||
GRANT USAGE ON SCHEMA public TO
|
||||
messenger_api,
|
||||
messenger_dispatcher,
|
||||
messenger_worker,
|
||||
messenger_reconciler,
|
||||
messenger_exporter;
|
||||
GRANT USAGE, CREATE ON SCHEMA public TO messenger_migrate;
|
||||
|
||||
ALTER DEFAULT PRIVILEGES FOR ROLE messenger_migrate IN SCHEMA public
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO messenger_api;
|
||||
Executable
+66
@@ -0,0 +1,66 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
usage() {
|
||||
echo "usage: $0 valkey|broker OUTPUT" >&2
|
||||
exit 2
|
||||
}
|
||||
|
||||
valid_secret() {
|
||||
value=$1
|
||||
case "$value" in
|
||||
""|*[!A-Za-z0-9_@%+=:,./-]*) return 1 ;;
|
||||
*) return 0 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
write_valkey_acl() {
|
||||
: "${VALKEY_API_PASSWORD:?VALKEY_API_PASSWORD is required}"
|
||||
: "${VALKEY_EXPORTER_PASSWORD:?VALKEY_EXPORTER_PASSWORD is required}"
|
||||
: "${VALKEY_HEALTH_PASSWORD:?VALKEY_HEALTH_PASSWORD is required}"
|
||||
valid_secret "$VALKEY_API_PASSWORD" || exit 1
|
||||
valid_secret "$VALKEY_EXPORTER_PASSWORD" || exit 1
|
||||
valid_secret "$VALKEY_HEALTH_PASSWORD" || exit 1
|
||||
|
||||
cat >"$output" <<EOF
|
||||
user default reset off
|
||||
user messenger_api reset on >$VALKEY_API_PASSWORD ~session:* ~login-rate:* +ping +get +set +del +unlink +expire +pexpire +ttl +pttl +incr +eval +evalsha +script|load
|
||||
user valkey_exporter reset on >$VALKEY_EXPORTER_PASSWORD ~* +ping +info +client +config|get +scan +dbsize +slowlog
|
||||
user valkey_health reset on >$VALKEY_HEALTH_PASSWORD +ping
|
||||
EOF
|
||||
}
|
||||
|
||||
write_broker_acl() {
|
||||
: "${REDIS_DISPATCHER_PASSWORD:?REDIS_DISPATCHER_PASSWORD is required}"
|
||||
: "${REDIS_WORKER_PASSWORD:?REDIS_WORKER_PASSWORD is required}"
|
||||
: "${REDIS_EXPORTER_PASSWORD:?REDIS_EXPORTER_PASSWORD is required}"
|
||||
: "${REDIS_HEALTH_PASSWORD:?REDIS_HEALTH_PASSWORD is required}"
|
||||
valid_secret "$REDIS_DISPATCHER_PASSWORD" || exit 1
|
||||
valid_secret "$REDIS_WORKER_PASSWORD" || exit 1
|
||||
valid_secret "$REDIS_EXPORTER_PASSWORD" || exit 1
|
||||
valid_secret "$REDIS_HEALTH_PASSWORD" || exit 1
|
||||
|
||||
cat >"$output" <<EOF
|
||||
user default reset off
|
||||
user messenger_dispatcher reset on >$REDIS_DISPATCHER_PASSWORD ~attachment-jobs +ping +xadd
|
||||
user messenger_worker reset on >$REDIS_WORKER_PASSWORD ~attachment-jobs ~attachment-jobs-failed +ping +xgroup +xreadgroup +xautoclaim +xpending +xack +xdel +xadd +eval +evalsha +script|load
|
||||
user redis_exporter reset on >$REDIS_EXPORTER_PASSWORD ~* +ping +info +client +config|get +scan +dbsize +slowlog +xinfo
|
||||
user redis_health reset on >$REDIS_HEALTH_PASSWORD +ping
|
||||
EOF
|
||||
}
|
||||
|
||||
[ "$#" -eq 2 ] || usage
|
||||
mode=$1
|
||||
output=$2
|
||||
|
||||
umask 077
|
||||
mkdir -p "$(dirname -- "$output")"
|
||||
|
||||
case "$mode" in
|
||||
valkey) write_valkey_acl ;;
|
||||
broker) write_broker_acl ;;
|
||||
*) usage ;;
|
||||
esac
|
||||
|
||||
chmod 0600 "$output"
|
||||
@@ -0,0 +1,136 @@
|
||||
static_resources:
|
||||
listeners:
|
||||
- name: valkey_api
|
||||
address:
|
||||
socket_address: {address: 0.0.0.0, port_value: 16379}
|
||||
filter_chains:
|
||||
- filters:
|
||||
- name: envoy.filters.network.tcp_proxy
|
||||
typed_config:
|
||||
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
|
||||
stat_prefix: valkey_api
|
||||
cluster: valkey
|
||||
transport_socket:
|
||||
name: envoy.transport_sockets.tls
|
||||
typed_config:
|
||||
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.DownstreamTlsContext
|
||||
require_client_certificate: true
|
||||
common_tls_context:
|
||||
tls_params: {tls_minimum_protocol_version: TLSv1_3, tls_maximum_protocol_version: TLSv1_3}
|
||||
tls_certificates:
|
||||
- certificate_chain: {filename: /run/security/redis-gateway/tls.crt}
|
||||
private_key: {filename: /run/security/redis-gateway/tls.key}
|
||||
validation_context:
|
||||
trusted_ca: {filename: /run/security/internal-ca/ca.crt}
|
||||
match_typed_subject_alt_names:
|
||||
- san_type: URI
|
||||
matcher: {exact: "spiffe://mtusi/lab7/api"}
|
||||
|
||||
- name: broker_dispatcher
|
||||
address:
|
||||
socket_address: {address: 0.0.0.0, port_value: 26379}
|
||||
filter_chains:
|
||||
- filters:
|
||||
- name: envoy.filters.network.tcp_proxy
|
||||
typed_config:
|
||||
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
|
||||
stat_prefix: broker_dispatcher
|
||||
cluster: redis_broker
|
||||
transport_socket:
|
||||
name: envoy.transport_sockets.tls
|
||||
typed_config:
|
||||
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.DownstreamTlsContext
|
||||
require_client_certificate: true
|
||||
common_tls_context:
|
||||
tls_params: {tls_minimum_protocol_version: TLSv1_3, tls_maximum_protocol_version: TLSv1_3}
|
||||
tls_certificates:
|
||||
- certificate_chain: {filename: /run/security/redis-gateway/tls.crt}
|
||||
private_key: {filename: /run/security/redis-gateway/tls.key}
|
||||
validation_context:
|
||||
trusted_ca: {filename: /run/security/internal-ca/ca.crt}
|
||||
match_typed_subject_alt_names:
|
||||
- san_type: URI
|
||||
matcher: {exact: "spiffe://mtusi/lab7/dispatcher"}
|
||||
|
||||
- name: broker_worker
|
||||
address:
|
||||
socket_address: {address: 0.0.0.0, port_value: 36379}
|
||||
filter_chains:
|
||||
- filters:
|
||||
- name: envoy.filters.network.tcp_proxy
|
||||
typed_config:
|
||||
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
|
||||
stat_prefix: broker_worker
|
||||
cluster: redis_broker
|
||||
transport_socket:
|
||||
name: envoy.transport_sockets.tls
|
||||
typed_config:
|
||||
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.DownstreamTlsContext
|
||||
require_client_certificate: true
|
||||
common_tls_context:
|
||||
tls_params: {tls_minimum_protocol_version: TLSv1_3, tls_maximum_protocol_version: TLSv1_3}
|
||||
tls_certificates:
|
||||
- certificate_chain: {filename: /run/security/redis-gateway/tls.crt}
|
||||
private_key: {filename: /run/security/redis-gateway/tls.key}
|
||||
validation_context:
|
||||
trusted_ca: {filename: /run/security/internal-ca/ca.crt}
|
||||
match_typed_subject_alt_names:
|
||||
- san_type: URI
|
||||
matcher: {exact: "spiffe://mtusi/lab7/worker"}
|
||||
|
||||
clusters:
|
||||
- name: valkey
|
||||
connect_timeout: 2s
|
||||
type: STRICT_DNS
|
||||
load_assignment:
|
||||
cluster_name: valkey
|
||||
endpoints:
|
||||
- lb_endpoints:
|
||||
- endpoint:
|
||||
address:
|
||||
socket_address: {address: valkey, port_value: 6379}
|
||||
transport_socket:
|
||||
name: envoy.transport_sockets.tls
|
||||
typed_config:
|
||||
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
|
||||
sni: valkey
|
||||
common_tls_context:
|
||||
tls_params: {tls_minimum_protocol_version: TLSv1_3, tls_maximum_protocol_version: TLSv1_3}
|
||||
tls_certificates:
|
||||
- certificate_chain: {filename: /run/security/client/tls.crt}
|
||||
private_key: {filename: /run/security/client/tls.key}
|
||||
validation_context:
|
||||
trusted_ca: {filename: /run/security/internal-ca/ca.crt}
|
||||
match_typed_subject_alt_names:
|
||||
- san_type: DNS
|
||||
matcher: {exact: valkey}
|
||||
|
||||
- name: redis_broker
|
||||
connect_timeout: 2s
|
||||
type: STRICT_DNS
|
||||
load_assignment:
|
||||
cluster_name: redis_broker
|
||||
endpoints:
|
||||
- lb_endpoints:
|
||||
- endpoint:
|
||||
address:
|
||||
socket_address: {address: redis-broker, port_value: 6379}
|
||||
transport_socket:
|
||||
name: envoy.transport_sockets.tls
|
||||
typed_config:
|
||||
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
|
||||
sni: redis-broker
|
||||
common_tls_context:
|
||||
tls_params: {tls_minimum_protocol_version: TLSv1_3, tls_maximum_protocol_version: TLSv1_3}
|
||||
tls_certificates:
|
||||
- certificate_chain: {filename: /run/security/client/tls.crt}
|
||||
private_key: {filename: /run/security/client/tls.key}
|
||||
validation_context:
|
||||
trusted_ca: {filename: /run/security/internal-ca/ca.crt}
|
||||
match_typed_subject_alt_names:
|
||||
- san_type: DNS
|
||||
matcher: {exact: redis-broker}
|
||||
|
||||
admin:
|
||||
address:
|
||||
socket_address: {address: 127.0.0.1, port_value: 9901}
|
||||
@@ -0,0 +1,17 @@
|
||||
bind 0.0.0.0
|
||||
protected-mode yes
|
||||
port 0
|
||||
tls-port 6379
|
||||
tls-cert-file /run/security/redis-broker/tls.crt
|
||||
tls-key-file /run/security/redis-broker/tls.key
|
||||
tls-ca-cert-file /run/security/internal-ca/ca.crt
|
||||
tls-auth-clients yes
|
||||
tls-protocols "TLSv1.3"
|
||||
unixsocket /tmp/redis.sock
|
||||
unixsocketperm 700
|
||||
|
||||
aclfile /run/security/redis-broker/users.acl
|
||||
appendonly yes
|
||||
appendfsync always
|
||||
maxmemory-policy noeviction
|
||||
dir /data
|
||||
@@ -0,0 +1,16 @@
|
||||
bind 0.0.0.0
|
||||
protected-mode yes
|
||||
port 0
|
||||
tls-port 6379
|
||||
tls-cert-file /run/security/valkey/tls.crt
|
||||
tls-key-file /run/security/valkey/tls.key
|
||||
tls-ca-cert-file /run/security/internal-ca/ca.crt
|
||||
tls-auth-clients yes
|
||||
tls-protocols "TLSv1.3"
|
||||
unixsocket /tmp/valkey.sock
|
||||
unixsocketperm 700
|
||||
|
||||
aclfile /run/security/valkey/users.acl
|
||||
appendonly yes
|
||||
appendfsync everysec
|
||||
dir /data
|
||||
Executable
+199
@@ -0,0 +1,199 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
: "${SECURITY_DIR:=/run/security-source}"
|
||||
: "${POSTGRES_HOST:=postgres}"
|
||||
: "${VALKEY_HOST:=redis-gateway}"
|
||||
: "${VALKEY_PORT:=16379}"
|
||||
: "${REDIS_HOST:=redis-gateway}"
|
||||
: "${REDIS_DISPATCHER_PORT:=26379}"
|
||||
: "${REDIS_WORKER_PORT:=36379}"
|
||||
: "${S3_HOST:=s3}"
|
||||
: "${OTEL_HOST:=otel-gateway}"
|
||||
: "${POSTGRES_DB:=messenger}"
|
||||
: "${S3_BUCKET:=messenger-attachments}"
|
||||
|
||||
required_commands="curl mc openssl psql redis-cli"
|
||||
for command in $required_commands; do
|
||||
command -v "$command" >/dev/null 2>&1 || {
|
||||
echo "$command is required" >&2
|
||||
exit 1
|
||||
}
|
||||
done
|
||||
|
||||
ca=$SECURITY_DIR/internal-ca/ca.crt
|
||||
|
||||
expect_failure() {
|
||||
description=$1
|
||||
shift
|
||||
if "$@" >/dev/null 2>&1; then
|
||||
echo "expected failure: $description" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
expect_redis_denied() {
|
||||
description=$1
|
||||
shift
|
||||
output=$("$@" 2>&1 || true)
|
||||
case "$output" in
|
||||
*NOPERM*|*DENIED*) ;;
|
||||
*) echo "expected Redis ACL denial: $description" >&2; exit 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
expect_redis_allowed() {
|
||||
description=$1
|
||||
shift
|
||||
output=$("$@" 2>&1) || {
|
||||
echo "expected Redis ACL allow: $description: $output" >&2
|
||||
exit 1
|
||||
}
|
||||
case "$output" in
|
||||
*NOPERM*|*DENIED*) echo "expected Redis ACL allow: $description: $output" >&2; exit 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
check_server() {
|
||||
host=$1
|
||||
port=$2
|
||||
identity=${3:-}
|
||||
client_options=
|
||||
if [ -n "$identity" ]; then
|
||||
client_options="-cert $SECURITY_DIR/$identity/tls.crt -key $SECURITY_DIR/$identity/tls.key"
|
||||
fi
|
||||
# client_options contains only paths assembled by this script.
|
||||
# shellcheck disable=SC2086
|
||||
printf '' | openssl s_client $client_options \
|
||||
-connect "$host:$port" \
|
||||
-servername "$host" \
|
||||
-verify_hostname "$host" \
|
||||
-verify_return_error \
|
||||
-CAfile "$ca" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
check_server "$POSTGRES_HOST" 5432 api-client
|
||||
check_server "$VALKEY_HOST" "$VALKEY_PORT" api-client
|
||||
check_server "$REDIS_HOST" "$REDIS_WORKER_PORT" worker-client
|
||||
check_server "$S3_HOST" 9000
|
||||
|
||||
: "${POSTGRES_API_PASSWORD:?POSTGRES_API_PASSWORD is required}"
|
||||
: "${POSTGRES_WORKER_PASSWORD:?POSTGRES_WORKER_PASSWORD is required}"
|
||||
: "${POSTGRES_DISPATCHER_PASSWORD:?POSTGRES_DISPATCHER_PASSWORD is required}"
|
||||
|
||||
pg_url() {
|
||||
role=$1
|
||||
cert=$2
|
||||
password=$3
|
||||
printf 'postgresql://%s:%s@%s:5432/%s?sslmode=verify-full&sslrootcert=%s&sslcert=%s&sslkey=%s' \
|
||||
"$role" "$password" "$POSTGRES_HOST" "$POSTGRES_DB" "$ca" \
|
||||
"$SECURITY_DIR/$cert/tls.crt" "$SECURITY_DIR/$cert/tls.key"
|
||||
}
|
||||
|
||||
api_db=$(pg_url messenger_api api-client "$POSTGRES_API_PASSWORD")
|
||||
worker_db=$(pg_url messenger_worker worker-client "$POSTGRES_WORKER_PASSWORD")
|
||||
dispatcher_db=$(pg_url messenger_dispatcher dispatcher-client "$POSTGRES_DISPATCHER_PASSWORD")
|
||||
api_with_worker_cert=$(pg_url messenger_api worker-client "$POSTGRES_API_PASSWORD")
|
||||
|
||||
psql "$api_db" --set=ON_ERROR_STOP=1 --command='SELECT count(*) FROM users' >/dev/null
|
||||
psql "$worker_db" --set=ON_ERROR_STOP=1 --command='SELECT count(*) FROM attachment_jobs' >/dev/null
|
||||
psql "$worker_db" --set=ON_ERROR_STOP=1 --command='UPDATE attachment_jobs SET updated_at = updated_at WHERE false' >/dev/null
|
||||
psql "$dispatcher_db" --set=ON_ERROR_STOP=1 --command='SELECT count(*) FROM outbox_events' >/dev/null
|
||||
expect_failure "worker reads users" psql "$worker_db" --set=ON_ERROR_STOP=1 --command='SELECT count(*) FROM users'
|
||||
expect_failure "dispatcher reads attachments" psql "$dispatcher_db" --set=ON_ERROR_STOP=1 --command='SELECT count(*) FROM attachments'
|
||||
expect_failure "API role uses worker certificate" psql "$api_with_worker_cert" --set=ON_ERROR_STOP=1 --command='SELECT 1'
|
||||
|
||||
: "${VALKEY_API_PASSWORD:?VALKEY_API_PASSWORD is required}"
|
||||
: "${REDIS_DISPATCHER_PASSWORD:?REDIS_DISPATCHER_PASSWORD is required}"
|
||||
: "${REDIS_WORKER_PASSWORD:?REDIS_WORKER_PASSWORD is required}"
|
||||
|
||||
expect_failure "broker without client certificate" env REDISCLI_AUTH="$REDIS_WORKER_PASSWORD" \
|
||||
redis-cli --tls --cacert "$ca" --user messenger_worker -h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" PING
|
||||
for identity in foreign-client api-client; do
|
||||
expect_failure "broker rejects $identity" env REDISCLI_AUTH="$REDIS_WORKER_PASSWORD" \
|
||||
redis-cli --tls --cacert "$ca" --cert "$SECURITY_DIR/$identity/tls.crt" \
|
||||
--key "$SECURITY_DIR/$identity/tls.key" --user messenger_worker \
|
||||
-h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" PING
|
||||
done
|
||||
|
||||
redis_tls() {
|
||||
identity=$1
|
||||
shift
|
||||
redis-cli --tls --cacert "$ca" \
|
||||
--cert "$SECURITY_DIR/$identity/tls.crt" \
|
||||
--key "$SECURITY_DIR/$identity/tls.key" "$@"
|
||||
}
|
||||
|
||||
REDISCLI_AUTH=$VALKEY_API_PASSWORD redis_tls api-client \
|
||||
--user messenger_api -h "$VALKEY_HOST" -p "$VALKEY_PORT" SET session:acl-test value EX 30 >/dev/null
|
||||
expect_redis_denied "session identity writes an arbitrary key" env REDISCLI_AUTH="$VALKEY_API_PASSWORD" \
|
||||
redis-cli --tls --cacert "$ca" --cert "$SECURITY_DIR/api-client/tls.crt" \
|
||||
--key "$SECURITY_DIR/api-client/tls.key" --user messenger_api -h "$VALKEY_HOST" -p "$VALKEY_PORT" SET forbidden value
|
||||
|
||||
REDISCLI_AUTH=$REDIS_DISPATCHER_PASSWORD redis_tls dispatcher-client \
|
||||
--user messenger_dispatcher -h "$REDIS_HOST" -p "$REDIS_DISPATCHER_PORT" XADD attachment-jobs '*' payload acl-test >/dev/null
|
||||
expect_redis_denied "dispatcher consumes jobs" env REDISCLI_AUTH="$REDIS_DISPATCHER_PASSWORD" \
|
||||
redis-cli --tls --cacert "$ca" --cert "$SECURITY_DIR/dispatcher-client/tls.crt" \
|
||||
--key "$SECURITY_DIR/dispatcher-client/tls.key" --user messenger_dispatcher -h "$REDIS_HOST" -p "$REDIS_DISPATCHER_PORT" \
|
||||
XREAD COUNT 1 STREAMS attachment-jobs 0
|
||||
REDISCLI_AUTH=$REDIS_WORKER_PASSWORD redis_tls worker-client \
|
||||
--user messenger_worker -h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" XGROUP DESTROY attachment-jobs acl-worker-check >/dev/null 2>&1 || true
|
||||
expect_redis_allowed "worker creates its consumer group" env REDISCLI_AUTH="$REDIS_WORKER_PASSWORD" \
|
||||
redis-cli --tls --cacert "$ca" --cert "$SECURITY_DIR/worker-client/tls.crt" \
|
||||
--key "$SECURITY_DIR/worker-client/tls.key" --user messenger_worker -h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" \
|
||||
XGROUP CREATE attachment-jobs acl-worker-check '$' MKSTREAM
|
||||
expect_redis_allowed "worker consumes its stream" env REDISCLI_AUTH="$REDIS_WORKER_PASSWORD" \
|
||||
redis-cli --tls --cacert "$ca" --cert "$SECURITY_DIR/worker-client/tls.crt" \
|
||||
--key "$SECURITY_DIR/worker-client/tls.key" --user messenger_worker -h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" \
|
||||
XREADGROUP GROUP acl-worker-check acl-worker-consumer COUNT 1 STREAMS attachment-jobs '>'
|
||||
expect_redis_allowed "worker acknowledges its stream" env REDISCLI_AUTH="$REDIS_WORKER_PASSWORD" \
|
||||
redis-cli --tls --cacert "$ca" --cert "$SECURITY_DIR/worker-client/tls.crt" \
|
||||
--key "$SECURITY_DIR/worker-client/tls.key" --user messenger_worker -h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" \
|
||||
XACK attachment-jobs acl-worker-check 0-1
|
||||
REDISCLI_AUTH=$REDIS_WORKER_PASSWORD redis_tls worker-client \
|
||||
--user messenger_worker -h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" XGROUP DESTROY attachment-jobs acl-worker-check >/dev/null
|
||||
expect_redis_denied "worker accesses an unrelated stream" env REDISCLI_AUTH="$REDIS_WORKER_PASSWORD" \
|
||||
redis-cli --tls --cacert "$ca" --cert "$SECURITY_DIR/worker-client/tls.crt" \
|
||||
--key "$SECURITY_DIR/worker-client/tls.key" --user messenger_worker -h "$REDIS_HOST" -p "$REDIS_WORKER_PORT" XADD unrelated '*' payload denied
|
||||
|
||||
: "${S3_API_ACCESS_KEY:?S3_API_ACCESS_KEY is required}"
|
||||
: "${S3_API_SECRET_KEY:?S3_API_SECRET_KEY is required}"
|
||||
: "${S3_WORKER_ACCESS_KEY:?S3_WORKER_ACCESS_KEY is required}"
|
||||
: "${S3_WORKER_SECRET_KEY:?S3_WORKER_SECRET_KEY is required}"
|
||||
|
||||
export SSL_CERT_FILE=$ca
|
||||
export MC_CONFIG_DIR=/tmp/mc-acl-test
|
||||
test_file=/tmp/acl-test-object
|
||||
printf 'acl-test' >"$test_file"
|
||||
mc alias set api "https://$S3_HOST:9000" "$S3_API_ACCESS_KEY" "$S3_API_SECRET_KEY" >/dev/null
|
||||
mc alias set worker "https://$S3_HOST:9000" "$S3_WORKER_ACCESS_KEY" "$S3_WORKER_SECRET_KEY" >/dev/null
|
||||
mc cp "$test_file" "api/$S3_BUCKET/staging/acl-test" >/dev/null
|
||||
expect_failure "API writes processed objects" mc cp "$test_file" "api/$S3_BUCKET/attachments/acl-test"
|
||||
mc cp "$test_file" "worker/$S3_BUCKET/attachments/acl-test" >/dev/null
|
||||
expect_failure "worker writes outside attachment prefixes" mc cp "$test_file" "worker/$S3_BUCKET/private/acl-test"
|
||||
mc rm "api/$S3_BUCKET/staging/acl-test" >/dev/null
|
||||
mc rm "worker/$S3_BUCKET/attachments/acl-test" >/dev/null
|
||||
|
||||
expect_failure "OTel without client certificate" curl --silent --show-error \
|
||||
--cacert "$ca" "https://$OTEL_HOST:4318/"
|
||||
expect_failure "OTel with foreign CA client certificate" curl --silent --show-error \
|
||||
--cacert "$ca" --cert "$SECURITY_DIR/foreign-client/tls.crt" \
|
||||
--key "$SECURITY_DIR/foreign-client/tls.key" "https://$OTEL_HOST:4318/"
|
||||
|
||||
curl --silent --show-error --fail-with-body \
|
||||
--cacert "$ca" \
|
||||
--cert "$SECURITY_DIR/api-client/tls.crt" \
|
||||
--key "$SECURITY_DIR/api-client/tls.key" \
|
||||
"https://$OTEL_HOST:4318/" >/dev/null 2>&1 || status=$?
|
||||
case "${status:-0}" in
|
||||
0|22) ;;
|
||||
*) echo "allowed OTel identity failed TLS" >&2; exit 1 ;;
|
||||
esac
|
||||
expect_failure "Prometheus same-CA identity reaches OTel" curl --silent --show-error \
|
||||
--cacert "$ca" \
|
||||
--cert "$SECURITY_DIR/prometheus/tls.crt" \
|
||||
--key "$SECURITY_DIR/prometheus/tls.key" \
|
||||
"https://$OTEL_HOST:4318/"
|
||||
|
||||
echo "dependency TLS and least-privilege matrix passed"
|
||||
@@ -0,0 +1,48 @@
|
||||
server:
|
||||
http_listen_address: 0.0.0.0
|
||||
http_listen_port: 3200
|
||||
grpc_listen_address: 0.0.0.0
|
||||
grpc_listen_port: 9095
|
||||
|
||||
distributor:
|
||||
receivers:
|
||||
otlp:
|
||||
protocols:
|
||||
grpc:
|
||||
endpoint: 0.0.0.0:4317
|
||||
http:
|
||||
endpoint: 0.0.0.0:4318
|
||||
|
||||
ingester:
|
||||
max_block_duration: 5m
|
||||
|
||||
compactor:
|
||||
compaction:
|
||||
block_retention: 168h
|
||||
|
||||
storage:
|
||||
trace:
|
||||
backend: local
|
||||
wal:
|
||||
path: /var/tempo/wal
|
||||
local:
|
||||
path: /var/tempo/traces
|
||||
|
||||
metrics_generator:
|
||||
registry:
|
||||
external_labels:
|
||||
source: tempo
|
||||
deployment_environment: lab
|
||||
storage:
|
||||
path: /var/tempo/generator/wal
|
||||
remote_write:
|
||||
- url: http://prometheus:9090/api/v1/write
|
||||
send_exemplars: true
|
||||
|
||||
overrides:
|
||||
defaults:
|
||||
metrics_generator:
|
||||
processors: [service-graphs, span-metrics]
|
||||
|
||||
usage_report:
|
||||
reporting_enabled: false
|
||||
@@ -0,0 +1,25 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/config"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/database"
|
||||
)
|
||||
|
||||
func RunMigrations() error {
|
||||
settings, err := config.LoadMigration()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pool, err := database.Open(context.Background(), database.Config{
|
||||
URL: settings.DatabaseURL, MinConnections: settings.DBMinConns, MaxConnections: settings.DBMaxConns,
|
||||
MaxConnectionAge: settings.DBMaxConnectionAge, MaxConnectionIdle: settings.DBMaxConnectionIdle,
|
||||
HealthCheckPeriod: settings.DBHealthCheckPeriod, ConnectTimeout: settings.DependencyTimeout,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
return database.Migrate(context.Background(), pool, settings.MigrationTimeout)
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/broker/redisstream"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/config"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/database"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/messenger/attachment/pipeline"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/messenger/attachment/transform"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/messenger/attachment/worker"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/objectstore"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/outbox"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/postgres"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/security/tlsconfig"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/telemetry"
|
||||
)
|
||||
|
||||
func RunDispatcher() error {
|
||||
settings, err := config.LoadDispatcher()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
instanceID, err := os.Hostname()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
observability, logger, err := startTelemetry("messenger-dispatcher", instanceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer shutdownTelemetry(observability)
|
||||
pool, err := database.Open(context.Background(), dispatcherDatabaseConfig(settings))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
if err := telemetry.ObserveDatabasePool(observability.MeterProvider().Meter("messenger-dispatcher/database"), pool); err != nil {
|
||||
return err
|
||||
}
|
||||
brokerTLSConfiguration, err := loadClientTLS(settings.BrokerTLS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
client, err := newRedisClient(settings.BrokerURL, brokerTLSConfiguration)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = client.Close() }()
|
||||
broker, err := redisstream.New(client, redisstream.Config{
|
||||
Stream: settings.Stream, Group: settings.ConsumerGroup, Consumer: "dispatcher",
|
||||
FailedStream: settings.FailedStream, FailedMaxLen: settings.FailedStreamMaxLength,
|
||||
ClaimMinIdle: settings.LeaseDuration, ReadCount: int64(settings.BatchSize),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metrics := pipeline.NewMetrics(observability.MeterProvider().Meter("messenger-dispatcher"), observability.MetricsHandler(), 0)
|
||||
dispatcher, err := outbox.New(postgres.New(pool), broker, metrics, outbox.Config{
|
||||
BatchSize: settings.BatchSize, LeaseDuration: settings.LeaseDuration,
|
||||
PollInterval: settings.PollInterval, RecoveryInterval: settings.RecoveryInterval,
|
||||
RetryDelay: settings.RetryDelay, Retention: settings.Retention,
|
||||
CleanupInterval: settings.CleanupInterval, CleanupBatchSize: settings.CleanupBatchSize,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metricsTLSConfiguration, err := loadServerTLS(settings.MetricsTLS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logger.Info("outbox dispatcher started", "stream", settings.Stream)
|
||||
return runPipelineProcess(
|
||||
logger, settings.MetricsAddress, settings.HealthAddress, metricsTLSConfiguration, metrics.Handler(), dispatcher.Run,
|
||||
)
|
||||
}
|
||||
|
||||
func RunWorker() error {
|
||||
settings, err := config.LoadWorker()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
observability, logger, err := startTelemetry("messenger-worker", hostname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer shutdownTelemetry(observability)
|
||||
pool, err := database.Open(context.Background(), workerDatabaseConfig(settings))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
if err := telemetry.ObserveDatabasePool(observability.MeterProvider().Meter("messenger-worker/database"), pool); err != nil {
|
||||
return err
|
||||
}
|
||||
objects, err := objectstore.New(objectstore.Config{
|
||||
Endpoint: settings.S3Endpoint, Region: settings.S3Region, Bucket: settings.S3Bucket,
|
||||
AccessKey: settings.S3AccessKey, SecretKey: settings.S3SecretKey,
|
||||
UseTLS: settings.S3UseTLS, CAPath: settings.S3CAPath, Timeout: settings.S3Timeout,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
brokerTLSConfiguration, err := loadClientTLS(settings.BrokerTLS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
client, err := newRedisClient(settings.BrokerURL, brokerTLSConfiguration)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = client.Close() }()
|
||||
consumer := hostname + "-" + uuid.NewString()
|
||||
broker, err := redisstream.New(client, redisstream.Config{
|
||||
Stream: settings.Stream, Group: settings.ConsumerGroup, Consumer: consumer,
|
||||
FailedStream: settings.FailedStream, FailedMaxLen: settings.FailedStreamMaxLength, Block: settings.ReadBlock,
|
||||
ClaimMinIdle: settings.LeaseDuration, ReadCount: int64(settings.Prefetch),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := broker.Ensure(context.Background()); err != nil {
|
||||
return err
|
||||
}
|
||||
processor, err := transform.NewProcessor(transform.DefaultConfig(settings.AttachmentMaxBytes, settings.AttachmentMaxPixels))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metrics := pipeline.NewMetrics(observability.MeterProvider().Meter("messenger-worker"), observability.MetricsHandler(), settings.Concurrency)
|
||||
processorWorker, err := worker.New(
|
||||
logger, postgres.New(pool), broker, objects, processor, metrics,
|
||||
worker.Config{
|
||||
WorkerID: consumer, Concurrency: settings.Concurrency, LeaseDuration: settings.LeaseDuration,
|
||||
HeartbeatInterval: settings.HeartbeatInterval, ProcessingTimeout: settings.ProcessingTimeout,
|
||||
ReclaimInterval: settings.ReclaimInterval, MaxAttempts: settings.MaxAttempts,
|
||||
RetryBase: settings.RetryBase, RetryMax: settings.RetryMax,
|
||||
MaxBytes: settings.AttachmentMaxBytes, MaxPixels: settings.AttachmentMaxPixels,
|
||||
MemoryBytes: settings.MemoryBytes,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metricsTLSConfiguration, err := loadServerTLS(settings.MetricsTLS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logger.Info("attachment worker started", "consumer", consumer, "concurrency", settings.Concurrency)
|
||||
return runPipelineProcess(
|
||||
logger, settings.MetricsAddress, settings.HealthAddress, metricsTLSConfiguration, metrics.Handler(), processorWorker.Run,
|
||||
)
|
||||
}
|
||||
|
||||
func loadClientTLS(config config.ClientTLSConfig) (*tls.Config, error) {
|
||||
return tlsconfig.LoadClient(tlsconfig.ClientConfig{
|
||||
CAPath: config.CAPath, CertificatePath: config.CertificatePath,
|
||||
PrivateKeyPath: config.PrivateKeyPath, ServerName: config.ServerName,
|
||||
})
|
||||
}
|
||||
|
||||
func loadServerTLS(config config.ServerTLSConfig) (*tls.Config, error) {
|
||||
return tlsconfig.LoadServer(tlsconfig.ServerConfig{
|
||||
CAPath: config.CAPath, CertificatePath: config.CertificatePath,
|
||||
PrivateKeyPath: config.PrivateKeyPath, AllowedClientURI: config.AllowedClientURI,
|
||||
})
|
||||
}
|
||||
|
||||
func newRedisClient(rawURL string, tlsConfiguration *tls.Config) (*redis.Client, error) {
|
||||
options, err := redis.ParseURL(rawURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
options.TLSConfig = tlsConfiguration.Clone()
|
||||
client := redis.NewClient(options)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := client.Ping(ctx).Err(); err != nil {
|
||||
_ = client.Close()
|
||||
return nil, err
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func runPipelineProcess(
|
||||
logger *slog.Logger,
|
||||
metricsAddress string,
|
||||
healthAddress string,
|
||||
metricsTLSConfiguration *tls.Config,
|
||||
metricsHandler http.Handler,
|
||||
run func(context.Context) error,
|
||||
) error {
|
||||
metricsMux := http.NewServeMux()
|
||||
metricsMux.Handle("/metrics", metricsHandler)
|
||||
metricsServer := &http.Server{
|
||||
Addr: metricsAddress, Handler: metricsMux, ReadHeaderTimeout: 5 * time.Second,
|
||||
TLSConfig: metricsTLSConfiguration,
|
||||
}
|
||||
healthMux := http.NewServeMux()
|
||||
healthMux.HandleFunc("/health/live", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusNoContent) })
|
||||
healthServer := &http.Server{Addr: healthAddress, Handler: healthMux, ReadHeaderTimeout: 5 * time.Second}
|
||||
metricsListener, err := net.Listen("tcp", metricsAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
healthListener, err := net.Listen("tcp", healthAddress)
|
||||
if err != nil {
|
||||
_ = metricsListener.Close()
|
||||
return err
|
||||
}
|
||||
serveResult := make(chan error, 2)
|
||||
go func() { serveResult <- metricsServer.Serve(tls.NewListener(metricsListener, metricsTLSConfiguration)) }()
|
||||
go func() { serveResult <- healthServer.Serve(healthListener) }()
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
runResult := make(chan error, 1)
|
||||
go func() { runResult <- run(ctx) }()
|
||||
|
||||
var result error
|
||||
select {
|
||||
case result = <-runResult:
|
||||
stop()
|
||||
case result = <-serveResult:
|
||||
stop()
|
||||
case <-ctx.Done():
|
||||
}
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
for _, server := range []*http.Server{metricsServer, healthServer} {
|
||||
if err := server.Shutdown(shutdownCtx); err != nil && result == nil {
|
||||
result = err
|
||||
}
|
||||
}
|
||||
if errors.Is(result, http.ErrServerClosed) || errors.Is(result, context.Canceled) {
|
||||
return nil
|
||||
}
|
||||
logger.Info("pipeline process stopped")
|
||||
return result
|
||||
}
|
||||
|
||||
func dispatcherDatabaseConfig(settings config.DispatcherConfig) database.Config {
|
||||
return database.Config{
|
||||
URL: settings.DatabaseURL, MinConnections: settings.DBMinConns, MaxConnections: settings.DBMaxConns,
|
||||
MaxConnectionAge: settings.DBMaxConnectionAge, MaxConnectionIdle: settings.DBMaxConnectionIdle,
|
||||
HealthCheckPeriod: settings.DBHealthCheckPeriod, ConnectTimeout: settings.DependencyTimeout,
|
||||
}
|
||||
}
|
||||
|
||||
func workerDatabaseConfig(settings config.WorkerConfig) database.Config {
|
||||
return database.Config{
|
||||
URL: settings.DatabaseURL, MinConnections: settings.DBMinConns, MaxConnections: settings.DBMaxConns,
|
||||
MaxConnectionAge: settings.DBMaxConnectionAge, MaxConnectionIdle: settings.DBMaxConnectionIdle,
|
||||
HealthCheckPeriod: settings.DBHealthCheckPeriod, ConnectTimeout: settings.DependencyTimeout,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"io"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/config"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/database"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/objectstore"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/postgres"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/reconcile"
|
||||
)
|
||||
|
||||
type reconciliationOutput struct {
|
||||
Mode string `json:"mode"`
|
||||
GracePeriod string `json:"grace_period"`
|
||||
UnreferencedRetention string `json:"unreferenced_retention"`
|
||||
PageSize int `json:"page_size"`
|
||||
DurationMillis int64 `json:"duration_ms"`
|
||||
Report reconcile.Report `json:"report"`
|
||||
}
|
||||
|
||||
func RunReconciliation(arguments []string, output io.Writer) error {
|
||||
flags := flag.NewFlagSet("reconcile-attachments", flag.ContinueOnError)
|
||||
flags.SetOutput(io.Discard)
|
||||
apply := flags.Bool("apply", false, "apply safe repairs")
|
||||
gracePeriod := flags.Duration("grace-period", time.Hour, "minimum anomaly age")
|
||||
retention := flags.Duration("unreferenced-retention", 24*time.Hour, "retention for ready unreferenced attachments")
|
||||
pageSize := flags.Int("page-size", 500, "S3 and PostgreSQL page size")
|
||||
if err := flags.Parse(arguments); err != nil || flags.NArg() != 0 {
|
||||
return errors.New("usage: messenger reconcile-attachments [--apply] [--grace-period=1h] [--unreferenced-retention=24h] [--page-size=500]")
|
||||
}
|
||||
if *apply && *gracePeriod <= 0 {
|
||||
return errors.New("grace-period must be positive in apply mode")
|
||||
}
|
||||
settings, err := config.LoadReconciliation()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
pool, err := database.Open(ctx, database.Config{
|
||||
URL: settings.DatabaseURL, MinConnections: settings.DBMinConns, MaxConnections: settings.DBMaxConns,
|
||||
MaxConnectionAge: settings.DBMaxConnectionAge, MaxConnectionIdle: settings.DBMaxConnectionIdle,
|
||||
HealthCheckPeriod: settings.DBHealthCheckPeriod, ConnectTimeout: settings.DependencyTimeout,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
objects, err := objectstore.New(objectstore.Config{
|
||||
Endpoint: settings.S3Endpoint, Region: settings.S3Region, Bucket: settings.S3Bucket,
|
||||
AccessKey: settings.S3AccessKey, SecretKey: settings.S3SecretKey,
|
||||
UseTLS: settings.S3UseTLS, CAPath: settings.S3CAPath, Timeout: settings.S3Timeout,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reconciler, err := reconcile.New(postgres.New(pool), objects, reconcile.Config{
|
||||
Prefix: "attachments/", PageSize: *pageSize, GraceTime: *gracePeriod,
|
||||
OrphanPrefixes: []string{"staging/"},
|
||||
UnreferencedRetention: *retention, Apply: *apply,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
started := time.Now()
|
||||
report, err := reconciler.Run(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mode := "dry-run"
|
||||
if *apply {
|
||||
mode = "apply"
|
||||
}
|
||||
return json.NewEncoder(output).Encode(reconciliationOutput{
|
||||
Mode: mode, GracePeriod: gracePeriod.String(), UnreferencedRetention: retention.String(),
|
||||
PageSize: *pageSize, DurationMillis: time.Since(started).Milliseconds(), Report: report,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestApplyReconciliationRequiresPositiveGracePeriod(t *testing.T) {
|
||||
err := RunReconciliation([]string{"--apply", "--grace-period=0"}, io.Discard)
|
||||
require.EqualError(t, err, "grace-period must be positive in apply mode")
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/config"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/database"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/postgres"
|
||||
)
|
||||
|
||||
func RunAttachmentReplay(arguments []string, output io.Writer) error {
|
||||
if len(arguments) != 3 {
|
||||
return errors.New("usage: messenger replay-attachment <attachment-id> <actor> <reason>")
|
||||
}
|
||||
settings, err := config.LoadJobAdmin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pool, err := database.Open(context.Background(), database.Config{
|
||||
URL: settings.DatabaseURL, MinConnections: settings.DBMinConns, MaxConnections: settings.DBMaxConns,
|
||||
MaxConnectionAge: settings.DBMaxConnectionAge, MaxConnectionIdle: settings.DBMaxConnectionIdle,
|
||||
HealthCheckPeriod: settings.DBHealthCheckPeriod, ConnectTimeout: settings.DependencyTimeout,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
job, err := postgres.New(pool).ReplayFailedAttachment(
|
||||
context.Background(), arguments[0], arguments[1], arguments[2],
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = fmt.Fprintf(output, "replayed attachment=%s job=%s attempt=%d\n", job.AttachmentID, job.ID, job.Attempt)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/config"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/database"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/loginlimit"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/messenger/attachment"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/messenger/httpapi"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/objectstore"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/password"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/postgres"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/security/tlsconfig"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/session"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/telemetry"
|
||||
)
|
||||
|
||||
type serverResult struct {
|
||||
name string
|
||||
err error
|
||||
}
|
||||
|
||||
func RunServer() error {
|
||||
settings, err := config.LoadServer()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
apiTLSConfiguration, err := tlsconfig.LoadServer(tlsconfig.ServerConfig{
|
||||
CAPath: settings.TLSCAPath, CertificatePath: settings.TLSCertificatePath,
|
||||
PrivateKeyPath: settings.TLSPrivateKeyPath, AllowedClientURI: settings.TLSAllowedClientURI,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metricsTLSConfiguration, err := tlsconfig.LoadServer(tlsconfig.ServerConfig{
|
||||
CAPath: settings.TLSCAPath, CertificatePath: settings.TLSCertificatePath,
|
||||
PrivateKeyPath: settings.TLSPrivateKeyPath, AllowedClientURI: settings.TLSAllowedMetricsClientURI,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
instanceUUID, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
instanceID := "api-" + instanceUUID.String()
|
||||
observability, logger, err := startTelemetry("messenger-api", instanceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer shutdownTelemetry(observability)
|
||||
pool, err := database.Open(context.Background(), databaseConfig(settings))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
if err := telemetry.ObserveDatabasePool(observability.MeterProvider().Meter("messenger-api/database"), pool); err != nil {
|
||||
return err
|
||||
}
|
||||
cacheTLSConfiguration, err := tlsconfig.LoadClient(tlsconfig.ClientConfig{
|
||||
CAPath: settings.CacheTLS.CAPath, CertificatePath: settings.CacheTLS.CertificatePath,
|
||||
PrivateKeyPath: settings.CacheTLS.PrivateKeyPath, ServerName: settings.CacheTLS.ServerName,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sessions, err := session.New(settings.CacheURL, cacheTLSConfiguration)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = sessions.Close() }()
|
||||
loginLimiter, err := loginlimit.New(settings.CacheURL, loginlimit.Config{
|
||||
AccountAttempts: int64(settings.LoginRateLimitAccountAttempts),
|
||||
SourceAttempts: int64(settings.LoginRateLimitSourceAttempts),
|
||||
Window: settings.LoginRateLimitWindow,
|
||||
}, cacheTLSConfiguration)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = loginLimiter.Close() }()
|
||||
passwords, err := password.New(password.Config{
|
||||
MemoryKiB: settings.PasswordMemoryKiB, Iterations: settings.PasswordIterations,
|
||||
Parallelism: settings.PasswordParallelism, SaltLength: settings.PasswordSaltLength,
|
||||
KeyLength: settings.PasswordKeyLength,
|
||||
}, settings.PasswordMaxConcurrentOperations)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
objects, err := objectstore.New(objectStoreConfig(settings))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
repository := postgres.New(pool)
|
||||
attachments, err := attachment.NewService(logger, repository, objects, attachment.Config{
|
||||
MaxBytes: settings.AttachmentMaxBytes, MaxPixels: settings.AttachmentMaxPixels,
|
||||
BodyMemoryBytes: settings.AttachmentBodyMemory, DecodeMemoryBytes: settings.AttachmentDecodeMemory,
|
||||
CleanupTimeout: settings.RequestTimeout, PipelineVersion: settings.PipelineVersion,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
app := httpapi.NewServer(logger, httpapi.Dependencies{
|
||||
Users: repository, Chats: repository, Messages: repository, Attachments: attachments,
|
||||
Sessions: sessions, Passwords: passwords, LoginLimiter: loginLimiter,
|
||||
Database: repository, Objects: objects,
|
||||
Meter: observability.MeterProvider().Meter("messenger-api"), Metrics: observability.MetricsHandler(),
|
||||
}, httpapi.Config{
|
||||
SessionTTL: settings.SessionTTL, IdempotencyTTL: settings.IdempotencyTTL,
|
||||
RequestTTL: settings.RequestTimeout, InstanceID: instanceID, SwaggerPort: settings.SwaggerPort,
|
||||
})
|
||||
apiServer := &http.Server{
|
||||
Addr: settings.Address, Handler: app.Handler(), ReadHeaderTimeout: settings.HTTPReadHeaderTimeout,
|
||||
ReadTimeout: settings.HTTPReadTimeout, WriteTimeout: settings.HTTPWriteTimeout,
|
||||
IdleTimeout: settings.HTTPIdleTimeout, TLSConfig: apiTLSConfiguration,
|
||||
}
|
||||
healthServer := &http.Server{
|
||||
Addr: settings.HealthAddress, Handler: app.HealthHandler(),
|
||||
ReadHeaderTimeout: settings.HTTPReadHeaderTimeout, ReadTimeout: settings.HTTPReadTimeout,
|
||||
WriteTimeout: settings.HTTPWriteTimeout, IdleTimeout: settings.HTTPIdleTimeout,
|
||||
}
|
||||
metricsServer := &http.Server{
|
||||
Addr: settings.MetricsAddress, Handler: app.MetricsHandler(),
|
||||
ReadHeaderTimeout: settings.HTTPReadHeaderTimeout, ReadTimeout: settings.HTTPReadTimeout,
|
||||
WriteTimeout: settings.HTTPWriteTimeout, IdleTimeout: settings.HTTPIdleTimeout,
|
||||
TLSConfig: metricsTLSConfiguration,
|
||||
}
|
||||
apiListener, err := net.Listen("tcp", apiServer.Addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
healthListener, err := net.Listen("tcp", healthServer.Addr)
|
||||
if err != nil {
|
||||
_ = apiListener.Close()
|
||||
return err
|
||||
}
|
||||
metricsListener, err := net.Listen("tcp", metricsServer.Addr)
|
||||
if err != nil {
|
||||
_ = apiListener.Close()
|
||||
_ = healthListener.Close()
|
||||
return err
|
||||
}
|
||||
serveResults := make(chan serverResult, 3)
|
||||
go serveHTTP(serveResults, "api", apiServer, tls.NewListener(apiListener, apiTLSConfiguration))
|
||||
go serveHTTP(serveResults, "health", healthServer, healthListener)
|
||||
go serveHTTP(serveResults, "metrics", metricsServer, tls.NewListener(metricsListener, metricsTLSConfiguration))
|
||||
logger.Info("servers started", "api_address", apiServer.Addr, "health_address", healthServer.Addr, "metrics_address", metricsServer.Addr)
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
var serveErr error
|
||||
select {
|
||||
case result := <-serveResults:
|
||||
serveErr = fmt.Errorf("%s server stopped: %w", result.name, result.err)
|
||||
case <-ctx.Done():
|
||||
logger.Info("shutdown requested")
|
||||
}
|
||||
app.SetReady(false)
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), settings.HTTPShutdownTimeout)
|
||||
defer cancel()
|
||||
shutdownErr := shutdownServers(shutdownCtx, apiServer, healthServer, metricsServer)
|
||||
return errors.Join(serveErr, shutdownErr)
|
||||
}
|
||||
|
||||
func serveHTTP(results chan<- serverResult, name string, server *http.Server, listener net.Listener) {
|
||||
results <- serverResult{name: name, err: server.Serve(listener)}
|
||||
}
|
||||
|
||||
func shutdownServers(ctx context.Context, servers ...*http.Server) error {
|
||||
results := make(chan error, len(servers))
|
||||
for _, server := range servers {
|
||||
go func() { results <- server.Shutdown(ctx) }()
|
||||
}
|
||||
var shutdownErr error
|
||||
for range servers {
|
||||
shutdownErr = errors.Join(shutdownErr, <-results)
|
||||
}
|
||||
if shutdownErr == nil {
|
||||
return nil
|
||||
}
|
||||
for _, server := range servers {
|
||||
_ = server.Close()
|
||||
}
|
||||
return shutdownErr
|
||||
}
|
||||
|
||||
func databaseConfig(settings config.ServerConfig) database.Config {
|
||||
return database.Config{
|
||||
URL: settings.DatabaseURL, MinConnections: settings.DBMinConns, MaxConnections: settings.DBMaxConns,
|
||||
MaxConnectionAge: settings.DBMaxConnectionAge, MaxConnectionIdle: settings.DBMaxConnectionIdle,
|
||||
HealthCheckPeriod: settings.DBHealthCheckPeriod, ConnectTimeout: settings.DependencyTimeout,
|
||||
}
|
||||
}
|
||||
|
||||
func objectStoreConfig(settings config.ServerConfig) objectstore.Config {
|
||||
return objectstore.Config{
|
||||
Endpoint: settings.S3Endpoint, Region: settings.S3Region, Bucket: settings.S3Bucket,
|
||||
AccessKey: settings.S3AccessKey, SecretKey: settings.S3SecretKey,
|
||||
UseTLS: settings.S3UseTLS, CAPath: settings.S3CAPath, Timeout: settings.S3Timeout,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/telemetry"
|
||||
)
|
||||
|
||||
func startTelemetry(serviceName, instanceID string) (*telemetry.Runtime, *slog.Logger, error) {
|
||||
runtime, err := telemetry.New(context.Background(), telemetry.ConfigFromEnvironment(serviceName, instanceID))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return runtime, runtime.Logger(), nil
|
||||
}
|
||||
|
||||
func shutdownTelemetry(runtime *telemetry.Runtime) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
_ = runtime.Shutdown(ctx)
|
||||
}
|
||||
@@ -0,0 +1,309 @@
|
||||
package redisstream
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/outbox"
|
||||
)
|
||||
|
||||
const (
|
||||
payloadField = "payload"
|
||||
failedPayloadMaxBytes = 64 << 10
|
||||
)
|
||||
|
||||
var failureCodePattern = regexp.MustCompile(`^[a-z][a-z0-9_]{0,63}$`)
|
||||
|
||||
var ErrDeliveryNotOwned = errors.New("redis stream delivery is not owned by this consumer")
|
||||
|
||||
var _ outbox.Publisher = (*Broker)(nil)
|
||||
|
||||
type Config struct {
|
||||
Stream string
|
||||
Group string
|
||||
Consumer string
|
||||
FailedStream string
|
||||
FailedMaxLen int64
|
||||
Block time.Duration
|
||||
ClaimMinIdle time.Duration
|
||||
ReadCount int64
|
||||
}
|
||||
|
||||
type Broker struct {
|
||||
client redis.UniversalClient
|
||||
config Config
|
||||
}
|
||||
|
||||
type Delivery struct {
|
||||
ID string
|
||||
Envelope Envelope
|
||||
DecodeError error
|
||||
|
||||
broker *Broker
|
||||
payload []byte
|
||||
}
|
||||
|
||||
type FailedDelivery struct {
|
||||
Code string
|
||||
FailedAt time.Time
|
||||
}
|
||||
|
||||
func New(client redis.UniversalClient, config Config) (*Broker, error) {
|
||||
if client == nil {
|
||||
return nil, errors.New("redis client is required")
|
||||
}
|
||||
if err := config.validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Broker{client: client, config: config}, nil
|
||||
}
|
||||
|
||||
func (c Config) validate() error {
|
||||
switch {
|
||||
case c.Stream == "":
|
||||
return errors.New("stream is required")
|
||||
case c.Group == "":
|
||||
return errors.New("consumer group is required")
|
||||
case c.Consumer == "":
|
||||
return errors.New("consumer name is required")
|
||||
case c.FailedStream == "":
|
||||
return errors.New("failed stream is required")
|
||||
case c.FailedStream == c.Stream:
|
||||
return errors.New("failed stream must differ from job stream")
|
||||
case c.FailedMaxLen <= 0:
|
||||
return errors.New("failed stream maximum length must be positive")
|
||||
case c.Block < 0:
|
||||
return errors.New("block duration must not be negative")
|
||||
case c.ClaimMinIdle <= 0:
|
||||
return errors.New("claim minimum idle time must be positive")
|
||||
case c.ReadCount <= 0:
|
||||
return errors.New("read count must be positive")
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Broker) Ensure(ctx context.Context) error {
|
||||
err := b.client.XGroupCreateMkStream(ctx, b.config.Stream, b.config.Group, "0").Err()
|
||||
if err != nil && !isBusyGroup(err) {
|
||||
return fmt.Errorf("create redis stream consumer group: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isBusyGroup(err error) bool {
|
||||
return err != nil && strings.HasPrefix(err.Error(), "BUSYGROUP")
|
||||
}
|
||||
|
||||
func (b *Broker) PublishConfirmed(ctx context.Context, payload []byte) (string, error) {
|
||||
ctx, span := otel.Tracer("redisstream").Start(ctx, "redis XADD", trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(attribute.String("db.system", "redis"), attribute.String("db.operation.name", "XADD"),
|
||||
attribute.String("messaging.destination.name", b.config.Stream)))
|
||||
defer span.End()
|
||||
envelope, err := DecodeEnvelope(payload)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
payload, err = EncodeEnvelope(envelope)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
id, err := b.client.XAdd(ctx, &redis.XAddArgs{
|
||||
Stream: b.config.Stream,
|
||||
Values: map[string]any{payloadField: string(payload)},
|
||||
}).Result()
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, "XADD failed")
|
||||
return "", fmt.Errorf("publish redis stream job: %w", err)
|
||||
}
|
||||
span.SetAttributes(attribute.String("messaging.message.id", id))
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (b *Broker) Read(ctx context.Context) ([]Delivery, error) {
|
||||
ctx, span := otel.Tracer("redisstream").Start(ctx, "redis XREADGROUP", trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(attribute.String("db.system", "redis"), attribute.String("db.operation.name", "XREADGROUP"),
|
||||
attribute.String("messaging.destination.name", b.config.Stream)))
|
||||
defer span.End()
|
||||
streams, err := b.client.XReadGroup(ctx, &redis.XReadGroupArgs{
|
||||
Group: b.config.Group,
|
||||
Consumer: b.config.Consumer,
|
||||
Streams: []string{b.config.Stream, ">"},
|
||||
Count: b.config.ReadCount,
|
||||
Block: b.config.Block,
|
||||
}).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, "XREADGROUP failed")
|
||||
return nil, fmt.Errorf("read redis stream jobs: %w", err)
|
||||
}
|
||||
return b.deliveries(streams)
|
||||
}
|
||||
|
||||
func (b *Broker) Reclaim(ctx context.Context, start string) ([]Delivery, string, error) {
|
||||
messages, next, err := b.client.XAutoClaim(ctx, &redis.XAutoClaimArgs{
|
||||
Stream: b.config.Stream,
|
||||
Group: b.config.Group,
|
||||
Consumer: b.config.Consumer,
|
||||
MinIdle: b.config.ClaimMinIdle,
|
||||
Start: start,
|
||||
Count: b.config.ReadCount,
|
||||
}).Result()
|
||||
if err != nil {
|
||||
return nil, start, fmt.Errorf("reclaim redis stream jobs: %w", err)
|
||||
}
|
||||
|
||||
deliveries, err := b.deliveries([]redis.XStream{{Stream: b.config.Stream, Messages: messages}})
|
||||
if err != nil {
|
||||
return nil, start, err
|
||||
}
|
||||
return deliveries, next, nil
|
||||
}
|
||||
|
||||
func (b *Broker) deliveries(streams []redis.XStream) ([]Delivery, error) {
|
||||
var deliveries []Delivery
|
||||
for _, stream := range streams {
|
||||
for _, message := range stream.Messages {
|
||||
delivery := Delivery{ID: message.ID, broker: b}
|
||||
payload, err := messagePayload(message)
|
||||
if err != nil {
|
||||
delivery.DecodeError = fmt.Errorf("decode redis stream message %s: %w", message.ID, err)
|
||||
deliveries = append(deliveries, delivery)
|
||||
continue
|
||||
}
|
||||
delivery.payload = boundedPayload(payload)
|
||||
envelope, err := DecodeEnvelope(payload)
|
||||
if err != nil {
|
||||
delivery.DecodeError = fmt.Errorf("decode redis stream message %s: %w", message.ID, err)
|
||||
deliveries = append(deliveries, delivery)
|
||||
continue
|
||||
}
|
||||
delivery.Envelope = envelope
|
||||
delivery.payload, err = EncodeEnvelope(envelope)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("encode validated redis stream message %s: %w", message.ID, err)
|
||||
}
|
||||
deliveries = append(deliveries, delivery)
|
||||
}
|
||||
}
|
||||
return deliveries, nil
|
||||
}
|
||||
|
||||
func boundedPayload(payload []byte) []byte {
|
||||
if len(payload) > failedPayloadMaxBytes {
|
||||
payload = payload[:failedPayloadMaxBytes]
|
||||
}
|
||||
return append([]byte(nil), payload...)
|
||||
}
|
||||
|
||||
func messagePayload(message redis.XMessage) ([]byte, error) {
|
||||
value, ok := message.Values[payloadField]
|
||||
if !ok {
|
||||
return nil, errors.New("payload field is missing")
|
||||
}
|
||||
switch payload := value.(type) {
|
||||
case string:
|
||||
return []byte(payload), nil
|
||||
case []byte:
|
||||
return payload, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("payload has type %T", value)
|
||||
}
|
||||
}
|
||||
|
||||
func (d Delivery) Ack(ctx context.Context) error {
|
||||
if d.broker == nil {
|
||||
return errors.New("delivery is detached from broker")
|
||||
}
|
||||
acked, err := d.broker.client.Eval(ctx, ackOwnedScript, []string{d.broker.config.Stream},
|
||||
d.broker.config.Group, d.ID, d.broker.config.Consumer,
|
||||
).Int64()
|
||||
if err != nil {
|
||||
return fmt.Errorf("ack redis stream job: %w", err)
|
||||
}
|
||||
if acked == 0 {
|
||||
return ErrDeliveryNotOwned
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
const ackOwnedScript = `
|
||||
local pending = redis.call('XPENDING', KEYS[1], ARGV[1], ARGV[2], ARGV[2], 1)
|
||||
if #pending == 0 or pending[1][2] ~= ARGV[3] then
|
||||
return 0
|
||||
end
|
||||
local acked = redis.call('XACK', KEYS[1], ARGV[1], ARGV[2])
|
||||
if acked == 1 then
|
||||
redis.call('XDEL', KEYS[1], ARGV[2])
|
||||
end
|
||||
return acked
|
||||
`
|
||||
|
||||
func (d Delivery) ToFailed(ctx context.Context, failure FailedDelivery) error {
|
||||
if d.broker == nil {
|
||||
return errors.New("delivery is detached from broker")
|
||||
}
|
||||
if !failureCodePattern.MatchString(failure.Code) {
|
||||
return errors.New("failure code is invalid")
|
||||
}
|
||||
if failure.FailedAt.IsZero() {
|
||||
return errors.New("failure time is required")
|
||||
}
|
||||
|
||||
payload := d.payload
|
||||
if d.DecodeError == nil && len(payload) == 0 {
|
||||
var err error
|
||||
payload, err = EncodeEnvelope(d.Envelope)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
moved, err := d.broker.client.Eval(ctx, moveToFailedScript,
|
||||
[]string{d.broker.config.FailedStream, d.broker.config.Stream},
|
||||
d.broker.config.Group, d.ID, d.broker.config.Consumer, string(payload), failure.Code,
|
||||
failure.FailedAt.UTC().Format(time.RFC3339Nano), strconv.Itoa(d.Envelope.Attempt),
|
||||
strconv.FormatInt(d.broker.config.FailedMaxLen, 10),
|
||||
).Int64()
|
||||
if err != nil {
|
||||
return fmt.Errorf("move redis stream job to failed stream: %w", err)
|
||||
}
|
||||
if moved == 0 {
|
||||
return ErrDeliveryNotOwned
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
const moveToFailedScript = `
|
||||
local pending = redis.call('XPENDING', KEYS[2], ARGV[1], ARGV[2], ARGV[2], 1)
|
||||
if #pending == 0 or pending[1][2] ~= ARGV[3] then
|
||||
return 0
|
||||
end
|
||||
redis.call('XADD', KEYS[1], 'MAXLEN', '~', ARGV[8], '*',
|
||||
'payload', ARGV[4],
|
||||
'source_message_id', ARGV[2],
|
||||
'error_code', ARGV[5],
|
||||
'failed_at', ARGV[6],
|
||||
'attempt', ARGV[7])
|
||||
local acked = redis.call('XACK', KEYS[2], ARGV[1], ARGV[2])
|
||||
if acked == 1 then
|
||||
redis.call('XDEL', KEYS[2], ARGV[2])
|
||||
end
|
||||
return acked
|
||||
`
|
||||
@@ -0,0 +1,93 @@
|
||||
package redisstream
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestBrokerDecodesDeliveries(t *testing.T) {
|
||||
payload, err := EncodeEnvelope(validEnvelope())
|
||||
require.NoError(t, err)
|
||||
|
||||
broker := &Broker{config: Config{Stream: "attachment-jobs"}}
|
||||
deliveries, err := broker.deliveries([]redis.XStream{{
|
||||
Stream: "attachment-jobs",
|
||||
Messages: []redis.XMessage{{
|
||||
ID: "1-0",
|
||||
Values: map[string]any{payloadField: string(payload)},
|
||||
}},
|
||||
}})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, deliveries, 1)
|
||||
require.Equal(t, "1-0", deliveries[0].ID)
|
||||
require.Equal(t, validEnvelope(), deliveries[0].Envelope)
|
||||
}
|
||||
|
||||
func TestBrokerRejectsPayloadWithStorageMetadata(t *testing.T) {
|
||||
broker := &Broker{config: Config{Stream: "attachment-jobs"}}
|
||||
deliveries, err := broker.deliveries([]redis.XStream{{
|
||||
Stream: "attachment-jobs",
|
||||
Messages: []redis.XMessage{{
|
||||
ID: "1-0",
|
||||
Values: map[string]any{payloadField: `{
|
||||
"schema_version":1,
|
||||
"job_id":"11111111-1111-4111-8111-111111111111",
|
||||
"type":"attachment.process.v1",
|
||||
"attachment_id":"22222222-2222-4222-8222-222222222222",
|
||||
"pipeline_version":1,
|
||||
"attempt":1,
|
||||
"created_at":"2026-09-01T12:00:00Z",
|
||||
"storage_key":"private/object"
|
||||
}`},
|
||||
}},
|
||||
}})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, deliveries, 1)
|
||||
require.Error(t, deliveries[0].DecodeError)
|
||||
require.Contains(t, string(deliveries[0].payload), `"storage_key":"private/object"`)
|
||||
}
|
||||
|
||||
func TestBrokerBoundsMalformedPayloadForFailedStream(t *testing.T) {
|
||||
payload := make([]byte, failedPayloadMaxBytes+1)
|
||||
for index := range payload {
|
||||
payload[index] = 'x'
|
||||
}
|
||||
broker := &Broker{config: Config{Stream: "attachment-jobs"}}
|
||||
deliveries, err := broker.deliveries([]redis.XStream{{
|
||||
Stream: "attachment-jobs",
|
||||
Messages: []redis.XMessage{{
|
||||
ID: "1-0",
|
||||
Values: map[string]any{payloadField: payload},
|
||||
}},
|
||||
}})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, deliveries, 1)
|
||||
require.Error(t, deliveries[0].DecodeError)
|
||||
require.Len(t, deliveries[0].payload, failedPayloadMaxBytes)
|
||||
}
|
||||
|
||||
func TestPublishRejectsInvalidPayloadBeforeRedisCall(t *testing.T) {
|
||||
broker := &Broker{}
|
||||
_, err := broker.PublishConfirmed(t.Context(), []byte(`{"schema_version":2}`))
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestConfigValidation(t *testing.T) {
|
||||
config := Config{
|
||||
Stream: "attachment-jobs",
|
||||
Group: "attachment-workers",
|
||||
Consumer: "worker-1",
|
||||
FailedStream: "attachment-jobs-failed",
|
||||
FailedMaxLen: 1000,
|
||||
Block: time.Second,
|
||||
ClaimMinIdle: time.Minute,
|
||||
ReadCount: 10,
|
||||
}
|
||||
require.NoError(t, config.validate())
|
||||
|
||||
config.ClaimMinIdle = 0
|
||||
require.Error(t, config.validate())
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package redisstream
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"regexp"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
SchemaVersion = 1
|
||||
JobType = "attachment.process.v1"
|
||||
)
|
||||
|
||||
var (
|
||||
errInvalidEnvelope = errors.New("invalid job envelope")
|
||||
traceparentPattern = regexp.MustCompile(`^00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$`)
|
||||
uuidPattern = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
|
||||
)
|
||||
|
||||
type Envelope struct {
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
JobID uuid.UUID `json:"job_id"`
|
||||
Type string `json:"type"`
|
||||
AttachmentID uuid.UUID `json:"attachment_id"`
|
||||
PipelineVersion int `json:"pipeline_version"`
|
||||
Attempt int `json:"attempt"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Traceparent string `json:"traceparent,omitempty"`
|
||||
Tracestate string `json:"tracestate,omitempty"`
|
||||
}
|
||||
|
||||
func (e Envelope) Validate() error {
|
||||
switch {
|
||||
case e.SchemaVersion != SchemaVersion:
|
||||
return fmt.Errorf("%w: unsupported schema_version", errInvalidEnvelope)
|
||||
case e.Type != JobType:
|
||||
return fmt.Errorf("%w: unsupported type", errInvalidEnvelope)
|
||||
case e.JobID == uuid.Nil:
|
||||
return fmt.Errorf("%w: job_id is required", errInvalidEnvelope)
|
||||
case e.AttachmentID == uuid.Nil:
|
||||
return fmt.Errorf("%w: attachment_id is required", errInvalidEnvelope)
|
||||
case e.PipelineVersion < 1:
|
||||
return fmt.Errorf("%w: pipeline_version must be positive", errInvalidEnvelope)
|
||||
case e.Attempt < 1:
|
||||
return fmt.Errorf("%w: attempt must be positive", errInvalidEnvelope)
|
||||
case e.CreatedAt.IsZero():
|
||||
return fmt.Errorf("%w: created_at is required", errInvalidEnvelope)
|
||||
case e.Traceparent != "" && !traceparentPattern.MatchString(e.Traceparent):
|
||||
return fmt.Errorf("%w: invalid traceparent", errInvalidEnvelope)
|
||||
case utf8.RuneCountInString(e.Tracestate) > 512:
|
||||
return fmt.Errorf("%w: tracestate is too long", errInvalidEnvelope)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func EncodeEnvelope(envelope Envelope) ([]byte, error) {
|
||||
if err := envelope.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(envelope)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("encode job envelope: %w", err)
|
||||
}
|
||||
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func DecodeEnvelope(payload []byte) (Envelope, error) {
|
||||
var wire envelopeJSON
|
||||
decoder := json.NewDecoder(bytes.NewReader(payload))
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(&wire); err != nil {
|
||||
return Envelope{}, fmt.Errorf("%w: decode: %v", errInvalidEnvelope, err)
|
||||
}
|
||||
if err := ensureJSONEnd(decoder); err != nil {
|
||||
return Envelope{}, err
|
||||
}
|
||||
|
||||
jobID, err := parseUUID(wire.JobID, "job_id")
|
||||
if err != nil {
|
||||
return Envelope{}, err
|
||||
}
|
||||
attachmentID, err := parseUUID(wire.AttachmentID, "attachment_id")
|
||||
if err != nil {
|
||||
return Envelope{}, err
|
||||
}
|
||||
createdAt, err := time.Parse(time.RFC3339, wire.CreatedAt)
|
||||
if err != nil {
|
||||
return Envelope{}, fmt.Errorf("%w: invalid created_at", errInvalidEnvelope)
|
||||
}
|
||||
envelope := Envelope{
|
||||
SchemaVersion: wire.SchemaVersion,
|
||||
JobID: jobID,
|
||||
Type: wire.Type,
|
||||
AttachmentID: attachmentID,
|
||||
PipelineVersion: wire.PipelineVersion,
|
||||
Attempt: wire.Attempt,
|
||||
CreatedAt: createdAt,
|
||||
Traceparent: wire.Traceparent,
|
||||
Tracestate: wire.Tracestate,
|
||||
}
|
||||
if err := envelope.Validate(); err != nil {
|
||||
return Envelope{}, err
|
||||
}
|
||||
|
||||
return envelope, nil
|
||||
}
|
||||
|
||||
type envelopeJSON struct {
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
JobID string `json:"job_id"`
|
||||
Type string `json:"type"`
|
||||
AttachmentID string `json:"attachment_id"`
|
||||
PipelineVersion int `json:"pipeline_version"`
|
||||
Attempt int `json:"attempt"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
Traceparent string `json:"traceparent,omitempty"`
|
||||
Tracestate string `json:"tracestate,omitempty"`
|
||||
}
|
||||
|
||||
func parseUUID(value, field string) (uuid.UUID, error) {
|
||||
if !uuidPattern.MatchString(value) {
|
||||
return uuid.Nil, fmt.Errorf("%w: invalid %s", errInvalidEnvelope, field)
|
||||
}
|
||||
parsed, err := uuid.Parse(value)
|
||||
if err != nil {
|
||||
return uuid.Nil, fmt.Errorf("%w: invalid %s", errInvalidEnvelope, field)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func ensureJSONEnd(decoder *json.Decoder) error {
|
||||
var value any
|
||||
if err := decoder.Decode(&value); !errors.Is(err, io.EOF) {
|
||||
return fmt.Errorf("%w: trailing JSON value", errInvalidEnvelope)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package redisstream
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestEnvelopeRoundTrip(t *testing.T) {
|
||||
envelope := validEnvelope()
|
||||
payload, err := EncodeEnvelope(envelope)
|
||||
require.NoError(t, err)
|
||||
|
||||
decoded, err := DecodeEnvelope(payload)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, envelope, decoded)
|
||||
}
|
||||
|
||||
func TestDecodeEnvelopeRejectsUnknownField(t *testing.T) {
|
||||
payload := []byte(`{
|
||||
"schema_version":1,
|
||||
"job_id":"11111111-1111-4111-8111-111111111111",
|
||||
"type":"attachment.process.v1",
|
||||
"attachment_id":"22222222-2222-4222-8222-222222222222",
|
||||
"pipeline_version":1,
|
||||
"attempt":1,
|
||||
"created_at":"2026-09-01T12:00:00Z",
|
||||
"storage_key":"must-not-enter-the-broker"
|
||||
}`)
|
||||
|
||||
_, err := DecodeEnvelope(payload)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestEnvelopeValidation(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
mutate func(*Envelope)
|
||||
}{
|
||||
{name: "schema", mutate: func(e *Envelope) { e.SchemaVersion = 2 }},
|
||||
{name: "type", mutate: func(e *Envelope) { e.Type = "attachment.delete.v1" }},
|
||||
{name: "job id", mutate: func(e *Envelope) { e.JobID = uuid.Nil }},
|
||||
{name: "attachment id", mutate: func(e *Envelope) { e.AttachmentID = uuid.Nil }},
|
||||
{name: "pipeline", mutate: func(e *Envelope) { e.PipelineVersion = 0 }},
|
||||
{name: "attempt", mutate: func(e *Envelope) { e.Attempt = 0 }},
|
||||
{name: "created at", mutate: func(e *Envelope) { e.CreatedAt = time.Time{} }},
|
||||
{name: "traceparent", mutate: func(e *Envelope) { e.Traceparent = "invalid" }},
|
||||
{name: "tracestate length", mutate: func(e *Envelope) { e.Tracestate = string(make([]byte, 513)) }},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
envelope := validEnvelope()
|
||||
tt.mutate(&envelope)
|
||||
require.Error(t, envelope.Validate())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func validEnvelope() Envelope {
|
||||
return Envelope{
|
||||
SchemaVersion: SchemaVersion,
|
||||
JobID: uuid.MustParse("11111111-1111-4111-8111-111111111111"),
|
||||
Type: JobType,
|
||||
AttachmentID: uuid.MustParse("22222222-2222-4222-8222-222222222222"),
|
||||
PipelineVersion: 1,
|
||||
Attempt: 1,
|
||||
CreatedAt: time.Date(2026, 9, 1, 12, 0, 0, 0, time.UTC),
|
||||
Traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
|
||||
Tracestate: "vendor=value",
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,561 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type ServerConfig struct {
|
||||
Address string
|
||||
HealthAddress string
|
||||
MetricsAddress string
|
||||
TLSCAPath string
|
||||
TLSCertificatePath string
|
||||
TLSPrivateKeyPath string
|
||||
TLSAllowedClientURI string
|
||||
TLSAllowedMetricsClientURI string
|
||||
DatabaseURL string
|
||||
CacheURL string
|
||||
CacheTLS ClientTLSConfig
|
||||
SessionTTL time.Duration
|
||||
IdempotencyTTL time.Duration
|
||||
LoginRateLimitAccountAttempts int
|
||||
LoginRateLimitSourceAttempts int
|
||||
LoginRateLimitWindow time.Duration
|
||||
DBMaxConns int32
|
||||
DBMinConns int32
|
||||
DBMaxConnectionAge time.Duration
|
||||
DBMaxConnectionIdle time.Duration
|
||||
DBHealthCheckPeriod time.Duration
|
||||
DependencyTimeout time.Duration
|
||||
RequestTimeout time.Duration
|
||||
HTTPReadHeaderTimeout time.Duration
|
||||
HTTPReadTimeout time.Duration
|
||||
HTTPWriteTimeout time.Duration
|
||||
HTTPIdleTimeout time.Duration
|
||||
HTTPShutdownTimeout time.Duration
|
||||
PasswordMemoryKiB uint32
|
||||
PasswordIterations uint32
|
||||
PasswordParallelism uint8
|
||||
PasswordSaltLength uint32
|
||||
PasswordKeyLength uint32
|
||||
PasswordMaxConcurrentOperations int
|
||||
SwaggerPort string
|
||||
S3Endpoint string
|
||||
S3Region string
|
||||
S3Bucket string
|
||||
S3AccessKey string
|
||||
S3SecretKey string
|
||||
S3UseTLS bool
|
||||
S3CAPath string
|
||||
S3Timeout time.Duration
|
||||
AttachmentMaxBytes int64
|
||||
AttachmentMaxPixels int64
|
||||
AttachmentBodyMemory int64
|
||||
AttachmentDecodeMemory int64
|
||||
PipelineVersion int
|
||||
}
|
||||
|
||||
type MigrationConfig struct {
|
||||
DatabaseURL string
|
||||
DBMaxConns int32
|
||||
DBMinConns int32
|
||||
DBMaxConnectionAge time.Duration
|
||||
DBMaxConnectionIdle time.Duration
|
||||
DBHealthCheckPeriod time.Duration
|
||||
DependencyTimeout time.Duration
|
||||
MigrationTimeout time.Duration
|
||||
}
|
||||
|
||||
type ReconciliationConfig struct {
|
||||
DatabaseURL string
|
||||
DBMaxConns int32
|
||||
DBMinConns int32
|
||||
DBMaxConnectionAge time.Duration
|
||||
DBMaxConnectionIdle time.Duration
|
||||
DBHealthCheckPeriod time.Duration
|
||||
DependencyTimeout time.Duration
|
||||
S3Endpoint string
|
||||
S3Region string
|
||||
S3Bucket string
|
||||
S3AccessKey string
|
||||
S3SecretKey string
|
||||
S3UseTLS bool
|
||||
S3CAPath string
|
||||
S3Timeout time.Duration
|
||||
}
|
||||
|
||||
type databaseConfig struct {
|
||||
url string
|
||||
maxConns int
|
||||
minConns int
|
||||
maxConnectionAge time.Duration
|
||||
maxConnectionIdle time.Duration
|
||||
healthCheckPeriod time.Duration
|
||||
dependencyTimeout time.Duration
|
||||
}
|
||||
|
||||
type objectStorageConfig struct {
|
||||
endpoint string
|
||||
region string
|
||||
bucket string
|
||||
accessKey string
|
||||
secretKey string
|
||||
useTLS bool
|
||||
caPath string
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
type ClientTLSConfig struct {
|
||||
CAPath string
|
||||
CertificatePath string
|
||||
PrivateKeyPath string
|
||||
ServerName string
|
||||
}
|
||||
|
||||
type ServerTLSConfig struct {
|
||||
CAPath string
|
||||
CertificatePath string
|
||||
PrivateKeyPath string
|
||||
AllowedClientURI string
|
||||
}
|
||||
|
||||
func LoadServer() (ServerConfig, error) {
|
||||
values, err := loadValues()
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
|
||||
host, err := requiredString(values, "server.host")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
port, err := requiredPort(values, "server.port")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
healthAddress, err := loopbackAddress(values, "server.health_address")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
metricsAddress, err := nonLoopbackAddress(values, "server.metrics_address")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
tlsCAPath, err := requiredString(values, "server.tls.ca_path")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
tlsCertificatePath, err := requiredString(values, "server.tls.certificate_path")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
tlsPrivateKeyPath, err := requiredString(values, "server.tls.private_key_path")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
tlsAllowedClientURI, err := requiredString(values, "server.tls.allowed_client_uri")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
tlsAllowedMetricsClientURI, err := requiredString(values, "server.tls.allowed_metrics_client_uri")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
database, err := loadDatabaseConfig(values)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
cacheURL, err := requiredString(values, "cache.url")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
cacheTLS, err := loadClientTLSConfig(values, "cache.tls")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
store, err := requiredString(values, "server.session_store")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
if store != "valkey" {
|
||||
return ServerConfig{}, errors.New("server.session_store must be valkey")
|
||||
}
|
||||
|
||||
sessionTTL, err := duration(values, "server.session_ttl", time.Second, 30*24*time.Hour)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
idempotencyTTL, err := duration(values, "server.idempotency_ttl", 24*time.Hour, 30*24*time.Hour)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
loginRateLimitAccountAttempts, err := integer(values, "server.login_rate_limit.account_attempts", 1, 1_000_000)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
loginRateLimitSourceAttempts, err := integer(values, "server.login_rate_limit.source_attempts", 1, 1_000_000)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
loginRateLimitWindow, err := duration(values, "server.login_rate_limit.window", time.Second, 24*time.Hour)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
requestTimeout, err := duration(values, "server.request_timeout", time.Second, 5*time.Minute)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
readHeaderTimeout, err := duration(values, "server.http.read_header_timeout", time.Second, 5*time.Minute)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
readTimeout, err := duration(values, "server.http.read_timeout", time.Second, 5*time.Minute)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
writeTimeout, err := duration(values, "server.http.write_timeout", time.Second, 5*time.Minute)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
idleTimeout, err := duration(values, "server.http.idle_timeout", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
shutdownTimeout, err := duration(values, "server.http.shutdown_timeout", time.Second, 5*time.Minute)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
passwordMemory, err := integer(values, "server.password.memory_kib", 8*1024, 1024*1024)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
passwordIterations, err := integer(values, "server.password.iterations", 1, 20)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
passwordParallelism, err := integer(values, "server.password.parallelism", 1, 255)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
passwordSaltLength, err := integer(values, "server.password.salt_length", 8, 1024)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
passwordKeyLength, err := integer(values, "server.password.key_length", 16, 1024)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
passwordMaxConcurrentOperations, err := integer(values, "server.password.max_concurrent_operations", 1, 64)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
swaggerPort, err := requiredPort(values, "server.swagger_port")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
storage, err := loadObjectStorageConfig(values)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
attachmentMaxBytes, err := integer(values, "attachments.max_bytes", 1, 10*1024*1024)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
attachmentMaxPixels, err := integer(values, "attachments.max_pixels", 1, 40_000_000)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
attachmentBodyMemory, err := integer(values, "attachments.body_memory_bytes", 1, 1024*1024*1024)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
if int64(attachmentBodyMemory) < int64(attachmentMaxBytes) {
|
||||
return ServerConfig{}, fmt.Errorf("attachments.body_memory_bytes: must be at least %d", attachmentMaxBytes)
|
||||
}
|
||||
attachmentDecodeMemory, err := integer(values, "attachments.decode_memory_bytes", 1, 1024*1024*1024)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
minimumDecodeMemory := int64(attachmentMaxPixels) * 8
|
||||
if int64(attachmentDecodeMemory) < minimumDecodeMemory {
|
||||
return ServerConfig{}, fmt.Errorf("attachments.decode_memory_bytes: must be at least %d", minimumDecodeMemory)
|
||||
}
|
||||
pipelineVersion, err := integer(values, "pipeline.version", 1, 1000)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
|
||||
return ServerConfig{
|
||||
Address: net.JoinHostPort(host, port), HealthAddress: healthAddress, MetricsAddress: metricsAddress,
|
||||
TLSCAPath: tlsCAPath, TLSCertificatePath: tlsCertificatePath,
|
||||
TLSPrivateKeyPath: tlsPrivateKeyPath, TLSAllowedClientURI: tlsAllowedClientURI,
|
||||
TLSAllowedMetricsClientURI: tlsAllowedMetricsClientURI,
|
||||
DatabaseURL: database.url, CacheURL: cacheURL, CacheTLS: cacheTLS,
|
||||
SessionTTL: sessionTTL, IdempotencyTTL: idempotencyTTL,
|
||||
LoginRateLimitAccountAttempts: loginRateLimitAccountAttempts,
|
||||
LoginRateLimitSourceAttempts: loginRateLimitSourceAttempts,
|
||||
LoginRateLimitWindow: loginRateLimitWindow,
|
||||
DBMaxConns: int32(database.maxConns), DBMinConns: int32(database.minConns),
|
||||
DBMaxConnectionAge: database.maxConnectionAge, DBMaxConnectionIdle: database.maxConnectionIdle,
|
||||
DBHealthCheckPeriod: database.healthCheckPeriod, DependencyTimeout: database.dependencyTimeout,
|
||||
RequestTimeout: requestTimeout,
|
||||
HTTPReadHeaderTimeout: readHeaderTimeout, HTTPReadTimeout: readTimeout,
|
||||
HTTPWriteTimeout: writeTimeout, HTTPIdleTimeout: idleTimeout,
|
||||
HTTPShutdownTimeout: shutdownTimeout, PasswordMemoryKiB: uint32(passwordMemory),
|
||||
PasswordIterations: uint32(passwordIterations), PasswordParallelism: uint8(passwordParallelism),
|
||||
PasswordSaltLength: uint32(passwordSaltLength), PasswordKeyLength: uint32(passwordKeyLength),
|
||||
PasswordMaxConcurrentOperations: passwordMaxConcurrentOperations,
|
||||
SwaggerPort: swaggerPort, S3Endpoint: storage.endpoint, S3Region: storage.region, S3Bucket: storage.bucket,
|
||||
S3AccessKey: storage.accessKey, S3SecretKey: storage.secretKey,
|
||||
S3UseTLS: storage.useTLS, S3CAPath: storage.caPath, S3Timeout: storage.timeout,
|
||||
AttachmentMaxBytes: int64(attachmentMaxBytes), AttachmentMaxPixels: int64(attachmentMaxPixels),
|
||||
AttachmentBodyMemory: int64(attachmentBodyMemory),
|
||||
AttachmentDecodeMemory: int64(attachmentDecodeMemory),
|
||||
PipelineVersion: pipelineVersion,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func LoadMigration() (MigrationConfig, error) {
|
||||
values, err := loadValues()
|
||||
if err != nil {
|
||||
return MigrationConfig{}, err
|
||||
}
|
||||
database, err := loadDatabaseConfig(values)
|
||||
if err != nil {
|
||||
return MigrationConfig{}, err
|
||||
}
|
||||
migrationTimeout, err := duration(values, "migration.timeout", time.Second, 10*time.Minute)
|
||||
if err != nil {
|
||||
return MigrationConfig{}, err
|
||||
}
|
||||
return MigrationConfig{
|
||||
DatabaseURL: database.url, DBMaxConns: int32(database.maxConns), DBMinConns: int32(database.minConns),
|
||||
DBMaxConnectionAge: database.maxConnectionAge, DBMaxConnectionIdle: database.maxConnectionIdle,
|
||||
DBHealthCheckPeriod: database.healthCheckPeriod, DependencyTimeout: database.dependencyTimeout,
|
||||
MigrationTimeout: migrationTimeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func LoadReconciliation() (ReconciliationConfig, error) {
|
||||
values, err := loadValues()
|
||||
if err != nil {
|
||||
return ReconciliationConfig{}, err
|
||||
}
|
||||
database, err := loadDatabaseConfig(values)
|
||||
if err != nil {
|
||||
return ReconciliationConfig{}, err
|
||||
}
|
||||
storage, err := loadObjectStorageConfig(values)
|
||||
if err != nil {
|
||||
return ReconciliationConfig{}, err
|
||||
}
|
||||
return ReconciliationConfig{
|
||||
DatabaseURL: database.url, DBMaxConns: int32(database.maxConns), DBMinConns: int32(database.minConns),
|
||||
DBMaxConnectionAge: database.maxConnectionAge, DBMaxConnectionIdle: database.maxConnectionIdle,
|
||||
DBHealthCheckPeriod: database.healthCheckPeriod, DependencyTimeout: database.dependencyTimeout,
|
||||
S3Endpoint: storage.endpoint, S3Region: storage.region, S3Bucket: storage.bucket,
|
||||
S3AccessKey: storage.accessKey, S3SecretKey: storage.secretKey,
|
||||
S3UseTLS: storage.useTLS, S3CAPath: storage.caPath, S3Timeout: storage.timeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func loadObjectStorageConfig(values *viper.Viper) (objectStorageConfig, error) {
|
||||
endpoint, err := requiredString(values, "object_storage.endpoint")
|
||||
if err != nil {
|
||||
return objectStorageConfig{}, err
|
||||
}
|
||||
region, err := requiredString(values, "object_storage.region")
|
||||
if err != nil {
|
||||
return objectStorageConfig{}, err
|
||||
}
|
||||
bucket, err := requiredString(values, "object_storage.bucket")
|
||||
if err != nil {
|
||||
return objectStorageConfig{}, err
|
||||
}
|
||||
accessKey, err := requiredString(values, "object_storage.access_key")
|
||||
if err != nil {
|
||||
return objectStorageConfig{}, err
|
||||
}
|
||||
secretKey, err := requiredString(values, "object_storage.secret_key")
|
||||
if err != nil {
|
||||
return objectStorageConfig{}, err
|
||||
}
|
||||
useTLS, err := boolean(values, "object_storage.use_tls")
|
||||
if err != nil {
|
||||
return objectStorageConfig{}, err
|
||||
}
|
||||
caPath, err := requiredString(values, "object_storage.ca_path")
|
||||
if err != nil {
|
||||
return objectStorageConfig{}, err
|
||||
}
|
||||
timeout, err := duration(values, "object_storage.timeout", time.Second, 5*time.Minute)
|
||||
if err != nil {
|
||||
return objectStorageConfig{}, err
|
||||
}
|
||||
return objectStorageConfig{
|
||||
endpoint: endpoint, region: region, bucket: bucket, accessKey: accessKey,
|
||||
secretKey: secretKey, useTLS: useTLS, caPath: caPath, timeout: timeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func loadClientTLSConfig(values *viper.Viper, prefix string) (ClientTLSConfig, error) {
|
||||
caPath, err := requiredString(values, prefix+".ca_path")
|
||||
if err != nil {
|
||||
return ClientTLSConfig{}, err
|
||||
}
|
||||
certificatePath, err := requiredString(values, prefix+".certificate_path")
|
||||
if err != nil {
|
||||
return ClientTLSConfig{}, err
|
||||
}
|
||||
privateKeyPath, err := requiredString(values, prefix+".private_key_path")
|
||||
if err != nil {
|
||||
return ClientTLSConfig{}, err
|
||||
}
|
||||
serverName, err := requiredString(values, prefix+".server_name")
|
||||
if err != nil {
|
||||
return ClientTLSConfig{}, err
|
||||
}
|
||||
return ClientTLSConfig{
|
||||
CAPath: caPath, CertificatePath: certificatePath,
|
||||
PrivateKeyPath: privateKeyPath, ServerName: serverName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func loadDatabaseConfig(values *viper.Viper) (databaseConfig, error) {
|
||||
databaseURL, err := requiredString(values, "database.url")
|
||||
if err != nil {
|
||||
return databaseConfig{}, err
|
||||
}
|
||||
maxConns, err := integer(values, "database.max_connections", 1, 100)
|
||||
if err != nil {
|
||||
return databaseConfig{}, err
|
||||
}
|
||||
minConns, err := integer(values, "database.min_connections", 0, maxConns)
|
||||
if err != nil {
|
||||
return databaseConfig{}, err
|
||||
}
|
||||
maxConnectionAge, err := duration(values, "database.max_connection_age", time.Second, 24*time.Hour)
|
||||
if err != nil {
|
||||
return databaseConfig{}, err
|
||||
}
|
||||
maxConnectionIdle, err := duration(values, "database.max_connection_idle", time.Second, 24*time.Hour)
|
||||
if err != nil {
|
||||
return databaseConfig{}, err
|
||||
}
|
||||
healthCheckPeriod, err := duration(values, "database.health_check_period", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return databaseConfig{}, err
|
||||
}
|
||||
dependencyTimeout, err := duration(values, "database.dependency_timeout", time.Second, time.Minute)
|
||||
if err != nil {
|
||||
return databaseConfig{}, err
|
||||
}
|
||||
return databaseConfig{
|
||||
url: databaseURL, maxConns: maxConns, minConns: minConns,
|
||||
maxConnectionAge: maxConnectionAge, maxConnectionIdle: maxConnectionIdle,
|
||||
healthCheckPeriod: healthCheckPeriod, dependencyTimeout: dependencyTimeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func requiredString(values *viper.Viper, name string) (string, error) {
|
||||
value := strings.TrimSpace(values.GetString(name))
|
||||
if value == "" {
|
||||
return "", fmt.Errorf("%s is required", name)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func requiredPort(values *viper.Viper, name string) (string, error) {
|
||||
value, err := requiredString(values, name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := integerValue(value, 1, 65535); err != nil {
|
||||
return "", fmt.Errorf("%s: %w", name, err)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func loopbackAddress(values *viper.Viper, name string) (string, error) {
|
||||
value, err := requiredString(values, name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
host, port, err := net.SplitHostPort(value)
|
||||
if err != nil || host != "127.0.0.1" {
|
||||
return "", fmt.Errorf("%s: must use 127.0.0.1 with an explicit port", name)
|
||||
}
|
||||
if _, err := integerValue(port, 1, 65535); err != nil {
|
||||
return "", fmt.Errorf("%s: %w", name, err)
|
||||
}
|
||||
return net.JoinHostPort(host, port), nil
|
||||
}
|
||||
|
||||
func nonLoopbackAddress(values *viper.Viper, name string) (string, error) {
|
||||
value, err := requiredString(values, name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
host, port, err := net.SplitHostPort(value)
|
||||
if err != nil || host == "" || strings.EqualFold(host, "localhost") {
|
||||
return "", fmt.Errorf("%s: must use a non-loopback host with an explicit port", name)
|
||||
}
|
||||
if address := net.ParseIP(host); address != nil && address.IsLoopback() {
|
||||
return "", fmt.Errorf("%s: must use a non-loopback host with an explicit port", name)
|
||||
}
|
||||
if _, err := integerValue(port, 1, 65535); err != nil {
|
||||
return "", fmt.Errorf("%s: %w", name, err)
|
||||
}
|
||||
return net.JoinHostPort(host, port), nil
|
||||
}
|
||||
|
||||
func duration(values *viper.Viper, name string, minimum, maximum time.Duration) (time.Duration, error) {
|
||||
value, err := requiredString(values, name)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
parsed, err := time.ParseDuration(value)
|
||||
if err != nil || parsed < minimum || parsed > maximum {
|
||||
return 0, fmt.Errorf("%s: must be between %s and %s", name, minimum, maximum)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func integer(values *viper.Viper, name string, minimum, maximum int) (int, error) {
|
||||
value, err := requiredString(values, name)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
parsed, err := integerValue(value, minimum, maximum)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("%s: %w", name, err)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func integerValue(value string, minimum, maximum int) (int, error) {
|
||||
parsed, err := strconv.Atoi(value)
|
||||
if err != nil || parsed < minimum || parsed > maximum {
|
||||
return 0, fmt.Errorf("must be between %d and %d", minimum, maximum)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func boolean(values *viper.Viper, name string) (bool, error) {
|
||||
value, err := requiredString(values, name)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
parsed, err := strconv.ParseBool(value)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("%s: must be true or false", name)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLoadReconciliationDoesNotRequireServerSettings(t *testing.T) {
|
||||
setConfigEnvironment(t, validTestConfig)
|
||||
t.Setenv("APP_HOST", "")
|
||||
t.Setenv("CACHE_URL", "")
|
||||
t.Setenv("SWAGGER_PORT", "")
|
||||
|
||||
settings, err := LoadReconciliation()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "attachments", settings.S3Bucket)
|
||||
}
|
||||
|
||||
func TestLoadWorkerUsesYAMLForRuntimeSettings(t *testing.T) {
|
||||
setConfigEnvironment(t, validTestConfig)
|
||||
t.Setenv("WORKER_CONCURRENCY", "64")
|
||||
|
||||
settings, err := LoadWorker()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 2, settings.Concurrency)
|
||||
require.Equal(t, 5*time.Second, settings.HeartbeatInterval)
|
||||
}
|
||||
|
||||
func TestLoadWorkerRejectsHeartbeatAtLeaseBoundary(t *testing.T) {
|
||||
content := strings.Replace(validTestConfig, "heartbeat_interval: 5s", "heartbeat_interval: 30s", 1)
|
||||
setConfigEnvironment(t, content)
|
||||
|
||||
_, err := LoadWorker()
|
||||
require.ErrorContains(t, err, "heartbeat_interval must be less than worker.lease")
|
||||
}
|
||||
|
||||
func TestLoadValuesRejectsUnknownVersion(t *testing.T) {
|
||||
setConfigEnvironment(t, "version: 2\n")
|
||||
|
||||
_, err := loadValues()
|
||||
require.ErrorContains(t, err, "version: must be 1")
|
||||
}
|
||||
|
||||
func TestLoadServerLoadsTLSAndHealthSettings(t *testing.T) {
|
||||
setConfigEnvironment(t, validTestConfig)
|
||||
|
||||
settings, err := LoadServer()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "127.0.0.1:8082", settings.HealthAddress)
|
||||
require.Equal(t, "0.0.0.0:9091", settings.MetricsAddress)
|
||||
require.Equal(t, "/run/security/internal-ca/ca.crt", settings.TLSCAPath)
|
||||
require.Equal(t, "spiffe://mtusi/lab7/load-balancer", settings.TLSAllowedClientURI)
|
||||
require.Equal(t, "spiffe://mtusi/lab7/prometheus", settings.TLSAllowedMetricsClientURI)
|
||||
require.Equal(t, 5, settings.LoginRateLimitAccountAttempts)
|
||||
require.Equal(t, 20, settings.LoginRateLimitSourceAttempts)
|
||||
require.Equal(t, time.Minute, settings.LoginRateLimitWindow)
|
||||
require.Equal(t, 2, settings.PasswordMaxConcurrentOperations)
|
||||
}
|
||||
|
||||
func TestLoadServerRejectsNonLoopbackHealthAddress(t *testing.T) {
|
||||
content := strings.Replace(validTestConfig, "health_address: 127.0.0.1:8082", "health_address: 0.0.0.0:8082", 1)
|
||||
setConfigEnvironment(t, content)
|
||||
|
||||
_, err := LoadServer()
|
||||
require.ErrorContains(t, err, "server.health_address: must use 127.0.0.1")
|
||||
}
|
||||
|
||||
func TestLoadServerRejectsLoopbackMetricsAddress(t *testing.T) {
|
||||
content := strings.Replace(validTestConfig, "metrics_address: 0.0.0.0:9091", "metrics_address: 127.0.0.1:9091", 1)
|
||||
setConfigEnvironment(t, content)
|
||||
|
||||
_, err := LoadServer()
|
||||
require.ErrorContains(t, err, "server.metrics_address: must use a non-loopback host")
|
||||
}
|
||||
|
||||
func TestLoadServerUsesRenderedOpenBaoSecrets(t *testing.T) {
|
||||
setConfigEnvironment(t, validTestConfig)
|
||||
path := filepath.Join(t.TempDir(), "secrets.json")
|
||||
require.NoError(t, os.WriteFile(path, []byte(`{
|
||||
"database_url":"postgres://openbao/database",
|
||||
"cache_url":"rediss://openbao/cache",
|
||||
"s3_access_key":"openbao-api",
|
||||
"s3_secret_key":"openbao-secret"
|
||||
}`), 0o600))
|
||||
t.Setenv("MESSENGER_SECRETS_FILE", path)
|
||||
|
||||
settings, err := LoadServer()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "postgres://openbao/database", settings.DatabaseURL)
|
||||
require.Equal(t, "rediss://openbao/cache", settings.CacheURL)
|
||||
require.Equal(t, "openbao-api", settings.S3AccessKey)
|
||||
require.Equal(t, "openbao-secret", settings.S3SecretKey)
|
||||
}
|
||||
|
||||
func TestLoadServerRejectsUnknownOpenBaoSecret(t *testing.T) {
|
||||
setConfigEnvironment(t, validTestConfig)
|
||||
path := filepath.Join(t.TempDir(), "secrets.json")
|
||||
require.NoError(t, os.WriteFile(path, []byte(`{"root_token":"forbidden"}`), 0o600))
|
||||
t.Setenv("MESSENGER_SECRETS_FILE", path)
|
||||
|
||||
_, err := LoadServer()
|
||||
require.ErrorContains(t, err, "unsupported field root_token")
|
||||
}
|
||||
|
||||
func setConfigEnvironment(t *testing.T, content string) {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "messenger.yaml")
|
||||
require.NoError(t, os.WriteFile(path, []byte(content), 0o600))
|
||||
t.Setenv("MESSENGER_CONFIG_PATH", path)
|
||||
for name, value := range map[string]string{
|
||||
"APP_HOST": "0.0.0.0", "APP_PORT": "8080", "SWAGGER_PORT": "8081",
|
||||
"DATABASE_URL": "postgres://user:password@postgres:5432/messenger",
|
||||
"CACHE_URL": "redis://valkey:6379/0", "BROKER_URL": "redis://redis-broker:6379/0",
|
||||
"S3_ENDPOINT": "s3:9000", "S3_REGION": "us-east-1", "S3_BUCKET": "attachments",
|
||||
"S3_ACCESS_KEY": "application", "S3_SECRET_KEY": "application-secret",
|
||||
} {
|
||||
t.Setenv(name, value)
|
||||
}
|
||||
}
|
||||
|
||||
const validTestConfig = `version: 1
|
||||
server:
|
||||
health_address: 127.0.0.1:8082
|
||||
metrics_address: 0.0.0.0:9091
|
||||
tls:
|
||||
ca_path: /run/security/internal-ca/ca.crt
|
||||
certificate_path: /run/security/api/tls.crt
|
||||
private_key_path: /run/security/api/tls.key
|
||||
allowed_client_uri: spiffe://mtusi/lab7/load-balancer
|
||||
allowed_metrics_client_uri: spiffe://mtusi/lab7/prometheus
|
||||
session_store: valkey
|
||||
session_ttl: 1h
|
||||
idempotency_ttl: 24h
|
||||
login_rate_limit:
|
||||
account_attempts: 5
|
||||
source_attempts: 20
|
||||
window: 1m
|
||||
request_timeout: 5s
|
||||
http:
|
||||
read_header_timeout: 5s
|
||||
read_timeout: 10s
|
||||
write_timeout: 10s
|
||||
idle_timeout: 1m
|
||||
shutdown_timeout: 5s
|
||||
password:
|
||||
memory_kib: 65536
|
||||
iterations: 3
|
||||
parallelism: 2
|
||||
salt_length: 16
|
||||
key_length: 32
|
||||
max_concurrent_operations: 2
|
||||
database:
|
||||
min_connections: 0
|
||||
max_connections: 4
|
||||
max_connection_age: 5m
|
||||
max_connection_idle: 1m
|
||||
health_check_period: 30s
|
||||
dependency_timeout: 5s
|
||||
cache:
|
||||
tls:
|
||||
ca_path: /run/security/internal-ca/ca.crt
|
||||
certificate_path: /run/security/client/tls.crt
|
||||
private_key_path: /run/security/client/tls.key
|
||||
server_name: redis-gateway
|
||||
migration:
|
||||
timeout: 30s
|
||||
object_storage:
|
||||
use_tls: false
|
||||
ca_path: /run/security/internal-ca/ca.crt
|
||||
timeout: 5s
|
||||
attachments:
|
||||
max_bytes: 10485760
|
||||
max_pixels: 40000000
|
||||
body_memory_bytes: 67108864
|
||||
decode_memory_bytes: 402653184
|
||||
pipeline:
|
||||
version: 1
|
||||
broker:
|
||||
tls:
|
||||
ca_path: /run/security/internal-ca/ca.crt
|
||||
certificate_path: /run/security/client/tls.crt
|
||||
private_key_path: /run/security/client/tls.key
|
||||
server_name: redis-gateway
|
||||
stream: attachment-jobs
|
||||
consumer_group: attachment-workers
|
||||
failed_stream: attachment-jobs-failed
|
||||
failed_stream_max_length: 10000
|
||||
outbox:
|
||||
batch_size: 32
|
||||
lease: 15s
|
||||
poll_interval: 1s
|
||||
recovery_interval: 10s
|
||||
retry_delay: 2s
|
||||
retention: 168h
|
||||
cleanup_interval: 1m
|
||||
cleanup_batch_size: 1000
|
||||
worker:
|
||||
concurrency: 2
|
||||
prefetch: 4
|
||||
lease: 30s
|
||||
heartbeat_interval: 5s
|
||||
processing_timeout: 20s
|
||||
read_block: 2s
|
||||
reclaim_interval: 5s
|
||||
max_attempts: 5
|
||||
retry:
|
||||
base: 1s
|
||||
max: 30s
|
||||
memory_bytes: 738197504
|
||||
monitoring:
|
||||
metrics_address: 0.0.0.0:9091
|
||||
health_address: 127.0.0.1:9092
|
||||
tls:
|
||||
ca_path: /run/security/internal-ca/ca.crt
|
||||
certificate_path: /run/security/metrics/tls.crt
|
||||
private_key_path: /run/security/metrics/tls.key
|
||||
allowed_client_uri: spiffe://mtusi/lab7/prometheus
|
||||
`
|
||||
@@ -0,0 +1,99 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const supportedConfigVersion = 1
|
||||
|
||||
var environmentKeys = map[string]string{
|
||||
"server.host": "APP_HOST",
|
||||
"server.port": "APP_PORT",
|
||||
"server.health_address": "APP_HEALTH_ADDRESS",
|
||||
"server.metrics_address": "APP_METRICS_ADDRESS",
|
||||
"server.tls.ca_path": "APP_TLS_CA_PATH",
|
||||
"server.tls.certificate_path": "APP_TLS_CERTIFICATE_PATH",
|
||||
"server.tls.private_key_path": "APP_TLS_PRIVATE_KEY_PATH",
|
||||
"server.tls.allowed_client_uri": "APP_TLS_ALLOWED_CLIENT_URI",
|
||||
"server.tls.allowed_metrics_client_uri": "APP_TLS_ALLOWED_METRICS_CLIENT_URI",
|
||||
"server.swagger_port": "SWAGGER_PORT",
|
||||
"database.url": "DATABASE_URL",
|
||||
"cache.url": "CACHE_URL",
|
||||
"object_storage.endpoint": "S3_ENDPOINT",
|
||||
"object_storage.region": "S3_REGION",
|
||||
"object_storage.bucket": "S3_BUCKET",
|
||||
"object_storage.access_key": "S3_ACCESS_KEY",
|
||||
"object_storage.secret_key": "S3_SECRET_KEY",
|
||||
"broker.url": "BROKER_URL",
|
||||
}
|
||||
|
||||
var secretKeys = map[string]string{
|
||||
"database_url": "database.url",
|
||||
"cache_url": "cache.url",
|
||||
"broker_url": "broker.url",
|
||||
"s3_access_key": "object_storage.access_key",
|
||||
"s3_secret_key": "object_storage.secret_key",
|
||||
}
|
||||
|
||||
func loadValues() (*viper.Viper, error) {
|
||||
path := strings.TrimSpace(os.Getenv("MESSENGER_CONFIG_PATH"))
|
||||
if path == "" {
|
||||
return nil, errors.New("MESSENGER_CONFIG_PATH is required")
|
||||
}
|
||||
values := viper.New()
|
||||
values.SetConfigFile(path)
|
||||
if err := values.ReadInConfig(); err != nil {
|
||||
return nil, fmt.Errorf("read messenger config: %w", err)
|
||||
}
|
||||
if version := values.GetInt("version"); version != supportedConfigVersion {
|
||||
return nil, fmt.Errorf("version: must be %d", supportedConfigVersion)
|
||||
}
|
||||
for key, name := range environmentKeys {
|
||||
if err := values.BindEnv(key, name); err != nil {
|
||||
return nil, fmt.Errorf("bind %s: %w", name, err)
|
||||
}
|
||||
}
|
||||
if err := loadSecretFile(values, strings.TrimSpace(os.Getenv("MESSENGER_SECRETS_FILE"))); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func loadSecretFile(values *viper.Viper, path string) error {
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open messenger secrets: %w", err)
|
||||
}
|
||||
defer func() { _ = file.Close() }()
|
||||
decoder := json.NewDecoder(io.LimitReader(file, 64<<10))
|
||||
var secrets map[string]string
|
||||
if err := decoder.Decode(&secrets); err != nil {
|
||||
return fmt.Errorf("decode messenger secrets: %w", err)
|
||||
}
|
||||
var trailing any
|
||||
if err := decoder.Decode(&trailing); !errors.Is(err, io.EOF) {
|
||||
return errors.New("decode messenger secrets: trailing JSON value")
|
||||
}
|
||||
for name, value := range secrets {
|
||||
key, ok := secretKeys[name]
|
||||
if !ok {
|
||||
return fmt.Errorf("messenger secrets: unsupported field %s", name)
|
||||
}
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return fmt.Errorf("messenger secrets: %s is empty", name)
|
||||
}
|
||||
values.Set(key, value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type DispatcherConfig struct {
|
||||
DatabaseURL string
|
||||
DBMaxConns int32
|
||||
DBMinConns int32
|
||||
DBMaxConnectionAge time.Duration
|
||||
DBMaxConnectionIdle time.Duration
|
||||
DBHealthCheckPeriod time.Duration
|
||||
DependencyTimeout time.Duration
|
||||
BrokerURL string
|
||||
BrokerTLS ClientTLSConfig
|
||||
Stream string
|
||||
ConsumerGroup string
|
||||
FailedStream string
|
||||
FailedStreamMaxLength int64
|
||||
BatchSize int
|
||||
LeaseDuration time.Duration
|
||||
PollInterval time.Duration
|
||||
RecoveryInterval time.Duration
|
||||
RetryDelay time.Duration
|
||||
Retention time.Duration
|
||||
CleanupInterval time.Duration
|
||||
CleanupBatchSize int
|
||||
MetricsAddress string
|
||||
HealthAddress string
|
||||
MetricsTLS ServerTLSConfig
|
||||
}
|
||||
|
||||
type WorkerConfig struct {
|
||||
DatabaseURL string
|
||||
DBMaxConns int32
|
||||
DBMinConns int32
|
||||
DBMaxConnectionAge time.Duration
|
||||
DBMaxConnectionIdle time.Duration
|
||||
DBHealthCheckPeriod time.Duration
|
||||
DependencyTimeout time.Duration
|
||||
S3Endpoint string
|
||||
S3Region string
|
||||
S3Bucket string
|
||||
S3AccessKey string
|
||||
S3SecretKey string
|
||||
S3UseTLS bool
|
||||
S3CAPath string
|
||||
S3Timeout time.Duration
|
||||
BrokerURL string
|
||||
BrokerTLS ClientTLSConfig
|
||||
Stream string
|
||||
ConsumerGroup string
|
||||
FailedStream string
|
||||
FailedStreamMaxLength int64
|
||||
Concurrency int
|
||||
Prefetch int
|
||||
LeaseDuration time.Duration
|
||||
HeartbeatInterval time.Duration
|
||||
ProcessingTimeout time.Duration
|
||||
ReadBlock time.Duration
|
||||
ReclaimInterval time.Duration
|
||||
MaxAttempts int
|
||||
RetryBase time.Duration
|
||||
RetryMax time.Duration
|
||||
AttachmentMaxBytes int64
|
||||
AttachmentMaxPixels int64
|
||||
MemoryBytes int64
|
||||
MetricsAddress string
|
||||
HealthAddress string
|
||||
MetricsTLS ServerTLSConfig
|
||||
}
|
||||
|
||||
type JobAdminConfig struct {
|
||||
DatabaseURL string
|
||||
DBMaxConns int32
|
||||
DBMinConns int32
|
||||
DBMaxConnectionAge time.Duration
|
||||
DBMaxConnectionIdle time.Duration
|
||||
DBHealthCheckPeriod time.Duration
|
||||
DependencyTimeout time.Duration
|
||||
}
|
||||
|
||||
type brokerConfig struct {
|
||||
url string
|
||||
stream string
|
||||
consumerGroup string
|
||||
failedStream string
|
||||
failedStreamMaxLength int
|
||||
}
|
||||
|
||||
func LoadJobAdmin() (JobAdminConfig, error) {
|
||||
values, err := loadValues()
|
||||
if err != nil {
|
||||
return JobAdminConfig{}, err
|
||||
}
|
||||
database, err := loadDatabaseConfig(values)
|
||||
if err != nil {
|
||||
return JobAdminConfig{}, err
|
||||
}
|
||||
return JobAdminConfig{
|
||||
DatabaseURL: database.url, DBMaxConns: int32(database.maxConns), DBMinConns: int32(database.minConns),
|
||||
DBMaxConnectionAge: database.maxConnectionAge, DBMaxConnectionIdle: database.maxConnectionIdle,
|
||||
DBHealthCheckPeriod: database.healthCheckPeriod, DependencyTimeout: database.dependencyTimeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func LoadDispatcher() (DispatcherConfig, error) {
|
||||
values, err := loadValues()
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
database, err := loadDatabaseConfig(values)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
broker, err := loadBrokerConfig(values)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
brokerTLS, err := loadClientTLSConfig(values, "broker.tls")
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
batchSize, err := integer(values, "outbox.batch_size", 1, 1000)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
leaseDuration, err := duration(values, "outbox.lease", time.Second, 10*time.Minute)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
pollInterval, err := duration(values, "outbox.poll_interval", time.Millisecond, time.Minute)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
recoveryInterval, err := duration(values, "outbox.recovery_interval", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
retryDelay, err := duration(values, "outbox.retry_delay", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
retention, err := duration(values, "outbox.retention", time.Hour, 365*24*time.Hour)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
cleanupInterval, err := duration(values, "outbox.cleanup_interval", time.Minute, 24*time.Hour)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
cleanupBatchSize, err := integer(values, "outbox.cleanup_batch_size", 1, 1000)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
metricsAddress, err := metricsAddress(values)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
healthAddress, err := loopbackAddress(values, "monitoring.health_address")
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
metricsTLS, err := loadServerTLSConfig(values, "monitoring.tls")
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
return DispatcherConfig{
|
||||
DatabaseURL: database.url, DBMaxConns: int32(database.maxConns), DBMinConns: int32(database.minConns),
|
||||
DBMaxConnectionAge: database.maxConnectionAge, DBMaxConnectionIdle: database.maxConnectionIdle,
|
||||
DBHealthCheckPeriod: database.healthCheckPeriod, DependencyTimeout: database.dependencyTimeout,
|
||||
BrokerURL: broker.url, BrokerTLS: brokerTLS, Stream: broker.stream, ConsumerGroup: broker.consumerGroup,
|
||||
FailedStream: broker.failedStream, FailedStreamMaxLength: int64(broker.failedStreamMaxLength),
|
||||
BatchSize: batchSize, LeaseDuration: leaseDuration, PollInterval: pollInterval,
|
||||
RecoveryInterval: recoveryInterval, RetryDelay: retryDelay,
|
||||
Retention: retention, CleanupInterval: cleanupInterval, CleanupBatchSize: cleanupBatchSize,
|
||||
MetricsAddress: metricsAddress, HealthAddress: healthAddress, MetricsTLS: metricsTLS,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func LoadWorker() (WorkerConfig, error) {
|
||||
values, err := loadValues()
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
database, err := loadDatabaseConfig(values)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
storage, err := loadObjectStorageConfig(values)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
broker, err := loadBrokerConfig(values)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
brokerTLS, err := loadClientTLSConfig(values, "broker.tls")
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
concurrency, err := integer(values, "worker.concurrency", 1, 64)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
prefetch, err := integer(values, "worker.prefetch", concurrency, 1024)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
leaseDuration, err := duration(values, "worker.lease", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
heartbeatInterval, err := duration(values, "worker.heartbeat_interval", 100*time.Millisecond, time.Hour)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
if heartbeatInterval >= leaseDuration {
|
||||
return WorkerConfig{}, errors.New("worker.heartbeat_interval must be less than worker.lease")
|
||||
}
|
||||
processingTimeout, err := duration(values, "worker.processing_timeout", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
if processingTimeout >= leaseDuration {
|
||||
return WorkerConfig{}, errors.New("worker.processing_timeout must be less than worker.lease")
|
||||
}
|
||||
readBlock, err := duration(values, "worker.read_block", 100*time.Millisecond, time.Minute)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
reclaimInterval, err := duration(values, "worker.reclaim_interval", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
maxAttempts, err := integer(values, "worker.max_attempts", 1, 100)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
retryBase, err := duration(values, "worker.retry.base", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
retryMax, err := duration(values, "worker.retry.max", time.Second, 24*time.Hour)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
if retryMax < retryBase {
|
||||
return WorkerConfig{}, errors.New("worker.retry.max must be at least worker.retry.base")
|
||||
}
|
||||
attachmentMaxBytes, err := integer(values, "attachments.max_bytes", 1, 10*1024*1024)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
attachmentMaxPixels, err := integer(values, "attachments.max_pixels", 1, 40_000_000)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
memoryBytes, err := integer(values, "worker.memory_bytes", attachmentMaxBytes, 2*1024*1024*1024)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
metricsAddress, err := metricsAddress(values)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
healthAddress, err := loopbackAddress(values, "monitoring.health_address")
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
metricsTLS, err := loadServerTLSConfig(values, "monitoring.tls")
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
return WorkerConfig{
|
||||
DatabaseURL: database.url, DBMaxConns: int32(database.maxConns), DBMinConns: int32(database.minConns),
|
||||
DBMaxConnectionAge: database.maxConnectionAge, DBMaxConnectionIdle: database.maxConnectionIdle,
|
||||
DBHealthCheckPeriod: database.healthCheckPeriod, DependencyTimeout: database.dependencyTimeout,
|
||||
S3Endpoint: storage.endpoint, S3Region: storage.region, S3Bucket: storage.bucket,
|
||||
S3AccessKey: storage.accessKey, S3SecretKey: storage.secretKey, S3UseTLS: storage.useTLS,
|
||||
S3CAPath: storage.caPath, S3Timeout: storage.timeout,
|
||||
BrokerURL: broker.url, BrokerTLS: brokerTLS, Stream: broker.stream,
|
||||
ConsumerGroup: broker.consumerGroup, FailedStream: broker.failedStream,
|
||||
FailedStreamMaxLength: int64(broker.failedStreamMaxLength),
|
||||
Concurrency: concurrency, Prefetch: prefetch, LeaseDuration: leaseDuration,
|
||||
HeartbeatInterval: heartbeatInterval, ProcessingTimeout: processingTimeout, ReadBlock: readBlock,
|
||||
ReclaimInterval: reclaimInterval, MaxAttempts: maxAttempts, RetryBase: retryBase, RetryMax: retryMax,
|
||||
AttachmentMaxBytes: int64(attachmentMaxBytes), AttachmentMaxPixels: int64(attachmentMaxPixels),
|
||||
MemoryBytes: int64(memoryBytes), MetricsAddress: metricsAddress,
|
||||
HealthAddress: healthAddress, MetricsTLS: metricsTLS,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func metricsAddress(values *viper.Viper) (string, error) {
|
||||
address, err := requiredString(values, "monitoring.metrics_address")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
_, port, err := net.SplitHostPort(address)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("monitoring.metrics_address: %w", err)
|
||||
}
|
||||
if _, err := integerValue(port, 1, 65535); err != nil {
|
||||
return "", fmt.Errorf("monitoring.metrics_address: %w", err)
|
||||
}
|
||||
return address, nil
|
||||
}
|
||||
|
||||
func loadServerTLSConfig(values *viper.Viper, prefix string) (ServerTLSConfig, error) {
|
||||
caPath, err := requiredString(values, prefix+".ca_path")
|
||||
if err != nil {
|
||||
return ServerTLSConfig{}, err
|
||||
}
|
||||
certificatePath, err := requiredString(values, prefix+".certificate_path")
|
||||
if err != nil {
|
||||
return ServerTLSConfig{}, err
|
||||
}
|
||||
privateKeyPath, err := requiredString(values, prefix+".private_key_path")
|
||||
if err != nil {
|
||||
return ServerTLSConfig{}, err
|
||||
}
|
||||
allowedClientURI, err := requiredString(values, prefix+".allowed_client_uri")
|
||||
if err != nil {
|
||||
return ServerTLSConfig{}, err
|
||||
}
|
||||
return ServerTLSConfig{
|
||||
CAPath: caPath, CertificatePath: certificatePath,
|
||||
PrivateKeyPath: privateKeyPath, AllowedClientURI: allowedClientURI,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func loadBrokerConfig(values *viper.Viper) (brokerConfig, error) {
|
||||
url, err := requiredString(values, "broker.url")
|
||||
if err != nil {
|
||||
return brokerConfig{}, err
|
||||
}
|
||||
stream, err := requiredString(values, "broker.stream")
|
||||
if err != nil {
|
||||
return brokerConfig{}, err
|
||||
}
|
||||
consumerGroup, err := requiredString(values, "broker.consumer_group")
|
||||
if err != nil {
|
||||
return brokerConfig{}, err
|
||||
}
|
||||
failedStream, err := requiredString(values, "broker.failed_stream")
|
||||
if err != nil {
|
||||
return brokerConfig{}, err
|
||||
}
|
||||
failedStreamMaxLength, err := integer(values, "broker.failed_stream_max_length", 100, 10_000_000)
|
||||
if err != nil {
|
||||
return brokerConfig{}, err
|
||||
}
|
||||
return brokerConfig{
|
||||
url: url, stream: stream, consumerGroup: consumerGroup,
|
||||
failedStream: failedStream, failedStreamMaxLength: failedStreamMaxLength,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab7/migrations"
|
||||
)
|
||||
|
||||
var (
|
||||
errURLRequired = errors.New("database URL is required")
|
||||
errMinConnections = errors.New("minimum database connections must not be negative")
|
||||
errMaxConnections = errors.New("maximum database connections must be positive")
|
||||
errConnectionRange = errors.New("minimum database connections exceed maximum")
|
||||
errMaxConnectionAge = errors.New("maximum database connection age must be positive")
|
||||
errMaxConnectionIdle = errors.New("maximum database connection idle time must be positive")
|
||||
errHealthCheckPeriod = errors.New("database health check period must be positive")
|
||||
errConnectionTimeout = errors.New("database connection timeout must be positive")
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
URL string
|
||||
MinConnections int32
|
||||
MaxConnections int32
|
||||
MaxConnectionAge time.Duration
|
||||
MaxConnectionIdle time.Duration
|
||||
HealthCheckPeriod time.Duration
|
||||
ConnectTimeout time.Duration
|
||||
}
|
||||
|
||||
func Open(ctx context.Context, config Config) (*pgxpool.Pool, error) {
|
||||
if err := validateConfig(config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
poolConfig, err := pgxpool.ParseConfig(config.URL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse database configuration: %w", err)
|
||||
}
|
||||
poolConfig.ConnConfig.Tracer = queryTracer{}
|
||||
poolConfig.MinConns = config.MinConnections
|
||||
poolConfig.MaxConns = config.MaxConnections
|
||||
poolConfig.MaxConnLifetime = config.MaxConnectionAge
|
||||
poolConfig.MaxConnIdleTime = config.MaxConnectionIdle
|
||||
poolConfig.HealthCheckPeriod = config.HealthCheckPeriod
|
||||
|
||||
connectCtx, cancel := context.WithTimeout(ctx, config.ConnectTimeout)
|
||||
defer cancel()
|
||||
pool, err := pgxpool.NewWithConfig(connectCtx, poolConfig)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create database pool: %w", err)
|
||||
}
|
||||
if err := pool.Ping(connectCtx); err != nil {
|
||||
pool.Close()
|
||||
return nil, fmt.Errorf("ping database: %w", err)
|
||||
}
|
||||
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
func Migrate(ctx context.Context, pool *pgxpool.Pool, timeout time.Duration) error {
|
||||
if timeout <= 0 {
|
||||
return errors.New("database migration timeout must be positive")
|
||||
}
|
||||
migrationCtx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
return applyMigrations(migrationCtx, pool, migrations.FS)
|
||||
}
|
||||
|
||||
func validateConfig(config Config) error {
|
||||
switch {
|
||||
case config.URL == "":
|
||||
return errURLRequired
|
||||
case config.MinConnections < 0:
|
||||
return errMinConnections
|
||||
case config.MaxConnections < 1:
|
||||
return errMaxConnections
|
||||
case config.MinConnections > config.MaxConnections:
|
||||
return errConnectionRange
|
||||
case config.MaxConnectionAge <= 0:
|
||||
return errMaxConnectionAge
|
||||
case config.MaxConnectionIdle <= 0:
|
||||
return errMaxConnectionIdle
|
||||
case config.HealthCheckPeriod <= 0:
|
||||
return errHealthCheckPeriod
|
||||
case config.ConnectTimeout <= 0:
|
||||
return errConnectionTimeout
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
const (
|
||||
migrationLockID int64 = 0x4d54555349324c42
|
||||
migrationUnlockTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
var migrationNamePattern = regexp.MustCompile(`^(\d+)_([a-z0-9_]+)\.sql$`)
|
||||
|
||||
type migration struct {
|
||||
version int64
|
||||
name string
|
||||
checksum string
|
||||
sql string
|
||||
}
|
||||
|
||||
func applyMigrations(ctx context.Context, pool *pgxpool.Pool, migrationFS fs.FS) (returnErr error) {
|
||||
migrations, err := readMigrations(migrationFS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
connection, err := pool.Acquire(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("acquire connection for migrations: %w", err)
|
||||
}
|
||||
defer connection.Release()
|
||||
if _, err := connection.Exec(ctx, "SELECT pg_advisory_lock($1)", migrationLockID); err != nil {
|
||||
return fmt.Errorf("lock migrations: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
unlockCtx, cancel := context.WithTimeout(context.Background(), migrationUnlockTimeout)
|
||||
defer cancel()
|
||||
if _, err := connection.Exec(unlockCtx, "SELECT pg_advisory_unlock($1)", migrationLockID); err != nil {
|
||||
_ = connection.Conn().Close(unlockCtx)
|
||||
if returnErr == nil {
|
||||
returnErr = fmt.Errorf("unlock migrations: %w", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
if _, err := connection.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version BIGINT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
checksum TEXT NOT NULL,
|
||||
applied_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp()
|
||||
)`); err != nil {
|
||||
return fmt.Errorf("create migration history: %w", err)
|
||||
}
|
||||
for _, migration := range migrations {
|
||||
if err := applyMigration(ctx, connection, migration); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func readMigrations(migrationFS fs.FS) ([]migration, error) {
|
||||
entries, err := fs.ReadDir(migrationFS, ".")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read embedded migrations: %w", err)
|
||||
}
|
||||
migrations := make([]migration, 0, len(entries))
|
||||
versions := make(map[int64]string)
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || filepath.Ext(entry.Name()) != ".sql" {
|
||||
continue
|
||||
}
|
||||
matches := migrationNamePattern.FindStringSubmatch(entry.Name())
|
||||
if matches == nil {
|
||||
return nil, fmt.Errorf("invalid migration filename %q", entry.Name())
|
||||
}
|
||||
version, err := strconv.ParseInt(matches[1], 10, 64)
|
||||
if err != nil || version < 1 {
|
||||
return nil, fmt.Errorf("invalid migration version in %q", entry.Name())
|
||||
}
|
||||
if previous, exists := versions[version]; exists {
|
||||
return nil, fmt.Errorf("migration version %d is used by %q and %q", version, previous, entry.Name())
|
||||
}
|
||||
data, err := fs.ReadFile(migrationFS, entry.Name())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read migration %q: %w", entry.Name(), err)
|
||||
}
|
||||
hash := sha256.Sum256(data)
|
||||
versions[version] = entry.Name()
|
||||
migrations = append(migrations, migration{
|
||||
version: version,
|
||||
name: entry.Name(),
|
||||
checksum: hex.EncodeToString(hash[:]),
|
||||
sql: string(data),
|
||||
})
|
||||
}
|
||||
if len(migrations) == 0 {
|
||||
return nil, errors.New("no database migrations found")
|
||||
}
|
||||
sort.Slice(migrations, func(i, j int) bool {
|
||||
return migrations[i].version < migrations[j].version
|
||||
})
|
||||
return migrations, nil
|
||||
}
|
||||
|
||||
func applyMigration(ctx context.Context, connection *pgxpool.Conn, migration migration) error {
|
||||
var checksum string
|
||||
err := connection.QueryRow(ctx, "SELECT checksum FROM schema_migrations WHERE version = $1", migration.version).Scan(&checksum)
|
||||
switch {
|
||||
case err == nil && checksum != migration.checksum:
|
||||
return fmt.Errorf("migration %q checksum does not match applied version", migration.name)
|
||||
case err == nil:
|
||||
return nil
|
||||
case !errors.Is(err, pgx.ErrNoRows):
|
||||
return fmt.Errorf("read migration %q state: %w", migration.name, err)
|
||||
}
|
||||
|
||||
transaction, err := connection.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin migration %q: %w", migration.name, err)
|
||||
}
|
||||
defer func() {
|
||||
_ = transaction.Rollback(context.WithoutCancel(ctx))
|
||||
}()
|
||||
if _, err := transaction.Exec(ctx, migration.sql, pgx.QueryExecModeSimpleProtocol); err != nil {
|
||||
return fmt.Errorf("apply migration %q: %w", migration.name, err)
|
||||
}
|
||||
if _, err := transaction.Exec(ctx, `
|
||||
INSERT INTO schema_migrations (version, name, checksum)
|
||||
VALUES ($1, $2, $3)`, migration.version, migration.name, migration.checksum); err != nil {
|
||||
return fmt.Errorf("record migration %q: %w", migration.name, err)
|
||||
}
|
||||
if err := transaction.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("commit migration %q: %w", migration.name, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type queryTracer struct{}
|
||||
|
||||
func (queryTracer) TraceQueryStart(ctx context.Context, _ *pgx.Conn, _ pgx.TraceQueryStartData) context.Context {
|
||||
ctx, _ = otel.Tracer("postgres").Start(ctx, "postgres query", trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(attribute.String("db.system", "postgresql")))
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (queryTracer) TraceQueryEnd(ctx context.Context, _ *pgx.Conn, data pgx.TraceQueryEndData) {
|
||||
span := trace.SpanFromContext(ctx)
|
||||
if data.Err != nil {
|
||||
span.RecordError(data.Err)
|
||||
span.SetStatus(codes.Error, "query failed")
|
||||
}
|
||||
span.End()
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
func New() *slog.Logger {
|
||||
return slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user