#!/usr/bin/env python3 import base64,json,ssl,sys,urllib.error,urllib.request from pathlib import Path root=Path(__file__).resolve().parents[1];s=root/'secrets/lab7';ctx=ssl.create_default_context(cafile=str(s/'grafana/ca.crt')) url='https://localhost:13443/api/user' try:urllib.request.urlopen(url,context=ctx) except urllib.error.HTTPError as e:assert e.code==401 else:raise AssertionError('Grafana user API allowed anonymous access') password=(s/'operator/grafana_password').read_text().strip();req=urllib.request.Request(url,headers={'Authorization':'Basic '+base64.b64encode(('admin:'+password).encode()).decode()}) with urllib.request.urlopen(req,context=ctx) as response: data=json.load(response);assert data['login']=='admin' (root/'evidence/admin.json').write_text(json.dumps({'grafana_verified_https':True,'anonymous_user_api_denied':True,'operator_authenticated':True},indent=2)+'\n');print('PASS: Grafana HTTPS operator authentication and anonymous denial')