fix(clients): express endpoints are key-only; has_oauth covers service accounts; make google-live

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-24 08:32:55 +02:00
co-authored by Claude Sonnet 5.5
parent 38687de58f
commit 316ddfdb69
10 changed files with 38 additions and 11 deletions
+3 -2
View File
@@ -68,8 +68,9 @@ interrupts it. Optional env: `GOOGLE_VOICE_LANG` (default `nl-NL`),
The Google layer is `src/google` (spec: `src/google/SPEC.md`, plan
`plans/16-agent-platform.md`). No test scripts a Google reply: each module
is derived from Google's Discovery documents and re-checked by
`src/google/<module>/tools/check_discovery.py`; `MOLO_LIVE_NET=1 make
google-test` adds read-only live checks against real Google.
`src/google/<module>/tools/check_discovery.py`; `make google-live`
runs read-only live checks against real Google (a service-account token is
minted for the OAuth-only APIs).
Credentials (environment, then `.env.json.local`, then `.env.json`):
`GOOGLE_API_KEY` (Vertex express keys, voice), `GOOGLE_CLOUD_PROJECT`,
+5
View File
@@ -76,6 +76,11 @@ std::shared_ptr<clients::Auth> Credentials::api_key_from_env(const char* name) {
return api_key(std::move(key));
}
bool Credentials::has_oauth() {
return !env("GOOGLE_ACCESS_TOKEN").empty() || !env("GOOGLE_ACCESS_TOKEN_FILE").empty() ||
!env("GOOGLE_APPLICATION_CREDENTIALS").empty();
}
std::shared_ptr<clients::Auth> Credentials::oauth_from_env() {
std::string token = env("GOOGLE_ACCESS_TOKEN");
if (!token.empty()) return bearer(std::move(token));
+3
View File
@@ -39,6 +39,9 @@ class Credentials {
// (re-read per request), else the service account in
// GOOGLE_APPLICATION_CREDENTIALS (self-refreshing), else nullptr.
static std::shared_ptr<clients::Auth> oauth_from_env();
// True when any OAuth source is configured: GOOGLE_ACCESS_TOKEN,
// GOOGLE_ACCESS_TOKEN_FILE, or GOOGLE_APPLICATION_CREDENTIALS.
static bool has_oauth();
// Legacy single credential: token, else key, else nullptr.
static std::shared_ptr<clients::Auth> from_env(
const char* name = "GOOGLE_API_KEY");
+7 -1
View File
@@ -28,7 +28,7 @@ GOOGLE_OBJS := $(patsubst $(ROOT)/%.cpp,$(GOOGLE_OBJDIR)/%.o,$(GOOGLE_SRCS))
# layout corrupts the heap).
-include $(GOOGLE_OBJS:.o=.d)
.PHONY: google google-test google-clean google-token
.PHONY: google google-test google-clean google-token google-live
# Dev tool: prints a service-account access token (see tools/google_sa_token.cpp).
google-token: build/google/sa-token
@@ -54,5 +54,11 @@ $(GOOGLE_TEST_BIN): $(GOOGLE_OBJS) $(GOOGLE_TEST_SRCS) $(CLIENTS_LIB) $(OPERABIL
@mkdir -p $(dir $@)
$(CXX) $(GOOGLE_CXXFLAGS) $(GOOGLE_INCS) $(GOOGLE_OBJS) $(GOOGLE_TEST_SRCS) $(ROOT)/tests/src/clients/main.cpp $(CLIENTS_LIB) $(OPERABILITY_LIB) -lwolfssl -o $@
# Opt-in live checks against real Google (read-only, a few tokens). Mints a
# service-account access token for the OAuth-only APIs; needs
# GOOGLE_APPLICATION_CREDENTIALS and GOOGLE_CLOUD_PROJECT configured.
google-live: $(GOOGLE_TEST_BIN) build/google/sa-token
GOOGLE_ACCESS_TOKEN=`build/google/sa-token` MOLO_LIVE_NET=1 $(GOOGLE_TEST_BIN)
google-clean:
rm -rf $(GOOGLE_BUILD) $(GOOGLE_TEST_BIN)
+8 -2
View File
@@ -25,12 +25,18 @@ const CloudContext& VertexTarget::context() const {
return *context_;
}
// Express mode (no project) is key-only on the wire: OAuth there is answered
// "Invalid resource field value", so the endpoint says so (prefer_key).
ServiceEndpoint VertexTarget::endpoint() const {
return endpoints::aiplatform(context_ ? context_->location : "global");
ServiceEndpoint e = endpoints::aiplatform(context_ ? context_->location : "global");
e.prefer_key = !context_;
return e;
}
ServiceEndpoint VertexTarget::beta_endpoint() const {
return endpoints::aiplatform_beta(context_ ? context_->location : "global");
ServiceEndpoint e = endpoints::aiplatform_beta(context_ ? context_->location : "global");
e.prefer_key = !context_;
return e;
}
void VertexTarget::require_segment(const std::string& value,
+1 -1
View File
@@ -14,7 +14,7 @@ std::shared_ptr<LanguageModel> make_gemini_model(std::shared_ptr<GoogleClient> c
if (model.empty()) model = Generative::default_model();
CloudContext ctx = CloudContext::from_env();
models::VertexTarget target = models::VertexTarget::express();
if (!Credentials::env("GOOGLE_ACCESS_TOKEN").empty() && ctx.valid()) {
if (Credentials::has_oauth() && ctx.valid()) {
ctx.location = "global";
target = models::VertexTarget::project(std::move(ctx));
}
+1 -1
View File
@@ -887,7 +887,7 @@ TEST(google_live_armor, oauth_token_reads_templates_and_findings) {
TEST(google_live_armor, express_key_is_refused) {
std::string key = Credentials::env("GOOGLE_API_KEY");
if (!std::getenv("MOLO_LIVE_NET") || key.empty() || !Credentials::env("GOOGLE_ACCESS_TOKEN").empty() ||
if (!std::getenv("MOLO_LIVE_NET") || key.empty() || Credentials::has_oauth() ||
Credentials::env("GOOGLE_CLOUD_PROJECT").empty()) {
std::cout << "SKIP google_live_armor.express_key_is_refused (needs MOLO_LIVE_NET=1, "
"GOOGLE_API_KEY, GOOGLE_CLOUD_PROJECT and no GOOGLE_ACCESS_TOKEN)\n";
+7 -1
View File
@@ -75,7 +75,9 @@ bool invalid_without_traffic(Rig& rig, const std::function<void()>& fn) {
class EnvGuard {
public:
EnvGuard() {
for (const char* k : {"GOOGLE_ACCESS_TOKEN", "GOOGLE_CLOUD_PROJECT"}) {
for (const char* k : {"GOOGLE_ACCESS_TOKEN", "GOOGLE_ACCESS_TOKEN_FILE",
"GOOGLE_APPLICATION_CREDENTIALS", "GOOGLE_CLOUD_PROJECT",
"MOLO_ENV_FILES", "MOLO_LIVE_NET"}) {
const char* v = std::getenv(k);
saved_.push_back({k, v != nullptr, v ? v : ""});
}
@@ -126,6 +128,10 @@ TEST(optimize_model, oauth_with_a_project_uses_the_global_project_path) {
TEST(optimize_model, without_a_token_it_is_vertex_express) {
EnvGuard guard;
unsetenv("GOOGLE_ACCESS_TOKEN");
unsetenv("GOOGLE_ACCESS_TOKEN_FILE");
unsetenv("GOOGLE_APPLICATION_CREDENTIALS");
unsetenv("MOLO_LIVE_NET");
setenv("MOLO_ENV_FILES", "off", 1); // no .env.json.local service account
setenv("GOOGLE_CLOUD_PROJECT", "p1", 1);
Rig rig;
auto model = make_gemini_model(rig.client, "m1");
+2 -2
View File
@@ -46,7 +46,7 @@ TEST(optimize_live, evaluate_instances_computation_metrics) {
TEST(optimize_live, resource_lists_need_oauth) {
if (!live("optimize_live.resource_lists_need_oauth", true)) return;
EvaluationResources res(GoogleClient::from_env(), CloudContext::from_env());
if (Credentials::env("GOOGLE_ACCESS_TOKEN").empty()) {
if (!Credentials::has_oauth()) {
// Verified 2026-10-24: API keys are refused with 401 UNAUTHENTICATED.
try {
res.list(Collection::kEvaluationRuns, ListOptions{1, "", "", 1});
@@ -68,7 +68,7 @@ TEST(optimize_live, resource_lists_need_oauth) {
TEST(optimize_live, observability_reads_with_oauth) {
if (!live("optimize_live.observability_reads_with_oauth", true)) return;
if (Credentials::env("GOOGLE_ACCESS_TOKEN").empty()) {
if (!Credentials::has_oauth()) {
std::cout << "SKIP optimize_live.observability_reads_with_oauth (needs GOOGLE_ACCESS_TOKEN)\n";
return;
}
@@ -34,7 +34,7 @@ bool live_ready(const char* test) {
}
return true;
}
bool have_oauth() { return !Credentials::env("GOOGLE_ACCESS_TOKEN").empty(); }
bool have_oauth() { return Credentials::has_oauth(); }
// With OAuth the call must work, or answer 403 SERVICE_DISABLED (the API is
// not enabled on the project: the path and auth were accepted, enabling an