def test_get_suitable_signing_servers(context, formats, expected): expected_servers = [] for info in expected: expected_servers.append(SigningServer(*info)) assert sign.get_suitable_signing_servers(context.signing_servers, TEST_CERT_TYPE, formats) == expected_servers
async def get_token(context, output_file, cert_type, signing_formats): """Retrieve a token from the signingserver tied to my ip. Args: context (Context): the signing context output_file (str): the path to write the token to. cert_type (str): the cert type used to find an appropriate signing server signing_formats (list): the signing formats used to find an appropriate signing server Raises: SigningServerError: on failure """ token = None data = { "slave_ip": context.config["my_ip"], "duration": context.config["token_duration_seconds"], } signing_servers = get_suitable_signing_servers( context.signing_servers, cert_type, [ fmt for fmt in signing_formats if not is_autograph_signing_format(fmt) ], ) random.shuffle(signing_servers) for s in signing_servers: log.info("getting token from %s", s.server) url = "https://{}/token".format(s.server) auth = aiohttp.BasicAuth(s.user, password=s.password) try: token = await retry_request(context, url, method="post", data=data, auth=auth, return_type="text") if token: break except ( ScriptWorkerException, aiohttp.ClientError, asyncio.TimeoutError, ) as exc: log.warning( "Error retrieving token: {}\nTrying the next server.".format( str(exc))) continue else: raise SigningServerError( "Cannot retrieve signing token from any signing server.") with open(output_file, "w") as fh: print(token, file=fh, end="")
def test_get_suitable_signing_servers_raises_signingscript_error(context): with pytest.raises(SigningScriptError): sign.get_suitable_signing_servers(context.signing_servers, TEST_CERT_TYPE, signing_formats=['invalid'], raise_on_empty_list=True)