Error: credentials argument needs to implement signRequest method?

When I get the Azure Credentials using "ms-rest-azure" library.

let credentials = await msRestAzure.loginWithServicePrincipalSecret(id, secret, tanent); let newCred = JSON.stringify(credentials); let duplicateCred = JSON.parse(newCred);  // Create duplicate credentials object 

Now if I going to use the "duplicateCred" for my further Azure function call, then I’ll get the above error. But if I’m using "credentials", then all is well. So how to I assign the "credentials" object to other variable? So that i’ll use the variable for my future azure API call.

Example:

let credentials = await msRestAzure.loginWithServicePrincipalSecret(id, secret, tanent); let newCred = JSON.stringify(credentials); let duplicateCred = JSON.parse(newCred); // Create duplicate credentials object  // Okay, here I'm getting the proper client object. Because I am using "credentials" in the below line of code. // I'm getting the results from the below lines of code. const client = new MonitorManagementClient(credentials, subscription);   const results = await client.operations.list(); context.log('results==> ', results);  // Error, here not getting the proper client object. Because I am using "duplicateCred" as credentials in the below line of code. // I'm not getting the results from the below lines of code. // At the below line I'm getting the above error. const client = new MonitorManagementClient(duplicateCred, subscription);   const results = await client.operations.list(); context.log('results==> ', results); 

How do I create a duplicate credential object from actual credentials object ?

Add Comment
1 Answer(s)

This is incorrect method to do so. Credentials returned by msRestAzure.loginWithServicePrincipalSecret call is not a plain object it’s an instance of ApplicationTokenCredentials. When you call JSON.stringify you loose this class reference and convert the whole value to Object. So cloned credentials object looses prototype chain with ApplicationTokenCredentials methods.

Also ApplicationTokenCredentials has no cloning logic what could mean it shouldn’t be cloned. Using your own cloning logic could conflict with future versions of Azure library. It’s better to add an issue in the official Azure SDK repository if you need to have a cloning method.

Note! ⚠️ Such way of cloning could be dangerous and leads to unexpected behaviour. You should strongly avoid it, except of cases when you really need to erase value’s prototype chain!

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.