Skip to content
Open
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file>`), whose paths are
relative to it:

Expand Down
2 changes: 1 addition & 1 deletion apps/origin-src/benches/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static SETUP: LazyLock<Setup> = 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()
};
Expand Down
2 changes: 1 addition & 1 deletion apps/origin-src/benches/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions apps/origin-src/benches/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static SETUP: LazyLock<Setup> = 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<u64>| {
Expand Down Expand Up @@ -283,7 +283,7 @@ static HISTORY_SETUP: LazyLock<History> = 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;
Expand Down
13 changes: 11 additions & 2 deletions apps/origin-src/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Principal>,
},
}

Expand Down Expand Up @@ -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,
Expand Down
33 changes: 29 additions & 4 deletions apps/origin-src/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,19 @@ impl TryFrom<PolicyFile> 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\
Expand All @@ -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\
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 9 additions & 5 deletions apps/origin-src/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <principal>]`: 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(())
}
Expand Down
2 changes: 1 addition & 1 deletion apps/origin-src/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apps/origin-src/src/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"");
Expand Down
2 changes: 1 addition & 1 deletion apps/origin-src/tests/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand Down