Реализовать лабораторную 7: mTLS, OpenBao и защита worker
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
"""Host operator API: root/unseal material never enters app containers."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import ssl
|
||||
import urllib.request
|
||||
ROOT=Path(__file__).resolve().parents[1];PRIVATE=ROOT/'.secrets'
|
||||
|
||||
def call(path,data=None,*,method=None,token=True):
|
||||
context=ssl.create_default_context(cafile=str(PRIVATE/'ca.crt'))
|
||||
headers={'Content-Type':'application/json'}
|
||||
if token:headers['X-Vault-Token']=json.loads((PRIVATE/'bao-operator.json').read_text())['root_token']
|
||||
req=urllib.request.Request('https://localhost:8207/v1/'+path,data=json.dumps(data).encode() if data is not None else None,headers=headers,method=method)
|
||||
with urllib.request.urlopen(req,context=context,timeout=20) as r:
|
||||
raw=r.read();return json.loads(raw) if raw else {}
|
||||
|
||||
def provision():
|
||||
try:call('sys/audit/local',{'type':'file','options':{'file_path':'/bao/data/audit.json'}})
|
||||
except Exception:
|
||||
if 'local/' not in call('sys/audit'):raise
|
||||
auth=call('sys/auth')
|
||||
if 'cert/' not in auth:call('sys/auth/cert',{'type':'cert'})
|
||||
if 'secret/' not in call('sys/mounts'):call('sys/mounts/secret',{'type':'kv','options':{'version':'2'}})
|
||||
credentials=json.loads((PRIVATE/'credentials.json').read_text())
|
||||
for role in ['api','worker','publisher','observer','janitor']:
|
||||
policy=f'path "secret/data/{role}" {{ capabilities=["read"] }}'
|
||||
call('sys/policies/acl/'+role,{'policy':policy})
|
||||
call('auth/cert/certs/'+role,{'certificate':(PRIVATE/'ca.crt').read_text(),'allowed_common_names':[role],
|
||||
'token_policies':[role],'token_ttl':30,'token_max_ttl':60,'token_no_default_policy':False})
|
||||
c=credentials[role]
|
||||
values={'DATABASE_URL':f'postgresql://messenger_{role}:{c["db_password"]}@127.0.0.1:15432/messenger?sslmode=disable',
|
||||
'BROKER_URL':f'redis://{role}:{c["broker_password"]}@127.0.0.1:16379/0',
|
||||
'CACHE_URL':f'redis://{role}:{c["cache_password"]}@127.0.0.1:16380/0'}
|
||||
if role in ('api','worker','janitor'):values.update(S3_ACCESS_KEY_ID='lab7-'+role,S3_SECRET_ACCESS_KEY=c['s3_secret'])
|
||||
call('secret/data/'+role,{'data':values})
|
||||
call('auth/cert/crls/operator-ca',{'crl':(PRIVATE/'public/revoked.crl').read_text()})
|
||||
|
||||
if __name__=='__main__':provision();print('Scoped roles, short tokens, audit and KV provisioned')
|
||||
Reference in New Issue
Block a user