forked from oras-project/oras-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistry.java
More file actions
1111 lines (1012 loc) · 43.4 KB
/
Registry.java
File metadata and controls
1111 lines (1012 loc) · 43.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* =LICENSE=
* ORAS Java SDK
* ===
* Copyright (C) 2024 - 2026 ORAS
* ===
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =LICENSEEND=
*/
package land.oras;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
import land.oras.auth.AuthProvider;
import land.oras.auth.AuthStoreAuthenticationProvider;
import land.oras.auth.HttpClient;
import land.oras.auth.NoAuthProvider;
import land.oras.auth.RegistriesConf;
import land.oras.auth.Scopes;
import land.oras.auth.UsernamePasswordProvider;
import land.oras.exception.OrasException;
import land.oras.utils.ArchiveUtils;
import land.oras.utils.Const;
import land.oras.utils.JsonUtils;
import land.oras.utils.SupportedAlgorithm;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
/**
* A registry is the main entry point for interacting with a container registry
*/
@NullMarked
public final class Registry extends OCI<ContainerRef> {
/**
* The registries configuration loaded from the environment
*/
private RegistriesConf registriesConf;
/**
* The HTTP client
*/
private HttpClient client;
/**
* The auth provider
*/
private AuthProvider authProvider;
/**
* Insecure. Use HTTP instead of HTTPS
*/
private boolean insecure;
/**
* The default registry to use. If null will use registry from the container ref
*/
private @Nullable String registry;
/**
* Skip TLS verification
*/
private boolean skipTlsVerify;
/**
* Constructor
*/
private Registry() {
this.authProvider = new NoAuthProvider();
this.client = HttpClient.Builder.builder().build();
this.registriesConf = RegistriesConf.newConf();
}
/**
* Get the registries configuration
* @return The registries configuration
*/
public RegistriesConf getRegistriesConf() {
return registriesConf;
}
/**
* Return a new builder for this registry
* @return The builder
*/
public static Builder builder() {
return Builder.builder();
}
/**
* Return this registry with insecure flag
* @param insecure Insecure
*/
private void setInsecure(boolean insecure) {
this.insecure = insecure;
}
/**
* Return if this registry is insecure
* @return True if insecure
*/
public boolean isInsecure() {
return insecure;
}
/**
* Return this registry with the auth provider
* @param authProvider The auth provider
*/
private void setAuthProvider(AuthProvider authProvider) {
this.authProvider = authProvider;
}
/**
* Return this registry with the registry URL
* @param registry The registry URL
*/
private void setRegistry(String registry) {
this.registry = registry;
}
/**
* Return this registry with skip TLS verification
* @param skipTlsVerify Skip TLS verification
*/
private void setSkipTlsVerify(boolean skipTlsVerify) {
this.skipTlsVerify = skipTlsVerify;
}
/**
* Build the provider
* @return The provider
*/
private Registry build() {
client = HttpClient.Builder.builder().withSkipTlsVerify(skipTlsVerify).build();
return this;
}
/**
* Get the HTTP scheme depending on the insecure flag
* @return The scheme
*/
public String getScheme() {
return insecure ? "http" : "https";
}
/**
* Return a new registry with the given registry URL but with same settings
* @param newRegistry The new target registry URL to use in the new registry
* @return The new registry
*/
public Registry copy(String newRegistry) {
return new Builder().from(this).withRegistry(newRegistry).build();
}
/**
* Return a new registry as insecure but with same settings
* @return The new registry
*/
public Registry asInsecure() {
return new Builder().from(this).withInsecure(true).build();
}
/**
* Get the registry URL
* @return The registry URL
*/
public @Nullable String getRegistry() {
return registry;
}
@Override
public Tags getTags(ContainerRef containerRef) {
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
if (ref.isInsecure(this) && !this.isInsecure()) {
return asInsecure().getTags(containerRef);
}
URI uri = URI.create("%s://%s".formatted(getScheme(), ref.getTagsPath(this)));
HttpClient.ResponseWrapper<String> response = client.get(
uri, Map.of(Const.ACCEPT_HEADER, Const.DEFAULT_JSON_MEDIA_TYPE), Scopes.of(this, ref), authProvider);
handleError(response);
return JsonUtils.fromJson(response.response(), Tags.class);
}
@Override
public Repositories getRepositories() {
if (registry != null
&& getRegistriesConf().isInsecure(ContainerRef.parse(registry).forRegistry(registry))
&& !this.isInsecure()) {
return asInsecure().getRepositories();
}
ContainerRef ref = ContainerRef.parse("default").forRegistry(this);
URI uri = URI.create("%s://%s".formatted(getScheme(), ref.getRepositoriesPath(this)));
HttpClient.ResponseWrapper<String> response = client.get(
uri, Map.of(Const.ACCEPT_HEADER, Const.DEFAULT_JSON_MEDIA_TYPE), Scopes.of(this, ref), authProvider);
handleError(response);
return JsonUtils.fromJson(response.response(), Repositories.class);
}
@Override
public Referrers getReferrers(ContainerRef containerRef, @Nullable ArtifactType artifactType) {
if (containerRef.getDigest() == null) {
throw new OrasException("Digest is required to get referrers");
}
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
if (ref.isInsecure(this) && !this.isInsecure()) {
return asInsecure().getReferrers(containerRef, artifactType);
}
URI uri = URI.create("%s://%s".formatted(getScheme(), ref.getReferrersPath(this, artifactType)));
HttpClient.ResponseWrapper<String> response = client.get(
uri, Map.of(Const.ACCEPT_HEADER, Const.DEFAULT_INDEX_MEDIA_TYPE), Scopes.of(this, ref), authProvider);
handleError(response);
return JsonUtils.fromJson(response.response(), Referrers.class);
}
/**
* Delete a manifest
* @param containerRef The artifact
*/
public void deleteManifest(ContainerRef containerRef) {
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
if (ref.isInsecure(this) && !this.isInsecure()) {
asInsecure().deleteManifest(containerRef);
return;
}
URI uri = URI.create("%s://%s".formatted(getScheme(), ref.getManifestsPath(this)));
HttpClient.ResponseWrapper<String> response = client.delete(uri, Map.of(), Scopes.of(this, ref), authProvider);
logResponse(response);
handleError(response);
}
@Override
public Manifest pushManifest(ContainerRef containerRef, Manifest manifest) {
Map<String, String> annotations = manifest.getAnnotations();
if (!annotations.containsKey(Const.ANNOTATION_CREATED) && containerRef.getDigest() == null) {
Map<String, String> manifestAnnotations = new HashMap<>(annotations);
manifestAnnotations.put(Const.ANNOTATION_CREATED, Const.currentTimestamp());
manifest = manifest.withAnnotations(manifestAnnotations);
}
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
if (ref.isInsecure(this) && !this.isInsecure()) {
return asInsecure().pushManifest(ref, manifest);
}
URI uri = URI.create("%s://%s".formatted(getScheme(), ref.getManifestsPath(this)));
byte[] manifestData = manifest.getJson() != null
? manifest.getJson().getBytes()
: manifest.toJson().getBytes();
LOG.debug("Manifest data to push: {}", new String(manifestData, StandardCharsets.UTF_8));
HttpClient.ResponseWrapper<String> response = client.put(
uri,
manifestData,
Map.of(Const.CONTENT_TYPE_HEADER, Const.DEFAULT_MANIFEST_MEDIA_TYPE),
Scopes.of(this, ref),
authProvider);
logResponse(response);
handleError(response);
if (manifest.getSubject() != null) {
// https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pushing-manifests-with-subject
if (!response.headers().containsKey(Const.OCI_SUBJECT_HEADER.toLowerCase())) {
throw new OrasException(
"Subject was set on manifest but not OCI subject header was returned. Legacy flow not implemented");
}
}
return getManifest(ref);
}
@Override
public Index pushIndex(ContainerRef containerRef, Index index) {
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
if (ref.isInsecure(this) && !this.isInsecure()) {
return asInsecure().pushIndex(containerRef, index);
}
URI uri = URI.create("%s://%s".formatted(getScheme(), ref.getManifestsPath(this)));
byte[] indexData = JsonUtils.toJson(index).getBytes();
LOG.debug("Index data to push: {}", new String(indexData, StandardCharsets.UTF_8));
HttpClient.ResponseWrapper<String> response = client.put(
uri,
indexData,
Map.of(Const.CONTENT_TYPE_HEADER, Const.DEFAULT_INDEX_MEDIA_TYPE),
Scopes.of(this, ref),
authProvider);
logResponse(response);
handleError(response);
return getIndex(ref);
}
/**
* Delete a blob
* @param containerRef The container
*/
public void deleteBlob(ContainerRef containerRef) {
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
if (ref.isInsecure(this) && !this.isInsecure()) {
asInsecure().deleteBlob(containerRef);
return;
}
URI uri = URI.create("%s://%s".formatted(getScheme(), ref.getBlobsPath(this)));
HttpClient.ResponseWrapper<String> response = client.delete(uri, Map.of(), Scopes.of(this, ref), authProvider);
logResponse(response);
handleError(response);
}
@Override
public void pullArtifact(ContainerRef containerRef, Path path, boolean overwrite) {
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
if (ref.isInsecure(this) && !this.isInsecure()) {
asInsecure().pullArtifact(containerRef, path, overwrite);
return;
}
// Only collect layer that are files
String contentType = getContentType(ref);
List<Layer> layers = collectLayers(ref, contentType, false);
if (layers.isEmpty()
|| layers.stream().noneMatch(layer -> layer.getAnnotations().containsKey(Const.ANNOTATION_TITLE))) {
LOG.info("Skipped pulling layers without file name in '{}'", Const.ANNOTATION_TITLE);
return;
}
for (Layer layer : layers) {
if (!layer.getAnnotations().containsKey(Const.ANNOTATION_TITLE)) {
LOG.info("Skipped pulling layer without file name in '{}'", Const.ANNOTATION_TITLE);
continue;
}
try (InputStream is = fetchBlob(ref.withDigest(layer.getDigest()))) {
// Unpack or just copy blob
if (Boolean.parseBoolean(layer.getAnnotations().getOrDefault(Const.ANNOTATION_ORAS_UNPACK, "false"))) {
LOG.debug("Extracting blob to: {}", path);
// Uncompress the tar.gz archive and verify digest if present
LocalPath tempArchive = ArchiveUtils.uncompress(is, layer.getMediaType());
String expectedDigest = layer.getAnnotations().get(Const.ANNOTATION_ORAS_CONTENT_DIGEST);
if (expectedDigest != null) {
LOG.trace("Expected digest: {}", expectedDigest);
String actualDigest = ref.getAlgorithm().digest(tempArchive.getPath());
LOG.trace("Actual digest: {}", actualDigest);
if (!expectedDigest.equals(actualDigest)) {
throw new OrasException(
"Digest mismatch: expected %s but got %s".formatted(expectedDigest, actualDigest));
}
}
// Extract the tar
ArchiveUtils.untar(Files.newInputStream(tempArchive.getPath()), path);
} else {
Path targetPath = path.resolve(layer.getAnnotations().get(Const.ANNOTATION_TITLE));
if (Files.exists(targetPath) && !overwrite) {
LOG.info("File already exists: {}", targetPath);
continue;
}
LOG.debug("Copying blob to: {}", targetPath);
Files.copy(is, targetPath, StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException e) {
throw new OrasException("Failed to pull artifact", e);
}
}
}
@Override
public Manifest pushArtifact(
ContainerRef containerRef,
ArtifactType artifactType,
Annotations annotations,
@Nullable Config config,
LocalPath... paths) {
Manifest manifest = Manifest.empty().withArtifactType(artifactType);
Map<String, String> manifestAnnotations = new HashMap<>(annotations.manifestAnnotations());
if (!manifestAnnotations.containsKey(Const.ANNOTATION_CREATED) && containerRef.getDigest() == null) {
manifestAnnotations.put(Const.ANNOTATION_CREATED, Const.currentTimestamp());
}
manifest = manifest.withAnnotations(manifestAnnotations);
if (config != null) {
config = config.withAnnotations(annotations);
manifest = manifest.withConfig(config);
}
// Push the config like any other blob
Config pushedConfig = pushConfig(containerRef, config != null ? config : Config.empty());
String resolvedRegistry = pushedConfig.getRegistry();
Objects.requireNonNull(resolvedRegistry, "Pushed config must have a registry resolved");
// Build the resolved ref including rewrite
ContainerRef resolvedRef = containerRef.forRegistry(this).forRegistry(resolvedRegistry);
// Push layers
List<Layer> layers = pushLayers(resolvedRef, annotations, false, paths);
// Add layer and config
manifest = manifest.withLayers(layers).withConfig(pushedConfig);
// Push the manifest
manifest = pushManifest(resolvedRef, manifest);
LOG.debug(
"Manifest pushed to: {}",
resolvedRef.withDigest(manifest.getDescriptor().getDigest()));
return manifest;
}
@Override
public Layer pushBlob(ContainerRef containerRef, Path blob, Map<String, String> annotations) {
String digest = containerRef.getAlgorithm().digest(blob);
LOG.debug("Digest: {}", digest);
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
if (ref.isInsecure(this) && !this.isInsecure()) {
return asInsecure().pushBlob(containerRef, blob, annotations);
}
// This might not works with registries performing HEAD request
if (hasBlob(ref.withDigest(digest))) {
LOG.info("Blob already exists: {}", digest);
return Layer.fromFile(blob, ref.getAlgorithm()).withAnnotations(annotations);
}
URI uri = URI.create(
"%s://%s".formatted(getScheme(), ref.withDigest(digest).getBlobsUploadDigestPath(this)));
HttpClient.ResponseWrapper<String> response = client.upload(
"POST",
uri,
Map.of(Const.CONTENT_TYPE_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
blob,
Scopes.of(this, ref),
authProvider);
logResponse(response);
// Accepted single POST push
if (response.statusCode() == 201) {
return Layer.fromFile(blob, ref.getAlgorithm()).withAnnotations(annotations);
}
// We need to push via PUT
if (response.statusCode() == 202) {
String location = response.headers().get(Const.LOCATION_HEADER.toLowerCase());
// Ensure location is absolute URI
if (!location.startsWith("http") && !location.startsWith("https")) {
location = "%s://%s/%s"
.formatted(getScheme(), containerRef.getApiRegistry(this), location.replaceFirst("^/", ""));
}
LOG.debug("Location header: {}", location);
URI uploadURI = createLocationWithDigest(location, digest);
response = client.upload(
"PUT",
uploadURI,
Map.of(Const.CONTENT_TYPE_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
blob,
Scopes.of(this, containerRef),
authProvider);
if (response.statusCode() == 201) {
LOG.debug("Successful push: {}", response.response());
} else {
throw new OrasException("Failed to push layer: %s".formatted(response.response()));
}
}
handleError(response);
return Layer.fromFile(blob, containerRef.getAlgorithm()).withAnnotations(annotations);
}
@Override
public Layer pushBlob(ContainerRef ref, long size, Supplier<InputStream> stream, Map<String, String> annotations) {
String digest = ref.getDigest();
if (digest == null) {
throw new OrasException("Digest is required to push blob with stream");
}
ContainerRef containerRef = ref.forRegistry(this).checkBlocked(this);
if (containerRef.isInsecure(this) && !this.isInsecure()) {
return asInsecure().pushBlob(ref, size, stream, annotations);
}
if (hasBlob(containerRef)) {
LOG.info("Blob already exists: {}", digest);
return Layer.fromDigest(digest, size).withAnnotations(annotations);
}
// Empty post without digest
URI uri = URI.create("%s://%s".formatted(getScheme(), containerRef.getBlobsUploadPath(this)));
HttpClient.ResponseWrapper<String> response = client.post(
uri,
new byte[0],
Map.of(Const.CONTENT_TYPE_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
Scopes.of(this, containerRef),
authProvider);
logResponse(response);
if (response.statusCode() != 202) {
throw new OrasException("Failed to initiate blob upload: %s".formatted(response.response()));
}
String location = response.headers().get(Const.LOCATION_HEADER.toLowerCase());
// Ensure location is absolute URI
if (!location.startsWith("http") && !location.startsWith("https")) {
location = "%s://%s/%s".formatted(getScheme(), ref.getApiRegistry(this), location.replaceFirst("^/", ""));
}
LOG.debug("Location header: {}", location);
URI uploadURI = createLocationWithDigest(location, digest);
response = client.upload(
uploadURI,
size,
Map.of(Const.CONTENT_TYPE_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
stream,
Scopes.of(this, containerRef),
authProvider);
logResponse(response);
if (response.statusCode() == 201) {
LOG.debug("Successful push: {}", response.response());
} else {
throw new OrasException("Failed to push layer: %s".formatted(response.response()));
}
handleError(response);
return Layer.fromDigest(digest, size).withAnnotations(annotations);
}
@Override
public Layer pushBlob(ContainerRef containerRef, byte[] data) {
String digest = containerRef.getAlgorithm().digest(data);
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
if (ref.isInsecure(this) && !this.isInsecure()) {
return asInsecure().pushBlob(containerRef, data);
}
if (ref.getDigest() != null) {
ensureDigest(ref, data);
}
if (hasBlob(ref.withDigest(digest))) {
LOG.info("Blob already exists: {}", digest);
return Layer.fromData(ref, data);
}
URI uri = URI.create(
"%s://%s".formatted(getScheme(), ref.withDigest(digest).getBlobsUploadDigestPath(this)));
HttpClient.ResponseWrapper<String> response = client.post(
uri,
data,
Map.of(Const.CONTENT_TYPE_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
Scopes.of(this, ref),
authProvider);
logResponse(response);
// Accepted single POST push
if (response.statusCode() == 201) {
return Layer.fromData(ref, data);
}
// We need to push via PUT
if (response.statusCode() == 202) {
String location = response.headers().get(Const.LOCATION_HEADER.toLowerCase());
// Ensure location is absolute URI
if (!location.startsWith("http") && !location.startsWith("https")) {
location =
"%s://%s/%s".formatted(getScheme(), ref.getApiRegistry(this), location.replaceFirst("^/", ""));
}
URI uploadURI = createLocationWithDigest(location, digest);
LOG.debug("Location header: {}", location);
response = client.put(
uploadURI,
data,
Map.of(Const.CONTENT_TYPE_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
Scopes.of(this, ref),
authProvider);
if (response.statusCode() == 201) {
LOG.debug("Successful push: {}", response.response());
} else {
throw new OrasException("Failed to push layer: %s".formatted(response.response()));
}
}
handleError(response);
return Layer.fromData(ref, data);
}
/**
* Return if the registry contains already the blob
* @param containerRef The container
* @return True if the blob exists
*/
private boolean hasBlob(ContainerRef containerRef) {
HttpClient.ResponseWrapper<String> response = headBlob(containerRef);
return response.statusCode() == 200;
}
private HttpClient.ResponseWrapper<String> headBlob(ContainerRef containerRef) {
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
if (ref.isInsecure(this) && !this.isInsecure()) {
return asInsecure().headBlob(containerRef);
}
URI uri = URI.create("%s://%s".formatted(getScheme(), ref.getBlobsPath(this)));
HttpClient.ResponseWrapper<String> response = client.head(
uri,
Map.of(Const.ACCEPT_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
Scopes.of(this, containerRef),
authProvider);
logResponse(response);
return response;
}
/**
* Get the blob for the given digest. Not be suitable for large blobs
* @param containerRef The container
* @return The blob as bytes
*/
@Override
public byte[] getBlob(ContainerRef containerRef) {
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
if (ref.isInsecure(this) && !this.isInsecure()) {
return asInsecure().getBlob(containerRef);
}
URI uri = URI.create("%s://%s".formatted(getScheme(), ref.getBlobsPath(this)));
HttpClient.ResponseWrapper<String> response = client.get(
uri,
Map.of(Const.ACCEPT_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
Scopes.of(this, ref),
authProvider);
logResponse(response);
handleError(response);
byte[] data = response.response().getBytes(StandardCharsets.UTF_8);
validateDockerContentDigest(response, data);
return data;
}
@Override
public void fetchBlob(ContainerRef containerRef, Path path) {
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
if (ref.isInsecure(this) && !this.isInsecure()) {
asInsecure().fetchBlob(containerRef, path);
return;
}
URI uri = URI.create("%s://%s".formatted(getScheme(), ref.getBlobsPath(this)));
HttpClient.ResponseWrapper<Path> response = client.download(
uri,
Map.of(Const.ACCEPT_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
path,
Scopes.of(this, ref),
authProvider);
logResponse(response);
handleError(response);
validateDockerContentDigest(response, path);
}
@Override
public InputStream fetchBlob(ContainerRef containerRef) {
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
if (ref.isInsecure(this) && !this.isInsecure()) {
return asInsecure().fetchBlob(containerRef);
}
URI uri = URI.create("%s://%s".formatted(getScheme(), ref.getBlobsPath(this)));
HttpClient.ResponseWrapper<InputStream> response = client.download(
uri,
Map.of(Const.ACCEPT_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE),
Scopes.of(this, ref),
authProvider);
logResponse(response);
handleError(response);
validateDockerContentDigest(response);
return response.response();
}
@Override
public Descriptor fetchBlobDescriptor(ContainerRef containerRef) {
HttpClient.ResponseWrapper<String> response = headBlob(containerRef);
handleError(response);
String size = response.headers().get(Const.CONTENT_LENGTH_HEADER.toLowerCase());
return Descriptor.of(
validateDockerContentDigest(response), Long.parseLong(size), Const.DEFAULT_DESCRIPTOR_MEDIA_TYPE);
}
@Override
public Manifest getManifest(ContainerRef containerRef) {
Descriptor descriptor = getDescriptor(containerRef);
String contentType = descriptor.getMediaType();
if (!isManifestMediaType(contentType)) {
throw new OrasException(
"Expected manifest but got index. Probably a multi-platform image instead of artifact");
}
String json = descriptor.getJson();
String digest = descriptor.getDigest();
if (digest == null) {
LOG.debug("Digest missing from header, using from reference");
digest = containerRef.getDigest();
if (digest == null) {
LOG.debug("Digest missing from reference, computing from content");
digest = containerRef.getAlgorithm().digest(json.getBytes(StandardCharsets.UTF_8));
LOG.debug("Computed index digest: {}", digest);
}
}
ManifestDescriptor manifestDescriptor = ManifestDescriptor.of(descriptor, digest);
return Manifest.fromJson(json).withDescriptor(manifestDescriptor);
}
@Override
public Index getIndex(ContainerRef containerRef) {
Descriptor descriptor = getDescriptor(containerRef);
String contentType = descriptor.getMediaType();
if (!isIndexMediaType(contentType)) {
throw new OrasException("Expected index but got %s".formatted(contentType));
}
String json = descriptor.getJson();
String digest = descriptor.getDigest();
if (digest == null) {
LOG.debug("Digest missing from header, using from reference");
digest = containerRef.getDigest();
if (digest == null) {
LOG.debug("Digest missing from reference, computing from content");
digest = containerRef.getAlgorithm().digest(json.getBytes(StandardCharsets.UTF_8));
LOG.debug("Computed index digest: {}", digest);
}
}
ManifestDescriptor manifestDescriptor = ManifestDescriptor.of(descriptor, digest);
return Index.fromJson(json).withDescriptor(manifestDescriptor);
}
@Override
public Descriptor getDescriptor(ContainerRef containerRef) {
HttpClient.ResponseWrapper<String> response = getManifestResponse(containerRef);
logResponse(response);
handleError(response);
String size = response.headers().get(Const.CONTENT_LENGTH_HEADER.toLowerCase());
String contentType = response.headers().get(Const.CONTENT_TYPE_HEADER.toLowerCase());
return Descriptor.of(
validateDockerContentDigest(response),
Long.parseLong(
size == null
? String.valueOf(response.response().length())
: size),
contentType)
.withJson(response.response());
}
@Override
public Descriptor probeDescriptor(ContainerRef ref) {
ResolvedRegistry resolvedRegistry = getResolvedHeaders(ref);
Map<String, String> headers = resolvedRegistry.headers();
String registry = resolvedRegistry.registry();
String digest = validateDockerContentDigest(headers);
if (digest != null) {
SupportedAlgorithm.fromDigest(digest);
}
String contentType = headers.get(Const.CONTENT_TYPE_HEADER.toLowerCase());
return Descriptor.of(digest, 0L, contentType).withRegistry(registry);
}
/**
* Return if the container ref manifests or index exists
* @param containerRef The container
* @return True if exists
*/
boolean exists(ContainerRef containerRef) {
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
if (ref.isInsecure(this) && !this.isInsecure()) {
return asInsecure().exists(containerRef);
}
URI uri = URI.create("%s://%s".formatted(getScheme(), ref.getManifestsPath(this)));
HttpClient.ResponseWrapper<String> response = client.head(
uri, Map.of(Const.ACCEPT_HEADER, Const.MANIFEST_ACCEPT_TYPE), Scopes.of(this, ref), authProvider);
logResponse(response);
return response.statusCode() == 200;
}
/**
* Get a manifest response
* @param containerRef The container
* @return The response
*/
private HttpClient.ResponseWrapper<String> getManifestResponse(ContainerRef containerRef) {
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
if (ref.isInsecure(this) && !this.isInsecure()) {
return asInsecure().getManifestResponse(containerRef);
}
URI uri = URI.create("%s://%s".formatted(getScheme(), ref.getManifestsPath(this)));
HttpClient.ResponseWrapper<String> response = client.head(
uri, Map.of(Const.ACCEPT_HEADER, Const.MANIFEST_ACCEPT_TYPE), Scopes.of(this, ref), authProvider);
logResponse(response);
handleError(response);
return client.get(uri, Map.of("Accept", Const.MANIFEST_ACCEPT_TYPE), Scopes.of(this, ref), authProvider);
}
private void validateDockerContentDigest(HttpClient.ResponseWrapper<String> response, byte[] data) {
String digest = response.headers().get(Const.DOCKER_CONTENT_DIGEST_HEADER.toLowerCase());
// This might happen when blob are hosted other storage.
// We need a way to propagate the headers like scoped.
// For now just skip validation
if (digest == null) {
LOG.debug("Docker-Content-Digest header not found in response. Skipping validation.");
return;
}
String computedDigest = SupportedAlgorithm.fromDigest(digest).digest(data);
ensureDigest(digest, computedDigest);
}
private void validateDockerContentDigest(HttpClient.ResponseWrapper<Path> response, Path path) {
String digest = response.headers().get(Const.DOCKER_CONTENT_DIGEST_HEADER.toLowerCase());
// This might happen when blob are hosted other storage.
// We need a way to propagate the headers like scoped.
// For now just skip validation
if (digest == null) {
LOG.debug("Docker-Content-Digest header not found in response. Skipping validation.");
return;
}
String computedDigest = SupportedAlgorithm.fromDigest(digest).digest(path);
ensureDigest(digest, computedDigest);
}
private @Nullable String validateDockerContentDigest(HttpClient.ResponseWrapper<?> response) {
return validateDockerContentDigest(response.headers());
}
private @Nullable String validateDockerContentDigest(Map<String, String> headers) {
String digest = headers.get(Const.DOCKER_CONTENT_DIGEST_HEADER.toLowerCase());
// This might happen when blob are hosted other storage.
// We need a way to propagate the headers like scoped.
// For now just skip validation
if (digest == null) {
LOG.debug("Docker-Content-Digest header not found in response. Skipping validation.");
return null;
}
SupportedAlgorithm.fromDigest(digest);
return digest;
}
private void ensureDigest(ContainerRef ref, byte[] data) {
if (ref.getDigest() == null) {
throw new OrasException("Missing digest");
}
SupportedAlgorithm algorithm = SupportedAlgorithm.fromDigest(ref.getDigest());
String dataDigest = algorithm.digest(data);
ensureDigest(ref.getDigest(), dataDigest);
}
private void ensureDigest(String expected, @Nullable String current) {
if (current == null) {
throw new OrasException("Received null digest");
}
if (!expected.equals(current)) {
throw new OrasException("Digest mismatch: %s != %s".formatted(expected, current));
}
}
/**
* Handle an error response
* @param responseWrapper The response
*/
@SuppressWarnings("unchecked")
private void handleError(HttpClient.ResponseWrapper<?> responseWrapper) {
if (responseWrapper.statusCode() >= 400) {
if (responseWrapper.response() instanceof String) {
LOG.debug("Response: {}", responseWrapper.response());
throw new OrasException((HttpClient.ResponseWrapper<String>) responseWrapper);
}
throw new OrasException(new HttpClient.ResponseWrapper<>("", responseWrapper.statusCode(), Map.of()));
}
}
/**
* Log the response
* @param response The response
*/
private void logResponse(HttpClient.ResponseWrapper<?> response) {
LOG.debug("Status Code: {}", response.statusCode());
LOG.debug("Headers: {}", response.headers());
String contentType = response.headers().get(Const.CONTENT_TYPE_HEADER.toLowerCase());
boolean isBinaryResponse = contentType != null && contentType.contains("octet-stream");
// Only log non-binary responses
if (response.response() instanceof String && !isBinaryResponse) {
LOG.debug("Response: {}", response.response());
} else {
LOG.debug("Not logging binary response of content type: {}", contentType);
}
}
/**
* Get blob as stream to avoid loading into memory
* @param containerRef The container ref
* @return The input stream
*/
public InputStream getBlobStream(ContainerRef containerRef) {
// Similar to fetchBlob()
return fetchBlob(containerRef);
}
/**
* Get the content type of the container
* @param containerRef The container
* @return The content type
*/
String getContentType(ContainerRef containerRef) {
return getResolvedHeaders(containerRef).headers().get(Const.CONTENT_TYPE_HEADER.toLowerCase());
}
/**
* Append digest to location header returned from upload post
* @param location The location header from upload post
* @return URI with location + digest query parameter
*/
URI createLocationWithDigest(String location, String digest) {
URI uploadURI;
try {
uploadURI = new URI(location);
// sometimes CI can add a query to this, so making sure we're using the right query param symbol
if (uploadURI.getQuery() == null) {
uploadURI = new URI(uploadURI + "?digest=%s".formatted(digest));
} else {
uploadURI = new URI(uploadURI + "&digest=%s".formatted(digest));
}
} catch (URISyntaxException e) {
throw new OrasException("Failed parse location header: %s".formatted(location));
}
return uploadURI;
}
/**
* Execute a head request on the manifest URL and return the headers
* @param containerRef The container ref
* @return The resolved registry and headers
*/
ResolvedRegistry getResolvedHeaders(ContainerRef containerRef) {
if (containerRef.isInsecure(this) && !this.isInsecure()) {
return asInsecure().getResolvedHeaders(containerRef);
}
ContainerRef ref = containerRef.forRegistry(this);
URI uri = URI.create(
"%s://%s".formatted(getScheme(), ref.forRegistry(this).getManifestsPath(this)));
HttpClient.ResponseWrapper<String> response = client.head(
uri, Map.of(Const.ACCEPT_HEADER, Const.MANIFEST_ACCEPT_TYPE), Scopes.of(this, ref), authProvider);
logResponse(response);
handleError(response);
return new ResolvedRegistry(ref.getRegistry(), response.headers());
}
/**
* Holds a resolved registry to avoid resolution on every request (specially like blob)
* @param registry The registry URL
* @param headers The headers to use for the registry
*/
private record ResolvedRegistry(String registry, Map<String, String> headers) {}
/**
* Builder for the registry
*/
public static class Builder {
private final Registry registry = new Registry();
/**
* Hidden constructor
*/
private Builder() {
// Hide constructor
}
/**
* Return a new builder with default authentication using existing host auth
* @return The builder
*/
public Builder defaults() {
registry.setAuthProvider(new AuthStoreAuthenticationProvider());
return this;
}
/**
* Return a new builder with default authentication using existing host auth and registry url
* @param registry The registry URL (ex: localhost:5000)
* @return The builder
*/
public Builder defaults(String registry) {
return defaults().withRegistry(registry);
}
/**
* Return a new builder with the same configuration as the given registry
* @param registry The registry to copy the configuration from
* @return The builder
*/
public Builder from(Registry registry) {
this.registry.setAuthProvider(registry.authProvider);
this.registry.setInsecure(registry.insecure);
this.registry.setRegistry(registry.registry);
this.registry.setSkipTlsVerify(registry.skipTlsVerify);
return this;
}