confium-store-cloud 0.10.2

AWS, GCP, and Azure cloud KMS storage backend for Confium
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Google Cloud Key Management Service backend.
//!
//! Talks to Cloud KMS via the [`google_cloud_kms::client::Client`]
//! async client. Construction goes through
//! [`google_cloud_kms::client::ClientConfig`] with auth resolved from
//! `GOOGLE_APPLICATION_CREDENTIALS` /
//! `GOOGLE_APPLICATION_CREDENTIALS_JSON` / the GCE metadata server —
//! exactly the same chain every other google-cloud-rust crate uses.
//!
//! # Wire name
//!
//! Registered as `"gcp-kms"`.
//!
//! # Options
//!
//! | key               | meaning                                              |
//! |-------------------|------------------------------------------------------|
//! | `credentials`     | Path to a service-account JSON key file.             |
//! | `credentials_json`| Inline service-account JSON (useful for sealed secrets).|
//! | `project`         | GCP project ID (used as the default key ring scope). |
//! | `location`        | KMS location (e.g. `global`, `us-central1`).         |
//! | `key_ring`        | Default key ring for `put_secret`.                   |
//!
//! # KMS API status
//!
//! Construction is wired; the [`StoreInstance`] methods return
//! [`NotImplemented`](confium_store::error::Error::NotImplemented)
//! pending the `cfmp_sign_with_handle` plugin contract (TODO #03).
//! Cloud KMS, like AWS KMS, never exports raw key material; it returns
//! opaque resource names (`projects/.../keyRings/.../cryptoKeys/...`)
//! that a signature plugin must use to invoke `AsymmetricSign`.

use std::ffi::c_void;

use confium_store::backend::{Compartment, Options, StoreBackend, StoreInstance};
use confium_store::error::{Error, Result};
use confium_store::register_backend;

/// Options key naming the path to a service-account JSON credentials
/// file. Falls back to `GOOGLE_APPLICATION_CREDENTIALS`.
pub const OPT_CREDENTIALS: &str = "credentials";

/// Options key naming inline service-account JSON. Useful when the
/// credentials are provisioned via a sealed secret rather than a file.
pub const OPT_CREDENTIALS_JSON: &str = "credentials_json";

/// Options key naming the GCP project ID.
pub const OPT_PROJECT: &str = "project";

/// Options key naming the KMS location (e.g. `global`).
pub const OPT_LOCATION: &str = "location";

/// Options key naming the default key ring.
pub const OPT_KEY_RING: &str = "key_ring";

/// Factory for the Google Cloud KMS backend.
pub struct GcpKmsBackend;

impl StoreBackend for GcpKmsBackend {
    fn name(&self) -> &'static str {
        "gcp-kms"
    }

    fn open(&self, opts: &Options) -> Result<Box<dyn StoreInstance>> {
        // Capture the resolved configuration now. The actual
        // `ClientConfig::default().with_auth().await` call is async and
        // the trait is synchronous, so we stash the inputs and defer
        // client construction to the first call that needs it.
        Ok(Box::new(GcpKmsInstance {
            config: GcpKmsConfig {
                credentials: opts.get(OPT_CREDENTIALS).cloned(),
                credentials_json: opts.get(OPT_CREDENTIALS_JSON).cloned(),
                project: opts.get(OPT_PROJECT).cloned(),
                location: opts.get(OPT_LOCATION).cloned(),
                key_ring: opts.get(OPT_KEY_RING).cloned(),
            },
            rt: tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .map_err(|e| Error::Wrapped {
                    message: format!("gcp-kms tokio runtime: {e}"),
                })?,
            client: std::sync::OnceLock::new(),
        }))
    }
}

register_backend!(GcpKmsBackend);

/// Resolved GCP-side configuration captured at `open` time. Read once
/// Held by the instance for the lazy client construction.
#[allow(dead_code)]
#[derive(Clone, Debug, Default)]
struct GcpKmsConfig {
    credentials: Option<String>,
    credentials_json: Option<String>,
    project: Option<String>,
    location: Option<String>,
    key_ring: Option<String>,
}

/// One open Cloud KMS connection.
///
/// The [`google_cloud_kms::client::Client`] is built lazily on first
/// use — the SDK's auth chain is async and the trait is not. A
/// per-instance tokio runtime drives the deferred load.
///
pub struct GcpKmsInstance {
    config: GcpKmsConfig,
    rt: tokio::runtime::Runtime,
    client: std::sync::OnceLock<google_cloud_kms::client::Client>,
}

impl GcpKmsInstance {
    /// Build the Cloud KMS client on first use: an explicit
    /// service-account file (`credentials`), or the default ADC chain
    /// (GOOGLE_APPLICATION_CREDENTIALS / _JSON env vars, metadata
    /// server). `OnceLock` lets read-side calls construct it too.
    fn ensure_client(&self) -> Result<&google_cloud_kms::client::Client> {
        if let Some(client) = self.client.get() {
            return Ok(client);
        }
        let cfg = google_cloud_kms::client::ClientConfig::default();
        let cfg = if let Some(path) = &self.config.credentials {
            let cred = self
                .rt
                .block_on(
                    google_cloud_auth::credentials::CredentialsFile::new_from_file(path.clone()),
                )
                .map_err(|e| Error::Wrapped {
                    message: format!("gcp-kms credentials file: {e}"),
                })?;
            self.rt
                .block_on(cfg.with_credentials(cred))
                .map_err(|e| Error::Wrapped {
                    message: format!("gcp-kms credentials: {e}"),
                })?
        } else if let Some(json) = &self.config.credentials_json {
            let cred = self
                .rt
                .block_on(google_cloud_auth::credentials::CredentialsFile::new_from_str(json))
                .map_err(|e| Error::Wrapped {
                    message: format!("gcp-kms credentials json: {e}"),
                })?;
            self.rt
                .block_on(cfg.with_credentials(cred))
                .map_err(|e| Error::Wrapped {
                    message: format!("gcp-kms credentials: {e}"),
                })?
        } else {
            self.rt
                .block_on(cfg.with_auth())
                .map_err(|e| Error::Wrapped {
                    message: format!("gcp-kms application default credentials: {e}"),
                })?
        };
        let built = self
            .rt
            .block_on(google_cloud_kms::client::Client::new(cfg))
            .map_err(|e| Error::Wrapped {
                message: format!("gcp-kms client: {e}"),
            })?;
        Ok(self.client.get_or_init(|| built))
    }
    /// Resolve the crypto key version resource name. Accepts a full
    /// `projects/.../cryptoKeyVersions/N` path, or a bare crypto key
    /// name combined with the `project`/`location`/`key_ring` options
    /// and the `key_version` option (default `1`).
    fn crypto_key_version_path(&self, key_id: &str) -> Result<String> {
        if key_id.starts_with("projects/") {
            if key_id.contains("/cryptoKeyVersions/") {
                return Ok(key_id.to_string());
            }
            return Ok(format!("{key_id}/cryptoKeyVersions/1"));
        }
        let project = self
            .config
            .project
            .as_deref()
            .ok_or_else(|| Error::Wrapped {
                message: "gcp-kms sign: option 'project' is required for a bare key id".to_string(),
            })?;
        let location = self.config.location.as_deref().unwrap_or("global");
        let key_ring = self
            .config
            .key_ring
            .as_deref()
            .ok_or_else(|| Error::Wrapped {
                message: "gcp-kms sign: option 'key_ring' is required for a bare key id"
                    .to_string(),
            })?;
        Ok(format!(
            "projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{key_id}/cryptoKeyVersions/1"
        ))
    }
}

impl StoreInstance for GcpKmsInstance {
    fn put_secret(
        &mut self,
        _module: &str,
        _app: &str,
        _key_id: &str,
        _key: *mut c_void,
    ) -> Result<()> {
        self.ensure_client()?;
        // Cloud KMS key rings / crypto keys are created via
        // `CreateKeyRing` / `CreateCryptoKey`. Deferred to the
        // post-TODO-#03 revision.
        Err(Error::NotImplemented {
            what: "gcp-kms put_secret",
        })
    }

    fn get_secret(&self, _module: &str, _app: &str, _key_id: &str) -> Result<*mut c_void> {
        Err(Error::NotImplemented {
            what: "gcp-kms get_secret",
        })
    }

    fn put_public(
        &mut self,
        _module: &str,
        _app: &str,
        _identity: &str,
        _key: *mut c_void,
        _sig: &[u8],
    ) -> Result<()> {
        let _ = self.ensure_client()?;
        Err(Error::NotImplemented {
            what: "gcp-kms put_public",
        })
    }

    fn get_public(
        &self,
        _module: &str,
        _app: &str,
        _identity: &str,
    ) -> Result<(*mut c_void, Vec<u8>)> {
        Err(Error::NotImplemented {
            what: "gcp-kms get_public",
        })
    }

    fn enumerate(
        &self,
        _module: &str,
        _app: &str,
        _compartment: Compartment,
    ) -> Result<Vec<(*mut c_void, String)>> {
        // `client.list_key_rings(...)` / `list_crypto_keys(...)` is the
        // eventual backing call.
        Err(Error::NotImplemented {
            what: "gcp-kms enumerate",
        })
    }
    fn sign(
        &self,
        _module: &str,
        _app: &str,
        key_id: &str,
        algorithm: &str,
        message: &[u8],
    ) -> Result<Vec<u8>> {
        if algorithm.is_empty() {
            return Err(Error::Wrapped {
                message: "gcp-kms sign: algorithm is required (it is only a hint for ".to_string()
                    + "logging; the key version's own algorithm signs)",
            });
        }
        let name = self.crypto_key_version_path(key_id)?;
        let client = self.ensure_client()?;
        let req = google_cloud_kms::grpc::kms::v1::AsymmetricSignRequest {
            name,
            data: message.to_vec(),
            ..Default::default()
        };
        let resp = self
            .rt
            .block_on(client.asymmetric_sign(req, None))
            .map_err(|e| Error::Wrapped {
                message: format!("gcp-kms AsymmetricSign: {e}"),
            })?;
        Ok(resp.signature)
    }
}

// SAFETY: `google_cloud_kms::client::Client` wraps an Arc'd gRPC
// channel and is `Send + Sync`. The config is plain owned strings.
unsafe impl Send for GcpKmsInstance {}
unsafe impl Sync for GcpKmsInstance {}

#[cfg(test)]
mod tests {
    use super::*;

    // Distinct non-null sentinel pointer so the tests can pass a handle
    // into `put_secret` without allocating real key material. The
    // backend treats it as an opaque token. Mirrors the same helper in
    // `confium_store::backends::memory`.
    // Credential chain reads process env; pin it off for hermetic tests.
    static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

    struct EnvVarGuard(&'static str, Option<String>);

    impl EnvVarGuard {
        fn remove(name: &'static str) -> Self {
            let prev = std::env::var(name).ok();
            // SAFETY: every env-touching GCP test holds ENV_LOCK.
            unsafe { std::env::remove_var(name) };
            Self(name, prev)
        }
    }

    impl Drop for EnvVarGuard {
        fn drop(&mut self) {
            if let Some(v) = &self.1 {
                // SAFETY: as above.
                unsafe { std::env::set_var(self.0, v) };
            }
        }
    }

    fn sentinel(n: usize) -> *mut c_void {
        n as *mut c_void
    }

    #[test]
    fn name_is_stable_wire_name() {
        assert_eq!(GcpKmsBackend.name(), "gcp-kms");
    }

    #[test]
    fn open_returns_instance_without_calling_gcp() {
        let opts = Options::new();
        let mut instance = GcpKmsBackend.open(&opts).expect("open");
        assert!(instance.put_secret("m", "a", "k", sentinel(1)).is_err());
    }

    // Client construction is real: with no credentials configured
    // (and no ADC on the machine) the auth failure surfaces as a
    // Wrapped error instead of the old NotImplemented stub.
    #[test]
    fn put_secret_without_credentials_surfaces_the_auth_error() {
        let _env = ENV_LOCK.lock().unwrap();
        let _g1 = EnvVarGuard::remove("GOOGLE_APPLICATION_CREDENTIALS");
        let _g2 = EnvVarGuard::remove("GOOGLE_APPLICATION_CREDENTIALS_JSON");
        let mut instance = GcpKmsBackend.open(&Options::new()).expect("open");
        let err = instance.put_secret("m", "a", "k", sentinel(1)).unwrap_err();
        match err {
            Error::Wrapped { message } => {
                assert!(message.contains("gcp-kms"), "message: {message}");
            }
            other => panic!("expected Wrapped, got {other:?}"),
        }
    }

    #[test]
    fn put_secret_with_malformed_credentials_reports_the_parse_error() {
        let _env = ENV_LOCK.lock().unwrap();
        let _g1 = EnvVarGuard::remove("GOOGLE_APPLICATION_CREDENTIALS");
        let _g2 = EnvVarGuard::remove("GOOGLE_APPLICATION_CREDENTIALS_JSON");
        let mut opts = Options::new();
        opts.insert(OPT_CREDENTIALS_JSON.to_string(), "not json".to_string());
        let mut instance = GcpKmsBackend.open(&opts).expect("open");
        let err = instance.put_secret("m", "a", "k", sentinel(1)).unwrap_err();
        match err {
            Error::Wrapped { message } => {
                assert!(message.contains("credentials json"), "message: {message}");
            }
            other => panic!("expected Wrapped, got {other:?}"),
        }
    }

    #[test]
    fn sign_rejects_a_bare_key_id_without_project_options() {
        let _env = ENV_LOCK.lock().unwrap();
        let _g1 = EnvVarGuard::remove("GOOGLE_APPLICATION_CREDENTIALS");
        let _g2 = EnvVarGuard::remove("GOOGLE_APPLICATION_CREDENTIALS_JSON");
        let instance = GcpKmsBackend.open(&Options::new()).expect("open");
        let err = instance
            .sign("m", "a", "my-key", "EC_SIGN_P256_SHA256", b"msg")
            .unwrap_err();
        match err {
            Error::Wrapped { message } => {
                assert!(message.contains("project"), "message: {message}");
            }
            other => panic!("expected Wrapped, got {other:?}"),
        }
    }

    #[test]
    fn sign_requires_an_algorithm() {
        let instance = GcpKmsBackend.open(&Options::new()).expect("open");
        let err = instance
            .sign(
                "m",
                "a",
                "projects/p/locations/l/keyRings/r/cryptoKeys/k/cryptoKeyVersions/1",
                "",
                b"msg",
            )
            .unwrap_err();
        match err {
            Error::Wrapped { message } => {
                assert!(message.contains("algorithm"), "message: {message}");
            }
            other => panic!("expected Wrapped, got {other:?}"),
        }
    }

    #[test]
    fn backend_is_registered() {
        let backend = confium_store::backend::find("gcp-kms").expect("gcp-kms backend registered");
        assert_eq!(backend.name(), "gcp-kms");
    }
}