Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Fix OCIString parsing of local docker images #556

Merged
merged 1 commit into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions pkg/apis/meta/v1alpha1/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"net/url"
"strings"

// "github.com/opencontainers/go-digest" requires us to load the algorithms
// that we want to use into the binary. (it calls algorithm.Available)
_ "crypto/sha256"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got this from @stealthybox 's branch https://github.com/stealthybox/ignite/blob/fix-oci-ref/pkg/apis/meta/v1alpha1/image.go#L9-L11 . Without this, the test run fails with "unsupported digest algorithm" error.


"github.com/containers/image/docker/reference"
"github.com/opencontainers/go-digest"
)
Expand Down Expand Up @@ -111,12 +115,20 @@ var _ json.Marshaler = &OCIContentID{}
var _ json.Unmarshaler = &OCIContentID{}

func parseOCIString(s string) (*OCIContentID, error) {
// Check if it's a local docker image.
// Example: docker://sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
if strings.HasPrefix(s, ociSchemeLocal) {
return ParseOCIContentID(strings.TrimPrefix(s, ociSchemeLocal))
}

// For full repo digest with repo name, url parse the string and obtain the
// url components.
u, err := url.Parse(s)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to parse oci string %q, err: %v", s, err)
}

// Remove the "docker://" or "oci://" scheme by only caring about the host and path
// Remove the "oci://" scheme by only caring about the host and path.
return ParseOCIContentID(u.Host + u.Path)
}

Expand Down
45 changes: 45 additions & 0 deletions pkg/apis/meta/v1alpha1/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,48 @@ func TestNewOCIImageRef(t *testing.T) {
}
}
}

func TestParseOCIString(t *testing.T) {
tests := []struct {
name string
in string
out OCIContentID
err bool
}{
{
name: "oci string with repo name",
in: "oci://docker.io/library/hello-world@sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e",
out: OCIContentID{
repoName: "docker.io/library/hello-world",
digest: "sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e",
},
},
{
name: "oci string local docker sha",
in: "docker://sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e",
out: OCIContentID{
digest: "sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e",
},
},
{
name: "invalid oci string",
in: "sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e",
err: true,
},
}

for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
actual, err := parseOCIString(rt.in)
if (err != nil) != rt.err {
t.Errorf("expected error %t, actual: %v", rt.err, err)
}

if !rt.err {
if actual.String() != rt.out.String() {
t.Errorf("expected %q, actual: %q", rt.out.String(), actual.String())
}
}
})
}
}