AtlassianPS / JiraPS

PowerShell module to interact with Atlassian JIRA
https://AtlassianPS.org/module/JiraPS
MIT License
325 stars 131 forks source link

New-JiraSession Error "Invoke-JiraMethod : The 'permissions' query parameter is required." #393

Closed jeffadavidson closed 4 years ago

jeffadavidson commented 4 years ago

Context

My team has encountered the same issue as #350 and #353 that was meant to be fixed by a documentation change in #387

Description

My team and I are writing scripts to interact with a cloud JIRA instance. The pattern we have been following was to create a JiraPs session at the beginning of our script using New-JiraSession. We are not using Remove-JiraSession and the end of our script as the documentation has it flagged as deprecated.

Because we are connecting to a cloud JIRA instance we have each been using a Personal Access Token(PAT) to create a session, using our username and the PAT as a password. This was working fine for the last 2 weeks. As of this morning (Nov 28 2019 7:00am MST) I started getting the following error when using the New-Session function

Invoke-JiraMethod : The 'permissions' query parameter is required.
At <Path_Removed>
+         $result = Invoke-JiraMethod @parameter
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidResult: (:) [Invoke-JiraMethod], RuntimeException
    + FullyQualifiedErrorId : InvalidResponse.Status400,Invoke-JiraMe~

then over the next few hours, the same issue started happening to all my coworkers.

Steps To Reproduce

As of now we have not been able to reliably reproduce. Once it starts happening to a user it seems to keep happening. Below is the functions we are running

  Import-Module JiraPS
  $JiraCredential = New-Object System.Management.Automation.PSCredential ($JiraUsername, (ConvertTo-SecureString $JiraPersonalAccessToken -AsPlainText -Force))
  Set-JiraConfigServer $JiraServerUri
  New-JiraSession -Credential $JiraCredential

Expected behaviour is to get a valid JiraPS session. Actual behaviour is the error above

Possible Solution

Using the debug and verbose commands I was able to trace the code flow for a user experiencing the issue and a user not experiencing the issue.

User Without Issue:

PC C:> New-JiraSession -Credential Get-Credential -Verbose -Debug
VERBOSE: [New-JiraSession] Function started
VERBOSE: [Get-JiraConfigServer] Function started
DEBUG: [Get-JiraConfigServer] ParameterSetName: __AllParameterSets
DEBUG: [Get-JiraConfigServer] PSBoundParameters:
Key         Value
---         -----
ErrorAction  Stop

VERBOSE: [Get-JiraConfigServer] Complete
DEBUG: [New-JiraSession] ParameterSetName: __AllParameterSets
DEBUG: [New-JiraSession] PSBoundParameters:
Key        Value
---        -----
Credential System.Management.Automation.PSCredential
Verbose    True
Debug      True

DEBUG: [New-JiraSession] Invoking JiraMethod with $parameter 

Confirm
Continue with this operation?
[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"): y
VERBOSE: [Invoke-JiraMethod] Function started
DEBUG: [Invoke-JiraMethod] ParameterSetName: __AllParameterSets
DEBUG: [Invoke-JiraMethod] PSBoundParameters:
Key          Value
---          -----
Credential   System.Management.Automation.PSCredential
Method       Get
Headers      {}
URI          https://domain.atlassian.net/rest/api/2/myself
StoreSession True

VERBOSE: [ConvertTo-GetParameter] Making HTTP get parameter string out of a hashtable
VERBOSE:
Name                           Value

----                           -----

maxResults                     25

VERBOSE: [Invoke-JiraMethod] Get https://domain.atlassian.net/rest/api/2/myself?maxResults=25
DEBUG: [Invoke-JiraMethod] Invoke-WebRequest with $splatParameters:
Name                           Value

----                           -----

ErrorAction                    Stop

SessionVariable                newSessionVar

Headers                        {Accept-Charset, Accept}

ContentType                    application/json; charset=utf-8

Credential                     System.Management.Automation.PSCredential

Method                         Get

Uri                            https://domain.atlassian.net/rest/api/2/myself?maxResults=25

UseBasicParsing                True

Verbose                        False

Confirm
Continue with this operation?
[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"): y
DEBUG: [Invoke-JiraMethod] Executed WebRequest. Access $webResponse to see details

Confirm
Continue with this operation?
[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"): y
VERBOSE: [Test-ServerResponse] Checking response headers for authentication errors
DEBUG: [Test-ServerResponse] Investigating $InputObject.Headers['X-Seraph-LoginReason']
VERBOSE: [Invoke-JiraMethod] Status code: 200
DEBUG: [ConvertTo-JiraSession] Converting $InputObject to custom object

Confirm
Continue with this operation?
[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"): y
VERBOSE: [Invoke-JiraMethod] Function ended
DEBUG: [New-JiraSession] Adding session result to existing module PrivateData

VERBOSE: [New-JiraSession] Complete
Username                  WebSession
--------                  ----------
SomeUsername@domain.com Microsoft.PowerShell.Commands.WebRequestSession

User With Issue:

PS C:\ New-JiraSession -Credential Get-Credential -verbose -debug
VERBOSE: [New-JiraSession] Function started
VERBOSE: [Get-JiraConfigServer] Function started
DEBUG: [Get-JiraConfigServer] ParameterSetName: __AllParameterSets
DEBUG: [Get-JiraConfigServer] PSBoundParameters: 
Key         Value
---         -----
ErrorAction  Stop

VERBOSE: [Get-JiraConfigServer] Complete
DEBUG: [New-JiraSession] ParameterSetName: __AllParameterSets
DEBUG: [New-JiraSession] PSBoundParameters: 
Key        Value
---        -----
Credential System.Management.Automation.PSCredential
Verbose    True
Debug      True

DEBUG: [New-JiraSession] Invoking JiraMethod with $parameter

Confirm
Continue with this operation?
[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"): y
VERBOSE: [Invoke-JiraMethod] Function started
DEBUG: [Invoke-JiraMethod] ParameterSetName: __AllParameterSets
DEBUG: [Invoke-JiraMethod] PSBoundParameters: 
Key          Value
---          -----
Credential   System.Management.Automation.PSCredential
Method       Get
Headers      {}
URI          https://domain.atlassian.net/rest/api/2/mypermissions
StoreSession True

VERBOSE: [ConvertTo-GetParameter] Making HTTP get parameter string out of a hashtable
VERBOSE: 
Name                           Value

----                           -----

maxResults                     25

VERBOSE: [Invoke-JiraMethod] Get https://domain.atlassian.net/rest/api/2/mypermissions?maxResults=25
DEBUG: [Invoke-JiraMethod] Invoke-WebRequest with $splatParameters: 
Name                           Value

----                           -----

ErrorAction                    Stop

SessionVariable                newSessionVar

Headers                        {Accept-Charset, Accept}

ContentType                    application/json; charset=utf-8

Credential                     System.Management.Automation.PSCredential

Method                         Get

Uri                            https://domain.atlassian.net/rest/api/2/mypermissions?maxResults=25

UseBasicParsing                True

Verbose                        False

Confirm
Continue with this operation?
[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"): y
VERBOSE: [Invoke-JiraMethod] Failed to get an answer from the server
DEBUG: [Invoke-JiraMethod] Executed WebRequest. Access $webResponse to see details

Confirm
Continue with this operation?
[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"): y
VERBOSE: [Test-ServerResponse] Checking response headers for authentication errors
DEBUG: [Test-ServerResponse] Investigating $InputObject.Headers['X-Seraph-LoginReason']
VERBOSE: [Invoke-JiraMethod] Status code: BadRequest
VERBOSE: [Resolve-ErrorWebResponse] Function started
DEBUG: [Resolve-ErrorWebResponse] ParameterSetName: __AllParameterSets
DEBUG: [Resolve-ErrorWebResponse] PSBoundParameters: 
Key        Value
---        -----
Exception  The remote server returned an error: (400) Bad Request.
StatusCode BadRequest
Cmdlet     System.Management.Automation.PSScriptCmdlet

VERBOSE: [Resolve-ErrorWebResponse] Retrieved body of HTTP response for more information about the error ($responseBody)
DEBUG: [Resolve-ErrorWebResponse] Got the following error as $responseBody

Confirm
Continue with this operation?
[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"): y
Invoke-JiraMethod : The 'permissions' query parameter is required.
At <PATH>
+         $result = Invoke-JiraMethod @parameter
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidResult: (:) [Invoke-JiraMethod], RuntimeException
    + FullyQualifiedErrorId : InvalidResponse.Status400,Invoke-JiraMethod

VERBOSE: [Resolve-ErrorWebResponse] Function ended
VERBOSE: [Invoke-JiraMethod] Function ended
DEBUG: [New-JiraSession] Adding session result to existing module PrivateData
VERBOSE: [New-JiraSession] Complete

Issue

The main issue seems to be to be that for when reaching the Invoking JiraMethod with $parameter step. The working used calls the 'https://domain.atlassian.net/rest/api/2/myself' endpoint, while the non-working user calls 'https://domain.atlassian.net/rest/api/2/mypermissions'. I am unsure what causes this altered code flow.

The my mypermissions endpoint has been altered in the last year to require a permissions parameter. JiraPS is not providing this parameter, so the call fails.

Suggested fix

A few possible ways to fix this:

  1. Alter the New-JiraSession code flow to not hit the mypermissions
    • I am not sure what its used for so it might not be practical
  2. Add parameter argument to my permissions
    • Again depending on how the output from this request is used, then this might not work because you might have to specify every permission you will need
jeffadavidson commented 4 years ago

After investigating further, updating to from version 12.2.4 to 12.2.5 seems to have resolved the issue

lipkau commented 4 years ago

see description of #391

JohnLBevan commented 4 years ago

Note: 12.2.4 to 12.2.5 should read 2.12.4 to 2.12.5. https://www.powershellgallery.com/packages/JiraPS/2.12.5