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