Skip to content

Commit

Permalink
Add Ingress TLS Hosts slice length check to avoid runtime panic (#358)
Browse files Browse the repository at this point in the history
* Added check for Ingress TLS Hosts length to avoid runtime panic
In accordance with the issue #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>
  • Loading branch information
hassaanakram and LilShah authored Jun 15, 2023
1 parent 6f78dd4 commit d9d4ef5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/kube/wrappers/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/kube/wrappers/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/testutil/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit d9d4ef5

Please sign in to comment.