feat(security): add scoped identities, TLS boundaries and OpenBao bootstrap
This commit is contained in:
@@ -0,0 +1,265 @@
|
||||
"""Operator-only, idempotent SQL roles and scoped S3 accounts."""
|
||||
|
||||
import json
|
||||
import ssl
|
||||
import time
|
||||
|
||||
import urllib3
|
||||
from minio import Minio
|
||||
from minio.credentials import StaticProvider
|
||||
from minio.minioadmin import MinioAdmin
|
||||
from psycopg import sql
|
||||
|
||||
from scripts.bootstrap_security import OPERATOR, TRUST
|
||||
from scripts.stand import compose
|
||||
|
||||
|
||||
def postgres(raw):
|
||||
# The bootstrap SQL channel is the server's local Unix socket. No password
|
||||
# or SQL literal is placed in argv; stdin and output are never recorded.
|
||||
statements = [
|
||||
"REVOKE ALL ON SCHEMA public FROM PUBLIC; REVOKE ALL ON DATABASE messenger FROM PUBLIC;"
|
||||
]
|
||||
grants = {
|
||||
"api": {
|
||||
"SELECT": [
|
||||
"users",
|
||||
"sessions",
|
||||
"chats",
|
||||
"members",
|
||||
"messages",
|
||||
"idempotency",
|
||||
"attachments",
|
||||
"message_attachments",
|
||||
"upload_reservations",
|
||||
"jobs",
|
||||
"outbox",
|
||||
"attachment_variants",
|
||||
"effect_publications",
|
||||
],
|
||||
"INSERT": [
|
||||
"users",
|
||||
"sessions",
|
||||
"chats",
|
||||
"members",
|
||||
"messages",
|
||||
"idempotency",
|
||||
"attachments",
|
||||
"message_attachments",
|
||||
"upload_reservations",
|
||||
"jobs",
|
||||
"outbox",
|
||||
],
|
||||
"UPDATE": ["users", "idempotency", "jobs"],
|
||||
"DELETE": ["sessions", "upload_reservations"],
|
||||
},
|
||||
"worker": {
|
||||
"SELECT": [
|
||||
"jobs",
|
||||
"attachments",
|
||||
"attachment_variants",
|
||||
"effect_publications",
|
||||
"pipeline_faults",
|
||||
],
|
||||
"INSERT": [
|
||||
"attachment_variants",
|
||||
"effect_publications",
|
||||
"pipeline_events",
|
||||
"outbox",
|
||||
],
|
||||
"UPDATE": ["jobs", "attachments", "pipeline_faults"],
|
||||
},
|
||||
"relay": {
|
||||
"SELECT": ["jobs", "outbox", "attachments"],
|
||||
"INSERT": ["pipeline_events", "outbox"],
|
||||
"UPDATE": ["jobs", "attachments", "outbox"],
|
||||
},
|
||||
"observer": {
|
||||
"SELECT": [
|
||||
"jobs",
|
||||
"effect_publications",
|
||||
"pipeline_events",
|
||||
"job_slo_observations",
|
||||
],
|
||||
"INSERT": ["job_slo_observations"],
|
||||
"UPDATE": ["job_slo_observations"],
|
||||
},
|
||||
"reconciler": {
|
||||
"SELECT": [
|
||||
"jobs",
|
||||
"attachments",
|
||||
"attachment_variants",
|
||||
"upload_reservations",
|
||||
"message_attachments",
|
||||
"effect_publications",
|
||||
],
|
||||
"UPDATE": ["jobs", "attachments"],
|
||||
"DELETE": [
|
||||
"attachments",
|
||||
"attachment_variants",
|
||||
"upload_reservations",
|
||||
"jobs",
|
||||
"outbox",
|
||||
"job_slo_observations",
|
||||
],
|
||||
},
|
||||
}
|
||||
# SQL literals use psycopg's own adapter; names are fixed local roles.
|
||||
for role, permissions in grants.items():
|
||||
name = "svc_" + role
|
||||
password = raw["services"][role]["database_password"]
|
||||
statements.append(
|
||||
"DO $$ BEGIN IF NOT EXISTS(SELECT 1 FROM pg_roles WHERE rolname='"
|
||||
+ name
|
||||
+ "') THEN CREATE ROLE "
|
||||
+ name
|
||||
+ " LOGIN; END IF; END $$;"
|
||||
)
|
||||
statements.append(
|
||||
sql.SQL(
|
||||
"ALTER ROLE {} WITH LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS PASSWORD {};"
|
||||
)
|
||||
.format(sql.Identifier(name), sql.Literal(password))
|
||||
.as_string()
|
||||
)
|
||||
statements.append(
|
||||
"GRANT CONNECT ON DATABASE messenger TO "
|
||||
+ name
|
||||
+ "; GRANT USAGE ON SCHEMA public TO "
|
||||
+ name
|
||||
+ ";"
|
||||
)
|
||||
statements.append(
|
||||
"REVOKE ALL ON ALL TABLES IN SCHEMA public FROM " + name + ";"
|
||||
)
|
||||
statements.append(
|
||||
"REVOKE ALL ON ALL SEQUENCES IN SCHEMA public FROM " + name + ";"
|
||||
)
|
||||
for action, tables in permissions.items():
|
||||
statements.append(
|
||||
"GRANT "
|
||||
+ action
|
||||
+ " ON "
|
||||
+ ",".join(tables)
|
||||
+ " TO "
|
||||
+ name
|
||||
+ ";"
|
||||
)
|
||||
# Only append-only technical event/outbox IDs use sequences.
|
||||
sequences = {
|
||||
"api": ["outbox_id_seq"],
|
||||
"worker": ["outbox_id_seq", "pipeline_events_id_seq"],
|
||||
"relay": ["outbox_id_seq", "pipeline_events_id_seq"],
|
||||
}.get(role, [])
|
||||
if sequences:
|
||||
statements.append(
|
||||
"GRANT USAGE ON SEQUENCE "
|
||||
+ ",".join(sequences)
|
||||
+ " TO "
|
||||
+ name
|
||||
+ ";"
|
||||
)
|
||||
statements.append("GRANT EXECUTE ON FUNCTION login_wait(text) TO svc_api;")
|
||||
result = compose(
|
||||
"exec",
|
||||
"-T",
|
||||
"postgres",
|
||||
"psql",
|
||||
"-q",
|
||||
"-U",
|
||||
"messenger",
|
||||
"-d",
|
||||
"messenger",
|
||||
"-v",
|
||||
"ON_ERROR_STOP=1",
|
||||
input="\n".join(statements),
|
||||
)
|
||||
assert "ERROR" not in result
|
||||
|
||||
|
||||
def s3(raw):
|
||||
context = ssl.create_default_context(cafile=TRUST / "ca.pem")
|
||||
context.minimum_version = ssl.TLSVersion.TLSv1_3
|
||||
context.load_cert_chain(
|
||||
OPERATOR / "client/cert.pem", OPERATOR / "client/key.pem"
|
||||
)
|
||||
pool = urllib3.PoolManager(
|
||||
ssl_context=context,
|
||||
timeout=urllib3.Timeout(connect=2, read=10),
|
||||
retries=False,
|
||||
)
|
||||
credentials = StaticProvider(raw["s3_root_user"], raw["s3_root_password"])
|
||||
admin = MinioAdmin(
|
||||
"localhost:9007",
|
||||
credentials=credentials,
|
||||
secure=True,
|
||||
http_client=pool,
|
||||
)
|
||||
client = Minio(
|
||||
"localhost:9007",
|
||||
credentials=credentials,
|
||||
secure=True,
|
||||
http_client=pool,
|
||||
region="us-east-1",
|
||||
)
|
||||
for attempt in range(60):
|
||||
try:
|
||||
if not client.bucket_exists("messenger"):
|
||||
client.make_bucket("messenger")
|
||||
break
|
||||
except Exception:
|
||||
if attempt == 59:
|
||||
raise RuntimeError("S3 TLS bootstrap unavailable") from None
|
||||
time.sleep(1)
|
||||
for role in ["api", "worker", "reconciler"]:
|
||||
statements = [
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:GetBucketLocation"],
|
||||
"Resource": ["arn:aws:s3:::messenger"],
|
||||
}
|
||||
]
|
||||
|
||||
def allow(actions, resource, condition=None, statements=statements):
|
||||
statement = {
|
||||
"Effect": "Allow",
|
||||
"Action": actions,
|
||||
"Resource": [resource],
|
||||
}
|
||||
if condition:
|
||||
statement["Condition"] = condition
|
||||
statements.append(statement)
|
||||
|
||||
allow(["s3:GetObject"], "arn:aws:s3:::messenger/objects/*")
|
||||
if role == "api":
|
||||
allow(["s3:PutObject"], "arn:aws:s3:::messenger/objects/staging/*")
|
||||
if role == "worker":
|
||||
allow(["s3:PutObject"], "arn:aws:s3:::messenger/objects/*/v*/*")
|
||||
if role == "reconciler":
|
||||
allow(["s3:DeleteObject"], "arn:aws:s3:::messenger/objects/*")
|
||||
allow(
|
||||
["s3:ListBucket"],
|
||||
"arn:aws:s3:::messenger",
|
||||
{"StringLike": {"s3:prefix": ["objects/*"]}},
|
||||
)
|
||||
values = raw["services"][role]["values"]
|
||||
admin.policy_add(
|
||||
"messenger-" + role,
|
||||
policy={"Version": "2012-10-17", "Statement": statements},
|
||||
)
|
||||
admin.user_add(values["S3_ACCESS_KEY"], values["S3_SECRET_KEY"])
|
||||
admin.attach_policy(
|
||||
["messenger-" + role], user=values["S3_ACCESS_KEY"]
|
||||
)
|
||||
pool.clear()
|
||||
|
||||
|
||||
def main():
|
||||
raw = json.loads((OPERATOR / "service-secrets.json").read_text())
|
||||
postgres(raw)
|
||||
s3(raw)
|
||||
print("SQL role grants and scoped S3 accounts prepared; no values printed")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user