diff --git a/Cargo.lock b/Cargo.lock index ca43328a..132b1f5b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -420,16 +420,19 @@ version = "0.21.0" dependencies = [ "anyhow", "atomic-canonical", + "atomic-config", "atomic-core", "atomic-identity", "atomic-objects", "bytes", "chrono", + "data-encoding", "env_logger", "log", "reqwest", "serde", "serde_json", + "serial_test", "tempfile", "thiserror 1.0.69", "tokio", diff --git a/atomic-cli/src/commands/client.rs b/atomic-cli/src/commands/client.rs index 72e0b6f7..af111a8e 100644 --- a/atomic-cli/src/commands/client.rs +++ b/atomic-cli/src/commands/client.rs @@ -15,7 +15,7 @@ //! 3. **Identity** — the server profile's `identity` field, if set, overrides //! the global default identity. //! 4. **Bearer token** — a short-lived, client-self-signed EdDSA JWT minted -//! for the resolved identity (see [`crate::commands::token`]). The raw +//! for the resolved identity (see [`atomic_remote::auth::token`]). The raw //! public key is never sent as a credential. //! //! # Identity resolution @@ -97,7 +97,7 @@ pub async fn build_apex_client(server_override: Option<&str>) -> CliResult Option< return None; } let store = IdentityStore::open_default().ok()?; - let resolved = crate::commands::delegation::active_for(&store, identity, Some(server)).ok()?; + let resolved = + atomic_remote::auth::delegation::active_for(&store, identity, Some(server)).ok()?; Some(atomic_canonical::delegation::encode_for_transport( &resolved.document, )) @@ -170,7 +171,7 @@ pub async fn build_apex_client_as( server_override: Option<&str>, ) -> CliResult<(StorageClient, String)> { let apex_url = apex_server_url(server_override)?; - let bearer_token = crate::commands::token::get_token(&apex_url, identity).await?; + let bearer_token = atomic_remote::auth::token::get_token(&apex_url, identity).await?; let delegation = delegation_for(identity, &apex_url); let client = @@ -220,7 +221,7 @@ pub async fn build_client_with_org( let identity = resolve_identity_for_server(server)?; // Log in against the server apex for a JWT bearer token. - let bearer_token = crate::commands::token::get_token(&server_url, &identity).await?; + let bearer_token = atomic_remote::auth::token::get_token(&server_url, &identity).await?; let client = StorageClient::new(&base_url, &org_slug, &bearer_token).map_err(|e| { CliError::Internal(anyhow::anyhow!("Failed to create storage client: {}", e)) diff --git a/atomic-cli/src/commands/clone/command.rs b/atomic-cli/src/commands/clone/command.rs index d6072b77..550e208f 100644 --- a/atomic-cli/src/commands/clone/command.rs +++ b/atomic-cli/src/commands/clone/command.rs @@ -339,7 +339,7 @@ impl Clone { .danger_accept_invalid_certs(self.insecure); // Clone uses the URL directly — identity inferred from subdomain only. - crate::commands::auth::attach_identity(config, &self.url, None).await + atomic_remote::auth::attach_identity(config, &self.url, None).await } /// CB-10B Git-transport bootstrap (RFC §7.3/§8.6): `git clone` the diff --git a/atomic-cli/src/commands/identity/agent/list.rs b/atomic-cli/src/commands/identity/agent/list.rs index b551d0c5..fc0059a0 100644 --- a/atomic-cli/src/commands/identity/agent/list.rs +++ b/atomic-cli/src/commands/identity/agent/list.rs @@ -6,10 +6,10 @@ use serde_json::json; use atomic_identity::delegation::DelegationStatus; use atomic_identity::{IdentityStore, IdentityType}; -use crate::commands::delegation::{load_for_delegate, ResolvedDelegation}; use crate::commands::Command; use crate::error::{CliError, CliResult}; use crate::output::print_hint; +use atomic_remote::auth::delegation::{load_for_delegate, ResolvedDelegation}; use super::humanize_remaining; diff --git a/atomic-cli/src/commands/identity/agent/renew.rs b/atomic-cli/src/commands/identity/agent/renew.rs index 7552fb7f..43f5e785 100644 --- a/atomic-cli/src/commands/identity/agent/renew.rs +++ b/atomic-cli/src/commands/identity/agent/renew.rs @@ -31,10 +31,10 @@ use atomic_identity::delegation::{Delegation, DelegationScope}; use atomic_identity::{Identity, IdentityStore}; use atomic_remote::storage_types::PushDelegationRequest; -use crate::commands::delegation::load_for_delegate; use crate::commands::Command; use crate::error::{CliError, CliResult}; use crate::output::{print_success, print_warning}; +use atomic_remote::auth::delegation::load_for_delegate; use super::{ humanize_remaining, parse_duration, parse_patterns, parse_permissions, DEFAULT_EXPIRY_DAYS, diff --git a/atomic-cli/src/commands/identity/agent/revoke.rs b/atomic-cli/src/commands/identity/agent/revoke.rs index f955d4d7..82f2a322 100644 --- a/atomic-cli/src/commands/identity/agent/revoke.rs +++ b/atomic-cli/src/commands/identity/agent/revoke.rs @@ -26,10 +26,10 @@ use atomic_canonical::delegation as cert; use atomic_identity::IdentityStore; use atomic_remote::storage_types::RevokeDelegationRequest; -use crate::commands::delegation::load_for_delegate; use crate::commands::Command; use crate::error::{CliError, CliResult}; use crate::output::{print_hint, print_success, print_warning}; +use atomic_remote::auth::delegation::load_for_delegate; /// Revoke an agent's delegation. #[derive(Debug, Parser)] diff --git a/atomic-cli/src/commands/identity/agent/show.rs b/atomic-cli/src/commands/identity/agent/show.rs index 7c1a56f7..ed3185f9 100644 --- a/atomic-cli/src/commands/identity/agent/show.rs +++ b/atomic-cli/src/commands/identity/agent/show.rs @@ -4,10 +4,10 @@ use clap::Parser; use atomic_identity::IdentityStore; -use crate::commands::delegation::load_for_delegate; use crate::commands::Command; use crate::error::{CliError, CliResult}; use crate::output::{print_hint, print_warning}; +use atomic_remote::auth::delegation::load_for_delegate; use super::humanize_remaining; diff --git a/atomic-cli/src/commands/mod.rs b/atomic-cli/src/commands/mod.rs index a36f4bac..120afaeb 100644 --- a/atomic-cli/src/commands/mod.rs +++ b/atomic-cli/src/commands/mod.rs @@ -110,7 +110,6 @@ pub mod view; pub mod agent; // Phase 3: Remote Commands -pub mod auth; pub mod blame; pub mod clone; pub mod pull; @@ -130,9 +129,7 @@ pub mod query; // Storage management commands (always available) pub mod client; -pub mod delegation; pub mod project; -pub mod token; pub mod workspace; pub(crate) mod workspace_txn; diff --git a/atomic-cli/src/commands/pull/command.rs b/atomic-cli/src/commands/pull/command.rs index 154b83dc..2b46c7a3 100644 --- a/atomic-cli/src/commands/pull/command.rs +++ b/atomic-cli/src/commands/pull/command.rs @@ -416,7 +416,7 @@ impl Pull { .with_timeout(Duration::from_secs(self.timeout)) .danger_accept_invalid_certs(self.insecure); - crate::commands::auth::attach_identity(config, remote_url, identity_hint).await + atomic_remote::auth::attach_identity(config, remote_url, identity_hint).await } /// Display the dry run preview. diff --git a/atomic-cli/src/commands/push/command.rs b/atomic-cli/src/commands/push/command.rs index 00d88345..753d9d6c 100644 --- a/atomic-cli/src/commands/push/command.rs +++ b/atomic-cli/src/commands/push/command.rs @@ -408,7 +408,7 @@ impl Push { .with_timeout(Duration::from_secs(self.timeout)) .danger_accept_invalid_certs(self.insecure); - crate::commands::auth::attach_identity(config, remote_url, identity_hint).await + atomic_remote::auth::attach_identity(config, remote_url, identity_hint).await } /// Display the dry run preview: per-view manifest sync plans. @@ -569,7 +569,7 @@ impl Push { // deliberately masked), which surfaces as a misleading "view not found". // Catching the missing credential here gives the user an accurate, // actionable error instead. - crate::commands::auth::check_push_credentials(&remote_url, identity_hint.as_deref())?; + atomic_remote::auth::check_push_credentials(&remote_url, identity_hint.as_deref())?; // Build the local ancestor chain, root → leaf. A draft's parent must // exist on the remote before the draft's manifest can reference it, diff --git a/atomic-cli/src/commands/repo.rs b/atomic-cli/src/commands/repo.rs index 12930c17..854f7517 100644 --- a/atomic-cli/src/commands/repo.rs +++ b/atomic-cli/src/commands/repo.rs @@ -117,12 +117,9 @@ impl Repo { CliError::Internal(anyhow::anyhow!("Failed to create async runtime: {e}")) })?; let (project_id, previous) = rt.block_on(async { - let config = crate::commands::auth::attach_identity( - HttpRemoteConfig::default(), - &entry.url, - None, - ) - .await; + let config = + atomic_remote::auth::attach_identity(HttpRemoteConfig::default(), &entry.url, None) + .await; let remote = HttpRemote::with_config(&entry.url, config).map_err(|e| { CliError::InvalidArgument { message: format!("cannot reach remote {}: {e}", entry.url), diff --git a/atomic-cli/src/commands/rpc.rs b/atomic-cli/src/commands/rpc.rs index 73b3d832..ed243dd7 100644 --- a/atomic-cli/src/commands/rpc.rs +++ b/atomic-cli/src/commands/rpc.rs @@ -5035,7 +5035,7 @@ fn sync_auth_headers(remote_url: &str, identity: Option<&str>) -> HashMap CliResult { style_view(&remote_name), hint(&remote_url) ); - crate::commands::auth::check_push_credentials(&remote_url, args.identity.as_deref())?; + atomic_remote::auth::check_push_credentials(&remote_url, args.identity.as_deref())?; let auth_headers = sync_auth_headers(&remote_url, args.identity.as_deref()); stage_line("Connected"); @@ -5161,6 +5161,7 @@ pub fn push(args: &super::push::Push) -> CliResult { timeout_secs: args.timeout, auth_headers: auth_headers.clone(), remote_url: Some(remote_url.clone()), + identity: args.identity.clone(), }; let response = match session.push_changes(request) { Ok(response) => response, diff --git a/atomic-cli/src/commands/view/list.rs b/atomic-cli/src/commands/view/list.rs index 7b9b2b2c..1ccb9815 100644 --- a/atomic-cli/src/commands/view/list.rs +++ b/atomic-cli/src/commands/view/list.rs @@ -46,10 +46,10 @@ use atomic_remote::{HttpRemote, HttpRemoteConfig, RemoteViewInfo}; use atomic_repository::repository::ViewInfo; use atomic_repository::Repository; -use crate::commands::auth::attach_identity; use crate::commands::{find_repository_root, Command}; use crate::error::{CliError, CliResult}; use crate::output::{hint, view as style_view}; +use atomic_remote::auth::attach_identity; /// Default request timeout (seconds) for the remote view listing. const DEFAULT_TIMEOUT_SECS: u64 = 30; diff --git a/atomic-cli/src/error.rs b/atomic-cli/src/error.rs index 7edbc9b6..398a46e7 100644 --- a/atomic-cli/src/error.rs +++ b/atomic-cli/src/error.rs @@ -425,6 +425,20 @@ pub enum CliError { Repository(#[from] atomic_repository::RepositoryError), } +impl From for CliError { + fn from(error: atomic_remote::auth::AuthError) -> Self { + match error { + atomic_remote::auth::AuthError::Internal(error) => Self::Internal(error), + atomic_remote::auth::AuthError::AuthenticationFailed { remote } => { + Self::AuthenticationFailed { remote } + } + atomic_remote::auth::AuthError::DelegationError { message } => { + Self::DelegationError { message } + } + } + } +} + impl CliError { // Constructor Helpers @@ -745,6 +759,30 @@ impl CliError { mod tests { use super::*; + #[test] + fn shared_auth_errors_preserve_cli_categories_and_context() { + use atomic_remote::auth::AuthError; + + let error = CliError::from(AuthError::Internal(anyhow::anyhow!("key store failure"))); + assert!( + matches!(&error, CliError::Internal(source) if source.to_string() == "key store failure") + ); + + let error = CliError::from(AuthError::AuthenticationFailed { + remote: "https://example.com: missing signing key".into(), + }); + assert!( + matches!(&error, CliError::AuthenticationFailed { remote } if remote == "https://example.com: missing signing key") + ); + + let error = CliError::from(AuthError::DelegationError { + message: "certificate expired; renew it".into(), + }); + assert!( + matches!(&error, CliError::DelegationError { message } if message == "certificate expired; renew it") + ); + } + // ------------------------------------------------------------------------- // Error Display Tests // ------------------------------------------------------------------------- diff --git a/atomic-remote/Cargo.toml b/atomic-remote/Cargo.toml index 3b324bde..e2d7e51c 100644 --- a/atomic-remote/Cargo.toml +++ b/atomic-remote/Cargo.toml @@ -15,6 +15,7 @@ categories = ["development-tools"] # Internal crates atomic-core = { workspace = true } atomic-objects = { workspace = true } +atomic-config = { workspace = true } # HTTP client reqwest = { workspace = true, features = ["json", "gzip", "deflate", "stream"] } @@ -35,12 +36,13 @@ anyhow = { workspace = true } url = { workspace = true } bytes = { workspace = true } log = { workspace = true } +data-encoding = { workspace = true } # Time chrono = { workspace = true } # IDs -uuid = { version = "1.0", features = ["serde"] } +uuid = { version = "1.0", features = ["serde", "v4"] } # URL encoding urlencoding = "2" @@ -52,6 +54,7 @@ atomic-identity = { workspace = true } tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } tempfile = { workspace = true } env_logger = { workspace = true } +serial_test = { workspace = true } [features] default = [] diff --git a/atomic-remote/README.md b/atomic-remote/README.md index 7946ecce..d024817b 100644 --- a/atomic-remote/README.md +++ b/atomic-remote/README.md @@ -11,6 +11,8 @@ communicate with `atomic-api` servers. - Storage management APIs for workspaces and projects. - Error types with retry/auth/not-found classification and user-facing suggestions. - Streaming push/pull support for chunked remote transfers. +- Shared local identity resolution, self-signed JWTs, and agent delegation for + CLI and trusted native callers (`auth`, `auth::token`, `auth::delegation`). ## Usage @@ -29,6 +31,46 @@ async fn example() -> Result<(), Box> { } ``` +## Local authentication + +Repository clients share one local authentication policy rather than minting tokens +in an RPC handler or passing signing keys through RPC: + +```rust,ignore +use atomic_remote::{auth, HttpRemote, HttpRemoteConfig}; + +async fn authenticated_remote( + url: &str, + identity: Option<&str>, +) -> Result> { + // Pushes fail before network I/O when no identity/signing key is usable. + // Omit this preflight for reads that may be publicly accessible. + auth::check_push_credentials(url, identity)?; + let config = auth::attach_identity(HttpRemoteConfig::new(), url, identity).await; + Ok(HttpRemote::with_config(url, config)?) +} +``` + +Identity precedence is explicit override, nonempty `ATOMIC_AGENT_IDENTITY`, URL +userinfo, configured server binding (longest host suffix, active profile wins ties; +agent binding preferred), then the legacy leading subdomain label. Missing inferred +identities fall back to the store's default; a missing explicit override never does. +Callers resolve the configured remote URL and pass its identity binding only when +no explicit override was supplied. + +`attach_identity` signs a fresh five-minute EdDSA JWT locally and presents an agent's +verified delegation certificate when applicable. It leaves the config unchanged on +authentication failure so public reads remain possible. Delegation resolution keeps +the existing environment-grant precedence, signature, expiry, local revocation, +and scope semantics; the server remains authoritative for remote revocations. +`AuthError` distinguishes `Internal`, `AuthenticationFailed { remote }`, and +`DelegationError { message }` so callers can preserve their error categories. + +Identity/config/key/delegation access is synchronous local filesystem work even +though token attachment has an async API. Async services should run this trusted +local credential preparation on a blocking thread; network-facing handlers should +receive prepared transport credentials, not read ambient local identities. + ## Feature flags - `integration-tests`: enables integration tests that require a running diff --git a/atomic-cli/src/commands/delegation.rs b/atomic-remote/src/auth/delegation.rs similarity index 95% rename from atomic-cli/src/commands/delegation.rs rename to atomic-remote/src/auth/delegation.rs index aff59629..38e63aa7 100644 --- a/atomic-cli/src/commands/delegation.rs +++ b/atomic-remote/src/auth/delegation.rs @@ -25,7 +25,7 @@ use atomic_identity::delegation::{ use atomic_identity::{Identity, IdentityStore}; use serde_json::Value; -use crate::error::{CliError, CliResult}; +use super::{AuthError, AuthResult}; /// Environment variable carrying an encoded grant. /// @@ -70,9 +70,9 @@ impl ResolvedDelegation { pub fn load_for_delegate( store: &IdentityStore, identity: &Identity, -) -> CliResult> { +) -> AuthResult> { let stored = cert::load_for_delegate(store, identity) - .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to load delegations: {e}")))?; + .map_err(|e| AuthError::Internal(anyhow::anyhow!("Failed to load delegations: {e}")))?; Ok(stored .into_iter() @@ -101,7 +101,7 @@ pub fn active_for( store: &IdentityStore, identity: &Identity, server: Option<&str>, -) -> CliResult { +) -> AuthResult { // A grant handed over out of band wins. It is the mechanism for machines // with no store to populate, and the freshest thing the caller has. if let Some(resolved) = from_environment(identity)? { @@ -111,7 +111,7 @@ pub fn active_for( let all = load_for_delegate(store, identity)?; if all.is_empty() { - return Err(CliError::DelegationError { + return Err(AuthError::DelegationError { message: format!( "'{}' is an agent identity with no delegation certificate on this machine.\n \ Issue one with: atomic identity delegate {} --can read,record,push", @@ -177,7 +177,7 @@ pub fn active_for( }, }; - Err(CliError::DelegationError { message }) + Err(AuthError::DelegationError { message }) } /// A grant supplied through [`DELEGATION_ENV`], verified and checked to belong @@ -187,7 +187,7 @@ pub fn active_for( /// Falling back to the store there would be worse than failing: the operator /// asked for a specific grant, and silently using a different one is how you /// get an agent acting under a scope nobody intended. -fn from_environment(identity: &Identity) -> CliResult> { +fn from_environment(identity: &Identity) -> AuthResult> { let Ok(encoded) = std::env::var(DELEGATION_ENV) else { return Ok(None); }; @@ -196,12 +196,13 @@ fn from_environment(identity: &Identity) -> CliResult return Ok(None); } - let document = cert::decode_from_transport(encoded).map_err(|e| CliError::DelegationError { - message: format!("{DELEGATION_ENV} is not a usable grant: {e}"), - })?; + let document = + cert::decode_from_transport(encoded).map_err(|e| AuthError::DelegationError { + message: format!("{DELEGATION_ENV} is not a usable grant: {e}"), + })?; let delegation = - cert::verify_self_contained(&document).map_err(|e| CliError::DelegationError { + cert::verify_self_contained(&document).map_err(|e| AuthError::DelegationError { message: format!("The grant in {DELEGATION_ENV} does not verify: {e}"), })?; @@ -210,7 +211,7 @@ fn from_environment(identity: &Identity) -> CliResult // called `alice+claude` on different machines are different keys, and // an error reading "issued to 'alice+claude', not 'alice+claude'" tells // the reader nothing. - return Err(CliError::DelegationError { + return Err(AuthError::DelegationError { message: format!( "The grant in {DELEGATION_ENV} was issued to a different key.\n \ Grant is for {} ({})\n \ @@ -224,7 +225,7 @@ fn from_environment(identity: &Identity) -> CliResult } if delegation.is_expired() { - return Err(CliError::DelegationError { + return Err(AuthError::DelegationError { message: format!( "The grant in {DELEGATION_ENV} expired {}. Ask for a fresh one.", delegation @@ -252,7 +253,7 @@ pub fn check_permission( permission: DelegationPermission, resource: &ResourceRef<'_>, identity_name: &str, -) -> CliResult<()> { +) -> AuthResult<()> { if resolved.delegation.allows(permission, resource) { return Ok(()); } @@ -286,7 +287,7 @@ pub fn check_permission( "\n Widen it with: atomic identity delegate {identity_name} --can {permission}" )); - Err(CliError::DelegationError { message }) + Err(AuthError::DelegationError { message }) } #[cfg(test)] diff --git a/atomic-cli/src/commands/auth.rs b/atomic-remote/src/auth/mod.rs similarity index 95% rename from atomic-cli/src/commands/auth.rs rename to atomic-remote/src/auth/mod.rs index 359226ae..ab87bc0c 100644 --- a/atomic-cli/src/commands/auth.rs +++ b/atomic-remote/src/auth/mod.rs @@ -1,25 +1,27 @@ -//! Shared authentication helpers for remote commands. +//! Shared local authentication for repository remote operations. //! //! Resolves the caller's identity from the remote URL (or an explicit //! override), mints a short-lived self-signed EdDSA JWT for that identity, //! and attaches it as the `Authorization: Bearer` header. //! //! A raw public key is NOT a credential — the JWT (signed with the identity's -//! private key) proves possession of that key. See [`crate::commands::token`] +//! private key) proves possession of that key. See [`token`] //! for the minting mechanics. //! //! Identity resolution order: //! 1. Explicit override — `identity_override` from the `--identity` flag. -//! 2. URL userinfo — `http://bob@alice.localhost:8080/...` → identity "bob" -//! 3. Configured server binding — a `[servers.*]`/`[server]` profile whose host -//! is the longest suffix of the remote host supplies its `identity`. Identity +//! 2. `ATOMIC_AGENT_IDENTITY`, when nonempty after trimming whitespace. +//! 3. URL userinfo — `http://bob@alice.localhost:8080/...` → identity "bob" +//! 4. Configured server binding — a `[servers.*]`/`[server]` profile whose host +//! is the longest suffix of the remote host supplies its `agent_identity` +//! when configured, otherwise its human `identity`. Identity //! follows the server you registered with (the binding `atomic identity //! register` writes), so any tenant under it resolves without `--identity`. //! When two profiles' hosts match equally well, the **active** profile //! (per `default_server`, else the legacy `[server]` block) wins. -//! 4. Subdomain — `http://alice.localhost:8080/...` → identity "alice" (legacy +//! 5. Subdomain — `http://alice.localhost:8080/...` → identity "alice" (legacy //! last resort). -//! 5. Default identity — if none of the above resolves to an identity that +//! 6. Default identity — if none of the above resolves to an identity that //! exists locally, the store's default identity is used. This makes the //! common single-identity case "just work": pushing to //! `https://aaron.atomic.storage/...` with a default identity named @@ -27,11 +29,26 @@ //! The default fallback does NOT apply when `--identity` explicitly names an //! identity that doesn't exist (that's a clear error, not a substitution). +use crate::HttpRemoteConfig; use atomic_identity::{Identity, IdentityStore}; -use atomic_remote::HttpRemoteConfig; use url::Url; -use crate::error::{CliError, CliResult}; +/// Shared authentication failures, preserving credential and delegation remedies. +#[derive(Debug, thiserror::Error)] +pub enum AuthError { + #[error("Internal error: {0}")] + Internal(#[from] anyhow::Error), + #[error("Authentication failed for remote '{remote}'")] + AuthenticationFailed { remote: String }, + #[error("Delegation error: {message}")] + DelegationError { message: String }, +} + +/// Result of resolving or producing local remote credentials. +pub type AuthResult = Result; + +pub mod delegation; +pub mod token; /// Environment override naming the agent identity to authenticate as. /// @@ -62,14 +79,15 @@ struct ServerBinding { /// /// Priority: /// 1. An explicit override (the `--identity` flag). -/// 2. URL userinfo (`http://bob@host/...`). -/// 3. A configured server profile whose host is the longest dot-boundary +/// 2. A nonempty `ATOMIC_AGENT_IDENTITY` environment override. +/// 3. URL userinfo (`http://bob@host/...`). +/// 4. A configured server profile whose host is the longest dot-boundary /// suffix of the remote host (`[servers.prod] url=... identity=...`). This is /// the binding that makes pushes to *any* tenant under a server you've /// registered with ("just works" without `--identity`): identity follows the /// server, not the tenant subdomain. Equal-length matches are won by the -/// active profile. -/// 4. The remote host's leading subdomain label (`http://bob.host/...`) — the +/// active profile. Its agent binding is preferred over its human binding. +/// 5. The remote host's leading subdomain label (`http://bob.host/...`) — the /// legacy heuristic, kept only as a last resort. /// /// Returns `None` only when there is no override and nothing inferable from the @@ -404,7 +422,7 @@ pub async fn attach_identity( }; let identity_name = identity.name.clone(); - match crate::commands::token::get_token(&server, &identity).await { + match crate::auth::token::get_token(&server, &identity).await { Ok(jwt) => { log::debug!("Attaching Bearer JWT for identity '{}'", identity_name); let config = config.with_header("Authorization", format!("Bearer {}", jwt)); @@ -442,7 +460,7 @@ fn delegation_header(store: &IdentityStore, identity: &Identity, server: &str) - return None; } - match crate::commands::delegation::active_for(store, identity, Some(server)) { + match crate::auth::delegation::active_for(store, identity, Some(server)) { Ok(resolved) => Some(atomic_canonical::delegation::encode_for_transport( &resolved.document, )), @@ -458,9 +476,9 @@ fn delegation_header(store: &IdentityStore, identity: &Identity, server: &str) - /// What went wrong (if anything) when checking for usable push credentials. /// -/// Authentication for atomic-storage is a *self-signed* EdDSA JWT: the CLI +/// Authentication for atomic-storage is a *self-signed* EdDSA JWT: the client /// mints a fresh, short-lived token per request by signing with the identity's -/// Ed25519 private key (see [`crate::commands::token`]). There is no stored +/// Ed25519 private key (see [`token`]). There is no stored /// token to inspect for expiry — a token is only ever created at the moment of /// use and is valid for its full TTL. So "having usable credentials" reduces to /// three local conditions, each a distinct failure with its own remedy: @@ -535,9 +553,9 @@ const REMEDY_SUFFIX: &str = /// "just work" without `--identity`. The fallback does **not** apply when /// `--identity` explicitly names an identity that doesn't exist — that is a /// clear error, not a silent substitution. -pub fn check_push_credentials(remote_url: &str, identity_override: Option<&str>) -> CliResult<()> { +pub fn check_push_credentials(remote_url: &str, identity_override: Option<&str>) -> AuthResult<()> { let store = IdentityStore::open_default() - .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to open identity store: {e}")))?; + .map_err(|e| AuthError::Internal(anyhow::anyhow!("Failed to open identity store: {e}")))?; let inferred = resolve_identity_name_with_override(remote_url, identity_override); let explicit = identity_override.is_some(); @@ -560,7 +578,7 @@ pub fn check_push_credentials(remote_url: &str, identity_override: Option<&str>) ); match issue { - Some(issue) => Err(CliError::AuthenticationFailed { + Some(issue) => Err(AuthError::AuthenticationFailed { remote: format!("{remote_url}: {}", issue.message()), }), None => Ok(()), @@ -634,15 +652,6 @@ fn apex_server_url(remote_url: &str) -> Option { } } -/// Extract the identity name from a remote URL alone (no server config). -/// -/// Tries URL userinfo first (`bob@host`), then the subdomain (`bob.host`). -/// This is the config-free path; [`resolve_identity_name_with_override`] layers -/// the `--identity` override and configured server bindings on top. -fn resolve_identity_name(remote_url: &str) -> Option { - resolve_identity_from_url(remote_url, &[]) -} - /// Extract the first subdomain label from a host string. /// /// `"alice.localhost"` → `Some("alice")` @@ -665,6 +674,10 @@ fn extract_subdomain(host: &str) -> Option { #[cfg(test)] mod tests { use super::*; + /// Config-free URL identity resolution for these tests. + fn resolve_identity_name(remote_url: &str) -> Option { + resolve_identity_from_url(remote_url, &[]) + } #[test] fn apex_strips_subdomain_with_port() { diff --git a/atomic-cli/src/commands/token.rs b/atomic-remote/src/auth/token.rs similarity index 93% rename from atomic-cli/src/commands/token.rs rename to atomic-remote/src/auth/token.rs index 86124cbf..ac0fbee1 100644 --- a/atomic-cli/src/commands/token.rs +++ b/atomic-remote/src/auth/token.rs @@ -1,6 +1,6 @@ //! Client-self-signed EdDSA JWT bearer tokens for remote commands. //! -//! Atomic Storage authenticates with short-lived JWTs that the CLI **signs +//! Atomic Storage authenticates with short-lived JWTs that the client **signs //! itself** with the identity's Ed25519 private key (EdDSA, RFC 8037). There is //! no server login endpoint and no token cache: minting a token is a local, //! cheap signing operation, so we just mint a fresh one per request. @@ -51,7 +51,7 @@ use uuid::Uuid; use atomic_identity::{Identity, IdentityStore}; -use crate::error::{CliError, CliResult}; +use super::{AuthError, AuthResult}; /// How long a self-signed token is valid. Kept short (5 minutes) since there is /// no server-side issuance to revoke — a leaked token has a small blast radius. @@ -112,13 +112,7 @@ struct Actor { /// `server` is the bare server URL (e.g. `https://atomic.storage`), NOT an org /// subdomain. The token is keyed by the identity's own Ed25519 public key, so /// no prior registration state needs to be on file locally to mint it. -pub async fn get_token(server: &str, identity: &Identity) -> CliResult { - mint_token(server, identity) -} - -/// Mint a fresh token. Tokens are not cached, so this is identical to -/// [`get_token`]; kept for call-site compatibility (e.g. retry-after-401). -pub async fn refresh_token(server: &str, identity: &Identity) -> CliResult { +pub async fn get_token(server: &str, identity: &Identity) -> AuthResult { mint_token(server, identity) } @@ -126,16 +120,16 @@ pub async fn refresh_token(server: &str, identity: &Identity) -> CliResult CliResult { +fn mint_token(server: &str, identity: &Identity) -> AuthResult { // The token is keyed by the signer's own public key — no server-assigned // identifier to look up. For an agent the signer is the agent itself. let public_key_b32 = identity.public_key_base32(); // Load the keypair (needs the secret key to sign). let store = IdentityStore::open_default() - .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to open identity store: {e}")))?; + .map_err(|e| AuthError::Internal(anyhow::anyhow!("Failed to open identity store: {e}")))?; let keypair = store.load_keypair(&identity.id, None).map_err(|e| { - CliError::Internal(anyhow::anyhow!( + AuthError::Internal(anyhow::anyhow!( "Failed to load keypair for '{}': {e}", identity.name )) @@ -146,11 +140,11 @@ fn mint_token(server: &str, identity: &Identity) -> CliResult { // certificate here means an unusable delegation is reported before any // request is made, with the command that fixes it. let (sub, act, dlg) = if identity.identity_type.is_delegated() { - let resolved = crate::commands::delegation::active_for(&store, identity, Some(server))?; + let resolved = crate::auth::delegation::active_for(&store, identity, Some(server))?; let delegator_key = atomic_canonical::delegation::delegator_public_key( &resolved.delegation, ) - .map_err(|e| CliError::DelegationError { + .map_err(|e| AuthError::DelegationError { message: format!("Delegation for '{}' is malformed: {e}", identity.name), })?; ( @@ -182,9 +176,9 @@ fn mint_token(server: &str, identity: &Identity) -> CliResult { }; let header_json = serde_json::to_vec(&header) - .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to encode JWT header: {e}")))?; + .map_err(|e| AuthError::Internal(anyhow::anyhow!("Failed to encode JWT header: {e}")))?; let claims_json = serde_json::to_vec(&claims) - .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to encode JWT claims: {e}")))?; + .map_err(|e| AuthError::Internal(anyhow::anyhow!("Failed to encode JWT claims: {e}")))?; let header_b64 = BASE64URL_NOPAD.encode(&header_json); let claims_b64 = BASE64URL_NOPAD.encode(&claims_json); diff --git a/atomic-remote/src/lib.rs b/atomic-remote/src/lib.rs index ba36db8f..b97d8b49 100644 --- a/atomic-remote/src/lib.rs +++ b/atomic-remote/src/lib.rs @@ -121,6 +121,7 @@ // Modules +pub mod auth; pub mod error; pub mod http; pub mod storage; diff --git a/libatomic/README.md b/libatomic/README.md index ff6c129b..82d39995 100644 --- a/libatomic/README.md +++ b/libatomic/README.md @@ -43,15 +43,43 @@ println!("{}", status.view); # Ok(()) } ``` -Supported local methods are ResolveRepository, Status, VaultService.GetVaultEntry, -ListVaultEntries, GetVaultContext, and AttestationService.VerifyAttestation. -Mutations and other methods return `Unimplemented`. Malformed messages and missing -repository references return `InvalidArgument`. +Supported local methods include repository resolution; status, log, change and +restore-preview reads; add/record and view lifecycle operations; knowledge +queries; repository-wide vault get/list/context; attestation verification; and +SyncService PushChanges, PullChanges, ListRemotes and ManageRemotes plus +RepositoryMutationService.InsertChanges. Other methods return `Unimplemented`. +Malformed messages and missing repository references return `InvalidArgument`. Repository references belong to the resolving service instance; its clones share them. Methods delegate to the existing handlers, not a parallel implementation. This API is for trusted local callers and does not run remote authentication middleware. Existing redb locking and database-open recovery behavior remain. +### Trusted-local sync credentials + +Push and pull select a remote by `remote` (configured name or direct URL; empty +selects the default, falling back to `origin`) and optionally select a local +identity by `identity` (Push field 15, Pull field 11). The native boundary resolves +the remote and invokes the same +`atomic_remote::auth` implementation as the CLI: explicit identity, agent +environment override, URL userinfo, configured server/agent binding, legacy +subdomain inference, then the existing default-identity fallback. An explicit +missing identity never falls back. Push checks for a usable identity/signing key +before network access; pull retains unauthenticated public reads. + +JWT signing and delegation-certificate selection stay in Rust at this trusted +client boundary. Native requests supplying `auth_headers` or `remote_url` are +rejected; generated credentials never appear in responses. Transport RPC handlers +still consume caller-resolved headers and never consult ambient identity stores. +There is no CLI subprocess, socket, daemon, or JavaScript private key/token flow. + +Push/pull execute the existing sync protocol, including manifests, dependencies, +ref compare-and-swap, and sidecar handling. Pull and insertion retain the existing +handler's current-target working-copy refresh/precondition policy; non-current +targets do not materialize host files. From-view insertion adds membership and +dependencies without changing source membership or view scope. Structured push +conflicts and pull partial-failure reports must be inspected by callers, not +treated as unconditional success. + `FILE_DESCRIPTOR_SET` contains the canonical protobuf descriptor compiled into this crate. Bindings can generate matching codecs/types without vendoring proto sources or requiring system protoc. This exports the full contract; it does not @@ -177,6 +205,8 @@ extension-host compatibility, which needs consumer testing on each platform. ```sh cargo test --locked -p libatomic --test local_service_test +cargo test --locked -p libatomic --test local_sync_test +cargo test --locked -p atomic-remote auth:: node --test libatomic/scripts/package-runtime.test.mjs ``` diff --git a/libatomic/proto/CONTRACT.md b/libatomic/proto/CONTRACT.md index 5ed37b22..751613c5 100644 --- a/libatomic/proto/CONTRACT.md +++ b/libatomic/proto/CONTRACT.md @@ -116,6 +116,25 @@ IDs are refused. No operation uses atomicd's cwd. Bootstrap Git import requires an explicit destination when no RepositoryRef exists. Physical paths occur only in locally authorized operations; content search paths are logical index filters. +### Embedded trusted-local client boundary + +`LocalService::call` is an in-process API for trusted applications, not another +network transport or an authentication-middleware bypass for RPC callers. Its +sync dispatch resolves a configured remote and caller-selected local identity, +then uses the shared `atomic-remote` client authentication policy before invoking +the existing SyncService handler. Push's optional `identity` field 15 and Pull's +optional `identity` field 11 select credentials only at that boundary; transport +handlers do not read ambient config, identities, signing keys, or delegations. + +Native Push/Pull reject caller-provided `auth_headers` and `remote_url`. Headers +are minted internally and never returned through protobuf responses. Existing +transport clients continue resolving and supplying their own outbound headers; +transport method allowlists, principal/capability checks and authorization remain +unchanged. Push preserves its identity/keypair preflight; public pulls may remain +unauthenticated. All supported embedded repository operations require a reference +resolved by the same LocalService instance (or one of its clones). + + ## Sandbox grants and visibility The issuing local admin supplies a stable ViewRef and an explicit capability diff --git a/libatomic/proto/atomic/services/sync/sync_messages.proto b/libatomic/proto/atomic/services/sync/sync_messages.proto index 709a6f61..1d42ac1f 100644 --- a/libatomic/proto/atomic/services/sync/sync_messages.proto +++ b/libatomic/proto/atomic/services/sync/sync_messages.proto @@ -29,12 +29,14 @@ message PushChangesRequest { string remote = 3; repeated string views = 4; // empty = current view manifest chain optional ExpectedState expected = 5; // CAS-on-prev precondition - // `atomic push`'s flag surface (add-only). The network sync IS domain — - // the handler performs it end to end; the client resolves auth - // CLIENT-SIDE (identity store + config are file reads, not redb) and - // ships the headers, so the handler never touches the caller's - // identities. `remote_url` carries the client-resolved remote URL (the - // ListRemotes read); `remote` stays the configured name for the report. + // `atomic push`'s flag surface (add-only). Network sync is domain work: + // the handler performs it end to end without ambient identity-store reads. + // Transport callers resolve authentication client-side and supply headers + // and remote_url; remote stays the configured name for the report. + // Trusted LocalService callers instead supply remote (empty = configured + // default) and optional identity. That boundary resolves the configured URL + // and local credentials, rejecting caller-supplied auth_headers/remote_url, + // before internally invoking the same handler. optional string to_view = 6; optional string from_view = 7; bool dry_run = 8; @@ -44,6 +46,8 @@ message PushChangesRequest { uint64 timeout_secs = 12; map auth_headers = 13; optional string remote_url = 14; + // Trusted LocalService identity selector; transport authentication uses headers. + optional string identity = 15; } message PushChangesResponse { uint64 pushed_changes = 1; @@ -136,15 +140,18 @@ message PullChangesRequest { // apply (default true) // Imports update repository/view state only. Host materialization is a // separately authorized working-copy operation, not a hidden pull effect. - // `atomic pull`'s flag surface (add-only) — same client-side auth model - // as PushChanges (headers resolved from the identity store, never redb). + // `atomic pull`'s flag surface (add-only) — same authentication boundary + // as PushChanges: transport callers supply headers; trusted LocalService + // resolves the configured remote and identity selector locally and rejects + // caller-supplied auth_headers/remote_url. Domain handlers never read the + // caller's ambient identity store. optional string to_view = 5; optional string from_view = 6; bool dry_run = 7; bool all = 8; bool insecure = 9; uint64 timeout_secs = 10; - optional string identity = 11; // informational; auth rides headers + optional string identity = 11; // LocalService selector; transport auth uses headers bool download_only = 12; map auth_headers = 13; optional string remote_url = 14; diff --git a/libatomic/src/local.rs b/libatomic/src/local.rs index 4e9a41dc..ac110237 100644 --- a/libatomic/src/local.rs +++ b/libatomic/src/local.rs @@ -6,6 +6,7 @@ use std::sync::Arc; +use atomic_remote::{auth, HttpRemoteConfig}; use prost::Message; use tonic::{Request, Status}; @@ -13,17 +14,21 @@ use crate::atomic::{ attestation_service_server::AttestationService, daemon_service_server::DaemonService, knowledge_service_server::KnowledgeService, repository_mutation_service_server::RepositoryMutationService, - repository_query_service_server::RepositoryQueryService, vault_service_server::VaultService, - view_service_server::ViewService, AddFilesRequest, CreateViewRequest, DeleteViewRequest, - GetChangeRequest, GetVaultContextRequest, GetVaultEntryRequest, ListVaultEntriesRequest, - ListViewsRequest, LogRequest, PreviewRestoreRequest, QueryGraphRequest, RecordRequest, - ReplayVaultEvidenceRequest, ResolveRepositoryRequest, StatusRequest, SwitchViewRequest, - VerifyAttestationRequest, + repository_query_service_server::RepositoryQueryService, sync_service_server::SyncService, + vault_service_server::VaultService, view_service_server::ViewService, AddFilesRequest, + CreateViewRequest, DeleteViewRequest, GetChangeRequest, GetVaultContextRequest, + GetVaultEntryRequest, GetWorkflowSnapshotRequest, InsertChangesRequest, ListRemotesRequest, + ListVaultEntriesRequest, ListViewsRequest, LogRequest, ManageRemotesRequest, + PreviewRestoreRequest, PullChangesRequest, PushChangesRequest, QueryGraphRequest, + RecordRequest, ReplayVaultEvidenceRequest, ResolveRepositoryRequest, StatusRequest, + SwitchViewRequest, VerifyAttestationRequest, }; use crate::daemon::{ services::{DaemonImpl, MutationImpl, QueryImpl}, services_agent::{AttestationImpl, KnowledgeImpl, VaultImpl}, services_query::ViewImpl, + services_sync::SyncImpl, + state::repository_error, state::DaemonState, }; @@ -34,6 +39,7 @@ use crate::daemon::{ /// This is not a replacement for remote authentication middleware. /// /// Supports repository resolution, basic working-copy/change/view operations, +/// change insertion, configured remote registry management and authenticated push/pull, /// read-only restore previews (including recorded file bytes), /// read-only knowledge queries, vault get/list/context, attestation verification, and /// bounded evidence replay. View-scoped vault reads are not supported; unsupported @@ -135,6 +141,80 @@ impl LocalService { .await?; Ok(response.into_inner().encode_to_vec()) } + "/atomic.RepositoryMutationService/InsertChanges" => { + let request = decode::(request)?; + require_repository(&request.repository)?; + let response = MutationImpl { + state: self.state.clone(), + } + .insert_changes(Request::new(request)) + .await?; + Ok(response.into_inner().encode_to_vec()) + } + "/atomic.SyncService/ListRemotes" => { + let request = decode::(request)?; + require_repository(&request.repository)?; + let response = SyncImpl { + state: self.state.clone(), + } + .list_remotes(Request::new(request)) + .await?; + Ok(response.into_inner().encode_to_vec()) + } + "/atomic.SyncService/ManageRemotes" => { + let request = decode::(request)?; + require_repository(&request.repository)?; + let response = SyncImpl { + state: self.state.clone(), + } + .manage_remotes(Request::new(request)) + .await?; + Ok(response.into_inner().encode_to_vec()) + } + "/atomic.SyncService/PushChanges" => { + let mut request = decode::(request)?; + require_repository(&request.repository)?; + reject_transport_credentials(&request.auth_headers, &request.remote_url)?; + let (name, url, config) = self + .sync_credentials( + request.repository.as_ref().unwrap(), + &request.remote, + request.identity.clone(), + true, + ) + .await?; + request.remote = name; + request.remote_url = Some(url); + request.auth_headers = config.extra_headers.into_iter().collect(); + let response = SyncImpl { + state: self.state.clone(), + } + .push_changes(Request::new(request)) + .await?; + Ok(response.into_inner().encode_to_vec()) + } + "/atomic.SyncService/PullChanges" => { + let mut request = decode::(request)?; + require_repository(&request.repository)?; + reject_transport_credentials(&request.auth_headers, &request.remote_url)?; + let (name, url, config) = self + .sync_credentials( + request.repository.as_ref().unwrap(), + &request.remote, + request.identity.clone(), + false, + ) + .await?; + request.remote = name; + request.remote_url = Some(url); + request.auth_headers = config.extra_headers.into_iter().collect(); + let response = SyncImpl { + state: self.state.clone(), + } + .pull_changes(Request::new(request)) + .await?; + Ok(response.into_inner().encode_to_vec()) + } "/atomic.RepositoryQueryService/Log" => { let request = decode::(request)?; require_repository(&request.repository)?; @@ -255,6 +335,16 @@ impl LocalService { .await?; Ok(response.into_inner().encode_to_vec()) } + "/atomic.KnowledgeService/GetWorkflowSnapshot" => { + let request = decode::(request)?; + require_repository(&request.repository)?; + let response = KnowledgeImpl { + state: self.state.clone(), + } + .get_workflow_snapshot(Request::new(request)) + .await?; + Ok(response.into_inner().encode_to_vec()) + } "/atomic.AttestationService/VerifyAttestation" => { let request = decode::(request)?; require_repository(&request.repository)?; @@ -270,6 +360,76 @@ impl LocalService { ))), } } + + /// Resolve all ambient configuration only at the trusted local boundary. + async fn sync_credentials( + &self, + repository: &crate::atomic::RepositoryRef, + remote: &str, + identity: Option, + push: bool, + ) -> Result<(String, String, HttpRemoteConfig), Status> { + let handle = self.state.resolve(repository)?; + let remote = (!remote.is_empty()).then(|| remote.to_owned()); + let runtime = tokio::runtime::Handle::current(); + tokio::task::spawn_blocking(move || { + let repo = handle.repository_readonly()?; + let name = remote.unwrap_or_else(|| { + repo.get_default_remote() + .map(|(name, _)| name) + .unwrap_or_else(|_| "origin".to_owned()) + }); + let url = if name.contains("://") { + name.clone() + } else { + repo.get_remote(&name) + .map_err(|error| match error { + atomic_repository::RepositoryError::RemoteNotFound { .. } => { + crate::daemon::state::domain_status( + crate::atomic::ErrorCode::NotFound, + format!("remote '{name}' not found"), + ) + } + other => repository_error(other), + })? + .url + }; + if push { + auth::check_push_credentials(&url, identity.as_deref()).map_err(auth_status)?; + } + drop(repo); + let config = runtime.block_on(auth::attach_identity( + HttpRemoteConfig::new(), + &url, + identity.as_deref(), + )); + Ok::<_, Status>((name, url, config)) + }) + .await + .map_err(|e| Status::internal(format!("credentials task failed: {e}")))? + } +} + +fn reject_transport_credentials( + headers: &std::collections::HashMap, + url: &Option, +) -> Result<(), Status> { + if !headers.is_empty() || url.is_some() { + return Err(Status::invalid_argument( + "trusted local sync does not accept auth_headers or remote_url", + )); + } + Ok(()) +} + +fn auth_status(error: auth::AuthError) -> Status { + match error { + auth::AuthError::Internal(error) => Status::internal(error.to_string()), + auth::AuthError::AuthenticationFailed { remote } => { + Status::unauthenticated(format!("authentication failed for {remote}")) + } + auth::AuthError::DelegationError { message } => Status::permission_denied(message), + } } fn require_repository(reference: &Option) -> Result<(), Status> { diff --git a/libatomic/tests/local_service_test.rs b/libatomic/tests/local_service_test.rs index e4173a01..79d528f3 100644 --- a/libatomic/tests/local_service_test.rs +++ b/libatomic/tests/local_service_test.rs @@ -82,6 +82,11 @@ async fn rejects_invalid_requests_and_unknown_methods() { for method in [ "/atomic.RepositoryMutationService/AddFiles", "/atomic.RepositoryMutationService/Record", + "/atomic.RepositoryMutationService/InsertChanges", + "/atomic.SyncService/PushChanges", + "/atomic.SyncService/PullChanges", + "/atomic.SyncService/ListRemotes", + "/atomic.SyncService/ManageRemotes", "/atomic.RepositoryQueryService/Log", "/atomic.RepositoryQueryService/GetChange", "/atomic.ViewService/ListViews", @@ -89,6 +94,7 @@ async fn rejects_invalid_requests_and_unknown_methods() { "/atomic.ViewService/SwitchView", "/atomic.ViewService/DeleteView", "/atomic.KnowledgeService/QueryGraph", + "/atomic.KnowledgeService/GetWorkflowSnapshot", ] { let error = service.call(method, &[]).await.unwrap_err(); assert_eq!(error.code(), tonic::Code::InvalidArgument, "{method}"); @@ -815,3 +821,163 @@ async fn recorded_file_preview_returns_graph_bytes_and_view_without_restoring_or ); assert!(!dir.path().join("deleted.txt").exists()); } + +#[tokio::test] +async fn native_remote_registry_persists_defaults_and_reports_domain_errors() { + let dir = tempfile::tempdir().unwrap(); + drop(Repository::init(dir.path()).unwrap()); + let service = LocalService::new(); + let reference = resolve(&service, dir.path()).await; + for (name, url, default) in [ + ("origin", "https://example.com/origin", false), + ("upstream", "https://example.com/upstream", true), + ] { + let _: proto::ManageRemotesResponse = invoke( + &service, + "/atomic.SyncService/ManageRemotes", + &proto::ManageRemotesRequest { + repository: Some(reference.clone()), + action: proto::RemoteAction::Add as i32, + name: Some(name.into()), + url: Some(url.into()), + set_default: default, + ..Default::default() + }, + ) + .await; + } + let reopened = LocalService::new(); + let fresh = resolve(&reopened, dir.path()).await; + let listed: proto::ListRemotesResponse = invoke( + &reopened, + "/atomic.SyncService/ListRemotes", + &proto::ListRemotesRequest { + repository: Some(fresh.clone()), + }, + ) + .await; + assert_eq!(listed.remotes.len(), 2); + assert!(listed + .remotes + .iter() + .any(|r| r.name == "upstream" && r.default)); + assert_eq!( + Repository::open_readonly(dir.path()) + .unwrap() + .get_default_remote() + .unwrap() + .0, + "upstream" + ); + for action in [proto::RemoteAction::Add, proto::RemoteAction::Remove] { + let request = proto::ManageRemotesRequest { + repository: Some(fresh.clone()), + action: action as i32, + name: Some( + if action == proto::RemoteAction::Add { + "origin" + } else { + "missing" + } + .into(), + ), + url: Some("https://example.com/duplicate".into()), + ..Default::default() + }; + assert!(reopened + .call( + "/atomic.SyncService/ManageRemotes", + &request.encode_to_vec() + ) + .await + .is_err()); + } + for (method, bytes) in [ + ( + "/atomic.SyncService/PushChanges", + proto::PushChangesRequest { + repository: Some(reference.clone()), + remote_url: Some("https://evil.example".into()), + ..Default::default() + } + .encode_to_vec(), + ), + ( + "/atomic.SyncService/PullChanges", + proto::PullChangesRequest { + repository: Some(reference), + auth_headers: [("Authorization".into(), "secret".into())] + .into_iter() + .collect(), + ..Default::default() + } + .encode_to_vec(), + ), + ] { + let error = service.call(method, &bytes).await.unwrap_err(); + assert_eq!(error.code(), tonic::Code::InvalidArgument); + assert!(error.message().contains("does not accept")); + } +} + +#[tokio::test] +async fn native_insert_from_view_preserves_source_scope_and_host_tree() { + use atomic_core::change::ChangeHeader; + use atomic_repository::record::RecordOptions; + let dir = tempfile::tempdir().unwrap(); + let mut repo = Repository::init(dir.path()).unwrap(); + let wc = repo.require_working_copy_id().unwrap(); + let source = repo.current_view().to_string(); + repo.create_shared_view("insert-target").unwrap(); + let file = dir.path().join("dependent.txt"); + std::fs::write(&file, b"first\n").unwrap(); + repo.add(wc, "dependent.txt", Default::default()).unwrap(); + let options = || { + RecordOptions::new() + .with_all(true) + .save_to_store(true) + .apply_after_record(true) + }; + let first = *repo + .record(wc, ChangeHeader::new("dependency"), options()) + .unwrap() + .hash(); + std::fs::write(&file, b"second\n").unwrap(); + let second = *repo + .record(wc, ChangeHeader::new("dependent"), options()) + .unwrap() + .hash(); + let before = repo.get_view_changes(Some(&source)).unwrap(); + let source_scope = repo.get_view_info(&source).unwrap().scope; + let target_scope = repo.get_view_info("insert-target").unwrap().scope; + let host = std::fs::read(&file).unwrap(); + drop(repo); + let service = LocalService::new(); + let reference = resolve(&service, dir.path()).await; + let _: proto::InsertChangesResponse = invoke( + &service, + "/atomic.RepositoryMutationService/InsertChanges", + &proto::InsertChangesRequest { + repository: Some(reference), + target_view: Some("insert-target".into()), + source: Some(proto::insert_changes_request::Source::FromView( + source.clone(), + )), + apply_dependencies: Some(true), + ..Default::default() + }, + ) + .await; + let repo = Repository::open_readonly(dir.path()).unwrap(); + let target = repo.get_view_changes(Some("insert-target")).unwrap(); + assert!(target.iter().any(|(_, hash)| *hash == first)); + assert!(target.iter().any(|(_, hash)| *hash == second)); + assert_eq!(repo.get_view_changes(Some(&source)).unwrap(), before); + assert_eq!(repo.get_view_info(&source).unwrap().scope, source_scope); + assert_eq!( + repo.get_view_info("insert-target").unwrap().scope, + target_scope + ); + assert_eq!(std::fs::read(file).unwrap(), host); + assert_eq!(repo.current_view(), source); +} diff --git a/libatomic/tests/local_sync_test.rs b/libatomic/tests/local_sync_test.rs new file mode 100644 index 00000000..acaf56c6 --- /dev/null +++ b/libatomic/tests/local_sync_test.rs @@ -0,0 +1,475 @@ +//! Real native protobuf push/pull against a stateful, authenticated sync/1 peer. +//! The peer verifies JWT signatures, stores objects, and enforces ref CAS; it +//! does not echo requests or invoke a CLI/daemon transport. + +use std::collections::HashMap; +use std::ffi::OsString; +use std::path::Path; +use std::sync::{Arc, Mutex}; + +use atomic_core::types::{Base32, Hash}; +use atomic_identity::{Identity, IdentityStore, KeyPair}; +use atomic_objects::{ObjectFamily, ObjectRecord, RefRecord, SyncPack, SyncWants}; +use atomic_repository::Repository; +use axum::body::Bytes; +use axum::extract::State; +use axum::http::{HeaderMap, Method, StatusCode, Uri}; +use axum::response::{IntoResponse, Response}; +use axum::routing::any; +use axum::Router; +use data_encoding::BASE64URL_NOPAD; +use libatomic::{proto, LocalService}; +use prost::Message; + +struct Environment(Vec<(&'static str, Option)>); + +impl Environment { + fn isolated(config: &Path) -> Self { + let names = [ + "ATOMIC_CONFIG_DIR", + "ATOMIC_AGENT_IDENTITY", + "ATOMIC_DELEGATION", + ]; + let saved = names + .iter() + .map(|name| (*name, std::env::var_os(name))) + .collect(); + std::env::set_var("ATOMIC_CONFIG_DIR", config); + std::env::remove_var("ATOMIC_AGENT_IDENTITY"); + std::env::remove_var("ATOMIC_DELEGATION"); + Self(saved) + } +} + +impl Drop for Environment { + fn drop(&mut self) { + for (name, value) in &self.0 { + match value { + Some(value) => std::env::set_var(name, value), + None => std::env::remove_var(name), + } + } + } +} + +#[derive(Default)] +struct PeerState { + objects: HashMap<(ObjectFamily, String), ObjectRecord>, + refs: HashMap, + requests: usize, + authenticated: usize, + pushes: usize, + require_identity: Option, + audience: String, +} + +fn authenticates(headers: &HeaderMap, identity: &Identity, audience: &str) -> bool { + let Some(token) = headers + .get("authorization") + .and_then(|v| v.to_str().ok()) + .and_then(|v| v.strip_prefix("Bearer ")) + else { + return false; + }; + let parts: Vec<_> = token.split('.').collect(); + if parts.len() != 3 { + return false; + } + let decode = |part: &str| BASE64URL_NOPAD.decode(part.as_bytes()).ok(); + let Some(header) = + decode(parts[0]).and_then(|v| serde_json::from_slice::(&v).ok()) + else { + return false; + }; + let Some(claims) = + decode(parts[1]).and_then(|v| serde_json::from_slice::(&v).ok()) + else { + return false; + }; + let Some(signature) = decode(parts[2]).and_then(|v| <[u8; 64]>::try_from(v).ok()) else { + return false; + }; + let now = chrono::Utc::now().timestamp(); + header["alg"] == "EdDSA" + && header["kid"] == identity.public_key_base32() + && claims["sub"] == identity.public_key_base32() + && claims["aud"] == audience + && claims["iat"].as_i64().is_some_and(|iat| iat <= now) + && claims["exp"].as_i64().is_some_and(|exp| exp > now) + && identity + .verify(format!("{}.{}", parts[0], parts[1]).as_bytes(), &signature) + .is_ok() +} + +async fn peer( + State(state): State>>, + method: Method, + uri: Uri, + headers: HeaderMap, + body: Bytes, +) -> Response { + let mut state = state.lock().unwrap(); + state.requests += 1; + if let Some(identity) = &state.require_identity { + if !authenticates(&headers, identity, &state.audience) { + return (StatusCode::UNAUTHORIZED, "identity credential required").into_response(); + } + state.authenticated += 1; + } + if uri.query() == Some("project-identity=1") { + return ([("content-type", "application/json")], "{\"bound\":false}").into_response(); + } + if headers.get("Atomic-Protocol").and_then(|v| v.to_str().ok()) != Some("sync/1") { + return (StatusCode::BAD_REQUEST, "sync/1 required").into_response(); + } + if method == Method::GET { + let Ok(wants) = SyncWants::decode(&body) else { + return (StatusCode::BAD_REQUEST, "invalid wants").into_response(); + }; + let mut selected: std::collections::HashSet<_> = wants.refs.iter().cloned().collect(); + let mut pending = wants.refs.clone(); + while let Some(name) = pending.pop() { + let Some(key) = state.refs.get(&name) else { + continue; + }; + let Some(object) = state.objects.get(&(ObjectFamily::View, key.clone())) else { + continue; + }; + let Some(snapshot) = atomic_objects::ViewSnapshot::from_bytes(&object.bytes) else { + continue; + }; + if let Some(parent) = snapshot.parent_view { + if selected.insert(parent.clone()) { + pending.push(parent); + } + } + } + let refs: Vec<_> = state + .refs + .iter() + .filter(|(name, _)| wants.refs.is_empty() || selected.contains(*name)) + .map(|(name, key)| RefRecord { + name: name.clone(), + expect_old: None, + new_target: key.clone(), + }) + .collect(); + let objects = state + .objects + .values() + .filter(|object| !wants.haves.contains(&object.key)) + .filter(|object| !wants.refs_only || object.family == ObjectFamily::View) + .cloned() + .collect(); + return ( + [("content-type", atomic_objects::SYNC_MEDIA_TYPE)], + SyncPack { objects, refs }.encode().unwrap(), + ) + .into_response(); + } + if method != Method::POST { + return StatusCode::METHOD_NOT_ALLOWED.into_response(); + } + let Ok(pack) = SyncPack::decode(&body) else { + return (StatusCode::BAD_REQUEST, "invalid pack").into_response(); + }; + for reference in &pack.refs { + if state.refs.get(&reference.name) != reference.expect_old.as_ref() { + return (StatusCode::CONFLICT, "ref CAS refused").into_response(); + } + if !pack + .objects + .iter() + .any(|object| object.family == ObjectFamily::View && object.key == reference.new_target) + && !state + .objects + .contains_key(&(ObjectFamily::View, reference.new_target.clone())) + { + return (StatusCode::BAD_REQUEST, "missing view snapshot").into_response(); + } + } + let stored = pack.objects.len(); + let moved = pack.refs.len(); + for object in pack.objects { + state + .objects + .insert((object.family, object.key.clone()), object); + } + for reference in pack.refs { + state.refs.insert(reference.name, reference.new_target); + } + state.pushes += 1; + ( + [("content-type", "application/json")], + serde_json::json!({"stored":stored,"refs_moved":moved}).to_string(), + ) + .into_response() +} + +async fn invoke( + service: &LocalService, + method: &str, + request: &Req, +) -> Res { + let bytes = service + .call(method, &request.encode_to_vec()) + .await + .unwrap(); + Res::decode(bytes.as_slice()).unwrap() +} + +async fn resolve(service: &LocalService, path: &Path) -> proto::RepositoryRef { + let response: proto::ResolveRepositoryResponse = invoke( + service, + "/atomic.DaemonService/ResolveRepository", + &proto::ResolveRepositoryRequest { + path: path.display().to_string(), + }, + ) + .await; + response.repository.unwrap() +} + +async fn record(service: &LocalService, repository: &proto::RepositoryRef, message: &str) -> Hash { + let response: proto::RecordResponse = invoke( + service, + "/atomic.RepositoryMutationService/Record", + &proto::RecordRequest { + repository: Some(repository.clone()), + message: message.into(), + paths: vec!["hello.txt".into()], + ..Default::default() + }, + ) + .await; + atomic_core::types::Merkle( + response + .change + .unwrap() + .hash + .unwrap() + .value + .try_into() + .unwrap(), + ) +} + +// This binary has one test, so its temporary process-global identity/config +// environment cannot race another test in this binary. +#[tokio::test] +async fn native_sync_authenticates_transfers_graph_and_does_not_materialize_host_files() { + let config = tempfile::tempdir().unwrap(); + let _environment = Environment::isolated(config.path()); + let keypair = KeyPair::generate(); + let identity = Identity::new("registered", &keypair); + let wrong_key = KeyPair::generate(); + let wrong_identity = Identity::new("default-other-user", &wrong_key); + let mut store = IdentityStore::open_default().unwrap(); + store.save_with_keypair(&identity, &keypair, None).unwrap(); + store + .save_with_keypair(&wrong_identity, &wrong_key, None) + .unwrap(); + store.set_default(&wrong_identity.id).unwrap(); + + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let audience = format!("http://localhost:{}", listener.local_addr().unwrap().port()); + let url = format!("{audience}/workspaces/test/projects/native/code"); + let state = Arc::new(Mutex::new(PeerState { + require_identity: Some(identity.clone()), + audience, + ..Default::default() + })); + let router = Router::new() + .route("/workspaces/test/projects/native/code", any(peer)) + .with_state(state.clone()); + let (stop, stopped) = tokio::sync::oneshot::channel(); + let server = tokio::spawn(async move { + axum::serve(listener, router) + .with_graceful_shutdown(async { + let _ = stopped.await; + }) + .await + .unwrap(); + }); + + let source = tempfile::tempdir().unwrap(); + let target = tempfile::tempdir().unwrap(); + let source_repo = Repository::init(source.path()).unwrap(); + let view = source_repo.current_view().to_string(); + source_repo.add_remote_default("origin", &url).unwrap(); + drop(source_repo); + let target_repo = Repository::init(target.path()).unwrap(); + target_repo.add_remote_default("origin", &url).unwrap(); + drop(target_repo); + std::fs::write(source.path().join("hello.txt"), "first native revision\n").unwrap(); + std::fs::write(target.path().join("hello.txt"), "local unrecorded bytes\n").unwrap(); + + let service = LocalService::new(); + let source_ref = resolve(&service, source.path()).await; + let target_ref = resolve(&service, target.path()).await; + let empty = tempfile::tempdir().unwrap(); + drop(Repository::init(empty.path()).unwrap()); + let empty_ref = resolve(&service, empty.path()).await; + let error = service + .call( + "/atomic.SyncService/PullChanges", + &proto::PullChangesRequest { + repository: Some(empty_ref), + ..Default::default() + } + .encode_to_vec(), + ) + .await + .unwrap_err(); + assert_eq!(error.code(), tonic::Code::NotFound); + assert!(error.message().contains("remote 'origin' not found")); + let _: proto::AddFilesResponse = invoke( + &service, + "/atomic.RepositoryMutationService/AddFiles", + &proto::AddFilesRequest { + repository: Some(source_ref.clone()), + paths: vec!["hello.txt".into()], + ..Default::default() + }, + ) + .await; + let first = record(&service, &source_ref, "first native change").await; + + let mut push = proto::PushChangesRequest { + repository: Some(source_ref.clone()), + ..Default::default() + }; + // A missing explicit identity must not silently use the default or network. + push.identity = Some("does-not-exist".into()); + let error = service + .call("/atomic.SyncService/PushChanges", &push.encode_to_vec()) + .await + .unwrap_err(); + assert_eq!(error.code(), tonic::Code::Unauthenticated); + assert!(error.message().contains("does-not-exist")); + assert_eq!(state.lock().unwrap().requests, 0); + // A usable but unauthorized default key must be rejected by the peer. + push.identity = None; + service + .call("/atomic.SyncService/PushChanges", &push.encode_to_vec()) + .await + .unwrap_err(); + assert!(state.lock().unwrap().refs.is_empty()); + + push.identity = Some(identity.name.clone()); + let pushed: proto::PushChangesResponse = + invoke(&service, "/atomic.SyncService/PushChanges", &push).await; + assert!(pushed.conflict.is_none()); + assert_eq!(pushed.pushed_changes, 1); + assert_eq!(state.lock().unwrap().pushes, 1); + assert!(state + .lock() + .unwrap() + .objects + .contains_key(&(ObjectFamily::Change, first.to_base32()))); + // A direct URL in the remote selector follows the same no-op path without + // requiring a registry entry whose name is that URL. + push.remote = url.clone(); + let repeated: proto::PushChangesResponse = + invoke(&service, "/atomic.SyncService/PushChanges", &push).await; + assert_eq!(repeated.pushed_changes, 0); + assert_eq!(state.lock().unwrap().pushes, 1); + push.remote.clear(); + + let pull = proto::PullChangesRequest { + repository: Some(target_ref.clone()), + identity: Some(identity.name.clone()), + from_view: Some(view.clone()), + to_view: Some("received".into()), + import_sidecars: true, + ..Default::default() + }; + let pulled: proto::PullChangesResponse = + invoke(&service, "/atomic.SyncService/PullChanges", &pull).await; + let report = pulled.report.unwrap(); + assert_eq!(report.changes_failed, 0); + assert!(report.apply_errors.is_empty()); + assert!(!report.materialized); + assert!(report.materialize_error.is_none()); + assert_eq!( + std::fs::read(target.path().join("hello.txt")).unwrap(), + b"local unrecorded bytes\n" + ); + { + let repo = Repository::open_readonly(target.path()).unwrap(); + assert!(repo + .view_manifest("received") + .unwrap() + .changes + .contains(&first)); + assert_eq!( + repo.get_file_content_on_view("hello.txt", "received") + .unwrap() + .unwrap(), + b"first native revision\n" + ); + } + + std::fs::write(source.path().join("hello.txt"), "second native revision\n").unwrap(); + let second = record(&service, &source_ref, "second native change").await; + let pushed: proto::PushChangesResponse = + invoke(&service, "/atomic.SyncService/PushChanges", &push).await; + assert_eq!(pushed.pushed_changes, 1); + let pulled: proto::PullChangesResponse = + invoke(&service, "/atomic.SyncService/PullChanges", &pull).await; + assert_eq!(pulled.report.as_ref().unwrap().changes_downloaded, 1); + assert!(pulled.report.as_ref().unwrap().apply_errors.is_empty()); + { + let repo = Repository::open_readonly(target.path()).unwrap(); + let manifest = repo.view_manifest("received").unwrap(); + assert!(manifest.changes.contains(&first)); + assert!(manifest.changes.contains(&second)); + assert_eq!( + repo.get_file_content_on_view("hello.txt", "received") + .unwrap() + .unwrap(), + b"second native revision\n" + ); + } + assert_eq!( + std::fs::read(target.path().join("hello.txt")).unwrap(), + b"local unrecorded bytes\n" + ); + assert!(state.lock().unwrap().authenticated >= 8); + + // Calling the transport handler with the same selector must NOT consult + // ambient identity storage: only LocalService is a trusted credential client. + use libatomic::proto::sync_service_server::SyncService; + let transport_state = Arc::new(libatomic::daemon::state::DaemonState::new()); + let reference = transport_state + .register(target.path().canonicalize().unwrap()) + .repository_ref(); + let transport = libatomic::daemon::services_sync::SyncImpl { + state: transport_state, + }; + let error = transport + .pull_changes(tonic::Request::new(proto::PullChangesRequest { + repository: Some(reference), + identity: Some(identity.name.clone()), + from_view: Some(view), + ..Default::default() + })) + .await + .unwrap_err(); + assert!(error.message().contains("authentication_failed")); + + // Public reads still work without a usable identity: explicit missing + // identity must not become a mandatory local authentication requirement. + state.lock().unwrap().require_identity = None; + let _: proto::PullChangesResponse = invoke( + &service, + "/atomic.SyncService/PullChanges", + &proto::PullChangesRequest { + identity: Some("does-not-exist".into()), + ..pull + }, + ) + .await; + let _ = stop.send(()); + server.await.unwrap(); +}