#include "google/credentials.h" #include #include "google/errors.h" #include "google/service_account.h" #include #include #include #include "clients/body/json.h" namespace molo::google { FileTokenAuth::FileTokenAuth(std::string path) : path_(std::move(path)) {} void FileTokenAuth::apply(clients::ClientRequest& request) const { std::ifstream in(path_, std::ios::binary); std::string token((std::istreambuf_iterator(in)), std::istreambuf_iterator()); if (token.size() > 8 * 1024) token.clear(); while (!token.empty() && (token.back() == '\n' || token.back() == '\r' || token.back() == ' ')) token.pop_back(); if (token.empty()) throw GoogleError(401, "UNAUTHENTICATED", "access token file is missing, empty or over the size cap"); request.bearer(token); } std::shared_ptr Credentials::api_key(std::string key) { return std::make_shared(std::move(key), "x-goog-api-key"); } std::shared_ptr Credentials::bearer(std::string token) { return std::make_shared(std::move(token)); } namespace { constexpr std::size_t kMaxConfigBytes = 64 * 1024; // String value of `name` in a flat JSON object file, or empty (missing, // oversized, malformed, or not a string: all treated as "not set"). std::string from_json_file(const char* path, const char* name) { std::ifstream in(path, std::ios::binary); if (!in) return ""; std::ostringstream text; text << in.rdbuf(); if (text.str().size() > kMaxConfigBytes) return ""; try { clients::body::Json doc = clients::body::Json::parse(text.str()); const clients::body::Json* v = doc.is_object() ? doc.find(name) : nullptr; return v && v->is_string() ? v->as_string() : ""; } catch (const std::exception&) { return ""; } } } // namespace // Order: process environment, then .env.json.local (git-ignored, for // secrets), then .env.json, both in the working directory. std::string Credentials::env(const char* name) { if (const char* v = std::getenv(name); v && *v) return v; // Test runs set MOLO_ENV_FILES=off so a developer's .env.json.local never // leaks into hermetic tests; opt-in live runs (MOLO_LIVE_NET) need it. const char* files = std::getenv("MOLO_ENV_FILES"); if (files && std::string(files) == "off" && !std::getenv("MOLO_LIVE_NET")) return ""; std::string local = from_json_file(".env.json.local", name); if (!local.empty()) return local; return from_json_file(".env.json", name); } std::shared_ptr Credentials::api_key_from_env(const char* name) { std::string key = env(name); if (key.empty()) return nullptr; 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)); std::string file = env("GOOGLE_ACCESS_TOKEN_FILE"); if (!file.empty()) return std::make_shared(std::move(file)); return ServiceAccountAuth::from_env(); } std::shared_ptr Credentials::from_env(const char* name) { // An OAuth access token (GOOGLE_ACCESS_TOKEN, e.g. from // `gcloud auth print-access-token`) wins: Agent Platform APIs need it. std::string token = env("GOOGLE_ACCESS_TOKEN"); if (!token.empty()) return bearer(std::move(token)); std::string key = env(name); if (key.empty()) return nullptr; return api_key(std::move(key)); } } // namespace molo::google