Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement object deep inspector #8456

Merged
merged 1 commit into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/nginx/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ Takes the form "<host>:port". If not provided, no admission controller is starte
shutdownGracePeriod = flags.Int("shutdown-grace-period", 0, "Seconds to wait after receiving the shutdown signal, before stopping the nginx process.")

postShutdownGracePeriod = flags.Int("post-shutdown-grace-period", 10, "Seconds to wait after the nginx process has stopped before controller exits.")

deepInspector = flags.Bool("deep-inspect", true, "Enables ingress object security deep inspector")
)

flags.StringVar(&nginx.MaxmindMirror, "maxmind-mirror", "", `Maxmind mirror url (example: http://geoip.local/databases`)
Expand Down Expand Up @@ -321,6 +323,7 @@ https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-g
UDPConfigMapName: *udpConfigMapName,
DisableFullValidationTest: *disableFullValidationTest,
DefaultSSLCertificate: *defSSLCertificate,
DeepInspector: *deepInspector,
PublishService: *publishSvc,
PublishStatusAddress: *publishStatusAddress,
UpdateStatusOnShutdown: *updateStatusOnShutdown,
Expand Down
1 change: 1 addition & 0 deletions docs/user-guide/cli-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ They are set in the container spec of the `ingress-nginx-controller` Deployment
| `--apiserver-host` | Address of the Kubernetes API server. Takes the form "protocol://address:port". If not specified, it is assumed the program runs inside a Kubernetes cluster and local discovery is attempted. |
| `--certificate-authority` | Path to a cert file for the certificate authority. This certificate is used only when the flag --apiserver-host is specified. |
| `--configmap` | Name of the ConfigMap containing custom global configurations for the controller. |
| `--deep-inspect` | Enables ingress object security deep inspector. (default true) |
| `--default-backend-service` | Service used to serve HTTP requests not matching any known server name (catch-all). Takes the form "namespace/name". The controller configures NGINX to forward requests to the first port of this Service. |
| `--default-server-port` | Port to use for exposing the default server (catch-all). (default 8181) |
| `--default-ssl-certificate` | Secret containing a SSL certificate to be used by the default HTTPS server (catch-all). Takes the form "namespace/name". |
Expand Down
8 changes: 7 additions & 1 deletion internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"k8s.io/ingress-nginx/internal/ingress/controller/ingressclass"
"k8s.io/ingress-nginx/internal/ingress/controller/store"
"k8s.io/ingress-nginx/internal/ingress/errors"
"k8s.io/ingress-nginx/internal/ingress/inspector"
"k8s.io/ingress-nginx/internal/ingress/metric/collectors"
"k8s.io/ingress-nginx/internal/k8s"
"k8s.io/ingress-nginx/internal/nginx"
Expand Down Expand Up @@ -123,6 +124,7 @@ type Configuration struct {

InternalLoggerAddress string
IsChroot bool
DeepInspector bool
}

// GetPublishService returns the Service used to set the load-balancer status of Ingresses.
Expand Down Expand Up @@ -237,7 +239,11 @@ func (n *NGINXController) CheckIngress(ing *networking.Ingress) error {
if !ing.DeletionTimestamp.IsZero() {
return nil
}

if n.cfg.DeepInspector {
if err := inspector.DeepInspect(ing); err != nil {
return fmt.Errorf("invalid object: %w", err)
}
}
// Do not attempt to validate an ingress that's not meant to be controlled by the current instance of the controller.
if ingressClass, err := n.store.GetIngressClass(ing, n.cfg.IngressClassConfiguration); ingressClass == "" {
klog.Warningf("ignoring ingress %v in %v based on annotation %v: %v", ing.Name, ing.ObjectMeta.Namespace, ingressClass, err)
Expand Down
2 changes: 2 additions & 0 deletions internal/ingress/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2396,6 +2396,7 @@ func newNGINXController(t *testing.T) *NGINXController {
clientSet,
channels.NewRingChannel(10),
false,
true,
&ingressclass.IngressClassConfiguration{
Controller: "k8s.io/ingress-nginx",
AnnotationValue: "nginx",
Expand Down Expand Up @@ -2460,6 +2461,7 @@ func newDynamicNginxController(t *testing.T, setConfigMap func(string) *v1.Confi
clientSet,
channels.NewRingChannel(10),
false,
true,
&ingressclass.IngressClassConfiguration{
Controller: "k8s.io/ingress-nginx",
AnnotationValue: "nginx",
Expand Down
1 change: 1 addition & 0 deletions internal/ingress/controller/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func NewNGINXController(config *Configuration, mc metric.Collector) *NGINXContro
config.Client,
n.updateCh,
config.DisableCatchAll,
config.DeepInspector,
config.IngressClassConfiguration)

n.syncQueue = task.NewTaskQueue(n.syncIngress)
Expand Down
15 changes: 15 additions & 0 deletions internal/ingress/controller/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
clientcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/ingress-nginx/internal/ingress/inspector"
"k8s.io/klog/v2"

"k8s.io/ingress-nginx/internal/file"
Expand Down Expand Up @@ -247,6 +248,7 @@ func New(
client clientset.Interface,
updateCh *channels.RingChannel,
disableCatchAll bool,
deepInspector bool,
icConfig *ingressclass.IngressClassConfiguration) Storer {

store := &k8sStore{
Expand Down Expand Up @@ -426,6 +428,12 @@ func New(

klog.InfoS("Found valid IngressClass", "ingress", klog.KObj(ing), "ingressclass", ic)

if deepInspector {
if err := inspector.DeepInspect(ing); err != nil {
klog.ErrorS(err, "received invalid ingress", "ingress", klog.KObj(ing))
return
}
}
if hasCatchAllIngressRule(ing.Spec) && disableCatchAll {
klog.InfoS("Ignoring add for catch-all ingress because of --disable-catch-all", "ingress", klog.KObj(ing))
return
Expand Down Expand Up @@ -482,6 +490,13 @@ func New(
return
}

if deepInspector {
if err := inspector.DeepInspect(curIng); err != nil {
klog.ErrorS(err, "received invalid ingress", "ingress", klog.KObj(curIng))
return
}
}

store.syncIngress(curIng)
store.updateSecretIngressMap(curIng)
store.syncSecrets(curIng)
Expand Down
11 changes: 11 additions & 0 deletions internal/ingress/controller/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)

storer.Run(stopCh)
Expand Down Expand Up @@ -204,6 +205,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)

storer.Run(stopCh)
Expand Down Expand Up @@ -307,6 +309,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)

storer.Run(stopCh)
Expand Down Expand Up @@ -422,6 +425,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
ingressClassconfig)

storer.Run(stopCh)
Expand Down Expand Up @@ -551,6 +555,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
ingressClassconfig)

storer.Run(stopCh)
Expand Down Expand Up @@ -650,6 +655,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)

storer.Run(stopCh)
Expand Down Expand Up @@ -743,6 +749,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)

storer.Run(stopCh)
Expand Down Expand Up @@ -828,6 +835,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)

storer.Run(stopCh)
Expand Down Expand Up @@ -923,6 +931,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)

storer.Run(stopCh)
Expand Down Expand Up @@ -1046,6 +1055,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)

storer.Run(stopCh)
Expand Down Expand Up @@ -1166,6 +1176,7 @@ func TestStore(t *testing.T) {
clientSet,
updateCh,
false,
true,
DefaultClassConfig)

storer.Run(stopCh)
Expand Down
62 changes: 62 additions & 0 deletions internal/ingress/inspector/ingress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
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 inspector

import (
"fmt"

networking "k8s.io/api/networking/v1"
)

// InspectIngress is used to do the deep inspection of an ingress object, walking through all
// of the spec fields and checking for matching strings and configurations that may represent
// an attempt to escape configs
func InspectIngress(ingress *networking.Ingress) error {
for _, rule := range ingress.Spec.Rules {
if rule.Host != "" {
if err := CheckRegex(rule.Host); err != nil {
return fmt.Errorf("invalid host in ingress %s/%s: %s", ingress.Namespace, ingress.Name, err)
}
}
if rule.HTTP != nil {
if err := inspectIngressRule(rule.HTTP); err != nil {
return fmt.Errorf("invalid rule in ingress %s/%s: %s", ingress.Namespace, ingress.Name, err)
}
}
}

for _, tls := range ingress.Spec.TLS {
if err := CheckRegex(tls.SecretName); err != nil {
return fmt.Errorf("invalid secret in ingress %s/%s: %s", ingress.Namespace, ingress.Name, err)
}
for _, host := range tls.Hosts {
if err := CheckRegex(host); err != nil {
return fmt.Errorf("invalid host in ingress tls config %s/%s: %s", ingress.Namespace, ingress.Name, err)
}
}
}
return nil
}

func inspectIngressRule(httprule *networking.HTTPIngressRuleValue) error {
for _, path := range httprule.Paths {
if err := CheckRegex(path.Path); err != nil {
return fmt.Errorf("invalid http path: %s", err)
}
}
return nil
}
99 changes: 99 additions & 0 deletions internal/ingress/inspector/ingress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
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 inspector

import (
"testing"

networking "k8s.io/api/networking/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func makeSimpleIngress(hostname string, paths ...string) *networking.Ingress {

newIngress := networking.Ingress{
ObjectMeta: v1.ObjectMeta{
Name: "test1",
Namespace: "default",
},
Spec: networking.IngressSpec{
Rules: []networking.IngressRule{
{
Host: hostname,
IngressRuleValue: networking.IngressRuleValue{
HTTP: &networking.HTTPIngressRuleValue{
Paths: []networking.HTTPIngressPath{},
},
},
},
},
},
}

prefix := networking.PathTypePrefix
for _, path := range paths {
newPath := networking.HTTPIngressPath{
Path: path,
PathType: &prefix,
}
newIngress.Spec.Rules[0].IngressRuleValue.HTTP.Paths = append(newIngress.Spec.Rules[0].IngressRuleValue.HTTP.Paths, newPath)
}
return &newIngress
}

func TestInspectIngress(t *testing.T) {
tests := []struct {
name string
hostname string
path []string
wantErr bool
}{
{
name: "invalid-path-etc",
hostname: "invalid.etc.com",
path: []string{
"/var/run/secrets",
"/mypage",
},
wantErr: true,
},
{
name: "invalid-path-etc",
hostname: "invalid.etc.com",
path: []string{
"/etc/nginx",
},
wantErr: true,
},
{
name: "invalid-path-etc",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the name can be changed

hostname: "invalid.etc.com",
path: []string{
"/mypage",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ingress := makeSimpleIngress(tt.hostname, tt.path...)
if err := InspectIngress(ingress); (err != nil) != tt.wantErr {
t.Errorf("InspectIngress() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Loading