diff --git a/README.md b/README.md index 58605ac..ee3ab0c 100644 --- a/README.md +++ b/README.md @@ -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//tools/check_discovery.py`; `MOLO_LIVE_NET=1 make -google-test` adds read-only live checks against real Google. +`src/google//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`, diff --git a/src/google/credentials.cpp b/src/google/credentials.cpp index 8be9ae3..7373aec 100644 --- a/src/google/credentials.cpp +++ b/src/google/credentials.cpp @@ -76,6 +76,11 @@ std::shared_ptr 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 Credentials::oauth_from_env() { std::string token = env("GOOGLE_ACCESS_TOKEN"); if (!token.empty()) return bearer(std::move(token)); diff --git a/src/google/credentials.h b/src/google/credentials.h index b6d62b7..51d031b 100644 --- a/src/google/credentials.h +++ b/src/google/credentials.h @@ -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 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 from_env( const char* name = "GOOGLE_API_KEY"); diff --git a/src/google/google.mk b/src/google/google.mk index c74e4e3..d31714b 100644 --- a/src/google/google.mk +++ b/src/google/google.mk @@ -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) diff --git a/src/google/models/target.cpp b/src/google/models/target.cpp index cfe8120..6ca7056 100644 --- a/src/google/models/target.cpp +++ b/src/google/models/target.cpp @@ -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, diff --git a/src/google/optimize/model_factory.cpp b/src/google/optimize/model_factory.cpp index 645aa65..9e5fb30 100644 --- a/src/google/optimize/model_factory.cpp +++ b/src/google/optimize/model_factory.cpp @@ -14,7 +14,7 @@ std::shared_ptr make_gemini_model(std::shared_ptr 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)); } diff --git a/tests/src/google/armor/test_armor.cpp b/tests/src/google/armor/test_armor.cpp index e9b4a3e..032777b 100644 --- a/tests/src/google/armor/test_armor.cpp +++ b/tests/src/google/armor/test_armor.cpp @@ -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"; diff --git a/tests/src/google/optimize/test_gaps.cpp b/tests/src/google/optimize/test_gaps.cpp index 14a63e4..5b468a9 100644 --- a/tests/src/google/optimize/test_gaps.cpp +++ b/tests/src/google/optimize/test_gaps.cpp @@ -75,7 +75,9 @@ bool invalid_without_traffic(Rig& rig, const std::function& 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"); diff --git a/tests/src/google/optimize/test_live.cpp b/tests/src/google/optimize/test_live.cpp index 851b2cc..b790da2 100644 --- a/tests/src/google/optimize/test_live.cpp +++ b/tests/src/google/optimize/test_live.cpp @@ -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; } diff --git a/tests/src/google/registry/test_registry_live.cpp b/tests/src/google/registry/test_registry_live.cpp index a5dc100..a031139 100644 --- a/tests/src/google/registry/test_registry_live.cpp +++ b/tests/src/google/registry/test_registry_live.cpp @@ -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