Skip to content

Commit

Permalink
Add validation for proxy settings
Browse files Browse the repository at this point in the history
  • Loading branch information
dergeberl committed Oct 16, 2024
1 parent 73497d1 commit bfc4f10
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/apis/registry/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ func validateRegistryCache(cache registry.RegistryCache, fldPath *field.Path) fi
allErrs = append(allErrs, field.Invalid(fldPath.Child("garbageCollection").Child("ttl"), ttl.Duration.String(), "ttl must be a non-negative duration"))
}
}
if cache.Proxy != nil {
if cache.Proxy.HTTPProxy != nil {
allErrs = append(allErrs, ValidateURL(fldPath.Child("proxy").Child("httpProxy"), *cache.Proxy.HTTPProxy)...)
}
if cache.Proxy.HTTPSProxy != nil {
allErrs = append(allErrs, ValidateURL(fldPath.Child("proxy").Child("httpsProxy"), *cache.Proxy.HTTPSProxy)...)
}
if cache.Proxy.NoProxy != nil && cache.Proxy.HTTPProxy == nil && cache.Proxy.HTTPSProxy == nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("proxy").Child("noProxy"), *cache.Proxy.NoProxy, "noProxy can only be set if HTTPProxy and/or HTTPSProxy is set."))
}
}

return allErrs
}
Expand Down
48 changes: 48 additions & 0 deletions pkg/apis/registry/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ var _ = Describe("Validation", func() {
api.RegistryCache{
Upstream: "my-registry.io:5000",
RemoteURL: ptr.To("http://my-registry.io:5000"),
Proxy: &api.Proxy{
HTTPProxy: ptr.To("http://127.0.0.1"),
HTTPSProxy: ptr.To("https://127.0.0.1:1234"),
NoProxy: ptr.To("127.0.0.1,127.0.0.2"),
},
},
api.RegistryCache{
Upstream: "quay.io",
Expand Down Expand Up @@ -224,6 +229,49 @@ var _ = Describe("Validation", func() {
})),
))
})

It("should deny invalid proxy config", func() {
registryConfig.Caches[0].Proxy = &api.Proxy{
HTTPProxy: ptr.To("10.10.10.10"),
HTTPSProxy: nil,
NoProxy: nil,
}
registryConfig.Caches = append(registryConfig.Caches,
api.RegistryCache{
Upstream: "my-registry.io",
Proxy: &api.Proxy{
HTTPProxy: nil,
HTTPSProxy: ptr.To("http://foo!bar"),
NoProxy: nil,
},
},
api.RegistryCache{
Upstream: "my-registry2.io",
Proxy: &api.Proxy{
HTTPProxy: nil,
HTTPSProxy: nil,
NoProxy: ptr.To("127.0.0.1"),
},
},
)
Expect(ValidateRegistryConfig(registryConfig, fldPath)).To(ConsistOf(
PointTo(MatchFields(IgnoreExtras, Fields{
"Type": Equal(field.ErrorTypeInvalid),
"Field": Equal("providerConfig.caches[0].proxy.httpProxy"),
"BadValue": Equal("10.10.10.10"),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"Type": Equal(field.ErrorTypeInvalid),
"Field": Equal("providerConfig.caches[1].proxy.httpsProxy"),
"BadValue": Equal("http://foo!bar"),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"Type": Equal(field.ErrorTypeInvalid),
"Field": Equal("providerConfig.caches[2].proxy.noProxy"),
"BadValue": Equal("127.0.0.1"),
})),
))
})
})

Describe("#ValidateRegistryConfigUpdate", func() {
Expand Down

0 comments on commit bfc4f10

Please sign in to comment.