Skip to main content

faucet_common_spanner/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2//! Shared Google Cloud Spanner types for the faucet-stream ecosystem.
3//!
4//! This crate holds everything the `faucet-source-spanner` and
5//! `faucet-sink-spanner` connectors have in common:
6//!
7//! - [`SpannerCredentials`] — the `{ type, config }` credentials enum shared
8//!   by both connectors (mirrors `faucet-common-bigquery`).
9//! - [`SpannerConnection`] — the flattened connection block (`project_id` /
10//!   `instance` / `database` / `auth` / `max_sessions` / `emulator_host`)
11//!   plus [`SpannerConnection::connect`] / [`SpannerConnection::connect_admin`]
12//!   client builders.
13//! - [`types`] — `INFORMATION_SCHEMA` type-string parsing and the mapping to
14//!   JSON-Schema fragments.
15//! - [`decode`] — generic Spanner row → `serde_json::Value` decoding for
16//!   arbitrary queries (no compile-time schema).
17//! - [`encode`] — `serde_json::Value` → Spanner mutation value encoding keyed
18//!   by the destination column type.
19
20pub mod decode;
21pub mod encode;
22pub mod types;
23
24use faucet_core::FaucetError;
25use gcloud_gax::conn::Environment;
26use gcloud_spanner::admin::AdminClientConfig;
27use gcloud_spanner::admin::client::Client as AdminClient;
28use gcloud_spanner::client::google_cloud_auth::credentials::CredentialsFile;
29use gcloud_spanner::client::{Client, ClientConfig};
30use schemars::JsonSchema;
31use serde::{Deserialize, Serialize};
32use std::time::Duration;
33
34/// Sessions Spanner allows per gRPC channel (a hard server-side limit the
35/// client enforces at construction time).
36const SESSIONS_PER_CHANNEL: usize = 100;
37
38/// How long a session acquisition may wait for a pooled session before
39/// failing. The crate default (1 s) is too tight for bursty page writes
40/// against a small pool; 30 s matches the channel connect/request timeouts.
41const SESSION_GET_TIMEOUT: Duration = Duration::from_secs(30);
42
43/// Credentials used to authenticate against Cloud Spanner.
44///
45/// Serialized in the project-wide adjacently-tagged shape:
46///
47/// ```yaml
48/// auth:
49///   type: service_account_key_path
50///   config:
51///     path: /secrets/sa.json
52/// ```
53#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
54#[serde(tag = "type", content = "config", rename_all = "snake_case")]
55pub enum SpannerCredentials {
56    /// Load a service-account key from a JSON file on disk.
57    ServiceAccountKeyPath {
58        /// Filesystem path to the service-account key JSON.
59        #[schemars(extend("x-faucet-resource" = "path"))]
60        path: String,
61    },
62    /// Inline service-account key JSON (prefer `${vault:…}` / `${env:…}`
63    /// interpolation over literal keys in config files).
64    ServiceAccountKey {
65        /// The full service-account key JSON document.
66        json: String,
67    },
68    /// Application Default Credentials: `GOOGLE_APPLICATION_CREDENTIALS`,
69    /// `gcloud auth application-default login`, or the GCE/GKE metadata
70    /// server.
71    #[default]
72    ApplicationDefault,
73}
74
75impl std::fmt::Debug for SpannerCredentials {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        match self {
78            Self::ServiceAccountKeyPath { path } => f
79                .debug_struct("ServiceAccountKeyPath")
80                .field("path", path)
81                .finish(),
82            // Never print inline key material.
83            Self::ServiceAccountKey { .. } => f
84                .debug_struct("ServiceAccountKey")
85                .field("json", &"***")
86                .finish(),
87            Self::ApplicationDefault => f.debug_struct("ApplicationDefault").finish(),
88        }
89    }
90}
91
92fn default_max_sessions() -> usize {
93    100
94}
95
96/// Connection block shared by the Spanner source and sink configs (flattened
97/// into each connector config via `#[serde(flatten)]`).
98#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
99pub struct SpannerConnection {
100    /// GCP project ID that owns the Spanner instance.
101    pub project_id: String,
102    /// Spanner instance ID.
103    pub instance: String,
104    /// Database name within the instance.
105    pub database: String,
106    /// Credentials. Defaults to Application Default Credentials.
107    #[serde(default)]
108    pub auth: SpannerCredentials,
109    /// Upper bound on pooled Spanner sessions (server-side resources).
110    /// Channels are sized automatically at one per 100 sessions.
111    #[serde(default = "default_max_sessions")]
112    pub max_sessions: usize,
113    /// Emulator endpoint override (`host:port`, e.g. `localhost:9010`).
114    /// When set, the connection is plaintext and unauthenticated — exactly
115    /// what `SPANNER_EMULATOR_HOST` does, but scoped to this connector so
116    /// parallel pipelines/tests don't race on a process-global env var.
117    /// The env var is still honored when this field is unset.
118    #[serde(default, skip_serializing_if = "Option::is_none")]
119    #[schemars(extend("x-faucet-resource" = "url"))]
120    pub emulator_host: Option<String>,
121}
122
123impl SpannerConnection {
124    /// Fully-qualified database path:
125    /// `projects/{project}/instances/{instance}/databases/{database}`.
126    pub fn database_path(&self) -> String {
127        format!(
128            "projects/{}/instances/{}/databases/{}",
129            self.project_id, self.instance, self.database
130        )
131    }
132
133    /// Validate the connection block at config-load time.
134    pub fn validate(&self) -> Result<(), FaucetError> {
135        for (field, value) in [
136            ("project_id", &self.project_id),
137            ("instance", &self.instance),
138            ("database", &self.database),
139        ] {
140            if value.trim().is_empty() {
141                return Err(FaucetError::Config(format!(
142                    "spanner: `{field}` must not be empty"
143                )));
144            }
145        }
146        if self.max_sessions == 0 {
147            return Err(FaucetError::Config(
148                "spanner: `max_sessions` must be at least 1".into(),
149            ));
150        }
151        Ok(())
152    }
153
154    /// Build the data-plane [`Client`]. Honors `emulator_host` (and, when
155    /// unset, the `SPANNER_EMULATOR_HOST` env var via the client default).
156    pub async fn connect(&self) -> Result<Client, FaucetError> {
157        self.validate()?;
158        let mut config = ClientConfig::default();
159        config.channel_config.num_channels = self.max_sessions.div_ceil(SESSIONS_PER_CHANNEL);
160        config.session_config.min_opened = 1;
161        config.session_config.max_idle = self.max_sessions;
162        config.session_config.max_opened = self.max_sessions;
163        config.session_config.session_get_timeout = SESSION_GET_TIMEOUT;
164        if let Some(host) = &self.emulator_host {
165            config.environment = Environment::Emulator(host.clone());
166        }
167        // Credential application is a no-op under an emulator environment,
168        // so it is safe to apply unconditionally.
169        let config = self.apply_data_credentials(config).await?;
170        Client::new(self.database_path(), config)
171            .await
172            .map_err(|e| FaucetError::Custom(Box::new(e)))
173    }
174
175    /// Build the admin (DDL) client. Same credential and emulator handling
176    /// as [`connect`](Self::connect).
177    pub async fn connect_admin(&self) -> Result<AdminClient, FaucetError> {
178        self.validate()?;
179        let mut config = AdminClientConfig::default();
180        if let Some(host) = &self.emulator_host {
181            config.environment = Environment::Emulator(host.clone());
182        }
183        let config = match &self.auth {
184            SpannerCredentials::ApplicationDefault => config
185                .with_auth()
186                .await
187                .map_err(|e| FaucetError::Auth(format!("spanner ADC: {e}")))?,
188            SpannerCredentials::ServiceAccountKeyPath { path } => config
189                .with_credentials(credentials_from_file(path).await?)
190                .await
191                .map_err(|e| FaucetError::Auth(format!("spanner key file {path}: {e}")))?,
192            SpannerCredentials::ServiceAccountKey { json } => config
193                .with_credentials(credentials_from_str(json).await?)
194                .await
195                .map_err(|e| FaucetError::Auth(format!("spanner inline key: {e}")))?,
196        };
197        AdminClient::new(config)
198            .await
199            .map_err(|e| FaucetError::Custom(Box::new(e)))
200    }
201
202    async fn apply_data_credentials(
203        &self,
204        config: ClientConfig,
205    ) -> Result<ClientConfig, FaucetError> {
206        match &self.auth {
207            SpannerCredentials::ApplicationDefault => config
208                .with_auth()
209                .await
210                .map_err(|e| FaucetError::Auth(format!("spanner ADC: {e}"))),
211            SpannerCredentials::ServiceAccountKeyPath { path } => config
212                .with_credentials(credentials_from_file(path).await?)
213                .await
214                .map_err(|e| FaucetError::Auth(format!("spanner key file {path}: {e}"))),
215            SpannerCredentials::ServiceAccountKey { json } => config
216                .with_credentials(credentials_from_str(json).await?)
217                .await
218                .map_err(|e| FaucetError::Auth(format!("spanner inline key: {e}"))),
219        }
220    }
221}
222
223async fn credentials_from_file(path: &str) -> Result<CredentialsFile, FaucetError> {
224    CredentialsFile::new_from_file(path.to_string())
225        .await
226        .map_err(|e| FaucetError::Auth(format!("spanner key file {path}: {e}")))
227}
228
229async fn credentials_from_str(json: &str) -> Result<CredentialsFile, FaucetError> {
230    CredentialsFile::new_from_str(json)
231        .await
232        .map_err(|e| FaucetError::Auth(format!("spanner inline key: {e}")))
233}
234
235/// Quote a Spanner identifier with backticks. GoogleSQL quoted identifiers
236/// take string-literal escapes, so an embedded backslash or backtick is
237/// backslash-escaped (a doubled backtick would end the identifier).
238pub fn quote_ident_spanner(ident: &str) -> String {
239    format!("`{}`", ident.replace('\\', "\\\\").replace('`', "\\`"))
240}
241
242#[cfg(test)]
243mod tests {
244    use super::*;
245    use serde_json::json;
246
247    #[test]
248    fn credentials_serde_shape_is_adjacently_tagged() {
249        let c: SpannerCredentials = serde_json::from_value(json!({
250            "type": "service_account_key_path",
251            "config": {"path": "/tmp/sa.json"}
252        }))
253        .unwrap();
254        assert!(matches!(
255            c,
256            SpannerCredentials::ServiceAccountKeyPath { ref path } if path == "/tmp/sa.json"
257        ));
258        let adc: SpannerCredentials =
259            serde_json::from_value(json!({"type": "application_default"})).unwrap();
260        assert!(matches!(adc, SpannerCredentials::ApplicationDefault));
261        assert!(matches!(
262            SpannerCredentials::default(),
263            SpannerCredentials::ApplicationDefault
264        ));
265    }
266
267    #[test]
268    fn debug_masks_inline_key_material() {
269        let c = SpannerCredentials::ServiceAccountKey {
270            json: "{\"private_key\": \"SECRET\"}".into(),
271        };
272        let rendered = format!("{c:?}");
273        assert!(!rendered.contains("SECRET"));
274        assert!(rendered.contains("***"));
275    }
276
277    fn conn() -> SpannerConnection {
278        SpannerConnection {
279            project_id: "p".into(),
280            instance: "i".into(),
281            database: "d".into(),
282            auth: SpannerCredentials::default(),
283            max_sessions: default_max_sessions(),
284            emulator_host: None,
285        }
286    }
287
288    #[test]
289    fn database_path_shape() {
290        assert_eq!(conn().database_path(), "projects/p/instances/i/databases/d");
291    }
292
293    #[test]
294    fn validate_rejects_empty_fields_and_zero_sessions() {
295        let mut c = conn();
296        c.project_id = " ".into();
297        assert!(matches!(c.validate(), Err(FaucetError::Config(_))));
298        let mut c = conn();
299        c.instance = String::new();
300        assert!(c.validate().is_err());
301        let mut c = conn();
302        c.database = String::new();
303        assert!(c.validate().is_err());
304        let mut c = conn();
305        c.max_sessions = 0;
306        assert!(c.validate().is_err());
307        assert!(conn().validate().is_ok());
308    }
309
310    #[test]
311    fn connection_defaults_from_minimal_config() {
312        let c: SpannerConnection = serde_json::from_value(json!({
313            "project_id": "p", "instance": "i", "database": "d"
314        }))
315        .unwrap();
316        assert_eq!(c.max_sessions, 100);
317        assert!(c.emulator_host.is_none());
318        assert!(matches!(c.auth, SpannerCredentials::ApplicationDefault));
319    }
320
321    #[test]
322    fn quote_ident_backslash_escapes() {
323        assert_eq!(quote_ident_spanner("plain"), "`plain`");
324        assert_eq!(quote_ident_spanner("we`ird"), "`we\\`ird`");
325        assert_eq!(quote_ident_spanner("a\\b"), "`a\\\\b`");
326    }
327
328    #[tokio::test]
329    async fn credentials_from_missing_file_is_a_typed_auth_error() {
330        let Err(err) = credentials_from_file("/definitely/not/here.json").await else {
331            panic!("missing key file unexpectedly loaded");
332        };
333        assert!(matches!(err, FaucetError::Auth(_)));
334        assert!(err.to_string().contains("/definitely/not/here.json"));
335    }
336
337    #[tokio::test]
338    async fn credentials_from_invalid_json_is_a_typed_auth_error() {
339        let Err(err) = credentials_from_str("{not json").await else {
340            panic!("invalid key json unexpectedly parsed");
341        };
342        assert!(matches!(err, FaucetError::Auth(_)));
343        assert!(err.to_string().contains("inline key"));
344    }
345
346    /// A syntactically valid (but fake) service-account document — every
347    /// field except `type` is optional in the credentials-file schema.
348    const FAKE_SA: &str = r#"{"type": "service_account", "project_id": "p"}"#;
349
350    /// Every credential variant must build under an emulator environment
351    /// (where token providers are inert) and then fail fast at client
352    /// construction against a dead endpoint — proving the credential arms
353    /// never panic and map errors into `FaucetError`.
354    #[tokio::test]
355    async fn connect_exercises_every_credential_variant_against_dead_emulator() {
356        let key_path = std::env::temp_dir().join("faucet-spanner-test-sa.json");
357        std::fs::write(&key_path, FAKE_SA).expect("write fake key");
358        let variants = [
359            SpannerCredentials::ApplicationDefault,
360            SpannerCredentials::ServiceAccountKey {
361                json: FAKE_SA.to_string(),
362            },
363            SpannerCredentials::ServiceAccountKeyPath {
364                path: key_path.to_string_lossy().into_owned(),
365            },
366        ];
367        for auth in variants {
368            let mut c = conn();
369            // Port 1 is never a Spanner emulator: connection is refused
370            // immediately, after the credential arm has already run.
371            c.emulator_host = Some("127.0.0.1:1".into());
372            c.auth = auth.clone();
373            let Err(err) = c.connect().await else {
374                panic!("data-plane connect unexpectedly succeeded for {auth:?}");
375            };
376            assert!(
377                matches!(err, FaucetError::Custom(_)),
378                "data-plane connect: unexpected error {err:?} for {auth:?}"
379            );
380            let Err(err) = c.connect_admin().await else {
381                panic!("admin connect unexpectedly succeeded for {auth:?}");
382            };
383            assert!(
384                matches!(err, FaucetError::Custom(_)),
385                "admin connect: unexpected error {err:?} for {auth:?}"
386            );
387        }
388    }
389}