5b9c6ad4 Andy McCall 1 month ago ↶
1
2
750251b2 Andy McCall 1 month ago ↶
3
4
5
6
7
5b9c6ad4 Andy McCall 1 month ago ↶
8
9
use crate :: attest:: Attest;
750251b2 Andy McCall 1 month ago ↶
10
use crate :: cbor:: { self , Value} ;
5b9c6ad4 Andy McCall 1 month ago ↶
11
use crate :: hash:: { ChangeHash, ObjectRef, ObjectType} ;
12
use crate :: identity:: Signature;
750251b2 Andy McCall 1 month ago ↶
13
use crate :: objects:: { self as o, Change, ObjectError} ;
83e0677b Andy McCall 1 month ago ↶
14
use crate :: policy:: Policy;
5b9c6ad4 Andy McCall 1 month ago ↶
15
use crate :: repo:: { Repo, RepoError, Result } ;
2b5ee371 Andy McCall 1 month ago ↶
16
use std:: path:: { Path, PathBuf} ;
9f9e72b9 Andy McCall 1 month ago ↶
17
use std:: sync:: { Arc, Mutex} ;
5b9c6ad4 Andy McCall 1 month ago ↶
18
750251b2 Andy McCall 1 month ago ↶
19
pub const CONTENT_TYPE : & str = " application/oxy-cbor" ;
20
pub const PROTOCOL_VERSION : u64 = 1 ;
21
5b9c6ad4 Andy McCall 1 month ago ↶
22
23
# [ derive ( Clone, Debug, PartialEq, Eq ) ]
24
pub struct SignedAttest {
25
pub attest : Attest,
26
pub sigs : Vec< Signature> ,
27
}
28
83e0677b Andy McCall 1 month ago ↶
29
30
# [ derive ( Clone, Debug, PartialEq, Eq ) ]
31
pub struct SignedPolicy {
32
pub policy : Policy,
33
pub sigs : Vec< Signature> ,
34
}
35
28fc23d7 Andy McCall 1 month ago ↶
36
impl SignedPolicy {
37
38
pub fn encode ( & self ) -> Vec< u8 > {
39
crate :: cbor:: encode( & wire:: policy_value( self ) )
40
}
41
pub fn decode ( b : & [u8 ]) -> Result< SignedPolicy> {
75167672 Andy McCall 1 month ago
42
wire:: policy_from(
43
& crate :: cbor:: decode( b) .map_err ( | e | RepoError:: Object( ObjectError:: from( e) ) ) ? ,
44
)
28fc23d7 Andy McCall 1 month ago ↶
45
}
46
}
47
132b884b Andy McCall 1 month ago ↶
48
83e0677b Andy McCall 1 month ago ↶
49
# [ derive ( Clone, Debug, Default ) ]
50
pub struct RefsResponse {
51
pub refs : Vec< RemoteRef> ,
52
pub policy : Option< SignedPolicy> ,
132b884b Andy McCall 1 month ago ↶
53
pub tags : Vec< TagRef> ,
28fc23d7 Andy McCall 1 month ago ↶
54
55
56
pub history : Vec< SignedPolicy> ,
14edbaaf Andy McCall 1 month ago ↶
57
58
pub default_bookmark : Option< String> ,
4b9d0677 Andy McCall 18 days ago ↶
59
60
61
62
pub log : Option< String> ,
028545df Andy McCall 17 days ago ↶
63
64
65
66
pub witnesses : Vec< ( String, Vec< u8 > ) > ,
b63fc7a7 Andy McCall 11 days ago ↶
67
68
69
70
71
pub withdrawn : Vec< TagRef> ,
83e0677b Andy McCall 1 month ago ↶
72
}
73
4b9d0677 Andy McCall 18 days ago ↶
74
75
76
77
# [ derive ( Clone, Debug, PartialEq, Eq ) ]
78
pub struct Logged {
79
pub index : u64 ,
80
pub entry : Vec< u8 > ,
81
pub proof : Vec< [ u8 ; 32 ] > ,
82
}
83
028545df Andy McCall 17 days ago ↶
84
85
86
87
# [ derive ( Clone, Debug, PartialEq, Eq ) ]
88
pub struct Integration {
89
pub note : String,
90
pub proof : Vec< [ u8 ; 32 ] > ,
91
}
92
132b884b Andy McCall 1 month ago ↶
93
94
# [ derive ( Clone, Debug, PartialEq, Eq ) ]
95
pub struct TagRef {
96
pub name : String,
97
pub state : ObjectRef,
98
pub heads : Vec< ChangeHash> ,
99
pub attestation : Option< SignedAttest> ,
1ec6fdbc Andy McCall 15 days ago ↶
100
101
102
103
pub accepted : Option< SignedAttest> ,
104
105
106
pub logged : Option< Logged> ,
107
108
109
pub integration : Vec< Integration> ,
110
}
111
112
impl TagRef {
b63fc7a7 Andy McCall 11 days ago ↶
113
114
115
pub fn is_withdrawal ( & self ) -> bool {
116
self .attestation
117
.as_ref ( )
118
.is_some_and ( | a | matches! ( a.attest.body, crate :: attest:: AttestBody:: Untag { .. } ) )
119
}
120
1ec6fdbc Andy McCall 15 days ago ↶
121
122
pub fn new (
123
name : String,
124
state : ObjectRef,
125
heads : Vec< ChangeHash> ,
126
attestation : Option< SignedAttest> ,
127
) -> TagRef {
128
TagRef {
129
name,
130
state,
131
heads,
132
attestation,
133
accepted: None ,
134
logged: None ,
135
integration: vec! [ ] ,
136
}
137
}
132b884b Andy McCall 1 month ago ↶
138
}
139
5b9c6ad4 Andy McCall 1 month ago ↶
140
141
# [ derive ( Clone, Debug ) ]
142
pub struct RemoteRef {
143
pub name : String,
144
pub state : ObjectRef,
145
pub heads : Vec< ChangeHash> ,
4161262e Andy McCall 1 month ago ↶
146
147
5b9c6ad4 Andy McCall 1 month ago ↶
148
pub attestation : Option< SignedAttest> ,
4161262e Andy McCall 1 month ago ↶
149
150
151
pub last_serial : Option< u64 > ,
f53cbe7a Andy McCall 18 days ago ↶
152
153
154
pub accepted : Option< SignedAttest> ,
4b9d0677 Andy McCall 18 days ago ↶
155
156
157
pub logged : Option< Logged> ,
028545df Andy McCall 17 days ago ↶
158
159
160
pub integration : Vec< Integration> ,
5b9c6ad4 Andy McCall 1 month ago ↶
161
}
162
163
164
# [ derive ( Clone, Debug, Default ) ]
165
pub struct Bundle {
166
167
pub changes : Vec< ( Change, Vec< Signature> ) > ,
168
169
pub attests : Vec< SignedAttest> ,
170
171
pub objects : Vec< ( ObjectType, Vec< u8 > ) > ,
28fc23d7 Andy McCall 1 month ago ↶
172
173
174
pub policies : Vec< SignedPolicy> ,
598009a7 Andy McCall 1 month ago ↶
175
176
177
178
pub hashes : Vec< ChangeHash> ,
2c75672f Andy McCall 1 month ago ↶
179
180
181
182
pub raw_changes : Vec< ( Vec< u8 > , Vec< Signature> ) > ,
f0a517d2 Andy McCall 18 days ago ↶
183
184
185
pub certificates : Vec< crate:: cert:: SignedCertificate> ,
5b9c6ad4 Andy McCall 1 month ago ↶
186
}
187
188
impl Bundle {
189
pub fn is_empty ( & self ) -> bool {
190
self .changes.is_empty ( ) & & self .attests.is_empty ( ) & & self .objects.is_empty ( )
191
}
192
}
193
194
195
# [ derive ( Clone, Debug ) ]
196
pub struct RefUpdate {
197
pub name : String,
198
pub old_state : ObjectRef,
199
pub new_state : ObjectRef,
200
pub force : bool ,
201
pub attestation : Option< SignedAttest> ,
202
}
203
204
# [ derive ( Clone, Debug, PartialEq, Eq ) ]
205
pub struct RefResult {
206
pub name : String,
207
pub ok : bool ,
208
pub reason : String,
209
}
210
211
pub trait Remote {
212
fn repo_id ( & self ) -> Result< [ u8 ; 16 ] > ;
83e0677b Andy McCall 1 month ago ↶
213
fn refs ( & self ) -> Result< RefsResponse> ;
5b9c6ad4 Andy McCall 1 month ago ↶
214
215
fn negotiate ( & self , have : & [ChangeHash], want : & [ChangeHash]) -> Result< Vec< ChangeHash> > ;
b3c5a59a Andy McCall 1 month ago ↶
216
217
218
fn fetch ( & self , changes : & [ChangeHash], max_blob : Option< u64 > ) -> Result< Bundle> ;
219
220
221
fn objects ( & self , refs : & [ObjectRef]) -> Result< Vec< ( ObjectType, Vec< u8 > ) > > ;
2863456a Andy McCall 1 month ago ↶
222
223
224
fn checkpoint ( & self , heads : & [ChangeHash]) -> Result< Bundle> ;
225
226
fn shallow ( & self , heads : & [ChangeHash], depth : u32 ) -> Result< Vec< ChangeHash> > ;
132b884b Andy McCall 1 month ago ↶
227
228
229
75167672 Andy McCall 1 month ago
230
fn push (
231
& self ,
232
bundle : Bundle,
233
updates : Vec< RefUpdate> ,
234
policy : Option< SignedPolicy> ,
235
tags : Vec< TagRef> ,
236
) -> Result< Vec< RefResult> > ;
5b9c6ad4 Andy McCall 1 month ago ↶
237
}
238
750251b2 Andy McCall 1 month ago ↶
239
240
5b9c6ad4 Andy McCall 1 month ago ↶
241
242
pub struct LocalRemote {
243
pub repo : Repo,
84b9e9ac Andy McCall 1 month ago ↶
244
245
246
pub claimed_id : Option< [ u8 ; 16 ] > ,
5b9c6ad4 Andy McCall 1 month ago ↶
247
}
248
249
impl LocalRemote {
250
pub fn open ( path : & Path) -> Result< LocalRemote> {
75167672 Andy McCall 1 month ago
251
Ok ( LocalRemote {
252
repo: Repo:: open( path) ? ,
84b9e9ac Andy McCall 1 month ago ↶
253
claimed_id: None ,
75167672 Andy McCall 1 month ago
254
} )
5b9c6ad4 Andy McCall 1 month ago ↶
255
}
256
}
257
258
impl Remote for LocalRemote {
259
fn repo_id ( & self ) -> Result< [ u8 ; 16 ] > {
260
self .repo.id ( )
261
}
83e0677b Andy McCall 1 month ago ↶
262
fn refs ( & self ) -> Result< RefsResponse> {
5b9c6ad4 Andy McCall 1 month ago ↶
263
self .repo.remote_refs ( )
264
}
265
fn negotiate ( & self , have : & [ChangeHash], want : & [ChangeHash]) -> Result< Vec< ChangeHash> > {
266
self .repo.negotiate ( have, want)
267
}
b3c5a59a Andy McCall 1 month ago ↶
268
fn fetch ( & self , changes : & [ChangeHash], max_blob : Option< u64 > ) -> Result< Bundle> {
2c75672f Andy McCall 1 month ago ↶
269
self .repo.bundle_raw_for ( changes, max_blob)
b3c5a59a Andy McCall 1 month ago ↶
270
}
271
fn objects ( & self , refs : & [ObjectRef]) -> Result< Vec< ( ObjectType, Vec< u8 > ) > > {
272
self .repo.objects_for ( refs)
5b9c6ad4 Andy McCall 1 month ago ↶
273
}
2863456a Andy McCall 1 month ago ↶
274
fn checkpoint ( & self , heads : & [ChangeHash]) -> Result< Bundle> {
275
self .repo.checkpoint_bundle_for ( heads)
276
}
277
fn shallow ( & self , heads : & [ChangeHash], depth : u32 ) -> Result< Vec< ChangeHash> > {
278
self .repo.shallow_changes ( heads, depth)
279
}
75167672 Andy McCall 1 month ago
280
fn push (
281
& self ,
282
bundle : Bundle,
283
updates : Vec< RefUpdate> ,
284
policy : Option< SignedPolicy> ,
285
tags : Vec< TagRef> ,
286
) -> Result< Vec< RefResult> > {
84b9e9ac Andy McCall 1 month ago ↶
287
if self .repo.is_unbound ( ) {
288
match self .claimed_id {
289
Some ( id) => self .repo.bind_id ( id) ? ,
eae59556 Andy McCall 1 month ago ↶
290
None => {
291
return Err ( RepoError:: Msg(
292
" the remote is unbound and this client did not say which repository it is"
293
.into ( ) ,
294
) )
295
}
84b9e9ac Andy McCall 1 month ago ↶
296
}
297
}
132b884b Andy McCall 1 month ago ↶
298
self .repo.receive_push ( bundle, updates, policy, tags)
5b9c6ad4 Andy McCall 1 month ago ↶
299
}
300
}
301
750251b2 Andy McCall 1 month ago ↶
302
303
2b5ee371 Andy McCall 1 month ago ↶
304
305
# [ derive ( Clone, Debug, Default ) ]
306
pub struct RemoteOptions {
307
308
pub token : Option< String> ,
309
310
311
pub ca : Option< PathBuf> ,
75167672 Andy McCall 1 month ago
312
313
314
pub repo_id : Option< [ u8 ; 16 ] > ,
9f9e72b9 Andy McCall 1 month ago ↶
315
316
317
318
319
pub cache_dir : Option< PathBuf> ,
68a92484 Andy McCall 21 days ago ↶
320
321
322
323
324
pub fallback_tokens : Vec< String> ,
2b5ee371 Andy McCall 1 month ago ↶
325
}
326
9f9e72b9 Andy McCall 1 month ago ↶
327
328
329
330
pub type StoreOpener =
331
fn ( & str , & RemoteOptions) -> Option< crate:: strata:: Result< Box< dyn crate:: strata:: Store> > > ;
332
333
static STORE_OPENERS : Mutex< Vec< StoreOpener> > = Mutex:: new( Vec :: new( ) ) ;
334
335
pub fn register_store_opener ( opener : StoreOpener) {
336
STORE_OPENERS
337
.lock ( )
338
.unwrap_or_else ( | p | p.into_inner ( ) )
339
.push ( opener) ;
340
}
341
342
343
pub fn open_store (
344
url : & str ,
345
options : & RemoteOptions,
346
) -> Option< crate:: strata:: Result< Box< dyn crate:: strata:: Store> > > {
347
if let Some ( path) = url.strip_prefix ( " dir://" ) {
348
return Some (
349
crate :: strata:: LocalStore:: open( path)
350
.map ( | s | Box :: new( s) as Box< dyn crate:: strata:: Store> ) ,
351
) ;
352
}
353
let openers = STORE_OPENERS .lock ( ) .unwrap_or_else ( | p | p.into_inner ( ) ) ;
354
openers.iter ( ) .find_map ( | open | open ( url, options) )
355
}
356
75167672 Andy McCall 1 month ago
357
358
pub const REPO_ID_HEADER : & str = " Oxy-Repo-Id" ;
359
360
361
pub const UNBOUND_ID : [ u8 ; 16 ] = [ 0 ; 16 ] ;
362
ee130dd9 Andy McCall 15 days ago ↶
363
364
365
366
367
pub const FEATURES_HEADER : & str = " Oxy-Features" ;
368
369
370
371
372
373
pub const FEATURE_CERTIFIED_TAGS : & str = " certified-tags" ;
374
375
376
pub const CLIENT_FEATURES : & [ & str ] = & [ FEATURE_CERTIFIED_TAGS ] ;
377
378
379
pub fn user_agent ( ) -> String {
380
format! ( " oxy/{} " , env! ( " CARGO_PKG_VERSION" ) )
381
}
382
383
384
385
386
pub fn declares_feature ( header : Option< & str > , feature : & str ) -> bool {
387
header.is_some_and ( | h | h.split ( ' ,' ) .any ( | f | f.trim ( ) .eq_ignore_ascii_case ( feature) ) )
388
}
389
750251b2 Andy McCall 1 month ago ↶
390
391
pub struct HttpRemote {
392
base : String,
68a92484 Andy McCall 21 days ago ↶
393
394
token : Mutex< Option< String> > ,
395
fallbacks : Mutex< Vec< String> > ,
75167672 Andy McCall 1 month ago
396
repo_id : Option< [ u8 ; 16 ] > ,
750251b2 Andy McCall 1 month ago ↶
397
agent : ureq:: Agent,
398
}
399
11c95802 Andy McCall 1 month ago ↶
400
401
402
pub fn tls_client_config ( ca : Option< & Path> ) -> Result< Arc< rustls:: ClientConfig> > {
2b5ee371 Andy McCall 1 month ago ↶
403
let mut roots = rustls:: RootCertStore:: empty( ) ;
404
roots.extend ( webpki_roots:: TLS_SERVER_ROOTS .iter ( ) .cloned ( ) ) ;
405
if let Some ( path) = ca {
75167672 Andy McCall 1 month ago
406
let pem = std:: fs:: read( path)
407
.map_err ( | e | RepoError:: Msg( format! ( " reading CA file {} : {e} " , path.display ( ) ) ) ) ? ;
2b5ee371 Andy McCall 1 month ago ↶
408
let certs = rustls_pemfile:: certs( & mut pem.as_slice ( ) )
409
.collect:: < std:: result:: Result< Vec< _ > , _ > > ( )
410
.map_err ( | e | RepoError:: Msg( format! ( " parsing CA file {} : {e} " , path.display ( ) ) ) ) ? ;
411
if certs.is_empty ( ) {
75167672 Andy McCall 1 month ago
412
return Err ( RepoError:: Msg( format! (
413
" CA file {} holds no certificates" ,
414
path.display ( )
415
) ) ) ;
2b5ee371 Andy McCall 1 month ago ↶
416
}
417
for c in certs {
75167672 Andy McCall 1 month ago
418
roots
419
.add ( c)
420
.map_err ( | e | RepoError:: Msg( format! ( " CA file {} : {e} " , path.display ( ) ) ) ) ? ;
2b5ee371 Andy McCall 1 month ago ↶
421
}
422
}
75167672 Andy McCall 1 month ago
423
let cfg = rustls:: ClientConfig:: builder_with_provider( Arc:: new(
424
rustls:: crypto:: ring:: default_provider( ) ,
425
) )
426
.with_safe_default_protocol_versions ( )
427
.map_err ( | e | RepoError:: Msg( e.to_string ( ) ) ) ?
428
.with_root_certificates ( roots)
429
.with_no_client_auth ( ) ;
2b5ee371 Andy McCall 1 month ago ↶
430
Ok ( Arc:: new( cfg) )
431
}
432
750251b2 Andy McCall 1 month ago ↶
433
impl HttpRemote {
2b5ee371 Andy McCall 1 month ago ↶
434
pub fn new ( base : & str , options : & RemoteOptions) -> Result< HttpRemote> {
ff1f62fe Andy McCall 1 month ago ↶
435
refuse_plain_token ( base, options) ? ;
75167672 Andy McCall 1 month ago
436
let agent = ureq:: AgentBuilder:: new( )
437
.tls_config ( tls_client_config ( options.ca.as_deref ( ) ) ? )
ee130dd9 Andy McCall 15 days ago ↶
438
.user_agent ( & user_agent ( ) )
75167672 Andy McCall 1 month ago
439
.build ( ) ;
440
Ok ( HttpRemote {
441
base: base.trim_end_matches ( ' /' ) .to_string ( ) ,
68a92484 Andy McCall 21 days ago ↶
442
token: Mutex:: new( options.token.clone ( ) ) ,
443
fallbacks: Mutex:: new( options.fallback_tokens.iter ( ) .rev ( ) .cloned ( ) .collect ( ) ) ,
75167672 Andy McCall 1 month ago
444
repo_id: options.repo_id,
445
agent,
446
} )
750251b2 Andy McCall 1 month ago ↶
447
}
448
68a92484 Andy McCall 21 days ago ↶
449
450
750251b2 Andy McCall 1 month ago ↶
451
fn call ( & self , post : bool , endpoint : & str , body : Option< & [ u8 ] > ) -> Result< Vec< u8 > > {
68a92484 Andy McCall 21 days ago ↶
452
loop {
453
let token = self .token.lock ( ) .unwrap_or_else ( | p | p.into_inner ( ) ) .clone ( ) ;
454
match self .call_once ( post, endpoint, body, token.as_deref ( ) ) {
455
Err ( RepoError:: Msg( m) ) if token.is_some ( ) & & refused ( & m) => {
456
let next = self
457
.fallbacks
458
.lock ( )
459
.unwrap_or_else ( | p | p.into_inner ( ) )
460
.pop ( ) ;
461
let Some ( next) = next else {
462
return Err ( RepoError:: Msg( m) ) ;
463
} ;
464
crate :: credentials:: warn_once( & format! (
465
" {} : the stored token was refused; using the one an enclosing checkout has for this hub" ,
466
self .base
467
) ) ;
468
* self .token.lock ( ) .unwrap_or_else ( | p | p.into_inner ( ) ) = Some ( next) ;
469
}
470
other => return other,
471
}
472
}
473
}
474
475
fn call_once (
476
& self ,
477
post : bool ,
478
endpoint : & str ,
479
body : Option< & [ u8 ] > ,
480
token : Option< & str > ,
481
) -> Result< Vec< u8 > > {
750251b2 Andy McCall 1 month ago ↶
482
let url = format! ( " {} /oxy/v1/{endpoint} " , self .base) ;
75167672 Andy McCall 1 month ago
483
let mut req = if post {
484
self .agent.post ( & url)
485
} else {
486
self .agent.get ( & url)
487
} ;
ee130dd9 Andy McCall 15 days ago ↶
488
req = req
489
.set ( " Accept" , CONTENT_TYPE )
490
.set ( FEATURES_HEADER , & CLIENT_FEATURES .join ( " ," ) ) ;
68a92484 Andy McCall 21 days ago ↶
491
if let Some ( t) = token {
750251b2 Andy McCall 1 month ago ↶
492
req = req.set ( " Authorization" , & format! ( " Bearer {t} " ) ) ;
493
}
75167672 Andy McCall 1 month ago
494
if let Some ( id) = & self .repo_id {
495
req = req.set ( REPO_ID_HEADER , & hex:: encode( id) ) ;
496
}
750251b2 Andy McCall 1 month ago ↶
497
let resp = match body {
498
Some ( b) => req.set ( " Content-Type" , CONTENT_TYPE ) .send_bytes ( b) ,
499
None => req.call ( ) ,
500
} ;
501
match resp {
502
Ok ( r) => {
503
let mut out = Vec :: new( ) ;
75167672 Andy McCall 1 month ago
504
r.into_reader ( )
505
.read_to_end ( & mut out)
506
.map_err ( | e | RepoError:: Msg( format! ( " {url} : {e} " ) ) ) ? ;
750251b2 Andy McCall 1 month ago ↶
507
Ok ( out)
508
}
509
Err ( ureq:: Error:: Status( code, r) ) => {
510
let text = r.into_string ( ) .unwrap_or_default ( ) ;
67a4fe1a Andy McCall 16 days ago ↶
511
512
d3b27090 Andy McCall 11 days ago ↶
513
514
515
67a4fe1a Andy McCall 16 days ago ↶
516
let status = format! ( " {url} : HTTP {code} " ) ;
d3b27090 Andy McCall 11 days ago ↶
517
let msg = format! ( " {status} : {} " , plain_error_text ( & text) ) ;
67a4fe1a Andy McCall 16 days ago ↶
518
Err ( RepoError:: Msg( match hint_for ( & status) {
519
Some ( h) => format! ( " {msg} \n hint: {h} " ) ,
520
None => msg,
521
} ) )
750251b2 Andy McCall 1 month ago ↶
522
}
3e5772cc Andy McCall 1 month ago ↶
523
524
Err ( e) => Err ( RepoError:: Msg( with_hint ( e.to_string ( ) ) ) ) ,
750251b2 Andy McCall 1 month ago ↶
525
}
526
}
68a92484 Andy McCall 21 days ago ↶
527
}
528
529
530
531
532
fn refused ( message : & str ) -> bool {
533
[ " : HTTP 401" , " : HTTP 403" , " : HTTP 404" ]
534
.iter ( )
535
.any ( | c | message.contains ( c) )
750251b2 Andy McCall 1 month ago ↶
536
}
537
ff1f62fe Andy McCall 1 month ago ↶
538
539
540
fn refuse_plain_token ( url : & str , options : & RemoteOptions) -> Result< ( ) > {
541
if options.token.is_some ( ) & & ! crate :: credentials:: token_allowed( url) {
542
return Err ( RepoError:: Msg( format! (
543
" {url} : refusing to send a token over plain http; use https"
544
) ) ) ;
545
}
546
Ok ( ( ) )
547
}
548
3e5772cc Andy McCall 1 month ago ↶
549
550
pub fn hint_for ( message : & str ) -> Option< & 'static str > {
551
let m = message.to_ascii_lowercase ( ) ;
552
let hint = if m.contains ( " http 401" ) {
ff1f62fe Andy McCall 1 month ago ↶
553
" the hub needs you signed in, or did not accept the token: run `oxy auth login HUB` (pushing needs repo:write)"
3e5772cc Andy McCall 1 month ago ↶
554
} else if m.contains ( " http 403" ) {
555
" the token or account lacks permission: pushing needs a token with repo:write and write access to the repository"
d3b27090 Andy McCall 11 days ago ↶
556
} else if m.contains ( " http 413" ) {
557
" the request was larger than the server, or a proxy in front of it, takes at once (Cloudflare's limit is 100 MB): push in smaller parts with `oxy config push.batch-bytes 33554432`, then push again"
3e5772cc Andy McCall 1 month ago ↶
558
} else if m.contains ( " http 404" ) {
ff1f62fe Andy McCall 1 month ago ↶
559
" nothing at this URL that you can see: check the owner and repository name; a private repository needs you signed in (`oxy auth login HUB`)"
3e5772cc Andy McCall 1 month ago ↶
560
} else if m.contains ( " unknownissuer" )
561
| | m.contains ( " unknown issuer" )
562
| | m.contains ( " certificate" )
563
| | m.contains ( " invalidcertificate" )
564
{
565
" the server's certificate is not trusted: for a hub with a self-signed certificate, download it from /.well-known/oxyhub-ca.pem on the hub and run `oxy config remote.<remote>.ca FILE`"
566
} else if m.contains ( " connection refused" ) {
567
" nothing is listening there: check the URL and port, and that the server is running"
568
} else if m.contains ( " dns" )
569
| | m.contains ( " failed to lookup" )
570
| | m.contains ( " name or service not known" )
571
{
572
" the host name does not resolve: check the URL"
573
} else if m.contains ( " timed out" ) | | m.contains ( " timeout" ) {
574
" the server did not answer in time: check the network, or try again"
575
} else {
576
return None ;
577
} ;
578
Some ( hint)
579
}
580
d3b27090 Andy McCall 11 days ago ↶
581
582
583
fn plain_error_text ( text : & str ) -> String {
584
let t = text.trim ( ) ;
585
let lower = t.to_ascii_lowercase ( ) ;
586
if ! ( lower.starts_with ( " <!doctype html" ) | | lower.starts_with ( " <html" ) ) {
587
return t.to_string ( ) ;
588
}
589
let title = lower
590
.find ( " <title>" )
591
.and_then ( | i | {
592
lower[ i + 7 .. ]
593
.find ( " </title>" )
594
.map ( | j | t[ i + 7 .. i + 7 + j] .trim ( ) )
595
} )
596
.filter ( | s | ! s.is_empty ( ) ) ;
597
match title {
598
Some ( s) => format! ( " {s} (an HTML error page, from a proxy in front of the server perhaps)" ) ,
599
None => " an HTML error page (from a proxy in front of the server perhaps)" .into ( ) ,
600
}
601
}
602
3e5772cc Andy McCall 1 month ago ↶
603
604
pub fn with_hint ( message : String) -> String {
605
match hint_for ( & message) {
606
Some ( h) => format! ( " {message} \n hint: {h} " ) ,
607
None => message,
ff1f62fe Andy McCall 1 month ago ↶
608
}
609
}
610
611
612
613
614
pub fn http_json (
615
method : & str ,
616
url : & str ,
617
body : Option< & str > ,
618
options : & RemoteOptions,
619
) -> Result< ( u16 , String) > {
620
refuse_plain_token ( url, options) ? ;
621
let agent = ureq:: AgentBuilder:: new( )
622
.tls_config ( tls_client_config ( options.ca.as_deref ( ) ) ? )
623
.timeout ( std:: time:: Duration:: from_secs( 60 ) )
ee130dd9 Andy McCall 15 days ago ↶
624
.user_agent ( & user_agent ( ) )
ff1f62fe Andy McCall 1 month ago ↶
625
.build ( ) ;
626
let mut req = agent.request ( method, url) .set ( " Accept" , " application/json" ) ;
627
if let Some ( t) = & options.token {
628
req = req.set ( " Authorization" , & format! ( " Bearer {t} " ) ) ;
629
}
630
let resp = match body {
631
Some ( b) => req.set ( " Content-Type" , " application/json" ) .send_string ( b) ,
632
None => req.call ( ) ,
633
} ;
634
match resp {
635
Ok ( r) => {
636
let code = r.status ( ) ;
637
Ok ( ( code, r.into_string ( ) .unwrap_or_default ( ) ) )
638
}
639
Err ( ureq:: Error:: Status( code, r) ) => Ok ( ( code, r.into_string ( ) .unwrap_or_default ( ) ) ) ,
640
Err ( e) => Err ( RepoError:: Msg( with_hint ( e.to_string ( ) ) ) ) ,
3e5772cc Andy McCall 1 month ago ↶
641
}
642
}
643
d82e4409 Andy McCall 1 month ago ↶
644
645
646
pub fn http_get ( url : & str , options : & RemoteOptions) -> Result< Vec< u8 > > {
ff1f62fe Andy McCall 1 month ago ↶
647
refuse_plain_token ( url, options) ? ;
d82e4409 Andy McCall 1 month ago ↶
648
let agent = ureq:: AgentBuilder:: new( )
649
.tls_config ( tls_client_config ( options.ca.as_deref ( ) ) ? )
ee130dd9 Andy McCall 15 days ago ↶
650
.user_agent ( & user_agent ( ) )
d82e4409 Andy McCall 1 month ago ↶
651
.build ( ) ;
652
let mut req = agent.get ( url) .set ( " Accept" , " application/json" ) ;
653
if let Some ( t) = & options.token {
654
req = req.set ( " Authorization" , & format! ( " Bearer {t} " ) ) ;
655
}
656
match req.call ( ) {
657
Ok ( r) => {
658
let mut out = Vec :: new( ) ;
659
r.into_reader ( )
660
.read_to_end ( & mut out)
661
.map_err ( | e | RepoError:: Msg( format! ( " {url} : {e} " ) ) ) ? ;
662
Ok ( out)
663
}
664
Err ( ureq:: Error:: Status( code, r) ) => {
665
let text = r.into_string ( ) .unwrap_or_default ( ) ;
3e5772cc Andy McCall 1 month ago ↶
666
Err ( RepoError:: Msg( with_hint ( format! (
4213c41d Andy McCall 1 month ago ↶
667
" {url} : HTTP {code} : {} " ,
668
text.trim ( )
3e5772cc Andy McCall 1 month ago ↶
669
) ) ) )
d82e4409 Andy McCall 1 month ago ↶
670
}
3e5772cc Andy McCall 1 month ago ↶
671
672
Err ( e) => Err ( RepoError:: Msg( with_hint ( e.to_string ( ) ) ) ) ,
d82e4409 Andy McCall 1 month ago ↶
673
}
674
}
675
750251b2 Andy McCall 1 month ago ↶
676
impl Remote for HttpRemote {
677
fn repo_id ( & self ) -> Result< [ u8 ; 16 ] > {
678
wire:: decode_capabilities( & self .call ( false , " capabilities" , None ) ? )
679
}
83e0677b Andy McCall 1 month ago ↶
680
fn refs ( & self ) -> Result< RefsResponse> {
750251b2 Andy McCall 1 month ago ↶
681
Ok ( wire:: decode_refs( & self .call ( false , " refs" , None ) ? ) ? .1 )
682
}
683
fn negotiate ( & self , have : & [ChangeHash], want : & [ChangeHash]) -> Result< Vec< ChangeHash> > {
75167672 Andy McCall 1 month ago
684
wire:: decode_hashes( & self .call (
685
true ,
686
" negotiate" ,
687
Some ( & wire:: encode_negotiate( have, want) ) ,
688
) ? )
750251b2 Andy McCall 1 month ago ↶
689
}
b3c5a59a Andy McCall 1 month ago ↶
690
fn fetch ( & self , changes : & [ChangeHash], max_blob : Option< u64 > ) -> Result< Bundle> {
75167672 Andy McCall 1 month ago
691
wire:: decode_bundle( & self .call (
692
true ,
693
" fetch" ,
694
Some ( & wire:: encode_fetch( changes, max_blob) ) ,
695
) ? )
750251b2 Andy McCall 1 month ago ↶
696
}
b3c5a59a Andy McCall 1 month ago ↶
697
fn objects ( & self , refs : & [ObjectRef]) -> Result< Vec< ( ObjectType, Vec< u8 > ) > > {
75167672 Andy McCall 1 month ago
698
Ok (
699
wire:: decode_bundle( & self .call ( true , " objects" , Some ( & wire:: encode_hashes( refs) ) ) ? ) ?
700
.objects,
701
)
b3c5a59a Andy McCall 1 month ago ↶
702
}
2863456a Andy McCall 1 month ago ↶
703
fn checkpoint ( & self , heads : & [ChangeHash]) -> Result< Bundle> {
f3811eb2 Andy McCall 11 days ago ↶
704
let body = {
705
let _t = crate :: timing:: phase( " remote: checkpoint answer" ) ;
706
self .call ( true , " checkpoint" , Some ( & wire:: encode_hashes( heads) ) ) ?
707
} ;
708
let _t = crate :: timing:: phase( " remote: decode the checkpoint" ) ;
709
wire:: decode_bundle( & body)
2863456a Andy McCall 1 month ago ↶
710
}
711
fn shallow ( & self , heads : & [ChangeHash], depth : u32 ) -> Result< Vec< ChangeHash> > {
75167672 Andy McCall 1 month ago
712
wire:: decode_hashes( & self .call (
713
true ,
714
" shallow" ,
715
Some ( & wire:: encode_shallow( heads, depth) ) ,
716
) ? )
2863456a Andy McCall 1 month ago ↶
717
}
75167672 Andy McCall 1 month ago
718
fn push (
719
& self ,
720
bundle : Bundle,
721
updates : Vec< RefUpdate> ,
722
policy : Option< SignedPolicy> ,
723
tags : Vec< TagRef> ,
724
) -> Result< Vec< RefResult> > {
725
wire:: decode_push_response( & self .call (
726
true ,
727
" push" ,
728
Some ( & wire:: encode_push(
729
& bundle,
730
& updates,
731
policy.as_ref ( ) ,
732
& tags,
733
) ) ,
734
) ? )
750251b2 Andy McCall 1 month ago ↶
735
}
736
}
737
827281ee Andy McCall 1 month ago ↶
738
739
740
741
pub fn stored_url ( url : & str ) -> String {
8d803563 Andy McCall 1 month ago ↶
742
743
9f9e72b9 Andy McCall 1 month ago ↶
744
let ( prefix, path) = match ( url.strip_prefix ( " file://" ) , url.strip_prefix ( " dir://" ) ) {
745
( Some ( p) , _ ) => ( " file://" , p) ,
746
( _ , Some ( p) ) => ( " dir://" , p) ,
8d803563 Andy McCall 1 month ago ↶
747
_ if url.contains ( " ://" ) => return url.to_string ( ) ,
9f9e72b9 Andy McCall 1 month ago ↶
748
_ => ( " " , url) ,
827281ee Andy McCall 1 month ago ↶
749
} ;
750
match std:: path:: absolute( path) {
751
Ok ( abs) => format! ( " {prefix} {} " , abs.display ( ) ) ,
752
Err ( _ ) => url.to_string ( ) ,
753
}
754
}
755
9f9e72b9 Andy McCall 1 month ago ↶
756
757
2b5ee371 Andy McCall 1 month ago ↶
758
pub fn open_remote ( url : & str , options : & RemoteOptions) -> Result< Box< dyn Remote> > {
750251b2 Andy McCall 1 month ago ↶
759
if url.starts_with ( " http://" ) | | url.starts_with ( " https://" ) {
2b5ee371 Andy McCall 1 month ago ↶
760
return Ok ( Box :: new( HttpRemote:: new( url, options) ? ) ) ;
9f9e72b9 Andy McCall 1 month ago ↶
761
}
762
if let Some ( store) = open_store ( url, options) {
763
return crate :: repo:: strata:: open_store_remote( store? , options) ;
750251b2 Andy McCall 1 month ago ↶
764
}
5b9c6ad4 Andy McCall 1 month ago ↶
765
let path = url.strip_prefix ( " file://" ) .unwrap_or ( url) ;
766
if path.contains ( " ://" ) {
750251b2 Andy McCall 1 month ago ↶
767
return Err ( RepoError:: Msg( format! ( " unsupported remote URL {url:?} " ) ) ) ;
5b9c6ad4 Andy McCall 1 month ago ↶
768
}
84b9e9ac Andy McCall 1 month ago ↶
769
let mut local = LocalRemote:: open( Path:: new( path) ) ? ;
770
local.claimed_id = options.repo_id;
771
Ok ( Box :: new( local) )
5b9c6ad4 Andy McCall 1 month ago ↶
772
}
750251b2 Andy McCall 1 month ago ↶
773
774
775
776
777
pub mod wire {
778
use super :: * ;
779
780
fn schema ( msg : & str ) -> RepoError {
781
RepoError:: Object( ObjectError:: Schema( msg.into ( ) ) )
782
}
783
fn sigs_value ( sigs : & [Signature]) -> Value {
784
Value:: Array( sigs.iter ( ) .map ( | s | s.to_value ( ) ) .collect ( ) )
785
}
786
fn sigs_from ( v : & Value) -> Result< Vec< Signature> > {
75167672 Andy McCall 1 month ago
787
Ok ( o:: array( v, " signatures" ) ?
788
.iter ( )
789
.map ( Signature:: from_value)
790
.collect:: < std:: result:: Result< Vec< _ > , _ > > ( ) ? )
750251b2 Andy McCall 1 month ago ↶
791
}
792
fn hashes_value ( hs : & [ObjectRef]) -> Value {
793
Value:: Array( hs.iter ( ) .map ( | h | Value:: Bytes( h.0. to_vec ( ) ) ) .collect ( ) )
794
}
795
fn hashes_from ( v : & Value) -> Result< Vec< ObjectRef> > {
75167672 Andy McCall 1 month ago
796
Ok ( o:: array( v, " hashes" ) ?
797
.iter ( )
798
.map ( | h | o:: oref( h, " hash" ) )
799
.collect:: < std:: result:: Result< Vec< _ > , _ > > ( ) ? )
750251b2 Andy McCall 1 month ago ↶
800
}
801
fn attest_value ( sa : & SignedAttest) -> Value {
802
Value:: Array( vec! [ Value:: Bytes( sa.attest.encode ( ) ) , sigs_value ( & sa.sigs) ] )
803
}
804
fn attest_from ( v : & Value) -> Result< SignedAttest> {
805
let a = o:: array( v, " signed attest" ) ? ;
806
if a.len ( ) ! = 2 {
807
return Err ( schema ( " signed attest: expected [attest, sigs]" ) ) ;
808
}
75167672 Andy McCall 1 month ago
809
Ok ( SignedAttest {
810
attest: Attest:: decode( o:: bytes( & a[ 0 ] , " attest" ) ? ) ? ,
811
sigs: sigs_from ( & a[ 1 ] ) ? ,
812
} )
750251b2 Andy McCall 1 month ago ↶
813
}
814
fn opt_attest_value ( sa : & Option< SignedAttest> ) -> Value {
815
sa.as_ref ( ) .map ( attest_value) .unwrap_or ( Value:: Null)
816
}
817
fn opt_attest_from ( v : & Value) -> Result< Option< SignedAttest> > {
75167672 Andy McCall 1 month ago
818
if v.is_null ( ) {
819
Ok ( None )
820
} else {
821
attest_from ( v) .map ( Some )
822
}
750251b2 Andy McCall 1 month ago ↶
823
}
28fc23d7 Andy McCall 1 month ago ↶
824
825
826
pub fn policy_value ( sp : & SignedPolicy) -> Value {
827
Value:: Array( vec! [ Value:: Bytes( sp.policy.encode ( ) ) , sigs_value ( & sp.sigs) ] )
83e0677b Andy McCall 1 month ago ↶
828
}
28fc23d7 Andy McCall 1 month ago ↶
829
pub fn policy_from ( v : & Value) -> Result< SignedPolicy> {
83e0677b Andy McCall 1 month ago ↶
830
let a = o:: array( v, " signed policy" ) ? ;
831
if a.len ( ) ! = 2 {
832
return Err ( schema ( " signed policy: expected [policy, sigs]" ) ) ;
833
}
75167672 Andy McCall 1 month ago
834
Ok ( SignedPolicy {
835
policy: Policy:: decode( o:: bytes( & a[ 0 ] , " policy" ) ? ) ? ,
836
sigs: sigs_from ( & a[ 1 ] ) ? ,
837
} )
28fc23d7 Andy McCall 1 month ago ↶
838
}
839
fn opt_policy_value ( sp : Option< & SignedPolicy> ) -> Value {
840
sp.map ( policy_value) .unwrap_or ( Value:: Null)
841
}
842
fn opt_policy_from ( v : & Value) -> Result< Option< SignedPolicy> > {
75167672 Andy McCall 1 month ago
843
if v.is_null ( ) {
844
Ok ( None )
845
} else {
846
policy_from ( v) .map ( Some )
847
}
83e0677b Andy McCall 1 month ago ↶
848
}
750251b2 Andy McCall 1 month ago ↶
849
fn map < 'a > ( v : & 'a Value, what : & str ) -> Result< & 'a o:: Map> {
850
Ok ( o:: map( v, what) ? )
851
}
852
fn field < 'a > ( m : & 'a o:: Map, k : u64 , what : & str ) -> Result< & 'a Value> {
853
Ok ( o:: field( m, k, what) ? )
854
}
855
6f8aeb99 Andy McCall 1 month ago ↶
856
857
858
750251b2 Andy McCall 1 month ago ↶
859
pub fn bundle_value ( b : & Bundle) -> Value {
2c75672f Andy McCall 1 month ago ↶
860
let mut changes = Vec :: with_capacity( b.raw_changes.len ( ) + b.changes.len ( ) ) ;
861
862
863
for ( bytes, s) in & b.raw_changes {
864
changes.push ( Value:: Array( vec! [
865
Value:: Bytes( bytes.clone ( ) ) ,
866
sigs_value ( s) ,
867
] ) ) ;
868
}
6f8aeb99 Andy McCall 1 month ago ↶
869
let mut texts: Vec< ( ObjectType, Vec< u8 > ) > = Vec :: new( ) ;
870
let mut listed: Option< std:: collections:: HashSet< ObjectRef> > = None ;
871
for ( c, s) in & b.changes {
872
let ( bytes, stored) = c.encode_parts ( ) ;
873
for t in stored {
874
let listed = listed.get_or_insert_with ( | | {
875
b.objects
876
.iter ( )
877
.map ( | ( ty , body ) | crate :: hash:: hash_object( * ty, body) )
878
.collect ( )
879
} ) ;
880
if ! listed.insert ( t.blob.hash ( ) ) {
881
continue ;
882
}
883
for chunk in crate :: chunk:: chunks( & t.content) {
884
if listed.insert ( crate :: hash:: hash_object( ObjectType:: Chunk, chunk) ) {
885
texts.push ( ( ObjectType:: Chunk, chunk.to_vec ( ) ) ) ;
886
}
887
}
888
texts.push ( ( ObjectType:: Blob, t.blob.encode ( ) ) ) ;
889
}
890
changes.push ( Value:: Array( vec! [ Value:: Bytes( bytes) , sigs_value ( s) ] ) ) ;
891
}
f0a517d2 Andy McCall 18 days ago ↶
892
let mut v = Value:: map( [
6f8aeb99 Andy McCall 1 month ago ↶
893
( 0 , Value:: Array( changes) ) ,
750251b2 Andy McCall 1 month ago ↶
894
(
75167672 Andy McCall 1 month ago
895
1 ,
896
Value:: Array( b.attests.iter ( ) .map ( attest_value) .collect ( ) ) ,
897
) ,
898
(
750251b2 Andy McCall 1 month ago ↶
899
2 ,
900
Value:: Array(
75167672 Andy McCall 1 month ago
901
b.objects
902
.iter ( )
6f8aeb99 Andy McCall 1 month ago ↶
903
.chain ( & texts)
75167672 Andy McCall 1 month ago
904
.map ( | ( t , body ) | {
905
Value:: Array( vec! [
906
Value:: Text( t.name ( ) .into ( ) ) ,
907
Value:: Bytes( body.clone ( ) ) ,
908
] )
909
} )
910
.collect ( ) ,
750251b2 Andy McCall 1 month ago ↶
911
) ,
912
) ,
75167672 Andy McCall 1 month ago
913
(
914
3 ,
915
Value:: Array( b.policies.iter ( ) .map ( policy_value) .collect ( ) ) ,
916
) ,
f0a517d2 Andy McCall 18 days ago ↶
917
] ) ;
918
919
if ! b.certificates.is_empty ( ) {
920
if let Value:: Map( m) = & mut v {
921
m.insert (
922
4 ,
923
Value:: Array(
924
b.certificates
925
.iter ( )
926
.map ( | c | Value:: Bytes( c.encode ( ) ) )
927
.collect ( ) ,
928
) ,
929
) ;
930
}
931
}
932
v
750251b2 Andy McCall 1 month ago ↶
933
}
934
935
pub fn bundle_from_value ( v : & Value) -> Result< Bundle> {
936
let m = map ( v, " bundle" ) ? ;
937
let mut b = Bundle:: default( ) ;
938
for ob in o:: array( field ( m, 2 , " objects" ) ? , " objects" ) ? {
939
let pair = o:: array( ob, " object" ) ? ;
940
if pair.len ( ) ! = 2 {
941
return Err ( schema ( " object: expected [type, body]" ) ) ;
942
}
75167672 Andy McCall 1 month ago
943
let ty = ObjectType:: from_name( o:: text( & pair[ 0 ] , " object type" ) ? )
944
.ok_or_else ( | | schema ( " unknown object type" ) ) ? ;
945
b.objects
946
.push ( ( ty, o:: bytes( & pair[ 1 ] , " object body" ) ? .to_vec ( ) ) ) ;
6f8aeb99 Andy McCall 1 month ago ↶
947
}
948
949
let mut by_ref: Option< std:: collections:: HashMap< ObjectRef, usize > > = None ;
950
for c in o:: array( field ( m, 0 , " changes" ) ? , " changes" ) ? {
951
let pair = o:: array( c, " change entry" ) ? ;
952
if pair.len ( ) ! = 2 {
953
return Err ( schema ( " change entry: expected [change, sigs]" ) ) ;
954
}
598009a7 Andy McCall 1 month ago ↶
955
let encoded = o:: bytes( & pair[ 0 ] , " change" ) ? ;
956
b.hashes
957
.push ( crate :: hash:: hash_object( ObjectType:: Change, encoded) ) ;
0f93f49c Andy McCall 1 month ago ↶
958
let change = Change:: decode_with( encoded, & mut | text, count, form| {
6f8aeb99 Andy McCall 1 month ago ↶
959
let by_ref = by_ref.get_or_insert_with ( | | {
960
b.objects
961
.iter ( )
962
.enumerate ( )
963
.map ( | ( i , (ty , body ) ) | ( crate :: hash:: hash_object( * ty, body) , i) )
964
.collect ( )
965
} ) ;
966
let get =
967
| r : & ObjectRef, want : ObjectType| match by_ref.get ( r) .map ( | i | & b.objects[ * i] ) {
968
Some ( ( ty, body) ) if * ty = = want => Ok ( body) ,
969
_ => Err ( ObjectError:: Schema( format! (
970
" the bundle lacks {} {} " ,
971
want.name ( ) ,
972
r.short ( 12 )
973
) ) ) ,
974
} ;
975
let blob = crate :: objects:: Blob:: decode( get ( text, ObjectType:: Blob) ? ) ? ;
976
let mut content = Vec :: with_capacity( blob.length as usize ) ;
977
for chunk in & blob.chunks {
978
content.extend_from_slice ( get ( chunk, ObjectType:: Chunk) ? ) ;
979
}
0f93f49c Andy McCall 1 month ago ↶
980
crate :: objects:: text_lines( & content, count, form)
6f8aeb99 Andy McCall 1 month ago ↶
981
} ) ? ;
982
b.changes.push ( ( change, sigs_from ( & pair[ 1 ] ) ? ) ) ;
983
}
984
for a in o:: array( field ( m, 1 , " attests" ) ? , " attests" ) ? {
985
b.attests.push ( attest_from ( a) ? ) ;
750251b2 Andy McCall 1 month ago ↶
986
}
28fc23d7 Andy McCall 1 month ago ↶
987
if let Some ( ps) = m.get ( & 3 ) {
988
for p in o:: array( ps, " policies" ) ? {
989
b.policies.push ( policy_from ( p) ? ) ;
f0a517d2 Andy McCall 18 days ago ↶
990
}
991
}
992
if let Some ( cs) = m.get ( & 4 ) {
993
for c in o:: array( cs, " certificates" ) ? {
994
b.certificates.push (
995
crate :: cert:: SignedCertificate:: decode( o:: bytes( c, " certificate" ) ? )
996
.map_err ( | e | schema ( & format! ( " certificate: {e} " ) ) ) ? ,
997
) ;
28fc23d7 Andy McCall 1 month ago ↶
998
}
999
}
750251b2 Andy McCall 1 month ago ↶
1000
Ok ( b)
1001
}
1002
1003
pub fn encode_bundle ( b : & Bundle) -> Vec< u8 > {
1004
cbor:: encode( & bundle_value ( b) )
1005
}
1006
pub fn decode_bundle ( bytes : & [u8 ]) -> Result< Bundle> {
1007
bundle_from_value ( & cbor:: decode( bytes) .map_err ( ObjectError:: from) ? )
1008
}
1009
1010
pub fn encode_capabilities ( id : [u8 ; 16]) -> Vec< u8 > {
1011
cbor:: encode( & Value:: map( [
1012
( 0 , Value:: Array( vec! [ Value:: Uint( PROTOCOL_VERSION ) ] ) ) ,
1013
( 1 , Value:: Bytes( id.to_vec ( ) ) ) ,
1014
( 2 , Value:: Array( vec! [ Value:: Text( " seal" .into ( ) ) ] ) ) ,
1015
] ) )
1016
}
1017
pub fn decode_capabilities ( bytes : & [u8 ]) -> Result< [ u8 ; 16 ] > {
1018
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
1019
let m = map ( & v, " capabilities" ) ? ;
1020
let versions = o:: array( field ( m, 0 , " versions" ) ? , " versions" ) ? ;
75167672 Andy McCall 1 month ago
1021
if ! versions
1022
.iter ( )
1023
.any ( | x | x.as_uint ( ) = = Some ( PROTOCOL_VERSION ) )
1024
{
1025
return Err ( RepoError:: Msg(
1026
" remote does not speak protocol version 1" .into ( ) ,
1027
) ) ;
750251b2 Andy McCall 1 month ago ↶
1028
}
75167672 Andy McCall 1 month ago
1029
o:: bytes( field ( m, 1 , " repo id" ) ? , " repo id" ) ?
1030
.try_into ( )
1031
.map_err ( | _| schema ( " repo id: expected 16 bytes" ) )
750251b2 Andy McCall 1 month ago ↶
1032
}
1033
132b884b Andy McCall 1 month ago ↶
1034
fn tags_value ( tags : & [TagRef]) -> Value {
1035
Value:: Array(
1036
tags.iter ( )
1037
.map ( | t | {
1ec6fdbc Andy McCall 15 days ago ↶
1038
let mut v = Value:: map( [
132b884b Andy McCall 1 month ago ↶
1039
( 0 , Value:: Text( t.name.clone ( ) ) ) ,
1040
( 1 , Value:: Bytes( t.state.0. to_vec ( ) ) ) ,
1041
( 2 , hashes_value ( & t.heads) ) ,
1042
( 3 , opt_attest_value ( & t.attestation) ) ,
1ec6fdbc Andy McCall 15 days ago ↶
1043
] ) ;
1044
1045
1046
if let Value:: Map( m) = & mut v {
1047
if t.accepted.is_some ( ) {
1048
m.insert ( 4 , opt_attest_value ( & t.accepted) ) ;
1049
}
1050
if let Some ( l) = & t.logged {
1051
m.insert ( 5 , logged_value ( l) ) ;
1052
}
1053
if ! t.integration.is_empty ( ) {
1054
m.insert ( 6 , integration_value ( & t.integration) ) ;
1055
}
1056
}
1057
v
132b884b Andy McCall 1 month ago ↶
1058
} )
1059
.collect ( ) ,
1060
)
1061
}
1062
fn tags_from ( v : & Value) -> Result< Vec< TagRef> > {
1063
let mut out = Vec :: new( ) ;
1064
for t in o:: array( v, " tags" ) ? {
1065
let tm = map ( t, " tag" ) ? ;
1066
out.push ( TagRef {
1067
name: o:: text( field ( tm, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
1068
state: o:: oref( field ( tm, 1 , " state" ) ? , " state" ) ? ,
1069
heads: hashes_from ( field ( tm, 2 , " heads" ) ? ) ? ,
1070
attestation: opt_attest_from ( field ( tm, 3 , " attestation" ) ? ) ? ,
1ec6fdbc Andy McCall 15 days ago ↶
1071
accepted: match tm.get ( & 4 ) {
1072
Some ( a) => opt_attest_from ( a) ? ,
1073
None => None ,
1074
} ,
1075
logged: tm.get ( & 5 ) .map ( logged_from) .transpose ( ) ? ,
1076
integration: match tm.get ( & 6 ) {
1077
Some ( l) => integration_from ( l) ? ,
1078
None => Vec :: new( ) ,
1079
} ,
132b884b Andy McCall 1 month ago ↶
1080
} ) ;
1ec6fdbc Andy McCall 15 days ago ↶
1081
}
1082
Ok ( out)
1083
}
1084
1085
1086
1087
1088
# [ cfg ( test ) ]
1089
pub ( super ) fn tags_as_0_5_read_them ( bytes : & [u8 ]) -> Result< Vec< TagRef> > {
1090
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
1091
let m = map ( & v, " refs" ) ? ;
1092
let mut out = Vec :: new( ) ;
1093
for t in o:: array( field ( m, 3 , " tags" ) ? , " tags" ) ? {
1094
let tm = map ( t, " tag" ) ? ;
1095
out.push ( TagRef:: new(
1096
o:: text( field ( tm, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
1097
o:: oref( field ( tm, 1 , " state" ) ? , " state" ) ? ,
1098
hashes_from ( field ( tm, 2 , " heads" ) ? ) ? ,
1099
opt_attest_from ( field ( tm, 3 , " attestation" ) ? ) ? ,
1100
) ) ;
132b884b Andy McCall 1 month ago ↶
1101
}
1102
Ok ( out)
1ec6fdbc Andy McCall 15 days ago ↶
1103
}
1104
1105
1106
fn logged_value ( l : & Logged) -> Value {
1107
Value:: Array( vec! [
1108
Value:: Uint( l.index) ,
1109
Value:: Bytes( l.entry.clone ( ) ) ,
1110
proof_value ( & l.proof) ,
1111
] )
132b884b Andy McCall 1 month ago ↶
1112
}
1ec6fdbc Andy McCall 15 days ago ↶
1113
fn logged_from ( v : & Value) -> Result< Logged> {
1114
let a = o:: array( v, " logged" ) ? ;
1115
if a.len ( ) ! = 3 {
1116
return Err ( schema ( " logged: expected [index, entry, proof]" ) ) ;
1117
}
1118
Ok ( Logged {
1119
index: o:: uint( & a[ 0 ] , " index" ) ? ,
1120
entry: o:: bytes( & a[ 1 ] , " entry" ) ? .to_vec ( ) ,
1121
proof: proof_from ( & a[ 2 ] ) ? ,
1122
} )
1123
}
132b884b Andy McCall 1 month ago ↶
1124
1ec6fdbc Andy McCall 15 days ago ↶
1125
1126
fn integration_value ( integration : & [Integration]) -> Value {
1127
Value:: Array(
1128
integration
1129
.iter ( )
1130
.map ( | i | Value:: Array( vec! [ Value:: Text( i.note.clone ( ) ) , proof_value ( & i.proof) ] ) )
1131
.collect ( ) ,
1132
)
1133
}
1134
fn integration_from ( v : & Value) -> Result< Vec< Integration> > {
1135
let mut out = Vec :: new( ) ;
1136
for i in o:: array( v, " integration" ) ? {
1137
let a = o:: array( i, " integration" ) ? ;
1138
if a.len ( ) ! = 2 {
1139
return Err ( schema ( " integration: expected [note, proof]" ) ) ;
1140
}
1141
out.push ( Integration {
1142
note: o:: text( & a[ 0 ] , " note" ) ? .to_owned ( ) ,
1143
proof: proof_from ( & a[ 1 ] ) ? ,
1144
} ) ;
1145
}
1146
Ok ( out)
1147
}
1148
83e0677b Andy McCall 1 month ago ↶
1149
pub fn encode_refs ( id : [u8 ; 16], resp : & RefsResponse) -> Vec< u8 > {
4b9d0677 Andy McCall 18 days ago ↶
1150
let mut v = Value:: map( [
750251b2 Andy McCall 1 month ago ↶
1151
(
1152
0 ,
1153
Value:: Array(
83e0677b Andy McCall 1 month ago ↶
1154
resp.refs
1155
.iter ( )
750251b2 Andy McCall 1 month ago ↶
1156
.map ( | r | {
f53cbe7a Andy McCall 18 days ago ↶
1157
let mut v = Value:: map( [
750251b2 Andy McCall 1 month ago ↶
1158
( 0 , Value:: Text( r.name.clone ( ) ) ) ,
1159
( 1 , Value:: Bytes( r.state.0. to_vec ( ) ) ) ,
1160
( 2 , hashes_value ( & r.heads) ) ,
1161
( 3 , opt_attest_value ( & r.attestation) ) ,
1162
( 4 , r.last_serial.map ( Value:: Uint) .unwrap_or ( Value:: Null) ) ,
f53cbe7a Andy McCall 18 days ago ↶
1163
] ) ;
1164
4b9d0677 Andy McCall 18 days ago ↶
1165
if let Value:: Map( m) = & mut v {
1166
if r.accepted.is_some ( ) {
f53cbe7a Andy McCall 18 days ago ↶
1167
m.insert ( 5 , opt_attest_value ( & r.accepted) ) ;
4b9d0677 Andy McCall 18 days ago ↶
1168
}
1169
if let Some ( l) = & r.logged {
1ec6fdbc Andy McCall 15 days ago ↶
1170
m.insert ( 6 , logged_value ( l) ) ;
f53cbe7a Andy McCall 18 days ago ↶
1171
}
028545df Andy McCall 17 days ago ↶
1172
if ! r.integration.is_empty ( ) {
1ec6fdbc Andy McCall 15 days ago ↶
1173
m.insert ( 7 , integration_value ( & r.integration) ) ;
028545df Andy McCall 17 days ago ↶
1174
}
f53cbe7a Andy McCall 18 days ago ↶
1175
}
1176
v
750251b2 Andy McCall 1 month ago ↶
1177
} )
1178
.collect ( ) ,
1179
) ,
1180
) ,
1181
( 1 , Value:: Bytes( id.to_vec ( ) ) ) ,
83e0677b Andy McCall 1 month ago ↶
1182
( 2 , opt_policy_value ( resp.policy.as_ref ( ) ) ) ,
132b884b Andy McCall 1 month ago ↶
1183
( 3 , tags_value ( & resp.tags) ) ,
75167672 Andy McCall 1 month ago
1184
(
1185
4 ,
1186
Value:: Array( resp.history.iter ( ) .map ( policy_value) .collect ( ) ) ,
1187
) ,
14edbaaf Andy McCall 1 month ago ↶
1188
(
1189
5 ,
1190
resp.default_bookmark
1191
.clone ( )
1192
.map ( Value:: Text)
1193
.unwrap_or ( Value:: Null) ,
1194
) ,
4b9d0677 Andy McCall 18 days ago ↶
1195
] ) ;
028545df Andy McCall 17 days ago ↶
1196
if let Value:: Map( m) = & mut v {
1197
if let Some ( log) = & resp.log {
1198
m.insert ( 6 , Value:: Text( log.clone ( ) ) ) ;
b63fc7a7 Andy McCall 11 days ago ↶
1199
}
1200
if ! resp.withdrawn.is_empty ( ) {
1201
m.insert ( 8 , tags_value ( & resp.withdrawn) ) ;
028545df Andy McCall 17 days ago ↶
1202
}
1203
if ! resp.witnesses.is_empty ( ) {
1204
m.insert (
1205
7 ,
1206
Value:: Array(
1207
resp.witnesses
1208
.iter ( )
1209
.map ( | ( name , id ) | {
1210
Value:: Array( vec! [
1211
Value:: Text( name.clone ( ) ) ,
1212
Value:: Bytes( id.clone ( ) ) ,
1213
] )
1214
} )
1215
.collect ( ) ,
1216
) ,
1217
) ;
1218
}
4b9d0677 Andy McCall 18 days ago ↶
1219
}
1220
cbor:: encode( & v)
750251b2 Andy McCall 1 month ago ↶
1221
}
83e0677b Andy McCall 1 month ago ↶
1222
pub fn decode_refs ( bytes : & [u8 ]) -> Result< ( [u8 ; 16], RefsResponse) > {
750251b2 Andy McCall 1 month ago ↶
1223
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
1224
let m = map ( & v, " refs" ) ? ;
75167672 Andy McCall 1 month ago
1225
let id: [ u8 ; 16 ] = o:: bytes( field ( m, 1 , " repo id" ) ? , " repo id" ) ?
1226
.try_into ( )
1227
.map_err ( | _| schema ( " repo id: expected 16 bytes" ) ) ? ;
750251b2 Andy McCall 1 month ago ↶
1228
let mut refs = Vec :: new( ) ;
1229
for r in o:: array( field ( m, 0 , " refs" ) ? , " refs" ) ? {
1230
let rm = map ( r, " ref" ) ? ;
1231
let ls = field ( rm, 4 , " last serial" ) ? ;
1232
refs.push ( RemoteRef {
1233
name: o:: text( field ( rm, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
1234
state: o:: oref( field ( rm, 1 , " state" ) ? , " state" ) ? ,
1235
heads: hashes_from ( field ( rm, 2 , " heads" ) ? ) ? ,
1236
attestation: opt_attest_from ( field ( rm, 3 , " attestation" ) ? ) ? ,
75167672 Andy McCall 1 month ago
1237
last_serial: if ls.is_null ( ) {
1238
None
1239
} else {
1240
Some ( o:: uint( ls, " last serial" ) ? )
f53cbe7a Andy McCall 18 days ago ↶
1241
} ,
1242
accepted: match rm.get ( & 5 ) {
1243
Some ( a) => opt_attest_from ( a) ? ,
1244
None => None ,
75167672 Andy McCall 1 month ago
1245
} ,
1ec6fdbc Andy McCall 15 days ago ↶
1246
logged: rm.get ( & 6 ) .map ( logged_from) .transpose ( ) ? ,
028545df Andy McCall 17 days ago ↶
1247
integration: match rm.get ( & 7 ) {
1ec6fdbc Andy McCall 15 days ago ↶
1248
Some ( l) => integration_from ( l) ? ,
028545df Andy McCall 17 days ago ↶
1249
None => Vec :: new( ) ,
1250
} ,
750251b2 Andy McCall 1 month ago ↶
1251
} ) ;
1252
}
83e0677b Andy McCall 1 month ago ↶
1253
let policy = match m.get ( & 2 ) {
1254
Some ( p) => opt_policy_from ( p) ? ,
1255
None => None ,
1256
} ;
132b884b Andy McCall 1 month ago ↶
1257
let tags = match m.get ( & 3 ) {
1258
Some ( t) => tags_from ( t) ? ,
1259
None => Vec :: new( ) ,
1260
} ;
28fc23d7 Andy McCall 1 month ago ↶
1261
let mut history = Vec :: new( ) ;
1262
if let Some ( h) = m.get ( & 4 ) {
1263
for p in o:: array( h, " history" ) ? {
1264
history.push ( policy_from ( p) ? ) ;
1265
}
1266
}
14edbaaf Andy McCall 1 month ago ↶
1267
let default_bookmark = match m.get ( & 5 ) {
4b9d0677 Andy McCall 18 days ago ↶
1268
Some ( Value:: Text( t) ) => Some ( t.clone ( ) ) ,
1269
_ => None ,
1270
} ;
1271
let log = match m.get ( & 6 ) {
14edbaaf Andy McCall 1 month ago ↶
1272
Some ( Value:: Text( t) ) => Some ( t.clone ( ) ) ,
1273
_ => None ,
1274
} ;
028545df Andy McCall 17 days ago ↶
1275
let mut witnesses = Vec :: new( ) ;
1276
if let Some ( w) = m.get ( & 7 ) {
1277
for x in o:: array( w, " witnesses" ) ? {
1278
let a = o:: array( x, " witness" ) ? ;
1279
if a.len ( ) ! = 2 {
1280
return Err ( schema ( " witness: expected [name, identity]" ) ) ;
1281
}
1282
witnesses.push ( (
1283
o:: text( & a[ 0 ] , " witness name" ) ? .to_owned ( ) ,
1284
o:: bytes( & a[ 1 ] , " witness identity" ) ? .to_vec ( ) ,
1285
) ) ;
1286
}
1287
}
b63fc7a7 Andy McCall 11 days ago ↶
1288
let withdrawn = match m.get ( & 8 ) {
1289
Some ( t) => tags_from ( t) ? ,
1290
None => Vec :: new( ) ,
1291
} ;
75167672 Andy McCall 1 month ago
1292
Ok ( (
1293
id,
1294
RefsResponse {
1295
refs,
1296
policy,
1297
tags,
1298
history,
14edbaaf Andy McCall 1 month ago ↶
1299
default_bookmark,
4b9d0677 Andy McCall 18 days ago ↶
1300
log,
028545df Andy McCall 17 days ago ↶
1301
witnesses,
b63fc7a7 Andy McCall 11 days ago ↶
1302
withdrawn,
75167672 Andy McCall 1 month ago
1303
} ,
1304
) )
750251b2 Andy McCall 1 month ago ↶
1305
}
1306
028545df Andy McCall 17 days ago ↶
1307
1308
fn proof_value ( proof : & [[u8 ; 32]]) -> Value {
1309
Value:: Array( proof.iter ( ) .map ( | h | Value:: Bytes( h.to_vec ( ) ) ) .collect ( ) )
1310
}
1311
1312
fn proof_from ( v : & Value) -> Result< Vec< [ u8 ; 32 ] > > {
1313
let mut proof = Vec :: new( ) ;
1314
for h in o:: array( v, " proof" ) ? {
1315
proof.push (
1316
o:: bytes( h, " proof hash" ) ?
1317
.try_into ( )
1318
.map_err ( | _| schema ( " proof hash: expected 32 bytes" ) ) ? ,
1319
) ;
1320
}
1321
Ok ( proof)
1322
}
1323
750251b2 Andy McCall 1 month ago ↶
1324
pub fn encode_negotiate ( have : & [ChangeHash], want : & [ChangeHash]) -> Vec< u8 > {
75167672 Andy McCall 1 month ago
1325
cbor:: encode( & Value:: map( [
1326
( 0 , hashes_value ( have) ) ,
1327
( 1 , hashes_value ( want) ) ,
1328
] ) )
750251b2 Andy McCall 1 month ago ↶
1329
}
1330
pub fn decode_negotiate ( bytes : & [u8 ]) -> Result< ( Vec< ChangeHash> , Vec< ChangeHash> ) > {
1331
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
1332
let m = map ( & v, " negotiate" ) ? ;
75167672 Andy McCall 1 month ago
1333
Ok ( (
1334
hashes_from ( field ( m, 0 , " have" ) ? ) ? ,
1335
hashes_from ( field ( m, 1 , " want" ) ? ) ? ,
1336
) )
750251b2 Andy McCall 1 month ago ↶
1337
}
1338
b3c5a59a Andy McCall 1 month ago ↶
1339
1340
pub fn encode_fetch ( hs : & [ChangeHash], max_blob : Option< u64 > ) -> Vec< u8 > {
75167672 Andy McCall 1 month ago
1341
cbor:: encode( & Value:: map( [
1342
( 0 , hashes_value ( hs) ) ,
1343
( 1 , max_blob.map ( Value:: Uint) .unwrap_or ( Value:: Null) ) ,
1344
] ) )
b3c5a59a Andy McCall 1 month ago ↶
1345
}
1346
pub fn decode_fetch ( bytes : & [u8 ]) -> Result< ( Vec< ChangeHash> , Option< u64 > ) > {
1347
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
1348
let m = map ( & v, " fetch" ) ? ;
1349
let max = match m.get ( & 1 ) {
1350
Some ( x) if ! x.is_null ( ) => Some ( o:: uint( x, " max blob" ) ? ) ,
1351
_ => None ,
1352
} ;
1353
Ok ( ( hashes_from ( field ( m, 0 , " changes" ) ? ) ? , max) )
1354
}
1355
2863456a Andy McCall 1 month ago ↶
1356
1357
pub fn encode_shallow ( hs : & [ChangeHash], depth : u32 ) -> Vec< u8 > {
75167672 Andy McCall 1 month ago
1358
cbor:: encode( & Value:: map( [
1359
( 0 , hashes_value ( hs) ) ,
1360
( 1 , Value:: Uint( depth as u64 ) ) ,
1361
] ) )
2863456a Andy McCall 1 month ago ↶
1362
}
1363
pub fn decode_shallow ( bytes : & [u8 ]) -> Result< ( Vec< ChangeHash> , u32 ) > {
1364
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
1365
let m = map ( & v, " shallow" ) ? ;
75167672 Andy McCall 1 month ago
1366
Ok ( (
1367
hashes_from ( field ( m, 0 , " heads" ) ? ) ? ,
1368
o:: u32_( field ( m, 1 , " depth" ) ? , " depth" ) ? ,
1369
) )
2863456a Andy McCall 1 month ago ↶
1370
}
1371
750251b2 Andy McCall 1 month ago ↶
1372
pub fn encode_hashes ( hs : & [ChangeHash]) -> Vec< u8 > {
1373
cbor:: encode( & Value:: map( [ ( 0 , hashes_value ( hs) ) ] ) )
1374
}
1375
pub fn decode_hashes ( bytes : & [u8 ]) -> Result< Vec< ChangeHash> > {
1376
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
1377
hashes_from ( field ( map ( & v, " hashes" ) ? , 0 , " hashes" ) ? )
1378
}
1379
75167672 Andy McCall 1 month ago
1380
pub fn encode_push (
1381
bundle : & Bundle,
1382
updates : & [RefUpdate],
1383
policy : Option< & SignedPolicy> ,
1384
tags : & [TagRef],
1385
) -> Vec< u8 > {
750251b2 Andy McCall 1 month ago ↶
1386
cbor:: encode( & Value:: map( [
1387
( 0 , bundle_value ( bundle) ) ,
83e0677b Andy McCall 1 month ago ↶
1388
( 2 , opt_policy_value ( policy) ) ,
132b884b Andy McCall 1 month ago ↶
1389
( 3 , tags_value ( tags) ) ,
750251b2 Andy McCall 1 month ago ↶
1390
(
1391
1 ,
1392
Value:: Array(
1393
updates
1394
.iter ( )
1395
.map ( | u | {
1396
Value:: map( [
1397
( 0 , Value:: Text( u.name.clone ( ) ) ) ,
1398
( 1 , Value:: Bytes( u.old_state.0. to_vec ( ) ) ) ,
1399
( 2 , Value:: Bytes( u.new_state.0. to_vec ( ) ) ) ,
1400
( 3 , Value:: Bool( u.force) ) ,
1401
( 4 , opt_attest_value ( & u.attestation) ) ,
1402
] )
1403
} )
1404
.collect ( ) ,
1405
) ,
1406
) ,
1407
] ) )
1408
}
132b884b Andy McCall 1 month ago ↶
1409
1410
pub type PushParts = ( Bundle, Vec< RefUpdate> , Option< SignedPolicy> , Vec< TagRef> ) ;
1411
1412
pub fn decode_push ( bytes : & [u8 ]) -> Result< PushParts> {
750251b2 Andy McCall 1 month ago ↶
1413
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
1414
let m = map ( & v, " push" ) ? ;
1415
let bundle = bundle_from_value ( field ( m, 0 , " bundle" ) ? ) ? ;
1416
let mut updates = Vec :: new( ) ;
1417
for u in o:: array( field ( m, 1 , " updates" ) ? , " updates" ) ? {
1418
let um = map ( u, " update" ) ? ;
1419
updates.push ( RefUpdate {
1420
name: o:: text( field ( um, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
1421
old_state: o:: oref( field ( um, 1 , " old_state" ) ? , " old_state" ) ? ,
1422
new_state: o:: oref( field ( um, 2 , " new_state" ) ? , " new_state" ) ? ,
75167672 Andy McCall 1 month ago
1423
force: field ( um, 3 , " force" ) ?
1424
.as_bool ( )
1425
.ok_or_else ( | | schema ( " force: expected bool" ) ) ? ,
750251b2 Andy McCall 1 month ago ↶
1426
attestation: opt_attest_from ( field ( um, 4 , " attestation" ) ? ) ? ,
1427
} ) ;
1428
}
83e0677b Andy McCall 1 month ago ↶
1429
let policy = match m.get ( & 2 ) {
1430
Some ( p) => opt_policy_from ( p) ? ,
1431
None => None ,
1432
} ;
132b884b Andy McCall 1 month ago ↶
1433
let tags = match m.get ( & 3 ) {
1434
Some ( t) => tags_from ( t) ? ,
1435
None => Vec :: new( ) ,
1436
} ;
1437
Ok ( ( bundle, updates, policy, tags) )
750251b2 Andy McCall 1 month ago ↶
1438
}
1439
1440
pub fn encode_push_response ( results : & [RefResult]) -> Vec< u8 > {
1441
cbor:: encode( & Value:: map( [ (
1442
0 ,
1443
Value:: Array(
1444
results
1445
.iter ( )
75167672 Andy McCall 1 month ago
1446
.map ( | r | {
1447
Value:: map( [
1448
( 0 , Value:: Text( r.name.clone ( ) ) ) ,
1449
( 1 , Value:: Bool( r.ok) ) ,
1450
( 2 , Value:: Text( r.reason.clone ( ) ) ) ,
1451
] )
1452
} )
750251b2 Andy McCall 1 month ago ↶
1453
.collect ( ) ,
1454
) ,
1455
) ] ) )
1456
}
1457
pub fn decode_push_response ( bytes : & [u8 ]) -> Result< Vec< RefResult> > {
1458
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
1459
let m = map ( & v, " push response" ) ? ;
1460
let mut out = Vec :: new( ) ;
1461
for r in o:: array( field ( m, 0 , " results" ) ? , " results" ) ? {
1462
let rm = map ( r, " result" ) ? ;
1463
out.push ( RefResult {
1464
name: o:: text( field ( rm, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
75167672 Andy McCall 1 month ago
1465
ok: field ( rm, 1 , " ok" ) ?
1466
.as_bool ( )
1467
.ok_or_else ( | | schema ( " ok: expected bool" ) ) ? ,
750251b2 Andy McCall 1 month ago ↶
1468
reason: o:: text( field ( rm, 2 , " reason" ) ? , " reason" ) ? .to_owned ( ) ,
1469
} ) ;
1470
}
1471
Ok ( out)
1472
}
1473
}
1474
1475
# [ cfg ( test ) ]
1476
mod tests {
1477
use super :: * ;
1478
use crate :: attest:: AttestBody;
1479
use crate :: objects:: Hlc;
d3b27090 Andy McCall 11 days ago ↶
1480
1481
1482
1483
# [ test ]
1484
fn html_error_pages_are_shown_by_their_title ( ) {
1485
let cloudflare = " <html>\r \n <head><title>413 Request Entity Too Large</title></head>\r \n <body><center><h1>413</h1></center><hr><center>cloudflare</center></body></html>" ;
1486
assert_eq! (
1487
plain_error_text ( cloudflare) ,
1488
" 413 Request Entity Too Large (an HTML error page, from a proxy in front of the server perhaps)"
1489
) ;
1490
assert_eq! (
1491
plain_error_text ( " refused: a secret\n at a.txt:3 " ) ,
1492
" refused: a secret\n at a.txt:3"
1493
) ;
1494
assert! ( hint_for ( " https://hub/x: HTTP 413: too large" )
1495
.unwrap ( )
1496
.contains ( " push.batch-bytes" ) ) ;
1497
}
ee130dd9 Andy McCall 15 days ago ↶
1498
1499
# [ test ]
1500
fn client_features_are_declared_and_read ( ) {
1501
assert_eq! ( CLIENT_FEATURES .join ( " ," ) , " certified-tags" ) ;
1502
assert! ( user_agent ( ) .starts_with ( " oxy/" ) ) ;
1503
assert! ( declares_feature (
1504
Some ( " certified-tags" ) ,
1505
FEATURE_CERTIFIED_TAGS
1506
) ) ;
1507
assert! ( declares_feature (
1508
Some ( " later-thing, Certified-Tags ,x" ) ,
1509
FEATURE_CERTIFIED_TAGS
1510
) ) ;
1511
assert! ( ! declares_feature (
1512
Some ( " certified-tags-v2" ) ,
1513
FEATURE_CERTIFIED_TAGS
1514
) ) ;
1515
assert! ( ! declares_feature ( Some ( " " ) , FEATURE_CERTIFIED_TAGS ) ) ;
1516
assert! ( ! declares_feature ( None , FEATURE_CERTIFIED_TAGS ) ) ;
1517
}
750251b2 Andy McCall 1 month ago ↶
1518
1519
# [ test ]
1520
fn wire_round_trips ( ) {
1521
let c = crate :: objects:: tests:: sample_change( ) ;
75167672 Andy McCall 1 month ago
1522
let sk = crate :: identity:: SecretKey {
1523
ed_seed: [ 1 ; 32 ] ,
1524
ml_seed: [ 2 ; 32 ] ,
1525
} ;
750251b2 Andy McCall 1 month ago ↶
1526
let id = sk.identity ( " a" , " b" , 0 ) ;
1527
let key = id.hash ( ) ;
1528
let sig = sk.sign ( crate :: identity:: Domain:: Change, & key, & c.hash ( ) ) ;
1529
let seal = Attest:: seal( [ 3 ; 16 ] , vec! [ c.hash ( ) ] ) ;
1530
let seal_sig = sk.sign ( crate :: identity:: Domain:: Attest, & key, & seal.hash ( ) ) ;
28fc23d7 Andy McCall 1 month ago ↶
1531
let policy = Policy:: single_root( [ 3 ; 16 ] , id.clone ( ) , 99 ) ;
1532
let psig = sk.sign ( crate :: identity:: Domain:: Policy, & key, & policy.hash ( ) ) ;
750251b2 Andy McCall 1 month ago ↶
1533
let bundle = Bundle {
1534
changes: vec! [ ( c.clone ( ) , vec! [ sig.clone ( ) ] ) ] ,
75167672 Andy McCall 1 month ago
1535
attests: vec! [ SignedAttest {
1536
attest: seal.clone ( ) ,
1537
sigs: vec! [ seal_sig] ,
1538
} ] ,
1539
objects: vec! [
1540
( ObjectType:: Identity, id.encode ( ) ) ,
1541
( ObjectType:: Chunk, b " xyz" .to_vec ( ) ) ,
1542
] ,
1543
policies: vec! [ SignedPolicy {
1544
policy: policy.clone ( ) ,
1545
sigs: vec! [ psig.clone ( ) ] ,
1546
} ] ,
598009a7 Andy McCall 1 month ago ↶
1547
hashes: vec! [ ] ,
2c75672f Andy McCall 1 month ago ↶
1548
raw_changes: vec! [ ] ,
f0a517d2 Andy McCall 18 days ago ↶
1549
certificates: vec! [ ] ,
750251b2 Andy McCall 1 month ago ↶
1550
} ;
1551
let d = wire:: decode_bundle( & wire:: encode_bundle( & bundle) ) .unwrap ( ) ;
598009a7 Andy McCall 1 month ago ↶
1552
assert_eq! (
1553
d.hashes,
1554
vec! [ c.hash ( ) ] ,
1555
" the receiver hashes what it was sent"
1556
) ;
750251b2 Andy McCall 1 month ago ↶
1557
assert_eq! ( d.changes, bundle.changes) ;
1558
assert_eq! ( d.attests, bundle.attests) ;
1559
assert_eq! ( d.objects, bundle.objects) ;
28fc23d7 Andy McCall 1 month ago ↶
1560
assert_eq! ( d.policies, bundle.policies) ;
f0a517d2 Andy McCall 18 days ago ↶
1561
assert! ( d.certificates.is_empty ( ) ) ;
1562
1563
1564
let plain = wire:: encode_bundle( & bundle) ;
1565
let mut with = bundle.clone ( ) ;
1566
let issuer_sk = crate :: identity:: SecretKey:: generate( ) ;
1567
let issuer = issuer_sk.identity ( " hub" , " h@x" , 1 ) ;
1568
with.certificates.push ( crate :: cert:: SignedCertificate:: sign(
1569
crate :: cert:: Certificate {
1570
key: id.clone ( ) ,
1571
issuer: issuer.hash ( ) ,
1572
subject: " agent:a@b" .into ( ) ,
1573
claims: vec! [ ] ,
1574
scope: crate :: identity:: Scope:: CHANGES ,
1575
not_before: 1 ,
1576
expires: 2 ,
a2387d54 Andy McCall 18 days ago ↶
1577
grant: None ,
f0a517d2 Andy McCall 18 days ago ↶
1578
} ,
1579
& issuer,
1580
& issuer_sk,
1581
) ) ;
1582
let encoded = wire:: encode_bundle( & with) ;
1583
assert_ne! ( encoded, plain) ;
1584
assert_eq! (
1585
wire:: decode_bundle( & encoded) .unwrap ( ) .certificates,
1586
with.certificates
1587
) ;
1588
with.certificates.clear ( ) ;
1589
assert_eq! ( wire:: encode_bundle( & with) , plain) ;
750251b2 Andy McCall 1 month ago ↶
1590
75167672 Andy McCall 1 month ago
1591
let att = Attest {
1592
repo: [ 3 ; 16 ] ,
1593
hlc: Hlc:: default( ) ,
1594
body: AttestBody:: Ref {
1595
name: " main" .into ( ) ,
1596
old_state: ObjectRef( [ 1 ; 32 ] ) ,
1597
new_state: ObjectRef( [ 2 ; 32 ] ) ,
1598
serial: 4 ,
1599
} ,
1600
} ;
750251b2 Andy McCall 1 month ago ↶
1601
let refs = vec! [
75167672 Andy McCall 1 month ago
1602
RemoteRef {
1603
name: " main" .into ( ) ,
1604
state: ObjectRef( [ 2 ; 32 ] ) ,
1605
heads: vec! [ c.hash ( ) ] ,
1606
attestation: Some ( SignedAttest {
1607
attest: att.clone ( ) ,
1608
sigs: vec! [ sig.clone ( ) ] ,
1609
} ) ,
1610
last_serial: Some ( 4 ) ,
f53cbe7a Andy McCall 18 days ago ↶
1611
accepted: None ,
4b9d0677 Andy McCall 18 days ago ↶
1612
logged: None ,
028545df Andy McCall 17 days ago ↶
1613
integration: vec! [ ] ,
75167672 Andy McCall 1 month ago
1614
} ,
1615
RemoteRef {
1616
name: " dev" .into ( ) ,
1617
state: ObjectRef( [ 5 ; 32 ] ) ,
1618
heads: vec! [ ] ,
1619
attestation: None ,
1620
last_serial: None ,
f53cbe7a Andy McCall 18 days ago ↶
1621
accepted: None ,
4b9d0677 Andy McCall 18 days ago ↶
1622
logged: None ,
028545df Andy McCall 17 days ago ↶
1623
integration: vec! [ ] ,
75167672 Andy McCall 1 month ago
1624
} ,
750251b2 Andy McCall 1 month ago ↶
1625
] ;
1ec6fdbc Andy McCall 15 days ago ↶
1626
let tag = TagRef:: new( " v1" .into ( ) , ObjectRef( [ 2 ; 32 ] ) , vec! [ c.hash ( ) ] , None ) ;
4b9d0677 Andy McCall 18 days ago ↶
1627
let mut resp = RefsResponse {
75167672 Andy McCall 1 month ago
1628
refs,
1629
policy: Some ( SignedPolicy {
1630
policy: policy.clone ( ) ,
1631
sigs: vec! [ psig.clone ( ) ] ,
1632
} ) ,
1633
tags: vec! [ tag.clone ( ) ] ,
1634
history: vec! [ SignedPolicy {
1635
policy: policy.clone ( ) ,
1636
sigs: vec! [ ] ,
1637
} ] ,
14edbaaf Andy McCall 1 month ago ↶
1638
default_bookmark: Some ( " develop" .into ( ) ) ,
4b9d0677 Andy McCall 18 days ago ↶
1639
log: Some ( " hub.example/log\n 2\n root\n \n - sig\n " .into ( ) ) ,
028545df Andy McCall 17 days ago ↶
1640
witnesses: vec! [ ] ,
b63fc7a7 Andy McCall 11 days ago ↶
1641
withdrawn: vec! [ ] ,
75167672 Andy McCall 1 month ago
1642
} ;
028545df Andy McCall 17 days ago ↶
1643
1644
1645
let old = wire:: encode_refs( [ 3 ; 16 ] , & resp) ;
1646
let back = wire:: decode_refs( & old) .unwrap ( ) .1 ;
1647
assert! ( back.witnesses.is_empty ( ) & & back.refs.iter ( ) .all ( | r | r.integration.is_empty ( ) ) ) ;
4b9d0677 Andy McCall 18 days ago ↶
1648
resp.refs[ 1 ] .logged = Some ( Logged {
1649
index: 41 ,
1650
entry: vec! [ 1 , 2 , 3 ] ,
1651
proof: vec! [ [ 9 ; 32 ] , [ 8 ; 32 ] ] ,
1652
} ) ;
028545df Andy McCall 17 days ago ↶
1653
resp.refs[ 1 ] .integration = vec! [ Integration {
1654
note: " hub.example/log\n 42\n root\n \n - sig\n " .into ( ) ,
1655
proof: vec! [ [ 7 ; 32 ] ] ,
1656
} ] ;
1657
resp.witnesses = vec! [ ( " w.example" .into ( ) , vec! [ 4 , 5 , 6 ] ) ] ;
1658
let new = wire:: encode_refs( [ 3 ; 16 ] , & resp) ;
1659
let ( id_back, back) = wire:: decode_refs( & new) .unwrap ( ) ;
4b9d0677 Andy McCall 18 days ago ↶
1660
assert_eq! ( back.log, resp.log) ;
1661
assert_eq! ( back.refs[ 1 ] .logged, resp.refs[ 1 ] .logged) ;
028545df Andy McCall 17 days ago ↶
1662
assert_eq! ( back.refs[ 1 ] .integration, resp.refs[ 1 ] .integration) ;
1663
assert_eq! ( back.witnesses, resp.witnesses) ;
4b9d0677 Andy McCall 18 days ago ↶
1664
assert_eq! ( back.refs[ 0 ] .logged, None ) ;
028545df Andy McCall 17 days ago ↶
1665
1666
let crate :: cbor:: Value:: Map( m) = crate :: cbor:: decode( & new) .unwrap ( ) else {
1667
panic! ( )
1668
} ;
1669
assert! ( m.contains_key ( & 7 ) ) ;
750251b2 Andy McCall 1 month ago ↶
1670
assert_eq! ( id_back, [ 3 ; 16 ] ) ;
83e0677b Andy McCall 1 month ago ↶
1671
assert_eq! ( back.refs.len ( ) , 2 ) ;
1672
assert_eq! ( back.refs[ 0 ] .attestation.as_ref ( ) .unwrap ( ) .attest, att) ;
1673
assert_eq! ( back.refs[ 1 ] .last_serial, None ) ;
1674
assert_eq! ( back.policy.as_ref ( ) .unwrap ( ) .policy, policy) ;
28fc23d7 Andy McCall 1 month ago ↶
1675
assert_eq! ( back.history, resp.history) ;
132b884b Andy McCall 1 month ago ↶
1676
assert_eq! ( back.tags, vec! [ tag.clone ( ) ] ) ;
1ec6fdbc Andy McCall 15 days ago ↶
1677
1678
1679
1680
let crate :: cbor:: Value:: Map( m) = crate :: cbor:: decode( & new) .unwrap ( ) else {
1681
panic! ( )
1682
} ;
1683
let crate :: cbor:: Value:: Array( tags) = & m[ & 3 ] else {
1684
panic! ( )
1685
} ;
1686
let crate :: cbor:: Value:: Map( plain) = & tags[ 0 ] else {
1687
panic! ( )
1688
} ;
1689
assert_eq! ( plain.keys ( ) .copied ( ) .collect:: < Vec< _ > > ( ) , vec! [ 0 , 1 , 2 , 3 ] ) ;
1690
let mut full = tag.clone ( ) ;
1691
full.name = " v2" .into ( ) ;
1692
full.attestation = Some ( SignedAttest {
1693
attest: att.clone ( ) ,
1694
sigs: vec! [ sig.clone ( ) ] ,
1695
} ) ;
1696
full.accepted = full.attestation.clone ( ) ;
1697
full.logged = Some ( Logged {
1698
index: 7 ,
1699
entry: vec! [ 4 ] ,
1700
proof: vec! [ [ 3 ; 32 ] ] ,
1701
} ) ;
1702
full.integration = vec! [ Integration {
1703
note: " hub.example/log\n 8\n root\n \n - sig\n " .into ( ) ,
1704
proof: vec! [ ] ,
1705
} ] ;
1706
resp.tags = vec! [ full.clone ( ) , tag.clone ( ) ] ;
1707
let bytes = wire:: encode_refs( [ 3 ; 16 ] , & resp) ;
1708
assert_eq! ( wire:: decode_refs( & bytes) .unwrap ( ) .1. tags, resp.tags) ;
1709
1710
let pushed = wire:: encode_push( & Bundle:: default( ) , & [ ] , None , & resp.tags) ;
1711
assert_eq! ( wire:: decode_push( & pushed) .unwrap ( ) .3 , resp.tags) ;
1712
1713
1714
let stripped: Vec< TagRef> = resp
1715
.tags
1716
.iter ( )
1717
.map ( | t | {
1718
TagRef:: new(
1719
t.name.clone ( ) ,
1720
t.state,
1721
t.heads.clone ( ) ,
1722
t.attestation.clone ( ) ,
1723
)
1724
} )
1725
.collect ( ) ;
1726
assert_eq! ( wire:: tags_as_0_5_read_them( & bytes) .unwrap ( ) , stripped) ;
750251b2 Andy McCall 1 month ago ↶
1727
75167672 Andy McCall 1 month ago
1728
let ( h, w) = wire:: decode_negotiate( & wire:: encode_negotiate(
1729
& [ ObjectRef( [ 1 ; 32 ] ) ] ,
1730
& [ ObjectRef( [ 2 ; 32 ] ) ] ,
1731
) )
1732
.unwrap ( ) ;
750251b2 Andy McCall 1 month ago ↶
1733
assert_eq! ( ( h, w) , ( vec! [ ObjectRef( [ 1 ; 32 ] ) ] , vec! [ ObjectRef( [ 2 ; 32 ] ) ] ) ) ;
75167672 Andy McCall 1 month ago
1734
assert_eq! (
1735
wire:: decode_hashes( & wire:: encode_hashes( & [ c.hash ( ) ] ) ) .unwrap ( ) ,
1736
vec! [ c.hash ( ) ]
1737
) ;
1738
assert_eq! (
1739
wire:: decode_fetch( & wire:: encode_fetch( & [ c.hash ( ) ] , Some ( 4096 ) ) ) .unwrap ( ) ,
1740
( vec! [ c.hash ( ) ] , Some ( 4096 ) )
1741
) ;
1742
assert_eq! (
1743
wire:: decode_fetch( & wire:: encode_fetch( & [ ] , None ) ) .unwrap ( ) ,
1744
( vec! [ ] , None )
1745
) ;
1746
assert_eq! (
1747
wire:: decode_shallow( & wire:: encode_shallow( & [ c.hash ( ) ] , 3 ) ) .unwrap ( ) ,
1748
( vec! [ c.hash ( ) ] , 3 )
1749
) ;
b3c5a59a Andy McCall 1 month ago ↶
1750
75167672 Andy McCall 1 month ago
1751
assert_eq! (
1752
wire:: decode_fetch( & wire:: encode_hashes( & [ c.hash ( ) ] ) ) .unwrap ( ) ,
1753
( vec! [ c.hash ( ) ] , None )
1754
) ;
750251b2 Andy McCall 1 month ago ↶
1755
75167672 Andy McCall 1 month ago
1756
let up = RefUpdate {
1757
name: " main" .into ( ) ,
1758
old_state: ObjectRef( [ 1 ; 32 ] ) ,
1759
new_state: ObjectRef( [ 2 ; 32 ] ) ,
1760
force: true ,
1761
attestation: Some ( SignedAttest {
1762
attest: att,
1763
sigs: vec! [ sig] ,
1764
} ) ,
1765
} ;
1766
let sp = SignedPolicy {
1767
policy,
1768
sigs: vec! [ psig] ,
1769
} ;
1770
let ( b2, ups, pol, tags) = wire:: decode_push( & wire:: encode_push(
1771
& bundle,
1772
std:: slice:: from_ref( & up) ,
1773
Some ( & sp) ,
1774
std:: slice:: from_ref( & tag) ,
1775
) )
1776
.unwrap ( ) ;
83e0677b Andy McCall 1 month ago ↶
1777
assert_eq! ( pol, Some ( sp) ) ;
132b884b Andy McCall 1 month ago ↶
1778
assert_eq! ( tags, vec! [ tag] ) ;
750251b2 Andy McCall 1 month ago ↶
1779
assert_eq! ( b2.changes.len ( ) , 1 ) ;
1780
assert_eq! ( ups[ 0 ] .name, " main" ) ;
1781
assert! ( ups[ 0 ] .force) ;
1782
assert_eq! ( ups[ 0 ] .attestation, up.attestation) ;
75167672 Andy McCall 1 month ago
1783
let res = vec! [ RefResult {
1784
name: " main" .into ( ) ,
1785
ok: false ,
1786
reason: " stale" .into ( ) ,
1787
} ] ;
1788
assert_eq! (
1789
wire:: decode_push_response( & wire:: encode_push_response( & res) ) .unwrap ( ) ,
1790
res
1791
) ;
1792
assert_eq! (
1793
wire:: decode_capabilities( & wire:: encode_capabilities( [ 7 ; 16 ] ) ) .unwrap ( ) ,
1794
[ 7 ; 16 ]
1795
) ;
750251b2 Andy McCall 1 month ago ↶
1796
}
1797
}