(context, url)
| 292 | |
| 293 | |
| 294 | def _fetch_server_key_from(context, url): # type: (RestApiContext, str) -> SelfManagedServerKey |
| 295 | rs = requests.get(url, proxies=context.proxies, verify=context.certificate_check, |
| 296 | timeout=SERVER_KEY_FETCH_TIMEOUT) |
| 297 | rs.raise_for_status() |
| 298 | try: |
| 299 | server_key = rs.json() |
| 300 | except ValueError: |
| 301 | raise ValueError('did not return JSON') |
| 302 | if not isinstance(server_key, dict): |
| 303 | raise ValueError('did not return a server key') |
| 304 | public_key = server_key.get('serverPublicKey') |
| 305 | key_id = server_key.get('serverPublicKeyId') |
| 306 | if not public_key or not key_id: |
| 307 | raise ValueError(f'has no server key: {server_key.get("error") or "serverPublicKey is missing"}') |
| 308 | key = _make_server_key(key_id, public_key, f'fetched from {url}', fetched=True) |
| 309 | published = server_key.get('fingerprint') |
| 310 | if isinstance(published, str) and published: |
| 311 | if _fingerprint_matches(published, public_key) is False: |
| 312 | raise ValueError(f'the fingerprint it published ({published}) does not match its key ' |
| 313 | f'({server_key_fingerprint(public_key)})') |
| 314 | return key |
| 315 | |
| 316 | |
| 317 | def _fetch_server_key(context, host): # type: (RestApiContext, str) -> SelfManagedServerKey |
no test coverage detected