Skip to main content

tuwunel_service/storage/provider/
gcs.rs

1//! Google Cloud Storage storage-provider construction.
2//!
3//! Configuration and environment values feed the object-store GCS builder.
4//! Providers without a bucket are disabled, while enabled providers expose
5//! signing through the common interface. Without configured credentials, the
6//! builder supports GKE Workload Identity through the instance metadata server.
7//! URL signing can use the IAM `signBlob` API when no private key is available.
8
9use std::{sync::Arc, time::Duration};
10
11/// Object-store transfer types used by the GCS provider boundary.
12///
13/// These re-exports match the common storage module's transfer vocabulary and
14/// avoid exposing backend-specific paths to callers.
15/// Their behavior remains defined by the object-store backend.
16pub use object_store::{GetResult, GetResultPayload, PutPayload, PutResult};
17use object_store::{client::ClientOptions, gcp::GoogleCloudStorageBuilder, signer::Signer};
18use tuwunel_core::{
19	Result,
20	config::{StorageProvider, StorageProviderGcs},
21	debug, debug_info, error, trace,
22	version::user_agent,
23};
24
25use super::Provider;
26
27type Registration = (String, Arc<Provider>);
28
29/// Builds an enabled Google Cloud Storage provider.
30///
31/// A configuration without a bucket returns `None`. Other settings are applied
32/// to the environment-derived builder before retaining the client and signer.
33#[tracing::instrument(name = "new", level = "info", skip_all, err)]
34pub(in super::super) fn new(
35	args: &crate::Args<'_>,
36	name: &str,
37	config: &StorageProviderGcs,
38) -> Result<Option<Registration>> {
39	let Some(bucket) = config
40		.bucket
41		.as_deref()
42		.filter(|_| config.is_enabled())
43	else {
44		debug!(?name, "gcs_provider.bucket not set. This configuration will be skipped");
45		return Ok(None);
46	};
47
48	// Seed from the environment so GOOGLE_* variables and the instance metadata
49	// server remain available alongside configured file paths.
50	let idle_per_host = args
51		.server
52		.config
53		.network
54		.http
55		.idle_per_host
56		.into();
57
58	let idle_timeout = Duration::from_secs(args.server.config.network.http.idle_timeout);
59
60	let options = ClientOptions::new()
61		.with_user_agent(user_agent().try_into()?)
62		.with_pool_max_idle_per_host(idle_per_host)
63		.with_pool_idle_timeout(idle_timeout);
64
65	let builder = GoogleCloudStorageBuilder::from_env()
66		.with_client_options(options)
67		.with_bucket_name(bucket);
68
69	// `with_url` discards its object prefix; `base_path` owns prefixing.
70	let builder = match config.service_account_path.as_deref() {
71		| Some(path) => builder.with_service_account_path(path),
72		| None => builder,
73	};
74
75	let builder = match config.application_credentials_path.as_deref() {
76		| Some(path) => builder.with_application_credentials(path),
77		| None => builder,
78	};
79
80	let builder = match config.use_signatures {
81		| Some(use_signatures) => builder.with_skip_signature(!use_signatures),
82		| None => builder,
83	};
84
85	trace!(?name, ?config, "Initializing GCS...");
86
87	let client = builder
88		.build()
89		.inspect_err(|e| error!(%e, "Failed to configure GCS storage client"))?;
90
91	debug_info!(name = %name, "Started GCS storage client.");
92
93	#[allow(clippy::allow_attributes, clippy::redundant_clone)] // buggy, nursery
94	let signer: Arc<dyn Signer> = Arc::new(client.clone());
95
96	let provider = Provider {
97		name: name.to_owned(),
98		base_path: config.base_path.clone().map(Into::into),
99		config: StorageProvider::gcs(Box::new(config.clone())),
100		startup_check: config.startup_check,
101		provider: Box::new(client),
102		signer: Some(signer),
103	};
104
105	Ok(Some((name.to_owned(), Arc::new(provider))))
106}