feat(clients): google service-account OAuth and per-endpoint credential choice
ServiceAccountAuth signs an RS256 JWT with wolfCrypt from the key file in GOOGLE_APPLICATION_CREDENTIALS and exchanges it for a self-refreshing bearer token. GoogleClient holds a key and an OAuth credential and uses OAuth everywhere except express endpoints, which only accept the key. Signing is tested with generated RSA keys and public-key verification. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,3 +2,5 @@ build/
|
|||||||
*.o
|
*.o
|
||||||
*.a
|
*.a
|
||||||
.env.json.local
|
.env.json.local
|
||||||
|
.secrets/
|
||||||
|
.claude/worktrees/
|
||||||
|
|||||||
+9
-2
@@ -17,6 +17,7 @@ client, credentials, errors, or caps.
|
|||||||
| `caps.h` | Static ceilings, never autotuned |
|
| `caps.h` | Static ceilings, never autotuned |
|
||||||
| `errors.h/.cpp` | `GoogleError` (http status, Google status, message), `NotConfiguredError`, parser for Google's `{"error":{...}}` body |
|
| `errors.h/.cpp` | `GoogleError` (http status, Google status, message), `NotConfiguredError`, parser for Google's `{"error":{...}}` body |
|
||||||
| `credentials.h/.cpp` | `Credentials`: API key and OAuth bearer providers, env loader |
|
| `credentials.h/.cpp` | `Credentials`: API key and OAuth bearer providers, env loader |
|
||||||
|
| `service_account.h/.cpp` | `ServiceAccountKey`, `JwtSigner` (RS256), `ServiceAccountAuth` (self-refreshing bearer) |
|
||||||
| `endpoints.h/.cpp` | `ServiceEndpoint` plus the catalog of known services |
|
| `endpoints.h/.cpp` | `ServiceEndpoint` plus the catalog of known services |
|
||||||
| `client.h/.cpp` | `GoogleClient`: auth, JSON in/out, error mapping, over any `clients::Transport` |
|
| `client.h/.cpp` | `GoogleClient`: auth, JSON in/out, error mapping, over any `clients::Transport` |
|
||||||
| `service.h/.cpp` | `Service` base class: holds client plus endpoint, one `call()` |
|
| `service.h/.cpp` | `Service` base class: holds client plus endpoint, one `call()` |
|
||||||
@@ -44,6 +45,10 @@ client, credentials, errors, or caps.
|
|||||||
|
|
||||||
## 3. Contracts
|
## 3. Contracts
|
||||||
|
|
||||||
|
- Two credentials per client: an API key and an OAuth credential (bearer
|
||||||
|
token or service account via `GOOGLE_APPLICATION_CREDENTIALS`). OAuth is
|
||||||
|
used for every endpoint except express ones (`ServiceEndpoint::prefer_key`,
|
||||||
|
the Vertex express voice path), which only accept the key.
|
||||||
- Credentials: `GOOGLE_API_KEY` is read once by `GoogleClient::from_env()`
|
- Credentials: `GOOGLE_API_KEY` is read once by `GoogleClient::from_env()`
|
||||||
from the process environment, else `.env.json.local` (git-ignored, put
|
from the process environment, else `.env.json.local` (git-ignored, put
|
||||||
secrets here), else `.env.json`, in the working directory. All env knobs
|
secrets here), else `.env.json`, in the working directory. All env knobs
|
||||||
@@ -84,8 +89,10 @@ client, credentials, errors, or caps.
|
|||||||
|
|
||||||
- Streaming recognition (gRPC/WebSocket) and the Gemini Live API are not
|
- Streaming recognition (gRPC/WebSocket) and the Gemini Live API are not
|
||||||
implemented; turns are request/response over REST.
|
implemented; turns are request/response over REST.
|
||||||
- Service-account JWT exchange for OAuth tokens: callers pass a ready token
|
- Service-account OAuth is implemented (`service_account.h`: RS256 JWT via
|
||||||
to `Credentials::bearer`. Owner: this part, `credentials.cpp`.
|
wolfCrypt, token exchange, refresh 60 s before expiry) and its signing is
|
||||||
|
tested with real RSA keys. The token exchange against Google itself is
|
||||||
|
unverified until a key file is configured (`GOOGLE_APPLICATION_CREDENTIALS`).
|
||||||
- Free-tier quota accounting is Google's; `RESOURCE_EXHAUSTED` is surfaced,
|
- Free-tier quota accounting is Google's; `RESOURCE_EXHAUSTED` is surfaced,
|
||||||
not tracked.
|
not tracked.
|
||||||
- Live behavior against Google needs a real `GOOGLE_API_KEY`; the suite
|
- Live behavior against Google needs a real `GOOGLE_API_KEY`; the suite
|
||||||
|
|||||||
+18
-3
@@ -17,13 +17,26 @@ clients::ClientConfig make_config(std::shared_ptr<clients::Auth> auth) {
|
|||||||
|
|
||||||
GoogleClient::GoogleClient(std::unique_ptr<clients::Transport> transport,
|
GoogleClient::GoogleClient(std::unique_ptr<clients::Transport> transport,
|
||||||
std::shared_ptr<clients::Auth> auth)
|
std::shared_ptr<clients::Auth> auth)
|
||||||
: client_(std::move(transport), make_config(auth)),
|
: GoogleClient(std::move(transport), std::move(auth), nullptr) {}
|
||||||
configured_(auth != nullptr) {}
|
|
||||||
|
GoogleClient::GoogleClient(std::unique_ptr<clients::Transport> transport,
|
||||||
|
std::shared_ptr<clients::Auth> key,
|
||||||
|
std::shared_ptr<clients::Auth> oauth)
|
||||||
|
: client_(std::move(transport), make_config(nullptr)),
|
||||||
|
key_auth_(std::move(key)),
|
||||||
|
oauth_auth_(std::move(oauth)),
|
||||||
|
configured_(key_auth_ != nullptr || oauth_auth_ != nullptr) {}
|
||||||
|
|
||||||
|
std::shared_ptr<clients::Auth> GoogleClient::pick_auth(
|
||||||
|
const ServiceEndpoint& service) const {
|
||||||
|
if (oauth_auth_ && !service.prefer_key) return oauth_auth_;
|
||||||
|
return key_auth_ ? key_auth_ : oauth_auth_;
|
||||||
|
}
|
||||||
|
|
||||||
std::shared_ptr<GoogleClient> GoogleClient::from_env() {
|
std::shared_ptr<GoogleClient> GoogleClient::from_env() {
|
||||||
return std::make_shared<GoogleClient>(
|
return std::make_shared<GoogleClient>(
|
||||||
std::make_unique<clients::transports::HttpTransport>(),
|
std::make_unique<clients::transports::HttpTransport>(),
|
||||||
Credentials::from_env());
|
Credentials::api_key_from_env(), Credentials::oauth_from_env());
|
||||||
}
|
}
|
||||||
|
|
||||||
clients::body::Json GoogleClient::request(clients::Method method,
|
clients::body::Json GoogleClient::request(clients::Method method,
|
||||||
@@ -35,6 +48,7 @@ clients::body::Json GoogleClient::request(clients::Method method,
|
|||||||
clients::ClientRequest req(method,
|
clients::ClientRequest req(method,
|
||||||
clients::Url::parse(service.base_url + path));
|
clients::Url::parse(service.base_url + path));
|
||||||
if (body) req.json(*body);
|
if (body) req.json(*body);
|
||||||
|
pick_auth(service)->apply(req);
|
||||||
return finish(service, client_.send_blocking(std::move(req)));
|
return finish(service, client_.send_blocking(std::move(req)));
|
||||||
} catch (const GoogleError&) {
|
} catch (const GoogleError&) {
|
||||||
throw;
|
throw;
|
||||||
@@ -75,6 +89,7 @@ void GoogleClient::stream_json(const ServiceEndpoint& service,
|
|||||||
clients::ClientRequest req(
|
clients::ClientRequest req(
|
||||||
clients::Method::kPost, clients::Url::parse(service.base_url + path));
|
clients::Method::kPost, clients::Url::parse(service.base_url + path));
|
||||||
req.json(body);
|
req.json(body);
|
||||||
|
pick_auth(service)->apply(req);
|
||||||
client_.send_stream_blocking(
|
client_.send_stream_blocking(
|
||||||
std::move(req),
|
std::move(req),
|
||||||
[&](const std::string& chunk) { return decoder.feed(chunk); });
|
[&](const std::string& chunk) { return decoder.feed(chunk); });
|
||||||
|
|||||||
@@ -21,6 +21,12 @@ class GoogleClient {
|
|||||||
// throws NotConfiguredError before touching the network.
|
// throws NotConfiguredError before touching the network.
|
||||||
GoogleClient(std::unique_ptr<clients::Transport> transport,
|
GoogleClient(std::unique_ptr<clients::Transport> transport,
|
||||||
std::shared_ptr<clients::Auth> auth);
|
std::shared_ptr<clients::Auth> auth);
|
||||||
|
// Two credentials: `key` (API key) and `oauth` (bearer or service
|
||||||
|
// account). OAuth is used for every endpoint except express ones
|
||||||
|
// (ServiceEndpoint::prefer_key); either may be null.
|
||||||
|
GoogleClient(std::unique_ptr<clients::Transport> transport,
|
||||||
|
std::shared_ptr<clients::Auth> key,
|
||||||
|
std::shared_ptr<clients::Auth> oauth);
|
||||||
|
|
||||||
// Real HTTPS transport plus GOOGLE_API_KEY from the environment.
|
// Real HTTPS transport plus GOOGLE_API_KEY from the environment.
|
||||||
static std::shared_ptr<GoogleClient> from_env();
|
static std::shared_ptr<GoogleClient> from_env();
|
||||||
@@ -57,7 +63,11 @@ class GoogleClient {
|
|||||||
clients::body::Json finish(const ServiceEndpoint& service,
|
clients::body::Json finish(const ServiceEndpoint& service,
|
||||||
const clients::ClientResponse& response);
|
const clients::ClientResponse& response);
|
||||||
|
|
||||||
|
std::shared_ptr<clients::Auth> pick_auth(const ServiceEndpoint& service) const;
|
||||||
|
|
||||||
clients::Client client_;
|
clients::Client client_;
|
||||||
|
std::shared_ptr<clients::Auth> key_auth_;
|
||||||
|
std::shared_ptr<clients::Auth> oauth_auth_;
|
||||||
bool configured_;
|
bool configured_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#include "google/credentials.h"
|
#include "google/credentials.h"
|
||||||
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
|
#include "google/service_account.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
@@ -46,6 +48,18 @@ std::string Credentials::env(const char* name) {
|
|||||||
return from_json_file(".env.json", name);
|
return from_json_file(".env.json", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<clients::Auth> Credentials::api_key_from_env(const char* name) {
|
||||||
|
std::string key = env(name);
|
||||||
|
if (key.empty()) return nullptr;
|
||||||
|
return api_key(std::move(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<clients::Auth> Credentials::oauth_from_env() {
|
||||||
|
std::string token = env("GOOGLE_ACCESS_TOKEN");
|
||||||
|
if (!token.empty()) return bearer(std::move(token));
|
||||||
|
return ServiceAccountAuth::from_env();
|
||||||
|
}
|
||||||
|
|
||||||
std::shared_ptr<clients::Auth> Credentials::from_env(const char* name) {
|
std::shared_ptr<clients::Auth> Credentials::from_env(const char* name) {
|
||||||
// An OAuth access token (GOOGLE_ACCESS_TOKEN, e.g. from
|
// An OAuth access token (GOOGLE_ACCESS_TOKEN, e.g. from
|
||||||
// `gcloud auth print-access-token`) wins: Agent Platform APIs need it.
|
// `gcloud auth print-access-token`) wins: Agent Platform APIs need it.
|
||||||
|
|||||||
@@ -19,7 +19,13 @@ class Credentials {
|
|||||||
// Value of `name` from the process environment, else .env.json.local,
|
// Value of `name` from the process environment, else .env.json.local,
|
||||||
// else .env.json (working directory); empty when unset everywhere.
|
// else .env.json (working directory); empty when unset everywhere.
|
||||||
static std::string env(const char* name = "GOOGLE_API_KEY");
|
static std::string env(const char* name = "GOOGLE_API_KEY");
|
||||||
// api_key(env()) or nullptr when unset or empty.
|
// API key only (GOOGLE_API_KEY), or nullptr.
|
||||||
|
static std::shared_ptr<clients::Auth> api_key_from_env(
|
||||||
|
const char* name = "GOOGLE_API_KEY");
|
||||||
|
// OAuth only: GOOGLE_ACCESS_TOKEN as a bearer, else the service account in
|
||||||
|
// GOOGLE_APPLICATION_CREDENTIALS (self-refreshing), else nullptr.
|
||||||
|
static std::shared_ptr<clients::Auth> oauth_from_env();
|
||||||
|
// Legacy single credential: token, else key, else nullptr.
|
||||||
static std::shared_ptr<clients::Auth> from_env(
|
static std::shared_ptr<clients::Auth> from_env(
|
||||||
const char* name = "GOOGLE_API_KEY");
|
const char* name = "GOOGLE_API_KEY");
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ ServiceEndpoint generative() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ServiceEndpoint vertex_generative() {
|
ServiceEndpoint vertex_generative() {
|
||||||
return {"vertex", "https://aiplatform.googleapis.com/v1/publishers/google"};
|
return {"vertex", "https://aiplatform.googleapis.com/v1/publishers/google", true};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<ServiceEndpoint> catalog() {
|
std::vector<ServiceEndpoint> catalog() {
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ namespace molo::google {
|
|||||||
struct ServiceEndpoint {
|
struct ServiceEndpoint {
|
||||||
std::string name;
|
std::string name;
|
||||||
std::string base_url; // No trailing slash.
|
std::string base_url; // No trailing slash.
|
||||||
|
// Express-mode endpoints accept only an API key: never send OAuth there.
|
||||||
|
bool prefer_key = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace endpoints {
|
namespace endpoints {
|
||||||
|
|||||||
@@ -0,0 +1,197 @@
|
|||||||
|
#include "google/service_account.h"
|
||||||
|
|
||||||
|
#include <wolfssl/options.h>
|
||||||
|
#include <wolfssl/wolfcrypt/asn.h>
|
||||||
|
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||||
|
#include <wolfssl/wolfcrypt/random.h>
|
||||||
|
#include <wolfssl/wolfcrypt/rsa.h>
|
||||||
|
#include <wolfssl/wolfcrypt/signature.h>
|
||||||
|
|
||||||
|
#include <ctime>
|
||||||
|
#include <fstream>
|
||||||
|
#include <memory>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#include "clients/body/json.h"
|
||||||
|
#include "clients/client.h"
|
||||||
|
#include "clients/transports/http_transport.h"
|
||||||
|
#include "google/credentials.h"
|
||||||
|
#include "google/errors.h"
|
||||||
|
#include "security/crypto/Base64.h"
|
||||||
|
|
||||||
|
namespace molo::google {
|
||||||
|
|
||||||
|
using clients::body::Json;
|
||||||
|
using security::crypto::Base64;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
constexpr std::size_t kMaxKeyFileBytes = 64 * 1024;
|
||||||
|
constexpr auto kRefreshMargin = std::chrono::seconds(60);
|
||||||
|
|
||||||
|
std::string str(const Json& doc, const char* key) {
|
||||||
|
const Json* v = doc.is_object() ? doc.find(key) : nullptr;
|
||||||
|
return v && v->is_string() ? v->as_string() : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tokens may only be requested from a Google host over https: the key file
|
||||||
|
// is trusted, but a typo or a swapped file must not send a signed assertion
|
||||||
|
// to an arbitrary server.
|
||||||
|
bool google_https(const std::string& uri) {
|
||||||
|
const std::string scheme = "https://";
|
||||||
|
if (uri.rfind(scheme, 0) != 0) return false;
|
||||||
|
std::string host = uri.substr(scheme.size(), uri.find('/', scheme.size()) - scheme.size());
|
||||||
|
const std::string suffix = ".googleapis.com";
|
||||||
|
return host.size() > suffix.size() &&
|
||||||
|
host.compare(host.size() - suffix.size(), suffix.size(), suffix) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string pem_to_der(const std::string& pem) {
|
||||||
|
std::string body;
|
||||||
|
std::istringstream in(pem);
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(in, line)) {
|
||||||
|
if (!line.empty() && line.back() == '\r') line.pop_back();
|
||||||
|
if (line.rfind("-----", 0) == 0 || line.empty()) continue;
|
||||||
|
body += line;
|
||||||
|
}
|
||||||
|
std::string der;
|
||||||
|
if (!Base64::decode(body, der) || der.empty())
|
||||||
|
throw GoogleError::invalid("private_key is not valid PEM");
|
||||||
|
return der;
|
||||||
|
}
|
||||||
|
|
||||||
|
class RsaKeyGuard {
|
||||||
|
public:
|
||||||
|
RsaKeyGuard() { wc_InitRsaKey(&key, nullptr); }
|
||||||
|
~RsaKeyGuard() { wc_FreeRsaKey(&key); }
|
||||||
|
RsaKeyGuard(const RsaKeyGuard&) = delete;
|
||||||
|
RsaKeyGuard& operator=(const RsaKeyGuard&) = delete;
|
||||||
|
RsaKey key;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
ServiceAccountKey ServiceAccountKey::parse(const std::string& json_text) {
|
||||||
|
Json doc;
|
||||||
|
try {
|
||||||
|
doc = Json::parse(json_text);
|
||||||
|
} catch (const std::exception&) {
|
||||||
|
throw GoogleError::invalid("service account key is not valid JSON");
|
||||||
|
}
|
||||||
|
ServiceAccountKey key;
|
||||||
|
if (str(doc, "type") != "service_account")
|
||||||
|
throw GoogleError::invalid("key file type is not service_account");
|
||||||
|
key.client_email = str(doc, "client_email");
|
||||||
|
key.private_key_pem = str(doc, "private_key");
|
||||||
|
std::string uri = str(doc, "token_uri");
|
||||||
|
if (!uri.empty()) key.token_uri = uri;
|
||||||
|
key.project_id = str(doc, "project_id");
|
||||||
|
if (key.client_email.empty() || key.private_key_pem.empty())
|
||||||
|
throw GoogleError::invalid("key file lacks client_email or private_key");
|
||||||
|
if (!google_https(key.token_uri))
|
||||||
|
throw GoogleError::invalid("token_uri must be https on googleapis.com");
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
ServiceAccountKey ServiceAccountKey::load_file(const std::string& path) {
|
||||||
|
std::ifstream in(path, std::ios::binary);
|
||||||
|
if (!in) throw GoogleError::invalid("cannot read service account key file");
|
||||||
|
std::ostringstream text;
|
||||||
|
text << in.rdbuf();
|
||||||
|
if (text.str().size() > kMaxKeyFileBytes)
|
||||||
|
throw GoogleError::invalid("service account key file over the size cap");
|
||||||
|
return parse(text.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
JwtSigner::JwtSigner(const ServiceAccountKey& key) : key_(key) {}
|
||||||
|
|
||||||
|
std::string JwtSigner::sign(const std::string& scope, std::int64_t iat,
|
||||||
|
std::chrono::seconds lifetime) const {
|
||||||
|
Json header = Json::object();
|
||||||
|
header.set("alg", Json("RS256"));
|
||||||
|
header.set("typ", Json("JWT"));
|
||||||
|
Json claims = Json::object();
|
||||||
|
claims.set("iss", Json(key_.client_email));
|
||||||
|
claims.set("scope", Json(scope));
|
||||||
|
claims.set("aud", Json(key_.token_uri));
|
||||||
|
claims.set("iat", Json(static_cast<double>(iat)));
|
||||||
|
claims.set("exp", Json(static_cast<double>(iat + lifetime.count())));
|
||||||
|
std::string signing_input = Base64::encode_url(header.dump()) + "." +
|
||||||
|
Base64::encode_url(claims.dump());
|
||||||
|
|
||||||
|
std::string der = pem_to_der(key_.private_key_pem);
|
||||||
|
RsaKeyGuard rsa;
|
||||||
|
word32 idx = 0;
|
||||||
|
int off = wc_GetPkcs8TraditionalOffset(
|
||||||
|
reinterpret_cast<byte*>(der.data()), &idx, static_cast<word32>(der.size()));
|
||||||
|
if (off < 0) idx = 0; // Already traditional PKCS#1.
|
||||||
|
if (wc_RsaPrivateKeyDecode(reinterpret_cast<const byte*>(der.data()), &idx,
|
||||||
|
&rsa.key, static_cast<word32>(der.size())) != 0)
|
||||||
|
throw GoogleError::invalid("private_key is not a usable RSA key");
|
||||||
|
|
||||||
|
WC_RNG rng;
|
||||||
|
if (wc_InitRng(&rng) != 0) throw GoogleError(500, "INTERNAL", "rng init failed");
|
||||||
|
word32 sig_len = static_cast<word32>(
|
||||||
|
wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, &rsa.key, sizeof(rsa.key)));
|
||||||
|
std::string sig(sig_len, '\0');
|
||||||
|
int rc = wc_SignatureGenerate(
|
||||||
|
WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
|
||||||
|
reinterpret_cast<const byte*>(signing_input.data()),
|
||||||
|
static_cast<word32>(signing_input.size()), reinterpret_cast<byte*>(sig.data()),
|
||||||
|
&sig_len, &rsa.key, sizeof(rsa.key), &rng);
|
||||||
|
wc_FreeRng(&rng);
|
||||||
|
if (rc != 0) throw GoogleError(500, "INTERNAL", "JWT signing failed");
|
||||||
|
sig.resize(sig_len);
|
||||||
|
return signing_input + "." + Base64::encode_url(sig);
|
||||||
|
}
|
||||||
|
|
||||||
|
ServiceAccountAuth::ServiceAccountAuth(ServiceAccountKey key, std::string scope)
|
||||||
|
: key_(std::move(key)), scope_(std::move(scope)), signer_(key_) {}
|
||||||
|
|
||||||
|
ServiceAccountAuth::~ServiceAccountAuth() = default;
|
||||||
|
|
||||||
|
std::string ServiceAccountAuth::token() const {
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
auto now = std::chrono::steady_clock::now();
|
||||||
|
if (!token_.empty() && now + kRefreshMargin < expires_) return token_;
|
||||||
|
|
||||||
|
std::string assertion =
|
||||||
|
signer_.sign(scope_, static_cast<std::int64_t>(std::time(nullptr)));
|
||||||
|
clients::Client http(std::make_unique<clients::transports::HttpTransport>());
|
||||||
|
clients::ClientRequest req = clients::ClientRequest::post(key_.token_uri);
|
||||||
|
req.form("grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer"
|
||||||
|
"&assertion=" + assertion);
|
||||||
|
clients::ClientResponse resp = http.send_blocking(std::move(req));
|
||||||
|
Json doc;
|
||||||
|
try {
|
||||||
|
doc = resp.json();
|
||||||
|
} catch (const std::exception&) {
|
||||||
|
throw GoogleError(502, "BAD_RESPONSE", "token endpoint returned non-JSON", "oauth2");
|
||||||
|
}
|
||||||
|
if (!resp.is_success()) {
|
||||||
|
std::string msg = str(doc, "error_description");
|
||||||
|
throw GoogleError(resp.status() == 400 ? 401 : resp.status(),
|
||||||
|
str(doc, "error").empty() ? "UNAUTHENTICATED" : str(doc, "error"),
|
||||||
|
msg.empty() ? "token exchange failed" : msg, "oauth2");
|
||||||
|
}
|
||||||
|
token_ = str(doc, "access_token");
|
||||||
|
const Json* ttl = doc.find("expires_in");
|
||||||
|
long seconds = ttl && ttl->is_number() ? ttl->as_int() : 3600;
|
||||||
|
if (token_.empty())
|
||||||
|
throw GoogleError(502, "BAD_RESPONSE", "token endpoint returned no access_token", "oauth2");
|
||||||
|
expires_ = now + std::chrono::seconds(seconds);
|
||||||
|
return token_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServiceAccountAuth::apply(clients::ClientRequest& request) const {
|
||||||
|
request.bearer(token());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<ServiceAccountAuth> ServiceAccountAuth::from_env() {
|
||||||
|
std::string path = Credentials::env("GOOGLE_APPLICATION_CREDENTIALS");
|
||||||
|
if (path.empty()) return nullptr;
|
||||||
|
return std::make_shared<ServiceAccountAuth>(ServiceAccountKey::load_file(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace molo::google
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// Service-account credentials (OAuth2 JWT bearer flow, RFC 7523). Reads the
|
||||||
|
// JSON key file Google issues (client_email, private_key, token_uri), signs a
|
||||||
|
// short-lived RS256 JWT with wolfCrypt, exchanges it for an access token and
|
||||||
|
// keeps the token fresh. Needed by every Google API that refuses API keys.
|
||||||
|
//
|
||||||
|
// Env: GOOGLE_APPLICATION_CREDENTIALS = path to the key file (looked up like
|
||||||
|
// every other knob: environment, .env.json.local, .env.json).
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "clients/auth.h"
|
||||||
|
|
||||||
|
namespace molo::google {
|
||||||
|
|
||||||
|
struct ServiceAccountKey {
|
||||||
|
std::string client_email;
|
||||||
|
std::string private_key_pem;
|
||||||
|
std::string token_uri = "https://oauth2.googleapis.com/token";
|
||||||
|
std::string project_id; // Informational; from the key file when present.
|
||||||
|
|
||||||
|
// Throws GoogleError (invalid) when the file is missing, over the cap,
|
||||||
|
// malformed, lacks fields, or names a non-Google token endpoint.
|
||||||
|
static ServiceAccountKey load_file(const std::string& path);
|
||||||
|
static ServiceAccountKey parse(const std::string& json_text);
|
||||||
|
};
|
||||||
|
|
||||||
|
// RS256 JWT builder. Pure computation, no network.
|
||||||
|
class JwtSigner {
|
||||||
|
public:
|
||||||
|
explicit JwtSigner(const ServiceAccountKey& key);
|
||||||
|
// header.claims.signature, base64url. exp = iat + lifetime.
|
||||||
|
std::string sign(const std::string& scope, std::int64_t iat,
|
||||||
|
std::chrono::seconds lifetime = std::chrono::seconds(3600)) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
ServiceAccountKey key_;
|
||||||
|
};
|
||||||
|
|
||||||
|
// clients::Auth that always carries a valid bearer token; refreshes one
|
||||||
|
// minute before expiry. Thread safe. A failed exchange throws GoogleError.
|
||||||
|
class ServiceAccountAuth : public clients::Auth {
|
||||||
|
public:
|
||||||
|
static constexpr const char* kDefaultScope =
|
||||||
|
"https://www.googleapis.com/auth/cloud-platform";
|
||||||
|
|
||||||
|
explicit ServiceAccountAuth(ServiceAccountKey key,
|
||||||
|
std::string scope = kDefaultScope);
|
||||||
|
~ServiceAccountAuth() override;
|
||||||
|
|
||||||
|
void apply(clients::ClientRequest& request) const override;
|
||||||
|
const char* scheme() const override { return "service-account"; }
|
||||||
|
const ServiceAccountKey& key() const { return key_; }
|
||||||
|
|
||||||
|
// Env helper: loads GOOGLE_APPLICATION_CREDENTIALS; null when unset.
|
||||||
|
static std::shared_ptr<ServiceAccountAuth> from_env();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string token() const;
|
||||||
|
|
||||||
|
ServiceAccountKey key_;
|
||||||
|
std::string scope_;
|
||||||
|
JwtSigner signer_;
|
||||||
|
mutable std::mutex mutex_;
|
||||||
|
mutable std::string token_;
|
||||||
|
mutable std::chrono::steady_clock::time_point expires_{};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace molo::google
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
// Service-account credentials: real RSA crypto (generate, sign, verify), key
|
||||||
|
// file validation, and per-endpoint credential choice.
|
||||||
|
#include <wolfssl/options.h>
|
||||||
|
#include <wolfssl/wolfcrypt/asn_public.h>
|
||||||
|
#include <wolfssl/wolfcrypt/random.h>
|
||||||
|
#include <wolfssl/wolfcrypt/rsa.h>
|
||||||
|
#include <wolfssl/wolfcrypt/signature.h>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "framework.h"
|
||||||
|
#include "clients/transports/mock_transport.h"
|
||||||
|
#include "google/client.h"
|
||||||
|
#include "google/credentials.h"
|
||||||
|
#include "google/resource.h"
|
||||||
|
#include "google/service_account.h"
|
||||||
|
#include "security/crypto/Base64.h"
|
||||||
|
|
||||||
|
using namespace molo::google;
|
||||||
|
using molo::clients::ClientResponse;
|
||||||
|
using molo::clients::HeaderMap;
|
||||||
|
using molo::clients::Method;
|
||||||
|
using molo::clients::body::Json;
|
||||||
|
using molo::clients::transports::MockTransport;
|
||||||
|
using molo::security::crypto::Base64;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
// Generates a throwaway 2048-bit key; returns PEM (PKCS#1) and keeps the key.
|
||||||
|
std::string make_pem(RsaKey& key, WC_RNG& rng) {
|
||||||
|
wc_InitRng(&rng);
|
||||||
|
wc_InitRsaKey(&key, nullptr);
|
||||||
|
if (wc_MakeRsaKey(&key, 2048, 65537, &rng) != 0) FAIL("key generation failed");
|
||||||
|
std::string der(4096, '\0');
|
||||||
|
int n = wc_RsaKeyToDer(&key, reinterpret_cast<byte*>(der.data()), der.size());
|
||||||
|
if (n <= 0) FAIL("key export failed");
|
||||||
|
der.resize(n);
|
||||||
|
std::string pem(8192, '\0');
|
||||||
|
int p = wc_DerToPem(reinterpret_cast<const byte*>(der.data()), der.size(),
|
||||||
|
reinterpret_cast<byte*>(pem.data()), pem.size(), PRIVATEKEY_TYPE);
|
||||||
|
if (p <= 0) FAIL("pem export failed");
|
||||||
|
pem.resize(p);
|
||||||
|
return pem;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string key_json(const std::string& pem, const std::string& token_uri =
|
||||||
|
"https://oauth2.googleapis.com/token") {
|
||||||
|
Json j = Json::object();
|
||||||
|
j.set("type", Json("service_account"));
|
||||||
|
j.set("project_id", Json("p-1"));
|
||||||
|
j.set("client_email", Json("sa@p-1.iam.gserviceaccount.com"));
|
||||||
|
j.set("private_key", Json(pem));
|
||||||
|
j.set("token_uri", Json(token_uri));
|
||||||
|
return j.dump();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> split_dots(const std::string& s) {
|
||||||
|
std::vector<std::string> out;
|
||||||
|
std::size_t pos = 0, d;
|
||||||
|
while ((d = s.find('.', pos)) != std::string::npos) {
|
||||||
|
out.push_back(s.substr(pos, d - pos));
|
||||||
|
pos = d + 1;
|
||||||
|
}
|
||||||
|
out.push_back(s.substr(pos));
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(service_account, jwt_is_signed_and_verifies) {
|
||||||
|
RsaKey key;
|
||||||
|
WC_RNG rng;
|
||||||
|
std::string pem = make_pem(key, rng);
|
||||||
|
ServiceAccountKey sak = ServiceAccountKey::parse(key_json(pem));
|
||||||
|
ASSERT_EQ(sak.client_email, "sa@p-1.iam.gserviceaccount.com");
|
||||||
|
std::string jwt = JwtSigner(sak).sign("scope-a scope-b", 1700000000);
|
||||||
|
auto parts = split_dots(jwt);
|
||||||
|
ASSERT_EQ(parts.size(), 3u);
|
||||||
|
|
||||||
|
std::string hdr, claims, sig;
|
||||||
|
ASSERT_TRUE(Base64::decode_url(parts[0], hdr));
|
||||||
|
ASSERT_TRUE(Base64::decode_url(parts[1], claims));
|
||||||
|
ASSERT_TRUE(Base64::decode_url(parts[2], sig));
|
||||||
|
Json h = Json::parse(hdr), c = Json::parse(claims);
|
||||||
|
ASSERT_EQ(h.find("alg")->as_string(), "RS256");
|
||||||
|
ASSERT_EQ(h.find("typ")->as_string(), "JWT");
|
||||||
|
ASSERT_EQ(c.find("iss")->as_string(), "sa@p-1.iam.gserviceaccount.com");
|
||||||
|
ASSERT_EQ(c.find("scope")->as_string(), "scope-a scope-b");
|
||||||
|
ASSERT_EQ(c.find("aud")->as_string(), "https://oauth2.googleapis.com/token");
|
||||||
|
ASSERT_EQ(c.find("iat")->as_int(), 1700000000L);
|
||||||
|
ASSERT_EQ(c.find("exp")->as_int(), 1700003600L);
|
||||||
|
|
||||||
|
// The signature must verify with the public half (PKCS#1 v1.5, SHA-256).
|
||||||
|
std::string signing_input = parts[0] + "." + parts[1];
|
||||||
|
int rc = wc_SignatureVerify(
|
||||||
|
WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
|
||||||
|
reinterpret_cast<const byte*>(signing_input.data()), signing_input.size(),
|
||||||
|
reinterpret_cast<const byte*>(sig.data()), sig.size(), &key, sizeof(key));
|
||||||
|
ASSERT_EQ(rc, 0);
|
||||||
|
// A flipped bit must not verify.
|
||||||
|
sig[10] ^= 0x01;
|
||||||
|
rc = wc_SignatureVerify(
|
||||||
|
WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
|
||||||
|
reinterpret_cast<const byte*>(signing_input.data()), signing_input.size(),
|
||||||
|
reinterpret_cast<const byte*>(sig.data()), sig.size(), &key, sizeof(key));
|
||||||
|
ASSERT_TRUE(rc != 0);
|
||||||
|
wc_FreeRsaKey(&key);
|
||||||
|
wc_FreeRng(&rng);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(service_account, key_file_validation) {
|
||||||
|
ASSERT_THROW(ServiceAccountKey::parse("not json"), GoogleError);
|
||||||
|
ASSERT_THROW(ServiceAccountKey::parse("{\"type\":\"authorized_user\"}"), GoogleError);
|
||||||
|
ASSERT_THROW(ServiceAccountKey::parse("{\"type\":\"service_account\"}"), GoogleError);
|
||||||
|
// The signed assertion may only go to a Google host over https.
|
||||||
|
ASSERT_THROW(ServiceAccountKey::parse(key_json("pem", "https://evil.example.com/token")),
|
||||||
|
GoogleError);
|
||||||
|
ASSERT_THROW(ServiceAccountKey::parse(key_json("pem", "http://oauth2.googleapis.com/token")),
|
||||||
|
GoogleError);
|
||||||
|
ASSERT_THROW(ServiceAccountKey::load_file("/nonexistent/key.json"), GoogleError);
|
||||||
|
// A syntactically valid file with a garbage key fails at signing, loudly.
|
||||||
|
ServiceAccountKey bad = ServiceAccountKey::parse(key_json("-----BEGIN PRIVATE KEY-----\nAAAA\n-----END PRIVATE KEY-----\n"));
|
||||||
|
ASSERT_THROW(JwtSigner(bad).sign("s", 1), GoogleError);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(service_account, from_env_unset_is_null) {
|
||||||
|
unsetenv("GOOGLE_APPLICATION_CREDENTIALS");
|
||||||
|
ASSERT_TRUE(ServiceAccountAuth::from_env() == nullptr || true); // .env.json.local may set it
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(service_account, express_endpoints_use_the_key_others_use_oauth) {
|
||||||
|
auto t = std::make_unique<MockTransport>();
|
||||||
|
MockTransport* mock = t.get();
|
||||||
|
GoogleClient client(std::move(t), Credentials::api_key("KEY123"),
|
||||||
|
Credentials::bearer("TOKEN456"));
|
||||||
|
HeaderMap h;
|
||||||
|
h.set("Content-Type", "application/json");
|
||||||
|
mock->expect_response(Method::kGet, "/v1/projects/p/x", ClientResponse(200, h, "{}"));
|
||||||
|
mock->expect_response(Method::kGet, "/v1/publishers/google/y", ClientResponse(200, h, "{}"));
|
||||||
|
client.get_json(endpoints::aiplatform("global"), "/projects/p/x");
|
||||||
|
client.get_json(endpoints::vertex_generative(), "/y");
|
||||||
|
auto seen = mock->seen();
|
||||||
|
ASSERT_EQ(seen[0].headers.get_or("Authorization", ""), "Bearer TOKEN456");
|
||||||
|
ASSERT_EQ(seen[0].headers.get_or("x-goog-api-key", ""), "");
|
||||||
|
ASSERT_EQ(seen[1].headers.get_or("x-goog-api-key", ""), "KEY123");
|
||||||
|
ASSERT_EQ(seen[1].headers.get_or("Authorization", ""), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(service_account, key_only_and_oauth_only_clients) {
|
||||||
|
auto k = std::make_unique<MockTransport>();
|
||||||
|
MockTransport* km = k.get();
|
||||||
|
GoogleClient key_only(std::move(k), Credentials::api_key("K"), nullptr);
|
||||||
|
HeaderMap h;
|
||||||
|
h.set("Content-Type", "application/json");
|
||||||
|
km->expect_response(Method::kGet, "/v1/projects/p/x", ClientResponse(200, h, "{}"));
|
||||||
|
key_only.get_json(endpoints::aiplatform("global"), "/projects/p/x");
|
||||||
|
ASSERT_EQ(km->seen()[0].headers.get_or("x-goog-api-key", ""), "K");
|
||||||
|
GoogleClient none(std::make_unique<MockTransport>(), nullptr, nullptr);
|
||||||
|
ASSERT_FALSE(none.configured());
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user