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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- Direct CLI writes and maintenance on a cluster graph now obey the graph's
applied policy, as `omnigraph access` already reported; they used to apply
none. Pass an `--as` actor the policy admits (`optimize` and `cleanup` now
accept `--as`), or the command is refused as `forbidden` with no effects.
See [direct writes][direct-cluster-writes].

[direct-cluster-writes]: ../docs/user/operations/policy.md#actor-identity
41 changes: 34 additions & 7 deletions crates/omnigraph-cli/src/admission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,50 @@ pub(crate) async fn open_read_only_with_binding(
Ok(db)
}

/// The one writable opener of an embedded command. A graph inside a cluster
/// is admitted first (before the opener's capability probe), and the policy
/// the applied revision binds to it is installed on the handle, so every
/// `_as` writer judges the declared actor as a server judges a served one. A
/// standalone graph has no policy. `storage` builds the adapter the caller
/// opens the graph with; it runs only once the graph is admitted, since
/// building a read-through adapter under `--base` writes the clone marker.
pub(crate) async fn open_writable(
binding: &CapturedStorageBinding,
storage: impl Future<Output = Result<Arc<dyn omnigraph::storage::StorageAdapter>>>,
stderr: &mut dyn Write,
) -> Result<omnigraph::db::Omnigraph> {
let policy = match ensure_graph_with_binding(binding, stderr).await? {
Some(owner) => owner.graph_policy(binding.uri()).await.map_err(report)?,
None => None,
};
crate::command_outcome::writable_open();
let db = omnigraph::db::Omnigraph::open_with_storage(binding.uri(), storage.await?).await?;
Ok(match policy {
Some(policy) => db.with_policy(policy),
None => db,
})
}

/// Acquire before the writable opener (including its capability probe), and
/// reuse only the same canonical root's admission during this command.
pub(crate) async fn ensure_graph_with_binding(
/// reuse only the same canonical root's admission during this command. The
/// admission a cluster graph is under, or `None` for a standalone graph.
async fn ensure_graph_with_binding(
binding: &CapturedStorageBinding,
stderr: &mut dyn Write,
) -> Result<()> {
) -> Result<Option<ClusterAdmission>> {
let uri = binding.uri();
let Some(root) = omnigraph_cluster::cluster_root_for_graph_binding(binding)
.await
.map_err(report)?
else {
return Ok(());
return Ok(None);
};
let key = binding.for_uri(&root)?.identity();
let owners = COMMAND.try_with(Arc::clone).unwrap_or_default();
let mut owners = owners.lock().await;
if let Some(owner) = owners.get(&key) {
return owner.validate_graph_uri(uri).await.map_err(report);
owner.validate_graph_uri(uri).await.map_err(report)?;
return Ok(Some(owner.clone()));
}
if let Some(owner) = omnigraph_cluster::acquire_graph_admission_with_binding(
binding,
Expand All @@ -107,9 +133,10 @@ pub(crate) async fn ensure_graph_with_binding(
writeln!(stderr, "{custody}")
.and_then(|()| stderr.flush())
.map_err(|error| color_eyre::Report::from(error).wrap_err(custody))?;
owners.insert(key, owner);
owners.insert(key, owner.clone());
return Ok(Some(owner));
}
Ok(())
Ok(None)
}

fn report(diagnostic: omnigraph_cluster::Diagnostic) -> color_eyre::Report {
Expand Down
5 changes: 3 additions & 2 deletions crates/omnigraph-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ pub(crate) struct Cli {
/// On a served write, and on every request `mcp` relays, it is an assertion:
/// the request carries `omnigraph-if-actor`, and the server refuses it
/// before anything runs when the bearer token resolves to another actor.
/// With a policy configured but no actor set, a direct operation is
/// denied — see docs/user/operations/policy.md.
/// A direct write to a cluster graph is judged by the policy the cluster
/// applied to that graph, and with one but no actor set it is denied —
/// see docs/user/operations/policy.md.
#[arg(long = "as", global = true, value_name = "ACTOR", value_parser = crate::invocation::single_selector("--as"))]
pub(crate) as_actor: Option<String>,

Expand Down
86 changes: 34 additions & 52 deletions crates/omnigraph-cli/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;

use color_eyre::Result;
use color_eyre::eyre::{bail, eyre};
use omnigraph::db::{Omnigraph, ReadTarget, SnapshotId};
use omnigraph::db::{ReadTarget, SnapshotId};
use omnigraph::settings::{SessionSettings, SettingId, SettingValue, Source};
use omnigraph::storage::CapturedStorageBinding;
use omnigraph::{BLOB_READ_RANGE_MAX_BYTES, BlobContent, Session};
Expand Down Expand Up @@ -203,8 +203,9 @@ pub(crate) fn parse_set_flags(flags: &[String]) -> Result<Vec<(SettingId, Settin
pub(crate) enum GraphClient {
/// Local engine at `uri`. Reads (`resolve()`) leave `actor` empty;
/// writes (`resolve_with_policy()`) attribute the resolved actor.
/// Direct-store access carries no Cedar policy (RFC-011: policy lives
/// in the cluster/server, not in per-operator addressing).
/// A writable open of a cluster graph installs the policy the cluster
/// applied to it (RFC-011: direct means no HTTP boundary, not unpoliced);
/// a standalone graph has none.
Embedded {
uri: String,
actor: Option<String>,
Expand Down Expand Up @@ -412,15 +413,15 @@ impl GraphClient {
matches!(self, GraphClient::Remote { .. })
}

/// Writable verbs alone acquire durable cluster admission. Direct-store
/// access carries no Cedar policy (RFC-011), the actor rides the `_as` APIs.
/// Writable verbs alone acquire durable cluster admission. A cluster
/// graph's applied policy is installed on the handle, and the declared
/// actor rides the `_as` APIs into it.
async fn open_write(
uri: &str,
binding: &CapturedStorageBinding,
base: Option<&str>,
stderr: &mut dyn Write,
) -> Result<Session> {
Self::open_write_session(uri, binding, base, &[], stderr).await
Self::open_write_session(binding, base, &[], stderr).await
}

/// Read dispatch uses no writable opener or retained writer admission.
Expand Down Expand Up @@ -461,37 +462,28 @@ impl GraphClient {
/// environment's defaults and the `--set` values, every setting accepted;
/// the source's own `set` lines apply per call, on top.
async fn open_write_session(
uri: &str,
binding: &CapturedStorageBinding,
base: Option<&str>,
settings: &[(SettingId, SettingValue)],
stderr: &mut dyn Write,
) -> Result<Session> {
Self::open_write_session_with_external_blob_policy(
uri, binding, base, settings, None, stderr,
)
.await
Self::open_write_session_with_external_blob_policy(binding, base, settings, None, stderr)
.await
}

/// An embedded write session, with `external_blob_policy` installed on
/// its fresh handle when given: the one way a CLI run admits a source
/// outside the graph (a `load csv "file://…"` statement run directly).
async fn open_write_session_with_external_blob_policy(
uri: &str,
binding: &CapturedStorageBinding,
base: Option<&str>,
settings: &[(SettingId, SettingValue)],
external_blob_policy: Option<omnigraph::ExternalBlobPolicy>,
stderr: &mut dyn Write,
) -> Result<Session> {
let (defaults, sources) = omnigraph::settings::from_env()?;
crate::admission::ensure_graph_with_binding(binding, stderr).await?;
crate::command_outcome::writable_open();
let db = Omnigraph::open_with_storage(
uri,
crate::admission::embedded_storage(binding, base).await?,
)
.await?;
let storage = crate::admission::embedded_storage(binding, base);
let db = crate::admission::open_writable(binding, storage, stderr).await?;
let db = match external_blob_policy {
Some(policy) => db.with_external_blob_policy(policy)?,
None => db,
Expand Down Expand Up @@ -1309,16 +1301,15 @@ impl GraphClient {
remote_response_json_bounded(response, token.as_deref(), *response_limit).await
}
GraphClient::Embedded {
uri,
uri: _,
actor,
binding,
base,
} => {
refuse_noncanonical_branches([Some(branch), from])?;
let data = std::fs::read_to_string(data)?;
let session =
Self::open_write_session(uri, binding, base.as_deref(), settings, stderr)
.await?;
Self::open_write_session(binding, base.as_deref(), settings, stderr).await?;
// A missing branch is forked first, which publishes on its
// own; the engine marks a later refusal as following it.
let receipt = session
Expand Down Expand Up @@ -1395,7 +1386,7 @@ impl GraphClient {
.await
}
GraphClient::Embedded {
uri,
uri: _,
actor,
binding,
base,
Expand All @@ -1405,8 +1396,7 @@ impl GraphClient {
let params = query_params_from_json(&query_params, params_json)?;
refuse_noncanonical_branches([branch])?;
let session =
Self::open_write_session(uri, binding, base.as_deref(), settings, stderr)
.await?;
Self::open_write_session(binding, base.as_deref(), settings, stderr).await?;
let actor = actor.as_deref();
let receipt = session
.mutate_as_with_optional_branch_receipt(
Expand Down Expand Up @@ -1520,7 +1510,7 @@ impl GraphClient {
Ok(output)
}
GraphClient::Embedded {
uri,
uri: _,
actor,
binding,
base,
Expand Down Expand Up @@ -1556,14 +1546,9 @@ impl GraphClient {
as_commit,
} => {
let target = into.unwrap_or_else(|| "main".to_string());
let mut session = Self::open_write_session(
uri,
binding,
base.as_deref(),
settings,
stderr,
)
.await?;
let mut session =
Self::open_write_session(binding, base.as_deref(), settings, stderr)
.await?;
Self::apply_prefix(&mut session, query_source)?;
let result = session
.branch_merge_as_with_options(
Expand Down Expand Up @@ -1656,15 +1641,14 @@ impl GraphClient {
Ok(output)
}
GraphClient::Embedded {
uri,
uri: _,
actor,
binding,
base,
} => {
let query_name = write.statement_name().to_string();
let mut session =
Self::open_write_session(uri, binding, base.as_deref(), settings, stderr)
.await?;
Self::open_write_session(binding, base.as_deref(), settings, stderr).await?;
Self::apply_prefix(&mut session, query_source)?;
let target = session
.get_commit(write.graph_commit_id())
Expand Down Expand Up @@ -1743,15 +1727,14 @@ impl GraphClient {
.await
}
GraphClient::Embedded {
uri,
uri: _,
actor,
binding,
base,
} => {
let query_name = stmt.statement_name().to_string();
let request = omnigraph::loader::CsvLoadRequest::from_statement(&stmt);
let mut session = Self::open_write_session_with_external_blob_policy(
uri,
binding,
base.as_deref(),
settings,
Expand Down Expand Up @@ -1807,13 +1790,13 @@ impl GraphClient {
.await?
}
GraphClient::Embedded {
uri,
uri: _,
actor,
binding,
base,
} => {
let session =
Self::open_write_session(uri, binding, base.as_deref(), &[], stderr).await?;
Self::open_write_session(binding, base.as_deref(), &[], stderr).await?;
commit_output(
&session
.publish_commit_as(commit_id, actor.as_deref())
Expand Down Expand Up @@ -1851,13 +1834,13 @@ impl GraphClient {
.await
}
GraphClient::Embedded {
uri,
uri: _,
actor,
binding,
base,
} => {
let session =
Self::open_write_session(uri, binding, base.as_deref(), &[], stderr).await?;
Self::open_write_session(binding, base.as_deref(), &[], stderr).await?;
session
.drop_commit_as(commit_id, actor.as_deref())
.await
Expand Down Expand Up @@ -2262,7 +2245,7 @@ impl GraphClient {
binding,
base,
} => {
let db = Self::open_write(uri, binding, base.as_deref(), stderr).await?;
let db = Self::open_write(binding, base.as_deref(), stderr).await?;
let actor = actor.as_deref();
let created = db
.branch_create_from_as(start, name, actor)
Expand Down Expand Up @@ -2310,7 +2293,7 @@ impl GraphClient {
binding,
base,
} => {
let db = Self::open_write(uri, binding, base.as_deref(), stderr).await?;
let db = Self::open_write(binding, base.as_deref(), stderr).await?;
let actor = actor.as_deref();
db.branch_delete_as(name, actor).await.engine_write()?;
Ok(BranchDeleteOutput {
Expand Down Expand Up @@ -2386,14 +2369,13 @@ impl GraphClient {
}
}
GraphClient::Embedded {
uri,
uri: _,
actor,
binding,
base,
} => {
let session =
Self::open_write_session(uri, binding, base.as_deref(), settings, stderr)
.await?;
Self::open_write_session(binding, base.as_deref(), settings, stderr).await?;
let actor = actor.as_deref();
let result = session
.branch_merge_as_with_options(source, into, actor, as_commit)
Expand Down Expand Up @@ -2572,7 +2554,7 @@ impl GraphClient {
binding,
base,
} => {
let db = Self::open_write(uri, binding, base.as_deref(), stderr).await?;
let db = Self::open_write(binding, base.as_deref(), stderr).await?;
let result = db
.apply_schema_as_with_catalog_check(schema_source, actor.as_deref(), validate)
.await
Expand Down Expand Up @@ -2827,13 +2809,13 @@ impl GraphClient {
let branch = query.branch.as_deref().unwrap_or("main");
match self {
GraphClient::Embedded {
uri,
uri: _,
actor,
binding,
base,
} => {
let session =
Self::open_write_session(uri, binding, base.as_deref(), &[], stderr).await?;
Self::open_write_session(binding, base.as_deref(), &[], stderr).await?;
let cell = blob_write_cell(query);
let outcome = match bytes {
Some(bytes) => {
Expand Down
Loading
Loading