tuwunel_service/storage/provider/
gcs.rs1use std::{sync::Arc, time::Duration};
10
11pub 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#[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 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 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)] 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}