REST API

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements

Welcome Idira to LIVEcommunity!

Our community just got stronger! Explore new identity security resources, connect with experts, and discover more ways to learn and collaborate.

Meet Idira

REST API

L4 Transporter

Hello All we are trying to on board safe via REST API however while executing the Powershell script I encounter the below error . I have made the changes in web.config file changing the binding to "https". 

Please advise 

 

Enter user id:: admin1

StatusCode:  

StatusDescription:  

Response: The underlying connection was closed: An unexpected error occurred on a receive.

8 REPLIES 8

L7 Applicator

Does the user that runs the PS script trust the PVWAs SSL cert?

Are you definitely using the right logon URL?

Does the error occur when the script log on or when it actually performs a Safe operation?

L4 Transporter

Hello BernhardB,

Thanks for your prompt response I have been running with my domain ID ,

Can you guide how I could create trust for domain ID on PVWA's SSL cert ?

Yes my colleague is able to logon perfectly from desktop and I have access to client network via Jump box so encounter error.

 

Attached is the script for your reference and we are using CyberArk authentication

 

function PASREST-Logon {

if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)

{

$certCallback = @"

  using System;

  using System.Net;

  using System.Net.Security;

  using System.Security.Cryptography.X509Certificates;

  public class ServerCertificateValidationCallback

  {

    public static void Ignore()

    {

      if(ServicePointManager.ServerCertificateValidationCallback ==null)

      {

        ServicePointManager.ServerCertificateValidationCallback += 

          delegate

          (

            Object obj, 

            X509Certificate certificate, 

            X509Chain chain, 

            SslPolicyErrors errors

          )

          {

            return true;

          };

      }

    }

  }

"@

  Add-Type $certCallback

 }

[ServerCertificateValidationCallback]::Ignore()

  # Declaration

  $webServicesLogon = "$PVWA_URL/PasswordVault/WebServices/auth/Cyberark/CyberArkAuthenticationService.svc/Logon"

 

  # Authentication

  $uid=Read-Host -Prompt "Enter user id:"

  $Pwd = Read-Host -Prompt "Enter password" -AsSecureString

  $pwd1=[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Pwd))

 

  $bodyParams = @{username = "$uid"; password = "$pwd1"} | ConvertTo-JSON

  #write-host $bodyParams

   

  # Execution

  try {

    $logonResult = Invoke-RestMethod -Uri $webServicesLogon -Method POST -ContentType "application/json" -Body $bodyParams -ErrorVariable logonResultErr

    Return $logonResult.CyberArkLogonResult

  }

  catch {

    Write-Host "StatusCode: " $_.Exception.Response.StatusCode.value__

    Write-Host "StatusDescription: " $_.Exception.Response.StatusDescription

    Write-Host "Response: " $_.Exception.Message

    Return $false

  }

}

 

function PASREST-Logoff ([string]$Authorization) {

 

  # Declaration

  $webServicesLogoff = "$PVWA_URL/PasswordVault/WebServices/auth/Cyberark/CyberArkAuthenticationService.svc/Logoff"

 

  # Authorization

  $headerParams = @{}

  $headerParams.Add("Authorization",$Authorization)

 

  # Execution

  try {

    $logoffResult = Invoke-RestMethod -Uri $webServicesLogoff -Method POST -ContentType "application/json" -Header $headerParams -ErrorVariable logoffResultErr

    Return $true

  }

  catch {

    Write-Host "StatusCode: " $_.Exception.Response.StatusCode.value__

    Write-Host "StatusDescription: " $_.Exception.Response.StatusDescription

    Write-Host "Response: " $_.Exception.Message

    Return $false

  }

}

 

function PASREST-ADDSAFE ([string]$Authorization) {

 

  # Declaration

  $webServicesGA = "$PVWA_URL/PasswordVault/WebServices/PIMServices.svc/SAFES"

 

  # Authorization

  $headerParams = @{}

  $headerParams.Add("Authorization",$sessionID)

 

  $bodyParams = @{safe = @{SafeName = "TESTSafe3"; Description = "Testsafe for testing"; OLACenabled = "true"; ManagingCPM = "PasswordManager"; NumberOfDaysRetention = "7"}} | ConvertTo-JSON

  write-host $bodyparams

  # Execution

  try {

    $getAccountResult = Invoke-RestMethod -Uri $webServicesGA -Method POST -ContentType "application/json" -Headers $headerParams -Body $bodyParams -ErrorVariable getAccountResultErr

    return $addsafeResult

  }

  catch {

    Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__

    Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription

    Write-Host "Response:" $_.Exception.Message

    return $false

  }

}

 

# Global Declaration

$PVWA_URL = "https://10.116.81.38"

$Keywords = "TestAccount"

$Safe = "TestSafe"

 

# Execute Logon

$sessionID = PASREST-Logon

 

# Error Handling for Logon

if ($sessionID -eq $false) {Write-Host "[ERROR] There was an error logging into the Vault." -ForegroundColor Red; break}

else {Write-Host "[INFO] Logon completed successfully." -ForegroundColor DarkYellow}

 

# Execute Addsafe

$addsafeResult = PASREST-Addsafe -Authorization $sessionID

if ($addsafeResult -eq $false) {Write-Host "[ERROR] There was an error adding the safe to the Vault."-ForegroundColor Red}

else {$addsafeResult.safes | Format-Table -Property SafeName}

 

# Execute Logoff

$logoffResult = PASREST-Logoff -Authorization $sessionID

if ($logoffResult -eq $true) {Write-Host "[INFO] Logoff completed successfully." -ForegroundColor DarkYellow}

else {Write-Host "[ERROR] Logoff was not completed successfully. Please logout manually using Authorization token:" $sessionID -ForegroundColor Red}

 

L7 Applicator

The script itself is fine. I ran it on my lab environment (only changed the PVWA URL) and it created the Safe successfully.

 

You can run "certmgr.msc" on your Windows client to add the CA that created the PVWA SSL cert, as a trusted CA.

L4 Transporter
Thanks for checking again and based on your inputs I have executed this script from PVWA server only (as my assumption this fix the certificate issue) But still result is same. Let me know if there could something else which could impact the execution here. [cid:[email protected]] Vidit Goswami CYBERSECURITY & RISK SERVICES Wipro Limited Plot No.31 MIDC, Pune Infotech Park, Ph-2 Hinjewadi, Pune - 411 057 | India Sensitivity: Internal & Restricted

L0 Member

Try this in your code

try { Write-Host "Adding TrustAllCertsPolicy type." -ForegroundColor Yellow Add-Type -TypeDefinition @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } "@ Write-Host "TrustAllCertsPolicy type added." -ForegroundColor Yellow } catch { Write-Host $_ -ForegroundColor "Yellow" } [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

 

L0 Member

By default PowerShell uses TLS 1.0 for cmdlets like Invoke-RestMethod and Invoke-WebRequest but your CyberArk environment is most likely configured to require TLS 1.2, which is why you are getting the error you referenced. You can force PowerShell to use TLS 1.2 instead by putting this at the beginning of your script:

 

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

L4 Transporter
Thanks Mason, We are through with safe creation in vault , have a great day ahead 😊 [cid:[email protected]] Vidit Goswami CYBERSECURITY & RISK SERVICES Wipro Limited Plot No.31 MIDC, Pune Infotech Park, Ph-2 Hinjewadi, Pune - 411 057 | India Sensitivity: Internal & Restricted

Hi Vidit,

 

can you please advise what was the issue with the script execution as even i am facing this error while trying to execute from my system

  • 1478 Views
  • 8 replies
  • 0 Likes
Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

Click Accept as Solution to acknowledge that the answer to your question has been provided.

The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!

These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!

The LIVEcommunity thanks you for your participation!