Skip to content

Commit

Permalink
register the npm proxy as a secret if it contains a password (#11139) (
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaylo authored Aug 22, 2019
1 parent 00c6a86 commit 7ca570b
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 75 deletions.
97 changes: 97 additions & 0 deletions Tasks/NpmV1/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,103 @@ import * as path from 'path';
import * as mockery from 'mockery';
import * as ttm from 'azure-pipelines-task-lib/mock-test';

class MockedTask {
private _proxyUrl: string;
private _proxyUsername: string;
private _proxyPassword: string;
private _proxyBypass: string;
private _secret: string;

public debug(message: string) {}
public loc(message: string): string { return message; }

public setMockedValues(proxyUrl?: string, proxyUsername?: string, proxyPassword?: string, proxyBypass?: string) {
this._proxyUrl = proxyUrl;
this._proxyUsername = proxyUsername;
this._proxyPassword = proxyPassword;
this._proxyBypass = proxyBypass;
this._secret = '';
}

public getVariable(name: string) {
switch (name.toLowerCase()) {
case "agent.proxyurl":
return this._proxyUrl;
case "agent.proxyusername":
return this._proxyUsername;
case "agent.proxypassword":
return this._proxyPassword;
case "agent.proxybypasslist":
return this._proxyBypass;
case "task.displayname":
return "Npm Test"
default:
return undefined;
}
}

public setSecret(secret: string) {
this._secret = secret;
}

public getSecret(): string {
return this._secret;
}
}

describe('Npm Toolrunner', function () {

var mockedTask: MockedTask = new MockedTask();
var mockedProxy: string = "http://proxy/";
var mockedUsername: string = "mockedUsername";
var mockedPassword: string = "mockedPassword#";
var e = mockery.registerMock("azure-pipelines-task-lib/task", mockedTask);

beforeEach(() => {
mockery.enable({
useCleanCache: true,
warnOnReplace: false,
warnOnUnregistered: false
});
});

afterEach(() => {
mockery.disable();
});

it("No HTTP_PROXY", (done: MochaDone) => {
mockedTask.setMockedValues();
let npmToolRunner = require("../npmtoolrunner");

let httpProxy: string = npmToolRunner.NpmToolRunner._getProxyFromEnvironment();
assert.strictEqual(httpProxy, undefined);

done();
});

it("gets proxy without auth", (done: MochaDone) => {
mockedTask.setMockedValues(mockedProxy);
let npmToolRunner = require("../npmtoolrunner");

let httpProxy: string = npmToolRunner.NpmToolRunner._getProxyFromEnvironment();
assert.strictEqual(httpProxy, mockedProxy);

done();
});

it("registers the proxy uri with a password as a secret", (done: MochaDone) => {
mockedTask.setMockedValues(mockedProxy, mockedUsername, mockedPassword);
let npmToolRunner = require("../npmtoolrunner");

let httpProxy: string = npmToolRunner.NpmToolRunner._getProxyFromEnvironment();
let expected = `http://${mockedUsername}:${encodeURIComponent(mockedPassword)}@proxy/`;
assert.strictEqual(httpProxy, expected);
assert.strictEqual(mockedTask.getSecret(), expected);

done();
});
});

describe('Npm Task', function () {
before(() => {
mockery.disable(); // needed to ensure that we can mock vsts-task-lib/task
Expand Down
20 changes: 16 additions & 4 deletions Tasks/NpmV1/npmtoolrunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,29 @@ export class NpmToolRunner extends tr.ToolRunner {
return execResult;
}

private static _getProxyFromEnvironment(): string {
public static _getProxyFromEnvironment(): string {
let proxyUrl: string = tl.getVariable('agent.proxyurl');
if (proxyUrl) {
let proxy: Url = parse(proxyUrl);
let proxyUsername: string = tl.getVariable('agent.proxyusername') || '';
let proxyPassword: string = tl.getVariable('agent.proxypassword') || '';

let auth = `${proxyUsername}:${proxyPassword}`;
proxy.auth = auth;
if (proxyUsername !== '') {
proxy.auth = proxyUsername;
}

if (proxyPassword !== '') {
proxy.auth = `${proxyUsername}:${proxyPassword}`;
}

const authProxy = format(proxy);

// register the formatted proxy url as a secret if it contains a password
if (proxyPassword !== '') {
tl.setSecret(authProxy);
}

return format(proxy);
return authProxy;
}

return undefined;
Expand Down
97 changes: 28 additions & 69 deletions Tasks/NpmV1/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Tasks/NpmV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"version": {
"Major": 1,
"Minor": 156,
"Patch": 1
"Patch": 2
},
"runsOn": [
"Agent",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/NpmV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"version": {
"Major": 1,
"Minor": 156,
"Patch": 1
"Patch": 2
},
"runsOn": [
"Agent",
Expand Down

0 comments on commit 7ca570b

Please sign in to comment.