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 > ) > ,
83e0677b Andy McCall 1 month ago ↶
67
}
68
4b9d0677 Andy McCall 18 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 17 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 16 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 19 days ago ↶
139
140
141
pub accepted : Option< SignedAttest> ,
4b9d0677 Andy McCall 18 days ago ↶
142
143
144
pub logged : Option< Logged> ,
028545df Andy McCall 17 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 19 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 10 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 10 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 22 days ago ↶
317
318
319
320
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 16 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 22 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 16 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 22 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 22 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 22 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! (
461
" {} : the stored token was refused; using the one an enclosing checkout has for this hub" ,
462
self .base
463
) ) ;
464
* self .token.lock ( ) .unwrap_or_else ( | p | p.into_inner ( ) ) = Some ( next) ;
465
}
466
other => return other,
467
}
468
}
469
}
470
471
fn call_once (
472
& self ,
473
post : bool ,
474
endpoint : & str ,
475
body : Option< & [ u8 ] > ,
476
token : Option< & str > ,
477
) -> Result< Vec< u8 > > {
750251b2 Andy McCall 1 month ago ↶
478
let url = format! ( " {} /oxy/v1/{endpoint} " , self .base) ;
75167672 Andy McCall 1 month ago
479
let mut req = if post {
480
self .agent.post ( & url)
481
} else {
482
self .agent.get ( & url)
483
} ;
ee130dd9 Andy McCall 16 days ago ↶
484
req = req
485
.set ( " Accept" , CONTENT_TYPE )
486
.set ( FEATURES_HEADER , & CLIENT_FEATURES .join ( " ," ) ) ;
68a92484 Andy McCall 22 days ago ↶
487
if let Some ( t) = token {
750251b2 Andy McCall 1 month ago ↶
488
req = req.set ( " Authorization" , & format! ( " Bearer {t} " ) ) ;
489
}
75167672 Andy McCall 1 month ago
490
if let Some ( id) = & self .repo_id {
491
req = req.set ( REPO_ID_HEADER , & hex:: encode( id) ) ;
492
}
750251b2 Andy McCall 1 month ago ↶
493
let resp = match body {
494
Some ( b) => req.set ( " Content-Type" , CONTENT_TYPE ) .send_bytes ( b) ,
495
None => req.call ( ) ,
496
} ;
497
match resp {
498
Ok ( r) => {
499
let mut out = Vec :: new( ) ;
75167672 Andy McCall 1 month ago
500
r.into_reader ( )
501
.read_to_end ( & mut out)
502
.map_err ( | e | RepoError:: Msg( format! ( " {url} : {e} " ) ) ) ? ;
750251b2 Andy McCall 1 month ago ↶
503
Ok ( out)
504
}
505
Err ( ureq:: Error:: Status( code, r) ) => {
506
let text = r.into_string ( ) .unwrap_or_default ( ) ;
67a4fe1a Andy McCall 16 days ago ↶
507
508
d3b27090 Andy McCall 11 days ago ↶
509
510
511
67a4fe1a Andy McCall 16 days ago ↶
512
let status = format! ( " {url} : HTTP {code} " ) ;
d3b27090 Andy McCall 11 days ago ↶
513
let msg = format! ( " {status} : {} " , plain_error_text ( & text) ) ;
67a4fe1a Andy McCall 16 days ago ↶
514
Err ( RepoError:: Msg( match hint_for ( & status) {
515
Some ( h) => format! ( " {msg} \n hint: {h} " ) ,
516
None => msg,
517
} ) )
750251b2 Andy McCall 1 month ago ↶
518
}
3e5772cc Andy McCall 1 month ago ↶
519
520
Err ( e) => Err ( RepoError:: Msg( with_hint ( e.to_string ( ) ) ) ) ,
750251b2 Andy McCall 1 month ago ↶
521
}
522
}
68a92484 Andy McCall 22 days ago ↶
523
}
524
525
526
527
528
fn refused ( message : & str ) -> bool {
529
[ " : HTTP 401" , " : HTTP 403" , " : HTTP 404" ]
530
.iter ( )
531
.any ( | c | message.contains ( c) )
750251b2 Andy McCall 1 month ago ↶
532
}
533
ff1f62fe Andy McCall 1 month ago ↶
534
535
536
fn refuse_plain_token ( url : & str , options : & RemoteOptions) -> Result< ( ) > {
537
if options.token.is_some ( ) & & ! crate :: credentials:: token_allowed( url) {
538
return Err ( RepoError:: Msg( format! (
539
" {url} : refusing to send a token over plain http; use https"
540
) ) ) ;
541
}
542
Ok ( ( ) )
543
}
544
3e5772cc Andy McCall 1 month ago ↶
545
546
pub fn hint_for ( message : & str ) -> Option< & 'static str > {
547
let m = message.to_ascii_lowercase ( ) ;
548
let hint = if m.contains ( " http 401" ) {
ff1f62fe Andy McCall 1 month ago ↶
549
" 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 ↶
550
} else if m.contains ( " http 403" ) {
551
" 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 ↶
552
} else if m.contains ( " http 413" ) {
553
" 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 ↶
554
} else if m.contains ( " http 404" ) {
ff1f62fe Andy McCall 1 month ago ↶
555
" 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 ↶
556
} else if m.contains ( " unknownissuer" )
557
| | m.contains ( " unknown issuer" )
558
| | m.contains ( " certificate" )
559
| | m.contains ( " invalidcertificate" )
560
{
561
" 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`"
562
} else if m.contains ( " connection refused" ) {
563
" nothing is listening there: check the URL and port, and that the server is running"
564
} else if m.contains ( " dns" )
565
| | m.contains ( " failed to lookup" )
566
| | m.contains ( " name or service not known" )
567
{
568
" the host name does not resolve: check the URL"
569
} else if m.contains ( " timed out" ) | | m.contains ( " timeout" ) {
570
" the server did not answer in time: check the network, or try again"
571
} else {
572
return None ;
573
} ;
574
Some ( hint)
575
}
576
d3b27090 Andy McCall 11 days ago ↶
577
578
579
fn plain_error_text ( text : & str ) -> String {
580
let t = text.trim ( ) ;
581
let lower = t.to_ascii_lowercase ( ) ;
582
if ! ( lower.starts_with ( " <!doctype html" ) | | lower.starts_with ( " <html" ) ) {
583
return t.to_string ( ) ;
584
}
585
let title = lower
586
.find ( " <title>" )
587
.and_then ( | i | {
588
lower[ i + 7 .. ]
589
.find ( " </title>" )
590
.map ( | j | t[ i + 7 .. i + 7 + j] .trim ( ) )
591
} )
592
.filter ( | s | ! s.is_empty ( ) ) ;
593
match title {
594
Some ( s) => format! ( " {s} (an HTML error page, from a proxy in front of the server perhaps)" ) ,
595
None => " an HTML error page (from a proxy in front of the server perhaps)" .into ( ) ,
596
}
597
}
598
3e5772cc Andy McCall 1 month ago ↶
599
600
pub fn with_hint ( message : String) -> String {
601
match hint_for ( & message) {
602
Some ( h) => format! ( " {message} \n hint: {h} " ) ,
603
None => message,
ff1f62fe Andy McCall 1 month ago ↶
604
}
605
}
606
607
608
609
610
pub fn http_json (
611
method : & str ,
612
url : & str ,
613
body : Option< & str > ,
614
options : & RemoteOptions,
615
) -> Result< ( u16 , String) > {
616
refuse_plain_token ( url, options) ? ;
617
let agent = ureq:: AgentBuilder:: new( )
618
.tls_config ( tls_client_config ( options.ca.as_deref ( ) ) ? )
619
.timeout ( std:: time:: Duration:: from_secs( 60 ) )
ee130dd9 Andy McCall 16 days ago ↶
620
.user_agent ( & user_agent ( ) )
ff1f62fe Andy McCall 1 month ago ↶
621
.build ( ) ;
622
let mut req = agent.request ( method, url) .set ( " Accept" , " application/json" ) ;
623
if let Some ( t) = & options.token {
624
req = req.set ( " Authorization" , & format! ( " Bearer {t} " ) ) ;
625
}
626
let resp = match body {
627
Some ( b) => req.set ( " Content-Type" , " application/json" ) .send_string ( b) ,
628
None => req.call ( ) ,
629
} ;
630
match resp {
631
Ok ( r) => {
632
let code = r.status ( ) ;
633
Ok ( ( code, r.into_string ( ) .unwrap_or_default ( ) ) )
634
}
635
Err ( ureq:: Error:: Status( code, r) ) => Ok ( ( code, r.into_string ( ) .unwrap_or_default ( ) ) ) ,
636
Err ( e) => Err ( RepoError:: Msg( with_hint ( e.to_string ( ) ) ) ) ,
3e5772cc Andy McCall 1 month ago ↶
637
}
638
}
639
d82e4409 Andy McCall 1 month ago ↶
640
641
642
pub fn http_get ( url : & str , options : & RemoteOptions) -> Result< Vec< u8 > > {
ff1f62fe Andy McCall 1 month ago ↶
643
refuse_plain_token ( url, options) ? ;
d82e4409 Andy McCall 1 month ago ↶
644
let agent = ureq:: AgentBuilder:: new( )
645
.tls_config ( tls_client_config ( options.ca.as_deref ( ) ) ? )
ee130dd9 Andy McCall 16 days ago ↶
646
.user_agent ( & user_agent ( ) )
d82e4409 Andy McCall 1 month ago ↶
647
.build ( ) ;
648
let mut req = agent.get ( url) .set ( " Accept" , " application/json" ) ;
649
if let Some ( t) = & options.token {
650
req = req.set ( " Authorization" , & format! ( " Bearer {t} " ) ) ;
651
}
652
match req.call ( ) {
653
Ok ( r) => {
654
let mut out = Vec :: new( ) ;
655
r.into_reader ( )
656
.read_to_end ( & mut out)
657
.map_err ( | e | RepoError:: Msg( format! ( " {url} : {e} " ) ) ) ? ;
658
Ok ( out)
659
}
660
Err ( ureq:: Error:: Status( code, r) ) => {
661
let text = r.into_string ( ) .unwrap_or_default ( ) ;
3e5772cc Andy McCall 1 month ago ↶
662
Err ( RepoError:: Msg( with_hint ( format! (
4213c41d Andy McCall 1 month ago ↶
663
" {url} : HTTP {code} : {} " ,
664
text.trim ( )
3e5772cc Andy McCall 1 month ago ↶
665
) ) ) )
d82e4409 Andy McCall 1 month ago ↶
666
}
3e5772cc Andy McCall 1 month ago ↶
667
668
Err ( e) => Err ( RepoError:: Msg( with_hint ( e.to_string ( ) ) ) ) ,
d82e4409 Andy McCall 1 month ago ↶
669
}
670
}
671
750251b2 Andy McCall 1 month ago ↶
672
impl Remote for HttpRemote {
673
fn repo_id ( & self ) -> Result< [ u8 ; 16 ] > {
674
wire:: decode_capabilities( & self .call ( false , " capabilities" , None ) ? )
675
}
83e0677b Andy McCall 1 month ago ↶
676
fn refs ( & self ) -> Result< RefsResponse> {
750251b2 Andy McCall 1 month ago ↶
677
Ok ( wire:: decode_refs( & self .call ( false , " refs" , None ) ? ) ? .1 )
678
}
679
fn negotiate ( & self , have : & [ChangeHash], want : & [ChangeHash]) -> Result< Vec< ChangeHash> > {
75167672 Andy McCall 1 month ago
680
wire:: decode_hashes( & self .call (
681
true ,
682
" negotiate" ,
683
Some ( & wire:: encode_negotiate( have, want) ) ,
684
) ? )
750251b2 Andy McCall 1 month ago ↶
685
}
b3c5a59a Andy McCall 1 month ago ↶
686
fn fetch ( & self , changes : & [ChangeHash], max_blob : Option< u64 > ) -> Result< Bundle> {
75167672 Andy McCall 1 month ago
687
wire:: decode_bundle( & self .call (
688
true ,
689
" fetch" ,
690
Some ( & wire:: encode_fetch( changes, max_blob) ) ,
691
) ? )
750251b2 Andy McCall 1 month ago ↶
692
}
b3c5a59a Andy McCall 1 month ago ↶
693
fn objects ( & self , refs : & [ObjectRef]) -> Result< Vec< ( ObjectType, Vec< u8 > ) > > {
75167672 Andy McCall 1 month ago
694
Ok (
695
wire:: decode_bundle( & self .call ( true , " objects" , Some ( & wire:: encode_hashes( refs) ) ) ? ) ?
696
.objects,
697
)
b3c5a59a Andy McCall 1 month ago ↶
698
}
2863456a Andy McCall 1 month ago ↶
699
fn checkpoint ( & self , heads : & [ChangeHash]) -> Result< Bundle> {
f3811eb2 Andy McCall 11 days ago ↶
700
let body = {
701
let _t = crate :: timing:: phase( " remote: checkpoint answer" ) ;
702
self .call ( true , " checkpoint" , Some ( & wire:: encode_hashes( heads) ) ) ?
703
} ;
704
let _t = crate :: timing:: phase( " remote: decode the checkpoint" ) ;
80b069fa Andy McCall 10 days ago ↶
705
wire:: decode_bundle( & body)
706
}
707
fn checkpoint_index ( & self , heads : & [ChangeHash]) -> Result< Bundle> {
708
let body = {
709
let _t = crate :: timing:: phase( " remote: checkpoint index" ) ;
710
match self .call ( true , " checkpoint-index" , Some ( & wire:: encode_hashes( heads) ) ) {
711
Ok ( b) => b,
712
713
Err ( RepoError:: Msg( m) ) if m.contains ( " HTTP 404" ) => return self .checkpoint ( heads) ,
714
Err ( e) => return Err ( e) ,
715
}
716
} ;
f3811eb2 Andy McCall 11 days ago ↶
717
wire:: decode_bundle( & body)
2863456a Andy McCall 1 month ago ↶
718
}
719
fn shallow ( & self , heads : & [ChangeHash], depth : u32 ) -> Result< Vec< ChangeHash> > {
75167672 Andy McCall 1 month ago
720
wire:: decode_hashes( & self .call (
721
true ,
722
" shallow" ,
723
Some ( & wire:: encode_shallow( heads, depth) ) ,
724
) ? )
2863456a Andy McCall 1 month ago ↶
725
}
75167672 Andy McCall 1 month ago
726
fn push (
727
& self ,
728
bundle : Bundle,
729
updates : Vec< RefUpdate> ,
730
policy : Option< SignedPolicy> ,
731
tags : Vec< TagRef> ,
732
) -> Result< Vec< RefResult> > {
733
wire:: decode_push_response( & self .call (
734
true ,
735
" push" ,
736
Some ( & wire:: encode_push(
737
& bundle,
738
& updates,
739
policy.as_ref ( ) ,
740
& tags,
741
) ) ,
742
) ? )
750251b2 Andy McCall 1 month ago ↶
743
}
744
}
745
827281ee Andy McCall 1 month ago ↶
746
747
748
749
pub fn stored_url ( url : & str ) -> String {
8d803563 Andy McCall 1 month ago ↶
750
751
9f9e72b9 Andy McCall 1 month ago ↶
752
let ( prefix, path) = match ( url.strip_prefix ( " file://" ) , url.strip_prefix ( " dir://" ) ) {
753
( Some ( p) , _ ) => ( " file://" , p) ,
754
( _ , Some ( p) ) => ( " dir://" , p) ,
8d803563 Andy McCall 1 month ago ↶
755
_ if url.contains ( " ://" ) => return url.to_string ( ) ,
9f9e72b9 Andy McCall 1 month ago ↶
756
_ => ( " " , url) ,
827281ee Andy McCall 1 month ago ↶
757
} ;
758
match std:: path:: absolute( path) {
759
Ok ( abs) => format! ( " {prefix} {} " , abs.display ( ) ) ,
760
Err ( _ ) => url.to_string ( ) ,
761
}
762
}
763
9f9e72b9 Andy McCall 1 month ago ↶
764
765
2b5ee371 Andy McCall 1 month ago ↶
766
pub fn open_remote ( url : & str , options : & RemoteOptions) -> Result< Box< dyn Remote> > {
750251b2 Andy McCall 1 month ago ↶
767
if url.starts_with ( " http://" ) | | url.starts_with ( " https://" ) {
2b5ee371 Andy McCall 1 month ago ↶
768
return Ok ( Box :: new( HttpRemote:: new( url, options) ? ) ) ;
9f9e72b9 Andy McCall 1 month ago ↶
769
}
770
if let Some ( store) = open_store ( url, options) {
771
return crate :: repo:: strata:: open_store_remote( store? , options) ;
750251b2 Andy McCall 1 month ago ↶
772
}
5b9c6ad4 Andy McCall 1 month ago ↶
773
let path = url.strip_prefix ( " file://" ) .unwrap_or ( url) ;
774
if path.contains ( " ://" ) {
750251b2 Andy McCall 1 month ago ↶
775
return Err ( RepoError:: Msg( format! ( " unsupported remote URL {url:?} " ) ) ) ;
5b9c6ad4 Andy McCall 1 month ago ↶
776
}
84b9e9ac Andy McCall 1 month ago ↶
777
let mut local = LocalRemote:: open( Path:: new( path) ) ? ;
778
local.claimed_id = options.repo_id;
779
Ok ( Box :: new( local) )
5b9c6ad4 Andy McCall 1 month ago ↶
780
}
750251b2 Andy McCall 1 month ago ↶
781
782
783
784
785
pub mod wire {
786
use super :: * ;
787
788
fn schema ( msg : & str ) -> RepoError {
789
RepoError:: Object( ObjectError:: Schema( msg.into ( ) ) )
790
}
791
fn sigs_value ( sigs : & [Signature]) -> Value {
792
Value:: Array( sigs.iter ( ) .map ( | s | s.to_value ( ) ) .collect ( ) )
793
}
794
fn sigs_from ( v : & Value) -> Result< Vec< Signature> > {
75167672 Andy McCall 1 month ago
795
Ok ( o:: array( v, " signatures" ) ?
796
.iter ( )
797
.map ( Signature:: from_value)
798
.collect:: < std:: result:: Result< Vec< _ > , _ > > ( ) ? )
750251b2 Andy McCall 1 month ago ↶
799
}
800
fn hashes_value ( hs : & [ObjectRef]) -> Value {
801
Value:: Array( hs.iter ( ) .map ( | h | Value:: Bytes( h.0. to_vec ( ) ) ) .collect ( ) )
802
}
803
fn hashes_from ( v : & Value) -> Result< Vec< ObjectRef> > {
75167672 Andy McCall 1 month ago
804
Ok ( o:: array( v, " hashes" ) ?
805
.iter ( )
806
.map ( | h | o:: oref( h, " hash" ) )
807
.collect:: < std:: result:: Result< Vec< _ > , _ > > ( ) ? )
750251b2 Andy McCall 1 month ago ↶
808
}
809
fn attest_value ( sa : & SignedAttest) -> Value {
810
Value:: Array( vec! [ Value:: Bytes( sa.attest.encode ( ) ) , sigs_value ( & sa.sigs) ] )
811
}
812
fn attest_from ( v : & Value) -> Result< SignedAttest> {
813
let a = o:: array( v, " signed attest" ) ? ;
814
if a.len ( ) ! = 2 {
815
return Err ( schema ( " signed attest: expected [attest, sigs]" ) ) ;
816
}
75167672 Andy McCall 1 month ago
817
Ok ( SignedAttest {
818
attest: Attest:: decode( o:: bytes( & a[ 0 ] , " attest" ) ? ) ? ,
819
sigs: sigs_from ( & a[ 1 ] ) ? ,
820
} )
750251b2 Andy McCall 1 month ago ↶
821
}
822
fn opt_attest_value ( sa : & Option< SignedAttest> ) -> Value {
823
sa.as_ref ( ) .map ( attest_value) .unwrap_or ( Value:: Null)
824
}
825
fn opt_attest_from ( v : & Value) -> Result< Option< SignedAttest> > {
75167672 Andy McCall 1 month ago
826
if v.is_null ( ) {
827
Ok ( None )
828
} else {
829
attest_from ( v) .map ( Some )
830
}
750251b2 Andy McCall 1 month ago ↶
831
}
28fc23d7 Andy McCall 1 month ago ↶
832
833
834
pub fn policy_value ( sp : & SignedPolicy) -> Value {
835
Value:: Array( vec! [ Value:: Bytes( sp.policy.encode ( ) ) , sigs_value ( & sp.sigs) ] )
83e0677b Andy McCall 1 month ago ↶
836
}
28fc23d7 Andy McCall 1 month ago ↶
837
pub fn policy_from ( v : & Value) -> Result< SignedPolicy> {
83e0677b Andy McCall 1 month ago ↶
838
let a = o:: array( v, " signed policy" ) ? ;
839
if a.len ( ) ! = 2 {
840
return Err ( schema ( " signed policy: expected [policy, sigs]" ) ) ;
841
}
75167672 Andy McCall 1 month ago
842
Ok ( SignedPolicy {
843
policy: Policy:: decode( o:: bytes( & a[ 0 ] , " policy" ) ? ) ? ,
844
sigs: sigs_from ( & a[ 1 ] ) ? ,
845
} )
28fc23d7 Andy McCall 1 month ago ↶
846
}
847
fn opt_policy_value ( sp : Option< & SignedPolicy> ) -> Value {
848
sp.map ( policy_value) .unwrap_or ( Value:: Null)
849
}
850
fn opt_policy_from ( v : & Value) -> Result< Option< SignedPolicy> > {
75167672 Andy McCall 1 month ago
851
if v.is_null ( ) {
852
Ok ( None )
853
} else {
854
policy_from ( v) .map ( Some )
855
}
83e0677b Andy McCall 1 month ago ↶
856
}
750251b2 Andy McCall 1 month ago ↶
857
fn map < 'a > ( v : & 'a Value, what : & str ) -> Result< & 'a o:: Map> {
858
Ok ( o:: map( v, what) ? )
859
}
860
fn field < 'a > ( m : & 'a o:: Map, k : u64 , what : & str ) -> Result< & 'a Value> {
861
Ok ( o:: field( m, k, what) ? )
862
}
863
6f8aeb99 Andy McCall 1 month ago ↶
864
865
866
750251b2 Andy McCall 1 month ago ↶
867
pub fn bundle_value ( b : & Bundle) -> Value {
2c75672f Andy McCall 1 month ago ↶
868
let mut changes = Vec :: with_capacity( b.raw_changes.len ( ) + b.changes.len ( ) ) ;
869
870
871
for ( bytes, s) in & b.raw_changes {
872
changes.push ( Value:: Array( vec! [
873
Value:: Bytes( bytes.clone ( ) ) ,
874
sigs_value ( s) ,
875
] ) ) ;
876
}
6f8aeb99 Andy McCall 1 month ago ↶
877
let mut texts: Vec< ( ObjectType, Vec< u8 > ) > = Vec :: new( ) ;
878
let mut listed: Option< std:: collections:: HashSet< ObjectRef> > = None ;
879
for ( c, s) in & b.changes {
880
let ( bytes, stored) = c.encode_parts ( ) ;
881
for t in stored {
882
let listed = listed.get_or_insert_with ( | | {
883
b.objects
884
.iter ( )
885
.map ( | ( ty , body ) | crate :: hash:: hash_object( * ty, body) )
886
.collect ( )
887
} ) ;
888
if ! listed.insert ( t.blob.hash ( ) ) {
889
continue ;
890
}
891
for chunk in crate :: chunk:: chunks( & t.content) {
892
if listed.insert ( crate :: hash:: hash_object( ObjectType:: Chunk, chunk) ) {
893
texts.push ( ( ObjectType:: Chunk, chunk.to_vec ( ) ) ) ;
894
}
895
}
896
texts.push ( ( ObjectType:: Blob, t.blob.encode ( ) ) ) ;
897
}
898
changes.push ( Value:: Array( vec! [ Value:: Bytes( bytes) , sigs_value ( s) ] ) ) ;
899
}
f0a517d2 Andy McCall 19 days ago ↶
900
let mut v = Value:: map( [
6f8aeb99 Andy McCall 1 month ago ↶
901
( 0 , Value:: Array( changes) ) ,
750251b2 Andy McCall 1 month ago ↶
902
(
75167672 Andy McCall 1 month ago
903
1 ,
904
Value:: Array( b.attests.iter ( ) .map ( attest_value) .collect ( ) ) ,
905
) ,
906
(
750251b2 Andy McCall 1 month ago ↶
907
2 ,
908
Value:: Array(
75167672 Andy McCall 1 month ago
909
b.objects
910
.iter ( )
6f8aeb99 Andy McCall 1 month ago ↶
911
.chain ( & texts)
75167672 Andy McCall 1 month ago
912
.map ( | ( t , body ) | {
913
Value:: Array( vec! [
914
Value:: Text( t.name ( ) .into ( ) ) ,
915
Value:: Bytes( body.clone ( ) ) ,
916
] )
917
} )
918
.collect ( ) ,
750251b2 Andy McCall 1 month ago ↶
919
) ,
920
) ,
75167672 Andy McCall 1 month ago
921
(
922
3 ,
923
Value:: Array( b.policies.iter ( ) .map ( policy_value) .collect ( ) ) ,
924
) ,
f0a517d2 Andy McCall 19 days ago ↶
925
] ) ;
926
927
if ! b.certificates.is_empty ( ) {
928
if let Value:: Map( m) = & mut v {
929
m.insert (
930
4 ,
931
Value:: Array(
932
b.certificates
933
.iter ( )
934
.map ( | c | Value:: Bytes( c.encode ( ) ) )
935
.collect ( ) ,
936
) ,
937
) ;
938
}
939
}
940
v
750251b2 Andy McCall 1 month ago ↶
941
}
942
943
pub fn bundle_from_value ( v : & Value) -> Result< Bundle> {
944
let m = map ( v, " bundle" ) ? ;
945
let mut b = Bundle:: default( ) ;
946
for ob in o:: array( field ( m, 2 , " objects" ) ? , " objects" ) ? {
947
let pair = o:: array( ob, " object" ) ? ;
948
if pair.len ( ) ! = 2 {
949
return Err ( schema ( " object: expected [type, body]" ) ) ;
950
}
75167672 Andy McCall 1 month ago
951
let ty = ObjectType:: from_name( o:: text( & pair[ 0 ] , " object type" ) ? )
952
.ok_or_else ( | | schema ( " unknown object type" ) ) ? ;
953
b.objects
954
.push ( ( ty, o:: bytes( & pair[ 1 ] , " object body" ) ? .to_vec ( ) ) ) ;
6f8aeb99 Andy McCall 1 month ago ↶
955
}
956
957
let mut by_ref: Option< std:: collections:: HashMap< ObjectRef, usize > > = None ;
958
for c in o:: array( field ( m, 0 , " changes" ) ? , " changes" ) ? {
959
let pair = o:: array( c, " change entry" ) ? ;
960
if pair.len ( ) ! = 2 {
961
return Err ( schema ( " change entry: expected [change, sigs]" ) ) ;
962
}
598009a7 Andy McCall 1 month ago ↶
963
let encoded = o:: bytes( & pair[ 0 ] , " change" ) ? ;
964
b.hashes
965
.push ( crate :: hash:: hash_object( ObjectType:: Change, encoded) ) ;
0f93f49c Andy McCall 1 month ago ↶
966
let change = Change:: decode_with( encoded, & mut | text, count, form| {
6f8aeb99 Andy McCall 1 month ago ↶
967
let by_ref = by_ref.get_or_insert_with ( | | {
968
b.objects
969
.iter ( )
970
.enumerate ( )
971
.map ( | ( i , (ty , body ) ) | ( crate :: hash:: hash_object( * ty, body) , i) )
972
.collect ( )
973
} ) ;
974
let get =
975
| r : & ObjectRef, want : ObjectType| match by_ref.get ( r) .map ( | i | & b.objects[ * i] ) {
976
Some ( ( ty, body) ) if * ty = = want => Ok ( body) ,
977
_ => Err ( ObjectError:: Schema( format! (
978
" the bundle lacks {} {} " ,
979
want.name ( ) ,
980
r.short ( 12 )
981
) ) ) ,
982
} ;
983
let blob = crate :: objects:: Blob:: decode( get ( text, ObjectType:: Blob) ? ) ? ;
984
let mut content = Vec :: with_capacity( blob.length as usize ) ;
985
for chunk in & blob.chunks {
986
content.extend_from_slice ( get ( chunk, ObjectType:: Chunk) ? ) ;
987
}
0f93f49c Andy McCall 1 month ago ↶
988
crate :: objects:: text_lines( & content, count, form)
6f8aeb99 Andy McCall 1 month ago ↶
989
} ) ? ;
990
b.changes.push ( ( change, sigs_from ( & pair[ 1 ] ) ? ) ) ;
991
}
992
for a in o:: array( field ( m, 1 , " attests" ) ? , " attests" ) ? {
993
b.attests.push ( attest_from ( a) ? ) ;
750251b2 Andy McCall 1 month ago ↶
994
}
28fc23d7 Andy McCall 1 month ago ↶
995
if let Some ( ps) = m.get ( & 3 ) {
996
for p in o:: array( ps, " policies" ) ? {
997
b.policies.push ( policy_from ( p) ? ) ;
f0a517d2 Andy McCall 19 days ago ↶
998
}
999
}
1000
if let Some ( cs) = m.get ( & 4 ) {
1001
for c in o:: array( cs, " certificates" ) ? {
1002
b.certificates.push (
1003
crate :: cert:: SignedCertificate:: decode( o:: bytes( c, " certificate" ) ? )
1004
.map_err ( | e | schema ( & format! ( " certificate: {e} " ) ) ) ? ,
1005
) ;
28fc23d7 Andy McCall 1 month ago ↶
1006
}
1007
}
750251b2 Andy McCall 1 month ago ↶
1008
Ok ( b)
1009
}
1010
1011
pub fn encode_bundle ( b : & Bundle) -> Vec< u8 > {
1012
cbor:: encode( & bundle_value ( b) )
1013
}
1014
pub fn decode_bundle ( bytes : & [u8 ]) -> Result< Bundle> {
1015
bundle_from_value ( & cbor:: decode( bytes) .map_err ( ObjectError:: from) ? )
1016
}
1017
1018
pub fn encode_capabilities ( id : [u8 ; 16]) -> Vec< u8 > {
1019
cbor:: encode( & Value:: map( [
1020
( 0 , Value:: Array( vec! [ Value:: Uint( PROTOCOL_VERSION ) ] ) ) ,
1021
( 1 , Value:: Bytes( id.to_vec ( ) ) ) ,
1022
( 2 , Value:: Array( vec! [ Value:: Text( " seal" .into ( ) ) ] ) ) ,
1023
] ) )
1024
}
1025
pub fn decode_capabilities ( bytes : & [u8 ]) -> Result< [ u8 ; 16 ] > {
1026
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
1027
let m = map ( & v, " capabilities" ) ? ;
1028
let versions = o:: array( field ( m, 0 , " versions" ) ? , " versions" ) ? ;
75167672 Andy McCall 1 month ago
1029
if ! versions
1030
.iter ( )
1031
.any ( | x | x.as_uint ( ) = = Some ( PROTOCOL_VERSION ) )
1032
{
1033
return Err ( RepoError:: Msg(
1034
" remote does not speak protocol version 1" .into ( ) ,
1035
) ) ;
750251b2 Andy McCall 1 month ago ↶
1036
}
75167672 Andy McCall 1 month ago
1037
o:: bytes( field ( m, 1 , " repo id" ) ? , " repo id" ) ?
1038
.try_into ( )
1039
.map_err ( | _| schema ( " repo id: expected 16 bytes" ) )
750251b2 Andy McCall 1 month ago ↶
1040
}
1041
132b884b Andy McCall 1 month ago ↶
1042
fn tags_value ( tags : & [TagRef]) -> Value {
1043
Value:: Array(
1044
tags.iter ( )
1045
.map ( | t | {
1ec6fdbc Andy McCall 16 days ago ↶
1046
let mut v = Value:: map( [
132b884b Andy McCall 1 month ago ↶
1047
( 0 , Value:: Text( t.name.clone ( ) ) ) ,
1048
( 1 , Value:: Bytes( t.state.0. to_vec ( ) ) ) ,
1049
( 2 , hashes_value ( & t.heads) ) ,
1050
( 3 , opt_attest_value ( & t.attestation) ) ,
1ec6fdbc Andy McCall 16 days ago ↶
1051
] ) ;
1052
1053
1054
if let Value:: Map( m) = & mut v {
1055
if t.accepted.is_some ( ) {
1056
m.insert ( 4 , opt_attest_value ( & t.accepted) ) ;
1057
}
1058
if let Some ( l) = & t.logged {
1059
m.insert ( 5 , logged_value ( l) ) ;
1060
}
1061
if ! t.integration.is_empty ( ) {
1062
m.insert ( 6 , integration_value ( & t.integration) ) ;
1063
}
1064
}
1065
v
132b884b Andy McCall 1 month ago ↶
1066
} )
1067
.collect ( ) ,
1068
)
1069
}
1070
fn tags_from ( v : & Value) -> Result< Vec< TagRef> > {
1071
let mut out = Vec :: new( ) ;
1072
for t in o:: array( v, " tags" ) ? {
1073
let tm = map ( t, " tag" ) ? ;
1074
out.push ( TagRef {
1075
name: o:: text( field ( tm, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
1076
state: o:: oref( field ( tm, 1 , " state" ) ? , " state" ) ? ,
1077
heads: hashes_from ( field ( tm, 2 , " heads" ) ? ) ? ,
1078
attestation: opt_attest_from ( field ( tm, 3 , " attestation" ) ? ) ? ,
1ec6fdbc Andy McCall 16 days ago ↶
1079
accepted: match tm.get ( & 4 ) {
1080
Some ( a) => opt_attest_from ( a) ? ,
1081
None => None ,
1082
} ,
1083
logged: tm.get ( & 5 ) .map ( logged_from) .transpose ( ) ? ,
1084
integration: match tm.get ( & 6 ) {
1085
Some ( l) => integration_from ( l) ? ,
1086
None => Vec :: new( ) ,
1087
} ,
132b884b Andy McCall 1 month ago ↶
1088
} ) ;
1ec6fdbc Andy McCall 16 days ago ↶
1089
}
1090
Ok ( out)
1091
}
1092
1093
1094
1095
1096
# [ cfg ( test ) ]
1097
pub ( super ) fn tags_as_0_5_read_them ( bytes : & [u8 ]) -> Result< Vec< TagRef> > {
1098
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
1099
let m = map ( & v, " refs" ) ? ;
1100
let mut out = Vec :: new( ) ;
1101
for t in o:: array( field ( m, 3 , " tags" ) ? , " tags" ) ? {
1102
let tm = map ( t, " tag" ) ? ;
1103
out.push ( TagRef:: new(
1104
o:: text( field ( tm, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
1105
o:: oref( field ( tm, 1 , " state" ) ? , " state" ) ? ,
1106
hashes_from ( field ( tm, 2 , " heads" ) ? ) ? ,
1107
opt_attest_from ( field ( tm, 3 , " attestation" ) ? ) ? ,
1108
) ) ;
132b884b Andy McCall 1 month ago ↶
1109
}
1110
Ok ( out)
1ec6fdbc Andy McCall 16 days ago ↶
1111
}
1112
1113
1114
fn logged_value ( l : & Logged) -> Value {
1115
Value:: Array( vec! [
1116
Value:: Uint( l.index) ,
1117
Value:: Bytes( l.entry.clone ( ) ) ,
1118
proof_value ( & l.proof) ,
1119
] )
132b884b Andy McCall 1 month ago ↶
1120
}
1ec6fdbc Andy McCall 16 days ago ↶
1121
fn logged_from ( v : & Value) -> Result< Logged> {
1122
let a = o:: array( v, " logged" ) ? ;
1123
if a.len ( ) ! = 3 {
1124
return Err ( schema ( " logged: expected [index, entry, proof]" ) ) ;
1125
}
1126
Ok ( Logged {
1127
index: o:: uint( & a[ 0 ] , " index" ) ? ,
1128
entry: o:: bytes( & a[ 1 ] , " entry" ) ? .to_vec ( ) ,
1129
proof: proof_from ( & a[ 2 ] ) ? ,
1130
} )
1131
}
132b884b Andy McCall 1 month ago ↶
1132
1ec6fdbc Andy McCall 16 days ago ↶
1133
1134
fn integration_value ( integration : & [Integration]) -> Value {
1135
Value:: Array(
1136
integration
1137
.iter ( )
1138
.map ( | i | Value:: Array( vec! [ Value:: Text( i.note.clone ( ) ) , proof_value ( & i.proof) ] ) )
1139
.collect ( ) ,
1140
)
1141
}
1142
fn integration_from ( v : & Value) -> Result< Vec< Integration> > {
1143
let mut out = Vec :: new( ) ;
1144
for i in o:: array( v, " integration" ) ? {
1145
let a = o:: array( i, " integration" ) ? ;
1146
if a.len ( ) ! = 2 {
1147
return Err ( schema ( " integration: expected [note, proof]" ) ) ;
1148
}
1149
out.push ( Integration {
1150
note: o:: text( & a[ 0 ] , " note" ) ? .to_owned ( ) ,
1151
proof: proof_from ( & a[ 1 ] ) ? ,
1152
} ) ;
1153
}
1154
Ok ( out)
1155
}
1156
83e0677b Andy McCall 1 month ago ↶
1157
pub fn encode_refs ( id : [u8 ; 16], resp : & RefsResponse) -> Vec< u8 > {
4b9d0677 Andy McCall 18 days ago ↶
1158
let mut v = Value:: map( [
750251b2 Andy McCall 1 month ago ↶
1159
(
1160
0 ,
1161
Value:: Array(
83e0677b Andy McCall 1 month ago ↶
1162
resp.refs
1163
.iter ( )
750251b2 Andy McCall 1 month ago ↶
1164
.map ( | r | {
f53cbe7a Andy McCall 19 days ago ↶
1165
let mut v = Value:: map( [
750251b2 Andy McCall 1 month ago ↶
1166
( 0 , Value:: Text( r.name.clone ( ) ) ) ,
1167
( 1 , Value:: Bytes( r.state.0. to_vec ( ) ) ) ,
1168
( 2 , hashes_value ( & r.heads) ) ,
1169
( 3 , opt_attest_value ( & r.attestation) ) ,
1170
( 4 , r.last_serial.map ( Value:: Uint) .unwrap_or ( Value:: Null) ) ,
f53cbe7a Andy McCall 19 days ago ↶
1171
] ) ;
1172
4b9d0677 Andy McCall 18 days ago ↶
1173
if let Value:: Map( m) = & mut v {
1174
if r.accepted.is_some ( ) {
f53cbe7a Andy McCall 19 days ago ↶
1175
m.insert ( 5 , opt_attest_value ( & r.accepted) ) ;
4b9d0677 Andy McCall 18 days ago ↶
1176
}
1177
if let Some ( l) = & r.logged {
1ec6fdbc Andy McCall 16 days ago ↶
1178
m.insert ( 6 , logged_value ( l) ) ;
f53cbe7a Andy McCall 19 days ago ↶
1179
}
028545df Andy McCall 17 days ago ↶
1180
if ! r.integration.is_empty ( ) {
1ec6fdbc Andy McCall 16 days ago ↶
1181
m.insert ( 7 , integration_value ( & r.integration) ) ;
028545df Andy McCall 17 days ago ↶
1182
}
f53cbe7a Andy McCall 19 days ago ↶
1183
}
1184
v
750251b2 Andy McCall 1 month ago ↶
1185
} )
1186
.collect ( ) ,
1187
) ,
1188
) ,
1189
( 1 , Value:: Bytes( id.to_vec ( ) ) ) ,
83e0677b Andy McCall 1 month ago ↶
1190
( 2 , opt_policy_value ( resp.policy.as_ref ( ) ) ) ,
132b884b Andy McCall 1 month ago ↶
1191
( 3 , tags_value ( & resp.tags) ) ,
75167672 Andy McCall 1 month ago
1192
(
1193
4 ,
1194
Value:: Array( resp.history.iter ( ) .map ( policy_value) .collect ( ) ) ,
1195
) ,
14edbaaf Andy McCall 1 month ago ↶
1196
(
1197
5 ,
1198
resp.default_bookmark
1199
.clone ( )
1200
.map ( Value:: Text)
1201
.unwrap_or ( Value:: Null) ,
1202
) ,
4b9d0677 Andy McCall 18 days ago ↶
1203
] ) ;
028545df Andy McCall 17 days ago ↶
1204
if let Value:: Map( m) = & mut v {
1205
if let Some ( log) = & resp.log {
1206
m.insert ( 6 , Value:: Text( log.clone ( ) ) ) ;
1207
}
1208
if ! resp.witnesses.is_empty ( ) {
1209
m.insert (
1210
7 ,
1211
Value:: Array(
1212
resp.witnesses
1213
.iter ( )
1214
.map ( | ( name , id ) | {
1215
Value:: Array( vec! [
1216
Value:: Text( name.clone ( ) ) ,
1217
Value:: Bytes( id.clone ( ) ) ,
1218
] )
1219
} )
1220
.collect ( ) ,
1221
) ,
1222
) ;
1223
}
4b9d0677 Andy McCall 18 days ago ↶
1224
}
1225
cbor:: encode( & v)
750251b2 Andy McCall 1 month ago ↶
1226
}
83e0677b Andy McCall 1 month ago ↶
1227
pub fn decode_refs ( bytes : & [u8 ]) -> Result< ( [u8 ; 16], RefsResponse) > {
750251b2 Andy McCall 1 month ago ↶
1228
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
1229
let m = map ( & v, " refs" ) ? ;
75167672 Andy McCall 1 month ago
1230
let id: [ u8 ; 16 ] = o:: bytes( field ( m, 1 , " repo id" ) ? , " repo id" ) ?
1231
.try_into ( )
1232
.map_err ( | _| schema ( " repo id: expected 16 bytes" ) ) ? ;
750251b2 Andy McCall 1 month ago ↶
1233
let mut refs = Vec :: new( ) ;
1234
for r in o:: array( field ( m, 0 , " refs" ) ? , " refs" ) ? {
1235
let rm = map ( r, " ref" ) ? ;
1236
let ls = field ( rm, 4 , " last serial" ) ? ;
1237
refs.push ( RemoteRef {
1238
name: o:: text( field ( rm, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
1239
state: o:: oref( field ( rm, 1 , " state" ) ? , " state" ) ? ,
1240
heads: hashes_from ( field ( rm, 2 , " heads" ) ? ) ? ,
1241
attestation: opt_attest_from ( field ( rm, 3 , " attestation" ) ? ) ? ,
75167672 Andy McCall 1 month ago
1242
last_serial: if ls.is_null ( ) {
1243
None
1244
} else {
1245
Some ( o:: uint( ls, " last serial" ) ? )
f53cbe7a Andy McCall 19 days ago ↶
1246
} ,
1247
accepted: match rm.get ( & 5 ) {
1248
Some ( a) => opt_attest_from ( a) ? ,
1249
None => None ,
75167672 Andy McCall 1 month ago
1250
} ,
1ec6fdbc Andy McCall 16 days ago ↶
1251
logged: rm.get ( & 6 ) .map ( logged_from) .transpose ( ) ? ,
028545df Andy McCall 17 days ago ↶
1252
integration: match rm.get ( & 7 ) {
1ec6fdbc Andy McCall 16 days ago ↶
1253
Some ( l) => integration_from ( l) ? ,
028545df Andy McCall 17 days ago ↶
1254
None => Vec :: new( ) ,
1255
} ,
750251b2 Andy McCall 1 month ago ↶
1256
} ) ;
1257
}
83e0677b Andy McCall 1 month ago ↶
1258
let policy = match m.get ( & 2 ) {
1259
Some ( p) => opt_policy_from ( p) ? ,
1260
None => None ,
1261
} ;
132b884b Andy McCall 1 month ago ↶
1262
let tags = match m.get ( & 3 ) {
1263
Some ( t) => tags_from ( t) ? ,
1264
None => Vec :: new( ) ,
1265
} ;
28fc23d7 Andy McCall 1 month ago ↶
1266
let mut history = Vec :: new( ) ;
1267
if let Some ( h) = m.get ( & 4 ) {
1268
for p in o:: array( h, " history" ) ? {
1269
history.push ( policy_from ( p) ? ) ;
1270
}
1271
}
14edbaaf Andy McCall 1 month ago ↶
1272
let default_bookmark = match m.get ( & 5 ) {
4b9d0677 Andy McCall 18 days ago ↶
1273
Some ( Value:: Text( t) ) => Some ( t.clone ( ) ) ,
1274
_ => None ,
1275
} ;
1276
let log = match m.get ( & 6 ) {
14edbaaf Andy McCall 1 month ago ↶
1277
Some ( Value:: Text( t) ) => Some ( t.clone ( ) ) ,
1278
_ => None ,
1279
} ;
028545df Andy McCall 17 days ago ↶
1280
let mut witnesses = Vec :: new( ) ;
1281
if let Some ( w) = m.get ( & 7 ) {
1282
for x in o:: array( w, " witnesses" ) ? {
1283
let a = o:: array( x, " witness" ) ? ;
1284
if a.len ( ) ! = 2 {
1285
return Err ( schema ( " witness: expected [name, identity]" ) ) ;
1286
}
1287
witnesses.push ( (
1288
o:: text( & a[ 0 ] , " witness name" ) ? .to_owned ( ) ,
1289
o:: bytes( & a[ 1 ] , " witness identity" ) ? .to_vec ( ) ,
1290
) ) ;
1291
}
1292
}
75167672 Andy McCall 1 month ago
1293
Ok ( (
1294
id,
1295
RefsResponse {
1296
refs,
1297
policy,
1298
tags,
1299
history,
14edbaaf Andy McCall 1 month ago ↶
1300
default_bookmark,
4b9d0677 Andy McCall 18 days ago ↶
1301
log,
028545df Andy McCall 17 days ago ↶
1302
witnesses,
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 16 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 19 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 19 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 19 days ago ↶
1577
grant: None ,
f0a517d2 Andy McCall 19 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 19 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 19 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 16 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! [ ] ,
75167672 Andy McCall 1 month ago
1641
} ;
028545df Andy McCall 17 days ago ↶
1642
1643
1644
let old = wire:: encode_refs( [ 3 ; 16 ] , & resp) ;
1645
let back = wire:: decode_refs( & old) .unwrap ( ) .1 ;
1646
assert! ( back.witnesses.is_empty ( ) & & back.refs.iter ( ) .all ( | r | r.integration.is_empty ( ) ) ) ;
4b9d0677 Andy McCall 18 days ago ↶
1647
resp.refs[ 1 ] .logged = Some ( Logged {
1648
index: 41 ,
1649
entry: vec! [ 1 , 2 , 3 ] ,
1650
proof: vec! [ [ 9 ; 32 ] , [ 8 ; 32 ] ] ,
1651
} ) ;
028545df Andy McCall 17 days ago ↶
1652
resp.refs[ 1 ] .integration = vec! [ Integration {
1653
note: " hub.example/log\n 42\n root\n \n - sig\n " .into ( ) ,
1654
proof: vec! [ [ 7 ; 32 ] ] ,
1655
} ] ;
1656
resp.witnesses = vec! [ ( " w.example" .into ( ) , vec! [ 4 , 5 , 6 ] ) ] ;
1657
let new = wire:: encode_refs( [ 3 ; 16 ] , & resp) ;
1658
let ( id_back, back) = wire:: decode_refs( & new) .unwrap ( ) ;
4b9d0677 Andy McCall 18 days ago ↶
1659
assert_eq! ( back.log, resp.log) ;
1660
assert_eq! ( back.refs[ 1 ] .logged, resp.refs[ 1 ] .logged) ;
028545df Andy McCall 17 days ago ↶
1661
assert_eq! ( back.refs[ 1 ] .integration, resp.refs[ 1 ] .integration) ;
1662
assert_eq! ( back.witnesses, resp.witnesses) ;
4b9d0677 Andy McCall 18 days ago ↶
1663
assert_eq! ( back.refs[ 0 ] .logged, None ) ;
028545df Andy McCall 17 days ago ↶
1664
1665
let crate :: cbor:: Value:: Map( m) = crate :: cbor:: decode( & new) .unwrap ( ) else {
1666
panic! ( )
1667
} ;
1668
assert! ( m.contains_key ( & 7 ) ) ;
750251b2 Andy McCall 1 month ago ↶
1669
assert_eq! ( id_back, [ 3 ; 16 ] ) ;
83e0677b Andy McCall 1 month ago ↶
1670
assert_eq! ( back.refs.len ( ) , 2 ) ;
1671
assert_eq! ( back.refs[ 0 ] .attestation.as_ref ( ) .unwrap ( ) .attest, att) ;
1672
assert_eq! ( back.refs[ 1 ] .last_serial, None ) ;
1673
assert_eq! ( back.policy.as_ref ( ) .unwrap ( ) .policy, policy) ;
28fc23d7 Andy McCall 1 month ago ↶
1674
assert_eq! ( back.history, resp.history) ;
132b884b Andy McCall 1 month ago ↶
1675
assert_eq! ( back.tags, vec! [ tag.clone ( ) ] ) ;
1ec6fdbc Andy McCall 16 days ago ↶
1676
1677
1678
1679
let crate :: cbor:: Value:: Map( m) = crate :: cbor:: decode( & new) .unwrap ( ) else {
1680
panic! ( )
1681
} ;
1682
let crate :: cbor:: Value:: Array( tags) = & m[ & 3 ] else {
1683
panic! ( )
1684
} ;
1685
let crate :: cbor:: Value:: Map( plain) = & tags[ 0 ] else {
1686
panic! ( )
1687
} ;
1688
assert_eq! ( plain.keys ( ) .copied ( ) .collect:: < Vec< _ > > ( ) , vec! [ 0 , 1 , 2 , 3 ] ) ;
1689
let mut full = tag.clone ( ) ;
1690
full.name = " v2" .into ( ) ;
1691
full.attestation = Some ( SignedAttest {
1692
attest: att.clone ( ) ,
1693
sigs: vec! [ sig.clone ( ) ] ,
1694
} ) ;
1695
full.accepted = full.attestation.clone ( ) ;
1696
full.logged = Some ( Logged {
1697
index: 7 ,
1698
entry: vec! [ 4 ] ,
1699
proof: vec! [ [ 3 ; 32 ] ] ,
1700
} ) ;
1701
full.integration = vec! [ Integration {
1702
note: " hub.example/log\n 8\n root\n \n - sig\n " .into ( ) ,
1703
proof: vec! [ ] ,
1704
} ] ;
1705
resp.tags = vec! [ full.clone ( ) , tag.clone ( ) ] ;
1706
let bytes = wire:: encode_refs( [ 3 ; 16 ] , & resp) ;
1707
assert_eq! ( wire:: decode_refs( & bytes) .unwrap ( ) .1. tags, resp.tags) ;
1708
1709
let pushed = wire:: encode_push( & Bundle:: default( ) , & [ ] , None , & resp.tags) ;
1710
assert_eq! ( wire:: decode_push( & pushed) .unwrap ( ) .3 , resp.tags) ;
1711
1712
1713
let stripped: Vec< TagRef> = resp
1714
.tags
1715
.iter ( )
1716
.map ( | t | {
1717
TagRef:: new(
1718
t.name.clone ( ) ,
1719
t.state,
1720
t.heads.clone ( ) ,
1721
t.attestation.clone ( ) ,
1722
)
1723
} )
1724
.collect ( ) ;
1725
assert_eq! ( wire:: tags_as_0_5_read_them( & bytes) .unwrap ( ) , stripped) ;
750251b2 Andy McCall 1 month ago ↶
1726
75167672 Andy McCall 1 month ago
1727
let ( h, w) = wire:: decode_negotiate( & wire:: encode_negotiate(
1728
& [ ObjectRef( [ 1 ; 32 ] ) ] ,
1729
& [ ObjectRef( [ 2 ; 32 ] ) ] ,
1730
) )
1731
.unwrap ( ) ;
750251b2 Andy McCall 1 month ago ↶
1732
assert_eq! ( ( h, w) , ( vec! [ ObjectRef( [ 1 ; 32 ] ) ] , vec! [ ObjectRef( [ 2 ; 32 ] ) ] ) ) ;
75167672 Andy McCall 1 month ago
1733
assert_eq! (
1734
wire:: decode_hashes( & wire:: encode_hashes( & [ c.hash ( ) ] ) ) .unwrap ( ) ,
1735
vec! [ c.hash ( ) ]
1736
) ;
1737
assert_eq! (
1738
wire:: decode_fetch( & wire:: encode_fetch( & [ c.hash ( ) ] , Some ( 4096 ) ) ) .unwrap ( ) ,
1739
( vec! [ c.hash ( ) ] , Some ( 4096 ) )
1740
) ;
1741
assert_eq! (
1742
wire:: decode_fetch( & wire:: encode_fetch( & [ ] , None ) ) .unwrap ( ) ,
1743
( vec! [ ] , None )
1744
) ;
1745
assert_eq! (
1746
wire:: decode_shallow( & wire:: encode_shallow( & [ c.hash ( ) ] , 3 ) ) .unwrap ( ) ,
1747
( vec! [ c.hash ( ) ] , 3 )
1748
) ;
b3c5a59a Andy McCall 1 month ago ↶
1749
75167672 Andy McCall 1 month ago
1750
assert_eq! (
1751
wire:: decode_fetch( & wire:: encode_hashes( & [ c.hash ( ) ] ) ) .unwrap ( ) ,
1752
( vec! [ c.hash ( ) ] , None )
1753
) ;
750251b2 Andy McCall 1 month ago ↶
1754
75167672 Andy McCall 1 month ago
1755
let up = RefUpdate {
1756
name: " main" .into ( ) ,
1757
old_state: ObjectRef( [ 1 ; 32 ] ) ,
1758
new_state: ObjectRef( [ 2 ; 32 ] ) ,
1759
force: true ,
1760
attestation: Some ( SignedAttest {
1761
attest: att,
1762
sigs: vec! [ sig] ,
1763
} ) ,
1764
} ;
1765
let sp = SignedPolicy {
1766
policy,
1767
sigs: vec! [ psig] ,
1768
} ;
1769
let ( b2, ups, pol, tags) = wire:: decode_push( & wire:: encode_push(
1770
& bundle,
1771
std:: slice:: from_ref( & up) ,
1772
Some ( & sp) ,
1773
std:: slice:: from_ref( & tag) ,
1774
) )
1775
.unwrap ( ) ;
83e0677b Andy McCall 1 month ago ↶
1776
assert_eq! ( pol, Some ( sp) ) ;
132b884b Andy McCall 1 month ago ↶
1777
assert_eq! ( tags, vec! [ tag] ) ;
750251b2 Andy McCall 1 month ago ↶
1778
assert_eq! ( b2.changes.len ( ) , 1 ) ;
1779
assert_eq! ( ups[ 0 ] .name, " main" ) ;
1780
assert! ( ups[ 0 ] .force) ;
1781
assert_eq! ( ups[ 0 ] .attestation, up.attestation) ;
75167672 Andy McCall 1 month ago
1782
let res = vec! [ RefResult {
1783
name: " main" .into ( ) ,
1784
ok: false ,
1785
reason: " stale" .into ( ) ,
1786
} ] ;
1787
assert_eq! (
1788
wire:: decode_push_response( & wire:: encode_push_response( & res) ) .unwrap ( ) ,
1789
res
1790
) ;
1791
assert_eq! (
1792
wire:: decode_capabilities( & wire:: encode_capabilities( [ 7 ; 16 ] ) ) .unwrap ( ) ,
1793
[ 7 ; 16 ]
1794
) ;
750251b2 Andy McCall 1 month ago ↶
1795
}
1796
}