-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix interacting with insecure HTTPS registries
The Docker daemon allows to interact with insecure registries served through plain HTTP or served through HTTPS with self-signed certificates, when the target registry is included inside "insecureRegistries". In this library it should be possible to interact with insecure registries likewise by using the "name.Insecure" option when creating references. Nonetheless it's currently not possible to interact with insecure registries served with HTTPS and self-signed certificates, since the TLS certificate is checked anyway and an "invalid certificate" error is returned. A common workaround consists into passing a tls.Config with InsecureSkipVerify set to true, but this disables TLS validation for every HTTP request, while the desired behavior is disabling TLS validation only when "name.Insecure" is in use. This patch changes the default "remote" options in order to provide a default tls.Config with InsecureSkipVerify set to true if and only if "name.Insecure" is in use. This also fixes bugs in dependent tools like Skaffold, that are using "name.Insecure", are not using InsecureSKipVerify and are expecting to be able to interact with insecure registries anyway.
- Loading branch information
Showing
13 changed files
with
146 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package remote | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
) | ||
|
||
func TestOptionsInsecure(t *testing.T) { | ||
for _, targetType := range []string{ | ||
"registry", | ||
"repository", | ||
"digest", | ||
} { | ||
for _, mode := range []string{ | ||
"secure", | ||
"insecure", | ||
} { | ||
t.Run(targetType+"_"+mode, func(t *testing.T) { | ||
server := httptest.NewTLSServer( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
defer server.Close() | ||
|
||
u, err := url.Parse(server.URL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
options := []name.Option{} | ||
|
||
if mode == "insecure" { | ||
options = append(options, name.Insecure) | ||
} | ||
|
||
var target resource | ||
|
||
switch targetType { | ||
case "registry": | ||
reg, err := name.NewRegistry("myregistry", options...) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
target = reg | ||
|
||
case "repository": | ||
ref, err := name.ParseReference("myregistry/name:tag", options...) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
target = ref.Context() | ||
|
||
case "digest": | ||
d, err := name.NewDigest("myregistry/name@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", options...) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
target = d | ||
} | ||
|
||
opts, err := makeOptions(target, []Option{}...) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
c := &http.Client{Transport: opts.transport} | ||
|
||
res, err := c.Get(u.String()) | ||
|
||
if mode == "secure" { | ||
if ue, ok := err.(*url.Error); !ok { | ||
t.Fatal(err) | ||
} else if _, ok := ue.Err.(*tls.CertificateVerificationError); !ok { | ||
t.Fatal(err) | ||
} | ||
} else { | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.StatusCode != http.StatusOK { | ||
t.Fatal(fmt.Printf("unexpected status code: %d", res.StatusCode)) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters