mirror of https://github.com/knative/func.git
Trust loopback builders (#2750)
Signed-off-by: Matej Vašek <mvasek@redhat.com>
This commit is contained in:
parent
24a7fedadd
commit
525761a199
|
@ -8,6 +8,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -242,6 +243,9 @@ func isPodmanV43(ctx context.Context, cli client.CommonAPIClient) (b bool, err e
|
||||||
// TrustBuilder determines whether the builder image should be trusted
|
// TrustBuilder determines whether the builder image should be trusted
|
||||||
// based on a set of trusted builder image registry prefixes.
|
// based on a set of trusted builder image registry prefixes.
|
||||||
func TrustBuilder(b string) bool {
|
func TrustBuilder(b string) bool {
|
||||||
|
if isLocalhost(b) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
for _, v := range trustedBuilderImagePrefixes {
|
for _, v := range trustedBuilderImagePrefixes {
|
||||||
// Ensure that all entries in this list are terminated with a trailing "/"
|
// Ensure that all entries in this list are terminated with a trailing "/"
|
||||||
if !strings.HasSuffix(v, "/") {
|
if !strings.HasSuffix(v, "/") {
|
||||||
|
@ -254,6 +258,14 @@ func TrustBuilder(b string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isLocalhost(img string) bool {
|
||||||
|
// Parsing logic is broken for localhost in go-containerregistry.
|
||||||
|
// See: https://github.com/google/go-containerregistry/issues/2048
|
||||||
|
// So I went for regex.
|
||||||
|
localhostRE := regexp.MustCompile(`^(localhost|127\.0\.0\.1|\[::1\])(:\d+)?/.+$`)
|
||||||
|
return localhostRE.MatchString(img)
|
||||||
|
}
|
||||||
|
|
||||||
// Builder Image chooses the correct builder image or defaults.
|
// Builder Image chooses the correct builder image or defaults.
|
||||||
func BuilderImage(f fn.Function, builderName string) (string, error) {
|
func BuilderImage(f fn.Function, builderName string) (string, error) {
|
||||||
return builders.Image(f, builderName, DefaultBuilderImages)
|
return builders.Image(f, builderName, DefaultBuilderImages)
|
||||||
|
|
|
@ -40,6 +40,22 @@ func TestBuild_BuilderImageTrusted(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuild_BuilderImageTrustedLocalhost(t *testing.T) {
|
||||||
|
for _, reg := range []string{
|
||||||
|
"localhost",
|
||||||
|
"localhost:5000",
|
||||||
|
"127.0.0.1",
|
||||||
|
"127.0.0.1:5000",
|
||||||
|
"[::1]",
|
||||||
|
"[::1]:5000"} {
|
||||||
|
t.Run(reg, func(t *testing.T) {
|
||||||
|
if !TrustBuilder(reg + "/project/builder:latest") {
|
||||||
|
t.Errorf("expected to be trusted: %q", reg)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestBuild_BuilderImageDefault ensures that a Function bing built which does not
|
// TestBuild_BuilderImageDefault ensures that a Function bing built which does not
|
||||||
// define a Builder Image will get the internally-defined default.
|
// define a Builder Image will get the internally-defined default.
|
||||||
func TestBuild_BuilderImageDefault(t *testing.T) {
|
func TestBuild_BuilderImageDefault(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue