Skip to content

Commit

Permalink
Bug: Undefined error in services without ports (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzsak authored Mar 5, 2024
1 parent 73b7bb5 commit d3d2219
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 17 deletions.
56 changes: 39 additions & 17 deletions web/src/Service.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<host-port>"
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) ?? "<app-port>";
const hostPort = getHostPort(service.svc.spec.ports) ?? "<host-port>";

return (
<>
Expand Down Expand Up @@ -167,7 +154,7 @@ function Service(props) {
<div className='absolute right-0 top-0'>
<CopyBtn
title='Port-forward command'
textToCopy={`kubectl port-forward deploy/${deployment.metadata.name} -n ${deployment.metadata.namespace} ${hostPort}:${svcPort}`}
textToCopy={`kubectl port-forward deploy/${deployment.metadata.name} -n ${deployment.metadata.namespace} ${hostPort}:${appPort}`}
/>
</div>
</div>
Expand All @@ -185,7 +172,7 @@ function Service(props) {
</p>
) : null
}
{svcPort &&
{service.svc.spec.ports &&
<>
<a href={'http://127.0.0.1:' + hostPort} target="_blank" rel="noopener noreferrer">http://127.0.0.1:{hostPort}
<svg xmlns="http://www.w3.org/2000/svg"
Expand Down Expand Up @@ -242,6 +229,41 @@ function Service(props) {

export default Service;

export function getAppPort(ports) {
if (!ports) {
return
}

const servicePort = getServicePort(ports);
return `${servicePort}`;
}

export function getHostPort(ports) {
if (!ports) {
return
}

let servicePort = getServicePort(ports);

if (servicePort <= 99) {
if (servicePort === 80) {
return "10081" // Connections to HTTP, HTTPS or FTP servers on port 10080 will fail. This is a mitigation for the NAT Slipstream 2.0 attack.
}
return `100${servicePort}`
}

if (servicePort <= 999) {
return `10${servicePort}`
}

return `${servicePort}`;
}

function getServicePort(ports) {
const servicePorts = jp.query(ports, '$..[?(@.port)].port');
return servicePorts.length === 1 ? servicePorts[0] : 80;
}

export function Pod(props) {
const {pod} = props;

Expand Down
55 changes: 55 additions & 0 deletions web/src/service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { getAppPort, getHostPort } from "./Service"

test('should return undefined for app port', () => {
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");
});

0 comments on commit d3d2219

Please sign in to comment.