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
52 changes: 52 additions & 0 deletions atomic-cli/src/commands/identity/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ impl Register {
.get("base_url")
.and_then(|v| v.as_str())
.unwrap_or("(unknown)");
// Single-tenant deployments report `mode: "single"` (field added
// server-side in v1.4.0; absent on older servers = multi-tenant).
// On a single-tenant server the typed URL IS the tenant URL —
// no `{org}.` prefixing — so the profile must keep it verbatim
// and mark the server single-tenant.
let (single_tenant, role) = parse_tenancy_mode(data);

let clean_server_url = self.server_url.trim_end_matches('/').to_string();
let mut config = GlobalConfig::load().map_err(|e| {
Expand All @@ -223,6 +229,7 @@ impl Register {
default_org: Some(slug.to_string()),
default_workspaces: std::collections::BTreeMap::new(),
identity: Some(identity.name.clone()),
single_tenant,
};

config.servers.insert(profile_name.clone(), profile);
Expand All @@ -248,6 +255,7 @@ impl Register {
// Default path: update the legacy [server] block.
config.server.url = Some(clean_server_url.clone());
config.server.default_org = Some(slug.to_string());
config.server.single_tenant = single_tenant;
}

config.save().map_err(|e| {
Expand All @@ -260,6 +268,12 @@ impl Register {
println!(" Slug: {slug}");
println!(" URL: {base_url}");
println!(" Identity: {} ({})", identity.name, identity.id.short());
if single_tenant {
println!(
" Mode: single-tenant (role: {})",
role.unwrap_or("member")
);
}

if slug != username {
println!();
Expand Down Expand Up @@ -356,6 +370,18 @@ fn derive_profile_name(server_url: &str) -> Option<String> {
}
}

/// Interpret the registration response's tenancy signal.
///
/// `mode: "single"` (server v1.4.0+) marks a single-tenant deployment and
/// carries the org `role` the identity received; anything else — `"multi"`
/// or a missing field (older servers) — keeps multi-tenant URL semantics.
/// Returns `(single_tenant, role)`.
fn parse_tenancy_mode(data: &serde_json::Value) -> (bool, Option<&str>) {
let single = data.get("mode").and_then(|v| v.as_str()) == Some("single");
let role = data.get("role").and_then(|v| v.as_str());
(single, role)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -385,6 +411,32 @@ mod tests {
assert_eq!(endpoint_b, "https://atomic.storage/register");
}

#[test]
fn tenancy_mode_single_with_role() {
let data = serde_json::json!({"mode": "single", "role": "owner"});
let (single, role) = parse_tenancy_mode(&data);
assert!(single);
assert_eq!(role, Some("owner"));
}

#[test]
fn tenancy_mode_multi() {
let data = serde_json::json!({"mode": "multi"});
let (single, role) = parse_tenancy_mode(&data);
assert!(!single);
assert_eq!(role, None);
}

#[test]
fn tenancy_mode_missing_field_defaults_multi() {
// Legacy server responses (pre-mode field) must not flip profile
// semantics — they keep org-prefixed URLs.
let data = serde_json::json!({"slug": "alice", "base_url": "https://alice.atomic.storage"});
let (single, role) = parse_tenancy_mode(&data);
assert!(!single);
assert_eq!(role, None);
}

#[test]
fn command_fields() {
let cmd = Register {
Expand Down
3 changes: 3 additions & 0 deletions atomic-cli/src/commands/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ impl ServerCmd {
default_org: add.org.clone(),
default_workspaces: std::collections::BTreeMap::new(),
identity: add.identity.clone(),
// Auto-detected at registration; manual profiles default to
// multi-tenant URL semantics.
single_tenant: false,
};

config.servers.insert(add.name.clone(), profile);
Expand Down
84 changes: 84 additions & 0 deletions atomic-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ pub struct ServerConfig {
/// Example: `identity = "alice-staging"`
#[serde(default, skip_serializing_if = "Option::is_none")]
pub identity: Option<String>,

/// Whether the server is a single-tenant deployment.
///
/// Single-tenant servers (reported by the registration response's
/// `mode = "single"`) serve exactly one organization at their bare host:
/// org-scoped URLs are NOT prefixed with the org slug — every path is
/// already scoped to the single tenant server-side.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub single_tenant: bool,
}

impl ServerConfig {
Expand All @@ -123,9 +132,18 @@ impl ServerConfig {
///
/// Given `url = "http://localhost:8080"` and `org = "alice"`,
/// returns `"http://alice.localhost:8080"`.
///
/// For single-tenant servers the org is already implied by the server
/// itself, so the URL is returned unchanged (no `{org}.` prefix).
pub fn org_base_url(&self, org_slug: &str) -> Option<String> {
let url = self.url.as_ref()?;

// Single-tenant: the bare host is already tenant-scoped server-side;
// prefixing would produce bogus hosts like `org.org.org.example.com`.
if self.single_tenant {
return Some(url.trim_end_matches('/').to_string());
}

// Parse the URL to extract scheme, host, port
let url_parsed = url::Url::parse(url).ok()?;
let scheme = url_parsed.scheme();
Expand Down Expand Up @@ -465,6 +483,7 @@ mod tests {
default_org: Some("alice".to_string()),
default_workspaces: BTreeMap::new(),
identity: None,
single_tenant: false,
};
assert!(config.is_configured());

Expand All @@ -474,6 +493,7 @@ mod tests {
default_org: None,
default_workspaces: BTreeMap::new(),
identity: None,
single_tenant: false,
};
assert!(!partial.is_configured());

Expand All @@ -483,6 +503,7 @@ mod tests {
default_org: Some("alice".to_string()),
default_workspaces: BTreeMap::new(),
identity: None,
single_tenant: false,
};
assert!(!partial.is_configured());
}
Expand All @@ -494,6 +515,7 @@ mod tests {
default_org: Some("alice".to_string()),
default_workspaces: BTreeMap::new(),
identity: None,
single_tenant: false,
};
assert_eq!(
config.org_base_url("alice"),
Expand All @@ -512,6 +534,7 @@ mod tests {
default_org: None,
default_workspaces: BTreeMap::new(),
identity: None,
single_tenant: false,
};
assert_eq!(
config.org_base_url("alice"),
Expand All @@ -526,6 +549,7 @@ mod tests {
default_org: Some("alice".to_string()),
default_workspaces: BTreeMap::new(),
identity: None,
single_tenant: false,
};
assert_eq!(
config.default_org_base_url(),
Expand All @@ -538,6 +562,7 @@ mod tests {
default_org: None,
default_workspaces: BTreeMap::new(),
identity: None,
single_tenant: false,
};
assert!(config.default_org_base_url().is_none());
}
Expand All @@ -549,6 +574,7 @@ mod tests {
default_org: Some("alice".to_string()),
default_workspaces: BTreeMap::new(),
identity: None,
single_tenant: false,
};

let toml_str = toml::to_string_pretty(&config).unwrap();
Expand All @@ -573,6 +599,7 @@ mod tests {
default_org: Some("alice".to_string()),
default_workspaces: BTreeMap::new(),
identity: None,
single_tenant: false,
},
..GlobalConfig::default()
};
Expand All @@ -597,6 +624,7 @@ mod tests {
default_org: Some("alice".to_string()),
default_workspaces: BTreeMap::new(),
identity: None,
single_tenant: false,
};
let toml_str = toml::to_string_pretty(&config).unwrap();
assert!(!toml_str.contains("default_workspaces"));
Expand All @@ -613,6 +641,7 @@ mod tests {
default_org: Some("alice".to_string()),
default_workspaces: workspaces,
identity: None,
single_tenant: false,
};

let toml_str = toml::to_string_pretty(&config).unwrap();
Expand Down Expand Up @@ -665,4 +694,59 @@ email = "test@example.com"
assert!(parsed.server.url.is_none());
assert!(parsed.server.default_org.is_none());
}

#[test]
fn test_org_base_url_single_tenant_not_prefixed() {
let config = ServerConfig {
url: Some("https://storage.acme.com".to_string()),
default_org: Some("acme".to_string()),
default_workspaces: BTreeMap::new(),
identity: None,
single_tenant: true,
};
// Single-tenant: the bare host is already tenant-scoped — no org prefix.
assert_eq!(
config.org_base_url("acme").as_deref(),
Some("https://storage.acme.com")
);
// Any org slug returns the bare host unchanged.
assert_eq!(
config.org_base_url("anything-else").as_deref(),
Some("https://storage.acme.com")
);
assert_eq!(
config.default_org_base_url().as_deref(),
Some("https://storage.acme.com")
);
}

#[test]
fn test_org_base_url_single_tenant_strips_trailing_slash() {
let config = ServerConfig {
url: Some("https://storage.acme.com/".to_string()),
default_org: Some("acme".to_string()),
default_workspaces: BTreeMap::new(),
identity: None,
single_tenant: true,
};
assert_eq!(
config.org_base_url("acme").as_deref(),
Some("https://storage.acme.com")
);
}

#[test]
fn test_single_tenant_defaults_false_for_legacy_configs() {
// Configs written before the field existed must deserialize as
// multi-tenant (org-prefixed) — no migration needed.
let legacy = r#"url = "https://atomic.storage"
default_org = "alice"
"#;
let config: ServerConfig = toml::from_str(legacy).unwrap();
assert!(!config.single_tenant);
assert_eq!(
config.org_base_url("alice").as_deref(),
Some("https://alice.atomic.storage")
);
}
}
Loading