forked from mtusi2026/mtusi2026lab7
grade3
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
export MSYS_NO_PATHCONV=1
|
||||
CERT_DIR="${CERT_DIR:-./infra/certs}"
|
||||
mkdir -p "$CERT_DIR"
|
||||
EMPTY_SSL_CNF="$CERT_DIR/empty-openssl.cnf"
|
||||
: > "$EMPTY_SSL_CNF"
|
||||
export OPENSSL_CONF="$(cygpath -w "$EMPTY_SSL_CNF")"
|
||||
|
||||
CERT_DIR="${CERT_DIR:-./infra/certs}"
|
||||
mkdir -p "$CERT_DIR"
|
||||
chmod 700 "$CERT_DIR"
|
||||
|
||||
if [ ! -f "$CERT_DIR/ca.key" ]; then
|
||||
openssl genrsa -out "$CERT_DIR/ca.key" 4096
|
||||
openssl req -x509 -new -nodes -key "$CERT_DIR/ca.key" -sha256 -days 3650 \
|
||||
-subj "/CN=MTUSI-Messenger-Local-CA" \
|
||||
-addext "basicConstraints=critical,CA:TRUE" \
|
||||
-addext "keyUsage=critical,keyCertSign,cRLSign" \
|
||||
-out "$CERT_DIR/ca.crt"
|
||||
fi
|
||||
|
||||
if [ ! -f "$CERT_DIR/lb-ca.key" ]; then
|
||||
openssl genrsa -out "$CERT_DIR/lb-ca.key" 4096
|
||||
openssl req -x509 -new -nodes -key "$CERT_DIR/lb-ca.key" -sha256 -days 3650 \
|
||||
-subj "/CN=MTUSI-Messenger-LB-CA" \
|
||||
-addext "basicConstraints=critical,CA:TRUE" \
|
||||
-addext "keyUsage=critical,keyCertSign,cRLSign" \
|
||||
-out "$CERT_DIR/lb-ca.crt"
|
||||
fi
|
||||
|
||||
issue_cert() {
|
||||
local name="$1"
|
||||
local san="$2"
|
||||
if [ -f "$CERT_DIR/${name}.crt" ]; then
|
||||
return 0
|
||||
fi
|
||||
openssl genrsa -out "$CERT_DIR/${name}.key" 2048
|
||||
cat > "$CERT_DIR/${name}.ext" <<EOF
|
||||
subjectAltName=${san}
|
||||
extendedKeyUsage=serverAuth,clientAuth
|
||||
EOF
|
||||
MSYS_NO_PATHCONV=1 openssl req -new -key "$CERT_DIR/${name}.key" \
|
||||
-subj "/CN=${name}" \
|
||||
-out "$CERT_DIR/${name}.csr"
|
||||
openssl x509 -req -in "$CERT_DIR/${name}.csr" \
|
||||
-CA "$CERT_DIR/ca.crt" -CAkey "$CERT_DIR/ca.key" -CAcreateserial \
|
||||
-out "$CERT_DIR/${name}.crt" -days 365 -sha256 \
|
||||
-extfile "$CERT_DIR/${name}.ext"
|
||||
rm -f "$CERT_DIR/${name}.csr" "$CERT_DIR/${name}.ext"
|
||||
chmod 600 "$CERT_DIR/${name}.key"
|
||||
}
|
||||
|
||||
issue_cert_signed_by() {
|
||||
local name="$1"; local san="$2"; local ca_crt="$3"; local ca_key="$4"
|
||||
if [ -f "$CERT_DIR/${name}.crt" ]; then return 0; fi
|
||||
openssl genrsa -out "$CERT_DIR/${name}.key" 2048
|
||||
cat > "$CERT_DIR/${name}.ext" <<EOF
|
||||
subjectAltName=${san}
|
||||
extendedKeyUsage=serverAuth,clientAuth
|
||||
EOF
|
||||
MSYS_NO_PATHCONV=1 openssl req -new -key "$CERT_DIR/${name}.key" \
|
||||
-subj "/CN=${name}" -out "$CERT_DIR/${name}.csr"
|
||||
openssl x509 -req -in "$CERT_DIR/${name}.csr" \
|
||||
-CA "$ca_crt" -CAkey "$ca_key" -CAcreateserial \
|
||||
-out "$CERT_DIR/${name}.crt" -days 365 -sha256 -extfile "$CERT_DIR/${name}.ext"
|
||||
rm -f "$CERT_DIR/${name}.csr" "$CERT_DIR/${name}.ext"
|
||||
chmod 600 "$CERT_DIR/${name}.key"
|
||||
}
|
||||
|
||||
issue_cert "edge-lb" "DNS:localhost,DNS:nginx,IP:127.0.0.1"
|
||||
issue_cert "api-1" "DNS:api-1,DNS:messenger-api"
|
||||
issue_cert "api-2" "DNS:api-2,DNS:messenger-api"
|
||||
issue_cert "worker" "DNS:worker,DNS:messenger-worker"
|
||||
issue_cert "postgres" "DNS:postgres"
|
||||
issue_cert "redis" "DNS:redis"
|
||||
issue_cert "s3" "DNS:s3"
|
||||
issue_cert "otel-collector" "DNS:otel-collector"
|
||||
issue_cert "openbao" "DNS:openbao,IP:127.0.0.1"
|
||||
|
||||
echo "PKI certificates generated in $CERT_DIR"
|
||||
+16
-9
@@ -2,19 +2,25 @@ import os
|
||||
import boto3
|
||||
from botocore.client import Config
|
||||
|
||||
S3_ENDPOINT = os.getenv("S3_ENDPOINT", "http://s3:8333")
|
||||
S3_ENDPOINT = os.getenv("S3_ENDPOINT", "https://s3:8333")
|
||||
S3_BUCKET = os.getenv("S3_BUCKET", "messenger-attachments")
|
||||
S3_ROOT_ACCESS_KEY = os.getenv("S3_ROOT_ACCESS_KEY", "minioadmin")
|
||||
S3_ROOT_SECRET_KEY = os.getenv("S3_ROOT_SECRET_KEY", "minioadminpassword")
|
||||
AWS_CA_BUNDLE = os.getenv("AWS_CA_BUNDLE")
|
||||
|
||||
client_kwargs = {
|
||||
"service_name": "s3",
|
||||
"endpoint_url": S3_ENDPOINT,
|
||||
"aws_access_key_id": S3_ROOT_ACCESS_KEY,
|
||||
"aws_secret_access_key": S3_ROOT_SECRET_KEY,
|
||||
"config": Config(signature_version="s3v4"),
|
||||
"region_name": "us-east-1",
|
||||
}
|
||||
if S3_ENDPOINT.startswith("https://") and AWS_CA_BUNDLE:
|
||||
client_kwargs["verify"] = AWS_CA_BUNDLE
|
||||
|
||||
s3_client = boto3.client(**client_kwargs)
|
||||
|
||||
s3_client = boto3.client(
|
||||
"s3",
|
||||
endpoint_url=S3_ENDPOINT,
|
||||
aws_access_key_id=S3_ROOT_ACCESS_KEY,
|
||||
aws_secret_access_key=S3_ROOT_SECRET_KEY,
|
||||
config=Config(signature_version="s3v4"),
|
||||
region_name="us-east-1"
|
||||
)
|
||||
|
||||
def init_bucket():
|
||||
print(f"Checking/Creating bucket: {S3_BUCKET}")
|
||||
@@ -25,5 +31,6 @@ def init_bucket():
|
||||
s3_client.create_bucket(Bucket=S3_BUCKET)
|
||||
print("Bucket successfully created.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
init_bucket()
|
||||
@@ -8,8 +8,8 @@ import re
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
def main():
|
||||
lab = json.loads((ROOT/'lab.json').read_text())['number']
|
||||
spec = json.loads((ROOT/'contracts/openapi.json').read_text())
|
||||
lab = json.loads((ROOT/'lab.json').read_text(encoding='utf-8'))['number']
|
||||
spec = json.loads((ROOT/'contracts/openapi.json').read_text(encoding='utf-8'))
|
||||
assert spec['openapi']=='3.1.0'
|
||||
assert spec['info']['version']=='1.%d.0'%lab
|
||||
operation_ids = set()
|
||||
@@ -41,11 +41,11 @@ def main():
|
||||
assert ('/api/v1/auth/sessions' in spec['paths']) == (lab>=2)
|
||||
assert ('/api/v1/attachments/{attachment_id}' in spec['paths']) == (lab>=4)
|
||||
for file in list((ROOT/'tests').glob('*.py'))+list((ROOT/'scripts').glob('*.py')):
|
||||
ast.parse(file.read_text(),filename=str(file))
|
||||
ast.parse(file.read_text(encoding='utf-8'),filename=str(file))
|
||||
for file in ROOT.rglob('*.json'):
|
||||
if any(part in {'.git','.venv','node_modules','vendor','output'} for part in file.parts):
|
||||
continue
|
||||
json.loads(file.read_text())
|
||||
json.loads(file.read_text(encoding='utf-8-sig'))
|
||||
for required in ('README.md','COURSE.md','CONTRIBUTING.md','REPORT.md','contracts/README.md','tests/smoke.py','tests/README.md'):
|
||||
assert (ROOT/required).is_file(), 'missing '+required
|
||||
print('PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.')
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
OPERATOR_STATE_DIR="${OPERATOR_STATE_DIR:-./.bao-operator}"
|
||||
mkdir -p "$OPERATOR_STATE_DIR" ./infra/certs
|
||||
chmod 700 "$OPERATOR_STATE_DIR"
|
||||
for f in ./infra/certs/api-approle.env ./infra/certs/worker-approle.env; do
|
||||
if [ ! -f "$f" ]; then
|
||||
printf "BAO_ROLE_ID=\nBAO_SECRET_ID=\n" > "$f"
|
||||
chmod 600 "$f"
|
||||
fi
|
||||
done
|
||||
|
||||
bao_exec() {
|
||||
MSYS_NO_PATHCONV=1 docker compose exec -T \
|
||||
-e BAO_ADDR=https://127.0.0.1:8200 \
|
||||
-e BAO_CACERT=/openbao/certs/ca.crt \
|
||||
-e BAO_CLIENT_CERT=/openbao/certs/openbao.crt \
|
||||
-e BAO_CLIENT_KEY=/openbao/certs/openbao.key \
|
||||
${BAO_TOKEN:+-e BAO_TOKEN="$BAO_TOKEN"} \
|
||||
openbao bao "$@"
|
||||
}
|
||||
|
||||
if [ ! -s "$OPERATOR_STATE_DIR/init.json" ]; then
|
||||
INIT_OUT="$(bao_exec operator init -key-shares=1 -key-threshold=1 -format=json)"
|
||||
printf '%s\n' "$INIT_OUT" > "$OPERATOR_STATE_DIR/init.json"
|
||||
chmod 600 "$OPERATOR_STATE_DIR/init.json"
|
||||
fi
|
||||
|
||||
UNSEAL_KEY=$(python3 -c "import json; p='$OPERATOR_STATE_DIR/init.json'; d=json.load(open(p, encoding='utf-8')); print(d['unseal_keys_b64'][0])")
|
||||
ROOT_TOKEN=$(python3 -c "import json; p='$OPERATOR_STATE_DIR/init.json'; d=json.load(open(p, encoding='utf-8')); print(d['root_token'])")
|
||||
|
||||
if ! bao_exec status >/dev/null 2>&1; then
|
||||
bao_exec operator unseal "$UNSEAL_KEY" >/dev/null
|
||||
fi
|
||||
|
||||
export BAO_TOKEN="$ROOT_TOKEN"
|
||||
|
||||
bao_exec secrets enable -path=secret kv-v2 >/dev/null 2>&1 || true
|
||||
bao_exec auth enable approle >/dev/null 2>&1 || true
|
||||
|
||||
bao_exec policy write messenger-api /openbao/policies/policy-api.hcl >/dev/null
|
||||
bao_exec policy write messenger-worker /openbao/policies/policy-worker.hcl >/dev/null
|
||||
|
||||
bao_exec kv put secret/messenger/api/db \
|
||||
dsn="${DATABASE_URL:?set DATABASE_URL}" \
|
||||
hmac_key="${SECRET_HMAC_KEY:?set SECRET_HMAC_KEY}" >/dev/null
|
||||
|
||||
bao_exec kv put secret/messenger/api/s3 \
|
||||
access_key_id="${S3_ACCESS_KEY_ID:-appuser}" \
|
||||
secret_access_key="${S3_SECRET_ACCESS_KEY:-appsecretpassword}" >/dev/null
|
||||
|
||||
bao_exec kv put secret/messenger/worker/db \
|
||||
dsn="${DATABASE_URL:?set DATABASE_URL}" >/dev/null
|
||||
|
||||
bao_exec kv put secret/messenger/worker/s3 \
|
||||
access_key_id="${S3_ACCESS_KEY_ID:-appuser}" \
|
||||
secret_access_key="${S3_SECRET_ACCESS_KEY:-appsecretpassword}" >/dev/null
|
||||
|
||||
bao_exec write auth/approle/role/messenger-api \
|
||||
token_policies="messenger-api" \
|
||||
token_ttl="1h" \
|
||||
token_max_ttl="4h" >/dev/null
|
||||
|
||||
bao_exec write auth/approle/role/messenger-worker \
|
||||
token_policies="messenger-worker" \
|
||||
token_ttl="1h" \
|
||||
token_max_ttl="4h" >/dev/null
|
||||
|
||||
API_ROLE_ID=$(bao_exec read -field=role_id auth/approle/role/messenger-api/role-id)
|
||||
API_SECRET_ID=$(bao_exec write -f -field=secret_id auth/approle/role/messenger-api/secret-id)
|
||||
|
||||
WORKER_ROLE_ID=$(bao_exec read -field=role_id auth/approle/role/messenger-worker/role-id)
|
||||
WORKER_SECRET_ID=$(bao_exec write -f -field=secret_id auth/approle/role/messenger-worker/secret-id)
|
||||
|
||||
cat > ./infra/certs/api-approle.env <<EOF
|
||||
BAO_ROLE_ID=${API_ROLE_ID}
|
||||
BAO_SECRET_ID=${API_SECRET_ID}
|
||||
EOF
|
||||
|
||||
cat > ./infra/certs/worker-approle.env <<EOF
|
||||
BAO_ROLE_ID=${WORKER_ROLE_ID}
|
||||
BAO_SECRET_ID=${WORKER_SECRET_ID}
|
||||
EOF
|
||||
|
||||
chmod 600 ./infra/certs/api-approle.env ./infra/certs/worker-approle.env
|
||||
echo "OpenBao configured with scoped AppRole credentials (root token NOT written to app env)."
|
||||
Reference in New Issue
Block a user