Skip to content

Commit 7206f71

Browse files
committed
feat: allow loading setup user credentials, remove unused config fields
1 parent 8058b21 commit 7206f71

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

src/database.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use crate::CliDatabaseConfiguration;
44

55
pub struct CliDatabaseProvider {
66
pub config: CliDatabaseConfiguration,
7+
pub username: String,
8+
pub password: String,
79
}
810

911
impl docbox_management::database::DatabaseProvider for CliDatabaseProvider {
@@ -14,8 +16,8 @@ impl docbox_management::database::DatabaseProvider for CliDatabaseProvider {
1416
let options = PgConnectOptions::new()
1517
.host(&self.config.host)
1618
.port(self.config.port)
17-
.username(&self.config.setup_user.username)
18-
.password(&self.config.setup_user.password)
19+
.username(&self.username)
20+
.password(&self.password)
1921
.database(database);
2022

2123
PgPool::connect_with(options)

src/main.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ pub struct CliConfiguration {
5151
pub struct CliDatabaseConfiguration {
5252
pub host: String,
5353
pub port: u16,
54-
pub setup_user: CliDatabaseSetupUserConfig,
54+
pub setup_user: Option<CliDatabaseSetupUserConfig>,
55+
pub setup_user_secret_name: Option<String>,
5556
pub root_secret_name: String,
56-
pub root_role_name: String,
57-
pub root_secret_password: String,
5857
}
5958

6059
#[derive(Clone, Deserialize)]
6160
pub struct CliDatabaseSetupUserConfig {
61+
#[serde(alias = "user")]
6262
pub username: String,
6363
pub password: String,
6464
}
@@ -182,8 +182,37 @@ async fn main() -> eyre::Result<()> {
182182
let search_factory =
183183
SearchIndexFactory::from_config(&aws_config, config.search.clone()).map_err(AnyhowError)?;
184184
let storage_factory = StorageLayerFactory::from_config(&aws_config, config.storage.clone());
185-
let db_provider = CliDatabaseProvider {
186-
config: config.database.clone(),
185+
186+
let db_provider = match (
187+
config.database.setup_user.as_ref(),
188+
config.database.setup_user_secret_name.as_deref(),
189+
) {
190+
(Some(setup_user), _) => CliDatabaseProvider {
191+
config: config.database.clone(),
192+
username: setup_user.username.clone(),
193+
password: setup_user.password.clone(),
194+
},
195+
(_, Some(setup_user_secret_name)) => {
196+
let secret: CliDatabaseSetupUserConfig = secrets
197+
.parsed_secret(setup_user_secret_name)
198+
.await
199+
.map_err(AnyhowError)
200+
.context("failed to get setup user database secret")?
201+
.context("setup user database secret not found")?;
202+
203+
tracing::debug!("loaded database secrets from secret manager");
204+
205+
CliDatabaseProvider {
206+
config: config.database.clone(),
207+
username: secret.username.clone(),
208+
password: secret.password.clone(),
209+
}
210+
}
211+
(None, None) => {
212+
return Err(eyre::eyre!(
213+
"must provided either setup_user or setup_user_secret_name in database config"
214+
));
215+
}
187216
};
188217

189218
match command {

0 commit comments

Comments
 (0)