forked from mtusi2026/mtusi2026lab7
40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Give containers group-read access only to individually mounted secret files.
|
|
|
|
The host directory stays owner-only; CA and operator credentials stay 0600.
|
|
Compose bind-mounted file secrets preserve host ownership on Linux.
|
|
"""
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
secret_dir = ROOT / 'secrets'
|
|
if secret_dir.is_symlink():
|
|
raise SystemExit('secrets directory must not be a symlink')
|
|
secret_dir.chmod(0o700)
|
|
compose = (ROOT / 'compose.yaml').read_text()
|
|
names = set(re.findall(r'\./secrets/([A-Za-z0-9_.-]+)', compose))
|
|
gid = os.getgid()
|
|
for name in sorted(names):
|
|
path = secret_dir / name
|
|
if path.is_symlink() or not path.is_file():
|
|
raise SystemExit('Missing or unsafe mounted secret: ' + name)
|
|
os.chown(path, -1, gid)
|
|
path.chmod(0o640)
|
|
for name in ('ca.key', 'openbao-operator.json'):
|
|
path = secret_dir / name
|
|
if path.is_symlink():
|
|
raise SystemExit('Operator credential must not be a symlink')
|
|
if path.exists():
|
|
path.chmod(0o600)
|
|
env = ROOT / '.env'
|
|
if env.is_symlink():
|
|
raise SystemExit('.env must not be a symlink')
|
|
text = env.read_text() if env.exists() else (ROOT / '.env.example').read_text()
|
|
text = re.sub(r'^LAB_SECRET_GID=.*\n?', '', text, flags=re.M)
|
|
with env.open('w') as stream:
|
|
env.chmod(0o600)
|
|
stream.write(text.rstrip() + '\nLAB_SECRET_GID=' + str(gid) + '\n')
|
|
print('Mounted secrets: group-readable; host directory: 0700; operator files: 0600.')
|