Skip to content

Commit

Permalink
Allow password-less proxySettings
Browse files Browse the repository at this point in the history
It turns out that in some enterprise customer the proxy auth settings include a
username, but not a password: microsoft/AzureStorageExplorer#4430

This change allows password-less auth settings to be passed to the underlying agent.

It's unclear whether it's a real scenario when a proxy auth has a password, but
not username so this PR doesn't consider this scenario.
  • Loading branch information
jeremymeng committed Jun 15, 2021
1 parent 062829a commit 9b6d599
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog

## 2.5.2 - (2021-06-15)
- Fixed an issue where `proxySettings` does not work when there is username but no password.

## 2.5.1 - (2021-06-07)
- [BugFix] Array flattening in deserializer loses previously de-serialized attributes (PR [#451](https://github.com/Azure/ms-rest-js/pull/451))

Expand Down
2 changes: 2 additions & 0 deletions lib/proxyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export function createProxyAgent(

if (proxySettings.username && proxySettings.password) {
tunnelOptions.proxy!.proxyAuth = `${proxySettings.username}:${proxySettings.password}`;
} else if (proxySettings.username) {
tunnelOptions.proxy!.proxyAuth = `${proxySettings.username}`;
}

const requestScheme = URLBuilder.parse(requestUrl).getScheme() || "";
Expand Down
32 changes: 32 additions & 0 deletions test/proxyAgent.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import https from "https";

import { HttpHeaders } from "../lib/msRest";
import { createProxyAgent, createTunnel } from "../lib/proxyAgent";
import { ProxySettings } from "../lib/serviceClient";

describe("proxyAgent", () => {
describe("createProxyAgent", () => {
Expand Down Expand Up @@ -62,6 +63,37 @@ describe("proxyAgent", () => {
agent.proxyOptions.headers!.should.contain({ "user-agent": "Node.js" });
done();
});

it("should set agent proxyAuth correctly", function (done) {
const proxySettings: ProxySettings = {
host: "http://proxy.microsoft.com",
port: 8080,
username: "username",
password: "pass123",
};

const proxyAgent = createProxyAgent("http://example.com", proxySettings);

const agent = proxyAgent.agent as HttpsAgent;
should().exist(agent.options.proxy.proxyAuth);
agent.options.proxy.proxyAuth!.should.equal("username:pass123");
done();
});

it("should set agent proxyAuth correctly when password is not specified", function (done) {
const proxySettings: ProxySettings = {
host: "http://proxy.microsoft.com",
port: 8080,
username: "username",
};

const proxyAgent = createProxyAgent("http://example.com", proxySettings);

const agent = proxyAgent.agent as HttpsAgent;
should().exist(agent.options.proxy.proxyAuth);
agent.options.proxy.proxyAuth!.should.equal("username");
done();
});
});

describe("createTunnel", () => {
Expand Down

0 comments on commit 9b6d599

Please sign in to comment.