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