Added proxy support for azure agent and github runner

This commit is contained in:
Thomas VAILLIER
2020-03-11 22:43:44 +01:00
committed by Artur
parent e1e31a379b
commit a2f365cab2
34 changed files with 256 additions and 180 deletions

View File

@@ -4,6 +4,7 @@ import * as taskLib from 'azure-pipelines-task-lib/task'
import * as toolLib from 'azure-pipelines-tool-lib/tool'
import { IBuildAgent, IExecResult } from '../../core/models'
import { IRequestOptions } from 'typed-rest-client/Interfaces'
@injectable()
class BuildAgent implements IBuildAgent {
@@ -11,6 +12,14 @@ class BuildAgent implements IBuildAgent {
return 'Azure Pipelines'
}
public proxyConfiguration(url: string): IRequestOptions {
return {
proxy: taskLib.getHttpProxyConfiguration(url),
cert: taskLib.getHttpCertConfiguration(),
ignoreSslError: !!taskLib.getVariable('Agent.SkipCertValidation')
}
}
public find(toolName: string, versionSpec: string, arch?: string): string {
return toolLib.findLocalTool(toolName, versionSpec, arch)
}

View File

@@ -1,7 +1,8 @@
import * as fs from 'fs'
import * as path from 'path'
import * as urlApi from 'url'
import { v4 as uuidv4 } from 'uuid';
import { v4 as uuidv4 } from 'uuid'
import * as core from '@actions/core'
import * as exe from '@actions/exec'
@@ -11,6 +12,10 @@ import * as toolCache from '@actions/tool-cache'
import { injectable } from 'inversify'
import { IBuildAgent, IExecResult } from '../../core/models'
import {
IRequestOptions,
IProxyConfiguration
} from 'typed-rest-client/Interfaces'
@injectable()
class BuildAgent implements IBuildAgent {
@@ -22,6 +27,57 @@ class BuildAgent implements IBuildAgent {
return toolCache.find(toolName, versionSpec, arch)
}
public proxyConfiguration(url: string): IRequestOptions {
return {
proxy: this.getProxyConfiguration(url),
cert: undefined,
ignoreSslError: undefined
}
}
private getProxyConfiguration(requestedUrl: string): IProxyConfiguration {
let noProxy = process.env.NO_PROXY || process.env.no_proxy || null
let allowedHostsWithoutProxy
let dontUseProxy = false
if (noProxy) {
if (noProxy === '*') {
dontUseProxy = true
} else {
allowedHostsWithoutProxy = noProxy.split(',')
if (requestedUrl) {
allowedHostsWithoutProxy.forEach(host => {
if (new RegExp(host, 'i').test(requestedUrl)) {
dontUseProxy = true
}
})
}
}
}
if (dontUseProxy) {
return undefined
}
let proxyURL =
process.env.HTTP_PROXY ||
process.env.http_proxy ||
process.env.HTTPS_PROXY ||
process.env.https_proxy ||
null
if (proxyURL) {
let url = new urlApi.URL(requestedUrl)
return {
proxyUrl: `${url.protocol}//${url.hostname}:${url.port}`,
proxyUsername: url.username,
proxyPassword: url.password,
proxyBypassHosts: allowedHostsWithoutProxy
}
}
return undefined
}
public cacheDir(
sourceDir: string,
tool: string,

View File

@@ -1,9 +1,14 @@
import { injectable } from 'inversify'
import { IBuildAgent, IExecResult } from '../../core/models'
import { IRequestOptions } from 'typed-rest-client/Interfaces'
@injectable()
class BuildAgent implements IBuildAgent {
proxyConfiguration(url: string): IRequestOptions {
console.log('proxyConfiguration')
return undefined
}
public get agentName(): string {
console.log('getAgentName')
return 'Mock'

View File

@@ -2,6 +2,7 @@ import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import * as http from 'typed-rest-client/HttpClient'
import * as ifm from 'typed-rest-client/Interfaces'
import { inject, injectable } from 'inversify'
import { TYPES, IExecResult, IBuildAgent } from './models'
@@ -23,13 +24,20 @@ export class DotnetTool implements IDotnetTool {
protected versionManager: IVersionManager
private httpClient: http.HttpClient
private static readonly nugetRoot: string =
'https://api-v2v3search-0.nuget.org/'
constructor(
@inject(TYPES.IBuildAgent) buildAgent: IBuildAgent,
@inject(TYPES.IVersionManager) versionManager: IVersionManager
) {
this.buildAgent = buildAgent
this.versionManager = versionManager
this.httpClient = new http.HttpClient('dotnet')
this.httpClient = new http.HttpClient(
'dotnet',
undefined,
this.buildAgent.proxyConfiguration(DotnetTool.nugetRoot)
)
}
public disableTelemetry(): void {
@@ -133,9 +141,12 @@ export class DotnetTool implements IDotnetTool {
} ${includePrerelease ? 'including pre-releases' : ''}`
)
const downloadPath = `https://api-v2v3search-0.nuget.org/query?q=${encodeURIComponent(
toolName.toLowerCase()
)}&prerelease=${includePrerelease ? 'true' : 'false'}&semVerLevel=2.0.0`
const downloadPath = `${
DotnetTool.nugetRoot
}query?q=${encodeURIComponent(toolName.toLowerCase())}&prerelease=${
includePrerelease ? 'true' : 'false'
}&semVerLevel=2.0.0`
const res = await this.httpClient.get(downloadPath)
if (!res || res.message.statusCode !== 200) {

View File

@@ -1,3 +1,5 @@
import { IRequestOptions } from 'typed-rest-client/Interfaces'
export const TYPES = {
IBuildAgent: Symbol.for('BuildAgent'),
IDotnetTool: Symbol.for('DotnetTool'),
@@ -25,6 +27,7 @@ export interface IExecResult {
export interface IBuildAgent {
agentName: string
proxyConfiguration(url: string): IRequestOptions
find(toolName: string, versionSpec: string, arch?: string): string
cacheDir(
sourceDir: string,

View File

@@ -41,7 +41,7 @@ export async function run() {
const result = await gitVersionTool.run(settings)
const { stdout } = result
const jsonOutput = stdout.substr(stdout.indexOf("{"))
const jsonOutput = stdout.substr(stdout.indexOf('{'))
const gitversion = JSON.parse(jsonOutput) as GitVersionOutput
gitVersionTool.writeGitVersionToAgent(gitversion)