This commit is contained in:
Executable
+55
@@ -0,0 +1,55 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
project_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$project_root"
|
||||
|
||||
docker compose up -d postgres redis minio broker dependency-gateway
|
||||
|
||||
wait_for() {
|
||||
service=$1
|
||||
shift
|
||||
attempts=0
|
||||
until docker compose exec -T "$service" "$@" >/dev/null 2>&1; do
|
||||
attempts=$((attempts + 1))
|
||||
if [ "$attempts" -ge 60 ]; then
|
||||
printf '%s\n' "Timed out waiting for $service" >&2
|
||||
exit 1
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
}
|
||||
|
||||
wait_for postgres pg_isready -U lab7_admin -d messenger
|
||||
wait_for redis redis-cli -u redis://api:$(cat secrets/redis_api_password.txt)@127.0.0.1:6379 ping
|
||||
wait_for broker rabbitmq-diagnostics -q ping
|
||||
|
||||
docker compose run --rm migrate
|
||||
docker compose exec -T postgres sh -ec '
|
||||
export PGPASSWORD=$(cat /run/secrets/postgres_admin_password)
|
||||
exec psql -v ON_ERROR_STOP=1 -U lab7_admin -d messenger -f /run/secrets/postgres_roles
|
||||
'
|
||||
|
||||
docker compose run --rm minio-bootstrap
|
||||
|
||||
docker compose exec -T broker sh -ec '
|
||||
dispatcher_password=$(cat /run/secrets/broker_dispatcher_password)
|
||||
worker_password=$(cat /run/secrets/broker_worker_password)
|
||||
if rabbitmqctl list_users -q | cut -f1 | grep -qx dispatcher; then
|
||||
rabbitmqctl change_password dispatcher "$dispatcher_password" >/dev/null
|
||||
else
|
||||
rabbitmqctl add_user dispatcher "$dispatcher_password" >/dev/null
|
||||
fi
|
||||
if rabbitmqctl list_users -q | cut -f1 | grep -qx worker; then
|
||||
rabbitmqctl change_password worker "$worker_password" >/dev/null
|
||||
else
|
||||
rabbitmqctl add_user worker "$worker_password" >/dev/null
|
||||
fi
|
||||
rabbitmqctl set_permissions -p / dispatcher "^attachment[.]process[.]v1$" "^amq[.]default$" "^$" >/dev/null
|
||||
rabbitmqctl set_permissions -p / worker "^attachment[.]process[.]v1$" "^$" "^attachment[.]process[.]v1$" >/dev/null
|
||||
if rabbitmqctl list_users -q | cut -f1 | grep -qx guest; then
|
||||
rabbitmqctl delete_user guest >/dev/null
|
||||
fi
|
||||
'
|
||||
|
||||
printf '%s\n' "PostgreSQL roles, Redis ACL, MinIO policies and RabbitMQ identities are configured."
|
||||
Executable
+81
@@ -0,0 +1,81 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
project_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
operator_dir="$project_root/.operator"
|
||||
state_file="$operator_dir/openbao-init.json"
|
||||
cd "$project_root"
|
||||
|
||||
command -v python3 >/dev/null 2>&1 || {
|
||||
printf '%s\n' "Python 3 is required for OpenBao operator bootstrap" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
json_field() {
|
||||
python3 -c 'import json, sys
|
||||
value = json.load(sys.stdin)
|
||||
for part in sys.argv[1].split("."):
|
||||
value = value[int(part)] if isinstance(value, list) else value[part]
|
||||
print(str(value).lower() if isinstance(value, bool) else value)' "$1"
|
||||
}
|
||||
|
||||
umask 077
|
||||
mkdir -p "$operator_dir" "$project_root/secrets/openbao"
|
||||
chmod 700 "$operator_dir"
|
||||
docker compose up -d openbao
|
||||
|
||||
attempts=0
|
||||
until status_json=$(docker compose exec -T openbao bao status -format=json 2>/dev/null) || [ -n "${status_json:-}" ]; do
|
||||
attempts=$((attempts + 1))
|
||||
if [ "$attempts" -ge 60 ]; then
|
||||
printf '%s\n' "Timed out waiting for OpenBao" >&2
|
||||
exit 1
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
|
||||
initialized=$(printf '%s' "$status_json" | json_field initialized)
|
||||
if [ "$initialized" != true ]; then
|
||||
init_json=$(docker compose exec -T openbao bao operator init -format=json -key-shares=1 -key-threshold=1)
|
||||
printf '%s\n' "$init_json" > "$state_file"
|
||||
chmod 600 "$state_file"
|
||||
fi
|
||||
|
||||
if [ ! -s "$state_file" ]; then
|
||||
printf '%s\n' "OpenBao is initialized but .operator/openbao-init.json is unavailable; an operator must provide unseal material" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
unseal_key=$(json_field unseal_keys_b64.0 < "$state_file")
|
||||
root_token=$(json_field root_token < "$state_file")
|
||||
sealed=$(docker compose exec -T openbao bao status -format=json 2>/dev/null | json_field sealed)
|
||||
if [ "$sealed" = true ]; then
|
||||
docker compose exec -T openbao bao operator unseal "$unseal_key" >/dev/null
|
||||
fi
|
||||
|
||||
bao_root() {
|
||||
docker compose exec -T -e BAO_TOKEN="$root_token" openbao bao "$@"
|
||||
}
|
||||
|
||||
if ! bao_root secrets list -format=json | grep -q '"secret/"'; then
|
||||
bao_root secrets enable -path=secret kv-v2 >/dev/null
|
||||
fi
|
||||
if ! bao_root auth list -format=json | grep -q '"approle/"'; then
|
||||
bao_root auth enable approle >/dev/null
|
||||
fi
|
||||
|
||||
for role in api worker dispatcher; do
|
||||
bao_root policy write "$role" "/policies/$role-policy.hcl" >/dev/null
|
||||
bao_root kv put "secret/$role" "@/bootstrap/$role.json" >/dev/null
|
||||
bao_root write "auth/approle/role/$role" \
|
||||
"token_policies=$role" token_ttl=15m token_max_ttl=30m \
|
||||
secret_id_ttl=168h secret_id_num_uses=0 >/dev/null
|
||||
bao_root read -field=role_id "auth/approle/role/$role/role-id" \
|
||||
> "$project_root/secrets/openbao/${role}_role_id.txt"
|
||||
bao_root write -f -field=secret_id "auth/approle/role/$role/secret-id" \
|
||||
> "$project_root/secrets/openbao/${role}_secret_id.txt"
|
||||
chmod 600 "$project_root/secrets/openbao/${role}_role_id.txt" \
|
||||
"$project_root/secrets/openbao/${role}_secret_id.txt"
|
||||
done
|
||||
|
||||
printf '%s\n' "OpenBao is unsealed and scoped AppRoles are configured; root/unseal material remains under .operator only."
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env sh
|
||||
set -eu
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
# Keep named volumes (business data and telemetry history) intact. Only this
|
||||
# Compose project's containers/networks and the four images built by lab7 are
|
||||
# removed. Pulled third-party images and resources from other labs are never
|
||||
# selected by this script.
|
||||
docker compose down --remove-orphans
|
||||
|
||||
for image in \
|
||||
mtusi2026lab7-app:latest \
|
||||
mtusi2026lab7-ingress:latest \
|
||||
mtusi2026lab7-minio:RELEASE.2025-10-15T17-29-55Z \
|
||||
mtusi2026lab7-mc:RELEASE.2025-08-13T08-35-41Z
|
||||
do
|
||||
if docker image inspect "$image" >/dev/null 2>&1; then
|
||||
docker image rm "$image"
|
||||
fi
|
||||
done
|
||||
|
||||
echo "lab7 containers are stopped; local lab7 images removed; named volumes preserved"
|
||||
|
||||
Executable
+275
@@ -0,0 +1,275 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
project_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
secrets_dir="$project_root/secrets"
|
||||
pki_dir="$secrets_dir/pki"
|
||||
openbao_dir="$secrets_dir/openbao"
|
||||
openbao_data_dir="$project_root/volumes/openbao"
|
||||
|
||||
command -v openssl >/dev/null 2>&1 || {
|
||||
printf '%s\n' "openssl is required" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
umask 077
|
||||
mkdir -p "$secrets_dir" "$pki_dir" "$openbao_dir" "$openbao_data_dir"
|
||||
chmod 700 "$secrets_dir" "$pki_dir" "$openbao_dir" "$openbao_data_dir"
|
||||
|
||||
write_if_missing() {
|
||||
destination=$1
|
||||
value=$2
|
||||
if [ ! -e "$destination" ]; then
|
||||
printf '%s' "$value" > "$destination"
|
||||
fi
|
||||
chmod 600 "$destination"
|
||||
}
|
||||
|
||||
random_hex() {
|
||||
openssl rand -hex "$1"
|
||||
}
|
||||
|
||||
write_if_missing "$secrets_dir/postgres_admin_password.txt" "$(random_hex 24)"
|
||||
write_if_missing "$secrets_dir/postgres_api_password.txt" "$(random_hex 24)"
|
||||
write_if_missing "$secrets_dir/postgres_worker_password.txt" "$(random_hex 24)"
|
||||
write_if_missing "$secrets_dir/postgres_dispatcher_password.txt" "$(random_hex 24)"
|
||||
write_if_missing "$secrets_dir/cursor_signing_key.txt" "$(random_hex 32)"
|
||||
write_if_missing "$secrets_dir/minio_root_user.txt" "lab7-root"
|
||||
write_if_missing "$secrets_dir/minio_root_password.txt" "$(random_hex 24)"
|
||||
write_if_missing "$secrets_dir/s3_api_access_key.txt" "lab7-api"
|
||||
write_if_missing "$secrets_dir/s3_api_secret_key.txt" "$(random_hex 24)"
|
||||
write_if_missing "$secrets_dir/s3_worker_access_key.txt" "lab7-worker"
|
||||
write_if_missing "$secrets_dir/s3_worker_secret_key.txt" "$(random_hex 24)"
|
||||
write_if_missing "$secrets_dir/broker_dispatcher_password.txt" "$(random_hex 24)"
|
||||
write_if_missing "$secrets_dir/broker_worker_password.txt" "$(random_hex 24)"
|
||||
write_if_missing "$secrets_dir/redis_api_password.txt" "$(random_hex 24)"
|
||||
write_if_missing "$secrets_dir/grafana_admin_password.txt" "$(random_hex 24)"
|
||||
|
||||
if [ ! -e "$pki_dir/ca.key" ]; then
|
||||
openssl req -x509 -newkey rsa:3072 -sha256 -nodes -days 3650 \
|
||||
-subj '/CN=MTUSI lab7 local root CA' \
|
||||
-addext 'basicConstraints=critical,CA:TRUE,pathlen:0' \
|
||||
-addext 'keyUsage=critical,keyCertSign,cRLSign' \
|
||||
-addext 'subjectKeyIdentifier=hash' \
|
||||
-keyout "$pki_dir/ca.key" -out "$pki_dir/ca.crt"
|
||||
fi
|
||||
|
||||
generate_leaf() {
|
||||
name=$1
|
||||
common_name=$2
|
||||
subject_alt_name=$3
|
||||
extended_usage=$4
|
||||
if [ -e "$pki_dir/$name.crt" ] && [ -e "$pki_dir/$name.key" ]; then
|
||||
return
|
||||
fi
|
||||
ext_file="$pki_dir/$name.ext"
|
||||
csr_file="$pki_dir/$name.csr"
|
||||
{
|
||||
printf 'subjectAltName=%s\n' "$subject_alt_name"
|
||||
printf 'extendedKeyUsage=%s\n' "$extended_usage"
|
||||
printf 'keyUsage=digitalSignature,keyEncipherment\n'
|
||||
} > "$ext_file"
|
||||
openssl req -new -newkey rsa:2048 -sha256 -nodes \
|
||||
-subj "/CN=$common_name" \
|
||||
-keyout "$pki_dir/$name.key" -out "$csr_file"
|
||||
openssl x509 -req -sha256 -days 365 \
|
||||
-in "$csr_file" -CA "$pki_dir/ca.crt" -CAkey "$pki_dir/ca.key" \
|
||||
-CAcreateserial -extfile "$ext_file" -out "$pki_dir/$name.crt"
|
||||
rm -f "$csr_file" "$ext_file"
|
||||
}
|
||||
|
||||
generate_leaf ingress localhost 'DNS:localhost,IP:127.0.0.1' serverAuth
|
||||
generate_leaf api api 'DNS:api,URI:spiffe://mtusi/lab7/api' serverAuth
|
||||
generate_leaf lb-client load-balancer 'URI:spiffe://mtusi/lab7/lb' clientAuth
|
||||
generate_leaf wrong-client intruder 'URI:spiffe://mtusi/lab7/intruder' clientAuth
|
||||
generate_leaf api-client api 'URI:spiffe://mtusi/lab7/api' clientAuth
|
||||
generate_leaf worker-client worker 'URI:spiffe://mtusi/lab7/worker' clientAuth
|
||||
generate_leaf dispatcher-client dispatcher 'URI:spiffe://mtusi/lab7/dispatcher' clientAuth
|
||||
generate_leaf dependency-gateway dependency-gateway 'DNS:dependency-gateway' serverAuth
|
||||
generate_leaf telemetry-gateway telemetry-gateway 'DNS:telemetry-gateway,DNS:localhost,IP:127.0.0.1' serverAuth
|
||||
generate_leaf postgres postgres 'DNS:postgres' serverAuth
|
||||
generate_leaf openbao openbao 'DNS:openbao,DNS:localhost,IP:127.0.0.1' serverAuth
|
||||
generate_leaf collector-client otel-collector 'URI:spiffe://mtusi/lab7/collector' clientAuth
|
||||
generate_leaf prometheus-client prometheus 'URI:spiffe://mtusi/lab7/prometheus' clientAuth
|
||||
generate_leaf grafana-client grafana 'URI:spiffe://mtusi/lab7/grafana' clientAuth
|
||||
|
||||
if [ ! -e "$pki_dir/foreign-ca.key" ]; then
|
||||
openssl req -x509 -newkey rsa:2048 -sha256 -nodes -days 365 \
|
||||
-subj '/CN=Untrusted lab7 CA' \
|
||||
-addext 'basicConstraints=critical,CA:TRUE,pathlen:0' \
|
||||
-addext 'keyUsage=critical,keyCertSign,cRLSign' \
|
||||
-addext 'subjectKeyIdentifier=hash' \
|
||||
-keyout "$pki_dir/foreign-ca.key" -out "$pki_dir/foreign-ca.crt"
|
||||
openssl req -new -newkey rsa:2048 -sha256 -nodes \
|
||||
-subj '/CN=foreign-client' \
|
||||
-addext 'subjectAltName=URI:spiffe://mtusi/lab7/lb' \
|
||||
-keyout "$pki_dir/foreign-client.key" -out "$pki_dir/foreign-client.csr"
|
||||
printf '%s\n' 'subjectAltName=URI:spiffe://mtusi/lab7/lb' 'extendedKeyUsage=clientAuth' > "$pki_dir/foreign-client.ext"
|
||||
openssl x509 -req -sha256 -days 365 \
|
||||
-in "$pki_dir/foreign-client.csr" \
|
||||
-CA "$pki_dir/foreign-ca.crt" -CAkey "$pki_dir/foreign-ca.key" \
|
||||
-CAcreateserial -extfile "$pki_dir/foreign-client.ext" \
|
||||
-out "$pki_dir/foreign-client.crt"
|
||||
rm -f "$pki_dir/foreign-client.csr" "$pki_dir/foreign-client.ext"
|
||||
fi
|
||||
|
||||
for name in ingress lb-client wrong-client dependency-gateway telemetry-gateway foreign-client; do
|
||||
cat "$pki_dir/$name.crt" "$pki_dir/$name.key" > "$pki_dir/$name.pem"
|
||||
done
|
||||
chmod 600 "$pki_dir"/*.key "$pki_dir"/*.pem
|
||||
chmod 644 "$pki_dir"/*.crt
|
||||
# Collector and Prometheus images use their own non-root UIDs. The additional
|
||||
# in-container GID below can read only these client keys; host access remains
|
||||
# limited to the local operator group.
|
||||
chmod 640 "$pki_dir/collector-client.key" "$pki_dir/prometheus-client.key"
|
||||
|
||||
write_if_missing "$pki_dir/api-client-uris.txt" 'spiffe://mtusi/lab7/lb'
|
||||
|
||||
write_grafana_tls() {
|
||||
sed 's/^/ /' "$1"
|
||||
}
|
||||
|
||||
{
|
||||
cat <<'EOF'
|
||||
apiVersion: 1
|
||||
|
||||
datasources:
|
||||
- name: Prometheus
|
||||
uid: prometheus
|
||||
type: prometheus
|
||||
access: proxy
|
||||
url: https://telemetry-gateway:9090
|
||||
isDefault: true
|
||||
editable: false
|
||||
jsonData:
|
||||
tlsAuth: true
|
||||
secureJsonData:
|
||||
tlsClientCert: |
|
||||
EOF
|
||||
write_grafana_tls "$pki_dir/grafana-client.crt"
|
||||
printf '%s\n' ' tlsClientKey: |'
|
||||
write_grafana_tls "$pki_dir/grafana-client.key"
|
||||
cat <<'EOF'
|
||||
|
||||
- name: Loki
|
||||
uid: loki
|
||||
type: loki
|
||||
access: proxy
|
||||
url: https://telemetry-gateway:3100
|
||||
editable: false
|
||||
jsonData:
|
||||
tlsAuth: true
|
||||
derivedFields:
|
||||
- name: TraceID
|
||||
matcherRegex: '([0-9a-f]{32})'
|
||||
datasourceUid: tempo
|
||||
url: '$${__value.raw}'
|
||||
secureJsonData:
|
||||
tlsClientCert: |
|
||||
EOF
|
||||
write_grafana_tls "$pki_dir/grafana-client.crt"
|
||||
printf '%s\n' ' tlsClientKey: |'
|
||||
write_grafana_tls "$pki_dir/grafana-client.key"
|
||||
cat <<'EOF'
|
||||
|
||||
- name: Tempo
|
||||
uid: tempo
|
||||
type: tempo
|
||||
access: proxy
|
||||
url: https://telemetry-gateway:3200
|
||||
editable: false
|
||||
jsonData:
|
||||
tlsAuth: true
|
||||
tracesToLogsV2:
|
||||
datasourceUid: loki
|
||||
spanStartTimeShift: -1m
|
||||
spanEndTimeShift: 1m
|
||||
tags:
|
||||
- key: service.name
|
||||
value: service_name
|
||||
filterByTraceID: true
|
||||
filterBySpanID: true
|
||||
serviceMap:
|
||||
datasourceUid: prometheus
|
||||
nodeGraph:
|
||||
enabled: true
|
||||
secureJsonData:
|
||||
tlsClientCert: |
|
||||
EOF
|
||||
write_grafana_tls "$pki_dir/grafana-client.crt"
|
||||
printf '%s\n' ' tlsClientKey: |'
|
||||
write_grafana_tls "$pki_dir/grafana-client.key"
|
||||
} > "$secrets_dir/grafana-datasources.yaml"
|
||||
chmod 600 "$secrets_dir/grafana-datasources.yaml"
|
||||
|
||||
postgres_api_password=$(cat "$secrets_dir/postgres_api_password.txt")
|
||||
postgres_worker_password=$(cat "$secrets_dir/postgres_worker_password.txt")
|
||||
postgres_dispatcher_password=$(cat "$secrets_dir/postgres_dispatcher_password.txt")
|
||||
redis_api_password=$(cat "$secrets_dir/redis_api_password.txt")
|
||||
s3_api_access_key=$(cat "$secrets_dir/s3_api_access_key.txt")
|
||||
s3_api_secret_key=$(cat "$secrets_dir/s3_api_secret_key.txt")
|
||||
s3_worker_access_key=$(cat "$secrets_dir/s3_worker_access_key.txt")
|
||||
s3_worker_secret_key=$(cat "$secrets_dir/s3_worker_secret_key.txt")
|
||||
broker_dispatcher_password=$(cat "$secrets_dir/broker_dispatcher_password.txt")
|
||||
broker_worker_password=$(cat "$secrets_dir/broker_worker_password.txt")
|
||||
cursor_signing_key=$(cat "$secrets_dir/cursor_signing_key.txt")
|
||||
|
||||
cat > "$openbao_dir/api.json" <<EOF
|
||||
{"DATABASE_URL":"postgres://messenger_api:${postgres_api_password}@postgres:5432/messenger?sslmode=verify-full&sslrootcert=/run/secrets/tls_ca","CACHE_URL":"rediss://api:${redis_api_password}@dependency-gateway:6380/0","S3_ACCESS_KEY_ID":"${s3_api_access_key}","S3_SECRET_ACCESS_KEY":"${s3_api_secret_key}","CURSOR_SIGNING_KEY":"${cursor_signing_key}"}
|
||||
EOF
|
||||
cat > "$openbao_dir/worker.json" <<EOF
|
||||
{"DATABASE_URL":"postgres://messenger_worker:${postgres_worker_password}@postgres:5432/messenger?sslmode=verify-full&sslrootcert=/run/secrets/tls_ca","S3_ACCESS_KEY_ID":"${s3_worker_access_key}","S3_SECRET_ACCESS_KEY":"${s3_worker_secret_key}","BROKER_URL":"amqps://worker:${broker_worker_password}@dependency-gateway:5671/"}
|
||||
EOF
|
||||
cat > "$openbao_dir/dispatcher.json" <<EOF
|
||||
{"DATABASE_URL":"postgres://messenger_dispatcher:${postgres_dispatcher_password}@postgres:5432/messenger?sslmode=verify-full&sslrootcert=/run/secrets/tls_ca","BROKER_URL":"amqps://dispatcher:${broker_dispatcher_password}@dependency-gateway:5671/"}
|
||||
EOF
|
||||
|
||||
cat > "$secrets_dir/redis.conf" <<EOF
|
||||
bind 0.0.0.0
|
||||
protected-mode no
|
||||
save ""
|
||||
appendonly no
|
||||
maxmemory 64mb
|
||||
maxmemory-policy allkeys-lru
|
||||
user default off
|
||||
user api on >${redis_api_password} ~chat:v2:* +get +set +del +ping
|
||||
EOF
|
||||
|
||||
cat > "$secrets_dir/postgres-roles.sql" <<EOF
|
||||
DO \$\$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'messenger_api') THEN
|
||||
CREATE ROLE messenger_api LOGIN PASSWORD '${postgres_api_password}';
|
||||
ELSE
|
||||
ALTER ROLE messenger_api PASSWORD '${postgres_api_password}';
|
||||
END IF;
|
||||
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'messenger_worker') THEN
|
||||
CREATE ROLE messenger_worker LOGIN PASSWORD '${postgres_worker_password}';
|
||||
ELSE
|
||||
ALTER ROLE messenger_worker PASSWORD '${postgres_worker_password}';
|
||||
END IF;
|
||||
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'messenger_dispatcher') THEN
|
||||
CREATE ROLE messenger_dispatcher LOGIN PASSWORD '${postgres_dispatcher_password}';
|
||||
ELSE
|
||||
ALTER ROLE messenger_dispatcher PASSWORD '${postgres_dispatcher_password}';
|
||||
END IF;
|
||||
END
|
||||
\$\$;
|
||||
GRANT CONNECT ON DATABASE messenger TO messenger_api, messenger_worker, messenger_dispatcher;
|
||||
GRANT USAGE ON SCHEMA public TO messenger_api, messenger_worker, messenger_dispatcher;
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON users, chats, chat_members, messages, sessions,
|
||||
message_idempotency, attachments, message_attachments, attachment_jobs,
|
||||
attachment_job_events, login_rate_limits TO messenger_api;
|
||||
GRANT SELECT ON attachment_variants TO messenger_api;
|
||||
GRANT SELECT, UPDATE ON attachments, attachment_jobs TO messenger_worker;
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON attachment_variants TO messenger_worker;
|
||||
GRANT SELECT, INSERT ON attachment_job_events TO messenger_worker;
|
||||
GRANT SELECT, UPDATE ON attachment_jobs TO messenger_dispatcher;
|
||||
GRANT SELECT, UPDATE ON attachments TO messenger_dispatcher;
|
||||
GRANT SELECT, INSERT ON attachment_job_events TO messenger_dispatcher;
|
||||
GRANT USAGE, SELECT ON SEQUENCE attachment_job_events_event_id_seq
|
||||
TO messenger_api, messenger_worker, messenger_dispatcher;
|
||||
EOF
|
||||
|
||||
chmod 600 "$openbao_dir"/*.json "$secrets_dir/redis.conf" "$secrets_dir/postgres-roles.sql"
|
||||
printf '%s\n' "Local lab7 credentials and certificates are present; existing keys were not overwritten."
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
project_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$project_root"
|
||||
|
||||
# Stop request producers/consumers first. Named volumes are deliberately kept.
|
||||
docker compose stop ingress api worker dispatcher
|
||||
docker compose restart postgres redis minio broker openbao loki tempo otel-collector prometheus grafana dependency-gateway telemetry-gateway
|
||||
|
||||
# OpenBao seals after restart. Only the operator-side bootstrap script can
|
||||
# unseal it; service containers never receive the root token or unseal key.
|
||||
./scripts/bootstrap-dependencies.sh >/dev/null
|
||||
./scripts/bootstrap-openbao.sh >/dev/null
|
||||
|
||||
# Recreate services so refreshed AppRole SecretID files are mounted. Business
|
||||
# data, sessions, object data, broker state and telemetry volumes are untouched.
|
||||
docker compose up -d --build --force-recreate api worker dispatcher ingress telemetry-gateway
|
||||
|
||||
attempts=0
|
||||
until curl --silent --fail --cacert secrets/pki/ca.crt https://127.0.0.1:${HTTPS_PORT:-8443}/health/ready >/dev/null; do
|
||||
attempts=$((attempts + 1))
|
||||
if [ "$attempts" -ge 60 ]; then
|
||||
printf '%s\n' "HTTPS readiness did not recover after restart" >&2
|
||||
exit 1
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
Executable
+109
@@ -0,0 +1,109 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
project_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
pki_dir="$project_root/secrets/pki"
|
||||
cd "$project_root"
|
||||
|
||||
temp_dir=$(mktemp -d "${TMPDIR:-/tmp}/mtusi-lab7-leaf.XXXXXX")
|
||||
case "$temp_dir" in
|
||||
"${TMPDIR:-/tmp}"/mtusi-lab7-leaf.*) ;;
|
||||
*) printf '%s\n' "Unexpected temporary directory" >&2; exit 1 ;;
|
||||
esac
|
||||
|
||||
old_uri=$(sed -n '1p' "$pki_dir/api-client-uris.txt")
|
||||
cp "$pki_dir/lb-client.crt" "$temp_dir/old.crt"
|
||||
cp "$pki_dir/lb-client.key" "$temp_dir/old.key"
|
||||
cp "$pki_dir/lb-client.pem" "$temp_dir/old.pem"
|
||||
app_image_before=$(docker image inspect -f '{{.Id}}' mtusi2026lab7-app:latest)
|
||||
ingress_image_before=$(docker image inspect -f '{{.Id}}' mtusi2026lab7-ingress:latest)
|
||||
new_uri="spiffe://mtusi/lab7/lb-$(openssl rand -hex 8)"
|
||||
completed=false
|
||||
|
||||
recreate_edge() {
|
||||
docker compose up -d --no-deps --no-build --force-recreate api ingress >/dev/null 2>&1
|
||||
}
|
||||
|
||||
restore_old_leaf() {
|
||||
install -m 600 "$temp_dir/old.key" "$pki_dir/lb-client.key"
|
||||
install -m 644 "$temp_dir/old.crt" "$pki_dir/lb-client.crt"
|
||||
install -m 600 "$temp_dir/old.pem" "$pki_dir/lb-client.pem"
|
||||
printf '%s' "$old_uri" > "$pki_dir/api-client-uris.txt"
|
||||
chmod 600 "$pki_dir/api-client-uris.txt"
|
||||
recreate_edge || true
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
if [ "$completed" != true ]; then
|
||||
restore_old_leaf
|
||||
fi
|
||||
rm -f "$temp_dir/old.crt" "$temp_dir/old.key" "$temp_dir/old.pem" \
|
||||
"$temp_dir/new.crt" "$temp_dir/new.key" "$temp_dir/new.csr" "$temp_dir/new.ext" \
|
||||
"$temp_dir/expired.crt" "$temp_dir/expired.key" "$temp_dir/expired.csr" "$temp_dir/expired.ext"
|
||||
rmdir "$temp_dir" 2>/dev/null || true
|
||||
}
|
||||
trap cleanup EXIT HUP INT TERM
|
||||
|
||||
cat > "$temp_dir/new.ext" <<EOF
|
||||
subjectAltName=URI:$new_uri
|
||||
extendedKeyUsage=clientAuth
|
||||
keyUsage=digitalSignature,keyEncipherment
|
||||
EOF
|
||||
openssl req -new -newkey rsa:2048 -sha256 -nodes -subj '/CN=load-balancer' \
|
||||
-keyout "$temp_dir/new.key" -out "$temp_dir/new.csr" >/dev/null 2>&1
|
||||
openssl x509 -req -sha256 -days 365 -in "$temp_dir/new.csr" \
|
||||
-CA "$pki_dir/ca.crt" -CAkey "$pki_dir/ca.key" -CAserial "$pki_dir/ca.srl" \
|
||||
-extfile "$temp_dir/new.ext" -out "$temp_dir/new.crt" >/dev/null 2>&1
|
||||
install -m 600 "$temp_dir/new.key" "$pki_dir/lb-client.key"
|
||||
install -m 644 "$temp_dir/new.crt" "$pki_dir/lb-client.crt"
|
||||
cat "$temp_dir/new.crt" "$temp_dir/new.key" > "$pki_dir/lb-client.pem"
|
||||
chmod 600 "$pki_dir/lb-client.pem"
|
||||
printf '%s' "$new_uri" > "$pki_dir/api-client-uris.txt"
|
||||
chmod 600 "$pki_dir/api-client-uris.txt"
|
||||
|
||||
recreate_edge
|
||||
attempts=0
|
||||
until curl --silent --show-error --fail --cacert "$pki_dir/ca.crt" https://127.0.0.1:8443/health/live >/dev/null 2>&1; do
|
||||
attempts=$((attempts + 1))
|
||||
[ "$attempts" -lt 40 ] || { printf '%s\n' "Rotated edge did not become ready" >&2; exit 1; }
|
||||
sleep 1
|
||||
done
|
||||
|
||||
if docker compose --profile tests run --rm --no-deps -v "$temp_dir:/rotation-old:ro" \
|
||||
--entrypoint curl security-test --silent --show-error --fail --cacert /run/secrets/ca.crt \
|
||||
--cert /rotation-old/old.crt --key /rotation-old/old.key https://api:8444/health/live >/dev/null 2>&1; then
|
||||
printf '%s\n' "Old leaf identity is still accepted" >&2
|
||||
exit 1
|
||||
fi
|
||||
docker compose --profile tests run --rm --no-deps --entrypoint curl security-test \
|
||||
--silent --show-error --fail --cacert /run/secrets/ca.crt \
|
||||
--cert /run/secrets/lb-client.crt --key /run/secrets/lb-client.key \
|
||||
https://api:8444/health/live >/dev/null
|
||||
|
||||
cat > "$temp_dir/expired.ext" <<EOF
|
||||
subjectAltName=URI:$new_uri
|
||||
extendedKeyUsage=clientAuth
|
||||
keyUsage=digitalSignature,keyEncipherment
|
||||
EOF
|
||||
openssl req -new -newkey rsa:2048 -sha256 -nodes -subj '/CN=load-balancer-expired' \
|
||||
-keyout "$temp_dir/expired.key" -out "$temp_dir/expired.csr" >/dev/null 2>&1
|
||||
openssl x509 -req -sha256 -days 0 -in "$temp_dir/expired.csr" \
|
||||
-CA "$pki_dir/ca.crt" -CAkey "$pki_dir/ca.key" -CAserial "$pki_dir/ca.srl" \
|
||||
-extfile "$temp_dir/expired.ext" -out "$temp_dir/expired.crt" >/dev/null 2>&1
|
||||
sleep 2
|
||||
if docker compose --profile tests run --rm --no-deps -v "$temp_dir:/expired:ro" \
|
||||
--entrypoint curl security-test --silent --show-error --fail --cacert /run/secrets/ca.crt \
|
||||
--cert /expired/expired.crt --key /expired/expired.key https://api:8444/health/live >/dev/null 2>&1; then
|
||||
printf '%s\n' "Expired leaf certificate is accepted" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
app_image_after=$(docker image inspect -f '{{.Id}}' mtusi2026lab7-app:latest)
|
||||
ingress_image_after=$(docker image inspect -f '{{.Id}}' mtusi2026lab7-ingress:latest)
|
||||
[ "$app_image_before" = "$app_image_after" ] && [ "$ingress_image_before" = "$ingress_image_after" ] || {
|
||||
printf '%s\n' "Image changed during leaf rotation" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
completed=true
|
||||
printf '%s\n' "PASS: LB leaf rotated without rebuild; old identity and expired leaf rejected"
|
||||
Executable
+61
@@ -0,0 +1,61 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
project_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$project_root"
|
||||
|
||||
state_file="$project_root/.operator/openbao-init.json"
|
||||
password_file="$project_root/secrets/broker_worker_password.txt"
|
||||
[ -s "$state_file" ] || { printf '%s\n' "OpenBao operator state is required" >&2; exit 1; }
|
||||
[ -s "$password_file" ] || { printf '%s\n' "Broker worker password is required" >&2; exit 1; }
|
||||
|
||||
old_password=$(cat "$password_file")
|
||||
new_password=$(openssl rand -hex 24)
|
||||
root_token=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1], encoding="utf-8"))["root_token"])' "$state_file")
|
||||
app_image_before=$(docker image inspect -f '{{.Id}}' mtusi2026lab7-app:latest)
|
||||
completed=false
|
||||
|
||||
restore_old_secret() {
|
||||
printf '%s' "$old_password" > "$password_file"
|
||||
chmod 600 "$password_file"
|
||||
./scripts/init-secrets.sh >/dev/null
|
||||
docker compose exec -T -e BAO_TOKEN="$root_token" openbao \
|
||||
bao kv put secret/worker @/bootstrap/worker.json >/dev/null 2>&1 || true
|
||||
docker compose exec -T broker rabbitmqctl change_password worker "$old_password" >/dev/null 2>&1 || true
|
||||
docker compose up -d --no-deps --no-build --force-recreate worker >/dev/null 2>&1 || true
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
if [ "$completed" != true ]; then
|
||||
restore_old_secret
|
||||
fi
|
||||
}
|
||||
trap cleanup EXIT HUP INT TERM
|
||||
|
||||
printf '%s' "$new_password" > "$password_file"
|
||||
chmod 600 "$password_file"
|
||||
./scripts/init-secrets.sh >/dev/null
|
||||
docker compose exec -T -e BAO_TOKEN="$root_token" openbao \
|
||||
bao kv put secret/worker @/bootstrap/worker.json >/dev/null
|
||||
docker compose exec -T broker rabbitmqctl change_password worker "$new_password" >/dev/null
|
||||
|
||||
if docker compose exec -T broker rabbitmqctl authenticate_user worker "$old_password" >/dev/null 2>&1; then
|
||||
printf '%s\n' "Old broker secret is still accepted" >&2
|
||||
exit 1
|
||||
fi
|
||||
docker compose exec -T broker rabbitmqctl authenticate_user worker "$new_password" >/dev/null
|
||||
|
||||
docker compose up -d --no-deps --no-build --force-recreate worker >/dev/null
|
||||
sleep 4
|
||||
[ -n "$(docker compose ps --status running -q worker)" ] || {
|
||||
printf '%s\n' "Worker did not restart with the rotated secret" >&2
|
||||
exit 1
|
||||
}
|
||||
app_image_after=$(docker image inspect -f '{{.Id}}' mtusi2026lab7-app:latest)
|
||||
[ "$app_image_before" = "$app_image_after" ] || {
|
||||
printf '%s\n' "Application image changed during secret rotation" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
completed=true
|
||||
printf '%s\n' "PASS: worker broker secret rotated, old credential revoked, image unchanged"
|
||||
Executable
+44
@@ -0,0 +1,44 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
project_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$project_root"
|
||||
|
||||
trivy_version=0.76.2
|
||||
trivy_image="ghcr.io/aquasecurity/trivy:${trivy_version}@sha256:8f6bb947dbc528902e11f2f5be52f7b519f6a39d5d148f41166f2040499768fb"
|
||||
trivy_cache="$project_root/.cache/trivy"
|
||||
mkdir -p "$trivy_cache"
|
||||
|
||||
docker pull "$trivy_image" >/dev/null
|
||||
|
||||
printf '%s\n' "== Go reachable-vulnerability scan =="
|
||||
go run golang.org/x/vuln/cmd/govulncheck@v1.10.0 ./...
|
||||
|
||||
for image in \
|
||||
mtusi2026lab7-app:latest \
|
||||
mtusi2026lab7-ingress:latest; do
|
||||
printf '%s\n' "== Strict Trivy gate $trivy_version: $image =="
|
||||
docker run --rm --read-only --security-opt no-new-privileges:true --cap-drop ALL \
|
||||
--tmpfs /tmp:rw,nosuid,nodev,noexec \
|
||||
-v /var/run/docker.sock:/var/run/docker.sock:ro \
|
||||
-v "$trivy_cache:/root/.cache/trivy" \
|
||||
"$trivy_image" image --scanners vuln --severity HIGH,CRITICAL \
|
||||
--ignore-unfixed --no-progress --exit-code 1 "$image"
|
||||
done
|
||||
|
||||
printf '%s\n' \
|
||||
"== Informational vendor scan: findings are triaged in security/SCAN_TRIAGE.md =="
|
||||
for image in \
|
||||
mtusi2026lab7-minio:RELEASE.2025-10-15T17-29-55Z \
|
||||
mtusi2026lab7-mc:RELEASE.2025-08-13T08-35-41Z; do
|
||||
printf '%s\n' "== Informational Trivy scan $trivy_version: $image =="
|
||||
docker run --rm --read-only --security-opt no-new-privileges:true --cap-drop ALL \
|
||||
--tmpfs /tmp:rw,nosuid,nodev,noexec \
|
||||
-v /var/run/docker.sock:/var/run/docker.sock:ro \
|
||||
-v "$trivy_cache:/root/.cache/trivy" \
|
||||
"$trivy_image" image --scanners vuln --severity HIGH,CRITICAL \
|
||||
--ignore-unfixed --no-progress --exit-code 0 "$image"
|
||||
done
|
||||
|
||||
printf '%s\n' \
|
||||
"PASS: authored images passed the strict gate; vendor residual risk is documented"
|
||||
Executable
+41
@@ -0,0 +1,41 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
|
||||
project_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$project_root"
|
||||
|
||||
env_file=${ENV_FILE:-.env}
|
||||
matched_container=
|
||||
|
||||
compose() {
|
||||
if [ -f "$env_file" ]; then
|
||||
docker compose --env-file "$env_file" "$@"
|
||||
else
|
||||
docker compose "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
for container_id in $(compose ps -a -q api); do
|
||||
instance_id=$(docker exec "$container_id" cat /tmp/instance-id 2>/dev/null || true)
|
||||
if [ "$instance_id" = "$TARGET_INSTANCE_ID" ]; then
|
||||
if [ -n "$matched_container" ]; then
|
||||
printf '%s\n' "multiple API containers match instance $TARGET_INSTANCE_ID" >&2
|
||||
exit 1
|
||||
fi
|
||||
matched_container=$container_id
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -z "$matched_container" ]; then
|
||||
printf '%s\n' "API instance not found: $TARGET_INSTANCE_ID" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# A manual docker stop suppresses the container restart policy until the next
|
||||
# explicit start, so the selected replica remains down for the whole test.
|
||||
docker stop --time 5 "$matched_container" >/dev/null
|
||||
|
||||
printf '%s\n' "stopped API instance $TARGET_INSTANCE_ID"
|
||||
|
||||
Reference in New Issue
Block a user