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