Framework/Models/AzSkConfig.ps1

Set-StrictMode -Version Latest
class AzSkConfig
{
    [string] $MaintenanceMessage
    [Environment] $Environment
    [string] $AzSKRGName
    [string] $AzSKRepoURL
    [string] $AzSKServerVersion
    [string[]] $SubscriptionMandatoryTags = @()
    [string] $ERvNetResourceGroupNames
    [string] $UpdateCompatibleCCVersion
    [string] $AzSKApiBaseURL;
    [bool] $PublishVulnDataToApi;
    [string] $ControlTelemetryKey;
    [bool] $EnableControlTelemetry;
    [string] $PolicyMessage;
    [string] $InstallationCommand;
    [string] $PublicPSGalleryUrl;
    [string] $AzSKCARunbookVersion;
    [string] $AzSKCAMinReqdRunbookVersion;
    [string] $AzSKAlertsMinReqdVersion;
    [string] $AzSKARMPolMinReqdVersion;
    [string[]] $PrivacyAcceptedSources = @();
    [string] $OutputFolderPath;
    [int] $BackwardCompatibleVersionCount;
    [string[]] $DefaultControlExculdeTags = @()
    [string[]] $DefaultControlFiltersTags = @()
    [System.Version[]] $AzSKVersionList = @()
    [int] $CAScanIntervalInHours;
    [string] $ConfigSchemaBaseVersion;
    [string] $AzSKASCMinReqdVersion;
    [bool] $AllowSelfSignedWebhookCertificate;
    [bool] $EnableDevOpsKitSetupCheck;
    [string] $CASetupRunbookURL;
    hidden static [AzSkConfig] $Instance = $null;

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

        return [AzSkConfig]::Instance
    }

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

    hidden  [string] GetLatestAzSKVersion([string] $moduleName)
    {
        if([string]::IsNullOrWhiteSpace($this.AzSKServerVersion))
        {
            $this.AzSKServerVersion = "0.0.0.0";
            try
            {
                $repoUrl = $this.AzSKRepoURL;
                #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.AzSKServerVersion = $PackageDetails.entry.properties.version
                    }
                }
            }
            catch
            {
                $this.AzSKServerVersion = "0.0.0.0";
            }
        }
        return $this.AzSKServerVersion;
    }

    #Function to get list of AzSK version using API
    hidden [System.Version[]] GetAzSKVersionList([string] $moduleName)
    {
        if(($this.AzSKVersionList | Measure-Object).Count -eq 0)
        {
            try
            {
                $repoUrl = $this.AzSKRepoURL;
                #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.AzSKVersionList = $versionList
                    }
                }
            }
            catch
            {
                $this.AzSKVersionList = @();
            }
        }
        return $this.AzSKVersionList;
    }

}