Skip to content

Commit

Permalink
fix: add no_proxy for localhost traffic
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterSchafer committed Dec 23, 2022
1 parent 48b5d9a commit f66df33
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions cliv2/internal/cliv2/cliv2.go
Expand Up @@ -249,6 +249,7 @@ func PrepareV1EnvironmentVariables(input []string, integrationName string, integ
inputAsMap[constants.SNYK_HTTPS_PROXY_ENV] = proxyAddress
inputAsMap[constants.SNYK_HTTP_PROXY_ENV] = proxyAddress
inputAsMap[constants.SNYK_CA_CERTIFICATE_LOCATION_ENV] = caCertificateLocation
inputAsMap[constants.SNYK_HTTP_NO_PROXY_ENV] = constants.SNYK_INTERNAL_NO_PROXY

result = utils.ToSlice(inputAsMap, "=")
}
Expand Down
5 changes: 4 additions & 1 deletion cliv2/internal/cliv2/cliv2_test.go
Expand Up @@ -21,7 +21,7 @@ func Test_PrepareV1EnvironmentVariables_Fill_and_Filter(t *testing.T) {
"something=1",
"in=2",
"here=3=2",
"no_proxy=noProxy",
"NO_PROXY=noProxy",
"HTTPS_PROXY=httpsProxy",
"HTTP_PROXY=httpProxy",
"NPM_CONFIG_PROXY=something",
Expand All @@ -41,6 +41,7 @@ func Test_PrepareV1EnvironmentVariables_Fill_and_Filter(t *testing.T) {
"SNYK_SYSTEM_NO_PROXY=noProxy",
"SNYK_SYSTEM_HTTP_PROXY=httpProxy",
"SNYK_SYSTEM_HTTPS_PROXY=httpsProxy",
"NO_PROXY=" + constants.SNYK_INTERNAL_NO_PROXY,
}

actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation")
Expand All @@ -66,6 +67,7 @@ func Test_PrepareV1EnvironmentVariables_DontOverrideExistingIntegration(t *testi
"SNYK_SYSTEM_NO_PROXY=",
"SNYK_SYSTEM_HTTP_PROXY=",
"SNYK_SYSTEM_HTTPS_PROXY=",
"NO_PROXY=" + constants.SNYK_INTERNAL_NO_PROXY,
}

actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation")
Expand All @@ -91,6 +93,7 @@ func Test_PrepareV1EnvironmentVariables_OverrideProxyAndCerts(t *testing.T) {
"SNYK_SYSTEM_NO_PROXY=312123",
"SNYK_SYSTEM_HTTP_PROXY=exists",
"SNYK_SYSTEM_HTTPS_PROXY=already",
"NO_PROXY=" + constants.SNYK_INTERNAL_NO_PROXY,
}

actual, err := cliv2.PrepareV1EnvironmentVariables(input, "foo", "bar", "proxy", "cacertlocation")
Expand Down
1 change: 1 addition & 0 deletions cliv2/internal/constants/constants.go
Expand Up @@ -15,6 +15,7 @@ const SNYK_NPM_NO_PROXY_ENV = "NPM_CONFIG_NO_PROXY"
const SNYK_NPM_ALL_PROXY = "ALL_PROXY"
const SNYK_CA_CERTIFICATE_LOCATION_ENV = "NODE_EXTRA_CA_CERTS"
const SNYK_DEFAULT_API_URL = "https://api.snyk.io"
const SNYK_INTERNAL_NO_PROXY = "localhost,127.0.0.1,::1"

const SNYK_HTTPS_PROXY_ENV_SYSTEM = "SNYK_SYSTEM_HTTPS_PROXY"
const SNYK_HTTP_PROXY_ENV_SYSTEM = "SNYK_SYSTEM_HTTP_PROXY"
Expand Down
25 changes: 23 additions & 2 deletions test/jest/acceptance/https.spec.ts
Expand Up @@ -4,20 +4,41 @@ import { createProjectFromWorkspace } from '../util/createProject';
import { getFixturePath } from '../util/getFixturePath';
import { runSnykCLI } from '../util/runSnykCLI';
import { isCLIV2 } from '../util/isCLIV2';
import * as os from 'os';

jest.setTimeout(1000 * 30);

function getFirstIPv4Address(): string {
let ipaddress = '';

const interfaces = os.networkInterfaces();
for (const [, group] of Object.entries(interfaces)) {
if (group) {
for (const inter of group) {
if (inter && inter.family == 'IPv4' && inter.address != '127.0.0.1') {
ipaddress = inter.address;
break;
}
}
}
}
return ipaddress;
}

describe('https', () => {
let server: FakeServer;
let env: Record<string, string>;

beforeAll(async () => {
const ipaddress = getFirstIPv4Address();
console.log('Using ip: ' + ipaddress);

const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const baseApi = '/api/v1';
env = {
...process.env,
SNYK_API: 'https://localhost:' + port + baseApi,
SNYK_HOST: 'https://localhost:' + port,
SNYK_API: 'https://' + ipaddress + ':' + port + baseApi,
SNYK_HOST: 'https://' + ipaddress + ':' + port,
SNYK_TOKEN: '123456789',
};
server = fakeServer(baseApi, env.SNYK_TOKEN);
Expand Down

0 comments on commit f66df33

Please sign in to comment.