diff --git a/README.md b/README.md index 6e0952ac..bffb4f6d 100644 --- a/README.md +++ b/README.md @@ -195,10 +195,14 @@ copy, and the authority of their targets: it runs their landing queues, and packs their small objects once a few thousand pile up loose. ```sh -origin repo create repos/demo # a repository the server orders `policy` and `main` of +origin repo create repos/demo [--admin user:alice] # a repository the server orders origin serve # host the repositories over HTTP ``` +`--admin` names who changes the new repository's policy over the API; +without it, only the server's operator does, by landing through the +repository's path. + Both read `origin-server.toml` (or `--config `), whose paths are relative to it: diff --git a/apps/origin-src/benches/permissions.rs b/apps/origin-src/benches/permissions.rs index 9395d23b..42bbda57 100644 --- a/apps/origin-src/benches/permissions.rs +++ b/apps/origin-src/benches/permissions.rs @@ -64,7 +64,7 @@ static SETUP: LazyLock = LazyLock::new(|| { name: "hosted".parse().unwrap(), key: Arc::new(NodeKey::from_bytes(&[7; 32])), }; - let initial = Policy::initial(&node.name, &node.key.public()); + let initial = Policy::initial(&node.name, &node.key.public(), None); let create = |name: &str, policy: &str| { OriginService::create(repos.path().join(name), node.clone(), policy).unwrap() }; diff --git a/apps/origin-src/benches/server.rs b/apps/origin-src/benches/server.rs index 0105e244..44eab978 100644 --- a/apps/origin-src/benches/server.rs +++ b/apps/origin-src/benches/server.rs @@ -44,7 +44,7 @@ impl Hosted { name: "hosted".parse().unwrap(), key: Arc::new(NodeKey::from_bytes(&[7; 32])), }; - let policy = Policy::initial(&node.name, &node.key.public()); + let policy = Policy::initial(&node.name, &node.key.public(), None); let service = OriginService::create(dir.path().join("demo"), node.clone(), &policy).unwrap(); let revision = Self::put_change(&service, 0); diff --git a/apps/origin-src/benches/sync.rs b/apps/origin-src/benches/sync.rs index b2efd1e2..9e8574f0 100644 --- a/apps/origin-src/benches/sync.rs +++ b/apps/origin-src/benches/sync.rs @@ -67,7 +67,7 @@ static SETUP: LazyLock = LazyLock::new(|| { name: "hosted".parse().unwrap(), key: Arc::new(NodeKey::from_bytes(&[7; 32])), }; - let policy = Policy::initial(&node.name, &node.key.public()); + let policy = Policy::initial(&node.name, &node.key.public(), None); let service = OriginService::create(server.path().join("demo"), node.clone(), &policy).unwrap(); let mut cache = StatCache::default(); let mut snapshot_into = |store: &Store, version: Option| { @@ -283,7 +283,7 @@ static HISTORY_SETUP: LazyLock = LazyLock::new(|| { name: "hosted".parse().unwrap(), key: Arc::new(NodeKey::from_bytes(&[7; 32])), }; - let policy = Policy::initial(&node.name, &node.key.public()); + let policy = Policy::initial(&node.name, &node.key.public(), None); let service = OriginService::create(server.path().join("demo"), node.clone(), &policy).unwrap(); let tip = service.store().head(&TargetName::MAIN).unwrap().unwrap(); let state = service.store().get(tip.acceptance).unwrap().body().state; diff --git a/apps/origin-src/src/cli.rs b/apps/origin-src/src/cli.rs index 9820d7cc..6ee40c3e 100644 --- a/apps/origin-src/src/cli.rs +++ b/apps/origin-src/src/cli.rs @@ -80,6 +80,11 @@ enum RepoCommand { /// The server configuration file. #[arg(short, long, default_value = "origin-server.toml")] config: PathBuf, + /// Who may change the policy over the API, such as `user:alice`: + /// by default, only the server's operator, through the + /// repository's path. + #[arg(long)] + admin: Option, }, } @@ -524,8 +529,12 @@ impl Cli { pub fn run(self) -> Result<()> { match self.command { Command::Serve { config } => return ServerConfig::load(&config)?.serve(), - Command::Repo(RepoCommand::Create { path, config }) => { - return ServerConfig::load(&config)?.create_repo(&path); + Command::Repo(RepoCommand::Create { + path, + config, + admin, + }) => { + return ServerConfig::load(&config)?.create_repo(&path, admin.as_ref()); } Command::Token(TokenCommand::Issue { scope, diff --git a/apps/origin-src/src/policy.rs b/apps/origin-src/src/policy.rs index 9f380688..9acca94b 100644 --- a/apps/origin-src/src/policy.rs +++ b/apps/origin-src/src/policy.rs @@ -496,9 +496,19 @@ impl TryFrom for Policy { impl Policy { /// The first `policy.toml` of a repository: `node`, whose public key is /// `key`, is the authority of `policy` and `main`, and neither requires - /// anything, so a solo project lands instantly. + /// anything, so a solo project lands instantly. With an `admin`, its + /// `[grants]` make them the one who changes the policy, and let + /// everyone land elsewhere and annotate, as without grants. #[must_use] - pub fn initial(node: &PrincipalName, key: &NodePublicKey) -> String { + pub fn initial(node: &PrincipalName, key: &NodePublicKey, admin: Option<&Principal>) -> String { + let grants = match admin { + Some(admin) => format!( + "[grants]\n\ + all = [\"land *\", \"annotate *\"]\n\ + \"{admin}\" = [\"admin\"]\n" + ), + None => "# [grants]\n# \"user:alice\" = [\"admin\", \"land *\"]\n".to_owned(), + }; format!( "# Who may land what. Changing it is a landing on `policy`.\n\ \n\ @@ -521,8 +531,7 @@ impl Policy { # Who may land, annotate, and change this policy (`admin`). Without\n\ # [grants], everyone lands and annotates, and no one is admin\n\ # remotely.\n\ - # [grants]\n\ - # \"user:alice\" = [\"admin\", \"land *\"]\n\ + {grants}\ \n\ # Who may read and write paths: everyone writes everything by default.\n\ # [[paths]]\n\ @@ -763,6 +772,22 @@ mod tests { ) } + #[test] + fn an_initial_admin_alone_changes_the_policy() { + let node = PrincipalName::new("hosted").unwrap(); + let key = origin_shared::NodeKey::from_bytes(&[42; 32]).public(); + let admin = principal("user:alice"); + for admin in [None, Some(&admin)] { + let policy: Policy = toml::from_str(&Policy::initial(&node, &key, admin)).unwrap(); + let alice = policy.rights(Some(&principal("user:alice"))); + assert_eq!(alice.admin, admin.is_some()); + let bob = policy.rights(Some(&principal("user:bob"))); + assert!(!bob.admin); + assert!(bob.may_land(&TargetName::MAIN)); + assert!(bob.may_annotate(&kind("ci.test"))); + } + } + #[test] fn rights_come_from_grants_and_paths() { let policy: Policy = toml::from_str(&full_policy()).unwrap(); diff --git a/apps/origin-src/src/server.rs b/apps/origin-src/src/server.rs index c27c0427..60bbc9c1 100644 --- a/apps/origin-src/src/server.rs +++ b/apps/origin-src/src/server.rs @@ -100,23 +100,27 @@ impl ServerConfig { }) } - /// `origin repo create`: creates an empty repository for this server - /// to host in `root`, with the server's node the authority of `policy` - /// and `main`. + /// `origin repo create [--admin ]`: creates an empty + /// repository for this server to host in `root`, with the server's node + /// the authority of `policy` and `main`, and `admin`, if given, the one + /// who changes its policy over the API. /// /// # Errors /// /// Fails if `root` isn't empty, or the key or repository cannot be /// written. - pub fn create_repo(&self, root: &Path) -> anyhow::Result<()> { + pub fn create_repo(&self, root: &Path, admin: Option<&Principal>) -> anyhow::Result<()> { let node = self.node()?; - let policy = Policy::initial(&node.name, &node.key.public()); + let policy = Policy::initial(&node.name, &node.key.public(), admin); OriginService::create(root.to_owned(), node, &policy)?; println!( "Created a repository in {}: node:{} is the authority of `policy` and `main`", root.display(), self.node ); + if let Some(admin) = admin { + println!("{admin} changes its policy"); + } println!("Name it in the [repos] of the configuration to serve it"); Ok(()) } diff --git a/apps/origin-src/src/service.rs b/apps/origin-src/src/service.rs index 589e7a79..46a7a2da 100644 --- a/apps/origin-src/src/service.rs +++ b/apps/origin-src/src/service.rs @@ -1525,7 +1525,7 @@ mod tests { name: "hosted".parse().unwrap(), key: Arc::new(NodeKey::from_bytes(&[7; 32])), }; - let policy = Policy::initial(&node.name, &node.key.public()); + let policy = Policy::initial(&node.name, &node.key.public(), None); let service = OriginService::create(dir.path().join("repo"), node, &policy).unwrap(); // A landing holds an index all along: a read meanwhile takes another. service diff --git a/apps/origin-src/src/workspace/mod.rs b/apps/origin-src/src/workspace/mod.rs index 135ae308..9ca6e46e 100644 --- a/apps/origin-src/src/workspace/mod.rs +++ b/apps/origin-src/src/workspace/mod.rs @@ -269,7 +269,7 @@ impl Workspace { let store = Store::new(meta, Native::default()).with_actor(actor.in_workspace(&root)); store.init()?; - let policy = Policy::initial(config.node(), &key.public()); + let policy = Policy::initial(config.node(), &key.public(), None); let main = Queue::new(&store, &key, config.node().clone()).init(&policy)?; let empty_tree = Tree::default().encode(); let empty_description = EncodedObject::attachment(b""); diff --git a/apps/origin-src/tests/http.rs b/apps/origin-src/tests/http.rs index 6753991a..02b15d64 100644 --- a/apps/origin-src/tests/http.rs +++ b/apps/origin-src/tests/http.rs @@ -53,7 +53,7 @@ impl Server { name: "hosted".parse().unwrap(), key: Arc::new(NodeKey::from_bytes(&[7; 32])), }; - let open = Policy::initial(&node.name, &node.key.public()); + let open = Policy::initial(&node.name, &node.key.public(), None); let strict = format!( "[nodes]\nhosted = \"{}\"\n[targets.policy]\nauthority = \"node:hosted\"\n\ [targets.main]\nauthority = \"node:hosted\"\n\