Skip to main content

opendal_service_obs/
config.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::ObsBuilder;
24
25/// Config for Huawei-Cloud Object Storage Service (OBS) support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct ObsConfig {
30    /// Root for obs.
31    pub root: Option<String>,
32    /// Endpoint for obs.
33    pub endpoint: Option<String>,
34    /// Access key id for obs.
35    pub access_key_id: Option<String>,
36    /// Secret access key for obs.
37    pub secret_access_key: Option<String>,
38    /// Security token (a.k.a. session token) for obs.
39    ///
40    /// Temporary credentials sign with `access_key_id`, `secret_access_key` and
41    /// this token, and the signer attaches the token as the
42    /// `x-obs-security-token` header.
43    ///
44    /// If this field is not set, OpenDAL reads the token from the environment
45    /// variable `HUAWEI_CLOUD_SECURITY_TOKEN` (unless `disable_config_load` is
46    /// enabled).
47    pub security_token: Option<String>,
48    /// Bucket for obs.
49    pub bucket: Option<String>,
50    /// Disable config load so that opendal will not load config from
51    /// environment.
52    ///
53    /// For examples:
54    ///
55    /// - envs like `HUAWEI_CLOUD_ACCESS_KEY_ID`
56    pub disable_config_load: bool,
57    /// Skip signature will skip loading credentials and signing requests.
58    pub skip_signature: bool,
59    /// Deprecated: OBS versioning capability is not controlled by service config.
60    #[deprecated(
61        since = "0.57.0",
62        note = "OBS versioning capability is not controlled by this option and this option is no longer needed."
63    )]
64    pub enable_versioning: bool,
65}
66
67impl Debug for ObsConfig {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        f.debug_struct("ObsConfig")
70            .field("root", &self.root)
71            .field("endpoint", &self.endpoint)
72            .field("bucket", &self.bucket)
73            .field(
74                "security_token",
75                &self.security_token.as_ref().map(|_| "<redacted>"),
76            )
77            .field("disable_config_load", &self.disable_config_load)
78            .finish_non_exhaustive()
79    }
80}
81
82impl opendal_core::Configurator for ObsConfig {
83    type Builder = ObsBuilder;
84
85    fn from_uri(uri: &opendal_core::OperatorUri) -> opendal_core::Result<Self> {
86        let mut map = uri.options().clone();
87
88        if let Some(name) = uri.name() {
89            map.insert("bucket".to_string(), name.to_string());
90        }
91
92        if let Some(root) = uri.root() {
93            map.insert("root".to_string(), root.to_string());
94        }
95
96        Self::from_iter(map)
97    }
98
99    fn into_builder(self) -> Self::Builder {
100        ObsBuilder { config: self }
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107    use opendal_core::Configurator;
108    use opendal_core::OperatorUri;
109
110    #[test]
111    fn debug_redacts_security_token() {
112        let cfg = ObsConfig {
113            security_token: Some("super-secret-token".to_string()),
114            ..Default::default()
115        };
116
117        let debug = format!("{cfg:?}");
118        assert!(!debug.contains("super-secret-token"), "{debug}");
119        assert!(debug.contains("<redacted>"), "{debug}");
120    }
121
122    #[test]
123    fn from_uri_extracts_bucket_and_root() {
124        let uri = OperatorUri::new(
125            "obs://example-bucket/path/to/root",
126            Vec::<(String, String)>::new(),
127        )
128        .unwrap();
129        let cfg = ObsConfig::from_uri(&uri).unwrap();
130        assert_eq!(cfg.bucket.as_deref(), Some("example-bucket"));
131        assert_eq!(cfg.root.as_deref(), Some("path/to/root"));
132    }
133}