Framework/Helpers/UserSubscriptionDataHelper.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
Set-StrictMode -Version Latest
class UserSubscriptionDataHelper: AzSKRoot
{
    hidden static [string] $ResourceGroupName = [ConfigurationManager]::GetAzSKConfigData().AzSKRGName
    hidden static [string] $AutomationAccountName = [Constants]::AutomationAccountName
    hidden static [string] $StorageResourceType = "Microsoft.Storage/storageAccounts";

    <#hidden [string] $AttestationContainerName
    hidden [string] $ScanProgressSnapshotsContainerName
    hidden [string] $CAMultiSubScanConfigContainerName
    hidden [string] $CAScanOutputLogsContainerName#>


    UserSubscriptionDataHelper([string] $subscriptionId):
        Base($subscriptionId)
    {
    }
    #region: Get operations
    static [PSObject] GetUserSubscriptionRGName()
    {
        return [UserSubscriptionDataHelper]::ResourceGroupName
    }
    static [PSObject] GetUserSubscriptionRG()
    {
        $ResourceGroup = Get-AzureRmResourceGroup -Name $([UserSubscriptionDataHelper]::ResourceGroupName) -ErrorAction Stop
        return $ResourceGroup
    }
    static [PSObject] GetUserSubscriptionStorage()
    {
        $StorageAccountPreName = [Constants]::StorageAccountPreName
        $storageAccount = Find-AzureRmResource -ResourceGroupNameEquals $([UserSubscriptionDataHelper]::ResourceGroupName) `
        -ResourceNameContains $StorageAccountPreName `
        -ResourceType $([UserSubscriptionDataHelper]::StorageResourceType) `
        -ErrorAction Stop
        $storageAccount = $storageAccount | Where-Object{$_.ResourceName -match '^azsk\d{14}$'}

        if(($storageAccount|Measure-Object).Count -gt 1)
        {
            throw [SuppressedException]::new("Multiple storage accounts found in resource group: [$([UserSubscriptionDataHelper]::ResourceGroupName)]. This is not expected. Please contact support team.");
        }
        return $storageAccount
    }
    [PSObject] GetUserSubscriptionStorageContainer([string] $StorageContainerType)
    {
        return "<containerobject>"
    }
    [string] GetUserSubscriptionStorageContainerName([string] $StorageContainerType)
    {
        return "<containername>"
    }    
    [PSObject] GetUserSubscriptionStorageContainerData([string] $StorageContainerType)
    {
        return "<containerdata>"
    }    
    static [PSObject] GetStorageContainer($ContainerName)
    {
        $storage = [UserSubscriptionDataHelper]::GetUserSubscriptionStorage()
        $container = $null
        if($storage)
        {
            $keys = Get-AzureRmStorageAccountKey -ResourceGroupName $([UserSubscriptionDataHelper]::ResourceGroupName) -Name $storage.Name
            $currentContext = New-AzureStorageContext -StorageAccountName $storage.Name -StorageAccountKey $keys[0].Value -Protocol Https
            $container = Get-AzureStorageContainer -Name $ContainerName -Context $currentContext -ErrorAction SilentlyContinue | Out-Null
        }
        return $container
    }
    static [string] GetCAName()
    {
        return [UserSubscriptionDataHelper]::AutomationAccountName
    }

    static [bool] IsMigrationRequired()
    {
        #TODO: Add more checks
        $isMigrationRequired = $true;
        try
        {
            $storage = [UserSubscriptionDataHelper]::GetOldStorage()
            $container = $null
            if($storage)
            {
                $temp = ($env:temp + "\AzSKTemp\");
                if(-not (Test-Path -Path $temp))
                {
                    mkdir -Path $temp -Force
                }
                $filePath = "$temp\MigrationOutputFromServer.json"
                Remove-Item -Path $filePath -Force -ErrorAction Ignore
                $keys = Get-AzureRmStorageAccountKey -ResourceGroupName $storage.ResourceGroupName -Name $storage.Name
                $currentContext = New-AzureStorageContext -StorageAccountName $storage.Name -StorageAccountKey $keys[0].Value -Protocol Https
                Get-AzureStorageBlobContent -Blob "MigrationOutput.json" -Container "migration" -Context $currentContext -Force -Destination $filepath -ErrorAction SilentlyContinue
                if((Test-Path $filePath))
                {
                    $MigrationOutput = Get-Content -Path $filePath | ConvertFrom-Json
                    if($MigrationOutput -and [Helpers]::CheckMember($MigrationOutput,"ErrorOccurred") -and $MigrationOutput.ErrorOccurred)
                    {
                            #TODO need to check the condition whether one failure should we consider as migration incomplete
                            $isMigrationRequired = $true;
                    }
                    else
                    {
                        #TODO need to check the condition whether one success should we consider as migration complete
                        $isMigrationRequired = $false;
                    }
                    
                }
                else
                {
                    #if the migration blob is not found then we need to migrate
                    $isMigrationRequired = $true;
                }
            }
            else
            {
                #if no storage found, there is no need to migrate as there are no old resources present on the subscription
                $isMigrationRequired = $false;                
            }
        }
        catch
        {
            $isMigrationRequired = $false
            throw;
            
        }
        return $isMigrationRequired
    }

    static [string] IsMigrationCompleted([string] $subscriptionId)
    {
        $MigrationCompleted = "NOTSET"
        $resources = Find-AzureRmResource -TagName $([Constants]::MigrationTagName) | Measure-Object
        if($resources.Count -gt 0)
        {
            $MigrationCompleted = "COMP"
        }
        else
        {
            $MigrationCompleted = "INIT"
        }
        return $MigrationCompleted;
    }

    static [PSObject] GetOldStorage()
    {
        $RGName = [OldConstants]::AzSDKRGName
        $StorageAccountPreName = [OldConstants]::StorageAccountPreName
        $existingStorage = Find-AzureRmResource -ResourceGroupNameEquals $RGName `
        -ResourceNameContains $StorageAccountPreName `
        -ResourceType $([UserSubscriptionDataHelper]::StorageResourceType) `
        -ErrorAction Stop
        if(($existingStorage|Measure-Object).Count -gt 1)
        {
            throw [SuppressedException]::new("Multiple storage accounts found in resource group: [$RGName]. This is not expected. Please contact support team.");
        }
        return $existingStorage
    }

    static [PSObject] GetOldRG()
    {
        $RGName = [OldConstants]::AzSDKRGName
        $ResourceGroup = Get-AzureRmResourceGroup -Name $RGName -ErrorAction SilentlyContinue
        return $ResourceGroup
    }

    <# TBR
     
    static [PSObject] GetLatestRG()
    {
        $RGName = [ConfigurationManager]::GetAzSKConfigData().AzSKRGName
        $ResourceGroup = Get-AzureRmResourceGroup -Name $RGName -ErrorAction Stop
        return $ResourceGroup
    }
 
     
    static [PSObject] GetLatestStorage()
    {
        $RGName = [ConfigurationManager]::GetAzSKConfigData().AzSKRGName
        $StorageAccountPreName = [Constants]::StorageAccountPreName
        $existingStorage = Find-AzureRmResource -ResourceGroupNameEquals $RGName `
        -ResourceNameContains $StorageAccountPreName `
        -ResourceType $([UserSubscriptionDataHelper]::StorageResourceType) `
        -ErrorAction Stop
 
        if(($existingStorage|Measure-Object).Count -gt 1)
        {
            throw [SuppressedException]::new("Multiple storage accounts found in resource group: [$RGName]. This is not expected. Please contact support team.");
        }
        return $existingStorage
    }
     
    <#[void] CreateStorageContainerIfNotExists([string] $StorageContainerType)
    {
        switch($StorageContainerType)
        {
            "AttestationDataContainer"
            {
                $this.StorageAccountInstance.CreateStorageContainerIfNotExists($this.AttestationContainerName);
            }
            "CAMultiSubScanConfigContainer"
            {
                $this.StorageAccountInstance.CreateStorageContainerIfNotExists($this.CAMultiSubScanConfigContainerName);
            }
            "ScanProgressSnapshotsContainer"
            {
                $this.StorageAccountInstance.CreateStorageContainerIfNotExists($this.ScanProgressSnapshotsContainerName);
            }
            "CAScanOutputLogsContainer"
            {
                $this.StorageAccountInstance.CreateStorageContainerIfNotExists($this.CAScanOutputLogsContainerName);
            }
        }
    } #>


    #endregion
}