Skip to main content

rocketmq_auth/
config.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use cheetah_string::CheetahString;
16
17#[derive(Clone, Debug)]
18pub struct AuthConfig {
19    pub config_name: CheetahString,
20    pub cluster_name: CheetahString,
21    pub auth_config_path: CheetahString,
22
23    pub authentication_enabled: bool,
24    pub authentication_provider: CheetahString,
25    pub authentication_metadata_provider: CheetahString,
26    pub authentication_strategy: CheetahString,
27    pub authentication_whitelist: CheetahString,
28    pub init_authentication_user: CheetahString,
29    pub inner_client_authentication_credentials: CheetahString,
30
31    pub authorization_enabled: bool,
32    pub authorization_provider: CheetahString,
33    pub authorization_metadata_provider: CheetahString,
34    pub authorization_strategy: CheetahString,
35    pub authorization_whitelist: CheetahString,
36
37    pub migrate_auth_from_v1_enabled: bool,
38
39    pub user_cache_max_num: u32,
40    pub user_cache_expired_second: u32,
41    pub user_cache_refresh_second: u32,
42
43    pub acl_cache_max_num: u32,
44    pub acl_cache_expired_second: u32,
45    pub acl_cache_refresh_second: u32,
46
47    pub stateful_authentication_cache_max_num: u32,
48    pub stateful_authentication_cache_expired_second: u32,
49    pub stateful_authorization_cache_max_num: u32,
50    pub stateful_authorization_cache_expired_second: u32,
51}
52
53impl Default for AuthConfig {
54    fn default() -> Self {
55        Self {
56            config_name: CheetahString::new(),
57            cluster_name: CheetahString::new(),
58            auth_config_path: CheetahString::new(),
59
60            authentication_enabled: false,
61            authentication_provider: CheetahString::new(),
62            authentication_metadata_provider: CheetahString::new(),
63            authentication_strategy: CheetahString::new(),
64            authentication_whitelist: CheetahString::new(),
65            init_authentication_user: CheetahString::new(),
66            inner_client_authentication_credentials: CheetahString::new(),
67
68            authorization_enabled: false,
69            authorization_provider: CheetahString::new(),
70            authorization_metadata_provider: CheetahString::new(),
71            authorization_strategy: CheetahString::new(),
72            authorization_whitelist: CheetahString::new(),
73
74            migrate_auth_from_v1_enabled: false,
75
76            user_cache_max_num: 1000,
77            user_cache_expired_second: 600,
78            user_cache_refresh_second: 60,
79
80            acl_cache_max_num: 1000,
81            acl_cache_expired_second: 600,
82            acl_cache_refresh_second: 60,
83
84            stateful_authentication_cache_max_num: 10000,
85            stateful_authentication_cache_expired_second: 60,
86            stateful_authorization_cache_max_num: 10000,
87            stateful_authorization_cache_expired_second: 60,
88        }
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    #[test]
97    fn test_auth_config_default_values() {
98        let config = AuthConfig::default();
99
100        // String fields
101        assert!(config.config_name.is_empty());
102        assert!(config.cluster_name.is_empty());
103        assert!(config.auth_config_path.is_empty());
104
105        assert!(config.authentication_provider.is_empty());
106        assert!(config.authentication_metadata_provider.is_empty());
107        assert!(config.authentication_strategy.is_empty());
108        assert!(config.authentication_whitelist.is_empty());
109        assert!(config.init_authentication_user.is_empty());
110        assert!(config.inner_client_authentication_credentials.is_empty());
111
112        assert!(config.authorization_provider.is_empty());
113        assert!(config.authorization_metadata_provider.is_empty());
114        assert!(config.authorization_strategy.is_empty());
115        assert!(config.authorization_whitelist.is_empty());
116
117        // Boolean fields
118        assert!(!config.authentication_enabled);
119        assert!(!config.authorization_enabled);
120        assert!(!config.migrate_auth_from_v1_enabled);
121
122        // Cache defaults
123        assert_eq!(config.user_cache_max_num, 1000);
124        assert_eq!(config.user_cache_expired_second, 600);
125        assert_eq!(config.user_cache_refresh_second, 60);
126
127        assert_eq!(config.acl_cache_max_num, 1000);
128        assert_eq!(config.acl_cache_expired_second, 600);
129        assert_eq!(config.acl_cache_refresh_second, 60);
130
131        assert_eq!(config.stateful_authentication_cache_max_num, 10000);
132        assert_eq!(config.stateful_authentication_cache_expired_second, 60);
133        assert_eq!(config.stateful_authorization_cache_max_num, 10000);
134        assert_eq!(config.stateful_authorization_cache_expired_second, 60);
135    }
136}