Esempio n. 1
0
def get_azure_token():
    credential_chain = ChainedTokenCredential(ManagedIdentityCredential(), AzureCliCredential())
    try:
        token = credential_chain.get_token("https://monitoring.azure.com//.default")
        return token.token
    except Exception as e:
        logging.exception(f"Failed to retrieve Azure token. Reason is {type(e).__name__} {e}")
        return None
def test_chain_returns_first_token():
    expected_token = Mock()
    first_credential = Mock(get_token=lambda _: expected_token)
    second_credential = Mock(get_token=Mock())

    aggregate = ChainedTokenCredential(first_credential, second_credential)
    credential = aggregate.get_token("scope")

    assert credential is expected_token
    assert second_credential.get_token.call_count == 0