Skip to content

Commit

Permalink
server: if authentication enabled, and no users, create one
Browse files Browse the repository at this point in the history
If authentication is enabled and there are no users, create an admin
user with username "admin" and a random password.

Issue: #274
  • Loading branch information
jasonish committed Jun 27, 2023
1 parent 163403d commit de611fa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 72 deletions.
82 changes: 12 additions & 70 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ md5 = "0.7.0"
mime_guess = "2.0.3"
nom = "7.1.0"
percent-encoding = "2.1.0"
rand = "0.7.3"
rand = "0.8.5"
reqwest = { version = "0.11.18", default-features = false, features = ["json", "rustls-tls", "rustls-tls-webpki-roots", "rustls-tls-native-roots"] }
rpassword = "4.0.5"
rust-embed = { version = "6.3.0", features = ["compression", "debug-embed"] }
Expand Down
20 changes: 19 additions & 1 deletion src/server/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ pub async fn main(args: &clap::ArgMatches) -> Result<()> {
let mut context = build_context(server_config.clone(), datastore).await?;

if server_config.authentication_required && !context.config_repo.has_users()? {
warn!("Username/password authentication is required, but no users exist");
warn!("Username/password authentication is required, but no users exist, creating a user");
let (username, password) = create_admin_user(&context)?;
warn!(
"Created administrator username and password: username={}, password={}",
username, password
);
}

if let Some(filename) = config_filename {
Expand Down Expand Up @@ -263,6 +268,19 @@ fn is_authentication_required(config: &Config) -> bool {
true
}

fn create_admin_user(context: &ServerContext) -> Result<(String, String)> {
use rand::Rng;
let rng = rand::thread_rng();
let username = "admin";
let password: String = rng
.sample_iter(&rand::distributions::Alphanumeric)
.take(12)
.map(char::from)
.collect();
context.config_repo.add_user(username, &password)?;
Ok((username.to_string(), password))
}

fn is_input_enabled(config: &Config) -> bool {
config.args.contains_id("input.filename")
|| config.get_bool("input.enabled").unwrap_or(false)
Expand Down

0 comments on commit de611fa

Please sign in to comment.