"""Local-only credentials. No values printed; preserve existing secrets.""" import json from pathlib import Path import secrets ROOT = Path(__file__).resolve().parents[1] identity=ROOT/'identity' identity.mkdir(exist_ok=True) for role in ('api1','api2'): path=identity/(role+'.txt') if not path.exists():path.write_text(secrets.token_hex(16),encoding='utf-8') folder = ROOT/'secrets' folder.mkdir(exist_ok=True) names = ['postgres_password','minio_root_user','minio_root_password','cursor_secret','grafana_password'] names += [role+'_'+kind for role in ('api','worker','publisher','janitor','operator') for kind in ('db_password','s3_key','s3_secret','broker_password')] values = {} for name in names: path = folder/name if not path.exists(): path.write_text(secrets.token_hex(24),encoding='utf-8') values[name] = path.read_text().strip() # bootstrap.sh maintains the lab5 policy names with newly generated identities. for old,new in {'s3_access_key_id':'api_s3_key','s3_secret_access_key':'api_s3_secret', 'worker_access_key_id':'worker_s3_key','worker_secret_access_key':'worker_s3_secret', 'operator_access_key_id':'operator_s3_key','operator_secret_access_key':'operator_s3_secret'}.items(): (folder/old).write_text(values[new]) if not (ROOT/'.env').exists(): (ROOT/'.env').write_text('LAB7_PORT=8443\nSESSION_TTL_SECONDS=3600\n',encoding='utf-8') acl = ['user default off'] for role in ('api','worker','publisher','operator'): password = values[role+'_broker_password'] if role=='operator': rights = '~* +@all' elif role=='publisher': rights = '~attachments:v1 +ping +xadd' elif role=='api': rights = '+ping' else: rights = '+ping (~attachments:v1 +xreadgroup +xgroup +xautoclaim +xack +xdel) (~attachments:invalid +xadd)' acl.append('user '+role+' on >'+password+' -@all '+rights) (folder/'broker.acl').write_text('\n'.join(acl)+'\n') print('Local lab7 credentials prepared; no secrets displayed.') def grafana(): folder = ROOT/'secrets/grafana-runtime' folder.mkdir(exist_ok=True) ca = (ROOT/'pki/ca.crt').read_text() cert = (ROOT/'secrets/tls/grafana/cert.crt').read_text() key = (ROOT/'secrets/tls/grafana/key.key').read_text() sources = [] for name,port,kind in [('prometheus',9443,'prometheus'),('loki',3443,'loki'),('tempo',3443,'tempo')]: sources.append({'name':name.title(),'uid':name,'type':kind,'access':'proxy','url':'https://'+name+':'+str(port), 'isDefault':name=='prometheus','jsonData':{'tlsAuth':True,'tlsAuthWithCACert':True,'tlsSkipVerify':False,'serverName':name,'timeInterval':'3s'}, 'secureJsonData':{'tlsCACert':ca,'tlsClientCert':cert,'tlsClientKey':key}}) (folder/'sources.yaml').write_text(json.dumps({'apiVersion':1,'datasources':sources},indent=2)) if (ROOT/'pki/ca.crt').exists(): grafana()