opendal/services/azblob/
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;
19use std::fmt::Formatter;
20
21use serde::Deserialize;
22use serde::Serialize;
23
24/// Azure Storage Blob services support.
25#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
26pub struct AzblobConfig {
27    /// The root of Azblob service backend.
28    ///
29    /// All operations will happen under this root.
30    pub root: Option<String>,
31
32    /// The container name of Azblob service backend.
33    #[serde(alias = "azure_container_name", alias = "container_name")]
34    pub container: String,
35
36    /// The endpoint of Azblob service backend.
37    ///
38    /// Endpoint must be full uri, e.g.
39    ///
40    /// - Azblob: `https://accountname.blob.core.windows.net`
41    /// - Azurite: `http://127.0.0.1:10000/devstoreaccount1`
42    #[serde(alias = "azure_storage_endpoint", alias = "azure_endpoint")]
43    pub endpoint: Option<String>,
44
45    /// The account name of Azblob service backend.
46    #[serde(alias = "azure_storage_account_name")]
47    pub account_name: Option<String>,
48
49    /// The account key of Azblob service backend.
50    #[serde(
51        alias = "azure_storage_account_key",
52        alias = "azure_storage_access_key",
53        alias = "azure_storage_master_key",
54        alias = "access_key",
55        alias = "master_key"
56    )]
57    pub account_key: Option<String>,
58
59    /// The encryption key of Azblob service backend.
60    pub encryption_key: Option<String>,
61
62    /// The encryption key sha256 of Azblob service backend.
63    pub encryption_key_sha256: Option<String>,
64
65    /// The encryption algorithm of Azblob service backend.
66    pub encryption_algorithm: Option<String>,
67
68    /// The sas token of Azblob service backend.
69    #[serde(
70        alias = "azure_storage_sas_key",
71        alias = "azure_storage_sas_token",
72        alias = "sas_key"
73    )]
74    pub sas_token: Option<String>,
75
76    /// The maximum batch operations of Azblob service backend.
77    pub batch_max_operations: Option<usize>,
78}
79
80impl Debug for AzblobConfig {
81    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
82        let mut ds = f.debug_struct("AzblobConfig");
83
84        ds.field("root", &self.root);
85        ds.field("container", &self.container);
86        ds.field("endpoint", &self.endpoint);
87
88        if self.account_name.is_some() {
89            ds.field("account_name", &"<redacted>");
90        }
91        if self.account_key.is_some() {
92            ds.field("account_key", &"<redacted>");
93        }
94        if self.sas_token.is_some() {
95            ds.field("sas_token", &"<redacted>");
96        }
97
98        ds.finish()
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105
106    #[test]
107    fn test_container_name_aliases() {
108        // Test original container field
109        let json = r#"{"container": "test-container"}"#;
110        let config: AzblobConfig = serde_json::from_str(json).unwrap();
111        assert_eq!(config.container, "test-container");
112
113        // Test azure_container_name alias
114        let json = r#"{"azure_container_name": "test-container"}"#;
115        let config: AzblobConfig = serde_json::from_str(json).unwrap();
116        assert_eq!(config.container, "test-container");
117
118        // Test container_name alias
119        let json = r#"{"container_name": "test-container"}"#;
120        let config: AzblobConfig = serde_json::from_str(json).unwrap();
121        assert_eq!(config.container, "test-container");
122    }
123
124    #[test]
125    fn test_account_name_aliases() {
126        // Test original account_name field
127        let json = r#"{"container": "test", "account_name": "testaccount"}"#;
128        let config: AzblobConfig = serde_json::from_str(json).unwrap();
129        assert_eq!(config.account_name, Some("testaccount".to_string()));
130
131        // Test azure_storage_account_name alias
132        let json = r#"{"container": "test", "azure_storage_account_name": "testaccount"}"#;
133        let config: AzblobConfig = serde_json::from_str(json).unwrap();
134        assert_eq!(config.account_name, Some("testaccount".to_string()));
135    }
136
137    #[test]
138    fn test_account_key_aliases() {
139        // Test original account_key field
140        let json = r#"{"container": "test", "account_key": "dGVzdGtleQ=="}"#;
141        let config: AzblobConfig = serde_json::from_str(json).unwrap();
142        assert_eq!(config.account_key, Some("dGVzdGtleQ==".to_string()));
143
144        // Test azure_storage_account_key alias
145        let json = r#"{"container": "test", "azure_storage_account_key": "dGVzdGtleQ=="}"#;
146        let config: AzblobConfig = serde_json::from_str(json).unwrap();
147        assert_eq!(config.account_key, Some("dGVzdGtleQ==".to_string()));
148
149        // Test azure_storage_access_key alias
150        let json = r#"{"container": "test", "azure_storage_access_key": "dGVzdGtleQ=="}"#;
151        let config: AzblobConfig = serde_json::from_str(json).unwrap();
152        assert_eq!(config.account_key, Some("dGVzdGtleQ==".to_string()));
153
154        // Test azure_storage_master_key alias
155        let json = r#"{"container": "test", "azure_storage_master_key": "dGVzdGtleQ=="}"#;
156        let config: AzblobConfig = serde_json::from_str(json).unwrap();
157        assert_eq!(config.account_key, Some("dGVzdGtleQ==".to_string()));
158
159        // Test access_key alias
160        let json = r#"{"container": "test", "access_key": "dGVzdGtleQ=="}"#;
161        let config: AzblobConfig = serde_json::from_str(json).unwrap();
162        assert_eq!(config.account_key, Some("dGVzdGtleQ==".to_string()));
163
164        // Test master_key alias
165        let json = r#"{"container": "test", "master_key": "dGVzdGtleQ=="}"#;
166        let config: AzblobConfig = serde_json::from_str(json).unwrap();
167        assert_eq!(config.account_key, Some("dGVzdGtleQ==".to_string()));
168    }
169
170    #[test]
171    fn test_sas_token_aliases() {
172        // Test original sas_token field
173        let json = r#"{"container": "test", "sas_token": "test-token"}"#;
174        let config: AzblobConfig = serde_json::from_str(json).unwrap();
175        assert_eq!(config.sas_token, Some("test-token".to_string()));
176
177        // Test azure_storage_sas_key alias
178        let json = r#"{"container": "test", "azure_storage_sas_key": "test-token"}"#;
179        let config: AzblobConfig = serde_json::from_str(json).unwrap();
180        assert_eq!(config.sas_token, Some("test-token".to_string()));
181
182        // Test azure_storage_sas_token alias
183        let json = r#"{"container": "test", "azure_storage_sas_token": "test-token"}"#;
184        let config: AzblobConfig = serde_json::from_str(json).unwrap();
185        assert_eq!(config.sas_token, Some("test-token".to_string()));
186
187        // Test sas_key alias
188        let json = r#"{"container": "test", "sas_key": "test-token"}"#;
189        let config: AzblobConfig = serde_json::from_str(json).unwrap();
190        assert_eq!(config.sas_token, Some("test-token".to_string()));
191    }
192
193    #[test]
194    fn test_endpoint_aliases() {
195        // Test original endpoint field
196        let json = r#"{"container": "test", "endpoint": "https://test.blob.core.windows.net"}"#;
197        let config: AzblobConfig = serde_json::from_str(json).unwrap();
198        assert_eq!(
199            config.endpoint,
200            Some("https://test.blob.core.windows.net".to_string())
201        );
202
203        // Test azure_storage_endpoint alias
204        let json = r#"{"container": "test", "azure_storage_endpoint": "https://test.blob.core.windows.net"}"#;
205        let config: AzblobConfig = serde_json::from_str(json).unwrap();
206        assert_eq!(
207            config.endpoint,
208            Some("https://test.blob.core.windows.net".to_string())
209        );
210
211        // Test azure_endpoint alias
212        let json =
213            r#"{"container": "test", "azure_endpoint": "https://test.blob.core.windows.net"}"#;
214        let config: AzblobConfig = serde_json::from_str(json).unwrap();
215        assert_eq!(
216            config.endpoint,
217            Some("https://test.blob.core.windows.net".to_string())
218        );
219    }
220}