From d9d4ef531ebffffcc2a81885dc1ccb827a85d5be Mon Sep 17 00:00:00 2001 From: Hassaan Akram <54958485+hassaanakram@users.noreply.github.com> Date: Thu, 15 Jun 2023 14:04:41 +0500 Subject: [PATCH] Add Ingress TLS Hosts slice length check to avoid runtime panic (#358) * Added check for Ingress TLS Hosts length to avoid runtime panic In accordance with the issue https://github.com/stakater/Forecastle/issues/345 a check has been added to ensure that the Hosts slice in Ingress TLS is not accessed while it is empty. * removed getTLSHosts() method --------- Co-authored-by: Muhammad Shahid Hussain <34099012+LilShah@users.noreply.github.com> --- pkg/kube/wrappers/ingress.go | 2 +- pkg/kube/wrappers/ingress_test.go | 8 ++++++++ pkg/testutil/kube.go | 11 +++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/kube/wrappers/ingress.go b/pkg/kube/wrappers/ingress.go index bb570891..cf81b7a0 100644 --- a/pkg/kube/wrappers/ingress.go +++ b/pkg/kube/wrappers/ingress.go @@ -99,7 +99,7 @@ func (iw *IngressWrapper) rulesExist() bool { } func (iw *IngressWrapper) tryGetTLSHost() (string, bool) { - if iw.supportsTLS() { + if iw.supportsTLS() && len(iw.ingress.Spec.TLS[0].Hosts) > 0 { return "https://" + iw.ingress.Spec.TLS[0].Hosts[0], true } diff --git a/pkg/kube/wrappers/ingress_test.go b/pkg/kube/wrappers/ingress_test.go index 02615d4b..572c07f5 100644 --- a/pkg/kube/wrappers/ingress_test.go +++ b/pkg/kube/wrappers/ingress_test.go @@ -274,6 +274,14 @@ func TestIngressWrapper_tryGetTLSHost(t *testing.T) { want: "https://google.com", want1: true, }, + { + name: "IngressWithTLSAndNoHosts", + fields: fields{ + ingress: testutil.CreateIngressWithHostAndEmptyTLSHost("someIngress", "google.com"), + }, + want: "", + want1: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/testutil/kube.go b/pkg/testutil/kube.go index 25e0e621..a72d1d7b 100644 --- a/pkg/testutil/kube.go +++ b/pkg/testutil/kube.go @@ -107,6 +107,17 @@ func CreateIngressWithHostAndTLSHost(name string, host string, tlsurl string) *v return ingress } +func CreateIngressWithHostAndEmptyTLSHost(name string, host string) *v1.Ingress { + ingress := CreateIngressWithHost(name, host) + ingress.Spec.TLS = []v1.IngressTLS{ + { + Hosts: []string{}, + }, + } + + return ingress +} + func CreateForecastleApp(name string, url string, group string, icon string) *v1alpha1.ForecastleApp { return &v1alpha1.ForecastleApp{ ObjectMeta: metav1.ObjectMeta{