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
+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));
}