Skip to content

Commit

Permalink
Merge pull request #97 from yimiaoxiehou/master
Browse files Browse the repository at this point in the history
add RNACOS_ADMIN_USERNAME and RNACOS_ADMIN_PASSWORD env for support mod default admin username and password
  • Loading branch information
heqingpan authored May 30, 2024
2 parents b683d6d + 56522e3 commit 76c79db
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ nacos_rust_client = "0.3.0"
![](https://github.com/r-nacos/r-nacos/raw/master/doc/assets/imgs/20231223222325.png)

系统会默认创建一个名为`admin`的用户,密码为`admin`
系统会默认创建一个名为`admin`的用户,密码为`admin`(也可以通过环境变量 RNACOS_INIT_ADMIN_USERNAME 和 RNACOS_INIT_ADMIN_PASSWORD 修改默认账号的账户名和密码)

进去控制台后可按需管理用户。

Expand Down
6 changes: 6 additions & 0 deletions book/src/cluster_deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ env01
```
#file:env01 , Initialize with the leader node role
RUST_LOG=warn
#RNACOS_INIT_ADMIN_USERNAME=admin
#RNACOS_INIT_ADMIN_PASSWORD=admin
RNACOS_HTTP_PORT=8848
RNACOS_RAFT_NODE_ADDR=127.0.0.1:9848
RNACOS_CONFIG_DB_DIR=db01
Expand All @@ -83,6 +85,8 @@ env02:
```
#file:env02 , Initialize with the follower node role
RUST_LOG=warn
#RNACOS_INIT_ADMIN_USERNAME=admin
#RNACOS_INIT_ADMIN_PASSWORD=admin
RNACOS_HTTP_PORT=8849
RNACOS_RAFT_NODE_ADDR=127.0.0.1:9849
RNACOS_CONFIG_DB_DIR=db02
Expand All @@ -95,6 +99,8 @@ env03:
```
#file:env03 , Initialize with the follower node role
RUST_LOG=warn
#RNACOS_INIT_ADMIN_USERNAME=admin
#RNACOS_INIT_ADMIN_PASSWORD=admin
RNACOS_HTTP_PORT=8850
RNACOS_RAFT_NODE_ADDR=127.0.0.1:9850
RNACOS_CONFIG_DB_DIR=db03
Expand Down
2 changes: 2 additions & 0 deletions book/src/deplay_env.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ rnacos 运行时支持的环境变量,如果不设置则按默认配置运行

| 参数KEY|内容描述|默认值|示例|开始支持的版本|
|--|--|--|--|--|
|RNACOS_INIT_ADMIN_USERNAME|rnacos默认管理员用户名|admin|rnacos|0.5.11|
|RNACOS_INIT_ADMIN_PASSWORD|rnacos默认管理员密码|admin|rnacos123456|0.5.11|
|RNACOS_HTTP_PORT|rnacos监听http端口|8848|8848|0.1.x|
|RNACOS_GRPC_PORT|rnacos监听grpc端口|默认是 HTTP端口+1000|9848|0.1.x|
|RNACOS_HTTP_CONSOLE_PORT|r-nacos独立控制台端口|默认是 HTTP端口+2000;设置为0可不开启独立控制台|10848|0.4.x|
Expand Down
2 changes: 1 addition & 1 deletion book/src/quick-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ curl "http://127.0.0.1:8848/nacos/v1/ns/instance/list?&namespaceId=public&servic

![](https://github.com/heqingpan/rnacos/raw/master/doc/assets/imgs/20231223222325.png)

系统会默认创建一个名为`admin`的用户,密码为`admin`
系统会默认创建一个名为`admin`的用户,密码为`admin`(也可以通过环境变量 RNACOS_INIT_ADMIN_USERNAME 和 RNACOS_INIT_ADMIN_PASSWORD 修改默认账号的账户名和密码)

进去控制台后可按需管理用户。

Expand Down
6 changes: 6 additions & 0 deletions doc/conf/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
# 2. 指定文件(放置于任意目录下, 通过 命令行参数“-e 文件路径”形式指定, 如“./rnacos -e /etc/rnacos/conf/default.cnf”)
# 更多说明请参照 https://r-nacos.github.io/docs/notes/env_config

# r-nacos默认管理员用户名,默认值:admin
RNACOS_INIT_ADMIN_USERNAME=admin

# r-nacos默认管理员密码,默认值:admin
RNACOS_INIT_ADMIN_PASSWORD=admin

# r-nacos监听http端口,默认值:8848
RNACOS_HTTP_PORT=8848

Expand Down
8 changes: 8 additions & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ impl NamingSysConfig {

#[derive(Default, Clone, Debug)]
pub struct AppSysConfig {
pub admin_username: String,
pub admin_password: String,
pub config_db_file: String,
pub config_db_dir: String,
pub config_max_content: usize,
Expand All @@ -76,6 +78,10 @@ pub struct AppSysConfig {

impl AppSysConfig {
pub fn init_from_env() -> Self {
let admin_username = std::env::var("RNACOS_INIT_ADMIN_USERNAME")
.unwrap_or("admin".to_owned());
let admin_password = std::env::var("RNACOS_INIT_ADMIN_PASSWORD")
.unwrap_or("admin".to_owned());
let config_db_file =
std::env::var("RNACOS_CONFIG_DB_FILE").unwrap_or("config.db".to_owned());
let config_max_content = std::env::var("RNACOS_CONFIG_MAX_CONTENT")
Expand Down Expand Up @@ -146,6 +152,8 @@ impl AppSysConfig {
.map(Arc::new)
.unwrap_or(constant::EMPTY_ARC_STRING.clone());
Self {
admin_username,
admin_password,
config_db_dir,
config_db_file,
config_max_content,
Expand Down
8 changes: 5 additions & 3 deletions src/user/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::common::AppSysConfig;
use std::{sync::Arc, time::Duration};

use actix::prelude::*;
Expand Down Expand Up @@ -61,10 +62,11 @@ impl UserManager {
};
if let TableManagerResult::PageListResult(count, _) = table_manager.send(req).await?? {
if count == 0 {
let sys_config = AppSysConfig::init_from_env();
let user = UserDto {
username: Arc::new("admin".to_owned()),
nickname: Some("admin".to_owned()),
password: Some("admin".to_owned()),
username: Arc::new(sys_config.admin_username.to_string()),
nickname: Some(sys_config.admin_username.to_owned()),
password: Some(sys_config.admin_password.to_owned()),
roles: Some(vec![USER_ROLE_MANAGER.clone()]),
..Default::default()
};
Expand Down
6 changes: 6 additions & 0 deletions test_cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ start_cluster() {
cat >$env_file <<EOF
#file:env01
#RUST_LOG=debug|info|warn|error, default is info
#RNACOS_INIT_ADMIN_USERNAME=admin
#RNACOS_INIT_ADMIN_PASSWORD=admin
RNACOS_HTTP_PORT=8848
RNACOS_RAFT_NODE_ADDR=127.0.0.1:9848
RNACOS_CONFIG_DB_DIR=cluster_example/db_01
Expand All @@ -59,6 +61,8 @@ EOF
cat >$env_file <<EOF
#file:env02
#RUST_LOG=debug|info|warn|error, default is info
#RNACOS_INIT_ADMIN_USERNAME=admin
#RNACOS_INIT_ADMIN_PASSWORD=admin
RNACOS_HTTP_PORT=8849
RNACOS_RAFT_NODE_ADDR=127.0.0.1:9849
RNACOS_CONFIG_DB_DIR=cluster_example/db_02
Expand All @@ -74,6 +78,8 @@ EOF
#file:env03
#RUST_LOG=debug|info|warn|error, default is info
RUST_LOG=warn
#RNACOS_INIT_ADMIN_USERNAME=admin
#RNACOS_INIT_ADMIN_PASSWORD=admin
RNACOS_HTTP_PORT=8850
RNACOS_RAFT_NODE_ADDR=127.0.0.1:9850
RNACOS_CONFIG_DB_DIR=cluster_example/db_03
Expand Down

0 comments on commit 76c79db

Please sign in to comment.