Implement lab4 S3 attachments and reliability checks
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
DIRECTORY = ROOT / "secrets"
|
||||
BUCKET = "messenger-attachments"
|
||||
|
||||
KEY_FILES = (
|
||||
"s3_admin_access_key",
|
||||
"s3_admin_secret_key",
|
||||
"s3_access_key_id",
|
||||
"s3_secret_access_key",
|
||||
)
|
||||
|
||||
|
||||
def configuration(values):
|
||||
return {
|
||||
"identities": [
|
||||
{
|
||||
"name": "bootstrap-admin",
|
||||
"credentials": [{
|
||||
"accessKey": values["s3_admin_access_key"],
|
||||
"secretKey": values["s3_admin_secret_key"],
|
||||
}],
|
||||
"actions": ["Admin", "Read", "Write", "List", "Tagging"],
|
||||
},
|
||||
{
|
||||
"name": "messenger-api",
|
||||
"credentials": [{
|
||||
"accessKey": values["s3_access_key_id"],
|
||||
"secretKey": values["s3_secret_access_key"],
|
||||
}],
|
||||
"actions": [
|
||||
f"Read:{BUCKET}",
|
||||
f"Write:{BUCKET}",
|
||||
f"List:{BUCKET}",
|
||||
],
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def ensure_ignores():
|
||||
patterns = (
|
||||
".env",
|
||||
"/secrets/",
|
||||
"*.bak",
|
||||
"__pycache__/",
|
||||
"*.py[cod]",
|
||||
)
|
||||
for filename in (".gitignore", ".dockerignore"):
|
||||
path = ROOT / filename
|
||||
text = path.read_text() if path.exists() else ""
|
||||
existing = set(text.splitlines())
|
||||
missing = [item for item in patterns if item not in existing]
|
||||
if missing:
|
||||
path.write_text(
|
||||
text.rstrip("\n") + "\n" + "\n".join(missing) + "\n"
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
if DIRECTORY.is_symlink():
|
||||
raise SystemExit("Refusing a symlink at secrets/")
|
||||
|
||||
paths = [DIRECTORY / name for name in KEY_FILES]
|
||||
paths.append(DIRECTORY / "s3_config.json")
|
||||
present = [path.exists() for path in paths]
|
||||
|
||||
if any(present):
|
||||
if not all(path.is_file() and not path.is_symlink() for path in paths):
|
||||
raise SystemExit(
|
||||
"Incomplete or unsafe secret files; nothing regenerated"
|
||||
)
|
||||
|
||||
values = {
|
||||
name: (DIRECTORY / name).read_text().rstrip("\r\n")
|
||||
for name in KEY_FILES
|
||||
}
|
||||
if not all(values.values()):
|
||||
raise SystemExit("Empty credential file; nothing regenerated")
|
||||
|
||||
actual = json.loads(
|
||||
(DIRECTORY / "s3_config.json").read_text()
|
||||
)
|
||||
if actual != configuration(values):
|
||||
raise SystemExit(
|
||||
"Credentials and S3 configuration differ; nothing regenerated"
|
||||
)
|
||||
message = "Existing credentials verified; keys unchanged."
|
||||
else:
|
||||
DIRECTORY.mkdir(mode=0o700, exist_ok=True)
|
||||
DIRECTORY.chmod(0o700)
|
||||
|
||||
values = {
|
||||
"s3_admin_access_key": "admin-" + secrets.token_hex(12),
|
||||
"s3_admin_secret_key": secrets.token_hex(32),
|
||||
"s3_access_key_id": "api-" + secrets.token_hex(12),
|
||||
"s3_secret_access_key": secrets.token_hex(32),
|
||||
}
|
||||
|
||||
for name, value in values.items():
|
||||
with (DIRECTORY / name).open("x") as stream:
|
||||
stream.write(value + "\n")
|
||||
|
||||
with (DIRECTORY / "s3_config.json").open("x") as stream:
|
||||
json.dump(configuration(values), stream, indent=2)
|
||||
stream.write("\n")
|
||||
|
||||
message = "New credentials created; values are not printed."
|
||||
|
||||
DIRECTORY.chmod(0o700)
|
||||
for path in paths:
|
||||
path.chmod(0o644)
|
||||
|
||||
ensure_ignores()
|
||||
print(message)
|
||||
print("Secrets, .env, Python cache and .bak files are excluded.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user