ЛР 7: безопасность, mTLS и OpenBao на оценку 3
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Skeleton contracts / contracts (pull_request) Blocked by required conditions
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
"""Verify the provisioned Grafana data sources through authenticated HTTPS."""
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import ssl
|
||||
import time
|
||||
from urllib.parse import urlencode
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
STATE = Path(os.environ.get("LAB7_STATE", ROOT / "secrets/lab7"))
|
||||
ADDRESS = f"https://localhost:{os.environ.get('GRAFANA_HTTPS_PORT', '3443')}"
|
||||
METRICS = (
|
||||
"http_requests_total", "http_request_duration_seconds_bucket",
|
||||
"jobs_processed_total", "job_processing_duration_seconds_bucket",
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
context = ssl.create_default_context(cafile=str(STATE / "ca/ca.crt"))
|
||||
password = (STATE / "grafana/admin/admin_password").read_text().strip()
|
||||
credentials = base64.b64encode(f"admin:{password}".encode()).decode()
|
||||
|
||||
def get(path, **params):
|
||||
uri = ADDRESS + path + ("?" + urlencode(params) if params else "")
|
||||
req = Request(uri, headers={"Authorization": "Basic " + credentials})
|
||||
with urlopen(req, context=context, timeout=6) as response:
|
||||
return json.load(response)
|
||||
|
||||
prefix = "/api/datasources/proxy/uid/"
|
||||
assert get("/api/health")["database"] == "ok"
|
||||
for _ in range(10):
|
||||
if all(get(prefix + "prometheus/api/v1/query", query=metric)["data"]["result"]
|
||||
for metric in METRICS):
|
||||
break
|
||||
time.sleep(3)
|
||||
else:
|
||||
raise AssertionError("Prometheus has not received all four metrics; run HTTPS smoke first")
|
||||
print("PASS Grafana → Prometheus: API and worker counters and histograms")
|
||||
|
||||
for service in ("messenger-api", "messenger-worker"):
|
||||
streams = get(prefix + "loki/loki/api/v1/query_range",
|
||||
query='{service_name="' + service + '"}', limit=50)["data"]["result"]
|
||||
trace_id = next(item["stream"]["trace_id"] for item in streams if item["stream"].get("trace_id"))
|
||||
trace = get(prefix + "tempo/api/traces/" + trace_id)
|
||||
assert trace.get("batches") or trace.get("resourceSpans"), service
|
||||
print(f"PASS Grafana → Loki → Tempo: {service}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user