Skip to main content

docbox_management/
config.rs

1use aws_config::SdkConfig;
2use docbox_core::{
3    search::SearchIndexFactoryConfig,
4    secrets::{
5        SecretManager, SecretManagerError, SecretsManagerConfig,
6        aws::{AwsSecretManagerConfig, AwsSecretsManagerConfigError},
7    },
8    storage::StorageLayerFactoryConfig,
9};
10use serde::{Deserialize, Serialize};
11use thiserror::Error;
12
13/// Administrative database credentials configuration used for managing the database
14#[derive(Clone, Deserialize, Serialize)]
15pub struct AdminDatabaseConfiguration {
16    /// Host of the database
17    pub host: String,
18    /// Port of the database
19    pub port: u16,
20    /// Setup user configuration if using an inline one
21    pub setup_user: Option<AdminDatabaseSetupUserConfig>,
22    /// Name of the setup user secret if using one
23    pub setup_user_secret_name: Option<String>,
24
25    /// Name of the secrets manager secret to use when connecting to
26    /// the root "docbox" database if using secret based authentication
27    pub root_secret_name: Option<String>,
28
29    /// Whether to use IAM authentication instead of secret based
30    /// authentication for connecting to the root database
31    #[serde(default)]
32    pub root_iam: bool,
33}
34
35/// Setup user configuration
36#[derive(Clone, Deserialize, Serialize)]
37pub struct AdminDatabaseSetupUserConfig {
38    /// Username for the database account
39    #[serde(alias = "user")]
40    pub username: String,
41    /// Password for the database account
42    pub password: String,
43}
44
45/// Configuration for accessing the docbox API
46#[derive(Clone, Deserialize, Serialize)]
47pub struct ApiConfig {
48    /// URL of the docbox server
49    pub url: String,
50    /// API key to access the server with
51    pub api_key: Option<String>,
52}
53
54/// Configuration for operating on a docbox server
55#[derive(Clone, Deserialize, Serialize)]
56pub struct ServerConfigData {
57    /// Config for accessing the docbox API
58    pub api: ApiConfig,
59    /// Database configuration
60    pub database: AdminDatabaseConfiguration,
61    /// Secret manager configuration
62    pub secrets: SecretsManagerConfig,
63    /// Search index configuration
64    pub search: SearchIndexFactoryConfig,
65    /// Storage backend configuration
66    pub storage: StorageLayerFactoryConfig,
67}
68
69#[derive(Debug, Error)]
70pub enum ServerConfigDataSecretError {
71    #[error("failed to load secret manager from env: {0}")]
72    SecretManager(AwsSecretsManagerConfigError),
73
74    #[error("failed to load secret: {0}")]
75    Secret(SecretManagerError),
76
77    #[error("secret not found")]
78    SecretNotFound,
79}
80
81/// Load a [ServerConfigData] from the AWS secret manager
82pub async fn load_server_config_data_secret(
83    aws_config: &SdkConfig,
84    secret_name: &str,
85) -> Result<ServerConfigData, ServerConfigDataSecretError> {
86    let secrets = SecretManager::from_config(
87        aws_config,
88        SecretsManagerConfig::Aws(
89            AwsSecretManagerConfig::from_env()
90                .map_err(ServerConfigDataSecretError::SecretManager)?,
91        ),
92    );
93
94    secrets
95        .parsed_secret(secret_name)
96        .await
97        .map_err(ServerConfigDataSecretError::Secret)?
98        .ok_or(ServerConfigDataSecretError::SecretNotFound)
99}