From d3d22192143a4a1a7789fc27f050614d98c846d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Sis=C3=A1k?= <86593861+dzsak@users.noreply.github.com> Date: Tue, 5 Mar 2024 09:16:51 +0100 Subject: [PATCH] Bug: Undefined error in services without ports (#59) --- web/src/Service.jsx | 56 ++++++++++++++++++++++++++++------------- web/src/service.test.js | 55 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 17 deletions(-) create mode 100644 web/src/service.test.js diff --git a/web/src/Service.jsx b/web/src/Service.jsx index a5f4953..2d6e449 100644 --- a/web/src/Service.jsx +++ b/web/src/Service.jsx @@ -27,21 +27,8 @@ function Service(props) { const configMapWidgets = configMaps(service.pods, service.svc.metadata.namespace, capacitorClient) const secretWidgets = secrets(service.pods, service.svc.metadata.namespace, capacitorClient) - const svcPort = service.svc.spec.ports[0].port - let hostPort = "" - if (svcPort) { - if (svcPort <= 99) { - hostPort = "100" + svcPort - } else if (svcPort <= 999) { - hostPort = "10" + svcPort - } else { - hostPort = svcPort - } - - if (hostPort === "10080") { // Connections to HTTP, HTTPS or FTP servers on port 10080 will fail. This is a mitigation for the NAT Slipstream 2.0 attack. - hostPort = "10081" - } - } + const appPort = getAppPort(service.svc.spec.ports) ?? ""; + const hostPort = getHostPort(service.svc.spec.ports) ?? ""; return ( <> @@ -167,7 +154,7 @@ function Service(props) {
@@ -185,7 +172,7 @@ function Service(props) {

) : null } - {svcPort && + {service.svc.spec.ports && <> http://127.0.0.1:{hostPort} { + const port = getAppPort(undefined) + + expect(port).toBeUndefined(); +}); + +test('should return undefined for host port', () => { + const port = getHostPort(undefined) + + expect(port).toBeUndefined(); +}); + +test('should return "80" for app port', () => { + const ports = [{}]; + const port = getAppPort(ports) + + expect(port).toEqual("80"); +}); + +test('should return "5000" for app port', () => { + const ports = [{"port": 5000}]; + const port = getAppPort(ports) + + expect(port).toEqual("5000"); +}); + +test('should return "5000" for host port', () => { + const ports = [{"port": 5000}]; + const port = getHostPort(ports) + + expect(port).toEqual("5000"); +}); + +test('should return "10081" for host port', () => { + const ports = [{"port": 80}]; + const port = getHostPort(ports) + + expect(port).toEqual("10081"); +}); + +test('should return "10090 for host port', () => { + const ports = [{"port": 90}]; + const port = getHostPort(ports) + + expect(port).toEqual("10090"); +}); + +test('should return "10200 for host port', () => { + const ports = [{"port": 200}]; + const port = getHostPort(ports) + + expect(port).toEqual("10200"); +});