Skip to content

Commit 93fde49

Browse files
committed
fix: detect insecure registry when checking push permissions
1 parent 92a92a9 commit 93fde49

2 files changed

Lines changed: 80 additions & 11 deletions

File tree

pkg/devspace/build/registry/util.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ func HasPushPermission(image *latest.Image) bool {
3232
}
3333

3434
pushErr := remote.CheckPushPermission(ref, authn.DefaultKeychain, http.DefaultTransport)
35+
36+
if isInsecureRegistry(pushErr) {
37+
// Retry with insecure registry
38+
ref, err := name.ParseReference(image.Image, name.Insecure)
39+
if err != nil {
40+
panic(err)
41+
}
42+
43+
pushErr = remote.CheckPushPermission(ref, authn.DefaultKeychain, http.DefaultTransport)
44+
}
45+
3546
return pushErr == nil
3647
}
3748

@@ -161,11 +172,6 @@ func UseLocalRegistry(client kubectl.Client, config *latest.Config, imageConfig
161172
} else if imageConfig.BuildKit != nil && imageConfig.BuildKit.InCluster != nil {
162173
return false
163174
}
164-
165-
imageWithoutPort := strings.Split(imageConfig.Image, ":")[0]
166-
if imageWithoutPort == "" || imageWithoutPort == "localhost" || imageWithoutPort == "127.0.0.1" || strings.HasSuffix(imageWithoutPort, ".local") || strings.HasSuffix(imageWithoutPort, ".localhost") {
167-
return false
168-
}
169175
}
170176

171177
// check if fallback
@@ -181,3 +187,11 @@ func UseLocalRegistry(client kubectl.Client, config *latest.Config, imageConfig
181187
isLocalKubernetes := kubectl.IsLocalKubernetes(context)
182188
return !isLocalKubernetes && !(isVClusterContext && isLocalKubernetes)
183189
}
190+
191+
func isInsecureRegistry(err error) bool {
192+
if err == nil {
193+
return false
194+
}
195+
196+
return strings.Contains(err.Error(), "http: server gave HTTP response to HTTPS client")
197+
}

pkg/devspace/build/registry/util_test.go

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import (
1111
)
1212

1313
type useLocalRegistryTestCase struct {
14-
name string
15-
client kubectl.Client
16-
config *latest.Config
17-
skipPush bool
18-
expected bool
14+
name string
15+
client kubectl.Client
16+
config *latest.Config
17+
imageConfig *latest.Image
18+
skipPush bool
19+
expected bool
1920
}
2021

2122
func TestUseLocalRegistry(t *testing.T) {
@@ -304,10 +305,64 @@ func TestUseLocalRegistry(t *testing.T) {
304305
},
305306
expected: false,
306307
},
308+
{
309+
name: "Remote Cluster BuildKit In Cluster Build Config",
310+
client: &kubectltesting.Client{
311+
Context: "arn:aws:eks:us-west-2:1234567890:cluster/remote-eks",
312+
},
313+
config: &latest.Config{
314+
LocalRegistry: nil,
315+
},
316+
imageConfig: &latest.Image{
317+
BuildKit: &latest.BuildKitConfig{
318+
InCluster: &latest.BuildKitInClusterConfig{},
319+
},
320+
},
321+
expected: false,
322+
},
323+
{
324+
name: "Remote Cluster BuildKit Build Config",
325+
client: &kubectltesting.Client{
326+
Context: "arn:aws:eks:us-west-2:1234567890:cluster/remote-eks",
327+
},
328+
config: &latest.Config{
329+
LocalRegistry: nil,
330+
},
331+
imageConfig: &latest.Image{
332+
BuildKit: &latest.BuildKitConfig{},
333+
},
334+
expected: true,
335+
},
336+
{
337+
name: "Remote Cluster Kaniko Build Config",
338+
client: &kubectltesting.Client{
339+
Context: "arn:aws:eks:us-west-2:1234567890:cluster/remote-eks",
340+
},
341+
config: &latest.Config{
342+
LocalRegistry: nil,
343+
},
344+
imageConfig: &latest.Image{
345+
Kaniko: &latest.KanikoConfig{},
346+
},
347+
expected: false,
348+
},
349+
{
350+
name: "Remote Cluster Custom Build Config",
351+
client: &kubectltesting.Client{
352+
Context: "arn:aws:eks:us-west-2:1234567890:cluster/remote-eks",
353+
},
354+
config: &latest.Config{
355+
LocalRegistry: nil,
356+
},
357+
imageConfig: &latest.Image{
358+
Custom: &latest.CustomConfig{},
359+
},
360+
expected: false,
361+
},
307362
}
308363

309364
for _, testCase := range testCases {
310-
actual := UseLocalRegistry(testCase.client, testCase.config, nil, nil, testCase.skipPush)
365+
actual := UseLocalRegistry(testCase.client, testCase.config, testCase.imageConfig, nil, testCase.skipPush)
311366
assert.Equal(t, actual, testCase.expected, "Unexpected result in test case %s", testCase.name)
312367
}
313368
}

0 commit comments

Comments
 (0)