Skip to content

Commit

Permalink
added formatted proxy URL (#834)
Browse files Browse the repository at this point in the history
* added formatted proxy URL
* version increment
  • Loading branch information
kirill-ivlev committed May 25, 2022
1 parent 7486d51 commit fb34438
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 4 deletions.
19 changes: 19 additions & 0 deletions node/docs/proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface ProxyConfiguration {
proxyUsername?: string;
proxyPassword?: string;
proxyBypassHosts?: string[];
proxyFormattedUrl: string;
}
```

Expand Down Expand Up @@ -61,6 +62,24 @@ async function run() {
run();
```

For some external applications executed from the shell, you might need to set an environment variable that contains a formatted URL
in the following format: protocol://user:password@hostname:port
You can retrieve such configuration directly from task-lib:
```typescript
import tl = require('azure-pipelines-task-lib/task');

async function run() {
let proxy = tl.getProxyConfiguration()

process.env['http_proxy'] = proxy.proxyFormattedUrl;
process.env['https_proxy'] = proxy.proxyFormattedUrl;
const gitPath: string = tl.which('git');
const gitPull = tl.tool(gitPath);
await gitPull.exec()
}

run();
```
#### PowerShell Lib

Method for retrieve proxy settings in PowerShell lib
Expand Down
2 changes: 1 addition & 1 deletion node/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 node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azure-pipelines-task-lib",
"version": "3.2.1",
"version": "3.3.0",
"description": "Azure Pipelines Task SDK",
"main": "./task.js",
"typings": "./task.d.ts",
Expand Down
26 changes: 25 additions & 1 deletion node/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1839,11 +1839,33 @@ export function findMatch(defaultRoot: string, patterns: string[] | string, find

export interface ProxyConfiguration {
proxyUrl: string;
/**
* Proxy URI formated as: protocol://username:password@hostname:port
*
* For tools that require setting proxy configuration in the single environment variable
*/
proxyFormattedUrl: string;
proxyUsername?: string;
proxyPassword?: string;
proxyBypassHosts?: string[];
}

/**
* Build Proxy URL in the following format: protocol://username:password@hostname:port
* @param proxyUrl Url address of the proxy server (eg: http://example.com)
* @param proxyUsername Proxy username (optional)
* @param proxyPassword Proxy password (optional)
* @returns string
*/
function getProxyFormattedUrl(proxyUrl: string, proxyUsername: string | undefined, proxyPassword: string | undefined): string {
const parsedUrl: URL = new URL(proxyUrl);
let proxyAddress: string = `${parsedUrl.protocol}//${parsedUrl.host}`;
if (proxyUsername) {
proxyAddress = `${parsedUrl.protocol}//${proxyUsername}:${proxyPassword}@${parsedUrl.host}`;
}
return proxyAddress;
}

/**
* Gets http proxy configuration used by Build/Release agent
*
Expand All @@ -1869,11 +1891,13 @@ export function getHttpProxyConfiguration(requestUrl?: string): ProxyConfigurati
return null;
}
else {
const proxyAddress = getProxyFormattedUrl(proxyUrl, proxyUsername, proxyPassword)
return {
proxyUrl: proxyUrl,
proxyUsername: proxyUsername,
proxyPassword: proxyPassword,
proxyBypassHosts: proxyBypassHosts
proxyBypassHosts: proxyBypassHosts,
proxyFormattedUrl: proxyAddress
};
}
}
Expand Down
56 changes: 56 additions & 0 deletions node/test/gethttpproxytests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as assert from 'assert';
import * as tl from '../_build/task';

enum ProxyEnvironmentEnum {
proxyUrl = 'AGENT_PROXYURL',
proxyUsername = 'AGENT_PROXYUSERNAME',
proxyPassword = 'AGENT_PROXYPASSWORD',
proxyBypass = 'AGENT_PROXYBYPASSLIST'
}

describe('GetHttpProxyConfiguration Tests', () => {
const proxyHost: string = 'proxy.example.com';
const proxyPort: number = 8888;
const proxyUrl: string = `http://${proxyHost}:${proxyPort}`;
const proxyUsername: string = 'proxyUser';
const proxyPassword: string = 'proxyPassword';
const proxyByPass: string[] = ['http://bypass.example.com'];
const formatedUrlWithoutCrednetials = proxyUrl;
const fortmatedUrlWithCredentials = `http://${proxyUsername}:${proxyPassword}@${proxyHost}:${proxyPort}`;

it('returns a valid proxy configuration if no credentials set', () => {
process.env[ProxyEnvironmentEnum.proxyUrl] = proxyUrl;
process.env[ProxyEnvironmentEnum.proxyBypass] = JSON.stringify(proxyByPass);
const expected: tl.ProxyConfiguration = {
proxyUrl: proxyUrl,
proxyBypassHosts: proxyByPass,
proxyUsername: undefined,
proxyPassword: undefined,
proxyFormattedUrl: formatedUrlWithoutCrednetials
}
const result = tl.getHttpProxyConfiguration();
assert.deepStrictEqual(result, expected, 'it should have valid configuration');
});

it('returns valid proxy configuration if credentials set', () => {
process.env[ProxyEnvironmentEnum.proxyUrl] = proxyUrl;
process.env[ProxyEnvironmentEnum.proxyUsername] = proxyUsername;
process.env[ProxyEnvironmentEnum.proxyPassword] = proxyPassword;
process.env[ProxyEnvironmentEnum.proxyBypass] = JSON.stringify(proxyByPass);
const expected: tl.ProxyConfiguration = {
proxyUrl: proxyUrl,
proxyBypassHosts: proxyByPass,
proxyFormattedUrl: fortmatedUrlWithCredentials,
proxyPassword: proxyPassword,
proxyUsername: proxyUsername
}
const result = tl.getHttpProxyConfiguration();
assert.deepStrictEqual(result, expected, 'it should have credentials in formatted url');
});

it('returns null if host should be bypassed', () => {
process.env[ProxyEnvironmentEnum.proxyUrl] = proxyUrl;
const result = tl.getHttpProxyConfiguration(proxyByPass[0]);
assert.strictEqual(result, null, 'result should be null');
});
})
3 changes: 2 additions & 1 deletion node/test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"findmatchtests.ts",
"mocktests.ts",
"retrytests.ts",
"isuncpathtests.ts"
"isuncpathtests.ts",
"gethttpproxytests.ts"
]
}

0 comments on commit fb34438

Please sign in to comment.