feat: реализовать ЛР7 — TLS, mTLS, OpenBao и hardening
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Skeleton contracts / contracts (pull_request) Blocked by required conditions
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
"""Operator-only OpenBao policies, KV bundles, AppRole issue. Avoid printing secrets.
|
||||
|
||||
The root token is read only by this ephemeral bootstrap container. No root token
|
||||
or unseal key is ever mounted into the long-running application containers.
|
||||
"""
|
||||
import json, os, ssl
|
||||
from pathlib import Path
|
||||
from urllib.request import Request, urlopen
|
||||
from urllib.error import HTTPError
|
||||
|
||||
addr='https://openbao:8200/v1/'
|
||||
context=ssl.create_default_context(cafile='/run/pki/ca.crt')
|
||||
operator=json.loads(Path('/operator/init.json').read_text())
|
||||
token=operator['root_token']
|
||||
|
||||
def call(method,path,data=None,raw=False):
|
||||
headers={'X-Vault-Token':token}
|
||||
body=None
|
||||
if data is not None:
|
||||
body=(data if raw else json.dumps(data).encode('utf-8'))
|
||||
headers['Content-Type']='application/x-www-form-urlencoded' if raw else 'application/json'
|
||||
req=Request(addr+path,data=body,headers=headers,method=method)
|
||||
try:
|
||||
with urlopen(req,context=context,timeout=10) as r:
|
||||
content=r.read()
|
||||
return json.loads(content) if content else {}
|
||||
except HTTPError as e:
|
||||
raise RuntimeError(f'OpenBao bootstrap HTTP {e.code} for {path} (body hidden)') from None
|
||||
|
||||
def main():
|
||||
try: call('POST','sys/mounts/kv',{'type':'kv','options':{'version':'2'},'description':'App service credentials'})
|
||||
except RuntimeError:
|
||||
# Re-executing bootstrap against an existing KV mount is expected.
|
||||
mounts=call('GET','sys/mounts')
|
||||
if 'kv/' not in mounts: raise
|
||||
try: call('POST','sys/auth/approle',{'type':'approle'})
|
||||
except RuntimeError:
|
||||
methods=call('GET','sys/auth')
|
||||
if 'approle/' not in methods: raise
|
||||
root=Path('/issued');root.mkdir(parents=True,exist_ok=True)
|
||||
def create(role,values):
|
||||
policy=Path('/policies/'+role+'.hcl').read_text()
|
||||
call('PUT','sys/policies/acl/'+role,{'policy':policy})
|
||||
call('POST','kv/data/'+role,{'data':values})
|
||||
call('POST','auth/approle/role/'+role,{
|
||||
'bind_secret_id':True,'secret_id_ttl':'24h','secret_id_num_uses':0,
|
||||
'token_policies':[role],'token_ttl':'10m','token_max_ttl':'30m',
|
||||
'token_no_default_policy':True})
|
||||
role_id=call('GET','auth/approle/role/'+role+'/role-id')['data']['role_id']
|
||||
secret_id=call('POST','auth/approle/role/'+role+'/secret-id',{})['data']['secret_id']
|
||||
# Docker named volumes: own role directory, not Windows bind-mounted files.
|
||||
# Fail closed if Linux ownership cannot be applied; never chmod secrets world-readable.
|
||||
role_dir=root/role
|
||||
role_dir.mkdir(parents=True,exist_ok=True)
|
||||
os.chown(role_dir,999,999)
|
||||
os.chmod(role_dir,0o700)
|
||||
for suffix, value in (('role_id',role_id),('secret_id',secret_id)):
|
||||
path=role_dir/suffix
|
||||
path.write_text(value)
|
||||
os.chown(path,999,999)
|
||||
os.chmod(path,0o600)
|
||||
pfx='postgresql://'
|
||||
host='@postgres:5432/'+os.environ['POSTGRES_DB']+'?sslmode=verify-full&sslrootcert=/run/pki/ca.crt'
|
||||
# passwords are URL-encoded to avoid reserved characters from token_urlsafe etc.
|
||||
from urllib.parse import quote
|
||||
def db(role): return pfx+'messenger_'+role+':'+quote(os.environ['DB_'+role.upper()+'_PASSWORD'],safe='')+host
|
||||
s3_common={'S3_ENDPOINT':'https://s3-gw:8334','S3_BUCKET':os.environ['S3_BUCKET']}
|
||||
create('api', {'DATABASE_URL':db('api'),'REDIS_URL':'rediss://:'+quote(os.environ['REDIS_PASSWORD'],safe='')+'@redis-gw:6380/0?ssl_ca_certs=/run/pki/ca.crt',
|
||||
**s3_common, 'S3_ACCESS_KEY':os.environ['S3_API_ACCESS_KEY'],'S3_SECRET_KEY':os.environ['S3_API_SECRET_KEY']})
|
||||
create('worker', {'DATABASE_URL':db('worker'), **s3_common,
|
||||
'S3_ACCESS_KEY':os.environ['S3_WORKER_ACCESS_KEY'],'S3_SECRET_KEY':os.environ['S3_WORKER_SECRET_KEY'],
|
||||
'BROKER_HOST':'broker-gw','BROKER_PORT':'5671','BROKER_USER':os.environ['BROKER_WORKER_USER'],'BROKER_PASSWORD':os.environ['BROKER_WORKER_PASSWORD']})
|
||||
create('dispatcher',{'DATABASE_URL':db('dispatcher'),
|
||||
**s3_common,'S3_ACCESS_KEY':os.environ['S3_DISPATCHER_ACCESS_KEY'],'S3_SECRET_KEY':os.environ['S3_DISPATCHER_SECRET_KEY'],
|
||||
'BROKER_HOST':'broker-gw','BROKER_PORT':'5671','BROKER_USER':os.environ['BROKER_DISPATCHER_USER'],'BROKER_PASSWORD':os.environ['BROKER_DISPATCHER_PASSWORD']})
|
||||
print('OpenBao policies, bundles, AppRole identities installed (values omitted)')
|
||||
|
||||
if __name__=='__main__': main()
|
||||
Reference in New Issue
Block a user