From 37721b5dd77d66edfb7e8377b2b96470b8a21d1b Mon Sep 17 00:00:00 2001 From: Ali <83188384+testA113@users.noreply.github.com> Date: Fri, 3 Nov 2023 14:02:51 +0000 Subject: [PATCH] fix: ensure ingress HTTP rule exists to prevent panic (#726) Signed-off-by: Ali Harris Co-authored-by: Aris Boutselis --- pkg/analyzer/ingress.go | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/pkg/analyzer/ingress.go b/pkg/analyzer/ingress.go index bc4ba084fb..7553405d13 100644 --- a/pkg/analyzer/ingress.go +++ b/pkg/analyzer/ingress.go @@ -98,26 +98,28 @@ func (IngressAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) { // loop over rules for _, rule := range ing.Spec.Rules { - // loop over paths - for _, path := range rule.HTTP.Paths { - _, err := a.Client.GetClient().CoreV1().Services(ing.Namespace).Get(a.Context, path.Backend.Service.Name, metav1.GetOptions{}) - if err != nil { - doc := apiDoc.GetApiDocV2("spec.rules.http.paths.backend.service") - - failures = append(failures, common.Failure{ - Text: fmt.Sprintf("Ingress uses the service %s/%s which does not exist.", ing.Namespace, path.Backend.Service.Name), - KubernetesDoc: doc, - Sensitive: []common.Sensitive{ - { - Unmasked: ing.Namespace, - Masked: util.MaskString(ing.Namespace), + // loop over HTTP paths + if rule.HTTP != nil { + for _, path := range rule.HTTP.Paths { + _, err := a.Client.GetClient().CoreV1().Services(ing.Namespace).Get(a.Context, path.Backend.Service.Name, metav1.GetOptions{}) + if err != nil { + doc := apiDoc.GetApiDocV2("spec.rules.http.paths.backend.service") + + failures = append(failures, common.Failure{ + Text: fmt.Sprintf("Ingress uses the service %s/%s which does not exist.", ing.Namespace, path.Backend.Service.Name), + KubernetesDoc: doc, + Sensitive: []common.Sensitive{ + { + Unmasked: ing.Namespace, + Masked: util.MaskString(ing.Namespace), + }, + { + Unmasked: path.Backend.Service.Name, + Masked: util.MaskString(path.Backend.Service.Name), + }, }, - { - Unmasked: path.Backend.Service.Name, - Masked: util.MaskString(path.Backend.Service.Name), - }, - }, - }) + }) + } } } }