Fixes for a couple @azure/core-http issues found during arm-* migration#4335
Fixes for a couple @azure/core-http issues found during arm-* migration#4335daviwil merged 2 commits intoAzure:masterfrom
Conversation
There was a problem hiding this comment.
I can't say that this is bad per se, but in my experience function length checks come back to haunt. Preserving function length during transpilation is... hit or miss. Parameters with defaults don't contribute to length either, so making getToken's parameter optional would make it pass this check. If there's no other way this seems fine but let's be careful!
There was a problem hiding this comment.
Where is it exactly that we want to differentiate between the ms-rest-nodeauth credential and the TokenCredential?
I wonder, instead of checking for isTokenCredential(), if we should check for the other type
The TokenClientCredentials has 2 functions signRequest and getToken where as TokenCredential has only one of these. So there is our differentiator...
There was a problem hiding this comment.
Yep, that's what I was planning to try after @bterlson's comment. Just updated the PR with that fix and a test to verify it.
There was a problem hiding this comment.
Perhaps add a comment here as to why the explicit check on signRequest? Will help others who have no idea of ms-rest-nodeauth
There was a problem hiding this comment.
Good point, thanks!
There was a problem hiding this comment.
Comment added:
// Check for an object with a 'getToken' function but without
// a 'signRequest' function. We do this check to make sure that
// a ServiceClientCredentials implementor (like TokenClientCredentials
// in ms-rest-nodeauth) doesn't get mistaken for a TokenCredential.
This change fixes two issues that were identified when trying to use
ms-rest-nodeauthwith@azure/arm-resourceswhen testing PR #4315:When a credential type from
ms-rest-nodeauthwhich implementsTokenClientCredentialswas passed to theResourceManagementClient,isTokenCredentialwas mistaking it for aTokenCredential(they both have agetTokenmethod) and then used the wrong authentication policy. This change checks whether the credential'sgetTokenfunction accepts parameters before assuming it's aTokenCredentialsince theTokenClientCredentials.getTokenmethod takes no parameters.In Preview 1,
@azure/core-http'sServiceClientimplementation was incorrectly hardcoding the scope of all authentication requests to/.defaultinstead of using the service-qualified scope name (i.e.https://management.azure.com/.default). This worked OK for data plane services but failed immediately for management plane services that do not accept the backcompat/.defaultscope.The solution for #2 is unfortunately a bit of a necessary hack.
ServiceClientimplementations that are generated for ARM libraries must set thebaseUriof their subclass instance to the service URI after the baseServiceClientconstructor completes. BecausebaseUriis set late, thebearerTokenAuthenticationPolicyfactory cannot be given ascopewhich includes thebaseUri.The fix here is to use a factory wrapper which accesses the
ServiceClient.baseUriproperty right at the time thebearerTokenAuthenticationPolicyfactory is first invoked so that the populated value gets used to create thescopestring.The alternative would be to change the constructor (or options type) of
ServiceClientto accept thebaseUriso that it can be used earlier in the initialization process, but that would then require additional changes toautorest.typescriptto wire up thebaseUriassignment correctly. I thought that this solution would be less trouble, but I'm happy to go for the full solution if folks think it's more appropriate.