Skip to content

Commit

Permalink
fix(core): error when attempting to forward to restricted local port
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Dec 14, 2019
1 parent 5d1d951 commit e6a103b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion garden-service/src/plugins/kubernetes/port-forward.ts
Expand Up @@ -8,7 +8,6 @@

import { ChildProcess } from "child_process"

import getPort = require("get-port")
const AsyncLock = require("async-lock")
import { V1Service } from "@kubernetes/client-node"

Expand All @@ -23,6 +22,7 @@ import { ForwardablePort } from "../../types/service"
import { isBuiltIn } from "./util"
import { LogEntry } from "../../logger/log-entry"
import { RuntimeError } from "../../exceptions"
import { getPort } from "../../util/network"

// TODO: implement stopPortForward handler

Expand Down
2 changes: 1 addition & 1 deletion garden-service/src/proxy.ts
Expand Up @@ -10,12 +10,12 @@ import { isEqual, invert } from "lodash"
import Bluebird from "bluebird"
import { createServer, Server, Socket } from "net"
const AsyncLock = require("async-lock")
import getPort = require("get-port")
import { Service, ServiceStatus, ForwardablePort } from "./types/service"
import { Garden } from "./garden"
import { registerCleanupFunction } from "./util/util"
import { LogEntry } from "./logger/log-entry"
import { GetPortForwardResult } from "./types/plugin/service/getPortForward"
import { getPort } from "./util/network"

interface PortProxy {
key: string
Expand Down
2 changes: 1 addition & 1 deletion garden-service/src/server/server.ts
Expand Up @@ -15,7 +15,6 @@ import serve = require("koa-static")
import Router = require("koa-router")
import websockify from "koa-websocket"
import bodyParser = require("koa-bodyparser")
import getPort = require("get-port")
import { omit } from "lodash"

import { Garden } from "../garden"
Expand All @@ -28,6 +27,7 @@ import { EventName, Events } from "../events"
import { ValueOf } from "../util/util"
import { AnalyticsHandler } from "../analytics/analytics"
import { joi } from "../config/common"
import { getPort } from "../util/network"

export const DEFAULT_PORT = 9777
const notReadyMessage = "Waiting for Garden instance to initialize"
Expand Down
24 changes: 24 additions & 0 deletions garden-service/src/util/network.ts
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2018 Garden Technologies, Inc. <info@garden.io>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import _getPort from "get-port"
import { omit } from "lodash"

export async function getPort(options?: _getPort.Options) {
try {
return await _getPort(options)
} catch (error) {
if (error.code === "EACCES") {
// Upstream library doesn't handle errors where a port is free but we're not allowed to listen on it.
// Fall back to using a random port.

This comment has been minimized.

Copy link
@mjgallag

mjgallag Dec 17, 2019

Contributor

@edvald oops, looks like my little change caused this, decided to submit a fix to upstream library sindresorhus/get-port#39. Somewhat interesting given Apple's changes https://news.ycombinator.com/item?id=18302380.

return _getPort(omit(options, "port"))
} else {
throw error
}
}
}
2 changes: 1 addition & 1 deletion garden-service/test/unit/src/server/server.ts
Expand Up @@ -14,8 +14,8 @@ import { expect } from "chai"
import { deepOmitUndefined } from "../../../../src/util/util"
import uuid from "uuid"
import request = require("supertest")
import getPort = require("get-port")
import WebSocket = require("ws")
import { getPort } from "../../../../src/util/network"

describe("startServer", () => {
let garden: Garden
Expand Down
10 changes: 10 additions & 0 deletions garden-service/test/unit/src/util/network.ts
@@ -0,0 +1,10 @@
import { expect } from "chai"
import { getPort } from "../../../../src/util/network"

describe("getPort", () => {
it("should fall back to random port if not allowed to bind to specified port", async () => {
const port = await getPort({ port: 0 })
expect(port).to.not.equal(1)
expect(typeof port).to.equal("number")
})
})

0 comments on commit e6a103b

Please sign in to comment.