Framework/Models/AzSkSettings.ps1
# # AzSkSettings.ps1 # Set-StrictMode -Version Latest class AzSkSettings { [string] $OMSWorkspaceId; [string] $OMSSharedKey; [string] $AltOMSWorkspaceId; [string] $AltOMSSharedKey; [string] $OMSType; [string] $OMSSource; [string] $EventHubNamespace; [string] $EventHubName; [string] $EventHubSendKeyName; [string] $EventHubSendKey; [string] $EventHubType; [string] $EventHubSource; [string] $WebhookUrl; [string] $WebhookAuthZHeaderName; [string] $WebhookAuthZHeaderValue; [string] $WebhookType; [string] $WebhookSource; [string] $AutoUpdateCommand; [AutoUpdate] $AutoUpdateSwitch = [AutoUpdate]::NotSet; [string] $OutputFolderPath; [bool] $EnableAADAuthForOnlinePolicyStore; [bool] $UseOnlinePolicyStore; [string] $OnlinePolicyStoreUrl; [string] $UsageTelemetryLevel; [string] $LocalControlTelemetryKey; [bool] $LocalEnableControlTelemetry; [bool] $PrivacyNoticeAccepted = $false; hidden static [AzSkSettings] $Instance = $null; hidden static [string] $FileName = "AzSkSettings.json"; static [AzSkSettings] GetInstance() { if (-not [AzSkSettings]::Instance) { [AzSkSettings]::LoadAzSkSettings($false); } return [AzSkSettings]::Instance } static [AzSkSettings] GetLocalInstance() { return [AzSkSettings]::LoadAzSkSettings($true); } hidden static [AzSkSettings] LoadAzSkSettings([bool] $loadUserCopy) { #Filename will be static. #For AzSK Settings, never use online policy store. Its assumed that file will be available offline $localAppDataSettings = [ConfigurationHelper]::LoadOfflineConfigFile([AzSkSettings]::FileName) [AzSkSettings] $parsedSettings = $null; [AzSkSettings] $localModuleSettings = $null; [AzSkSettings] $serverSettings = $null; $migratedPropNames = @(); #Validate settings content is not null if ($localAppDataSettings) { try { #Step1: Try parsing the object from local app data settings. If parse is successful then there is no change to settings schema. $parsedSettings = [AzSkSettings] $localAppDataSettings } catch { #Step2: Any error occurred while converting local json file indicates change in schema #Load latest AzSKSettings from modules folder $parsedSettings = [ConfigurationHelper]::LoadModuleJsonFile([AzSkSettings]::FileName) $parsedSettings | Get-Member -MemberType Properties | ForEach-Object { $propertyName = $_.Name; if([Helpers]::CheckMember($localAppDataSettings, $propertyName)) { $parsedSettings.$propertyName = $localAppDataSettings.$propertyName; $migratedPropNames += $propertyName; } }; if($migratedPropNames.Count -ne 0) { [AzSkSettings]::Update($parsedSettings); [EventBase]::PublishGenericCustomMessage("Local AzSK settings file was not compatible with the latest version. `r`nMigrated the existing values for properties: [$([string]::Join(", ", $migratedPropNames))] ", [MessageType]::Warning); } } #Step 3: Get the latest server settings and merge with that if(-not $loadUserCopy) { [bool] $_useOnlinePolicyStore = $parsedSettings.UseOnlinePolicyStore; [string] $_onlineStoreUri = $parsedSettings.OnlinePolicyStoreUrl; [bool] $_enableAADAuthForOnlinePolicyStore = $parsedSettings.EnableAADAuthForOnlinePolicyStore; $serverSettings = [ConfigurationHelper]::LoadServerConfigFile([AzSkSettings]::FileName, $_useOnlinePolicyStore, $_onlineStoreUri, $_enableAADAuthForOnlinePolicyStore); $mergedServerPropNames = @(); $serverSettings | Get-Member -MemberType Properties | ForEach-Object { $propertyName = $_.Name; if([string]::IsNullOrWhiteSpace($parsedSettings.$propertyName) -and -not [string]::IsNullOrWhiteSpace($serverSettings.$propertyName)) { $parsedSettings.$propertyName = $serverSettings.$propertyName; $mergedServerPropNames += $propertyName; } }; if($mergedServerPropNames.Count -ne 0) { #TODO need to check for the right message here #[EventBase]::PublishGenericCustomMessage("Merged the existing values for properties from server settings file: [$([string]::Join(", ", $mergedServerPropNames))] ", [MessageType]::Warning); } [AzSkSettings]::Instance = $parsedSettings; } #Sever merged settings should not be persisted, as it should always take latest from the server return $parsedSettings; } else { return $null; } } [void] Update() { if (-not (Test-Path $([Constants]::AzSkAppFolderPath))) { mkdir -Path $([Constants]::AzSkAppFolderPath) -ErrorAction Stop | Out-Null } #persisting back to file [AzSkSettings]::Instance | ConvertTo-Json | Out-File -Force -FilePath ([Constants]::AzSkAppFolderPath + "\" + [AzSkSettings]::FileName) } static [void] Update([AzSkSettings] $localSettings) { if (-not (Test-Path $([Constants]::AzSkAppFolderPath))) { mkdir -Path $([Constants]::AzSkAppFolderPath) -ErrorAction Stop | Out-Null } #persisting back to file $localSettings | ConvertTo-Json | Out-File -Force -FilePath ([Constants]::AzSkAppFolderPath + "\" + [AzSkSettings]::FileName) } #TODO: Replace OMSSource with framework level source hidden [string] GetScanSource() { return $this.OMSSource } } |