Private/Import-PAConfig.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function Import-PAConfig {
    [CmdletBinding()]
    param(
        [Parameter(Position=0)]
        [ValidateSet('Server','Account','Order')]
        [string]$Level
    )

    # The config structure looks like this:
    # %LOCALAPPDATA%\Posh-ACME
    # - current-server.txt
    # %LOCALAPPDATA%\Posh-ACME\(server)
    # - dir.json
    # - current-account.txt
    # %LOCALAPPDATA%\Posh-ACME\(server)\(account)
    # - acct.json
    # - current-order.txt
    # %LOCALAPPDATA%\Posh-ACME\(server)\(account)\(order)
    # - order.json
    # - cert.cer/key/pfx/etc

    # Each level of the config is dependent on its parent. So if the user changes the server,
    # they need to reload the server and all of the child accounts and orders. But if they only
    # change the account, they only need to reload it and orders. And so on.

    # make sure we have the root config folder
    if ([string]::IsNullOrWhiteSpace((Get-ConfigRoot))) {
        if ($IsWindows -or $PSVersionTable.PSEdition -eq 'Desktop') {
            Set-ConfigRoot (Join-Path $env:LOCALAPPDATA 'Posh-ACME')
        } elseif ($IsLinux) {
            Set-ConfigRoot (Join-Path $env:HOME '.config/Posh-ACME')
        } elseif ($IsMacOs) {
            Set-ConfigRoot (Join-Path $env:HOME 'Library/Preferences/Posh-ACME')
        } else {
            throw "Unrecognized PowerShell platform"
        }

        # allow overriding the default config location with a custom path
        # based on an the POSHACME_HOME environment variable
        if (-not [string]::IsNullOrWhiteSpace($env:POSHACME_HOME)) {
            if (Test-Path $env:POSHACME_HOME -PathType Container) {
                Set-ConfigRoot $env:POSHACME_HOME
            } else {
                Write-Warning "The POSHACME_HOME environment variable exists but the path it points to, $($env:POSHACME_HOME), does not. Using default config location."
            }
        }

        # create the config folder if it doesn't already exist.
        if (-not (Test-Path (Get-ConfigRoot) -PathType Container)) {
            New-Item -ItemType Directory -Path (Get-ConfigRoot) -Force -EA Stop | Out-Null
        }
    }

    # start at the server level if nothing was specified or specifically requested
    if (!$Level -or $Level -eq 'Server') {

        # load the current ACME directory into memory if it exists on disk
        $dirUrl = [string](Get-Content (Join-Path (Get-ConfigRoot) 'current-server.txt') -EA Ignore)
        if (![string]::IsNullOrWhiteSpace($dirUrl)) {

            Set-DirFolder (ConvertTo-DirFolder $dirUrl)
            $script:Dir = Get-PAServer $dirUrl

            # deal with cert validation options between PS editions
            if ($script:Dir.SkipCertificateCheck) {
                Write-Debug "skipping cert validation"
                if ($script:SkipCertSupported) {
                    $script:UseBasic.SkipCertificateCheck = $true
                } else {
                    [CertValidation]::Ignore()
                }
            } else {
                Write-Debug "restoring cert validation"
                if ($script:SkipCertSupported) {
                    $script:UseBasic.SkipCertificateCheck = $false
                } else {
                    [CertValidation]::Restore()
                }
            }

            $ImportAccount = $true

        } else {
            # wipe references since we have no current server
            $script:DirFolder = $null
            $script:Dir = $null
            $script:AcctFolder = $null
            $script:Acct = $null
            $script:OrderFolder = $null
            $script:Order = $null
        }
    }

    if ($ImportAccount -or $Level -eq 'Account') {

        # load the current account into memory if it exists on disk
        $acctID = [string](Get-Content (Join-Path (Get-DirFolder) 'current-account.txt') -EA Ignore)
        if (![string]::IsNullOrWhiteSpace($acctID)) {

            $script:AcctFolder = Join-Path (Get-DirFolder) $acctID
            $script:Acct = Get-PAAccount $acctID

            $ImportOrder = $true

        } else {
            # wipe references since we have no current account
            $script:AcctFolder = $null
            $script:Acct = $null
            $script:OrderFolder = $null
            $script:Order = $null
        }
    }

    if ($ImportOrder -or $Level -eq 'Order') {

        # load the current order into memory if it exists on disk
        $domain = [string](Get-Content (Join-Path $script:AcctFolder 'current-order.txt') -EA Ignore)
        if (![string]::IsNullOrWhiteSpace($domain)) {

            $script:OrderFolder = Join-Path $script:AcctFolder $domain.Replace('*','!')
            $script:Order = Get-PAOrder $domain

        } else {
            # wipe references since we have no current order
            $script:OrderFolder = $null
            $script:Order = $null
        }
    }

}