Framework/Models/SubscriptionCore/ManagementCertificate.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
Set-StrictMode -Version Latest 

class ManagementCertificate
{
    [string] $CertThumbprint
    [string] $SubjectName
    [string] $Issuer
    [PSObject] $Created
    [PSObject] $ExpiryDate
    [string] $IsExpired
    [PSObject] $Difference    
    [bool] $Whitelisted

    hidden static [ManagementCertificate[]] ListManagementCertificates([PSObject] $certObjects)
    {
        [ManagementCertificate[]] $certs = @()
        $certObjects | ForEach-Object{               
                            [ManagementCertificate] $certObject = [ManagementCertificate]::new();
                            $b64cert = $_.SubscriptionCertificateData                               
                            $certData = [System.Convert]::FromBase64String($b64Cert)                                                                      
                            $certX = [System.Security.Cryptography.X509Certificates.X509Certificate2]($certData)   
                            $certObject.ExpiryDate = $certX.NotAfter.ToString("yyyy-MM-dd HH:mm:ss")
                            $certObject.CertThumbprint = $_.SubscriptionCertificateThumbprint
                            $certObject.SubjectName = $certX.Subject
                            $certObject.Issuer = $certX.Issuer
                            $certObject.Created = $_.Created
                            $certObject.IsExpired = "False"
                            $certObject.Difference = New-TimeSpan -Start ([datetime]$certX.NotBefore) -End ([datetime]$certX.NotAfter)
                            if([System.DateTime]::UtcNow -ge $certX.NotAfter)
                            {
                                $certObject.IsExpired = "True"
                            }
                            #Has to be moved to new configuration model
                            $certObject.Whitelisted = $false                            
                            $certs += $certObject
                        }
        return $certs;
    }
}