#include "google/sandbox/data_plane_connector.h" #include "google/credentials.h" #include "google/errors.h" #include "google/sandbox/sandboxes.h" #include "google/service_account.h" namespace molo::google::sandbox { DataPlaneOptions DataPlaneOptions::from_env() { DataPlaneOptions o; o.service_account_email = Credentials::env("GOOGLE_SERVICE_ACCOUNT_EMAIL"); if (o.service_account_email.empty()) { std::string path = Credentials::env("GOOGLE_APPLICATION_CREDENTIALS"); if (!path.empty()) { try { o.service_account_email = ServiceAccountKey::load_file(path).client_email; } catch (const std::exception&) { // No usable key file: the data plane stays unavailable, loudly, at use. } } } return o; } DataPlaneConnector::DataPlaneConnector(std::shared_ptr client, DataPlaneOptions options, std::shared_ptr tokens) : SandboxService(client), options_(std::move(options)), tokens_(std::move(tokens)) { if (!tokens_ && !options_.service_account_email.empty()) tokens_ = std::make_shared(client, options_.service_account_email, options_.jwt_ttl_seconds); } SandboxEnvironment DataPlaneConnector::environment(const std::string& sandbox_name) const { return Sandboxes(client_).get(sandbox_name); } std::shared_ptr DataPlaneConnector::open(const std::string& sandbox_name) const { SandboxNames::parse_sandbox(sandbox_name); if (!client_->configured()) throw NotConfiguredError("sandbox-data-plane"); // No signer is a configuration gap, not bad input: 503 like a missing credential. if (!tokens_) throw NotConfiguredError("sandbox-data-plane (set GOOGLE_SERVICE_ACCOUNT_EMAIL)"); SandboxEnvironment env = environment(sandbox_name); if (!env.running()) throw GoogleError(409, "FAILED_PRECONDITION", "sandbox is " + (env.state.empty() ? std::string("in an unknown state") : env.state) + ", not STATE_RUNNING", "sandbox-data-plane"); DataPlaneTarget target = DataPlaneTarget::from_environment( env, options_.psc_endpoint, options_.loopback_override, options_.port); return std::make_shared(std::move(target), tokens_, nullptr, options_.timeout_ms); } std::unique_ptr DataPlaneConnector::open_cdp(const std::string& sandbox_name, CdpOptions options) const { return std::make_unique(open(sandbox_name), std::move(options)); } } // namespace molo::google::sandbox