Modules/PASeasyInstall.Vault/PASeasyInstall.Vault.psm1

# PASeasyInstall.Vault. Independent component API; NativeHTMLKit is never imported here.
Set-StrictMode -Version 2.0
$ErrorActionPreference='Stop'
$root=Split-Path -Parent $PSScriptRoot
$core=Join-Path $root 'PASeasyInstall.Core/PASeasyInstall.Core.psd1'
if(-not(Test-Path -LiteralPath $core -PathType Leaf)){throw 'PASeasyInstall.Core is required'}
Import-Module $core -ErrorAction Stop

function New-PASVaultConfiguration {
    [CmdletBinding()]param([Parameter(Mandatory)][string]$PrimaryIP,
        [Parameter(Mandatory)][string]$ControllerIP,[Parameter(Mandatory)][string]$MediaRoot,
        [string]$Environment='MyPAM',[string]$Username='Administrator',
        [string]$LicenseFile='',[string]$PublicRecoveryKey='',
        [string]$ServerZip='',[string]$PACLIZip='',
        [ValidateSet('Secure','Lab')][string]$SecurityProfile='Secure')
    if($PrimaryIP -eq $ControllerIP){throw 'Controller and Primary Vault must have different addresses'}
    $c=New-PASeasyConfig -Template Vault -Path 'template.json' -WhatIf -Force
    $c.name=$Environment.Trim()
    $c.mediaRoot=$MediaRoot.Trim()
    $c.controllerAddress=$ControllerIP.Trim()
    $c.security.profile=$SecurityProfile
    $c.hosts=@{vault01=@{address=$PrimaryIP.Trim();os='Windows';user=$Username.Trim();credentialRef='vault01'}}
    $c.vault.address=$PrimaryIP.Trim();$c.vault.existing=$false
    $c.vault.managementHost='vault01'
    $c.components=@(@{id='vault';type='Vault';host='vault01';variant='Standalone'})
    $c.activeDirectory.mode='None';$c.activeDirectory.join=@()
    $c.media=@{}
    if($LicenseFile){$c.media.License=$LicenseFile}
    if($PublicRecoveryKey){
        $keyPath=$PublicRecoveryKey
        if(Test-Path -LiteralPath $keyPath -PathType Container){$keyPath=Join-Path $keyPath 'recpub.key'}
        $c.media.PublicRecoveryKey=$keyPath
    }
    if($ServerZip){$c.media.ServerZip=$ServerZip}
    if($PACLIZip){$c.media.PACLIZip=$PACLIZip}
    $result=Test-PASeasyConfig -Configuration $c
    if(-not $result.Valid){throw ('Invalid Vault configuration: '+($result.Issues -join '; '))}
    return $result.Configuration
}
function Invoke-PASVault {
    [CmdletBinding(SupportsShouldProcess=$true)]param([string]$Config,$Configuration,
        [ValidateSet('All','LocalPreflight')][string]$Stage='All',
        [hashtable]$CredentialMap=@{},[hashtable]$SecretMap=@{},[switch]$PlanOnly)
    $validation=Test-PASeasyConfig -Config $Config -Configuration $Configuration -CheckMedia:($Stage -eq 'LocalPreflight')
    if(-not $validation.Valid){throw ($validation.Issues -join '; ')}
    $types=@($validation.Configuration.components|ForEach-Object {[string]$_.type})
    if($types.Count -ne 1 -or $types[0] -ne 'Vault'){throw 'Invoke-PASVault only accepts a Primary-only Vault configuration'}
    if($Stage -eq 'LocalPreflight'){return $validation}
    if($PlanOnly){return (Get-PASeasyPlan -Configuration $validation.Configuration)}
    if($PSCmdlet.ShouldProcess($validation.Configuration.name,'Deploy Primary Vault')){
        return (Start-PASeasyDeployment -Configuration $validation.Configuration -CredentialMap $CredentialMap -SecretMap $SecretMap -Confirm:$false)
    }
}
function Get-PASVaultStages {
    [CmdletBinding()]param()
    $names=@('LocalPreflight','preflight','management-endpoint','prepare-paths','upload-server','upload-license','prerequisites','extract','keygen','install-server','configure-primary','harden-primary','hardening-reboot','verify-primary')
    foreach($stage in $names){[pscustomobject]@{Component='Vault';Stage=$stage;ReadOnly=($stage -eq 'LocalPreflight');TargetExecutionQualified=$false}}
}

Export-ModuleMember -Function @('New-PASVaultConfiguration','Get-PASVaultStages','Invoke-PASVault')