Public/Certificates/Remove-JIMCertificate.ps1

# Copyright (c) Tetron Limited. All rights reserved.
# Licensed under the Tetron Commercial License. See LICENSE file in the project root.

function Remove-JIMCertificate {
    <#
    .SYNOPSIS
        Removes a certificate from JIM's trusted certificate store.
 
    .DESCRIPTION
        Permanently deletes a certificate from the trusted certificate store.
 
        Deleting the only certificate sign-in depends on stops everyone signing in to JIM; the cmdlet warns before
        asking.
 
    .PARAMETER Id
        The unique identifier (GUID) of the certificate to delete.
 
    .PARAMETER InputObject
        The certificate to remove, as Get-JIMCertificate returns it.
 
    .PARAMETER ChangeReason
        Optional reason for the change, recorded on the audit Activity and shown in the certificate's
        Configuration Change History.
 
    .PARAMETER Force
        Suppresses the confirmation prompt.
 
    .PARAMETER PassThru
        Returns the certificate as it was before it was removed.
 
    .OUTPUTS
        With -PassThru, the certificate as it was before it was removed; otherwise nothing.
 
    .EXAMPLE
        Remove-JIMCertificate -Id $certId
 
        Removes the certificate (prompts for confirmation).
 
    .EXAMPLE
        Remove-JIMCertificate -Id $certId -Force
 
        Removes the certificate without confirmation.
 
    .EXAMPLE
        Get-JIMCertificate | Where-Object { $_.name -like "Test*" } | Remove-JIMCertificate -Force
 
        Removes all certificates with names starting with "Test".
 
    .LINK
        Get-JIMCertificate
        Add-JIMCertificate
        Set-JIMCertificate
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High', DefaultParameterSetName = 'ById')]
    [OutputType('JIM.TrustedCertificateDetailDto')]
    param(
        [Parameter(Mandatory, ParameterSetName = 'ById', ValueFromPipelineByPropertyName)]
        [Guid]$Id,

        [Parameter(Mandatory, ParameterSetName = 'ByInputObject', ValueFromPipeline)]
        [PSCustomObject]$InputObject,

        [ValidateNotNullOrEmpty()]
        [string]$ChangeReason,

        [switch]$Force,

        [switch]$PassThru
    )

    process {
        # Check connection first
        if (-not $script:JIMConnection) {
            Write-Error "You are not connected to JIM. Run Connect-JIM -Url <your JIM URL> to authenticate, then try again."
            return
        }

        $certId = if ($InputObject) { $InputObject.id } else { $Id }

        # Get the certificate first for confirmation message and PassThru
        $existing = $null
        try {
            $existing = Invoke-JIMApi -Endpoint "/api/v1/certificates/$certId"
        }
        catch {
            Write-Error "Certificate not found: $certId"
            return
        }

        if ($existing.signInDependsOnIt) {
            Write-Warning "Sign-in depends on '$($existing.name)'. Deleting it stops everyone signing in to JIM, including you. Until another certificate is trusted for sign-in, only the infrastructure API key can put it right."
        }

        if (Confirm-JIMAction -Cmdlet $PSCmdlet -Target $existing.name -Action "Delete Certificate" -Force:$Force) {
            Write-Verbose "Deleting certificate: $certId"

            try {
                $endpoint = "/api/v1/certificates/$certId"
                if ($ChangeReason) {
                    $endpoint += "?changeReason=$([uri]::EscapeDataString($ChangeReason))"
                }
                Invoke-JIMApi -Endpoint $endpoint -Method 'DELETE'

                Write-Verbose "Deleted certificate: $certId"

                if ($PassThru) {
                    $existing
                }
            }
            catch {
                Write-Error "Failed to delete certificate: $_"
            }
        }
    }
}