Skip to content

Commit

Permalink
fix(clippy): removed code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Jul 12, 2024
1 parent 61985e8 commit cde4ffc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
# args: -- -D warnings
args: -- -D warnings
- name: Unit and Integration Tests
uses: actions-rs/cargo@v1
env:
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(crate) fn read_config(config_path: PathBuf) -> Config {

let mut config_data: String = String::new();
let mut config_file: File = File::open(config_path)
.expect(format!("Failed to read configuration file: '{path_string}'").as_str());
.unwrap_or_else(|_| panic!("Failed to read configuration file: '{path_string}'"));
config_file
.read_to_string(&mut config_data)
.expect("Failed to read configuration file");
Expand Down
6 changes: 3 additions & 3 deletions src/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use vaultrs::kv2;

use crate::config::{Config, VaultConfig};

const VAULT_TOKEN: &'static str = "VAULT_TOKEN";
const VAULT_TOKEN: &str = "VAULT_TOKEN";

#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct VaultStructure {
Expand Down Expand Up @@ -67,7 +67,7 @@ impl Vault {
self.rt.block_on(kv2::read(
&self.vault_client,
"secret",
&*self.vault_config.path,
&self.vault_config.path,
))
}

Expand All @@ -78,7 +78,7 @@ impl Vault {
self.rt.block_on(kv2::set(
&self.vault_client,
"secret",
&*self.vault_config.path,
&self.vault_config.path,
&vault_structure,
))
}
Expand Down
24 changes: 16 additions & 8 deletions src/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(crate) fn rotate_secrets_using_switch_method(
let vault_path = config.vault.clone().path;
let mut secret: VaultStructure = vault
.read_secret()
.expect(format!("Failed to read path '{vault_path}' - did you init Vault?").as_str());
.unwrap_or_else(|_| panic!("Failed to read path '{vault_path}' - did you init Vault?"));

if secret.postgresql_active_user != secret.postgresql_user_1
&& secret.postgresql_active_user != secret.postgresql_user_2
Expand Down Expand Up @@ -52,11 +52,19 @@ pub(crate) fn rotate_secrets_using_switch_method(

fn switch_active_user(secret: &mut VaultStructure) {
if secret.postgresql_active_user == secret.postgresql_user_1 {
secret.postgresql_active_user = secret.postgresql_user_2.clone();
secret.postgresql_active_user_password = secret.postgresql_user_2_password.clone()
secret
.postgresql_active_user
.clone_from(&secret.postgresql_user_2);
secret
.postgresql_active_user_password
.clone_from(&secret.postgresql_user_2_password);
} else {
secret.postgresql_active_user = secret.postgresql_user_1.clone();
secret.postgresql_active_user_password = secret.postgresql_user_1_password.clone()
secret
.postgresql_active_user
.clone_from(&secret.postgresql_user_1);
secret
.postgresql_active_user_password
.clone_from(&secret.postgresql_user_1_password);
}

trace!("Switched active and passive user in Vault secret (locally)")
Expand All @@ -70,19 +78,19 @@ fn update_passive_user_postgres_password(
let (passive_user, passive_user_password) =
if secret.postgresql_active_user == secret.postgresql_user_1 {
let original_password = secret.postgresql_user_2_password.clone();
secret.postgresql_user_2_password = new_password.clone();
secret.postgresql_user_2_password.clone_from(&new_password);
(secret.postgresql_user_2.clone(), original_password)
} else {
let original_password = secret.postgresql_user_1_password.clone();
secret.postgresql_user_1_password = new_password.clone();
secret.postgresql_user_1_password.clone_from(&new_password);
(secret.postgresql_user_1.clone(), original_password)
};

let mut conn = db.connect_for_user(passive_user.clone(), passive_user_password);
let query = format!("ALTER ROLE {passive_user} WITH PASSWORD '{new_password}'");

conn.execute(query.as_str(), &[])
.expect(format!("Failed to update password of '{passive_user}'").as_str());
.unwrap_or_else(|_| panic!("Failed to update password of '{passive_user}'"));

debug!("Successfully rotated PostgreSQL password of passive user");
}
Expand Down

0 comments on commit cde4ffc

Please sign in to comment.