AlertMonitoring/OMS.ps1

Set-StrictMode -Version Latest
function Set-AzSKOMSSettings
{
    <#
    .SYNOPSIS
    This command would help in updating the OMS configuration settings under the current powershell session.
    .DESCRIPTION
    This command will update the OMS Settings under the current powershell session. This also remembers the current settings and use them in the subsequent sessions.
     
    .PARAMETER OMSWorkspaceID
        Workspace ID of your OMS instance. AzSDK data would be pushed to this instance.
    .PARAMETER OMSSharedKey
        Shared key of your OMS instance.
    .PARAMETER Source
        Provide the source of OMS Events. (e. g. CC,CICD,SDL)
    .PARAMETER Disable
        Use -Disable option to clean the OMS setting under the current instance.
 
    .LINK
    https://aka.ms/azsdkossdocs
 
    #>

    param(
        
        [Parameter(Mandatory = $false, HelpMessage="Workspace ID of your OMS instance. AzSDK data would be pushed to this instance.", ParameterSetName = "Setup")]
        [AllowEmptyString()]
        [string]
        $OMSWorkspaceID,

        [Parameter(Mandatory = $false, HelpMessage="Shared key of your OMS instance.", ParameterSetName = "Setup")]
        [AllowEmptyString()]
        [string]
        $OMSSharedKey,

        [Parameter(Mandatory = $false, HelpMessage="Alternate Workspace ID of your OMS instance. AzSDK data would be pushed to this instance.", ParameterSetName = "Setup")]
        [AllowEmptyString()]
        [string]
        $AltOMSWorkspaceID,

        [Parameter(Mandatory = $false, HelpMessage="Shared key of your alternate OMS instance.", ParameterSetName = "Setup")]
        [AllowEmptyString()]
        [string]
        $AltOMSSharedKey,

        [Parameter(Mandatory = $false, HelpMessage="Provide the source of OMS Events.(e.g. CC,CICD,SDL)", ParameterSetName = "Setup")]
        [AllowEmptyString()]
        [string]
        $Source,

        [Parameter(Mandatory = $true, HelpMessage="Use -Disable option to clean the OMS setting under the current instance.", ParameterSetName = "Disable")]
        [switch]
        $Disable

    )
    Begin
    {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [ListenerHelper]::RegisterListeners();
    }
    Process
    {
        try
        {
            $appSettings = [ConfigurationManager]::GetLocalAzSdkSettings();
            if(-not $Disable) 
            {
                if(-not [string]::IsNullOrWhiteSpace($OMSWorkspaceID) -and -not [string]::IsNullOrWhiteSpace($OMSSharedKey))
                {
                    $appSettings.OMSWorkspaceId = $OMSWorkspaceID
                    $appSettings.OMSSharedKey = $OMSSharedKey
                }
                elseif(([string]::IsNullOrWhiteSpace($OMSWorkspaceID) -and -not [string]::IsNullOrWhiteSpace($OMSSharedKey)) `
                        -and (-not [string]::IsNullOrWhiteSpace($OMSWorkspaceID) -and [string]::IsNullOrWhiteSpace($OMSSharedKey)))
                {                    
                    [EventBase]::PublishGenericCustomMessage("You need to send both the OMSWorkspaceId and OMSSharedKey", [MessageType]::Error);
                    return;
                }
                if(-not [string]::IsNullOrWhiteSpace($AltOMSWorkspaceID) -and -not [string]::IsNullOrWhiteSpace($AltOMSSharedKey))
                {
                    $appSettings.AltOMSWorkspaceId = $AltOMSWorkspaceID
                    $appSettings.AltOMSSharedKey = $AltOMSSharedKey
                }
                elseif(([string]::IsNullOrWhiteSpace($AltOMSWorkspaceID) -and -not [string]::IsNullOrWhiteSpace($AltOMSSharedKey)) `
                        -and (-not [string]::IsNullOrWhiteSpace($AltOMSWorkspaceID) -and [string]::IsNullOrWhiteSpace($AltOMSSharedKey)))
                {                    
                    [EventBase]::PublishGenericCustomMessage("You need to send both the AltOMSWorkspaceId and AltOMSSharedKey", [MessageType]::Error);
                    return;
                }
            }
            else {
                $appSettings.OMSWorkspaceId = ""
                $appSettings.OMSSharedKey = ""
                $appSettings.AltOMSWorkspaceId = ""
                $appSettings.AltOMSSharedKey = ""
            }
            if(-not [string]::IsNullOrWhiteSpace($Source))
            {                
                $appSettings.OMSSource = $Source
            }
            else
            {
                $appSettings.OMSSource = "SDL"
            }
            $appSettings.OMSType = "AzSDK"
            [ConfigurationManager]::UpdateAzSdkSettings($appSettings);
            [EventBase]::PublishGenericCustomMessage([Constants]::SingleDashLine + "`r`nWe have added new queries for the OMS solution. These will help reflect the aggregate control pass/fail status more accurately. Please go here to get them: https://aka.ms/azsdk/omsqueries `r`n",[MessageType]::Warning);
            [EventBase]::PublishGenericCustomMessage("Successfully changed policy settings");
        }
        catch
        {
            [EventBase]::PublishGenericException($_);
        }
    }
    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}

function Install-AzSKOMSSolution
{
    <#
 
    .SYNOPSIS
    This command would help in creating security dashboard for applications
 
    .DESCRIPTION
    This command would help in creating security dashboard for applications
 
    .PARAMETER ApplicationRGMappingJSON
        Provide the application and resource group mapping config as JSON input
    .PARAMETER ApplicationRGMappingJSONConfigPath
        Provide the application and resource group mapping config as JSON config file
 
    .EXAMPLE
 
     
    .NOTES
    This command helps the application team to check compliance of Azure Subscriptions with the AzSDK security guidance.
 
    .LINK
    https://aka.ms/azsdkossdocs
 
    #>

    param(
        [Parameter(ParameterSetName="NewModel", Mandatory = $true)]
        [Parameter(ParameterSetName="OldModel", Mandatory = $true)]
        [string]
        [ValidateNotNullOrEmpty()]
        $OMSSubscriptionId,  
                
        [Parameter(ParameterSetName="NewModel", Mandatory = $true)]
        [Parameter(ParameterSetName="OldModel", Mandatory = $true)]
        [string]
        [ValidateNotNullOrEmpty()]
        $OMSResourceGroup, 

        [Parameter(ParameterSetName="NewModel", Mandatory = $true)]
        [Parameter(ParameterSetName="OldModel", Mandatory = $true)]
        [string]
        [ValidateNotNullOrEmpty()]
        $OMSWorkspaceId, 
        
        [Parameter(ParameterSetName="OldModel", Mandatory = $false)]
        [string]
        $ApplicationSubscriptionId,     
        
        [Parameter(ParameterSetName="OldModel", Mandatory = $false)]        
        [string]
        $ApplicationResourceGroups,     
        
        [Parameter(ParameterSetName="NewModel", Mandatory = $false)]
        [Parameter(ParameterSetName="OldModel", Mandatory = $false)]
        [string]
        $ViewName = "SecurityCompliance", 
                        
        [string]
        [Parameter(ParameterSetName="OldModel", Mandatory = $false)]
        $SecurityContactEmails,

        [OMSInstallationOption]
        [Parameter(ParameterSetName="OldModel", Mandatory = $false)]
        $OMSInstallationOption,

        [switch]
        [Parameter(ParameterSetName="OldModel", Mandatory = $true)]
        $UseOldModel,

        [switch]
        [Parameter(Mandatory = $False)]
        $ValidateOnly
    )
    Begin
    {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [ListenerHelper]::RegisterListeners();
    }
    Process
    {
        try
        {
            if($null -eq $OMSInstallationOption)
            {
                $OMSInstallationOption = [OMSInstallationOption]::All
            }
            $OMSMonitoringInstance = [OMSMonitoring]::new($OMSSubscriptionId, $OMSResourceGroup, $OMSWorkspaceId, $PSCmdlet.MyInvocation);
            $OMSMonitoringInstance.InvokeFunction($OMSMonitoringInstance.ConfigureOMS, @($ApplicationSubscriptionId, $ApplicationResourceGroups, $ViewName, $SecurityContactEmails, $OMSInstallationOption, $ValidateOnly, $UseOldModel));
        }
        catch
        {
            [EventBase]::PublishGenericException($_);
        }
    }
    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}


function Uninstall-AzSKOMSetup
{
    param(
        # OMS SubscriptionID
        [Parameter(Mandatory=$true)]
        [string]
        $OMSSubscriptionID,

        # OMS WorkspaceId
        [Parameter(Mandatory=$True)]
        [string]
        $OMSWorkspaceId,

        # OMS ResourceGroupName
        [Parameter(Mandatory=$true)]
        [string]
        $OMSResourceGroup        
    )
    Begin
    {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [ListenerHelper]::RegisterListeners();
    }
    Process
    {
        try
        {
            $OMSMonitoringInstance = [OMSMonitoring]::new($OMSSubscriptionId, $OMSResourceGroup, $OMSWorkspaceId, $PSCmdlet.MyInvocation);
            $OMSMonitoringInstance.UninstallOMSPack()
        }
        catch
        {
            [EventBase]::PublishGenericException($_);
        }
    }
    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}