Files
mtusi2026lab7/infra/openbao/operator.sh
T
wanderinho 7ee5192cbd
Skeleton contracts / contracts (pull_request) Blocked by required conditions
ЛР7 - БПИ2303 - Стругов - на 4
2026-12-06 23:11:49 +03:00

65 lines
1.8 KiB
Bash

# Sourced by the operator scripts from the project root.
bao_request() {
curl --silent --show-error --noproxy '*' --max-time 10 \
--cacert secrets/pki/ca.crt --resolve bao:8200:127.0.0.1 "$@"
}
bao_admin() {
printf 'header = "X-Vault-Token: %s"\n' "$operator_token" |
bao_request --config - --fail -o /dev/null \
-H 'Content-Type: application/json' "$@"
}
load_operator_token() {
operator_token=$(python3 - "${BAO_INIT_FILE:-secrets/operator/bao-init.json}" <<'PY'
import json
import re
import sys
with open(sys.argv[1]) as file:
token = json.load(file)['root_token']
if not re.fullmatch(r'[A-Za-z0-9._-]+', token):
sys.exit('Invalid operator token')
print(token)
PY
)
}
write_cert_role() {
identity=$1
python3 - "$identity" "$2" <<'PY'
import json
from pathlib import Path
import sys
identity, certificate = sys.argv[1:]
print(json.dumps(dict(certificate=Path(certificate).read_text(),
allowed_dns_sans=[identity + ".messenger.local"], token_policies=[identity],
token_no_default_policy=True, token_type='batch',
token_ttl=15, token_max_ttl=15)))
PY
}
write_kv_payload() {
python3 - "$1" <<'PY'
import json
from pathlib import Path
import sys
def value(name):
return Path('secrets', f'{name}.txt').read_text().strip()
identity = sys.argv[1]
data = {'DATABASE_URL': value('database_url')}
if identity == 'api':
data['CACHE_URL'] = value('cache_url')
if identity in ('api', 'worker'):
data['S3_ACCESS_KEY'] = value('minio_api_user')
data['S3_SECRET_KEY'] = value('minio_api_password')
print(json.dumps({'data': data}))
PY
}
publish_kv() {
write_kv_payload "$1" > "$payload"
bao_admin -X POST --data-binary "@$payload" "https://bao:8200/v1/secret/data/$1"
}