@@ -36,10 +36,12 @@ import ntlmm = require('./handlers/ntlm');
3636import patm = require( './handlers/personalaccesstoken' ) ;
3737
3838import * as rm from 'typed-rest-client/RestClient' ;
39- //import * as hm from 'typed-rest-client/HttpClient';
4039import vsom = require( './VsoClient' ) ;
4140import lim = require( "./interfaces/LocationsInterfaces" ) ;
4241
42+ import fs = require( 'fs' ) ;
43+ import crypto = require( 'crypto' ) ;
44+
4345/**
4446 * Methods to return handler objects (see handlers folder)
4547 */
@@ -89,8 +91,37 @@ export class WebApi {
8991 constructor ( defaultUrl : string , authHandler : VsoBaseInterfaces . IRequestHandler , options ?: VsoBaseInterfaces . IRequestOptions ) {
9092 this . serverUrl = defaultUrl ;
9193 this . authHandler = authHandler ;
92- this . options = options ;
93- this . rest = new rm . RestClient ( 'vsts-node-api' , null , [ this . authHandler ] , options ) ;
94+ this . options = options || { } ;
95+
96+ // try get proxy setting from environment variable set by VSTS-Task-Lib if there is no proxy setting in the options
97+ if ( ! this . options . proxy || ! this . options . proxy . proxyUrl ) {
98+ if ( global [ '_vsts_task_lib_proxy' ] ) {
99+ let proxyFromEnv : VsoBaseInterfaces . IProxyConfiguration = {
100+ proxyUrl : global [ '_vsts_task_lib_proxy_url' ] ,
101+ proxyUsername : global [ '_vsts_task_lib_proxy_username' ] ,
102+ proxyPassword : this . _readTaskLibSecrets ( global [ '_vsts_task_lib_proxy_password' ] ) ,
103+ proxyBypassHosts : JSON . parse ( global [ '_vsts_task_lib_proxy_bypass' ] || "[]" ) ,
104+ } ;
105+
106+ this . options . proxy = proxyFromEnv ;
107+ }
108+ }
109+
110+ // try get cert setting from environment variable set by VSTS-Task-Lib if there is no cert setting in the options
111+ if ( ! this . options . cert ) {
112+ if ( global [ '_vsts_task_lib_cert' ] ) {
113+ let certFromEnv : VsoBaseInterfaces . ICertConfiguration = {
114+ caFile : global [ '_vsts_task_lib_cert_ca' ] ,
115+ certFile : global [ '_vsts_task_lib_cert_clientcert' ] ,
116+ keyFile : global [ '_vsts_task_lib_cert_key' ] ,
117+ passphrase : this . _readTaskLibSecrets ( global [ '_vsts_task_lib_cert_passphrase' ] ) ,
118+ } ;
119+
120+ this . options . cert = certFromEnv ;
121+ }
122+ }
123+
124+ this . rest = new rm . RestClient ( 'vsts-node-api' , null , [ this . authHandler ] , this . options ) ;
94125 this . vsoClient = new vsom . VsoClient ( defaultUrl , this . rest ) ;
95126 }
96127
@@ -280,4 +311,24 @@ export class WebApi {
280311 handlers = handlers || [ this . authHandler ] ;
281312 return new workitemtrackingm . WorkItemTrackingApi ( serverUrl , handlers , this . options ) ;
282313 }
314+
315+ private _readTaskLibSecrets ( lookupKey : string ) : string {
316+ // the lookupKey should has following format
317+ // base64encoded<keyFilePath>:base64encoded<encryptedContent>
318+ if ( lookupKey && lookupKey . indexOf ( ':' ) > 0 ) {
319+ let lookupInfo : string [ ] = lookupKey . split ( ':' , 2 ) ;
320+
321+ // file contains encryption key
322+ let keyFile = new Buffer ( lookupInfo [ 0 ] , 'base64' ) . toString ( 'utf8' ) ;
323+ let encryptKey = new Buffer ( fs . readFileSync ( keyFile , 'utf8' ) , 'base64' ) ;
324+
325+ let encryptedContent : string = new Buffer ( lookupInfo [ 1 ] , 'base64' ) . toString ( 'utf8' ) ;
326+
327+ let decipher = crypto . createDecipher ( "aes-256-ctr" , encryptKey )
328+ let decryptedContent = decipher . update ( encryptedContent , 'hex' , 'utf8' )
329+ decryptedContent += decipher . final ( 'utf8' ) ;
330+
331+ return decryptedContent ;
332+ }
333+ }
283334}
0 commit comments