Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Unable to run in azure functions #234

Closed
Shadoxity opened this issue Dec 23, 2019 · 16 comments
Closed

Unable to run in azure functions #234

Shadoxity opened this issue Dec 23, 2019 · 16 comments
Labels
bug Something isn't working

Comments

@Shadoxity
Copy link

I am trying to set up a number of scripts in azure functions to run maintenance etc on our customer tenants but I cannot get it to produce a token.
The same commands work fine when running from powershell on my machine

Steps to reproduce

Create an azure function, load the modules for partner center

Using command

$token = New-PartnerAccessToken -ApplicationId $client_Id -Credential $credential -RefreshToken $refreshToken -Scopes 'https://api.partnercenter.microsoft.com/user_impersonation' -ServicePrincipal -Tenant $tenant_id

Fails

Expected behavior

Create a token to use for partner center powershell access

Actual behavior

Error message
ERROR: New-PartnerAccessToken : Error reading JObject from JsonReader. Path '', line 0, position 0.
At D:\home\site\wwwroot\RefreshToken\run.ps1:29 char:1

  • New-PartnerAccessToken -ApplicationId $client_Id -Credential $credent ...
  • CategoryInfo : CloseError: (:) [New-PartnerAccessToken], JsonReaderException
  • FullyQualifiedErrorId : Microsoft.Store.PartnerCenter.PowerShell.Commands.NewPartnerAccessToken

Script stack trace:
at , D:\home\site\wwwroot\RefreshToken\run.ps1: line 29

Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Path '', line 0, position 0.
at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)
at Newtonsoft.Json.Linq.JObject.Parse(String json, JsonLoadSettings settings)
at Newtonsoft.Json.Linq.JObject.Parse(String json)
at Microsoft.Store.PartnerCenter.PowerShell.Commands.NewPartnerAccessToken.b__54_0()
at Microsoft.Store.PartnerCenter.PowerShell.Utilities.ConcurrencyTaskScheduler.RunConcurrentTaskAsync(Int64 taskId, Task task)

Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException : Result: ERROR: New-PartnerAccessToken : Error reading JObject from JsonReader. Path '', line 0, position 0.
At D:\home\site\wwwroot\RefreshToken\run.ps1:29 char:1

  • New-PartnerAccessToken -ApplicationId $client_Id -Credential $credent ...
  • CategoryInfo : CloseError: (:) [New-PartnerAccessToken], JsonReaderException
  • FullyQualifiedErrorId : Microsoft.Store.PartnerCenter.PowerShell.Commands.NewPartnerAccessToken

Script stack trace:
at , D:\home\site\wwwroot\RefreshToken\run.ps1: line 29

Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Path '', line 0, position 0.
at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)
at Newtonsoft.Json.Linq.JObject.Parse(String json, JsonLoadSettings settings)
at Newtonsoft.Json.Linq.JObject.Parse(String json)
at Microsoft.Store.PartnerCenter.PowerShell.Commands.NewPartnerAccessToken.b__54_0()
at Microsoft.Store.PartnerCenter.PowerShell.Utilities.ConcurrencyTaskScheduler.RunConcurrentTaskAsync(Int64 taskId, Task task)

Exception: Error reading JObject from JsonReader. Path '', line 0, position 0.
Stack: at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)
at Newtonsoft.Json.Linq.JObject.Parse(String json, JsonLoadSettings settings)
at Newtonsoft.Json.Linq.JObject.Parse(String json)
at Microsoft.Store.PartnerCenter.PowerShell.Commands.NewPartnerAccessToken.b__54_0()
at Microsoft.Store.PartnerCenter.PowerShell.Utilities.ConcurrencyTaskScheduler.RunConcurrentTaskAsync(Int64 taskId, Task task)
2019-12-23T04:29:03.193 [Error] ERROR: Trace-PipelineObject : Cannot bind argument to parameter 'InputObject' because it is null.

  • CategoryInfo : InvalidData: (:) [Trace-PipelineObject], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Functions.PowerShellWorker.Commands.TracePipelineObjectCommand

Script stack trace:
at , D:\home\site\wwwroot\RefreshToken\run.ps1: line 31

System.Management.Automation.ParameterBindingValidationException: Cannot bind argument to parameter 'InputObject' because it is null.
at System.Management.Automation.CmdletParameterBinderController.BindValueFromPipeline(PSObject inputToOperateOn, MergedCompiledCommandParameter parameter, ParameterBindingFlags flags)
at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingStateInParameterSet(PSObject inputToOperateOn, CurrentlyBinding currentlyBinding, UInt32 validParameterSets)
at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingState(PSObject inputToOperateOn, CurrentlyBinding currentlyBinding, UInt32 validParameterSets)
at System.Management.Automation.CmdletParameterBinderController.BindPipelineParametersPrivate(PSObject inputToOperateOn)
at System.Management.Automation.CmdletParameterBinderController.BindPipelineParameters(PSObject inputToOperateOn)
at System.Management.Automation.CommandProcessor.Read()

Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException : Result: ERROR: Trace-PipelineObject : Cannot bind argument to parameter 'InputObject' because it is null.

  • CategoryInfo : InvalidData: (:) [Trace-PipelineObject], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Functions.PowerShellWorker.Commands.TracePipelineObjectCommand

Script stack trace:
at , D:\home\site\wwwroot\RefreshToken\run.ps1: line 31

System.Management.Automation.ParameterBindingValidationException: Cannot bind argument to parameter 'InputObject' because it is null.
at System.Management.Automation.CmdletParameterBinderController.BindValueFromPipeline(PSObject inputToOperateOn, MergedCompiledCommandParameter parameter, ParameterBindingFlags flags)
at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingStateInParameterSet(PSObject inputToOperateOn, CurrentlyBinding currentlyBinding, UInt32 validParameterSets)
at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingState(PSObject inputToOperateOn, CurrentlyBinding currentlyBinding, UInt32 validParameterSets)
at System.Management.Automation.CmdletParameterBinderController.BindPipelineParametersPrivate(PSObject inputToOperateOn)
at System.Management.Automation.CmdletParameterBinderController.BindPipelineParameters(PSObject inputToOperateOn)
at System.Management.Automation.CommandProcessor.Read()

Exception: Cannot bind argument to parameter 'InputObject' because it is null.
Stack: at System.Management.Automation.CmdletParameterBinderController.BindValueFromPipeline(PSObject inputToOperateOn, MergedCompiledCommandParameter parameter, ParameterBindingFlags flags)
at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingStateInParameterSet(PSObject inputToOperateOn, CurrentlyBinding currentlyBinding, UInt32 validParameterSets)
at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingState(PSObject inputToOperateOn, CurrentlyBinding currentlyBinding, UInt32 validParameterSets)
at System.Management.Automation.CmdletParameterBinderController.BindPipelineParametersPrivate(PSObject inputToOperateOn)
at System.Management.Automation.CmdletParameterBinderController.BindPipelineParameters(PSObject inputToOperateOn)
at System.Management.Automation.CommandProcessor.Read()

Diagnostic logs

Please share test platform diagnostics logs.
The logs may contain test assembly paths, kindly review and mask those before sharing.

Environment

Azure functions with partner center and msonline module loaded.
Tried using version 2 and 3

@ghost ghost self-assigned this Dec 23, 2019
@ghost ghost added the bug Something isn't working label Dec 23, 2019
@ghost
Copy link

ghost commented Dec 23, 2019

@Shadoxity thank you for opening this issue. More than likely this error has to deal with how the New-PartnerAccessToken command tries to get refresh token value from the cache. It will do this every time the command is invoked. So, it definitely makes sense that you would be encountering this error with both version 2.0 and 3.0. Let me see if I can figure out why this is happening and how best to resolve it.

@ghost ghost added the Azure Functions label Dec 23, 2019
@ghost
Copy link

ghost commented Dec 23, 2019

@Shadoxity what type of service plan are you using? I have been testing using a consumption plan and everything is working as excepted. Also, would you be able to share the value for the FUNCTIONS_EXTENSION_VERSION configuration?

The following is a screenshot of my configuration where this is currently working

image

Having this information will be extremely helpful in reproducing the error

@Shadoxity
Copy link
Author

Hey @IsaiahWilliams, thanks for your help.
Sure, here is the image. I am trying this on an app service plan. I will try a consumption based to see what happens

image

@Shadoxity
Copy link
Author

I got the same error running the code in a consumption azure function.
It has partner center 3.0.2 module, im using modules in the wwwroot\Modules area

@ghost
Copy link

ghost commented Dec 23, 2019

@Shadoxity thank you for sharing thia information. This is an interesting issue, that I am working to reproduce. Currently the only difference I see is how the dependencies are managed. You can read about how I got the module installed at

https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-powershell#dependency-management

Let me dig into this further to see if I can find the root cause.

@Shadoxity
Copy link
Author

@IsaiahWilliams How do I add the modules in that method? Where is the requirements.psd1 file? Can you share the contents of yours and I can try that way

@ghost
Copy link

ghost commented Dec 23, 2019

@Shadoxity I am using the Azure Functions extension for Visual Studio Code to deploy. The following figure shows what is being deployed

image

The following figure shows what was actually deployed

image

Note the .IdentityService directory was created by the module and is not part of the deployment. All of this means you can create the requirements.psd1 file in the root of the wwwroot direct. With my testing I used the following

# This file enables modules to be automatically managed by the Functions service.
# See https://aka.ms/functionsmanageddependency for additional information.
#
@{
    'Az' = '3.*'
    'PartnerCenter' = '3.*'
}

The Az module is not necessary, it was just part of the template I used to generate my test app.

@Shadoxity
Copy link
Author

I removed the modules directory, added the partnercenter into the requirements.psd1 file. Restarted the automation and ran again. still gives the same error.

For reference this is what I am trying to run and it works via powershell normally.

$client_id = ""
$client_secret = ""
$tenant_id = "domain"
$secpasswd = ConvertTo-SecureString $client_secret -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($client_id, $secpasswd)
$refreshToken = ""

$token = New-PartnerAccessToken -ApplicationId $client_Id -Credential $credential -RefreshToken $refreshToken -Scopes 'https://api.partnercenter.microsoft.com/user_impersonation' -ServicePrincipal -Tenant $tenant_id

write-output $token

@ghost
Copy link

ghost commented Dec 23, 2019

@Shadoxity that is similar to what my test script is doing. I have found a way to reproduce the issue, and if you need a temporary work around use the following for your requirements.psd1

# This file enables modules to be automatically managed by the Functions service.
# See https://aka.ms/functionsmanageddependency for additional information.
#
@{
    'Az' = '3.*'
    'PartnerCenter' = '3.*'
}

This particular issue will be resolved, but I wanted to make sure you are not blocked while I work to pinpoint the exact root cause.

@Shadoxity
Copy link
Author

Shadoxity commented Dec 23, 2019 via email

@ghost
Copy link

ghost commented Dec 24, 2019

@Shadoxity through my testing it should take a couple of minutes to work as expected. Also, I wanted to share that I recently made some changes to the way the New-PartnerAccessToken interacts with the token cache to prevent this issue. This change will be included in the next release which is slated for the week of January 6, 2020.

@Shadoxity
Copy link
Author

@IsaiahWilliams How do you mean a couple mins? Like open the app and let it sit for a while? Does this mean it will fail running on a timer?

I have opened it again and still getting the same errors

Exception: Cannot bind argument to parameter 'InputObject'

@ghost
Copy link

ghost commented Dec 24, 2019

@Shadoxity if the above does not work, then you can try including the following before the New-PartnerAccessToken command

if($env:LOCALAPPDATA)
{
    $path = "$($env:LOCALAPPDATA)\.IdentityService"

    if(-not (Test-Path $path))
    {
        New-Item -Path "$path\msal.cache" -ErrorAction Stop -Force | Out-Null
    }
}

Once the next update, which will be release 3.0.2, is published you will no longer need this.

@Shadoxity
Copy link
Author

@IsaiahWilliams I tried adding that in and unfortunately it made no difference. Still get the same error :(

@Shadoxity
Copy link
Author

@IsaiahWilliams and just to note, if I run the same code in an azure automation runbook it works perfectly fine.

I guess I may use things that way till this is fixed considering we cannot find a work around to make it work?

@ghost
Copy link

ghost commented Dec 27, 2019

@Shadoxity I decided to release the next version early to help unblock a handful of scenarios. Starting with version 3.0.3 this issue has been addressed.

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant