| 84b9e9ac Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
use super::*; |
| 7 |
use crate::identity::SecretKey; |
| 8 |
|
| 9 |
|
| 10 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 11 |
pub struct FileEdit { |
| 12 |
pub path: String, |
| 13 |
pub content: Option<Vec<u8>>, |
| 14 |
} |
| 15 |
|
| 16 |
impl Repo { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| eae59556 Andy McCall ↶ |
22 |
pub fn build_change( |
| 23 |
&self, |
| 24 |
heads: &[ChangeHash], |
| 25 |
edits: &[FileEdit], |
| 26 |
message: &str, |
| 27 |
author: KeyId, |
| 28 |
) -> Result<Change> { |
| f2f91ceb Andy McCall ↶ |
29 |
self.build_change_with(heads, edits, &[], message, author) |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
pub fn build_pin_change( |
| 37 |
&self, |
| 38 |
heads: &[ChangeHash], |
| 39 |
path: &str, |
| 40 |
pin: &crate::subrepo::Pin, |
| 41 |
message: &str, |
| 42 |
author: KeyId, |
| 43 |
) -> Result<Change> { |
| 44 |
self.build_change_with( |
| 45 |
heads, |
| 46 |
&[], |
| 47 |
&[(path.to_string(), pin.clone())], |
| 48 |
message, |
| 49 |
author, |
| 50 |
) |
| 51 |
} |
| 52 |
|
| 53 |
fn build_change_with( |
| 54 |
&self, |
| 55 |
heads: &[ChangeHash], |
| 56 |
edits: &[FileEdit], |
| 57 |
pins: &[(String, crate::subrepo::Pin)], |
| 58 |
message: &str, |
| 59 |
author: KeyId, |
| 60 |
) -> Result<Change> { |
| 84b9e9ac Andy McCall ↶ |
61 |
if message.trim().is_empty() { |
| 62 |
return fail("a change message is required"); |
| 63 |
} |
| f2f91ceb Andy McCall ↶ |
64 |
if edits.is_empty() && pins.is_empty() { |
| 84b9e9ac Andy McCall ↶ |
65 |
return fail("nothing to change"); |
| 66 |
} |
| 67 |
let base = self.load_state(heads)?; |
| 68 |
let base_snapshot = base.materialize(); |
| 69 |
let mut entries: BTreeMap<String, FsEntry> = BTreeMap::new(); |
| 70 |
for (path, f) in &base_snapshot.files { |
| 71 |
let entry = match (&f.kind, &f.content) { |
| 72 |
(NodeKind::Dir, _) | (_, FileContent::Dir) => continue, |
| eae59556 Andy McCall ↶ |
73 |
(kind, FileContent::Text { bytes, .. }) => { |
| 74 |
FsEntry::new(*kind, f.mode.exec, bytes.clone()) |
| 75 |
} |
| 76 |
(kind, FileContent::Opaque { blobs }) => FsEntry { |
| 77 |
blob: blobs.first().copied(), |
| cc7a0056 Andy McCall ↶ |
78 |
..FsEntry::new(*kind, f.mode.exec, vec![]) |
| eae59556 Andy McCall ↶ |
79 |
}, |
| 84b9e9ac Andy McCall ↶ |
80 |
}; |
| 81 |
entries.insert(path.clone(), entry); |
| 82 |
} |
| 83 |
for e in edits { |
| 84 |
let path = e.path.trim_matches('/'); |
| eae59556 Andy McCall ↶ |
85 |
if path.is_empty() |
| 86 |
|| path |
| 87 |
.split('/') |
| 88 |
.any(|c| c.is_empty() || c == "." || c == ".." || c == OXY_DIR) |
| 89 |
{ |
| 84b9e9ac Andy McCall ↶ |
90 |
return fail(format!("{:?} is not a valid path", e.path)); |
| 91 |
} |
| 92 |
match &e.content { |
| 93 |
Some(bytes) => { |
| 94 |
let exec = entries.get(path).map(|x| x.exec).unwrap_or(false); |
| 95 |
|
| eae59556 Andy McCall ↶ |
96 |
if base_snapshot |
| 97 |
.files |
| 98 |
.get(path) |
| 99 |
.is_some_and(|f| f.kind == NodeKind::Dir) |
| 100 |
{ |
| 84b9e9ac Andy McCall ↶ |
101 |
return fail(format!("{path} is a directory")); |
| 102 |
} |
| 103 |
let mut parent = path; |
| 104 |
while let Some((p, _)) = parent.rsplit_once('/') { |
| eae59556 Andy McCall ↶ |
105 |
if base_snapshot |
| 106 |
.files |
| 107 |
.get(p) |
| 108 |
.is_some_and(|f| f.kind != NodeKind::Dir) |
| 109 |
{ |
| 84b9e9ac Andy McCall ↶ |
110 |
return fail(format!("{p} is a file, not a directory")); |
| 111 |
} |
| 112 |
parent = p; |
| 113 |
} |
| eae59556 Andy McCall ↶ |
114 |
entries.insert( |
| 115 |
path.to_string(), |
| 116 |
FsEntry::new(NodeKind::File, exec, bytes.clone()), |
| 117 |
); |
| 84b9e9ac Andy McCall ↶ |
118 |
} |
| 119 |
None => { |
| 120 |
if entries.remove(path).is_none() { |
| 121 |
|
| 122 |
let prefix = format!("{path}/"); |
| 123 |
let before = entries.len(); |
| 124 |
entries.retain(|p, _| !p.starts_with(&prefix)); |
| 125 |
if entries.len() == before { |
| 126 |
return fail(format!("{path} does not exist")); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| f2f91ceb Andy McCall ↶ |
130 |
} |
| 131 |
} |
| 132 |
for (path, pin) in pins { |
| 133 |
let path = path.trim_matches('/'); |
| 134 |
match base_snapshot.files.get(path) { |
| 135 |
Some(f) if f.kind == NodeKind::Subrepo => {} |
| 136 |
Some(_) => return fail(format!("{path} is not a subrepo")), |
| 137 |
None => return fail(format!("{path} does not exist")), |
| 84b9e9ac Andy McCall ↶ |
138 |
} |
| f2f91ceb Andy McCall ↶ |
139 |
entries.insert( |
| 140 |
path.to_string(), |
| 141 |
FsEntry::new(NodeKind::Subrepo, false, pin.encode()), |
| 142 |
); |
| 84b9e9ac Andy McCall ↶ |
143 |
} |
| f7510b63 Andy McCall ↶ |
144 |
|
| 145 |
|
| 146 |
let written: std::collections::HashSet<String> = edits |
| 147 |
.iter() |
| 148 |
.filter(|e| e.content.is_some()) |
| 149 |
.map(|e| e.path.trim_matches('/').to_string()) |
| f2f91ceb Andy McCall ↶ |
150 |
.chain(pins.iter().map(|(p, _)| p.trim_matches('/').to_string())) |
| f7510b63 Andy McCall ↶ |
151 |
.collect(); |
| 152 |
let built = self.build_from_entries_resolving( |
| 153 |
&base, |
| 154 |
&base_snapshot, |
| 155 |
HashMap::new(), |
| 156 |
&entries, |
| 157 |
&written, |
| 158 |
)?; |
| eae59556 Andy McCall ↶ |
159 |
if let Some(s) = built |
| 160 |
.statuses |
| 161 |
.iter() |
| 162 |
.find(|s| s.status == ChangeStatus::ConflictEdited) |
| 163 |
{ |
| 84b9e9ac Andy McCall ↶ |
164 |
return fail(format!("{} still contains conflict markers", s.path)); |
| 165 |
} |
| 166 |
if built.tree_ops.is_empty() && built.edits.is_empty() { |
| 167 |
return fail("the edits change nothing"); |
| 168 |
} |
| 169 |
let mut change = Change { |
| 6f8aeb99 Andy McCall ↶ |
170 |
version: crate::objects::CHANGE_VERSION, |
| 84b9e9ac Andy McCall ↶ |
171 |
change_id: ChangeId::random(), |
| 172 |
deps: vec![], |
| 173 |
author, |
| 174 |
hlc: Hlc::now(), |
| 175 |
message: message.trim_end().to_string(), |
| 176 |
tree_ops: built.tree_ops, |
| 177 |
edits: built.edits, |
| eae59556 Andy McCall ↶ |
178 |
kind: if built.resolves { |
| 179 |
ChangeKind::Resolution |
| 180 |
} else { |
| 181 |
ChangeKind::Normal |
| 182 |
}, |
| 84b9e9ac Andy McCall ↶ |
183 |
}; |
| 184 |
let mut deps: BTreeSet<ChangeHash> = referenced_changes(&change, &base); |
| 185 |
deps.extend(built.contributors.iter().copied()); |
| 186 |
change.deps = deps.into_iter().collect(); |
| 187 |
change.normalize(); |
| 188 |
Ok(change) |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| eae59556 Andy McCall ↶ |
193 |
pub fn host_ref_update( |
| 194 |
&self, |
| 195 |
bookmark: &str, |
| 196 |
heads: &[ChangeHash], |
| 197 |
signer: Option<(&KeyId, &SecretKey)>, |
| 198 |
) -> Result<RefUpdate> { |
| 84b9e9ac Andy McCall ↶ |
199 |
let view = self.view()?; |
| 200 |
let empty = self.store.put_state(&StateObject::new(vec![]))?; |
| 201 |
let old_state = view.bookmarks.get(bookmark).copied().unwrap_or(empty); |
| 202 |
let new_state = self.store.put_state(&StateObject::new(heads.to_vec()))?; |
| 203 |
let attestation = match signer { |
| 204 |
Some((key, sk)) => { |
| 205 |
let serial = self |
| 206 |
.latest_ref_attest(bookmark)? |
| 207 |
.and_then(|(_, a)| match a.attest.body { |
| 208 |
AttestBody::Ref { serial, .. } => Some(serial), |
| 209 |
_ => None, |
| 210 |
}) |
| 211 |
.map(|s| s + 1) |
| 212 |
.unwrap_or(1); |
| 213 |
let attest = Attest { |
| 214 |
repo: self.id()?, |
| 215 |
hlc: Hlc::now(), |
| eae59556 Andy McCall ↶ |
216 |
body: AttestBody::Ref { |
| 217 |
name: bookmark.to_string(), |
| 218 |
old_state, |
| 219 |
new_state, |
| 220 |
serial, |
| 221 |
}, |
| 84b9e9ac Andy McCall ↶ |
222 |
}; |
| 223 |
let r = attest.hash(); |
| 224 |
let sig = sk.sign(Domain::Attest, key, &r); |
| eae59556 Andy McCall ↶ |
225 |
Some(SignedAttest { |
| 226 |
attest, |
| 227 |
sigs: vec![sig], |
| 228 |
}) |
| 84b9e9ac Andy McCall ↶ |
229 |
} |
| 230 |
None => None, |
| 231 |
}; |
| eae59556 Andy McCall ↶ |
232 |
Ok(RefUpdate { |
| 233 |
name: bookmark.to_string(), |
| 234 |
old_state, |
| 235 |
new_state, |
| 236 |
force: false, |
| 237 |
attestation, |
| 238 |
}) |
| 84b9e9ac Andy McCall ↶ |
239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| eae59556 Andy McCall ↶ |
245 |
pub fn apply_hosted( |
| 246 |
&self, |
| 247 |
change: Option<(Change, Vec<Signature>)>, |
| 248 |
identities: &[Identity], |
| 249 |
update: RefUpdate, |
| 250 |
) -> Result<PushReport> { |
| 84b9e9ac Andy McCall ↶ |
251 |
let mut bundle = Bundle::default(); |
| 252 |
for id in identities { |
| 253 |
bundle.objects.push((ObjectType::Identity, id.encode())); |
| 254 |
} |
| 255 |
if let Ok(st) = self.store.get_state(&update.new_state) { |
| 256 |
bundle.objects.push((ObjectType::State, st.encode())); |
| 257 |
} |
| 258 |
if let Some((c, sigs)) = change { |
| 259 |
bundle.changes.push((c, sigs)); |
| 260 |
} |
| 261 |
self.receive_push_report(bundle, vec![update], None, vec![]) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
#[cfg(test)] |
| 266 |
mod tests { |
| 267 |
use super::*; |
| 268 |
use crate::identity::SecretKey; |
| 269 |
|
| 270 |
fn hermetic() { |
| 271 |
static ONCE: std::sync::Once = std::sync::Once::new(); |
| 272 |
static DIR: std::sync::OnceLock<tempfile::TempDir> = std::sync::OnceLock::new(); |
| 273 |
ONCE.call_once(|| { |
| 274 |
let d = tempfile::tempdir().unwrap(); |
| 275 |
std::env::set_var("OXY_HOME", d.path()); |
| 276 |
DIR.set(d).ok(); |
| 277 |
}); |
| 278 |
} |
| 279 |
|
| 280 |
fn write(root: &Path, rel: &str, content: &[u8]) { |
| 281 |
let p = root.join(rel); |
| 282 |
fs::create_dir_all(p.parent().unwrap()).unwrap(); |
| 283 |
fs::write(p, content).unwrap(); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| eae59556 Andy McCall ↶ |
288 |
fn setup( |
| 289 |
root: &Path, |
| 290 |
with_policy: bool, |
| 291 |
delegate: bool, |
| 292 |
) -> (Repo, Repo, KeyId, SecretKey, Identity) { |
| 84b9e9ac Andy McCall ↶ |
293 |
hermetic(); |
| 294 |
let host_dir = root.join("host"); |
| 295 |
Repo::init_bare_unbound(&host_dir).unwrap(); |
| 296 |
let work = root.join("work"); |
| 297 |
let mut user = Repo::init(&work).unwrap(); |
| eae59556 Andy McCall ↶ |
298 |
user.create_identity("Andy", "[email protected]", IdentityScope::Repo) |
| 299 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
300 |
user.config.set("core.sign", "change").unwrap(); |
| eae59556 Andy McCall ↶ |
301 |
user.config |
| 302 |
.set("remote.origin.url", host_dir.to_str().unwrap()) |
| 303 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
304 |
user.save_config().unwrap(); |
| 305 |
let hub_sk = SecretKey::generate(); |
| 306 |
let hub_id = hub_sk.identity("Hub", "hub@example", 1); |
| 307 |
let hub_key = hub_id.hash(); |
| 308 |
if with_policy { |
| 309 |
user.policy_init().unwrap(); |
| 310 |
} |
| 311 |
if delegate { |
| 7ba155f2 Andy McCall ↶ |
312 |
user.delegate_add(hub_id.clone(), Scope::CHANGES | Scope::REFS, None) |
| 313 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
314 |
} |
| 315 |
write(&work, "a.txt", b"one\ntwo\n"); |
| 316 |
user.snapshot("t").unwrap(); |
| 317 |
user.commit("first", &[]).unwrap(); |
| 318 |
let r = user.push("origin", false).unwrap(); |
| 319 |
assert!(r.iter().all(|x| x.ok), "{r:?}"); |
| eae59556 Andy McCall ↶ |
320 |
( |
| 321 |
Repo::open(&host_dir).unwrap(), |
| 322 |
user, |
| 323 |
hub_key, |
| 324 |
hub_sk, |
| 325 |
hub_id, |
| 326 |
) |
| 84b9e9ac Andy McCall ↶ |
327 |
} |
| 328 |
|
| 329 |
#[test] |
| 330 |
fn a_host_edit_signed_by_a_delegate_lands() { |
| 331 |
let dir = tempfile::tempdir().unwrap(); |
| 332 |
let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, true); |
| 333 |
let author = user.config.author_key.unwrap(); |
| 334 |
let heads = host.bookmark_heads().unwrap(); |
| eae59556 Andy McCall ↶ |
335 |
let change = host |
| 336 |
.build_change( |
| 337 |
&heads, |
| 338 |
&[ |
| 339 |
FileEdit { |
| 340 |
path: "a.txt".into(), |
| 341 |
content: Some(b"one\ntwo\nthree\n".to_vec()), |
| 342 |
}, |
| 343 |
FileEdit { |
| 344 |
path: "docs/new.md".into(), |
| 345 |
content: Some(b"# New\n".to_vec()), |
| 346 |
}, |
| 347 |
], |
| 348 |
"Edit on the web", |
| 349 |
author, |
| 350 |
) |
| 351 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
352 |
assert_eq!(change.author, author); |
| 353 |
assert_eq!(change.kind, ChangeKind::Normal); |
| 354 |
let hash = change.hash(); |
| 355 |
let sig = hub_sk.sign(Domain::Change, &hub_key, &hash); |
| 356 |
let mut new_heads = heads.clone(); |
| 357 |
new_heads.push(hash); |
| eae59556 Andy McCall ↶ |
358 |
let update = host |
| 359 |
.host_ref_update("main", &[hash], Some((&hub_key, &hub_sk))) |
| 360 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
361 |
let ids = vec![hub_id, user.identity(&author).unwrap()]; |
| eae59556 Andy McCall ↶ |
362 |
let report = host |
| 363 |
.apply_hosted(Some((change, vec![sig])), &ids, update) |
| 364 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
365 |
assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results); |
| 366 |
assert_eq!(host.bookmark_heads().unwrap(), vec![hash]); |
| eae59556 Andy McCall ↶ |
367 |
assert_eq!( |
| 368 |
host.file_at("a.txt", Some(&[hash])).unwrap(), |
| 369 |
b"one\ntwo\nthree\n" |
| 370 |
); |
| 371 |
assert_eq!( |
| 372 |
host.file_at("docs/new.md", Some(&[hash])).unwrap(), |
| 373 |
b"# New\n" |
| 374 |
); |
| 84b9e9ac Andy McCall ↶ |
375 |
|
| eae59556 Andy McCall ↶ |
376 |
assert!( |
| 377 |
matches!(host.authenticate_change(&hash).unwrap(), SignatureStatus::Valid { key, name, .. } if key == hub_key && name == "Hub") |
| 378 |
); |
| 84b9e9ac Andy McCall ↶ |
379 |
let _ = new_heads; |
| 380 |
|
| eae59556 Andy McCall ↶ |
381 |
assert!(host |
| 382 |
.build_change( |
| 383 |
&[hash], |
| 384 |
&[FileEdit { |
| 385 |
path: "../x".into(), |
| 386 |
content: Some(vec![]) |
| 387 |
}], |
| 388 |
"m", |
| 389 |
author |
| 390 |
) |
| 391 |
.is_err()); |
| 392 |
assert!( |
| 393 |
host.build_change( |
| 394 |
&[hash], |
| 395 |
&[FileEdit { |
| 396 |
path: "a.txt".into(), |
| 397 |
content: Some(b"one\ntwo\nthree\n".to_vec()) |
| 398 |
}], |
| 399 |
"m", |
| 400 |
author |
| 401 |
) |
| 402 |
.is_err(), |
| 403 |
"no-op edits are refused" |
| 404 |
); |
| 405 |
assert!( |
| 406 |
host.build_change( |
| 407 |
&[hash], |
| 408 |
&[FileEdit { |
| 409 |
path: "a.txt/b".into(), |
| 410 |
content: Some(vec![]) |
| 411 |
}], |
| 412 |
"m", |
| 413 |
author |
| 414 |
) |
| 415 |
.is_err(), |
| 416 |
"a file inside a file" |
| 417 |
); |
| 418 |
let rm = host |
| 419 |
.build_change( |
| 420 |
&[hash], |
| 421 |
&[FileEdit { |
| 422 |
path: "docs".into(), |
| 423 |
content: None, |
| 424 |
}], |
| 425 |
"Remove docs", |
| 426 |
author, |
| 427 |
) |
| 428 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
429 |
assert!(!rm.tree_ops.is_empty()); |
| 430 |
} |
| 431 |
|
| 432 |
#[test] |
| 433 |
fn a_host_edit_by_a_key_that_is_not_a_delegate_is_refused() { |
| 434 |
let dir = tempfile::tempdir().unwrap(); |
| 435 |
let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, false); |
| 436 |
let author = user.config.author_key.unwrap(); |
| 437 |
let heads = host.bookmark_heads().unwrap(); |
| eae59556 Andy McCall ↶ |
438 |
let change = host |
| 439 |
.build_change( |
| 440 |
&heads, |
| 441 |
&[FileEdit { |
| 442 |
path: "a.txt".into(), |
| 443 |
content: Some(b"x\n".to_vec()), |
| 444 |
}], |
| 445 |
"Edit", |
| 446 |
author, |
| 447 |
) |
| 448 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
449 |
let hash = change.hash(); |
| 450 |
let sig = hub_sk.sign(Domain::Change, &hub_key, &hash); |
| eae59556 Andy McCall ↶ |
451 |
let update = host |
| 452 |
.host_ref_update("main", &[hash], Some((&hub_key, &hub_sk))) |
| 453 |
.unwrap(); |
| 454 |
let report = host |
| 455 |
.apply_hosted( |
| 456 |
Some((change, vec![sig])), |
| 457 |
&[hub_id, user.identity(&author).unwrap()], |
| 458 |
update, |
| 459 |
) |
| 460 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
461 |
|
| 462 |
|
| 463 |
assert!(report.results.iter().all(|r| r.ok)); |
| eae59556 Andy McCall ↶ |
464 |
assert!( |
| 465 |
matches!(host.authenticate_change(&hash).unwrap(), SignatureStatus::Invalid(why) if why.contains("neither the author")) |
| 466 |
); |
| 84b9e9ac Andy McCall ↶ |
467 |
} |
| 468 |
|
| 469 |
#[test] |
| 470 |
fn under_a_policy_a_delegate_may_update_the_reference_and_a_stranger_may_not() { |
| 471 |
let dir = tempfile::tempdir().unwrap(); |
| 472 |
let (host, user, hub_key, hub_sk, hub_id) = setup(dir.path(), true, true); |
| 473 |
assert!(host.current_policy().unwrap().is_some()); |
| 474 |
let author = user.config.author_key.unwrap(); |
| 475 |
let heads = host.bookmark_heads().unwrap(); |
| eae59556 Andy McCall ↶ |
476 |
let change = host |
| 477 |
.build_change( |
| 478 |
&heads, |
| 479 |
&[FileEdit { |
| 480 |
path: "a.txt".into(), |
| 481 |
content: Some(b"one\n".to_vec()), |
| 482 |
}], |
| 483 |
"Trim", |
| 484 |
author, |
| 485 |
) |
| 486 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
487 |
let hash = change.hash(); |
| 488 |
let sig = hub_sk.sign(Domain::Change, &hub_key, &hash); |
| 489 |
let ids = vec![hub_id.clone(), user.identity(&author).unwrap()]; |
| eae59556 Andy McCall ↶ |
490 |
let update = host |
| 491 |
.host_ref_update("main", &[hash], Some((&hub_key, &hub_sk))) |
| 492 |
.unwrap(); |
| 493 |
let report = host |
| 494 |
.apply_hosted(Some((change, vec![sig])), &ids, update) |
| 495 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
496 |
assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results); |
| 497 |
assert_eq!(host.bookmark_heads().unwrap(), vec![hash]); |
| 498 |
|
| 499 |
|
| 500 |
let stranger = SecretKey::generate(); |
| 501 |
let sid = stranger.identity("Mallory", "m@example", 1); |
| 502 |
let skey = sid.hash(); |
| eae59556 Andy McCall ↶ |
503 |
let change2 = host |
| 504 |
.build_change( |
| 505 |
&[hash], |
| 506 |
&[FileEdit { |
| 507 |
path: "a.txt".into(), |
| 508 |
content: Some(b"evil\n".to_vec()), |
| 509 |
}], |
| 510 |
"Evil", |
| 511 |
author, |
| 512 |
) |
| 513 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
514 |
let h2 = change2.hash(); |
| 515 |
let sig2 = stranger.sign(Domain::Change, &skey, &h2); |
| eae59556 Andy McCall ↶ |
516 |
let update = host |
| 517 |
.host_ref_update("main", &[h2], Some((&skey, &stranger))) |
| 518 |
.unwrap(); |
| 519 |
let report = host |
| 520 |
.apply_hosted(Some((change2, vec![sig2])), &[sid], update) |
| 521 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
522 |
assert!(report.results.iter().all(|r| !r.ok), "{:?}", report.results); |
| eae59556 Andy McCall ↶ |
523 |
assert_eq!( |
| 524 |
host.bookmark_heads().unwrap(), |
| 525 |
vec![hash], |
| 526 |
"the reference did not move" |
| 527 |
); |
| 84b9e9ac Andy McCall ↶ |
528 |
|
| 529 |
let update = host.host_ref_update("main", &[hash], None).unwrap(); |
| 530 |
let report = host.apply_hosted(None, &[], update).unwrap(); |
| 531 |
assert!(report.results.iter().all(|r| !r.ok)); |
| 7ba155f2 Andy McCall ↶ |
532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
#[test] |
| 538 |
fn a_changes_only_delegate_signs_changes_but_cannot_seal_or_move_a_reference() { |
| 539 |
let dir = tempfile::tempdir().unwrap(); |
| 540 |
let (host, mut user, _, _, _) = setup(dir.path(), true, false); |
| 541 |
let agent_sk = SecretKey::generate(); |
| 542 |
let agent_id = agent_sk.identity("aider on a laptop", "agent@example", 1); |
| 543 |
let agent = agent_id.hash(); |
| 544 |
user.delegate_add(agent_id.clone(), Scope::CHANGES, None) |
| 545 |
.unwrap(); |
| 546 |
|
| 547 |
assert!(user.push("origin", false).unwrap().iter().all(|x| x.ok)); |
| 548 |
let author = user.config.author_key.unwrap(); |
| 549 |
let ids = vec![agent_id.clone(), user.identity(&author).unwrap()]; |
| 550 |
let heads = host.bookmark_heads().unwrap(); |
| 551 |
let change = host |
| 552 |
.build_change( |
| 553 |
&heads, |
| 554 |
&[FileEdit { |
| 555 |
path: "a.txt".into(), |
| 556 |
content: Some(b"by the agent\n".to_vec()), |
| 557 |
}], |
| 558 |
"Agent edit", |
| 559 |
author, |
| 560 |
) |
| 561 |
.unwrap(); |
| 562 |
let hash = change.hash(); |
| 563 |
let sig = agent_sk.sign(Domain::Change, &agent, &hash); |
| 564 |
|
| 565 |
let update = host |
| 566 |
.host_ref_update("main", &[hash], Some((&agent, &agent_sk))) |
| 567 |
.unwrap(); |
| 568 |
let report = host |
| 569 |
.apply_hosted(Some((change.clone(), vec![sig.clone()])), &ids, update) |
| 570 |
.unwrap(); |
| 571 |
assert!(report.results.iter().all(|r| !r.ok), "{:?}", report.results); |
| 572 |
assert_eq!(host.bookmark_heads().unwrap(), heads); |
| 573 |
|
| 574 |
let (owner, owner_sk) = user.secret_key().unwrap().unwrap(); |
| 575 |
let update = host |
| 576 |
.host_ref_update("main", &[hash], Some((&owner, &owner_sk))) |
| 577 |
.unwrap(); |
| 578 |
let report = host |
| 579 |
.apply_hosted(Some((change, vec![sig])), &ids, update) |
| 580 |
.unwrap(); |
| 581 |
assert!(report.results.iter().all(|r| r.ok), "{:?}", report.results); |
| 582 |
assert_eq!(host.bookmark_heads().unwrap(), vec![hash]); |
| 583 |
assert!(matches!( |
| 584 |
host.authenticate_change(&hash).unwrap(), |
| 585 |
SignatureStatus::Valid { key, sealed: false, .. } if key == agent |
| 586 |
)); |
| 587 |
|
| 588 |
let change2 = host |
| 589 |
.build_change( |
| 590 |
&[hash], |
| 591 |
&[FileEdit { |
| 592 |
path: "a.txt".into(), |
| 593 |
content: Some(b"sealed by the agent\n".to_vec()), |
| 594 |
}], |
| 595 |
"Sealed", |
| 596 |
author, |
| 597 |
) |
| 598 |
.unwrap(); |
| 599 |
let h2 = host.store().put_change(&change2).unwrap(); |
| 600 |
let seal = host |
| 601 |
.store() |
| 602 |
.put_attest(&Attest::seal(host.id().unwrap(), vec![h2])) |
| 603 |
.unwrap(); |
| 604 |
host.store() |
| 605 |
.put_signature(&seal, &agent_sk.sign(Domain::Attest, &agent, &seal)) |
| 606 |
.unwrap(); |
| 607 |
host.store().index_seal(&h2, &seal).unwrap(); |
| 608 |
assert!(matches!( |
| 609 |
host.authenticate_change(&h2).unwrap(), |
| 610 |
SignatureStatus::Invalid(e) if e.contains("may not seal") |
| 611 |
)); |
| 612 |
} |
| 613 |
|
| ff1a64f8 Andy McCall ↶ |
614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
#[test] |
| 620 |
fn an_agent_signs_its_owner_s_changes_through_author_signer() { |
| 621 |
let dir = tempfile::tempdir().unwrap(); |
| 622 |
let (host, mut user, _, _, _) = setup(dir.path(), true, false); |
| 623 |
let owner = user.config.author_key.unwrap(); |
| 624 |
|
| 625 |
let agent_home = dir.path().join("agent-home"); |
| 626 |
let mut agent_repo = Repo::init(&dir.path().join("agent-work")) |
| 627 |
.unwrap() |
| 628 |
.with_home(agent_home.clone()); |
| 629 |
let (agent, agent_id) = agent_repo |
| 630 |
.create_identity("aider on a laptop", "agent@example", IdentityScope::User) |
| 631 |
.unwrap(); |
| 632 |
user.delegate_add(agent_id.clone(), Scope::CHANGES, None) |
| 633 |
.unwrap(); |
| 634 |
assert!(user.push("origin", false).unwrap().iter().all(|x| x.ok)); |
| 635 |
let owner = { |
| 636 |
let _ = owner; |
| 637 |
user.config.author_key.unwrap() |
| 638 |
}; |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
let work = dir.path().join("work"); |
| 644 |
let mut as_agent = Repo::open(&work).unwrap().with_home(agent_home); |
| 645 |
as_agent |
| 646 |
.set_user_config("author.signer", &agent.to_hex()) |
| 647 |
.unwrap(); |
| 648 |
as_agent.set_user_config("core.sign", "change").unwrap(); |
| 649 |
assert_eq!(as_agent.config.author_key, Some(owner)); |
| 650 |
assert_eq!(as_agent.signing_key(), Some(agent)); |
| 651 |
write(&work, "a.txt", b"by the agent\n"); |
| 652 |
as_agent.snapshot("t").unwrap(); |
| 653 |
let h = as_agent.commit("Agent: edit a", &[]).unwrap(); |
| 654 |
let meta = as_agent.store().get_change_meta(&h).unwrap(); |
| 655 |
assert_eq!(meta.author, owner, "the owner stays the author"); |
| 656 |
assert!(matches!( |
| 657 |
as_agent.verify_change(&h).unwrap(), |
| 658 |
SignatureStatus::Valid { key, .. } if key == agent |
| 659 |
)); |
| 660 |
assert_eq!(as_agent.revset("delegated()").unwrap().hashes, vec![h]); |
| 661 |
|
| 662 |
|
| 663 |
let r = as_agent.push("origin", false).unwrap(); |
| 664 |
assert!(r.iter().all(|x| !x.ok), "{r:?}"); |
| 665 |
assert!(!host.bookmark_heads().unwrap().contains(&h)); |
| 5cde7882 Andy McCall ↶ |
666 |
|
| 667 |
|
| 668 |
let r = user.push("origin", false).unwrap(); |
| ff1a64f8 Andy McCall ↶ |
669 |
assert!(r.iter().all(|x| x.ok), "{r:?}"); |
| 670 |
assert!(host.bookmark_heads().unwrap().contains(&h)); |
| 671 |
assert!(matches!( |
| 672 |
host.authenticate_change(&h).unwrap(), |
| 673 |
SignatureStatus::Valid { key, .. } if key == agent |
| 674 |
)); |
| 675 |
} |
| 676 |
|
| 7ba155f2 Andy McCall ↶ |
677 |
|
| 678 |
|
| 679 |
#[test] |
| 680 |
fn an_expired_delegation_is_refused_and_the_delegator_may_revoke() { |
| 681 |
let dir = tempfile::tempdir().unwrap(); |
| 682 |
let (_, mut user, _, _, _) = setup(dir.path(), false, false); |
| 683 |
let agent_sk = SecretKey::generate(); |
| 684 |
let agent_id = agent_sk.identity("agent", "agent@example", 1); |
| 685 |
let agent = agent_id.hash(); |
| 686 |
let yesterday = Hlc::now().wall_ms - 24 * 3600 * 1000; |
| 687 |
user.delegate_add(agent_id, Scope::CHANGES, Some(yesterday)) |
| 688 |
.unwrap(); |
| 689 |
user.config.set("core.sign", "seal").unwrap(); |
| 690 |
user.save_config().unwrap(); |
| 691 |
write(&dir.path().join("work"), "a.txt", b"later\n"); |
| 692 |
user.snapshot("t").unwrap(); |
| 693 |
let h = user.commit("after the delegation ended", &[]).unwrap(); |
| 694 |
user.store() |
| 695 |
.put_signature(&h, &agent_sk.sign(Domain::Change, &agent, &h)) |
| 696 |
.unwrap(); |
| 697 |
let status = user.verify_change(&h).unwrap(); |
| 698 |
assert!( |
| 699 |
matches!(&status, SignatureStatus::Invalid(e) if e.contains("expired")), |
| 700 |
"{status:?}" |
| 701 |
); |
| 702 |
|
| 703 |
let stranger = SecretKey::generate().identity("x", "x@example", 1).hash(); |
| 704 |
assert!(user.revoke_identity(&stranger, "").is_err()); |
| 705 |
assert!(user.revoke_identity(&agent, "session over").is_ok()); |
| 706 |
assert!(user.is_revoked(&agent).unwrap()); |
| 707 |
let r = user.delegate_remove(&agent).unwrap(); |
| 708 |
assert!(user.identity(&r.new).unwrap().delegates.is_empty()); |
| 84b9e9ac Andy McCall ↶ |
709 |
} |
| 710 |
|
| 711 |
#[test] |
| 712 |
fn a_host_edit_that_removes_markers_resolves_the_conflict() { |
| 713 |
let dir = tempfile::tempdir().unwrap(); |
| 714 |
let (host, mut user, hub_key, hub_sk, hub_id) = setup(dir.path(), false, true); |
| 715 |
let author = user.config.author_key.unwrap(); |
| 716 |
let base = host.bookmark_heads().unwrap(); |
| 717 |
|
| eae59556 Andy McCall ↶ |
718 |
let a = host |
| 719 |
.build_change( |
| 720 |
&base, |
| 721 |
&[FileEdit { |
| 722 |
path: "a.txt".into(), |
| 723 |
content: Some(b"ONE\ntwo\n".to_vec()), |
| 724 |
}], |
| 725 |
"A", |
| 726 |
author, |
| 727 |
) |
| 728 |
.unwrap(); |
| 729 |
let b = host |
| 730 |
.build_change( |
| 731 |
&base, |
| 732 |
&[FileEdit { |
| 733 |
path: "a.txt".into(), |
| 734 |
content: Some(b"uno\ntwo\n".to_vec()), |
| 735 |
}], |
| 736 |
"B", |
| 737 |
author, |
| 738 |
) |
| 739 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
740 |
let (ha, hb) = (a.hash(), b.hash()); |
| 741 |
let ids = vec![hub_id.clone(), user.identity(&author).unwrap()]; |
| 742 |
for (c, h) in [(a, ha), (b, hb)] { |
| 743 |
let sig = hub_sk.sign(Domain::Change, &hub_key, &h); |
| 744 |
let mut heads = host.bookmark_heads().unwrap(); |
| 745 |
heads.retain(|x| !base.contains(x)); |
| 746 |
heads.push(h); |
| eae59556 Andy McCall ↶ |
747 |
let update = host |
| 748 |
.host_ref_update("main", &heads, Some((&hub_key, &hub_sk))) |
| 749 |
.unwrap(); |
| 750 |
let r = host |
| 751 |
.apply_hosted(Some((c, vec![sig])), &ids, update) |
| 752 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
753 |
assert!(r.results.iter().all(|x| x.ok), "{:?}", r.results); |
| 754 |
} |
| 755 |
let heads = host.bookmark_heads().unwrap(); |
| 756 |
assert_eq!(heads.len(), 2); |
| 757 |
let marked = String::from_utf8(host.file_at("a.txt", Some(&heads)).unwrap()).unwrap(); |
| 758 |
assert!(marked.contains("<<<<<<< oxy conflict"), "{marked}"); |
| 759 |
|
| eae59556 Andy McCall ↶ |
760 |
assert!(host |
| 761 |
.build_change( |
| 762 |
&heads, |
| 763 |
&[FileEdit { |
| 764 |
path: "a.txt".into(), |
| 765 |
content: Some(format!("{marked}x\n").into_bytes()) |
| 766 |
}], |
| 767 |
"half", |
| 768 |
author |
| 769 |
) |
| 770 |
.is_err()); |
| 771 |
let res = host |
| 772 |
.build_change( |
| 773 |
&heads, |
| 774 |
&[FileEdit { |
| 775 |
path: "a.txt".into(), |
| 776 |
content: Some(b"One\ntwo\n".to_vec()), |
| 777 |
}], |
| 778 |
"Resolve", |
| 779 |
author, |
| 780 |
) |
| 781 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
782 |
assert_eq!(res.kind, ChangeKind::Resolution); |
| eae59556 Andy McCall ↶ |
783 |
assert!( |
| 784 |
res.deps.contains(&ha) && res.deps.contains(&hb), |
| 785 |
"a resolution depends on both sides" |
| 786 |
); |
| 84b9e9ac Andy McCall ↶ |
787 |
let hr = res.hash(); |
| 788 |
let sig = hub_sk.sign(Domain::Change, &hub_key, &hr); |
| eae59556 Andy McCall ↶ |
789 |
let update = host |
| 790 |
.host_ref_update("main", &[hr], Some((&hub_key, &hub_sk))) |
| 791 |
.unwrap(); |
| 792 |
let r = host |
| 793 |
.apply_hosted(Some((res, vec![sig])), &ids, update) |
| 794 |
.unwrap(); |
| 84b9e9ac Andy McCall ↶ |
795 |
assert!(r.results.iter().all(|x| x.ok), "{:?}", r.results); |
| 796 |
assert_eq!(host.file_at("a.txt", Some(&[hr])).unwrap(), b"One\ntwo\n"); |
| eae59556 Andy McCall ↶ |
797 |
assert!(!host |
| 798 |
.load_state(&[hr]) |
| 799 |
.unwrap() |
| 800 |
.materialize() |
| 801 |
.is_conflicted()); |
| 84b9e9ac Andy McCall ↶ |
802 |
let _ = &mut user; |
| 803 |
} |
| 804 |
} |