Skip to content

Commit f01abed

Browse files
authored
Changes: Add aliyun ram auth plugin demo and doc (#248)
* chore: Add example for aliyun ram auth plugin and improve the doc. * fix: remove invalid test case in auth_by_aliyun_ram
1 parent 90c8d49 commit f01abed

File tree

3 files changed

+173
-18
lines changed

3 files changed

+173
-18
lines changed

README.md

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ nacos-sdk = { version = "0.4", features = ["default"] }
3636
// Attention! "public" is "", it is recommended to customize the namespace with clear meaning.
3737
.namespace("")
3838
.app_name("simple_app"),
39-
// .auth_username("TODO")
40-
// .auth_password("TODO")
39+
.auth_username("username")
40+
.auth_password("password")
4141
)
42-
// .enable_auth_plugin_http()
42+
.enable_auth_plugin_http()
4343
.build()?;
4444

4545
// example get a config
@@ -79,10 +79,10 @@ nacos-sdk = { version = "0.4", features = ["default"] }
7979
// Attention! "public" is "", it is recommended to customize the namespace with clear meaning.
8080
.namespace("")
8181
.app_name("simple_app"),
82-
// .auth_username("TODO")
83-
// .auth_password("TODO")
82+
.auth_username("username")
83+
.auth_password("password")
8484
)
85-
// .enable_auth_plugin_http()
85+
.enable_auth_plugin_http()
8686
.build()?;
8787

8888
pub struct ExampleInstanceChangeListener;
@@ -121,6 +121,65 @@ See them in `nacos_sdk::api::props::ClientProps` or `nacos_sdk::api::constants::
121121
e.g.
122122
- env `NACOS_CLIENT_COMMON_THREAD_CORES` to set nacos-client-thread-pool num, default 1
123123
- env `NACOS_CLIENT_NAMING_PUSH_EMPTY_PROTECTION` for naming empty data notify protection, default true
124+
- env `NACOS_CLIENT_USERNAME` to set http auth username
125+
- env `NACOS_CLIENT_PASSWORD` to set http auth password
126+
- env `NACOS_CLIENT_ACCESS_KEY` to set Aliyun ram access-key
127+
- env `NACOS_CLIENT_SECRET_KEY` to set Aliyun ram access-secret
128+
129+
### AuthPlugin Features
130+
- > Set access-key, access-secret via Environment variables are recommended.
131+
- auth-by-http
132+
- support http auth via username and password
133+
- how to use
134+
- enable auth-by-http(default is enabled)
135+
```toml
136+
[dependencies]
137+
nacos-sdk = { version = "0.4", features = ["default"] }
138+
```
139+
- Set username and password via environment variables
140+
```shell
141+
export NACOS_CLIENT_USERNAME=you_username
142+
export NACOS_CLIENT_PASSWORD=you_password
143+
```
144+
- Enable auth-by-http in your code
145+
```rust
146+
ConfigServiceBuilder::new(
147+
ClientProps::new()
148+
.server_addr("localhost:8848"))
149+
.enable_auth_plugin_http()
150+
151+
NamingServiceBuilder::new(
152+
ClientProps::new()
153+
.server_addr("localhost:8848"))
154+
.enable_auth_plugin_http()
155+
.build()
156+
```
157+
- auth-by-aliyun
158+
- support aliyun ram auth via access-key and access-secret
159+
- how to use
160+
- enable auth-by-aliyun feature in toml
161+
```toml
162+
[dependencies]
163+
nacos-sdk = { version = "0.4", features = ["default", "auth-by-aliyun"] }
164+
```
165+
- Set accessKey and secretKey via environment variables
166+
```shell
167+
export NACOS_CLIENT_ACCESS_KEY=you_access_key
168+
export NACOS_CLIENT_SECRET_KEY=you_secret_key
169+
```
170+
- Enable aliyun ram auth plugin in code by enable_auth_plugin_aliyun
171+
```rust
172+
ConfigServiceBuilder::new(
173+
ClientProps::new()
174+
.server_addr("localhost:8848"))
175+
.enable_auth_plugin_aliyun()
176+
177+
NamingServiceBuilder::new(
178+
ClientProps::new()
179+
.server_addr("localhost:8848"))
180+
.enable_auth_plugin_aliyun()
181+
.build()
182+
```
124183

125184
## 开发说明
126185
- Build with `cargo build`
@@ -181,7 +240,7 @@ gRPC 交互的 Payload 和 Metadata 由 `Protocol Buffers` 序列化,具体的
181240
#### Common 通用能力
182241
- [x] 创建参数,自定义传参 + ENV 环境变量读取,后者优先级高;ENV 统一前缀,例如 `NACOS_CLIENT_CONFIG_*` 于配置管理, `NACOS_CLIENT_NAMING_*` 于服务注册
183242
- [x] 通用客户端请求交互,Request/Response 通用 gRPC 逻辑,提供给 Config/Naming
184-
- [x] Auth 鉴权;账密登陆 username/password,TODO accessKey/secretKey
243+
- [x] Auth 鉴权;账密登陆 username/password,阿里云RAM鉴权 accessKey/secretKey
185244
- [x] 通用日志,`tracing::info!()`
186245
- [ ] Monitor,`opentelemetry`
187246
- [ ] 数据落盘与加载(用于服务端宕机弱依赖)

examples/aliyun_ram_app.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
use nacos_sdk::api::config::{ConfigService, ConfigServiceBuilder};
2+
use nacos_sdk::api::naming::{NamingService, NamingServiceBuilder, ServiceInstance};
3+
use nacos_sdk::api::props::ClientProps;
4+
use std::time::Duration;
5+
use tokio::time::sleep;
6+
7+
/// Aliyun Ram plugin support
8+
///
9+
/// Notice:
10+
/// accessKey and secretKey are sensitive data, don't encode them in you code
11+
/// directly, inject it via environment variables are recommended.
12+
///
13+
/// Example run preparations:
14+
/// 1. inject you accessKey and secretKey via environment variables by following command
15+
/// export NACOS_CLIENT_ACCESS_KEY=you_access_key
16+
/// export NACOS_CLIENT_SECRET_KEY=you_secret_key
17+
///
18+
/// 2. run command
19+
/// cargo run --example aliyun_ram_app --features default,auth-by-aliyun
20+
21+
#[tokio::main]
22+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
23+
#[cfg(feature = "auth-by-aliyun")]
24+
run_config_demo().await;
25+
26+
#[cfg(feature = "auth-by-aliyun")]
27+
run_naming_demo().await;
28+
Ok(())
29+
}
30+
31+
#[cfg(feature = "auth-by-aliyun")]
32+
async fn run_naming_demo() {
33+
let server_addr = "localhost:8848";
34+
35+
/// NamingService
36+
let mut naming_client = NamingServiceBuilder::new(ClientProps::new().server_addr(server_addr))
37+
.enable_auth_plugin_aliyun()
38+
.build()
39+
.unwrap();
40+
41+
let mut instance = ServiceInstance::default();
42+
instance.ip = "localhost".to_string();
43+
instance.port = 8080;
44+
45+
println!("Register localhost:8080 to service(name: test, group: test)");
46+
naming_client
47+
.register_instance("test".to_owned(), Some("test".to_owned()), instance)
48+
.await
49+
.unwrap();
50+
51+
println!("Get All instance from service(name:test, group: test)");
52+
let instances = naming_client
53+
.get_all_instances(
54+
"test".to_string(),
55+
Some("test".to_string()),
56+
Vec::new(),
57+
false,
58+
)
59+
.await
60+
.unwrap();
61+
assert_eq!(1, instances.len());
62+
assert_eq!("localhost", instances[0].ip);
63+
assert_eq!(8080, instances[0].port);
64+
}
65+
66+
#[cfg(feature = "auth-by-aliyun")]
67+
async fn run_config_demo() {
68+
let server_addr = "localhost:8848";
69+
70+
/// Config service
71+
let mut config_client = ConfigServiceBuilder::new(ClientProps::new().server_addr(server_addr))
72+
.enable_auth_plugin_aliyun()
73+
.build()
74+
.unwrap();
75+
76+
println!(
77+
"Publish config dataId = {}, group = {}, content = {}",
78+
"test", "test", "test=test"
79+
);
80+
config_client
81+
.publish_config(
82+
"test".to_string(),
83+
"test".to_string(),
84+
"test=test".to_string(),
85+
Some("properties".to_string()),
86+
)
87+
.await
88+
.unwrap();
89+
90+
println!("Waiting...");
91+
sleep(Duration::from_secs(5)).await;
92+
93+
let response = config_client
94+
.get_config("test".to_string(), "test".to_string())
95+
.await
96+
.unwrap();
97+
println!(
98+
"Get config from nacos for dataId = {}, group = {}, content = {}",
99+
"test",
100+
"test",
101+
response.content()
102+
);
103+
assert_eq!("test=test", response.content());
104+
assert_eq!("properties", response.content_type());
105+
assert_eq!("test", response.group());
106+
assert_eq!("test", response.data_id());
107+
}

src/api/plugin/auth/auth_by_aliyun_ram.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,6 @@ mod test {
495495
);
496496
}
497497

498-
#[test]
499-
fn test_final_signing_key_string_with_default_info() {
500-
let sign_data = calculate_v4_signing_key_util::final_signing_key_string_with_default_info(
501-
"test",
502-
"cn-hangzhou",
503-
);
504-
assert_eq!("lHVX6NEPs3+EKxO3g2iklCwbseQnAWz5nLce9Lm0Po4=", sign_data)
505-
}
506-
507498
struct TestNamingEventListener {
508499
instance_now: ArcSwap<Vec<ServiceInstance>>,
509500
}
@@ -527,8 +518,6 @@ mod test {
527518
ClientProps::new()
528519
.namespace(std::env::var("NAMESPACE").unwrap_or("".to_string()))
529520
.server_addr(std::env::var("SERVER_ADDR").unwrap())
530-
.auth_ext(ACCESS_KEY, std::env::var("AK").unwrap())
531-
.auth_ext(ACCESS_SECRET, std::env::var("SK").unwrap())
532521
}
533522

534523
fn make_service_instance(ip: &str, port: i32) -> ServiceInstance {

0 commit comments

Comments
 (0)