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