ЛР 7: Безопасность, mTLS и OpenBao
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,77 @@
|
||||
import json
|
||||
import os
|
||||
import ssl
|
||||
import urllib.request
|
||||
|
||||
|
||||
def read_file(path):
|
||||
try:
|
||||
return open(path, "r", encoding="utf-8").read().strip()
|
||||
except OSError:
|
||||
return ""
|
||||
|
||||
|
||||
def bao_request(address, ca_file, path, body=None, token=None):
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if token:
|
||||
headers["X-Vault-Token"] = token
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
request = urllib.request.Request(
|
||||
address.rstrip("/") + path,
|
||||
data=data,
|
||||
headers=headers,
|
||||
method="POST" if body is not None else "GET",
|
||||
)
|
||||
context = ssl.create_default_context(cafile=ca_file)
|
||||
with urllib.request.urlopen(request, context=context, timeout=5) as response:
|
||||
return json.loads(response.read())
|
||||
|
||||
|
||||
def load_service_secrets(service):
|
||||
if service not in ("api", "worker"):
|
||||
raise ValueError("Unknown OpenBao service identity")
|
||||
address = os.environ.get("BAO_ADDR", "").strip()
|
||||
ca_file = os.environ.get("BAO_CACERT", "").strip()
|
||||
role_id_file = os.environ.get("BAO_ROLE_ID_FILE", "").strip()
|
||||
secret_id_file = os.environ.get("BAO_SECRET_ID_FILE", "").strip()
|
||||
if not all((address, ca_file, role_id_file, secret_id_file)):
|
||||
raise RuntimeError("OpenBao service authentication is not configured")
|
||||
|
||||
role_id = read_file(role_id_file)
|
||||
secret_id = read_file(secret_id_file)
|
||||
if not role_id or not secret_id:
|
||||
raise RuntimeError("OpenBao AppRole secret files are unavailable")
|
||||
|
||||
login = bao_request(
|
||||
address,
|
||||
ca_file,
|
||||
"/v1/auth/approle/login",
|
||||
{"role_id": role_id, "secret_id": secret_id},
|
||||
)
|
||||
token = login["auth"]["client_token"]
|
||||
response = bao_request(
|
||||
address,
|
||||
ca_file,
|
||||
"/v1/secret/data/messenger/" + service,
|
||||
token=token,
|
||||
)
|
||||
data = response["data"]["data"]
|
||||
mapping = {
|
||||
"PGUSER": "pg_user",
|
||||
"PGPASSWORD": "pg_password",
|
||||
"S3_SECRET_ACCESS_KEY": "s3_secret",
|
||||
"S3_ACCESS_KEY_ID": "s3_access",
|
||||
}
|
||||
for env_name, secret_name in mapping.items():
|
||||
if not data.get(secret_name):
|
||||
raise RuntimeError("OpenBao service secret is incomplete")
|
||||
os.environ[env_name] = str(data[secret_name])
|
||||
if not data.get("broker_user") or not data.get("broker_password"):
|
||||
raise RuntimeError("OpenBao broker credentials are unavailable")
|
||||
from urllib.parse import quote
|
||||
|
||||
username = quote(str(data["broker_user"]), safe="")
|
||||
password = quote(str(data["broker_password"]), safe="")
|
||||
os.environ["BROKER_URL"] = (
|
||||
"amqps://" + username + ":" + password + "@broker:5671/%2F"
|
||||
)
|
||||
Reference in New Issue
Block a user