huserben / TfsExtensions

Extensions for TFS 2015+ such as custom Widgets (require TFS 2017) and Build Tasks
MIT License
45 stars 22 forks source link

Failed Authentication should fail Task and output a proper Message #25

Closed Koshak118 closed 6 years ago

Koshak118 commented 7 years ago

Hi Benjamin

First of all thanks for doing a great work with you extension which saves a lot of work for many ppl which use VSTS.

I've been using your extension for a couple of month and today the 1st version of it stopped worked all of a sudden with an authentication error (it uses Basic Authentication):

2017-08-29T13:32:30.5527745Z ##[error]System.Net.WebException: The remote server returned an error: (401) Unauthorized.
2017-08-29T13:32:30.5527745Z    at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
2017-08-29T13:32:30.5537746Z    at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
2017-08-29T13:32:30.5547747Z ##[error]System.Management.Automation.RuntimeException: Cannot index into a null array.
2017-08-29T13:32:30.5547747Z    at CallSite.Target(Closure , CallSite , Object , Int32 )
2017-08-29T13:32:30.5547747Z    at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
2017-08-29T13:32:30.5547747Z    at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
2017-08-29T13:32:30.5547747Z    at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
2017-08-29T13:32:30.5547747Z Queue new Build for definition Internals Build on https://QQQ.visualstudio.com/QQQ/_apis/build/builds?api-version=2.0
2017-08-29T13:32:30.5547747Z { definition: { id:  }, sourceBranch: "refs/heads/master", requestedFor: { id: "7fb404ba-eecb-4fc7-a868-44a743150a58"}}
2017-08-29T13:32:30.7987585Z ##[error]System.Net.WebException: The remote server returned an error: (401) Unauthorized.
2017-08-29T13:32:30.8007597Z    at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
2017-08-29T13:32:30.8017617Z    at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
2017-08-29T13:32:30.9613808Z ##[error]System.Net.WebException: The remote server returned an error: (401) Unauthorized.
2017-08-29T13:32:30.9623804Z    at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
2017-08-29T13:32:30.9693784Z    at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
2017-08-29T13:32:30.9693784Z Queued new Build for Definition Internals Build: https://QQQ.visualstudio.com/QQQ/_build/index?buildId=
2017-08-29T13:32:30.9803777Z ##[error]PowerShell script completed with 4 errors.

After I switched it to the 2.0 version the task ends up with "Cannot read property 'count' of undefined" error. The whole output is as the following:

2017-08-29T13:38:35.8928130Z ##[section]Starting: Trigger Say2eat.Internals build
2017-08-29T13:38:35.8938198Z ==============================================================================
2017-08-29T13:38:35.8938198Z Task         : Trigger Build
2017-08-29T13:38:35.8938198Z Description  : This tasks allows to trigger a new Build (add it to the queue) as part of a Build Definition. It contains as well some conditions that can be applied, for example if the last build of certain definition was successful or not.
2017-08-29T13:38:35.8938198Z Version      : 2.1.0
2017-08-29T13:38:35.8938198Z Author       : Benjamin Huser
2017-08-29T13:38:35.8938198Z Help         : 
2017-08-29T13:38:35.8938198Z ==============================================================================
2017-08-29T13:38:37.1284918Z Using Custom Team Project Url
2017-08-29T13:38:37.1284918Z Path to Server: https://QQQ.visualstudio.com/QQQ
2017-08-29T13:38:37.1844920Z Using Basic Authentication
2017-08-29T13:38:37.2094934Z Build shall be triggered for same user that triggered current build: QQQ QQQ
2017-08-29T13:38:37.2094934Z Using same branch as source version: refs/heads/master
2017-08-29T13:38:39.2686533Z ##[error]Cannot read property 'count' of undefined
2017-08-29T13:38:39.2756512Z ##[section]Finishing: Trigger Say2eat.Internals build

Can you please advise?

Koshak118 commented 7 years ago

Update: Unchecking of "Use same source branch as triggered build" didn't help, it ends up with the same error

huserben commented 7 years ago

Hi @Koshak118

it seems that the tasks fail as soon as we want to fetch something from the TFS REST API. If it worked fine and suddenly stops I would say something on the TFS has changed, maybe someone disabled Basic Authentication for some reason?

To verify you could try to change it quickly to use the OAuth Token and try with that configuration.

huserben commented 7 years ago

Hi @Koshak118 did you make any progress on this issue?

Koshak118 commented 6 years ago

Hi @huserben

Nope, didn't have a time to deal with that yet too much, but from the source code it looks like there are 2 places where you check the "count" parameter of the "result" variable - after dealing with authentication and after dealing with artifacts.

So it seems that "result" is null after the authentication error, so checking it before checking "result.count >0" would help to give a more comprehensive error

Regardless, could please help with where the Authentication methods are configured in the VSTS?

huserben commented 6 years ago

Hi @Koshak118 thanks for the input, that's true, I will have a look at making a bit more meaningful message if I see that the authentication failed.

You mean how to configure it? If you select the Task you can expand the "Authentication" part and configure it in more detail. See as well the description provided here: https://marketplace.visualstudio.com/items?itemName=benjhuser.tfs-extensions-build-tasks

Koshak118 commented 6 years ago

Hi @huserben

I mean in the docs of your extension you write "If you have enabled Basic Authentication you can as well use this sort of authentication by providing the username and password".

So that "Basic Authentication" is enabled anywhere in VSTS? I mean since this authentication suddenly stopped working without any change in your extension, it might some external change which happened. Any ideas what was that?

huserben commented 6 years ago

Hi @Koshak118 yes, you can relate to this article where it is explained how to get this working: https://binary-stuff.com/post/how-to-enable-alternate-credentials-in-visual-studio-online-vso

However as an alternate solution it should be rather simple to switch to the OAuth Token Authentication if you just want to verify if it is in general still working.

huserben commented 6 years ago

Hi @Koshak118 yes, you can relate to this article where it is explained how to get this working: https://binary-stuff.com/post/how-to-enable-alternate-credentials-in-visual-studio-online-vso

However as an alternate solution it should be rather simple to switch to the OAuth Token Authentication if you just want to verify if it is in general still working.

Koshak118 commented 6 years ago

Hi @huserben

I managed to get it working with Personal Tokens So unless you don't want to keep this issue to fix the null "result" variable you can close it

Kudos again to the great extension

huserben commented 6 years ago

Hi

cool, very good :-) Did you by any chance figure out why the Basic Auth suddenly stop working (just curious if something changed automatically in VSTS or something...)

I'll keep it open, it's a good reminder that I have something open to be fixed in a next version. Especially as right now I don't have too much time and as this is not the most pressing thing it might take some time till I will add it.

And thanks for the nice words and especially for reporting issues here and helping making the extension better.

huserben commented 6 years ago

With my next update of the Task (sheduled somewhen this week depending on how the testing goes) I will add this improvement in the logging and output a proper message including some "common pitfalls" for the different authentication methods: grafik

Edit: Just updated the Task to version 2.2 which includes the above mentioned change.