Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

40 changes: 40 additions & 0 deletions crates/openshell-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,35 @@ use std::path::PathBuf;
use std::str::FromStr;
use std::time::Duration;

/// Database credential source. The default preserves URL/SQLite behavior.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum DatabaseAuthMode {
#[default]
Default,
AzureWorkloadIdentity,
}

impl fmt::Display for DatabaseAuthMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Default => "default",
Self::AzureWorkloadIdentity => "azure-workload-identity",
})
}
}

impl FromStr for DatabaseAuthMode {
type Err = &'static str;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"default" => Ok(Self::Default),
"azure-workload-identity" => Ok(Self::AzureWorkloadIdentity),
_ => Err("database authentication must be default or azure-workload-identity"),
}
}
}

// ── Public default constants ────────────────────────────────────────────
//
// Canonical source for default values used across multiple crates.
Expand Down Expand Up @@ -301,6 +330,9 @@ pub struct Config {
/// Database URL for persistence.
pub database_url: String,

/// Explicit database identity mode; independent of provider credentials.
pub database_auth: DatabaseAuthMode,

/// Connection ceiling for the persistence pool.
///
/// `None` leaves the backend's built-in default in place. The right
Expand Down Expand Up @@ -1084,6 +1116,7 @@ impl Config {
mtls_auth: MtlsAuthConfig::default(),
gateway_jwt: None,
database_url: String::new(),
database_auth: DatabaseAuthMode::default(),
database_max_connections: None,
database_lock_max_connections: None,
compute_driver: None,
Expand Down Expand Up @@ -1144,6 +1177,13 @@ impl Config {
self
}

/// Select database credentials independently of the connection URL.
#[must_use]
pub fn with_database_auth(mut self, mode: DatabaseAuthMode) -> Self {
self.database_auth = mode;
self
}

/// Create a new configuration with a database URL.
#[must_use]
pub fn with_database_url(mut self, url: impl Into<String>) -> Self {
Expand Down
1 change: 1 addition & 0 deletions crates/openshell-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ sha2 = { workspace = true }
jsonwebtoken = { workspace = true }
spiffe = { workspace = true }
async-trait = "0.1"
async-stream = "0.3"
url = { workspace = true }
glob = { workspace = true }
hex = "0.4"
Expand Down
40 changes: 39 additions & 1 deletion crates/openshell-server/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ struct RunArgs {
#[arg(long, env = "OPENSHELL_DB_URL")]
db_url: Option<String>,

/// Database identity source: default or azure-workload-identity.
#[arg(long, env = "OPENSHELL_DB_AUTH")]
db_auth: Option<openshell_core::config::DatabaseAuthMode>,

/// Connection ceiling for the database pool, shared by every
/// database-backed request.
///
Expand Down Expand Up @@ -629,6 +633,7 @@ fn prepare_server_config_with_drivers(

config = config
.with_database_url(db_url)
.with_database_auth(args.db_auth.unwrap_or_default())
.with_database_max_connections(args.db_max_connections)
.with_database_lock_max_connections(args.db_lock_max_connections);
if let Some(driver) = &args.compute_driver {
Expand Down Expand Up @@ -1698,6 +1703,12 @@ fn merge_file_into_args(args: &mut RunArgs, file: &GatewayFileSection, matches:
{
args.grpc_rate_limit_window_seconds = Some(window);
}
if let Some(mode) = file.database_auth
&& args.db_auth.is_none()
&& arg_defaulted(matches, "db_auth")
{
args.db_auth = Some(mode);
}
if let Some(max_connections) = file.database_max_connections
&& args.db_max_connections.is_none()
&& arg_defaulted(matches, "db_max_connections")
Expand Down Expand Up @@ -1801,7 +1812,7 @@ mod tests {
use super::{Cli, command};
use crate::test_environment::Environment;
use clap::Parser;
use openshell_core::config::ServiceHostnameFormat;
use openshell_core::config::{DatabaseAuthMode, ServiceHostnameFormat};
use std::net::{IpAddr, Ipv4Addr};
use std::sync::atomic::{AtomicUsize, Ordering};

Expand Down Expand Up @@ -4468,6 +4479,33 @@ grpc_rate_limit_window_seconds = 30
assert_eq!(args.grpc_rate_limit_window_seconds, Some(30));
}

#[test]
fn database_auth_file_and_explicit_overrides() {
Environment::new().remove("OPENSHELL_DB_AUTH").run(|| {
let file = config_file_from_toml(
"[openshell.gateway]\ndatabase_auth = \"azure-workload-identity\"\n",
);
let (mut args, matches) = parse_with_args(&["openshell-gateway"]);
assert_eq!(args.db_auth, None);
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
assert_eq!(args.db_auth, Some(DatabaseAuthMode::AzureWorkloadIdentity));
let (mut args, matches) =
parse_with_args(&["openshell-gateway", "--db-auth", "default"]);
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
assert_eq!(args.db_auth, Some(DatabaseAuthMode::Default));
});
Environment::new()
.set("OPENSHELL_DB_AUTH", "default")
.run(|| {
let file = config_file_from_toml(
"[openshell.gateway]\ndatabase_auth = \"azure-workload-identity\"\n",
);
let (mut args, matches) = parse_with_args(&["openshell-gateway"]);
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
assert_eq!(args.db_auth, Some(DatabaseAuthMode::Default));
});
}

#[test]
fn file_db_max_connections_populates_args_when_cli_omits() {
Environment::new()
Expand Down
3 changes: 3 additions & 0 deletions crates/openshell-server/src/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ pub struct GatewayFileSection {
pub log_level: Option<String>,

// ── Database ─────────────────────────────────────────────────────────
/// Explicit identity source; connection URL stays environment-only.
#[serde(default)]
pub database_auth: Option<openshell_core::config::DatabaseAuthMode>,
/// Connection ceiling for the persistence pool. One shared pool serves
/// every database-backed RPC, so this is also the gateway's ceiling on
/// concurrent database work. On Postgres, each replica also opens up to
Expand Down
3 changes: 2 additions & 1 deletion crates/openshell-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1631,10 +1631,11 @@ async fn connect_store(
let mut delay = DATABASE_CONNECT_RETRY_INITIAL_DELAY;
let mut waiting = false;
loop {
let attempt = Store::try_connect_with_pool_sizes(
let attempt = Store::try_connect_with_database_auth(
database_url,
config.database_max_connections,
config.database_lock_max_connections,
config.database_auth,
);
let result = if waiting {
tokio::select! {
Expand Down
Loading
Loading