rubrikinc / rubrik-sdk-for-powershell

Rubrik Module for PowerShell
https://build.rubrik.com/sdks/powershell/
MIT License
102 stars 87 forks source link

Connect-Rubrik cmdlet throws error in powershell 5.1 when using with service account and secretid #829

Closed JanosPesti closed 2 months ago

JanosPesti commented 1 year ago

Current Behavior

PowerShell When using Windows PowerShell 5.1, Rubrik module 6.0.1 and service accounts, the Connect-Rubrik command gives an error related to

Invoke-RestMethod : A parameter cannot be found that matches parameter name 'SkipCertificateCheck'.
At <path retracted>\WindowsPowerShell\Modules\Rubrik\6.0.1\Public\Connect-Rubrik.ps1:160 char:43

On PowerShell 7 it works fine. Verbose output on PS 5.1:

VERBOSE: Allowing self-signed certificates
VERBOSE: Gather API Data for Connect-Rubrik
VERBOSE: Gather API Data for Get-RubrikSoftwareVersion
VERBOSE: Selected 1.0 API Data for Get-RubrikSoftwareVersion
VERBOSE: Load API data for Get-RubrikSoftwareVersion
VERBOSE: Description: Retrieves software version of the Rubrik cluster
VERBOSE: Build the URI
VERBOSE: URI = https://<cluster retracted>/api/v1/cluster/me/version
VERBOSE: Build the query parameters for <null>
VERBOSE: URI = https://<cluster retracted>/api/v1/cluster/me/version
VERBOSE: Submitting the request
VERBOSE: Invoking request with a custom timeout of 100 seconds
VERBOSE: GET https://<cluster retracted>/api/v1/cluster/me/version with 0-byte payload
VERBOSE: received 28-byte response of content type application/json
VERBOSE: Received HTTP Status 200
VERBOSE: Formatting return value
VERBOSE: Filter the results
VERBOSE: Selected 3.0 API Data for Connect-Rubrik
VERBOSE: Load API data for Connect-Rubrik
VERBOSE: Description: Create a new login session
VERBOSE: Using User Agent RubrikPowerShellSDK-6.0.1--5.1.19041.2364--platform--Win32NT--platform_version--Microsoft Windows 10 Enterprise19044
Invoke-RestMethod : A parameter cannot be found that matches parameter name 'SkipCertificateCheck'.
At <path>\WindowsPowerShell\Modules\Rubrik\6.0.1\Public\Connect-Rubrik.ps1:160 char:43
+             $response = Invoke-RestMethod @RestSplat -Verbose
+                                           ~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

The variable cannot be validated because the value  is not a valid value for the Token variable.
At <path>\WindowsPowerShell\Modules\Rubrik\6.0.1\Public\Connect-Rubrik.ps1:161 char:13
+             $Token = $response.token
+             ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ValidationMetadataException
    + FullyQualifiedErrorId : ValidateSetFailure

VERBOSE: Storing all connection details into $global:rubrikConnection
VERBOSE: Gather API Data for Get-RubrikAPIVersion
VERBOSE: Selected 1.0 API Data for Get-RubrikAPIVersion
VERBOSE: Load API data for Get-RubrikAPIVersion
VERBOSE: Description: Retrieves software version of the Rubrik cluster
VERBOSE: Build the URI
VERBOSE: URI = https://<cluster retracted>/api/v1/cluster/me/api_version
VERBOSE: Build the query parameters for <null>
VERBOSE: URI = https://<cluster retracted>/api/v1/cluster/me/api_version
VERBOSE: Submitting the request
VERBOSE: Invoking request with a custom timeout of 100 seconds
VERBOSE: GET https://<cluster retracted>/api/v1/cluster/me/api_version with 0-byte payload
VERBOSE: received 18-byte response of content type application/json
VERBOSE: Received HTTP Status 200
VERBOSE: Formatting return value
VERBOSE: Filter the results
VERBOSE: Gather API Data for Get-RubrikSoftwareVersion
VERBOSE: Selected 1.0 API Data for Get-RubrikSoftwareVersion
VERBOSE: Load API data for Get-RubrikSoftwareVersion
VERBOSE: Description: Retrieves software version of the Rubrik cluster
VERBOSE: Build the URI
VERBOSE: URI = https://<cluster retracted>/api/v1/cluster/me/version
VERBOSE: Build the query parameters for <null>
VERBOSE: URI = https://<cluster retracted>/api/v1/cluster/me/version
VERBOSE: Submitting the request
VERBOSE: Invoking request with a custom timeout of 100 seconds
VERBOSE: GET https://<cluster retracted>/api/v1/cluster/me/version with 0-byte payload
VERBOSE: received 28-byte response of content type application/json
VERBOSE: Received HTTP Status 200
VERBOSE: Formatting return value
VERBOSE: Filter the results
VERBOSE: Adding connection details into the $global:RubrikConnections array

Name                           Value                                                                                                                                                      
----                           -----                                                                                                                                                      
authType                       ServiceAccount                                                                                                                                             
api                            1                                                                                                                                                          
id                                                                                                                                                                                        
server                         <servername>                                                                                                                            
version                        7.0.4-p2-16260                                                                                                                                             
header                         {User-Agent, Authorization}                                                                                                                                
time                           03/03/2023 13:48:16                                                                                                                                        
userId    

Expected Behavior

Not throw an error and store a token in $global:RubrikConnection Like on PowerShell 7

Name                           Value
----                           -----
server                         <cluster>
time                           03/03/2023 13:57:19
version                        7.0.4-p2-16260
id
userId
authType                       ServiceAccount
api                            1
header                         {[Authorization, Bearer <token retracted>

Steps to Reproduce

Start Windows PowerShell 5.1

Ipmo Rubrik
Connect-Rubrik -Server <cluster> -Id <userid> -Secret <secretid> -OrganizationID <orgid>

Context

Get-RubrikDebugInfo

PSVersion                     : 5.1.19041.2364
PSEdition                     : Desktop
PSCompatibleVersions          : {1.0, 2.0, 3.0, 4.0...}
BuildVersion                  : 10.0.19041.2364
CLRVersion                    : 4.0.30319.42000
WSManStackVersion             : 3.0
PSRemotingProtocolVersion     : 2.3
SerializationVersion          : 1.1.0.1
HostConsoleName               : Windows PowerShell ISE Host
HostConsoleVersion            : 5.1.19041.2364
HostCulture                   : en-GB
HostCultureUI                 : en-US
RubrikConnection              : True
UserAgentString               : RubrikPowerShellSDK-6.0.1--5.1.19041.2364--platform--Win32NT--platform_version--Microsoft Windows 10 Enterprise19044
RubrikAuthentication          : Bearer
RubrikClusterVersion          : 7.0.4-p2-16260
RubrikCurrentModuleVersion    : 6.0.1
RubrikInstalledModule         : 6.0.1
RubrikModuleOptions           : ApplyCustomViewDefinitions = True; CredentialPath = ; DefaultWebRequestTimeOut = 100
RubrikModuleDefaultParameters :

Failure Logs

No response

JanosPesti commented 1 year ago

Can a check be included and if PS version is less than 6, don't put in the SkipCertificateCheck in the payload? like this in line 150 of Connect-Rubrik.ps1

            if (Test-PowerShellSix) {
                $RestSplat = @{
                    Method = 'Post'
                    ContentType = "application/json"
                    URI = "https://$Server/api/v1/service_account/session"
                    SkipCertificateCheck = $true
                    Body = @{
                        serviceAccountId = "$($Id)"
                        secret = "$($Secret)"
                    } | ConvertTo-Json
                }

            }
            else {            
                $RestSplat = @{
                    Method = 'Post'
                    ContentType = "application/json"
                    URI = "https://$Server/api/v1/service_account/session"
                    Body = @{
                        serviceAccountId = "$($Id)"
                        secret = "$($Secret)"
                    } | ConvertTo-Json
                }
            }
            $response = Invoke-RestMethod @RestSplat -Verbose

I tested on ps 5.1 and 7 and it works, however I have root signed SSL certs installed. need a test on insecure connections.

sitecorepm commented 1 year ago

Looks like this was fixed here: https://github.com/rubrikinc/rubrik-sdk-for-powershell/commit/004c450ef712a8701ec8ac43c7fccb0071cb492d

When will this be released?

mwpreston commented 2 months ago

This has been merged.