Files
moloapp/tests/src/vendors/azure/test_entra.cpp
T

162 lines
6.9 KiB
C++

// Microsoft Entra client-credentials flow: wire shape, parsing of the
// documented success body and of an error body observed live (2026-10-25),
// caching, refresh, failure floor, bearer on every surface.
#include "clients/body/form.h"
#include "rig.h"
#include "vendors/azure/entra.h"
#include "vendors/azure/files.h"
#include "vendors/azure/safety.h"
using namespace molo::azure;
using namespace az_test;
namespace {
// Documented success body (Microsoft identity platform, client credentials flow).
const char kOk[] =
R"({"token_type":"Bearer","expires_in":3599,"ext_expires_in":3599,"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Imk2bEdrM0ZaenhSY1ViMkMzbkVRN3N5SEpsWSJ9"})";
// Observed live from login.microsoftonline.com for a client id that does not exist.
const char kErr[] =
R"({"error":"unauthorized_client","error_description":"AADSTS700038: 00000000-0000-0000-0000-000000000000 is not a valid application identifier. Trace ID: 856e791a-6d26-41ce-a6d4-be68759f9101 Correlation ID: 93a74863-808b-4409-88a1-c2563d0fb599 Timestamp: 2026-10-25 09:24:16Z","error_codes":[700038],"timestamp":"2026-10-25 09:24:16Z","trace_id":"856e791a-6d26-41ce-a6d4-be68759f9101","correlation_id":"93a74863-808b-4409-88a1-c2563d0fb599"})";
struct Clock {
std::int64_t now = 1000000;
EntraTokenSource::Clock fn() {
return [this] { return now; };
}
};
} // namespace
TEST(azure_entra, parses_the_documented_success_body) {
auto p = EntraTokenSource::parse(200, kOk);
ASSERT_TRUE(p.access_token.rfind("eyJ0eXAi", 0) == 0);
ASSERT_EQ(p.expires_in, 3599L);
ASSERT_TRUE(rejects([] { EntraTokenSource::parse(200, R"({"token_type":"Bearer","expires_in":3599})"); }, "access_token"));
ASSERT_TRUE(rejects([] { EntraTokenSource::parse(200, R"({"access_token":"x","expires_in":0})"); }, "expires_in"));
ASSERT_TRUE(rejects([] { EntraTokenSource::parse(200, R"({"access_token":"x","expires_in":999999999})"); }, "expires_in"));
ASSERT_TRUE(rejects([] { EntraTokenSource::parse(200, "<html>"); }, "non-JSON"));
ASSERT_EQ(EntraTokenSource::parse(200, R"({"access_token":"x","expires_in":"3600"})").expires_in, 3600L);
}
TEST(azure_entra, parses_the_observed_error_body) {
try {
EntraTokenSource::parse(400, kErr);
FAIL("expected an error");
} catch (const molo::llm::ApiError& e) {
ASSERT_EQ(e.http_status(), 400);
ASSERT_EQ(e.type(), std::string("unauthorized_client"));
ASSERT_TRUE(e.message().rfind("AADSTS700038", 0) == 0);
ASSERT_EQ(e.request_id(), std::string("856e791a-6d26-41ce-a6d4-be68759f9101"));
}
}
TEST(azure_entra, exchange_request_has_the_documented_shape) {
Clock clock;
Rig rig(entra_settings(), clock.fn());
rig.token().answer = kOk;
std::string tok = rig.client->tokens()->token();
ASSERT_TRUE(tok.rfind("eyJ0eXAi", 0) == 0);
const Seen& s = rig.token().seen.back();
ASSERT_EQ(s.method, std::string("POST"));
ASSERT_EQ(s.target, std::string("/contoso.onmicrosoft.com/oauth2/v2.0/token"));
ASSERT_EQ(s.headers.get_or("Content-Type", ""), std::string("application/x-www-form-urlencoded"));
auto form = molo::clients::body::UrlEncodedForm::decode(s.body);
std::map<std::string, std::string> f(form.fields().begin(), form.fields().end());
ASSERT_EQ(f["grant_type"], std::string("client_credentials"));
ASSERT_EQ(f["client_id"], std::string("11111111-2222-3333-4444-555555555555"));
ASSERT_EQ(f["client_secret"], std::string("s3cr3t-value"));
ASSERT_EQ(f["scope"], std::string("https://cognitiveservices.azure.com/.default"));
ASSERT_EQ(f.size(), static_cast<std::size_t>(4));
}
TEST(azure_entra, token_is_cached_and_refreshed_before_expiry) {
Clock clock;
Rig rig(entra_settings(), clock.fn());
rig.token().answer = kOk;
auto src = rig.client->tokens();
src->token();
clock.now += 3000; // 599 s left, above the 300 s skew
src->token();
ASSERT_EQ(src->exchanges(), static_cast<std::size_t>(1));
clock.now += 400; // 199 s left: inside the skew, refresh
src->token();
ASSERT_EQ(src->exchanges(), static_cast<std::size_t>(2));
ASSERT_EQ(src->expires_at(), clock.now + 3599);
}
TEST(azure_entra, failed_exchange_has_a_floor_and_never_leaks_the_secret) {
Clock clock;
Rig rig(entra_settings(), clock.fn());
rig.token().status = 400;
rig.token().answer = kErr;
auto src = rig.client->tokens();
try {
src->token();
FAIL("expected failure");
} catch (const molo::llm::ApiError& e) {
ASSERT_EQ(e.type(), std::string("unauthorized_client"));
ASSERT_TRUE(std::string(e.what()).find("s3cr3t-value") == std::string::npos);
}
// Within the floor: no second network call.
try {
src->token();
FAIL("expected backoff");
} catch (const molo::llm::ApiError& e) {
ASSERT_EQ(e.type(), std::string("TOKEN_BACKOFF"));
ASSERT_EQ(e.http_status(), 503);
ASSERT_TRUE(std::string(e.what()).find("s3cr3t-value") == std::string::npos);
}
ASSERT_EQ(src->exchanges(), static_cast<std::size_t>(1));
clock.now += 5; // floor passed: one more try
rig.token().status = 200;
rig.token().answer = kOk;
ASSERT_FALSE(src->token().empty());
ASSERT_EQ(src->exchanges(), static_cast<std::size_t>(2));
}
TEST(azure_entra, every_surface_sends_the_bearer_token) {
Clock clock;
Rig rig(entra_settings(), clock.fn());
rig.token().answer = kOk;
FilesService f(rig.client);
f.get("file-1");
std::string want = "Bearer " + rig.client->tokens()->token();
ASSERT_EQ(rig.last().headers.get_or("Authorization", ""), want);
ASSERT_FALSE(rig.last().headers.has("api-key"));
SafetyService s(rig.client);
s.list_blocklists();
ASSERT_EQ(rig.safety().seen.back().headers.get_or("Authorization", ""), want);
ASSERT_FALSE(rig.safety().seen.back().headers.has("Ocp-Apim-Subscription-Key"));
ASSERT_EQ(rig.client->tokens()->exchanges(), static_cast<std::size_t>(1)); // one token serves all
ASSERT_TRUE(rig.last().target.find("eyJ") == std::string::npos);
}
TEST(azure_entra, a_failed_exchange_fails_the_api_call_without_sending_it) {
Clock clock;
Rig rig(entra_settings(), clock.fn());
rig.token().status = 400;
rig.token().answer = kErr;
FilesService f(rig.client);
ASSERT_TRUE(rejects([&] { f.get("file-1"); }, "AADSTS700038"));
ASSERT_EQ(rig.openai().seen.size(), static_cast<std::size_t>(0));
}
TEST(azure_entra, key_wins_and_no_exchange_happens_in_key_mode) {
Settings s = entra_settings();
s.api_key = "k";
s.safety_key = "k";
Rig rig(s);
ASSERT_TRUE(rig.client->tokens() == nullptr);
ASSERT_TRUE(rig.client->auth() == AuthKind::kApiKey);
ASSERT_EQ(rig.rec.count(Channel::kToken), static_cast<std::size_t>(0)); // no token transport at all
}
TEST(azure_entra, sovereign_authority_hosts) {
Settings s = entra_settings();
s.entra.authority = "https://login.microsoftonline.us";
Rig rig(s);
rig.token().answer = kOk;
rig.client->tokens()->token();
ASSERT_EQ(rig.token().seen.back().target, std::string("/contoso.onmicrosoft.com/oauth2/v2.0/token"));
ASSERT_EQ(s.entra.token_url(), std::string("https://login.microsoftonline.us/contoso.onmicrosoft.com/oauth2/v2.0/token"));
}