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