-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add override for proxy_intercept_errors when using Custom HTTP Errors (…
…#9497) * added proxy-intercept-errors config option * fixed error when comparing locations * fixed missing location config from annotation added e2e test * reversed logic for proxy-intercept-errors to disable-proxy-intercept-errors * reversed logic to disable-proxy-intercept-errors * reversed logic * default has to be false * put comment in same line as return * run gofmt * fixing wrong Boilerplate header * updated code to new IngressAnnotation interface * fixes to satisfy PR comments * synced with upstream; fixed typo * gofumpt disableproxyintercepterrors.go * gofumpt
- Loading branch information
Showing
12 changed files
with
395 additions
and
108 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
76 changes: 76 additions & 0 deletions
76
internal/ingress/annotations/disableproxyintercepterrors/main.go
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,76 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package disableproxyintercepterrors | ||
|
||
import ( | ||
networking "k8s.io/api/networking/v1" | ||
|
||
"k8s.io/ingress-nginx/internal/ingress/annotations/parser" | ||
"k8s.io/ingress-nginx/internal/ingress/errors" | ||
"k8s.io/ingress-nginx/internal/ingress/resolver" | ||
) | ||
|
||
const ( | ||
disableProxyInterceptErrorsAnnotation = "disable-proxy-intercept-errors" | ||
) | ||
|
||
var disableProxyInterceptErrorsAnnotations = parser.Annotation{ | ||
Group: "backend", | ||
Annotations: parser.AnnotationFields{ | ||
disableProxyInterceptErrorsAnnotation: { | ||
Validator: parser.ValidateBool, | ||
Scope: parser.AnnotationScopeLocation, | ||
Risk: parser.AnnotationRiskLow, | ||
Documentation: `This annotation allows to disable NGINX proxy-intercept-errors when custom-http-errors are set. | ||
If a default backend annotation is specified on the ingress, the errors will be routed to that annotation's default backend service (instead of the global default backend). | ||
Different ingresses can specify different sets of errors codes and there are UseCases where NGINX shall not intercept all errors returned from upstream.`, | ||
}, | ||
}, | ||
} | ||
|
||
type disableProxyInterceptErrors struct { | ||
r resolver.Resolver | ||
annotationConfig parser.Annotation | ||
} | ||
|
||
func (pie disableProxyInterceptErrors) GetDocumentation() parser.AnnotationFields { | ||
return pie.annotationConfig.Annotations | ||
} | ||
|
||
func (pie disableProxyInterceptErrors) Validate(anns map[string]string) error { | ||
maxrisk := parser.StringRiskToRisk(pie.r.GetSecurityConfiguration().AnnotationsRiskLevel) | ||
return parser.CheckAnnotationRisk(anns, maxrisk, disableProxyInterceptErrorsAnnotations.Annotations) | ||
} | ||
|
||
// NewParser creates a new disableProxyInterceptErrors annotation parser | ||
func NewParser(r resolver.Resolver) parser.IngressAnnotation { | ||
return disableProxyInterceptErrors{ | ||
r: r, | ||
annotationConfig: disableProxyInterceptErrorsAnnotations, | ||
} | ||
} | ||
|
||
func (pie disableProxyInterceptErrors) Parse(ing *networking.Ingress) (interface{}, error) { | ||
val, err := parser.GetBoolAnnotation(disableProxyInterceptErrorsAnnotation, ing, pie.annotationConfig.Annotations) | ||
|
||
// A missing annotation is not a problem, just use the default | ||
if err == errors.ErrMissingAnnotations { | ||
return false, nil // default is false | ||
} | ||
|
||
return val, nil | ||
} |
64 changes: 64 additions & 0 deletions
64
internal/ingress/annotations/disableproxyintercepterrors/main_test.go
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,64 @@ | ||
/* | ||
Copyright 2022 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package disableproxyintercepterrors | ||
|
||
import ( | ||
"testing" | ||
|
||
api "k8s.io/api/core/v1" | ||
networking "k8s.io/api/networking/v1" | ||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/ingress-nginx/internal/ingress/annotations/parser" | ||
"k8s.io/ingress-nginx/internal/ingress/resolver" | ||
) | ||
|
||
func buildIngress() *networking.Ingress { | ||
return &networking.Ingress{ | ||
ObjectMeta: meta_v1.ObjectMeta{ | ||
Name: "foo", | ||
Namespace: api.NamespaceDefault, | ||
}, | ||
Spec: networking.IngressSpec{ | ||
DefaultBackend: &networking.IngressBackend{ | ||
Service: &networking.IngressServiceBackend{ | ||
Name: "default-backend", | ||
Port: networking.ServiceBackendPort{ | ||
Number: 80, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func TestParseAnnotations(t *testing.T) { | ||
ing := buildIngress() | ||
|
||
_, err := NewParser(&resolver.Mock{}).Parse(ing) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
|
||
data := map[string]string{} | ||
data[parser.GetAnnotationWithPrefix("disable-proxy-intercept-errors")] = "true" | ||
ing.SetAnnotations(data) | ||
// test ingress using the annotation without a TLS section | ||
_, err = NewParser(&resolver.Mock{}).Parse(ing) | ||
if err != nil { | ||
t.Errorf("unexpected error parsing ingress with disable-proxy-intercept-errors") | ||
} | ||
} |
Oops, something went wrong.