@@ -6,6 +6,7 @@ import {ToolRunner, IExecOptions, IExecResult} from 'vsts-task-lib/toolrunner';
66import * as auth from "./Authentication"
77import * as os from 'os' ;
88import * as path from 'path' ;
9+ import * as url from 'url' ;
910
1011interface EnvironmentDictionary { [ key : string ] : string }
1112
@@ -50,6 +51,7 @@ function prepareNuGetExeEnvironment(input: EnvironmentDictionary, settings: NuGe
5051 env [ 'NUGET_CREDENTIAL_PROVIDER_OVERRIDE_DEFAULT' ] = 'true' ;
5152
5253 if ( credProviderPath ) {
54+ tl . debug ( `credProviderPath = ${ credProviderPath } ` ) ;
5355 env [ 'NUGET_CREDENTIALPROVIDERS_PATH' ] = credProviderPath ;
5456 }
5557
@@ -155,6 +157,79 @@ export function locateNuGetExe(userNuGetExePath: string): string {
155157 return toolPath ;
156158}
157159
160+ function isHosted ( ) : boolean {
161+ // not an ideal way to detect hosted, but there isn't a variable for it, and we can't make network calls from here
162+ // due to proxy issues.
163+ const collectionUri = tl . getVariable ( "System.TeamFoundationCollectionUri" ) ;
164+ const parsedCollectionUri = url . parse ( collectionUri ) ;
165+ return / \. v i s u a l s t u d i o \. c o m $ / i. test ( parsedCollectionUri . hostname ) ;
166+ }
167+
168+ // Currently, there is a race condition of some sort that causes nuget to not send credentials sometimes
169+ // when using the credential provider.
170+ // Unfortunately, on on-premises TFS, we must use credential provider to override NTLM auth with the build
171+ // identity's token.
172+ // Therefore, we are enabling credential provider on on-premises and disabling it on hosted. We allow for test
173+ // instances by an override variable.
174+
175+ export function isCredentialProviderEnabled ( ) : boolean {
176+ // set NuGet.ForceEnableCredentialProvider to "true" to force allowing the credential provider flow, "false"
177+ // to force *not* allowing the credential provider flow, or unset/anything else to fall through to the
178+ // hosted environment detection logic
179+ const credentialProviderOverrideFlag = tl . getVariable ( "NuGet.ForceEnableCredentialProvider" ) ;
180+ if ( credentialProviderOverrideFlag === "true" ) {
181+ tl . debug ( "Credential provider is force-enabled for testing purposes." ) ;
182+ return true ;
183+ }
184+
185+ if ( credentialProviderOverrideFlag === "false" ) {
186+ tl . debug ( "Credential provider is force-disabled for testing purposes." ) ;
187+ return false ;
188+ }
189+
190+ if ( isHosted ( ) ) {
191+ tl . debug ( "Credential provider is disabled on hosted." ) ;
192+ return false ;
193+ }
194+ else {
195+ tl . debug ( "Credential provider is enabled." )
196+ return true ;
197+ }
198+ }
199+
200+ export function isCredentialConfigEnabled ( ) : boolean {
201+ // set NuGet.ForceEnableCredentialConfig to "true" to force allowing config-based credential flow, "false"
202+ // to force *not* allowing config-based credential flow, or unset/anything else to fall through to the
203+ // hosted environment detection logic
204+ const credentialConfigOverrideFlag = tl . getVariable ( "NuGet.ForceEnableCredentialConfig" ) ;
205+ if ( credentialConfigOverrideFlag === "true" ) {
206+ tl . debug ( "Credential config is force-enabled for testing purposes." ) ;
207+ return true ;
208+ }
209+
210+ if ( credentialConfigOverrideFlag === "false" ) {
211+ tl . debug ( "Credential config is force-disabled for testing purposes." ) ;
212+ return false ;
213+ }
214+
215+ // credentials in config will always fail for on-prem
216+ if ( ! isHosted ( ) ) {
217+ tl . debug ( "Credential config is disabled on on-premises TFS." ) ;
218+ return false ;
219+ }
220+ else {
221+ tl . debug ( "Credential config is enabled." )
222+ return true ;
223+ }
224+ }
225+
158226export function locateCredentialProvider ( ) : string {
159- return locateTool ( 'CredentialProvider.TeamBuild.exe' ) ;
160- }
227+ const credentialProviderLocation = locateTool ( 'CredentialProvider.TeamBuild.exe' ) ;
228+ if ( ! credentialProviderLocation ) {
229+ tl . debug ( "Credential provider is not present." ) ;
230+ return null ;
231+ }
232+
233+ return isCredentialProviderEnabled ( ) ? credentialProviderLocation : null ;
234+ }
235+
0 commit comments