public/Add-VPASApplicationAuthentication.ps1
<#
.Synopsis ADD APPLICATION ID AUTHENTICATION METHOD CREATED BY: Vadim Melamed, EMAIL: vpasmodule@gmail.com .DESCRIPTION USE THIS FUNCTION TO ADD AN AUTHENTICATION METHOD TO AN EXISTING APPLICATION ID .LINK https://vpasmodule.com/commands/Add-VPASApplicationAuthentication .PARAMETER token HashTable of data containing various pieces of login information (PVWA, LoginToken, HeaderType, etc). If -token is not passed, function will use last known hashtable generated by New-VPASToken .PARAMETER AppID Unique ApplicationID (or Application Name) that will be used by the credential provider(s) to retrieve credentials .PARAMETER AuthType Define the type of the target authentication Possible values: Path, Hash, OSUser, machineAddress, certificateSerialNumber .PARAMETER AuthValue Value to be added to the target AppID .PARAMETER IsFolder Define if the AuthValue is a folder if using an authentication type: path .PARAMETER AllowInternalScripts Define if internal scripts have permission to pull credentials if using an authentication type: path .PARAMETER InputParameters HashTable of values containing the parameters required to make the API call .EXAMPLE $AddApplicationAuthenticationStatus = Add-VPASApplicationAuthentication -AppID {APPID VALUE} -AuthType {AUTHTYPE VALUE} -AuthValue {AUTHVALUE VALUE} .EXAMPLE $InputParameters = @{ AppID = "TestApplication01" AuthType = "Path"|"Hash"|"OSUser"|"machineAddress"|"certificateSerialNumber" AuthValue = "TargetValueHere" IsFolder = $true|$false AllowInternalScripts = $true|$false } $AddApplicationAuthenticationStatus = Add-VPASApplicationAuthentication -InputParameters $InputParameters .OUTPUTS $true if successful --- $false if failed #> function Add-VPASApplicationAuthentication{ [OutputType([bool])] [CmdletBinding(DefaultParameterSetName='Set1')] Param( [Parameter(Mandatory=$true,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true,HelpMessage="Enter target AppID to add Authentication Method to (for example: TestAppID1)")] [String]$AppID, [Parameter(Mandatory=$true,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true,HelpMessage="Enter Authentication Method type (Path, Hash, OSUser, Address, Certificate)")] [ValidateSet('Path','Hash','OSUser','machineAddress','certificateSerialNumber')] [String]$AuthType, [Parameter(Mandatory=$true,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true,HelpMessage="Enter Authentication Method value (for example: 1.1.1.1")] [String]$AuthValue, [Parameter(Mandatory=$false,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true)] [Switch]$IsFolder, [Parameter(Mandatory=$false,ParameterSetName='Set1',ValueFromPipelineByPropertyName=$true)] [Switch]$AllowInternalScripts, [Parameter(Mandatory=$true,ParameterSetName='InputParameters',ValueFromPipelineByPropertyName=$true,HelpMessage="Hashtable of parameters required to make API call, refer to get-help -examples for valid inputs")] [hashtable]$InputParameters, [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)] [hashtable]$token ) Begin{ $tokenval,$sessionval,$PVWA,$Header,$ISPSS,$IdentityURL,$EnableTextRecorder,$AuditTimeStamp,$NoSSL,$VaultVersion,$HideWarnings,$AuthenticatedAs,$SubDomain,$EnableTroubleshooting = Get-VPASSession -token $token $CommandName = $MyInvocation.MyCommand.Name $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType COMMAND } process{ try{ if($PSCmdlet.ParameterSetName -eq "InputParameters"){ $KeyHash = @{ set1 = @{ AcceptableKeys = @("AppID","AuthType","AuthValue","IsFolder","AllowInternalScripts") MandatoryKeys = @("AppID","AuthType","AuthValue") } } $CheckSet = Test-VPASHashtableKeysHelper -InputHash $InputParameters -KeyHash $KeyHash if(!$CheckSet){ $log = Write-VPASTextRecorder -inputval "FAILED TO FIND TARGET PARAMETER SET" -token $token -LogType MISC Write-Verbose "FAILED TO FIND TARGET PARAMETER SET" Write-VPASOutput -str "FAILED TO FIND TARGET PARAMETER SET...VIEW EXAMPLES BELOW:" -type E $examples = Write-VPASExampleHelper -CommandName $CommandName return $false } else{ foreach($key in $InputParameters.Keys){ Set-Variable -Name $key -Value $InputParameters.$key } } } }catch{ $log = Write-VPASTextRecorder -inputval $_ -token $token -LogType ERROR $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: FALSE" -token $token -LogType MISC Write-Verbose "FAILED TO ADD APPLICATION AUTHENTICATION METHOD" Write-VPASOutput -str $_ -type E return $false } $AuthTrigger = 0 Write-Verbose "SETTING APPLICATION AUTHENTICATION TYPE" $authtypelower = $AuthType.ToLower() if($authtypelower -eq "path"){ $AuthTrigger = 1 Write-Verbose "APPLICATION AUTHENTICATION OF TYPE PATH WAS SELECTED" } elseif($authtypelower -eq "hash"){ $AuthTrigger = 2 Write-Verbose "APPLICATION AUTHENTICATION OF TYPE HASH WAS SELECTED" } elseif($authtypelower -eq "osuser"){ $AuthTrigger = 3 Write-Verbose "APPLICATION AUTHENTICATION OF TYPE OSUSER WAS SELECTED" } elseif($authtypelower -eq "machineaddress"){ $AuthTrigger = 4 Write-Verbose "APPLICATION AUTHENTICATION OF TYPE MACHINE ADDRESS WAS SELECTED" } elseif($authtypelower -eq "certificateserialnumber"){ $AuthTrigger = 5 Write-Verbose "APPLICATION AUTHENTICATION OF TYPE CERTIFICATE SERIAL NUMBER WAS SELECTED" } if($AuthTrigger -eq 1){ if(!$IsFolder){ Write-VPASOutput -str "ISFOLDER NOT SPECIFIED, SETTING DEFAULT VALUE: FALSE" -type M Write-Verbose "ISFOLDER NOT SPECIFIED, SETTING DEFAULT VALUE: FALSE" $isfolderflag = $false } elseif($IsFolder){ Write-Verbose "ISFOLDER SPECIFIED, SETTING VALUE: TRUE" $isfolderflag = $true } if(!$AllowInternalScripts){ Write-Verbose "ALLOWINTERNALSCRIPTS NOT SPECIFIED, SETTING DEFAULT VALUE: FALSE" Write-VPASOutput -str "ALLOWINTERNALSCRIPTS NOT SPECIFIED, SETTING DEFAULT VALUE: FALSE" -type M $allowinternalscriptsflag = $false } elseif($AllowInternalScripts){ Write-Verbose "ALLOWINTERNALSCRIPTS SPECIFIED, SETTING VALUE: TRUE" $allowinternalscriptsflag = $true } Write-Verbose "SETTING PARAMETERS FOR API CALL" $params = @{ authentication = @{ AuthType = "path"; AuthValue = $AuthValue; IsFolder = $isfolderflag; AllowInternalScripts = $allowinternalscriptsflag; } } $log = Write-VPASTextRecorder -inputval $params -token $token -LogType PARAMS $params = $params | ConvertTo-Json } if($AuthTrigger -eq 2){ Write-Verbose "SETTING PARAMETERS FOR API CALL" $params = @{ authentication = @{ AuthType = "hash"; AuthValue = $AuthValue; Comment = $comment } } $log = Write-VPASTextRecorder -inputval $params -token $token -LogType PARAMS $params = $params | ConvertTo-Json } if($AuthTrigger -eq 3){ Write-Verbose "SETTING PARAMETERS FOR API CALL" $params = @{ authentication = @{ AuthType = "osuser"; AuthValue = $AuthValue; } } $log = Write-VPASTextRecorder -inputval $params -token $token -LogType PARAMS $params = $params | ConvertTo-Json } if($AuthTrigger -eq 4){ Write-Verbose "SETTING PARAMETERS FOR API CALL" $params = @{ authentication = @{ AuthType = "machineAddress"; AuthValue = $AuthValue; } } $log = Write-VPASTextRecorder -inputval $params -token $token -LogType PARAMS $params = $params | ConvertTo-Json } if($AuthTrigger -eq 5){ Write-Verbose "SETTING PARAMETERS FOR API CALL" $params = @{ authentication = @{ AuthType = "certificateserialnumber"; AuthValue = $AuthValue; Comment = $comment; } } $log = Write-VPASTextRecorder -inputval $params -token $token -LogType PARAMS $params = $params | ConvertTo-Json } try{ Write-Verbose "MAKING API CALL TO CYBERARK" if($NoSSL){ Write-Verbose "NO SSL ENABLED, USING HTTP INSTEAD OF HTTPS" $uri = "http://$PVWA/PasswordVault/WebServices/PIMServices.svc/Applications/$AppID/Authentications/" } else{ Write-Verbose "SSL ENABLED BY DEFAULT, USING HTTPS" $uri = "https://$PVWA/PasswordVault/WebServices/PIMServices.svc/Applications/$AppID/Authentications/" } $log = Write-VPASTextRecorder -inputval $uri -token $token -LogType URI $log = Write-VPASTextRecorder -inputval "POST" -token $token -LogType METHOD if($sessionval){ $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Method POST -Body $params -ContentType "application/json" -WebSession $sessionval } else{ $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Method POST -Body $params -ContentType "application/json" } Write-Verbose "PARSING DATA FROM CYBERARK" Write-Verbose "RETURNING TRUE" $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: TRUE" -token $token -LogType MISC return $true }catch{ $log = Write-VPASTextRecorder -inputval $_ -token $token -LogType ERROR $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: FALSE" -token $token -LogType MISC Write-Verbose "UNABLE TO ADD APPLICATION AUTHENTICATION METHOD" Write-VPASOutput -str $_ -type E return $false } } End{ $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType DIVIDER } } |