Skip to content

Commit

Permalink
chore: random password function
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Jul 4, 2024
1 parent 60ac1bf commit cf7e7ba
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ serde = { version = "1.0.203", features = ["derive"] }
serde_yaml = "0.9.34+deprecated"
tokio = { version = "1.38.0", features = ["macros", "rt"] }
vaultrs = "0.7.2"
rand = "0.9.0-alpha.1"
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::workflow::switch_active_users;

mod cli;
mod config;
mod password;
mod vault;
mod workflow;

Expand Down
31 changes: 31 additions & 0 deletions src/password.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use rand::{Rng, thread_rng};
use rand::distributions::Alphanumeric;

fn generate_random_password(length: usize) -> String {
let mut rng = thread_rng();
let password: String = (0..length)
.map(|_| rng.sample(Alphanumeric) as char)
.collect();
password
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_password_length() {
for length in vec![8, 16, 32] {
let password = generate_random_password(length);
assert_eq!(password.len(), length);
}
}

#[test]
fn test_password_content() {
let password = generate_random_password(10);
assert!(password.chars().all(|c| c.is_alphanumeric()));
assert!(password.chars().any(|c| c.is_lowercase()));
assert!(password.chars().any(|c| c.is_uppercase()));
}
}
7 changes: 2 additions & 5 deletions src/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,10 @@ fn get_vault_client(config: &Config) -> VaultClient {
}

mod test {
use std::env;

use super::*;
use crate::config::PostgresConfig;
use vaultrs::client::Client;

use crate::config::{Config, PostgresConfig, VaultConfig};
use crate::vault::{get_vault_client, Vault, VAULT_TOKEN};

#[test]
fn test_vault_connect() {
let config = Config {
Expand Down

0 comments on commit cf7e7ba

Please sign in to comment.