Skip to content

Commit

Permalink
ociregistry/ociref: add IsValidHost
Browse files Browse the repository at this point in the history
This makes it easier for users to check that a bare hostname
is valid.

Also make it clearer in the docs that "host" also includes "host:port".

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I6308a4c30212addf270be3313237c8f2f6c5391c
Reviewed-on: https://review.gerrithub.io/c/cue-labs/oci/+/1170299
TryBot-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
rogpeppe committed Oct 4, 2023
1 parent ac40565 commit 2c3ad8a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
17 changes: 12 additions & 5 deletions ociregistry/ociref/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ var referencePat = regexp.MustCompile(
`)$`,
)

var hostPat = regexp.MustCompile(`^(?:` + domainAndPort + `)$`)

// Reference represents an entry in an OCI repository.
type Reference struct {
// Host holds the host name of the registry
// within which the repository is stored. This might
// be empty.
// within which the repository is stored, optionally in
// the form host:port. This might be empty.
Host string

// Repository holds the repository name.
Expand All @@ -120,10 +122,15 @@ type Reference struct {
Digest ociregistry.Digest
}

// IsValidHost reports whether s is a valid host (or host:port) part of a reference string.
func IsValidHost(s string) bool {
return hostPat.MatchString(s)
}

// Parse parses a reference string that must include
// a host name component.
// a host name (or host:port pair) component.
//
// It is represented in string form as HOST/NAME[:TAG|@DIGEST]
// It is represented in string form as HOST[:PORT]/NAME[:TAG|@DIGEST]
// form: the same syntax accepted by "docker pull".
// Unlike "docker pull" however, there is no default registry: when
// presented with a bare repository name, Parse will return an error.
Expand All @@ -141,7 +148,7 @@ func Parse(refStr string) (Reference, error) {
// ParseRelative parses a reference string that may
// or may not include a host name component.
//
// It is represented in string form as [HOST/]NAME[:TAG|@DIGEST]
// It is represented in string form as [HOST[:PORT]/]NAME[:TAG|@DIGEST]
// form: the same syntax accepted by "docker pull".
// Unlike "docker pull" however, there is no default registry: when
// presented with a bare repository name, the Host field will be empty.
Expand Down
37 changes: 37 additions & 0 deletions ociregistry/ociref/reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,40 @@ func TestParseReference(t *testing.T) {
})
}
}

var isValidHostTests = []struct {
host string
want bool
}{{
host: "foo.com:5000",
want: true,
}, {
host: "foo.com",
want: true,
}, {
host: "localhost:1234",
want: true,
}, {
host: "localhost",
want: false,
}, {
host: "foo",
want: false,
}, {
host: "foo..com",
want: false,
}, {
host: "[::1]",
want: true,
}, {
host: "[::1]:3456",
want: true,
}}

func TestIsValidHost(t *testing.T) {
for _, test := range isValidHostTests {
t.Run(test.host, func(t *testing.T) {
qt.Assert(t, qt.Equals(IsValidHost(test.host), test.want))
})
}
}

0 comments on commit 2c3ad8a

Please sign in to comment.