diff --git a/REPORT.md b/REPORT.md index f2f9b22..bf54eb1 100644 --- a/REPORT.md +++ b/REPORT.md @@ -34,4 +34,4 @@ OpenBao запускается server mode с file-backed persistent volume, б ## Ограничения -OpenBao init/unseal и выдача AppRole остаются операторской процедурой, потому что root token и unseal keys нельзя автоматизировать в репозитории. Для production нужно включить TLS listener OpenBao, заменить локальный CA на управляемый PKI и подключить API/worker к AppRole через short-lived wrapped credentials. Эти границы явно описаны, а не скрыты за зелёным smoke-тестом. +OpenBao init/unseal выполняются отдельным одноразовым `openbao-init` контейнером: root token и unseal keys сохраняются только в локальном ignored storage, API и worker получают разные AppRole credentials с TTL 5 минут и читают свои scoped secrets через `src/vault.py`. Для production нужно включить TLS listener OpenBao и заменить локальный CA на управляемый PKI; plaintext listener оставлен только внутри изолированной учебной Docker-сети. diff --git a/compose.yaml b/compose.yaml index cfad23e..0db02a2 100644 --- a/compose.yaml +++ b/compose.yaml @@ -10,15 +10,22 @@ x-api: &api S3_ENDPOINT: http://minio:9000 S3_ACCESS_KEY_FILE: /run/secrets/s3_access_key S3_SECRET_KEY_FILE: /run/secrets/s3_secret_key + BAO_ADDR: http://openbao:8200 + BAO_ROLE_ID_FILE: /run/bao/role_id + BAO_SECRET_ID_FILE: /run/bao/secret_id volumes: - ./secrets/database_url:/run/secrets/database_url:ro - ./secrets/s3_access_key:/run/secrets/s3_access_key:ro - ./secrets/s3_secret_key:/run/secrets/s3_secret_key:ro + - ./secrets/bao_api_role_id:/run/bao/role_id:ro + - ./secrets/bao_api_secret_id:/run/bao/secret_id:ro depends_on: migrate: condition: service_completed_successfully s3init: condition: service_completed_successfully + openbao-init: + condition: service_completed_successfully healthcheck: test: [CMD-SHELL, "python -c \"import os,ssl,urllib.request; c=ssl.create_default_context(cafile='/run/certs/ca.crt'); c.check_hostname=False; c.load_cert_chain('/run/certs/'+os.environ['TLS_CERT'],'/run/certs/'+os.environ['TLS_KEY']); urllib.request.urlopen('https://127.0.0.1:8080/health/ready',context=c,timeout=4)\""] interval: 3s @@ -40,6 +47,8 @@ services: - ./secrets/database_url:/run/secrets/database_url:ro - ./secrets/s3_access_key:/run/secrets/s3_access_key:ro - ./secrets/s3_secret_key:/run/secrets/s3_secret_key:ro + - ./secrets/bao_api_role_id:/run/bao/role_id:ro + - ./secrets/bao_api_secret_id:/run/bao/secret_id:ro api2: <<: *api environment: @@ -52,6 +61,8 @@ services: - ./secrets/database_url:/run/secrets/database_url:ro - ./secrets/s3_access_key:/run/secrets/s3_access_key:ro - ./secrets/s3_secret_key:/run/secrets/s3_secret_key:ro + - ./secrets/bao_api_role_id:/run/bao/role_id:ro + - ./secrets/bao_api_secret_id:/run/bao/secret_id:ro migrate: build: . image: vp-lab4-api @@ -65,6 +76,21 @@ services: condition: service_healthy mem_limit: 128m cpus: 0.5 + openbao-init: + build: . + image: vp-lab4-api + command: [python, scripts/openbao_init.py] + volumes: + - ./secrets:/run/bao-bootstrap + - ./secrets/database_url:/run/local-secrets/database_url:ro + - ./secrets/s3_access_key:/run/local-secrets/s3_access_key:ro + - ./secrets/s3_secret_key:/run/local-secrets/s3_secret_key:ro + depends_on: + openbao: + condition: service_healthy + restart: "no" + mem_limit: 128m + cpus: 0.25 lb: image: haproxy:3.0.6-alpine ports: ["127.0.0.1:8443:8443", "127.0.0.1:9000:9000"] @@ -115,6 +141,17 @@ services: worker: <<: *api command: [python, -m, src.worker] + environment: + <<: *app-env + BAO_SECRET_PATH: messenger/worker + BAO_ROLE_ID_FILE: /run/bao/worker_role_id + BAO_SECRET_ID_FILE: /run/bao/worker_secret_id + volumes: + - ./secrets/database_url:/run/secrets/database_url:ro + - ./secrets/s3_access_key:/run/secrets/s3_access_key:ro + - ./secrets/s3_secret_key:/run/secrets/s3_secret_key:ro + - ./secrets/bao_worker_role_id:/run/bao/worker_role_id:ro + - ./secrets/bao_worker_secret_id:/run/bao/worker_secret_id:ro healthcheck: test: [CMD, python, -c, "import os; os.kill(os.getpid(), 0)"] depends_on: @@ -141,7 +178,9 @@ services: cpus: 0.5 openbao: image: openbao/openbao:2.2.0 - command: [bao, server, -config=/bao/config/config.hcl] + user: "0:0" + entrypoint: [/bin/sh, -c] + command: ["exec bao server -config=/bao/config/config.hcl"] security_opt: [no-new-privileges:true] volumes: - bao-data:/bao/data diff --git a/scripts/bootstrap.py b/scripts/bootstrap.py index f2ebeec..2b95c9d 100644 --- a/scripts/bootstrap.py +++ b/scripts/bootstrap.py @@ -30,6 +30,8 @@ for name in ('s3_root_user','s3_root_password','s3_access_key','s3_secret_key'): if not path.exists(): with path.open('x', encoding='utf-8') as file: file.write(secrets.token_hex(16)) +for name in ('bao_api_role_id', 'bao_api_secret_id', 'bao_worker_role_id', 'bao_worker_secret_id'): + (directory / name).touch(exist_ok=True) certs = root / 'certs' certs.mkdir(exist_ok=True) ca = certs / 'ca.crt' diff --git a/scripts/openbao-bootstrap.md b/scripts/openbao-bootstrap.md index 38501ee..e0b39f5 100644 --- a/scripts/openbao-bootstrap.md +++ b/scripts/openbao-bootstrap.md @@ -1,6 +1,6 @@ # OpenBao operator bootstrap -The service runs in server mode with file-backed persistent storage, never in dev mode. The operator initializes and unseals it interactively; root token and unseal keys must not be written to the repository: +The service runs in server mode with file-backed persistent storage, never in dev mode. `openbao-init` performs the reproducible local initialization and AppRole issuance; root token and unseal keys are written only to ignored local storage and must not be committed: ```sh docker compose exec openbao bao operator init -key-shares=3 -key-threshold=2 @@ -11,4 +11,4 @@ docker compose exec openbao bao policy write messenger-api /bao/policies/api.hcl docker compose exec openbao bao policy write messenger-worker /bao/policies/worker.hcl ``` -The application is deliberately not given a root token. Production deployment must add AppRole or workload identity, short TTLs, audit logging, and TLS on the OpenBao listener. +The application is never given a root token. API and worker use separate AppRole role/secret-id files, exchange them for short-lived tokens, and can read only their own KV path. Production deployment should add wrapped secret IDs, audit logging, and TLS on the OpenBao listener. diff --git a/scripts/openbao_init.py b/scripts/openbao_init.py new file mode 100644 index 0000000..082fec6 --- /dev/null +++ b/scripts/openbao_init.py @@ -0,0 +1,51 @@ +"""Idempotent local operator bootstrap for OpenBao + AppRole. + +Root/unseal material is generated only into the ignored secrets/ directory. +""" +import json, os, time, urllib.request +from pathlib import Path + +root = Path('/run/bao-bootstrap') +root.mkdir(exist_ok=True) +addr = 'http://openbao:8200' +def call(path, method='GET', payload=None, token=None): + req = urllib.request.Request(addr + path, method=method, + data=None if payload is None else json.dumps(payload).encode(), + headers={'Content-Type':'application/json', **({'X-Vault-Token':token} if token else {})}) + with urllib.request.urlopen(req, timeout=5) as response: return json.loads(response.read() or b'{}') +for _ in range(60): + try: status=call('/v1/sys/seal-status'); break + except Exception: time.sleep(1) +else: raise SystemExit('OpenBao did not become reachable') +if not status.get('initialized'): + result=call('/v1/sys/init','PUT',{'secret_shares':3,'secret_threshold':2}) + (root/'bao_root_token').write_text(result['root_token']) + (root/'bao_unseal_keys').write_text('\n'.join(result['keys'])) + status=call('/v1/sys/seal-status') +else: + if not (root/'bao_root_token').exists() or not (root/'bao_unseal_keys').exists(): raise SystemExit('existing OpenBao needs operator recovery material') +if status.get('sealed'): + for key in (root/'bao_unseal_keys').read_text().splitlines()[:2]: call('/v1/sys/unseal','PUT',{'key':key}) +token=(root/'bao_root_token').read_text().strip() +try: call('/v1/sys/mounts/secret','POST',{'type':'kv','options':{'version':'2'}},token) +except Exception: pass +policy='path "secret/data/messenger/api" { capabilities = ["read"] }' +call('/v1/sys/policies/acl/messenger-api','PUT',{'policy':policy},token) +try: call('/v1/sys/auth/approle','PUT',{'type':'approle','description':'short-lived service identities'},token) +except Exception: pass +try: call('/v1/auth/approle/role/messenger-api','POST',{'token_policies':['messenger-api'],'token_ttl':'5m','token_max_ttl':'15m'},token) +except Exception: pass +secrets={name:Path('/run/local-secrets/'+name).read_text().strip() for name in ('database_url','s3_access_key','s3_secret_key')} +call('/v1/secret/data/messenger/api','POST',{'data':{'DATABASE_URL':secrets['database_url'],'S3_ACCESS_KEY':secrets['s3_access_key'],'S3_SECRET_KEY':secrets['s3_secret_key']}},token) +role=call('/v1/auth/approle/role/messenger-api/role-id',token=token)['data']['role_id'] +sid=call('/v1/auth/approle/role/messenger-api/secret-id','POST',{},token)['data']['secret_id'] +(root/'bao_api_role_id').write_text(role); (root/'bao_api_secret_id').write_text(sid) +worker_policy='path "secret/data/messenger/worker" { capabilities = ["read"] }' +call('/v1/sys/policies/acl/messenger-worker','PUT',{'policy':worker_policy},token) +try: call('/v1/auth/approle/role/messenger-worker','POST',{'token_policies':['messenger-worker'],'token_ttl':'5m','token_max_ttl':'15m'},token) +except Exception: pass +call('/v1/secret/data/messenger/worker','POST',{'data':{'DATABASE_URL':secrets['database_url'],'S3_ACCESS_KEY':secrets['s3_access_key'],'S3_SECRET_KEY':secrets['s3_secret_key']}},token) +wrole=call('/v1/auth/approle/role/messenger-worker/role-id',token=token)['data']['role_id'] +wsid=call('/v1/auth/approle/role/messenger-worker/secret-id','POST',{},token)['data']['secret_id'] +(root/'bao_worker_role_id').write_text(wrole); (root/'bao_worker_secret_id').write_text(wsid) +print('OpenBao initialized/unsealed; AppRole credentials issued with a bounded TTL.') diff --git a/src/db.py b/src/db.py index 19cf69c..9f332ff 100644 --- a/src/db.py +++ b/src/db.py @@ -7,9 +7,12 @@ from pathlib import Path import psycopg from psycopg.rows import dict_row from psycopg_pool import ConnectionPool +from src.vault import secret def setting(name): + if name in {'DATABASE_URL', 'S3_ACCESS_KEY', 'S3_SECRET_KEY'} and os.getenv('BAO_ROLE_ID_FILE'): + return secret(name) path = os.getenv(name + '_FILE') return Path(path).read_text().strip() if path else os.environ[name] diff --git a/src/vault.py b/src/vault.py new file mode 100644 index 0000000..4360273 --- /dev/null +++ b/src/vault.py @@ -0,0 +1,29 @@ +"""Small AppRole client used at process startup; no root token is accepted here.""" +import json, os, urllib.request +from pathlib import Path + +_cache = {} + +def _read(name): + path = os.getenv(name) + return Path(path).read_text().strip() if path else None + +def _call(path, method='GET', payload=None, token=None): + body = None if payload is None else json.dumps(payload).encode() + req = urllib.request.Request(os.environ.get('BAO_ADDR', 'http://openbao:8200') + path, + data=body, method=method, + headers={'Content-Type':'application/json', **({'X-Vault-Token': token} if token else {})}) + with urllib.request.urlopen(req, timeout=3) as response: + return json.loads(response.read() or b'{}') + +def secret(name): + if name in _cache: return _cache[name] + role_id, secret_id = _read('BAO_ROLE_ID_FILE'), _read('BAO_SECRET_ID_FILE') + if not role_id or not secret_id: + raise RuntimeError('AppRole credentials are required; refusing file-secret fallback') + login = _call('/v1/auth/approle/login', 'POST', {'role_id': role_id, 'secret_id': secret_id}) + token = login['auth']['client_token'] + data = _call('/v1/secret/data/' + os.getenv('BAO_SECRET_PATH', 'messenger/api'), token=token).get('data', {}).get('data', {}) + if name not in data: raise RuntimeError('secret is not present in OpenBao: ' + name) + _cache[name] = data[name] + return data[name]