Code Monkey home page Code Monkey logo

Comments (1)

hassaanakram avatar hassaanakram commented on September 23, 2024

Hi @karl-johan-grahn
I'd like to work on this enhancement. I have gone through the relevant code and my analysis is as follows.

Cause of this panic
In

return "https://" + iw.ingress.Spec.TLS[0].Hosts[0], true
the Hosts slice is empty and therefore attempting to access Hosts[0] causes the runtime panic.

What can be done to make this access safe
Add a new method getTLSHosts on the IngressWrapper:

func (iw *IngressWrapper) getTLSHosts() []string {
	if iw.supportsTLS() {
		return iw.ingress.Spec.TLS[0].Hosts
	}
	return []string{}
}

Similarly change the method tryGetTLSHost to:

func (iw *IngressWrapper) tryGetTLSHost() (string, bool) {
	if iw.supportsTLS() && len(iw.getTLSHosts()) > 0 {
		return "https://" + iw.ingress.Spec.TLS[0].Hosts[0], true
	}

	return "", false
}

Test cases
In https://github.com/stakater/Forecastle/blob/master/pkg/testutil/kube.go add the following function to simulate a case where TLS hosts is empty:

func CreateIngressWithHostAndEmptyTLSHost(name string, host string) *v1.Ingress {
	ingress := CreateIngressWithHost(name, host)
	ingress.Spec.TLS = []v1.IngressTLS{
		{
			Hosts: []string{},
		},
	}

	return ingress
}

Add the following test case in

func TestIngressWrapper_tryGetTLSHost(t *testing.T) {

{
			name: "IngressWithTLSAndNoHosts",
			fields: fields{
				ingress: testutil.CreateIngressWithHostAndEmptyTLSHost("someIngress", "google.com"),
			},
			want:  "",
			want1: false,
		},

Let me know what do you think about this.

from forecastle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.