diff --git a/config.go b/config.go new file mode 100644 index 0000000..8e04892 --- /dev/null +++ b/config.go @@ -0,0 +1,42 @@ +// Copyright 2022 go-imageinspect authors +// +// 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. + +package imageinspect + +import ( + "context" + "encoding/json" + + "github.com/containerd/containerd/content" + "github.com/containerd/containerd/remotes" + ocispecs "github.com/opencontainers/image-spec/specs-go/v1" +) + +type Config struct { + ocispecs.Image +} + +func (l *Loader) scanConfig(ctx context.Context, fetcher remotes.Fetcher, desc ocispecs.Descriptor, img *Image) error { + _, err := remotes.FetchHandler(l.cache, fetcher)(ctx, desc) + if err != nil { + return err + } + + dt, err := content.ReadBlob(ctx, l.cache, desc) + if err != nil { + return err + } + + return json.Unmarshal(dt, &img.Config) +} diff --git a/load.go b/load.go index b103814..620a810 100644 --- a/load.go +++ b/load.go @@ -173,6 +173,10 @@ func (l *Loader) Load(ctx context.Context, ref string) (*Result, error) { img.Title = title } + if err := l.scanConfig(ctx, fetcher, mfst.manifest.Config, &img); err != nil { + return nil, err + } + refs, ok := r.refs[dgst] if ok { if err := l.scanSBOM(ctx, fetcher, r, dgst, refs, &img); err != nil { diff --git a/load_test.go b/load_test.go index b1bea6a..a3a1ace 100644 --- a/load_test.go +++ b/load_test.go @@ -70,6 +70,7 @@ func TestSingleArchManifest(t *testing.T) { require.Equal(t, int64(300), img.Size) require.Equal(t, "linux/arm64", img.Platform) + require.NotNil(t, img.Config) } func TestMultiArchManifest(t *testing.T) { @@ -146,12 +147,14 @@ func TestMultiArchManifest(t *testing.T) { require.Equal(t, int64(25), img.Size) require.Equal(t, "linux/arm64", img.Platform) + require.NotNil(t, img.Config) img, ok = r.Images["linux/amd64"] require.True(t, ok) require.Equal(t, int64(50), img.Size) require.Equal(t, "linux/amd64", img.Platform) + require.NotNil(t, img.Config) } func TestTitle(t *testing.T) { diff --git a/types.go b/types.go index 159b1a1..c1642aa 100644 --- a/types.go +++ b/types.go @@ -58,6 +58,7 @@ type Image struct { Size int64 Signatures []Signature + Config *Config `json:",omitempty"` SBOM *SBOM `json:",omitempty"` Provenance *Provenance `json:",omitempty"`