@@ -36,10 +36,12 @@ import ntlmm = require('./handlers/ntlm');
36
36
import patm = require( './handlers/personalaccesstoken' ) ;
37
37
38
38
import * as rm from 'typed-rest-client/RestClient' ;
39
- //import * as hm from 'typed-rest-client/HttpClient';
40
39
import vsom = require( './VsoClient' ) ;
41
40
import lim = require( "./interfaces/LocationsInterfaces" ) ;
42
41
42
+ import fs = require( 'fs' ) ;
43
+ import crypto = require( 'crypto' ) ;
44
+
43
45
/**
44
46
* Methods to return handler objects (see handlers folder)
45
47
*/
@@ -89,8 +91,37 @@ export class WebApi {
89
91
constructor ( defaultUrl : string , authHandler : VsoBaseInterfaces . IRequestHandler , options ?: VsoBaseInterfaces . IRequestOptions ) {
90
92
this . serverUrl = defaultUrl ;
91
93
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 ) ;
94
125
this . vsoClient = new vsom . VsoClient ( defaultUrl , this . rest ) ;
95
126
}
96
127
@@ -280,4 +311,24 @@ export class WebApi {
280
311
handlers = handlers || [ this . authHandler ] ;
281
312
return new workitemtrackingm . WorkItemTrackingApi ( serverUrl , handlers , this . options ) ;
282
313
}
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
+ }
283
334
}
0 commit comments