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