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} ;
17
use std:: sync:: Arc;
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> ,
83e0677b Andy McCall 1 month ago ↶
57
}
58
132b884b Andy McCall 1 month ago ↶
59
60
# [ derive ( Clone, Debug, PartialEq, Eq ) ]
61
pub struct TagRef {
62
pub name : String,
63
pub state : ObjectRef,
64
pub heads : Vec< ChangeHash> ,
65
pub attestation : Option< SignedAttest> ,
66
}
67
5b9c6ad4 Andy McCall 1 month ago ↶
68
69
# [ derive ( Clone, Debug ) ]
70
pub struct RemoteRef {
71
pub name : String,
72
pub state : ObjectRef,
73
pub heads : Vec< ChangeHash> ,
74
pub attestation : Option< SignedAttest> ,
75
}
76
77
78
# [ derive ( Clone, Debug, Default ) ]
79
pub struct Bundle {
80
81
pub changes : Vec< ( Change, Vec< Signature> ) > ,
82
83
pub attests : Vec< SignedAttest> ,
84
85
pub objects : Vec< ( ObjectType, Vec< u8 > ) > ,
28fc23d7 Andy McCall 1 month ago ↶
86
87
88
pub policies : Vec< SignedPolicy> ,
598009a7 Andy McCall 1 month ago ↶
89
90
91
92
pub hashes : Vec< ChangeHash> ,
2c75672f Andy McCall 1 month ago ↶
93
94
95
96
pub raw_changes : Vec< ( Vec< u8 > , Vec< Signature> ) > ,
5b9c6ad4 Andy McCall 1 month ago ↶
97
}
98
99
impl Bundle {
100
pub fn is_empty ( & self ) -> bool {
101
self .changes.is_empty ( ) & & self .attests.is_empty ( ) & & self .objects.is_empty ( )
102
}
103
}
104
105
106
# [ derive ( Clone, Debug ) ]
107
pub struct RefUpdate {
108
pub name : String,
109
pub old_state : ObjectRef,
110
pub new_state : ObjectRef,
111
pub force : bool ,
112
pub attestation : Option< SignedAttest> ,
113
}
114
115
# [ derive ( Clone, Debug, PartialEq, Eq ) ]
116
pub struct RefResult {
117
pub name : String,
118
pub ok : bool ,
119
pub reason : String,
120
}
121
122
pub trait Remote {
123
fn repo_id ( & self ) -> Result< [ u8 ; 16 ] > ;
83e0677b Andy McCall 1 month ago ↶
124
fn refs ( & self ) -> Result< RefsResponse> ;
5b9c6ad4 Andy McCall 1 month ago ↶
125
126
fn negotiate ( & self , have : & [ChangeHash], want : & [ChangeHash]) -> Result< Vec< ChangeHash> > ;
b3c5a59a Andy McCall 1 month ago ↶
127
128
129
fn fetch ( & self , changes : & [ChangeHash], max_blob : Option< u64 > ) -> Result< Bundle> ;
130
131
132
fn objects ( & self , refs : & [ObjectRef]) -> Result< Vec< ( ObjectType, Vec< u8 > ) > > ;
2863456a Andy McCall 1 month ago ↶
133
134
135
fn checkpoint ( & self , heads : & [ChangeHash]) -> Result< Bundle> ;
136
137
fn shallow ( & self , heads : & [ChangeHash], depth : u32 ) -> Result< Vec< ChangeHash> > ;
132b884b Andy McCall 1 month ago ↶
138
139
140
75167672 Andy McCall 1 month ago
141
fn push (
142
& self ,
143
bundle : Bundle,
144
updates : Vec< RefUpdate> ,
145
policy : Option< SignedPolicy> ,
146
tags : Vec< TagRef> ,
147
) -> Result< Vec< RefResult> > ;
5b9c6ad4 Andy McCall 1 month ago ↶
148
}
149
750251b2 Andy McCall 1 month ago ↶
150
151
5b9c6ad4 Andy McCall 1 month ago ↶
152
153
pub struct LocalRemote {
154
pub repo : Repo,
84b9e9ac Andy McCall 1 month ago ↶
155
156
157
pub claimed_id : Option< [ u8 ; 16 ] > ,
5b9c6ad4 Andy McCall 1 month ago ↶
158
}
159
160
impl LocalRemote {
161
pub fn open ( path : & Path) -> Result< LocalRemote> {
75167672 Andy McCall 1 month ago
162
Ok ( LocalRemote {
163
repo: Repo:: open( path) ? ,
84b9e9ac Andy McCall 1 month ago ↶
164
claimed_id: None ,
75167672 Andy McCall 1 month ago
165
} )
5b9c6ad4 Andy McCall 1 month ago ↶
166
}
167
}
168
169
impl Remote for LocalRemote {
170
fn repo_id ( & self ) -> Result< [ u8 ; 16 ] > {
171
self .repo.id ( )
172
}
83e0677b Andy McCall 1 month ago ↶
173
fn refs ( & self ) -> Result< RefsResponse> {
5b9c6ad4 Andy McCall 1 month ago ↶
174
self .repo.remote_refs ( )
175
}
176
fn negotiate ( & self , have : & [ChangeHash], want : & [ChangeHash]) -> Result< Vec< ChangeHash> > {
177
self .repo.negotiate ( have, want)
178
}
b3c5a59a Andy McCall 1 month ago ↶
179
fn fetch ( & self , changes : & [ChangeHash], max_blob : Option< u64 > ) -> Result< Bundle> {
2c75672f Andy McCall 1 month ago ↶
180
self .repo.bundle_raw_for ( changes, max_blob)
b3c5a59a Andy McCall 1 month ago ↶
181
}
182
fn objects ( & self , refs : & [ObjectRef]) -> Result< Vec< ( ObjectType, Vec< u8 > ) > > {
183
self .repo.objects_for ( refs)
5b9c6ad4 Andy McCall 1 month ago ↶
184
}
2863456a Andy McCall 1 month ago ↶
185
fn checkpoint ( & self , heads : & [ChangeHash]) -> Result< Bundle> {
186
self .repo.checkpoint_bundle_for ( heads)
187
}
188
fn shallow ( & self , heads : & [ChangeHash], depth : u32 ) -> Result< Vec< ChangeHash> > {
189
self .repo.shallow_changes ( heads, depth)
190
}
75167672 Andy McCall 1 month ago
191
fn push (
192
& self ,
193
bundle : Bundle,
194
updates : Vec< RefUpdate> ,
195
policy : Option< SignedPolicy> ,
196
tags : Vec< TagRef> ,
197
) -> Result< Vec< RefResult> > {
84b9e9ac Andy McCall 1 month ago ↶
198
if self .repo.is_unbound ( ) {
199
match self .claimed_id {
200
Some ( id) => self .repo.bind_id ( id) ? ,
eae59556 Andy McCall 1 month ago ↶
201
None => {
202
return Err ( RepoError:: Msg(
203
" the remote is unbound and this client did not say which repository it is"
204
.into ( ) ,
205
) )
206
}
84b9e9ac Andy McCall 1 month ago ↶
207
}
208
}
132b884b Andy McCall 1 month ago ↶
209
self .repo.receive_push ( bundle, updates, policy, tags)
5b9c6ad4 Andy McCall 1 month ago ↶
210
}
211
}
212
750251b2 Andy McCall 1 month ago ↶
213
214
2b5ee371 Andy McCall 1 month ago ↶
215
216
# [ derive ( Clone, Debug, Default ) ]
217
pub struct RemoteOptions {
218
219
pub token : Option< String> ,
220
221
222
pub ca : Option< PathBuf> ,
75167672 Andy McCall 1 month ago
223
224
225
pub repo_id : Option< [ u8 ; 16 ] > ,
2b5ee371 Andy McCall 1 month ago ↶
226
}
227
75167672 Andy McCall 1 month ago
228
229
pub const REPO_ID_HEADER : & str = " Oxy-Repo-Id" ;
230
231
232
pub const UNBOUND_ID : [ u8 ; 16 ] = [ 0 ; 16 ] ;
233
750251b2 Andy McCall 1 month ago ↶
234
235
pub struct HttpRemote {
236
base : String,
237
token : Option< String> ,
75167672 Andy McCall 1 month ago
238
repo_id : Option< [ u8 ; 16 ] > ,
750251b2 Andy McCall 1 month ago ↶
239
agent : ureq:: Agent,
240
}
241
2b5ee371 Andy McCall 1 month ago ↶
242
243
fn tls_client_config ( ca : Option< & Path> ) -> Result< Arc< rustls:: ClientConfig> > {
244
let mut roots = rustls:: RootCertStore:: empty( ) ;
245
roots.extend ( webpki_roots:: TLS_SERVER_ROOTS .iter ( ) .cloned ( ) ) ;
246
if let Some ( path) = ca {
75167672 Andy McCall 1 month ago
247
let pem = std:: fs:: read( path)
248
.map_err ( | e | RepoError:: Msg( format! ( " reading CA file {} : {e} " , path.display ( ) ) ) ) ? ;
2b5ee371 Andy McCall 1 month ago ↶
249
let certs = rustls_pemfile:: certs( & mut pem.as_slice ( ) )
250
.collect:: < std:: result:: Result< Vec< _ > , _ > > ( )
251
.map_err ( | e | RepoError:: Msg( format! ( " parsing CA file {} : {e} " , path.display ( ) ) ) ) ? ;
252
if certs.is_empty ( ) {
75167672 Andy McCall 1 month ago
253
return Err ( RepoError:: Msg( format! (
254
" CA file {} holds no certificates" ,
255
path.display ( )
256
) ) ) ;
2b5ee371 Andy McCall 1 month ago ↶
257
}
258
for c in certs {
75167672 Andy McCall 1 month ago
259
roots
260
.add ( c)
261
.map_err ( | e | RepoError:: Msg( format! ( " CA file {} : {e} " , path.display ( ) ) ) ) ? ;
2b5ee371 Andy McCall 1 month ago ↶
262
}
263
}
75167672 Andy McCall 1 month ago
264
let cfg = rustls:: ClientConfig:: builder_with_provider( Arc:: new(
265
rustls:: crypto:: ring:: default_provider( ) ,
266
) )
267
.with_safe_default_protocol_versions ( )
268
.map_err ( | e | RepoError:: Msg( e.to_string ( ) ) ) ?
269
.with_root_certificates ( roots)
270
.with_no_client_auth ( ) ;
2b5ee371 Andy McCall 1 month ago ↶
271
Ok ( Arc:: new( cfg) )
272
}
273
750251b2 Andy McCall 1 month ago ↶
274
impl HttpRemote {
2b5ee371 Andy McCall 1 month ago ↶
275
pub fn new ( base : & str , options : & RemoteOptions) -> Result< HttpRemote> {
75167672 Andy McCall 1 month ago
276
let agent = ureq:: AgentBuilder:: new( )
277
.tls_config ( tls_client_config ( options.ca.as_deref ( ) ) ? )
278
.build ( ) ;
279
Ok ( HttpRemote {
280
base: base.trim_end_matches ( ' /' ) .to_string ( ) ,
281
token: options.token.clone ( ) ,
282
repo_id: options.repo_id,
283
agent,
284
} )
750251b2 Andy McCall 1 month ago ↶
285
}
286
287
fn call ( & self , post : bool , endpoint : & str , body : Option< & [ u8 ] > ) -> Result< Vec< u8 > > {
288
let url = format! ( " {} /oxy/v1/{endpoint} " , self .base) ;
75167672 Andy McCall 1 month ago
289
let mut req = if post {
290
self .agent.post ( & url)
291
} else {
292
self .agent.get ( & url)
293
} ;
750251b2 Andy McCall 1 month ago ↶
294
req = req.set ( " Accept" , CONTENT_TYPE ) ;
295
if let Some ( t) = & self .token {
296
req = req.set ( " Authorization" , & format! ( " Bearer {t} " ) ) ;
297
}
75167672 Andy McCall 1 month ago
298
if let Some ( id) = & self .repo_id {
299
req = req.set ( REPO_ID_HEADER , & hex:: encode( id) ) ;
300
}
750251b2 Andy McCall 1 month ago ↶
301
let resp = match body {
302
Some ( b) => req.set ( " Content-Type" , CONTENT_TYPE ) .send_bytes ( b) ,
303
None => req.call ( ) ,
304
} ;
305
match resp {
306
Ok ( r) => {
307
let mut out = Vec :: new( ) ;
75167672 Andy McCall 1 month ago
308
r.into_reader ( )
309
.read_to_end ( & mut out)
310
.map_err ( | e | RepoError:: Msg( format! ( " {url} : {e} " ) ) ) ? ;
750251b2 Andy McCall 1 month ago ↶
311
Ok ( out)
312
}
313
Err ( ureq:: Error:: Status( code, r) ) => {
314
let text = r.into_string ( ) .unwrap_or_default ( ) ;
3e5772cc Andy McCall 1 month ago ↶
315
let msg = format! ( " {url} : HTTP {code} : {} " , text.trim ( ) ) ;
316
Err ( RepoError:: Msg( with_hint ( msg) ) )
750251b2 Andy McCall 1 month ago ↶
317
}
3e5772cc Andy McCall 1 month ago ↶
318
319
Err ( e) => Err ( RepoError:: Msg( with_hint ( e.to_string ( ) ) ) ) ,
750251b2 Andy McCall 1 month ago ↶
320
}
321
}
322
}
323
3e5772cc Andy McCall 1 month ago ↶
324
325
pub fn hint_for ( message : & str ) -> Option< & 'static str > {
326
let m = message.to_ascii_lowercase ( ) ;
327
let hint = if m.contains ( " http 401" ) {
328
" the server did not accept the token: set one with `oxy config remote.<remote>.token TOKEN` (a personal access token; pushing needs repo:write)"
329
} else if m.contains ( " http 403" ) {
330
" the token or account lacks permission: pushing needs a token with repo:write and write access to the repository"
331
} else if m.contains ( " http 404" ) {
332
" nothing at this URL: check the owner and repository name, or create the repository on the hub first"
333
} else if m.contains ( " unknownissuer" )
334
| | m.contains ( " unknown issuer" )
335
| | m.contains ( " certificate" )
336
| | m.contains ( " invalidcertificate" )
337
{
338
" 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`"
339
} else if m.contains ( " connection refused" ) {
340
" nothing is listening there: check the URL and port, and that the server is running"
341
} else if m.contains ( " dns" )
342
| | m.contains ( " failed to lookup" )
343
| | m.contains ( " name or service not known" )
344
{
345
" the host name does not resolve: check the URL"
346
} else if m.contains ( " timed out" ) | | m.contains ( " timeout" ) {
347
" the server did not answer in time: check the network, or try again"
348
} else {
349
return None ;
350
} ;
351
Some ( hint)
352
}
353
354
355
pub fn with_hint ( message : String) -> String {
356
match hint_for ( & message) {
357
Some ( h) => format! ( " {message} \n hint: {h} " ) ,
358
None => message,
359
}
360
}
361
d82e4409 Andy McCall 1 month ago ↶
362
363
364
pub fn http_get ( url : & str , options : & RemoteOptions) -> Result< Vec< u8 > > {
365
let agent = ureq:: AgentBuilder:: new( )
366
.tls_config ( tls_client_config ( options.ca.as_deref ( ) ) ? )
367
.build ( ) ;
368
let mut req = agent.get ( url) .set ( " Accept" , " application/json" ) ;
369
if let Some ( t) = & options.token {
370
req = req.set ( " Authorization" , & format! ( " Bearer {t} " ) ) ;
371
}
372
match req.call ( ) {
373
Ok ( r) => {
374
let mut out = Vec :: new( ) ;
375
r.into_reader ( )
376
.read_to_end ( & mut out)
377
.map_err ( | e | RepoError:: Msg( format! ( " {url} : {e} " ) ) ) ? ;
378
Ok ( out)
379
}
380
Err ( ureq:: Error:: Status( code, r) ) => {
381
let text = r.into_string ( ) .unwrap_or_default ( ) ;
3e5772cc Andy McCall 1 month ago ↶
382
Err ( RepoError:: Msg( with_hint ( format! (
4213c41d Andy McCall 1 month ago ↶
383
" {url} : HTTP {code} : {} " ,
384
text.trim ( )
3e5772cc Andy McCall 1 month ago ↶
385
) ) ) )
d82e4409 Andy McCall 1 month ago ↶
386
}
3e5772cc Andy McCall 1 month ago ↶
387
388
Err ( e) => Err ( RepoError:: Msg( with_hint ( e.to_string ( ) ) ) ) ,
d82e4409 Andy McCall 1 month ago ↶
389
}
390
}
391
750251b2 Andy McCall 1 month ago ↶
392
impl Remote for HttpRemote {
393
fn repo_id ( & self ) -> Result< [ u8 ; 16 ] > {
394
wire:: decode_capabilities( & self .call ( false , " capabilities" , None ) ? )
395
}
83e0677b Andy McCall 1 month ago ↶
396
fn refs ( & self ) -> Result< RefsResponse> {
750251b2 Andy McCall 1 month ago ↶
397
Ok ( wire:: decode_refs( & self .call ( false , " refs" , None ) ? ) ? .1 )
398
}
399
fn negotiate ( & self , have : & [ChangeHash], want : & [ChangeHash]) -> Result< Vec< ChangeHash> > {
75167672 Andy McCall 1 month ago
400
wire:: decode_hashes( & self .call (
401
true ,
402
" negotiate" ,
403
Some ( & wire:: encode_negotiate( have, want) ) ,
404
) ? )
750251b2 Andy McCall 1 month ago ↶
405
}
b3c5a59a Andy McCall 1 month ago ↶
406
fn fetch ( & self , changes : & [ChangeHash], max_blob : Option< u64 > ) -> Result< Bundle> {
75167672 Andy McCall 1 month ago
407
wire:: decode_bundle( & self .call (
408
true ,
409
" fetch" ,
410
Some ( & wire:: encode_fetch( changes, max_blob) ) ,
411
) ? )
750251b2 Andy McCall 1 month ago ↶
412
}
b3c5a59a Andy McCall 1 month ago ↶
413
fn objects ( & self , refs : & [ObjectRef]) -> Result< Vec< ( ObjectType, Vec< u8 > ) > > {
75167672 Andy McCall 1 month ago
414
Ok (
415
wire:: decode_bundle( & self .call ( true , " objects" , Some ( & wire:: encode_hashes( refs) ) ) ? ) ?
416
.objects,
417
)
b3c5a59a Andy McCall 1 month ago ↶
418
}
2863456a Andy McCall 1 month ago ↶
419
fn checkpoint ( & self , heads : & [ChangeHash]) -> Result< Bundle> {
420
wire:: decode_bundle( & self .call ( true , " checkpoint" , Some ( & wire:: encode_hashes( heads) ) ) ? )
421
}
422
fn shallow ( & self , heads : & [ChangeHash], depth : u32 ) -> Result< Vec< ChangeHash> > {
75167672 Andy McCall 1 month ago
423
wire:: decode_hashes( & self .call (
424
true ,
425
" shallow" ,
426
Some ( & wire:: encode_shallow( heads, depth) ) ,
427
) ? )
2863456a Andy McCall 1 month ago ↶
428
}
75167672 Andy McCall 1 month ago
429
fn push (
430
& self ,
431
bundle : Bundle,
432
updates : Vec< RefUpdate> ,
433
policy : Option< SignedPolicy> ,
434
tags : Vec< TagRef> ,
435
) -> Result< Vec< RefResult> > {
436
wire:: decode_push_response( & self .call (
437
true ,
438
" push" ,
439
Some ( & wire:: encode_push(
440
& bundle,
441
& updates,
442
policy.as_ref ( ) ,
443
& tags,
444
) ) ,
445
) ? )
750251b2 Andy McCall 1 month ago ↶
446
}
447
}
448
449
2b5ee371 Andy McCall 1 month ago ↶
450
pub fn open_remote ( url : & str , options : & RemoteOptions) -> Result< Box< dyn Remote> > {
750251b2 Andy McCall 1 month ago ↶
451
if url.starts_with ( " http://" ) | | url.starts_with ( " https://" ) {
2b5ee371 Andy McCall 1 month ago ↶
452
return Ok ( Box :: new( HttpRemote:: new( url, options) ? ) ) ;
750251b2 Andy McCall 1 month ago ↶
453
}
5b9c6ad4 Andy McCall 1 month ago ↶
454
let path = url.strip_prefix ( " file://" ) .unwrap_or ( url) ;
455
if path.contains ( " ://" ) {
750251b2 Andy McCall 1 month ago ↶
456
return Err ( RepoError:: Msg( format! ( " unsupported remote URL {url:?} " ) ) ) ;
5b9c6ad4 Andy McCall 1 month ago ↶
457
}
84b9e9ac Andy McCall 1 month ago ↶
458
let mut local = LocalRemote:: open( Path:: new( path) ) ? ;
459
local.claimed_id = options.repo_id;
460
Ok ( Box :: new( local) )
5b9c6ad4 Andy McCall 1 month ago ↶
461
}
750251b2 Andy McCall 1 month ago ↶
462
463
464
465
466
pub mod wire {
467
use super :: * ;
468
469
fn schema ( msg : & str ) -> RepoError {
470
RepoError:: Object( ObjectError:: Schema( msg.into ( ) ) )
471
}
472
fn sigs_value ( sigs : & [Signature]) -> Value {
473
Value:: Array( sigs.iter ( ) .map ( | s | s.to_value ( ) ) .collect ( ) )
474
}
475
fn sigs_from ( v : & Value) -> Result< Vec< Signature> > {
75167672 Andy McCall 1 month ago
476
Ok ( o:: array( v, " signatures" ) ?
477
.iter ( )
478
.map ( Signature:: from_value)
479
.collect:: < std:: result:: Result< Vec< _ > , _ > > ( ) ? )
750251b2 Andy McCall 1 month ago ↶
480
}
481
fn hashes_value ( hs : & [ObjectRef]) -> Value {
482
Value:: Array( hs.iter ( ) .map ( | h | Value:: Bytes( h.0. to_vec ( ) ) ) .collect ( ) )
483
}
484
fn hashes_from ( v : & Value) -> Result< Vec< ObjectRef> > {
75167672 Andy McCall 1 month ago
485
Ok ( o:: array( v, " hashes" ) ?
486
.iter ( )
487
.map ( | h | o:: oref( h, " hash" ) )
488
.collect:: < std:: result:: Result< Vec< _ > , _ > > ( ) ? )
750251b2 Andy McCall 1 month ago ↶
489
}
490
fn attest_value ( sa : & SignedAttest) -> Value {
491
Value:: Array( vec! [ Value:: Bytes( sa.attest.encode ( ) ) , sigs_value ( & sa.sigs) ] )
492
}
493
fn attest_from ( v : & Value) -> Result< SignedAttest> {
494
let a = o:: array( v, " signed attest" ) ? ;
495
if a.len ( ) ! = 2 {
496
return Err ( schema ( " signed attest: expected [attest, sigs]" ) ) ;
497
}
75167672 Andy McCall 1 month ago
498
Ok ( SignedAttest {
499
attest: Attest:: decode( o:: bytes( & a[ 0 ] , " attest" ) ? ) ? ,
500
sigs: sigs_from ( & a[ 1 ] ) ? ,
501
} )
750251b2 Andy McCall 1 month ago ↶
502
}
503
fn opt_attest_value ( sa : & Option< SignedAttest> ) -> Value {
504
sa.as_ref ( ) .map ( attest_value) .unwrap_or ( Value:: Null)
505
}
506
fn opt_attest_from ( v : & Value) -> Result< Option< SignedAttest> > {
75167672 Andy McCall 1 month ago
507
if v.is_null ( ) {
508
Ok ( None )
509
} else {
510
attest_from ( v) .map ( Some )
511
}
750251b2 Andy McCall 1 month ago ↶
512
}
28fc23d7 Andy McCall 1 month ago ↶
513
514
515
pub fn policy_value ( sp : & SignedPolicy) -> Value {
516
Value:: Array( vec! [ Value:: Bytes( sp.policy.encode ( ) ) , sigs_value ( & sp.sigs) ] )
83e0677b Andy McCall 1 month ago ↶
517
}
28fc23d7 Andy McCall 1 month ago ↶
518
pub fn policy_from ( v : & Value) -> Result< SignedPolicy> {
83e0677b Andy McCall 1 month ago ↶
519
let a = o:: array( v, " signed policy" ) ? ;
520
if a.len ( ) ! = 2 {
521
return Err ( schema ( " signed policy: expected [policy, sigs]" ) ) ;
522
}
75167672 Andy McCall 1 month ago
523
Ok ( SignedPolicy {
524
policy: Policy:: decode( o:: bytes( & a[ 0 ] , " policy" ) ? ) ? ,
525
sigs: sigs_from ( & a[ 1 ] ) ? ,
526
} )
28fc23d7 Andy McCall 1 month ago ↶
527
}
528
fn opt_policy_value ( sp : Option< & SignedPolicy> ) -> Value {
529
sp.map ( policy_value) .unwrap_or ( Value:: Null)
530
}
531
fn opt_policy_from ( v : & Value) -> Result< Option< SignedPolicy> > {
75167672 Andy McCall 1 month ago
532
if v.is_null ( ) {
533
Ok ( None )
534
} else {
535
policy_from ( v) .map ( Some )
536
}
83e0677b Andy McCall 1 month ago ↶
537
}
750251b2 Andy McCall 1 month ago ↶
538
fn map < 'a > ( v : & 'a Value, what : & str ) -> Result< & 'a o:: Map> {
539
Ok ( o:: map( v, what) ? )
540
}
541
fn field < 'a > ( m : & 'a o:: Map, k : u64 , what : & str ) -> Result< & 'a Value> {
542
Ok ( o:: field( m, k, what) ? )
543
}
544
6f8aeb99 Andy McCall 1 month ago ↶
545
546
547
750251b2 Andy McCall 1 month ago ↶
548
pub fn bundle_value ( b : & Bundle) -> Value {
2c75672f Andy McCall 1 month ago ↶
549
let mut changes = Vec :: with_capacity( b.raw_changes.len ( ) + b.changes.len ( ) ) ;
550
551
552
for ( bytes, s) in & b.raw_changes {
553
changes.push ( Value:: Array( vec! [
554
Value:: Bytes( bytes.clone ( ) ) ,
555
sigs_value ( s) ,
556
] ) ) ;
557
}
6f8aeb99 Andy McCall 1 month ago ↶
558
let mut texts: Vec< ( ObjectType, Vec< u8 > ) > = Vec :: new( ) ;
559
let mut listed: Option< std:: collections:: HashSet< ObjectRef> > = None ;
560
for ( c, s) in & b.changes {
561
let ( bytes, stored) = c.encode_parts ( ) ;
562
for t in stored {
563
let listed = listed.get_or_insert_with ( | | {
564
b.objects
565
.iter ( )
566
.map ( | ( ty , body ) | crate :: hash:: hash_object( * ty, body) )
567
.collect ( )
568
} ) ;
569
if ! listed.insert ( t.blob.hash ( ) ) {
570
continue ;
571
}
572
for chunk in crate :: chunk:: chunks( & t.content) {
573
if listed.insert ( crate :: hash:: hash_object( ObjectType:: Chunk, chunk) ) {
574
texts.push ( ( ObjectType:: Chunk, chunk.to_vec ( ) ) ) ;
575
}
576
}
577
texts.push ( ( ObjectType:: Blob, t.blob.encode ( ) ) ) ;
578
}
579
changes.push ( Value:: Array( vec! [ Value:: Bytes( bytes) , sigs_value ( s) ] ) ) ;
580
}
750251b2 Andy McCall 1 month ago ↶
581
Value:: map( [
6f8aeb99 Andy McCall 1 month ago ↶
582
( 0 , Value:: Array( changes) ) ,
750251b2 Andy McCall 1 month ago ↶
583
(
75167672 Andy McCall 1 month ago
584
1 ,
585
Value:: Array( b.attests.iter ( ) .map ( attest_value) .collect ( ) ) ,
586
) ,
587
(
750251b2 Andy McCall 1 month ago ↶
588
2 ,
589
Value:: Array(
75167672 Andy McCall 1 month ago
590
b.objects
591
.iter ( )
6f8aeb99 Andy McCall 1 month ago ↶
592
.chain ( & texts)
75167672 Andy McCall 1 month ago
593
.map ( | ( t , body ) | {
594
Value:: Array( vec! [
595
Value:: Text( t.name ( ) .into ( ) ) ,
596
Value:: Bytes( body.clone ( ) ) ,
597
] )
598
} )
599
.collect ( ) ,
750251b2 Andy McCall 1 month ago ↶
600
) ,
601
) ,
75167672 Andy McCall 1 month ago
602
(
603
3 ,
604
Value:: Array( b.policies.iter ( ) .map ( policy_value) .collect ( ) ) ,
605
) ,
750251b2 Andy McCall 1 month ago ↶
606
] )
607
}
608
609
pub fn bundle_from_value ( v : & Value) -> Result< Bundle> {
610
let m = map ( v, " bundle" ) ? ;
611
let mut b = Bundle:: default( ) ;
612
for ob in o:: array( field ( m, 2 , " objects" ) ? , " objects" ) ? {
613
let pair = o:: array( ob, " object" ) ? ;
614
if pair.len ( ) ! = 2 {
615
return Err ( schema ( " object: expected [type, body]" ) ) ;
616
}
75167672 Andy McCall 1 month ago
617
let ty = ObjectType:: from_name( o:: text( & pair[ 0 ] , " object type" ) ? )
618
.ok_or_else ( | | schema ( " unknown object type" ) ) ? ;
619
b.objects
620
.push ( ( ty, o:: bytes( & pair[ 1 ] , " object body" ) ? .to_vec ( ) ) ) ;
6f8aeb99 Andy McCall 1 month ago ↶
621
}
622
623
let mut by_ref: Option< std:: collections:: HashMap< ObjectRef, usize > > = None ;
624
for c in o:: array( field ( m, 0 , " changes" ) ? , " changes" ) ? {
625
let pair = o:: array( c, " change entry" ) ? ;
626
if pair.len ( ) ! = 2 {
627
return Err ( schema ( " change entry: expected [change, sigs]" ) ) ;
628
}
598009a7 Andy McCall 1 month ago ↶
629
let encoded = o:: bytes( & pair[ 0 ] , " change" ) ? ;
630
b.hashes
631
.push ( crate :: hash:: hash_object( ObjectType:: Change, encoded) ) ;
632
let change = Change:: decode_with( encoded, & mut | text, count| {
6f8aeb99 Andy McCall 1 month ago ↶
633
let by_ref = by_ref.get_or_insert_with ( | | {
634
b.objects
635
.iter ( )
636
.enumerate ( )
637
.map ( | ( i , (ty , body ) ) | ( crate :: hash:: hash_object( * ty, body) , i) )
638
.collect ( )
639
} ) ;
640
let get =
641
| r : & ObjectRef, want : ObjectType| match by_ref.get ( r) .map ( | i | & b.objects[ * i] ) {
642
Some ( ( ty, body) ) if * ty = = want => Ok ( body) ,
643
_ => Err ( ObjectError:: Schema( format! (
644
" the bundle lacks {} {} " ,
645
want.name ( ) ,
646
r.short ( 12 )
647
) ) ) ,
648
} ;
649
let blob = crate :: objects:: Blob:: decode( get ( text, ObjectType:: Blob) ? ) ? ;
650
let mut content = Vec :: with_capacity( blob.length as usize ) ;
651
for chunk in & blob.chunks {
652
content.extend_from_slice ( get ( chunk, ObjectType:: Chunk) ? ) ;
653
}
654
crate :: objects:: text_lines( & content, count)
655
} ) ? ;
656
b.changes.push ( ( change, sigs_from ( & pair[ 1 ] ) ? ) ) ;
657
}
658
for a in o:: array( field ( m, 1 , " attests" ) ? , " attests" ) ? {
659
b.attests.push ( attest_from ( a) ? ) ;
750251b2 Andy McCall 1 month ago ↶
660
}
28fc23d7 Andy McCall 1 month ago ↶
661
if let Some ( ps) = m.get ( & 3 ) {
662
for p in o:: array( ps, " policies" ) ? {
663
b.policies.push ( policy_from ( p) ? ) ;
664
}
665
}
750251b2 Andy McCall 1 month ago ↶
666
Ok ( b)
667
}
668
669
pub fn encode_bundle ( b : & Bundle) -> Vec< u8 > {
670
cbor:: encode( & bundle_value ( b) )
671
}
672
pub fn decode_bundle ( bytes : & [u8 ]) -> Result< Bundle> {
673
bundle_from_value ( & cbor:: decode( bytes) .map_err ( ObjectError:: from) ? )
674
}
675
676
pub fn encode_capabilities ( id : [u8 ; 16]) -> Vec< u8 > {
677
cbor:: encode( & Value:: map( [
678
( 0 , Value:: Array( vec! [ Value:: Uint( PROTOCOL_VERSION ) ] ) ) ,
679
( 1 , Value:: Bytes( id.to_vec ( ) ) ) ,
680
( 2 , Value:: Array( vec! [ Value:: Text( " seal" .into ( ) ) ] ) ) ,
681
] ) )
682
}
683
pub fn decode_capabilities ( bytes : & [u8 ]) -> Result< [ u8 ; 16 ] > {
684
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
685
let m = map ( & v, " capabilities" ) ? ;
686
let versions = o:: array( field ( m, 0 , " versions" ) ? , " versions" ) ? ;
75167672 Andy McCall 1 month ago
687
if ! versions
688
.iter ( )
689
.any ( | x | x.as_uint ( ) = = Some ( PROTOCOL_VERSION ) )
690
{
691
return Err ( RepoError:: Msg(
692
" remote does not speak protocol version 1" .into ( ) ,
693
) ) ;
750251b2 Andy McCall 1 month ago ↶
694
}
75167672 Andy McCall 1 month ago
695
o:: bytes( field ( m, 1 , " repo id" ) ? , " repo id" ) ?
696
.try_into ( )
697
.map_err ( | _| schema ( " repo id: expected 16 bytes" ) )
750251b2 Andy McCall 1 month ago ↶
698
}
699
132b884b Andy McCall 1 month ago ↶
700
fn tags_value ( tags : & [TagRef]) -> Value {
701
Value:: Array(
702
tags.iter ( )
703
.map ( | t | {
704
Value:: map( [
705
( 0 , Value:: Text( t.name.clone ( ) ) ) ,
706
( 1 , Value:: Bytes( t.state.0. to_vec ( ) ) ) ,
707
( 2 , hashes_value ( & t.heads) ) ,
708
( 3 , opt_attest_value ( & t.attestation) ) ,
709
] )
710
} )
711
.collect ( ) ,
712
)
713
}
714
fn tags_from ( v : & Value) -> Result< Vec< TagRef> > {
715
let mut out = Vec :: new( ) ;
716
for t in o:: array( v, " tags" ) ? {
717
let tm = map ( t, " tag" ) ? ;
718
out.push ( TagRef {
719
name: o:: text( field ( tm, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
720
state: o:: oref( field ( tm, 1 , " state" ) ? , " state" ) ? ,
721
heads: hashes_from ( field ( tm, 2 , " heads" ) ? ) ? ,
722
attestation: opt_attest_from ( field ( tm, 3 , " attestation" ) ? ) ? ,
723
} ) ;
724
}
725
Ok ( out)
726
}
727
83e0677b Andy McCall 1 month ago ↶
728
pub fn encode_refs ( id : [u8 ; 16], resp : & RefsResponse) -> Vec< u8 > {
750251b2 Andy McCall 1 month ago ↶
729
cbor:: encode( & Value:: map( [
730
(
731
0 ,
732
Value:: Array(
83e0677b Andy McCall 1 month ago ↶
733
resp.refs
734
.iter ( )
750251b2 Andy McCall 1 month ago ↶
735
.map ( | r | {
736
Value:: map( [
737
( 0 , Value:: Text( r.name.clone ( ) ) ) ,
738
( 1 , Value:: Bytes( r.state.0. to_vec ( ) ) ) ,
739
( 2 , hashes_value ( & r.heads) ) ,
740
( 3 , opt_attest_value ( & r.attestation) ) ,
741
( 4 , r.last_serial.map ( Value:: Uint) .unwrap_or ( Value:: Null) ) ,
742
] )
743
} )
744
.collect ( ) ,
745
) ,
746
) ,
747
( 1 , Value:: Bytes( id.to_vec ( ) ) ) ,
83e0677b Andy McCall 1 month ago ↶
748
( 2 , opt_policy_value ( resp.policy.as_ref ( ) ) ) ,
132b884b Andy McCall 1 month ago ↶
749
( 3 , tags_value ( & resp.tags) ) ,
75167672 Andy McCall 1 month ago
750
(
751
4 ,
752
Value:: Array( resp.history.iter ( ) .map ( policy_value) .collect ( ) ) ,
753
) ,
750251b2 Andy McCall 1 month ago ↶
754
] ) )
755
}
83e0677b Andy McCall 1 month ago ↶
756
pub fn decode_refs ( bytes : & [u8 ]) -> Result< ( [u8 ; 16], RefsResponse) > {
750251b2 Andy McCall 1 month ago ↶
757
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
758
let m = map ( & v, " refs" ) ? ;
75167672 Andy McCall 1 month ago
759
let id: [ u8 ; 16 ] = o:: bytes( field ( m, 1 , " repo id" ) ? , " repo id" ) ?
760
.try_into ( )
761
.map_err ( | _| schema ( " repo id: expected 16 bytes" ) ) ? ;
750251b2 Andy McCall 1 month ago ↶
762
let mut refs = Vec :: new( ) ;
763
for r in o:: array( field ( m, 0 , " refs" ) ? , " refs" ) ? {
764
let rm = map ( r, " ref" ) ? ;
765
let ls = field ( rm, 4 , " last serial" ) ? ;
766
refs.push ( RemoteRef {
767
name: o:: text( field ( rm, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
768
state: o:: oref( field ( rm, 1 , " state" ) ? , " state" ) ? ,
769
heads: hashes_from ( field ( rm, 2 , " heads" ) ? ) ? ,
770
attestation: opt_attest_from ( field ( rm, 3 , " attestation" ) ? ) ? ,
75167672 Andy McCall 1 month ago
771
last_serial: if ls.is_null ( ) {
772
None
773
} else {
774
Some ( o:: uint( ls, " last serial" ) ? )
775
} ,
750251b2 Andy McCall 1 month ago ↶
776
} ) ;
777
}
83e0677b Andy McCall 1 month ago ↶
778
let policy = match m.get ( & 2 ) {
779
Some ( p) => opt_policy_from ( p) ? ,
780
None => None ,
781
} ;
132b884b Andy McCall 1 month ago ↶
782
let tags = match m.get ( & 3 ) {
783
Some ( t) => tags_from ( t) ? ,
784
None => Vec :: new( ) ,
785
} ;
28fc23d7 Andy McCall 1 month ago ↶
786
let mut history = Vec :: new( ) ;
787
if let Some ( h) = m.get ( & 4 ) {
788
for p in o:: array( h, " history" ) ? {
789
history.push ( policy_from ( p) ? ) ;
790
}
791
}
75167672 Andy McCall 1 month ago
792
Ok ( (
793
id,
794
RefsResponse {
795
refs,
796
policy,
797
tags,
798
history,
799
} ,
800
) )
750251b2 Andy McCall 1 month ago ↶
801
}
802
803
pub fn encode_negotiate ( have : & [ChangeHash], want : & [ChangeHash]) -> Vec< u8 > {
75167672 Andy McCall 1 month ago
804
cbor:: encode( & Value:: map( [
805
( 0 , hashes_value ( have) ) ,
806
( 1 , hashes_value ( want) ) ,
807
] ) )
750251b2 Andy McCall 1 month ago ↶
808
}
809
pub fn decode_negotiate ( bytes : & [u8 ]) -> Result< ( Vec< ChangeHash> , Vec< ChangeHash> ) > {
810
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
811
let m = map ( & v, " negotiate" ) ? ;
75167672 Andy McCall 1 month ago
812
Ok ( (
813
hashes_from ( field ( m, 0 , " have" ) ? ) ? ,
814
hashes_from ( field ( m, 1 , " want" ) ? ) ? ,
815
) )
750251b2 Andy McCall 1 month ago ↶
816
}
817
b3c5a59a Andy McCall 1 month ago ↶
818
819
pub fn encode_fetch ( hs : & [ChangeHash], max_blob : Option< u64 > ) -> Vec< u8 > {
75167672 Andy McCall 1 month ago
820
cbor:: encode( & Value:: map( [
821
( 0 , hashes_value ( hs) ) ,
822
( 1 , max_blob.map ( Value:: Uint) .unwrap_or ( Value:: Null) ) ,
823
] ) )
b3c5a59a Andy McCall 1 month ago ↶
824
}
825
pub fn decode_fetch ( bytes : & [u8 ]) -> Result< ( Vec< ChangeHash> , Option< u64 > ) > {
826
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
827
let m = map ( & v, " fetch" ) ? ;
828
let max = match m.get ( & 1 ) {
829
Some ( x) if ! x.is_null ( ) => Some ( o:: uint( x, " max blob" ) ? ) ,
830
_ => None ,
831
} ;
832
Ok ( ( hashes_from ( field ( m, 0 , " changes" ) ? ) ? , max) )
833
}
834
2863456a Andy McCall 1 month ago ↶
835
836
pub fn encode_shallow ( hs : & [ChangeHash], depth : u32 ) -> Vec< u8 > {
75167672 Andy McCall 1 month ago
837
cbor:: encode( & Value:: map( [
838
( 0 , hashes_value ( hs) ) ,
839
( 1 , Value:: Uint( depth as u64 ) ) ,
840
] ) )
2863456a Andy McCall 1 month ago ↶
841
}
842
pub fn decode_shallow ( bytes : & [u8 ]) -> Result< ( Vec< ChangeHash> , u32 ) > {
843
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
844
let m = map ( & v, " shallow" ) ? ;
75167672 Andy McCall 1 month ago
845
Ok ( (
846
hashes_from ( field ( m, 0 , " heads" ) ? ) ? ,
847
o:: u32_( field ( m, 1 , " depth" ) ? , " depth" ) ? ,
848
) )
2863456a Andy McCall 1 month ago ↶
849
}
850
750251b2 Andy McCall 1 month ago ↶
851
pub fn encode_hashes ( hs : & [ChangeHash]) -> Vec< u8 > {
852
cbor:: encode( & Value:: map( [ ( 0 , hashes_value ( hs) ) ] ) )
853
}
854
pub fn decode_hashes ( bytes : & [u8 ]) -> Result< Vec< ChangeHash> > {
855
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
856
hashes_from ( field ( map ( & v, " hashes" ) ? , 0 , " hashes" ) ? )
857
}
858
75167672 Andy McCall 1 month ago
859
pub fn encode_push (
860
bundle : & Bundle,
861
updates : & [RefUpdate],
862
policy : Option< & SignedPolicy> ,
863
tags : & [TagRef],
864
) -> Vec< u8 > {
750251b2 Andy McCall 1 month ago ↶
865
cbor:: encode( & Value:: map( [
866
( 0 , bundle_value ( bundle) ) ,
83e0677b Andy McCall 1 month ago ↶
867
( 2 , opt_policy_value ( policy) ) ,
132b884b Andy McCall 1 month ago ↶
868
( 3 , tags_value ( tags) ) ,
750251b2 Andy McCall 1 month ago ↶
869
(
870
1 ,
871
Value:: Array(
872
updates
873
.iter ( )
874
.map ( | u | {
875
Value:: map( [
876
( 0 , Value:: Text( u.name.clone ( ) ) ) ,
877
( 1 , Value:: Bytes( u.old_state.0. to_vec ( ) ) ) ,
878
( 2 , Value:: Bytes( u.new_state.0. to_vec ( ) ) ) ,
879
( 3 , Value:: Bool( u.force) ) ,
880
( 4 , opt_attest_value ( & u.attestation) ) ,
881
] )
882
} )
883
.collect ( ) ,
884
) ,
885
) ,
886
] ) )
887
}
132b884b Andy McCall 1 month ago ↶
888
889
pub type PushParts = ( Bundle, Vec< RefUpdate> , Option< SignedPolicy> , Vec< TagRef> ) ;
890
891
pub fn decode_push ( bytes : & [u8 ]) -> Result< PushParts> {
750251b2 Andy McCall 1 month ago ↶
892
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
893
let m = map ( & v, " push" ) ? ;
894
let bundle = bundle_from_value ( field ( m, 0 , " bundle" ) ? ) ? ;
895
let mut updates = Vec :: new( ) ;
896
for u in o:: array( field ( m, 1 , " updates" ) ? , " updates" ) ? {
897
let um = map ( u, " update" ) ? ;
898
updates.push ( RefUpdate {
899
name: o:: text( field ( um, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
900
old_state: o:: oref( field ( um, 1 , " old_state" ) ? , " old_state" ) ? ,
901
new_state: o:: oref( field ( um, 2 , " new_state" ) ? , " new_state" ) ? ,
75167672 Andy McCall 1 month ago
902
force: field ( um, 3 , " force" ) ?
903
.as_bool ( )
904
.ok_or_else ( | | schema ( " force: expected bool" ) ) ? ,
750251b2 Andy McCall 1 month ago ↶
905
attestation: opt_attest_from ( field ( um, 4 , " attestation" ) ? ) ? ,
906
} ) ;
907
}
83e0677b Andy McCall 1 month ago ↶
908
let policy = match m.get ( & 2 ) {
909
Some ( p) => opt_policy_from ( p) ? ,
910
None => None ,
911
} ;
132b884b Andy McCall 1 month ago ↶
912
let tags = match m.get ( & 3 ) {
913
Some ( t) => tags_from ( t) ? ,
914
None => Vec :: new( ) ,
915
} ;
916
Ok ( ( bundle, updates, policy, tags) )
750251b2 Andy McCall 1 month ago ↶
917
}
918
919
pub fn encode_push_response ( results : & [RefResult]) -> Vec< u8 > {
920
cbor:: encode( & Value:: map( [ (
921
0 ,
922
Value:: Array(
923
results
924
.iter ( )
75167672 Andy McCall 1 month ago
925
.map ( | r | {
926
Value:: map( [
927
( 0 , Value:: Text( r.name.clone ( ) ) ) ,
928
( 1 , Value:: Bool( r.ok) ) ,
929
( 2 , Value:: Text( r.reason.clone ( ) ) ) ,
930
] )
931
} )
750251b2 Andy McCall 1 month ago ↶
932
.collect ( ) ,
933
) ,
934
) ] ) )
935
}
936
pub fn decode_push_response ( bytes : & [u8 ]) -> Result< Vec< RefResult> > {
937
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
938
let m = map ( & v, " push response" ) ? ;
939
let mut out = Vec :: new( ) ;
940
for r in o:: array( field ( m, 0 , " results" ) ? , " results" ) ? {
941
let rm = map ( r, " result" ) ? ;
942
out.push ( RefResult {
943
name: o:: text( field ( rm, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
75167672 Andy McCall 1 month ago
944
ok: field ( rm, 1 , " ok" ) ?
945
.as_bool ( )
946
.ok_or_else ( | | schema ( " ok: expected bool" ) ) ? ,
750251b2 Andy McCall 1 month ago ↶
947
reason: o:: text( field ( rm, 2 , " reason" ) ? , " reason" ) ? .to_owned ( ) ,
948
} ) ;
949
}
950
Ok ( out)
951
}
952
}
953
954
# [ cfg ( test ) ]
955
mod tests {
956
use super :: * ;
957
use crate :: attest:: AttestBody;
958
use crate :: objects:: Hlc;
959
960
# [ test ]
961
fn wire_round_trips ( ) {
962
let c = crate :: objects:: tests:: sample_change( ) ;
75167672 Andy McCall 1 month ago
963
let sk = crate :: identity:: SecretKey {
964
ed_seed: [ 1 ; 32 ] ,
965
ml_seed: [ 2 ; 32 ] ,
966
} ;
750251b2 Andy McCall 1 month ago ↶
967
let id = sk.identity ( " a" , " b" , 0 ) ;
968
let key = id.hash ( ) ;
969
let sig = sk.sign ( crate :: identity:: Domain:: Change, & key, & c.hash ( ) ) ;
970
let seal = Attest:: seal( [ 3 ; 16 ] , vec! [ c.hash ( ) ] ) ;
971
let seal_sig = sk.sign ( crate :: identity:: Domain:: Attest, & key, & seal.hash ( ) ) ;
28fc23d7 Andy McCall 1 month ago ↶
972
let policy = Policy:: single_root( [ 3 ; 16 ] , id.clone ( ) , 99 ) ;
973
let psig = sk.sign ( crate :: identity:: Domain:: Policy, & key, & policy.hash ( ) ) ;
750251b2 Andy McCall 1 month ago ↶
974
let bundle = Bundle {
975
changes: vec! [ ( c.clone ( ) , vec! [ sig.clone ( ) ] ) ] ,
75167672 Andy McCall 1 month ago
976
attests: vec! [ SignedAttest {
977
attest: seal.clone ( ) ,
978
sigs: vec! [ seal_sig] ,
979
} ] ,
980
objects: vec! [
981
( ObjectType:: Identity, id.encode ( ) ) ,
982
( ObjectType:: Chunk, b " xyz" .to_vec ( ) ) ,
983
] ,
984
policies: vec! [ SignedPolicy {
985
policy: policy.clone ( ) ,
986
sigs: vec! [ psig.clone ( ) ] ,
987
} ] ,
598009a7 Andy McCall 1 month ago ↶
988
hashes: vec! [ ] ,
2c75672f Andy McCall 1 month ago ↶
989
raw_changes: vec! [ ] ,
750251b2 Andy McCall 1 month ago ↶
990
} ;
991
let d = wire:: decode_bundle( & wire:: encode_bundle( & bundle) ) .unwrap ( ) ;
598009a7 Andy McCall 1 month ago ↶
992
assert_eq! (
993
d.hashes,
994
vec! [ c.hash ( ) ] ,
995
" the receiver hashes what it was sent"
996
) ;
750251b2 Andy McCall 1 month ago ↶
997
assert_eq! ( d.changes, bundle.changes) ;
998
assert_eq! ( d.attests, bundle.attests) ;
999
assert_eq! ( d.objects, bundle.objects) ;
28fc23d7 Andy McCall 1 month ago ↶
1000
assert_eq! ( d.policies, bundle.policies) ;
750251b2 Andy McCall 1 month ago ↶
1001
75167672 Andy McCall 1 month ago
1002
let att = Attest {
1003
repo: [ 3 ; 16 ] ,
1004
hlc: Hlc:: default( ) ,
1005
body: AttestBody:: Ref {
1006
name: " main" .into ( ) ,
1007
old_state: ObjectRef( [ 1 ; 32 ] ) ,
1008
new_state: ObjectRef( [ 2 ; 32 ] ) ,
1009
serial: 4 ,
1010
} ,
1011
} ;
750251b2 Andy McCall 1 month ago ↶
1012
let refs = vec! [
75167672 Andy McCall 1 month ago
1013
RemoteRef {
1014
name: " main" .into ( ) ,
1015
state: ObjectRef( [ 2 ; 32 ] ) ,
1016
heads: vec! [ c.hash ( ) ] ,
1017
attestation: Some ( SignedAttest {
1018
attest: att.clone ( ) ,
1019
sigs: vec! [ sig.clone ( ) ] ,
1020
} ) ,
1021
last_serial: Some ( 4 ) ,
1022
} ,
1023
RemoteRef {
1024
name: " dev" .into ( ) ,
1025
state: ObjectRef( [ 5 ; 32 ] ) ,
1026
heads: vec! [ ] ,
1027
attestation: None ,
1028
last_serial: None ,
1029
} ,
750251b2 Andy McCall 1 month ago ↶
1030
] ;
75167672 Andy McCall 1 month ago
1031
let tag = TagRef {
1032
name: " v1" .into ( ) ,
1033
state: ObjectRef( [ 2 ; 32 ] ) ,
1034
heads: vec! [ c.hash ( ) ] ,
1035
attestation: None ,
1036
} ;
1037
let resp = RefsResponse {
1038
refs,
1039
policy: Some ( SignedPolicy {
1040
policy: policy.clone ( ) ,
1041
sigs: vec! [ psig.clone ( ) ] ,
1042
} ) ,
1043
tags: vec! [ tag.clone ( ) ] ,
1044
history: vec! [ SignedPolicy {
1045
policy: policy.clone ( ) ,
1046
sigs: vec! [ ] ,
1047
} ] ,
1048
} ;
83e0677b Andy McCall 1 month ago ↶
1049
let ( id_back, back) = wire:: decode_refs( & wire:: encode_refs( [ 3 ; 16 ] , & resp) ) .unwrap ( ) ;
750251b2 Andy McCall 1 month ago ↶
1050
assert_eq! ( id_back, [ 3 ; 16 ] ) ;
83e0677b Andy McCall 1 month ago ↶
1051
assert_eq! ( back.refs.len ( ) , 2 ) ;
1052
assert_eq! ( back.refs[ 0 ] .attestation.as_ref ( ) .unwrap ( ) .attest, att) ;
1053
assert_eq! ( back.refs[ 1 ] .last_serial, None ) ;
1054
assert_eq! ( back.policy.as_ref ( ) .unwrap ( ) .policy, policy) ;
28fc23d7 Andy McCall 1 month ago ↶
1055
assert_eq! ( back.history, resp.history) ;
132b884b Andy McCall 1 month ago ↶
1056
assert_eq! ( back.tags, vec! [ tag.clone ( ) ] ) ;
750251b2 Andy McCall 1 month ago ↶
1057
75167672 Andy McCall 1 month ago
1058
let ( h, w) = wire:: decode_negotiate( & wire:: encode_negotiate(
1059
& [ ObjectRef( [ 1 ; 32 ] ) ] ,
1060
& [ ObjectRef( [ 2 ; 32 ] ) ] ,
1061
) )
1062
.unwrap ( ) ;
750251b2 Andy McCall 1 month ago ↶
1063
assert_eq! ( ( h, w) , ( vec! [ ObjectRef( [ 1 ; 32 ] ) ] , vec! [ ObjectRef( [ 2 ; 32 ] ) ] ) ) ;
75167672 Andy McCall 1 month ago
1064
assert_eq! (
1065
wire:: decode_hashes( & wire:: encode_hashes( & [ c.hash ( ) ] ) ) .unwrap ( ) ,
1066
vec! [ c.hash ( ) ]
1067
) ;
1068
assert_eq! (
1069
wire:: decode_fetch( & wire:: encode_fetch( & [ c.hash ( ) ] , Some ( 4096 ) ) ) .unwrap ( ) ,
1070
( vec! [ c.hash ( ) ] , Some ( 4096 ) )
1071
) ;
1072
assert_eq! (
1073
wire:: decode_fetch( & wire:: encode_fetch( & [ ] , None ) ) .unwrap ( ) ,
1074
( vec! [ ] , None )
1075
) ;
1076
assert_eq! (
1077
wire:: decode_shallow( & wire:: encode_shallow( & [ c.hash ( ) ] , 3 ) ) .unwrap ( ) ,
1078
( vec! [ c.hash ( ) ] , 3 )
1079
) ;
b3c5a59a Andy McCall 1 month ago ↶
1080
75167672 Andy McCall 1 month ago
1081
assert_eq! (
1082
wire:: decode_fetch( & wire:: encode_hashes( & [ c.hash ( ) ] ) ) .unwrap ( ) ,
1083
( vec! [ c.hash ( ) ] , None )
1084
) ;
750251b2 Andy McCall 1 month ago ↶
1085
75167672 Andy McCall 1 month ago
1086
let up = RefUpdate {
1087
name: " main" .into ( ) ,
1088
old_state: ObjectRef( [ 1 ; 32 ] ) ,
1089
new_state: ObjectRef( [ 2 ; 32 ] ) ,
1090
force: true ,
1091
attestation: Some ( SignedAttest {
1092
attest: att,
1093
sigs: vec! [ sig] ,
1094
} ) ,
1095
} ;
1096
let sp = SignedPolicy {
1097
policy,
1098
sigs: vec! [ psig] ,
1099
} ;
1100
let ( b2, ups, pol, tags) = wire:: decode_push( & wire:: encode_push(
1101
& bundle,
1102
std:: slice:: from_ref( & up) ,
1103
Some ( & sp) ,
1104
std:: slice:: from_ref( & tag) ,
1105
) )
1106
.unwrap ( ) ;
83e0677b Andy McCall 1 month ago ↶
1107
assert_eq! ( pol, Some ( sp) ) ;
132b884b Andy McCall 1 month ago ↶
1108
assert_eq! ( tags, vec! [ tag] ) ;
750251b2 Andy McCall 1 month ago ↶
1109
assert_eq! ( b2.changes.len ( ) , 1 ) ;
1110
assert_eq! ( ups[ 0 ] .name, " main" ) ;
1111
assert! ( ups[ 0 ] .force) ;
1112
assert_eq! ( ups[ 0 ] .attestation, up.attestation) ;
75167672 Andy McCall 1 month ago
1113
let res = vec! [ RefResult {
1114
name: " main" .into ( ) ,
1115
ok: false ,
1116
reason: " stale" .into ( ) ,
1117
} ] ;
1118
assert_eq! (
1119
wire:: decode_push_response( & wire:: encode_push_response( & res) ) .unwrap ( ) ,
1120
res
1121
) ;
1122
assert_eq! (
1123
wire:: decode_capabilities( & wire:: encode_capabilities( [ 7 ; 16 ] ) ) .unwrap ( ) ,
1124
[ 7 ; 16 ]
1125
) ;
750251b2 Andy McCall 1 month ago ↶
1126
}
1127
}