-
Notifications
You must be signed in to change notification settings - Fork 63
/
manager.go
885 lines (750 loc) · 23.2 KB
/
manager.go
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package oci
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net/http"
"slices"
"strings"
"sync"
"github.com/gobwas/glob"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"kraftkit.sh/config"
"kraftkit.sh/internal/set"
"kraftkit.sh/internal/version"
"kraftkit.sh/log"
"kraftkit.sh/oci/cache"
"kraftkit.sh/oci/handler"
"kraftkit.sh/oci/simpleauth"
ociutils "kraftkit.sh/oci/utils"
"kraftkit.sh/pack"
"kraftkit.sh/packmanager"
"kraftkit.sh/unikraft"
"kraftkit.sh/unikraft/component"
"kraftkit.sh/unikraft/target"
)
type ociManager struct {
registries []string
auths map[string]config.AuthConfig
handle func(ctx context.Context) (context.Context, handler.Handler, error)
}
const OCIFormat pack.PackageFormat = "oci"
// NewOCIManager instantiates a new package manager based on OCI archives.
func NewOCIManager(ctx context.Context, opts ...any) (packmanager.PackageManager, error) {
manager := ociManager{}
for _, mopt := range opts {
opt, ok := mopt.(OCIManagerOption)
if !ok {
return nil, fmt.Errorf("cannot cast OCI Manager option")
}
if err := opt(ctx, &manager); err != nil {
return nil, err
}
}
if manager.handle == nil {
return nil, fmt.Errorf("cannot instantiate OCI Manager without handler")
}
return &manager, nil
}
// Update implements packmanager.PackageManager
func (manager *ociManager) Update(ctx context.Context) error {
packs, err := manager.update(ctx, nil, nil)
if err != nil {
return err
}
for _, pack := range packs {
pack := pack.(*ociPackage) // Safe since we're in the oci package
log.G(ctx).Infof("saving %s", pack.String())
if _, err := pack.index.Save(ctx, pack.imageRef(), nil); err != nil {
return fmt.Errorf("error saving %s: %w", pack.String(), err)
}
}
return nil
}
func (manager *ociManager) update(ctx context.Context, auths map[string]config.AuthConfig, query *packmanager.Query) (map[string]pack.Package, error) {
ctx, handle, err := manager.handle(ctx)
if err != nil {
return nil, err
}
if auths == nil {
auths, err = defaultAuths(ctx)
if err != nil {
return nil, fmt.Errorf("accessing credentials: %w", err)
}
}
packs := make(map[string]pack.Package)
for _, domain := range manager.registries {
log.G(ctx).
WithField("registry", domain).
Trace("querying")
nopts := []name.Option{}
authConfig := &authn.AuthConfig{}
transport := http.DefaultTransport.(*http.Transport).Clone()
// Annoyingly convert between regtypes and authn.
if auth, ok := auths[domain]; ok {
authConfig.Username = auth.User
authConfig.Password = auth.Token
if !auth.VerifySSL {
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
nopts = append(nopts, name.Insecure)
}
}
regName, err := name.NewRegistry(domain, nopts...)
if err != nil {
log.G(ctx).
WithField("registry", domain).
Debugf("could not parse registry: %v", err)
continue
}
catalog, err := remote.Catalog(ctx, regName,
remote.WithContext(ctx),
remote.WithAuth(&simpleauth.SimpleAuthenticator{
Auth: authConfig,
}),
remote.WithTransport(transport),
)
if err != nil {
log.G(ctx).
WithField("registry", domain).
Debugf("could not query catalog: %v", err)
continue
}
var wg sync.WaitGroup
wg.Add(len(catalog))
var mu sync.RWMutex
for _, fullref := range catalog {
go func(fullref string) {
defer wg.Done()
ref, err := name.ParseReference(fullref,
name.WithDefaultRegistry(domain),
name.WithDefaultTag(DefaultTag),
)
if err != nil {
log.G(ctx).
WithField("ref", fullref).
Tracef("skipping index: could not parse reference: %s", err.Error())
return
}
index, err := cache.RemoteIndex(ref,
remote.WithContext(ctx),
remote.WithAuth(&simpleauth.SimpleAuthenticator{
Auth: authConfig,
}),
remote.WithTransport(transport),
)
if err != nil {
log.G(ctx).
WithField("ref", fullref).
Tracef("skipping index: could not retrieve image: %s", err.Error())
return
}
v1IndexManifest, err := index.IndexManifest()
if err != nil {
log.G(ctx).
WithField("ref", fullref).
Tracef("could not access the index's manifest object: %s", err.Error())
return
}
v1ManifestPackages := processV1IndexManifests(ctx,
handle,
fullref,
query,
FromGoogleV1DescriptorToOCISpec(v1IndexManifest.Manifests...),
)
mu.Lock()
for checksum, pack := range v1ManifestPackages {
var title []string
for _, column := range pack.Columns() {
if len(column.Value) > 12 {
continue
}
title = append(title, column.Value)
}
log.G(ctx).
Tracef("found %s (%s)", pack.String(), strings.Join(title, ", "))
packs[checksum] = pack
}
mu.Unlock()
}(fullref)
}
wg.Wait()
}
return packs, nil
}
// Pack implements packmanager.PackageManager
func (manager *ociManager) Pack(ctx context.Context, entity component.Component, opts ...packmanager.PackOption) ([]pack.Package, error) {
targ, ok := entity.(target.Target)
if !ok {
return nil, fmt.Errorf("entity is not Unikraft target")
}
pkg, err := NewPackageFromTarget(ctx, targ, opts...)
if err != nil {
return nil, err
}
return []pack.Package{pkg}, nil
}
// Unpack implements packmanager.PackageManager
func (manager *ociManager) Unpack(ctx context.Context, entity pack.Package, opts ...packmanager.UnpackOption) ([]component.Component, error) {
return nil, fmt.Errorf("not implemented: oci.manager.Unpack")
}
// processV1IndexManifests is an internal utility method which is able to
// iterate over the supplied slice of ocispec.Descriptors which represent a
// Manifest from an Index. Based on the provided criterium from the query,
// identify the Descriptor that is compatible and instantiate a pack.Package
// structure from it.
func processV1IndexManifests(ctx context.Context, handle handler.Handler, fullref string, query *packmanager.Query, manifests []ocispec.Descriptor) map[string]pack.Package {
packs := make(map[string]pack.Package)
var wg sync.WaitGroup
wg.Add(len(manifests))
var mu sync.RWMutex
for _, descriptor := range manifests {
go func(descriptor ocispec.Descriptor) {
defer wg.Done()
if ok, err := IsOCIDescriptorKraftKitCompatible(&descriptor); !ok {
log.G(ctx).
WithField("digest", descriptor.Digest.String()).
WithField("ref", fullref).
Tracef("incompatible index structure: %s", err.Error())
return
}
if query != nil && query.Platform() != "" && query.Platform() != descriptor.Platform.OS {
log.G(ctx).
WithField("ref", fullref).
WithField("digest", descriptor.Digest.String()).
WithField("want", query.Platform()).
WithField("got", descriptor.Platform.OS).
Trace("skipping manifest: platform does not match query")
return
}
if query != nil && query.Architecture() != "" && query.Architecture() != descriptor.Platform.Architecture {
log.G(ctx).
WithField("ref", fullref).
WithField("digest", descriptor.Digest.String()).
WithField("want", query.Architecture()).
WithField("got", descriptor.Platform.Architecture).
Trace("skipping manifest: architecture does not match query")
return
}
if query != nil && len(query.KConfig()) > 0 {
// If the list of requested features is greater than the list of
// available features, there will be no way for the two to match. We
// are searching for a subset of query.KConfig() from
// m.Platform.OSFeatures to match.
if len(query.KConfig()) > len(descriptor.Platform.OSFeatures) {
log.G(ctx).
WithField("ref", fullref).
WithField("digest", descriptor.Digest.String()).
Trace("skipping descriptor: query contains more features than available")
return
}
available := set.NewStringSet(descriptor.Platform.OSFeatures...)
// Iterate through the query's requested set of features and skip only
// if the descriptor does not contain the requested KConfig feature.
for _, a := range query.KConfig() {
if !available.Contains(a) {
log.G(ctx).
WithField("ref", fullref).
WithField("digest", descriptor.Digest.String()).
WithField("feature", a).
Trace("skipping manifest: missing feature")
return
}
}
}
var auths map[string]config.AuthConfig
if query != nil {
auths = query.Auths()
}
log.G(ctx).
WithField("ref", fullref).
WithField("digest", descriptor.Digest.String()).
Trace("found")
// If we have made it this far, the query has been successfully
// satisfied by this particular manifest and we can generate a package
// from it.
pack, err := NewPackageFromOCIManifestDigest(ctx,
handle,
fullref,
auths,
descriptor.Digest,
)
if err != nil {
log.G(ctx).
WithField("ref", fullref).
WithField("digest", descriptor.Digest.String()).
Tracef("skipping manifest: could not instantiate package from manifest digest: %s", err.Error())
return
}
checksum, err := ociutils.PlatformChecksum(pack.String(), descriptor.Platform)
if err != nil {
log.G(ctx).
WithField("ref", fullref).
Debugf("could not calculate platform digest for '%s': %s", descriptor.Digest.String(), err)
return
}
mu.Lock()
packs[checksum] = pack
mu.Unlock()
}(descriptor)
}
wg.Wait()
return packs
}
// Catalog implements packmanager.PackageManager
func (manager *ociManager) Catalog(ctx context.Context, qopts ...packmanager.QueryOption) ([]pack.Package, error) {
query := packmanager.NewQuery(qopts...)
// Do not perform a search if a query for a specific type is requested and it
// does not include the application-type.
if len(query.Types()) > 0 && !slices.Contains(query.Types(), unikraft.ComponentTypeApp) {
return nil, nil
}
var qglob glob.Glob
var err error
packs := make(map[string]pack.Package)
qname := query.Name()
total := 0
if strings.ContainsRune(qname, '*') {
qglob, err = glob.Compile(qname)
if err != nil {
return nil, fmt.Errorf("query name is not globable: %w", err)
}
} else if !strings.ContainsRune(qname, ':') && len(query.Version()) > 0 {
qname = fmt.Sprintf("%s:%s", qname, query.Version())
}
qversion := query.Version()
// Adjust for the version being suffixed in a prototypical OCI reference
// format.
ref, refErr := name.ParseReference(qname,
name.WithDefaultRegistry(""),
name.WithDefaultTag(DefaultTag),
)
if refErr == nil {
qname = ref.Context().Name()
if ref.Identifier() != "latest" && qversion != "" && ref.Identifier() != qversion {
return nil, fmt.Errorf("cannot determine which version as name contains version and version query paremeter set")
} else if qversion == "" {
qversion = ref.Identifier()
}
}
unsetRegistry := false
// No default registry found, re-parse with
if ref != nil && ref.Context().RegistryStr() == "" {
unsetRegistry = true
ref, refErr = name.ParseReference(fmt.Sprintf("%s:%s", qname, qversion),
name.WithDefaultRegistry(DefaultRegistry),
name.WithDefaultTag(DefaultTag),
)
}
log.G(ctx).
WithFields(query.Fields()).
Debug("querying catalog")
ctx, handle, err := manager.handle(ctx)
if err != nil {
return nil, err
}
var auths map[string]config.AuthConfig
if query.Auths() == nil {
auths, err = defaultAuths(ctx)
if err != nil {
return nil, fmt.Errorf("could not access credentials: %w", err)
}
} else {
auths = query.Auths()
}
// If a direct reference can be made, attempt to generate a package from it.
if query.Remote() && refErr == nil && !unsetRegistry {
authConfig := &authn.AuthConfig{}
ropts := []remote.Option{
remote.WithContext(ctx),
}
// Annoyingly convert between regtypes and authn.
if auth, ok := auths[ref.Context().RegistryStr()]; ok {
authConfig.Username = auth.User
authConfig.Password = auth.Token
if !auth.VerifySSL {
rt := http.DefaultTransport.(*http.Transport).Clone()
rt.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
ropts = append(ropts,
remote.WithTransport(rt),
)
}
ropts = append(ropts,
remote.WithAuth(&simpleauth.SimpleAuthenticator{
Auth: authConfig,
}),
)
}
log.G(ctx).
WithField("ref", ref.Name()).
Trace("getting remote index")
v1ImageIndex, err := cache.RemoteIndex(ref, ropts...)
if err != nil {
log.G(ctx).
Debugf("could not get index: %v", err)
goto resolveLocalIndex
}
v1IndexManifest, err := v1ImageIndex.IndexManifest()
if err != nil {
log.G(ctx).
WithField("ref", ref).
Tracef("could not access the index's manifest object: %s", err.Error())
goto resolveLocalIndex
}
for checksum, pack := range processV1IndexManifests(ctx,
handle,
ref.String(),
query,
FromGoogleV1DescriptorToOCISpec(v1IndexManifest.Manifests...),
) {
packs[checksum] = pack
total++
}
// No need to search remote indexes by registry if the registry has been
// included as part of the ref.
goto searchLocalIndexes
}
if query.Remote() {
more, err := manager.update(ctx, auths, query)
if err != nil {
log.G(ctx).
Debugf("could not update: %v", err)
} else {
for checksum, pack := range more {
total++
ref, err := name.ParseReference(fmt.Sprintf("%s:%s", pack.Name(), pack.Version()))
if err != nil {
log.G(ctx).
WithField("ref", pack.Name()).
Tracef("skipping index: could not parse reference: %s", err.Error())
continue
}
fullref := fmt.Sprintf("%s:%s", ref.Context().RepositoryStr(), ref.Identifier())
// If the query did specify a registry include this in check otherwise
// search for indexes without this as prefix.
if !unsetRegistry {
fullref = fmt.Sprintf("%s/%s", ref.Context().RegistryStr(), fullref)
}
if qglob != nil && !qglob.Match(fullref) {
log.G(ctx).
WithField("want", qname).
WithField("got", fullref).
Trace("skipping manifest: glob does not match")
continue
} else if qglob == nil {
if len(qversion) > 0 && len(qname) > 0 {
if fullref != fmt.Sprintf("%s:%s", qname, qversion) {
log.G(ctx).
WithField("want", fmt.Sprintf("%s:%s", qname, qversion)).
WithField("got", fullref).
Trace("skipping manifest: name does not match")
continue
}
} else if len(qname) > 0 && fullref != qname {
log.G(ctx).
WithField("want", qname).
WithField("got", fullref).
Trace("skipping manifest: name does not match")
continue
}
}
packs[checksum] = pack
}
}
}
resolveLocalIndex:
// If the query is local and the reference is a fully qualified OCI reference,
// attempt to resolve the exact index and generate packages from it.
if query.Local() && len(qversion) > 0 && len(qname) > 0 {
oref := fmt.Sprintf("%s:%s", qname, qversion)
index, err := handle.ResolveIndex(ctx, oref)
if err != nil {
log.G(ctx).
WithField("ref", oref).
Trace("could not resolve exact index")
goto searchLocalIndexes
}
for checksum, pack := range processV1IndexManifests(ctx,
handle,
oref,
query,
index.Manifests,
) {
packs[checksum] = pack
total++
}
// If the register was set, then an exact local index lookup was expected so
// we can return here.
if !unsetRegistry {
goto returnPacks
}
}
searchLocalIndexes:
if query.Local() {
// Access local indexes that are available on the host
indexes, err := handle.ListIndexes(ctx)
if err != nil {
return nil, err
}
for oref, index := range indexes {
ref, err := name.ParseReference(oref,
name.WithDefaultRegistry(""),
name.WithDefaultTag(DefaultTag),
)
if err != nil {
log.G(ctx).
WithField("ref", oref).
Tracef("skipping index: invalid reference format: %s", err.Error())
total += len(index.Manifests)
continue
}
fullref := fmt.Sprintf("%s:%s", ref.Context().RepositoryStr(), ref.Identifier())
// If the query did specify a registry include this in check otherwise
// search for indexes without this as prefix.
if !unsetRegistry {
fullref = fmt.Sprintf("%s/%s", ref.Context().RegistryStr(), fullref)
}
if ok, err := IsOCIIndexKraftKitCompatible(index); !ok {
log.G(ctx).
WithField("ref", fullref).
Tracef("skipping index: incompatible index structure: %s", err.Error())
total += len(index.Manifests)
continue
}
if qglob != nil && !qglob.Match(fullref) {
log.G(ctx).
WithField("want", qname).
WithField("got", fullref).
Trace("skipping index: glob does not match")
total += len(index.Manifests)
continue
} else if qglob == nil {
if len(qversion) > 0 && len(qname) > 0 {
if fullref != fmt.Sprintf("%s:%s", qname, qversion) {
log.G(ctx).
WithField("want", fmt.Sprintf("%s:%s", qname, qversion)).
WithField("got", fullref).
Trace("skipping index: name does not match")
total += len(index.Manifests)
continue
}
} else if len(qname) > 0 && fullref != qname {
log.G(ctx).
WithField("want", qname).
WithField("got", fullref).
Trace("skipping index: name does not match")
total += len(index.Manifests)
continue
}
}
for checksum, pack := range processV1IndexManifests(ctx,
handle,
oref,
query,
index.Manifests,
) {
packs[checksum] = pack
total++
}
}
}
returnPacks:
var ret []pack.Package
for _, pack := range packs {
var title []string
for _, column := range pack.Columns() {
if len(column.Value) > 12 {
continue
}
title = append(title, column.Value)
}
log.G(ctx).
Infof("found %s (%s)", pack.String(), strings.Join(title, ", "))
ret = append(ret, pack)
}
log.G(ctx).Debugf("found %d/%d matching packages in oci catalog", len(packs), total)
return ret, nil
}
// SetSources implements packmanager.PackageManager
func (manager *ociManager) SetSources(_ context.Context, sources ...string) error {
manager.registries = sources
return nil
}
// AddSource implements packmanager.PackageManager
func (manager *ociManager) AddSource(ctx context.Context, source string) error {
if manager.registries == nil {
manager.registries = make([]string, 0)
}
manager.registries = append(manager.registries, source)
return nil
}
// Delete implements packmanager.PackageManager.
func (manager *ociManager) Delete(ctx context.Context, qopts ...packmanager.QueryOption) error {
packs, err := manager.Catalog(ctx, qopts...)
if err != nil {
return err
}
var errs []error
for _, pack := range packs {
if err := pack.Delete(ctx); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
// RemoveSource implements packmanager.PackageManager
func (manager *ociManager) RemoveSource(ctx context.Context, source string) error {
for i, needle := range manager.registries {
if needle == source {
ret := make([]string, 0)
ret = append(ret, manager.registries[:i]...)
manager.registries = append(ret, manager.registries[i+1:]...)
break
}
}
return nil
}
// IsCompatible implements packmanager.PackageManager
func (manager *ociManager) IsCompatible(ctx context.Context, source string, qopts ...packmanager.QueryOption) (packmanager.PackageManager, bool, error) {
ctx, handle, err := manager.handle(ctx)
if err != nil {
return nil, false, err
}
isFullyQualifiedNameReference := func(source string) bool {
_, err := name.ParseReference(source)
if err != nil && errors.Is(err, &name.ErrBadName{}) {
return false
}
return true
}
query := packmanager.NewQuery(qopts...)
// Check if the provided source is a fully qualified OCI reference
isLocalImage := func(source string) bool {
// First try without known registries
if _, err := handle.ResolveIndex(ctx, source); err == nil {
return true
}
// Now try with known registries
for _, registry := range manager.registries {
ref, err := name.ParseReference(source,
name.WithDefaultRegistry(registry),
name.WithDefaultTag(DefaultTag),
)
if err != nil {
continue
}
if _, err := handle.ResolveIndex(ctx, ref.Context().String()); err == nil {
return true
}
}
return false
}
// Check if the provided source an OCI Distrubtion Spec capable registry
isRegistry := func(source string) bool {
log.G(ctx).
WithField("source", source).
Tracef("checking if source is registry")
regName, err := name.NewRegistry(source)
if err != nil {
return false
}
if _, err := transport.Ping(ctx, regName, http.DefaultTransport.(*http.Transport).Clone()); err == nil {
return true
}
return false
}
// Check if the provided source is OCI registry
isRemoteImage := func(source string) bool {
log.G(ctx).
WithField("source", source).
Tracef("checking if source is remote image")
ref, err := name.ParseReference(source,
name.WithDefaultRegistry(DefaultRegistry),
name.WithDefaultTag(DefaultTag),
)
if err != nil {
return false
}
source = fmt.Sprintf("%s:%s", ref.Context().String(), ref.Identifier())
// log.G(ctx).WithField("source", source).Debug("checking if source is registry")
opts := []crane.Option{
crane.WithContext(ctx),
crane.WithUserAgent(version.UserAgent()),
crane.WithPlatform(&v1.Platform{
OS: query.Platform(),
OSFeatures: query.KConfig(),
Architecture: query.Architecture(),
}),
}
if auth, ok := config.G[config.KraftKit](ctx).Auth[ref.Context().Registry.RegistryStr()]; ok {
// We split up the options for authenticating and the option for
// "verifying ssl" such that a user can simply disable secure connection
// to a registry if desired.
if auth.User != "" && auth.Token != "" {
log.G(ctx).
WithField("registry", source).
Debug("authenticating")
opts = append(opts,
crane.WithAuth(authn.FromConfig(authn.AuthConfig{
Username: auth.User,
Password: auth.Token,
})),
)
}
if !auth.VerifySSL {
rt := http.DefaultTransport.(*http.Transport).Clone()
rt.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
opts = append(opts,
crane.Insecure,
crane.WithTransport(rt),
)
}
}
desc, err := crane.Head(source, opts...)
if err == nil && desc != nil {
return true
}
log.G(ctx).WithField("source", source).Trace(err)
return false
}
checks := []func(string) bool{
isLocalImage,
isFullyQualifiedNameReference,
}
if query.Remote() {
checks = append(checks,
isRegistry,
isRemoteImage,
)
}
for _, check := range checks {
if check(source) {
return manager, true, nil
}
}
return nil, false, nil
}
// From implements packmanager.PackageManager
func (manager *ociManager) From(pack.PackageFormat) (packmanager.PackageManager, error) {
return nil, fmt.Errorf("not possible: oci.manager.From")
}
// Format implements packmanager.PackageManager
func (manager *ociManager) Format() pack.PackageFormat {
return OCIFormat
}