Реализовать лабораторную 7
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
import ssl
|
||||
import sys
|
||||
import time
|
||||
from urllib.error import URLError
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
OPERATOR = ROOT / "secrets" / "operator"
|
||||
ADDRESS = "https://localhost:18207"
|
||||
|
||||
|
||||
def request(path, data=None, token=None, method=None):
|
||||
context = ssl.create_default_context(cafile=str(OPERATOR / "ca.pem"))
|
||||
context.load_cert_chain(ROOT / "secrets/tls/operator/cert.pem", ROOT / "secrets/tls/operator/key.pem")
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if token:
|
||||
headers["X-Vault-Token"] = token
|
||||
req = Request(ADDRESS + "/v1/" + path,
|
||||
data=json.dumps(data).encode() if data is not None else None,
|
||||
headers=headers, method=method)
|
||||
with urlopen(req, context=context, timeout=5) as response:
|
||||
return json.load(response) if response.status != 204 else {}
|
||||
|
||||
|
||||
def main():
|
||||
state_path = OPERATOR / "bao-init.json"
|
||||
deadline = time.monotonic() + 30
|
||||
while True:
|
||||
try:
|
||||
request("sys/init")
|
||||
break
|
||||
except URLError as error:
|
||||
if isinstance(error.reason, ssl.SSLCertVerificationError) or time.monotonic() >= deadline:
|
||||
raise
|
||||
time.sleep(0.5)
|
||||
if not request("sys/init")["initialized"]:
|
||||
state = request("sys/init", {"secret_shares": 1, "secret_threshold": 1}, method="PUT")
|
||||
state_path.write_text(json.dumps(state))
|
||||
state_path.chmod(0o600)
|
||||
state = json.loads(state_path.read_text())
|
||||
if request("sys/seal-status")["sealed"]:
|
||||
request("sys/unseal", {"key": state["keys"][0]}, method="PUT")
|
||||
token = state["root_token"]
|
||||
if len(sys.argv) > 1 and sys.argv[1] == "seal":
|
||||
request("sys/seal", {}, token, "PUT")
|
||||
print("OpenBao sealed")
|
||||
return
|
||||
if "secret/" not in request("sys/mounts", token=token):
|
||||
request("sys/mounts/secret", {"type": "kv", "options": {"version": "2"}}, token)
|
||||
if "cert/" not in request("sys/auth", token=token):
|
||||
request("sys/auth/cert", {"type": "cert"}, token)
|
||||
if (OPERATOR / "crl.pem").exists():
|
||||
request("auth/cert/crls/lab", {"crl": (OPERATOR / "crl.pem").read_text()}, token)
|
||||
for service, values in json.loads((OPERATOR / "kv-bootstrap.json").read_text()).items():
|
||||
policy = f'path "secret/data/{service}" {{ capabilities = ["read"] }}'
|
||||
request("sys/policies/acl/" + service, {"policy": policy}, token, "PUT")
|
||||
request("auth/cert/certs/" + service, {
|
||||
"certificate": (OPERATOR / "ca.pem").read_text(),
|
||||
"allowed_dns_sans": [service], "token_policies": [service],
|
||||
"token_ttl": 60, "token_max_ttl": 120,
|
||||
}, token)
|
||||
request("secret/data/" + service, {"data": values}, token)
|
||||
print("OpenBao initialized, unsealed and scoped policies configured")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user