Framework/Models/AzSdkConfig.ps1

Set-StrictMode -Version Latest
class AzSdkConfig
{
    [string] $MaintenanceMessage
    [Environment] $Environment
    [string] $AzSDKRGName
    [string] $AzSDKRepoURL
    [string] $AzSDKServerVersion
    [string[]] $SubscriptionMandatoryTags = @()
    [string] $ERvNetResourceGroupNames
    [string] $UpdateCompatibleCCVersion
    [string] $AzSDKApiBaseURL;
    [bool] $PublishVulnDataToApi;
    [string] $ControlTelemetryKey;
    [bool] $EnableControlTelemetry;
    [string] $PolicyMessage;
    [string] $InstallationCommand;
    [string] $PublicPSGalleryUrl;
    [string] $AzSDKCARunbookVersion;
    [string] $AzSDKCAMinReqdRunbookVersion;
    [string] $AzSDKAlertsMinReqdVersion;
    [string] $AzSDKARMPolMinReqdVersion;
    [string[]] $PrivacyAcceptedSources = @();
    [string] $OutputFolderPath;
    [int] $BackwardCompatibleVersionCount;
    [string[]] $DefaultControlExculdeTags = @()
    [string[]] $DefaultControlFiltersTags = @()
    [System.Version[]] $AzSDKVersionList = @()
    [int] $CAScanIntervalInHours;
    [string] $ConfigSchemaBaseVersion;
    [string] $AzSDKASCMinReqdVersion;
    #keeping the selfsigned cert check property bool as it would break the current configurations
    [bool] $AllowSelfSignedWebhookCertificate;
    [bool] $EnableDevOpsKitSetupCheck;
    [bool] $UpdateToLatestVersion;
    [string] $CASetupRunbookURL;
    [string] $AzSKConfigURL;
        [bool] $IsAlertMonitoringEnabled;
    [string] $MigrationWarning;
    [bool] $BlockCommandInHalfMigration;
    hidden static [AzSdkConfig] $Instance = $null;

    static [AzSdkConfig] GetInstance([bool] $useOnlinePolicyStore, [string] $onlineStoreUri, [bool] $enableAADAuthForOnlinePolicyStore)
    {
        if ( $null -eq  [AzSdkConfig]::Instance)
        {
            [AzSdkConfig]::Instance = [AzSdkConfig]::LoadRootConfiguration($useOnlinePolicyStore,$onlineStoreUri,$enableAADAuthForOnlinePolicyStore)
        }

        return [AzSdkConfig]::Instance
    }

    hidden static [AzSdkConfig] LoadRootConfiguration([bool] $useOnlinePolicyStore, [string] $onlineStoreUri, [bool] $enableAADAuthForOnlinePolicyStore)
    {
        #Filename will be static.
        return [AzSdkConfig] ([ConfigurationHelper]::LoadServerConfigFile("AzSdk.json", $useOnlinePolicyStore, $onlineStoreUri, $enableAADAuthForOnlinePolicyStore));
    }

    hidden  [string] GetLatestAzSDKVersion([string] $moduleName)
    {
        if([string]::IsNullOrWhiteSpace($this.AzSDKServerVersion))
        {
            $this.AzSDKServerVersion = "0.0.0.0";
            try
            {
                if((-not [string]::IsNullOrWhiteSpace($this.AzSKConfigURL)) -and (-not $this.UpdateToLatestVersion))
                {
                    try
                    {
                        $serverFileContent = [ConfigurationHelper]::InvokeControlsAPI($this.AzSKConfigURL, '', '', '');
                        if($null -ne $serverFileContent)
                        {
                            if(-not [string]::IsNullOrWhiteSpace($serverFileContent.CurrentVersionForOrg))
                            {
                                $this.AzSDKServerVersion = $serverFileContent.CurrentVersionForOrg
                            }
                        }
                    }
                    catch
                    {
                        #left blank intentionally
                    }
                }
                
                if($this.AzSDKServerVersion -eq '0.0.0.0')
                {
                    $repoUrl = $this.AzSDKRepoURL;
                    #Searching for the module in the repo
                    $Url = "$repoUrl/api/v2/Search()?`$filter=IsLatestVersion&searchTerm=%27$moduleName%27&includePrerelease=false" 
                    [System.Uri] $validatedUri = $null;
                    if([System.Uri]::TryCreate($Url, [System.UriKind]::Absolute, [ref] $validatedUri))
                    {
                        $SearchResult = @()
                        $SearchResult += Invoke-RestMethod -Method Get -Uri $validatedUri -UseBasicParsing
                        if($SearchResult.Length -and $SearchResult.Length -gt 0) 
                        {
                            #filter latest module
                            $SearchResult = $SearchResult | Where-Object -FilterScript {
                                return $_.title.'#text' -eq $moduleName
                            } 
                            $moduleName = $SearchResult.title.'#text' # get correct casing for the module name
                            $PackageDetails = Invoke-RestMethod -Method Get -UseBasicParsing -Uri $SearchResult.id 
                            $this.AzSDKServerVersion = $PackageDetails.entry.properties.version
                        }
                    }
                }
            }
            catch
            {
                $this.AzSDKServerVersion = "0.0.0.0";
            }
        }
        return $this.AzSDKServerVersion;
    }

    #Function to get list of AzSDK version using API
    hidden [System.Version[]] GetAzSDKVersionList([string] $moduleName)
    {
        if(($this.AzSDKVersionList | Measure-Object).Count -eq 0)
        {
            try
            {
                $repoUrl = $this.AzSDKRepoURL;
                #Searching for the module in the repo
                $Url = "$repoUrl/api/v2/FindPackagesById()?id='$moduleName'&`$skip=0&`$top=40&`$orderby=Version desc" 
                [System.Uri] $validatedUri = $null;
                if([System.Uri]::TryCreate($Url, [System.UriKind]::Absolute, [ref] $validatedUri))
                {                    
                    $searchResult = Invoke-RestMethod -Method Get -Uri $validatedUri -UseBasicParsing
                    $versionList =@()
                    if($searchResult.Length -and $searchResult.Length -gt 0) 
                    {
                        $versionList += $SearchResult | Where-Object {$_.title.'#text' -eq $ModuleName
                        } | % {[System.Version] $_.properties.version }                                            
                        $this.AzSDKVersionList = $versionList
                    }
                }
            }
            catch
            {
                $this.AzSDKVersionList = @();
            }
        }
        return $this.AzSDKVersionList;
    }
}