SVT/FixControl.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
Set-StrictMode -Version Latest
function Repair-AzSKAzureServicesSecurity 
{
    <#
    .SYNOPSIS
    This command would help in fixing the security controls for the Azure resources meeting the specified input criteria.
 
    .PARAMETER ResourceGroupNames
        ResourceGroups for which the security evaluation has to be performed. Comma seperated values are supported. Wildcards are not permitted. By default, the command gets all resources in the subscription.
    .PARAMETER ResourceType
        Gets only resources of the specified resource type. Wildcards are not permitted. e.g.: Microsoft.KeyVault/vaults. Run command 'Get-AzSKSupportedResourceTypes' to get the list of supported types.
    .PARAMETER ResourceTypeName
        Friendly name of resource type. e.g.: KeyVault. Run command 'Get-AzSKSupportedResourceTypes' to get the list of supported values.
    .PARAMETER ResourceName
        Gets a resource with the specified name. Wildcards are not permitted. By default, the command gets all resources in the subscription.
 
    .NOTES
    This command helps the application team to fix the Azure resources for security compliance
 
    .LINK
    https://aka.ms/azskossdocs
 
    #>

    [OutputType([String])]
    Param
    (

        [string]
        [Parameter(Mandatory = $true, HelpMessage = "ResourceGroups for which the security evaluation has to be performed. Comma seperated values are supported. Wildcards are not permitted. By default, the command gets all resources in the subscription.")]
        $ParameterFilePath,

        #[string]
        #[Parameter(Mandatory = $false, HelpMessage = "Subscription id for which the security evaluation has to be performed.")]
        #$SubscriptionIds,

        [string]
        [Parameter(Mandatory = $false, HelpMessage = "ResourceGroups for which the security evaluation has to be performed. Comma seperated values are supported. Wildcards are not permitted. By default, the command gets all resources in the subscription.")]
        $ResourceGroupNames,
        
        [string]
        [Parameter(Mandatory = $false, HelpMessage = "Gets only resources of the specified resource type. Wildcards are not permitted. e.g.: Microsoft.KeyVault/vaults. Run command 'Get-AzSKSupportedResourceTypes' to get the list of supported types.")]
        $ResourceTypes,

        [Parameter(Mandatory = $false, HelpMessage = "Friendly name of resource type. e.g.: KeyVault. Run command 'Get-AzSKSupportedResourceTypes' to get the list of supported values.")]
        [string]
        $ResourceTypeNames,
        
        [string]
        [Parameter(Mandatory = $false, HelpMessage = "Gets a resource with the specified name. Wildcards are not permitted. By default, the command gets all resources in the subscription.")]
        $ResourceNames,

        [string] 
        [Parameter(Mandatory = $false, HelpMessage = "Comma separated control ids to filter the security controls. e.g.: Azure_Subscription_AuthZ_Limit_Admin_Owner_Count, Azure_Storage_DP_Encrypt_At_Rest_Blob etc.")]
        $ControlIds,

        [switch] 
        [Parameter(Mandatory = $false, HelpMessage = "Bypass consent to modify Azure resources.")]
        $Force
    )

    Begin
    {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [ListenerHelper]::RegisterListeners();
    }

    Process
    {
        try 
        {
            $SubscriptionIds = "";
            $resolver = [FixControlConfigResolver]::new($ParameterFilePath, $SubscriptionIds, $ResourceGroupNames, $ResourceTypes, $ResourceTypeNames, $ResourceNames, $ControlIds);
            if($resolver)
            {
                #currently supporting for only 1 subscription
                $fixControlParam = $resolver.GetFixControlParameters() | Select-Object -First 1
                if($fixControlParam)
                {
                    $controlFixes = [ControlSecurityFixes]::new($fixControlParam.SubscriptionContext.SubscriptionId, $PSCmdlet.MyInvocation, $fixControlParam, $resolver.ConfigFilePath);
                    if ($controlFixes) 
                    {
                        $controlFixes.Force = $Force;
                        return $controlFixes.InvokeFunction($controlFixes.ImplementFix);
                    }   
                }
            }
        }
        catch 
        {
            [EventBase]::PublishGenericException($_);
        }  
    }

    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}

function Repair-AzSKSubscriptionSecurity 
{
    <#
    .SYNOPSIS
    This command would help in fixing the security controls for the Azure subscription.
 
    .NOTES
    This command helps the application team to fix the Azure subscription for security compliance
 
    .LINK
    https://aka.ms/azskossdocs
 
    #>

    [OutputType([String])]
    Param
    (

        [string]
        [Parameter(Mandatory = $true, HelpMessage = "ResourceGroups for which the security evaluation has to be performed. Comma seperated values are supported. Wildcards are not permitted. By default, the command gets all resources in the subscription.")]
        $ParameterFilePath,

        #[string]
        #[Parameter(Mandatory = $false, HelpMessage = "Subscription id for which the security evaluation has to be performed.")]
        #$SubscriptionIds,

        [string] 
        [Parameter(Mandatory = $false, HelpMessage = "Comma separated control ids to filter the security controls. e.g.: Azure_Subscription_AuthZ_Limit_Admin_Owner_Count, Azure_Storage_DP_Encrypt_At_Rest_Blob etc.")]
        $ControlIds,

        [switch] 
        [Parameter(Mandatory = $false, HelpMessage = "Bypass consent to modify Azure resources.")]
        $Force
    )

    Begin
    {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [ListenerHelper]::RegisterListeners();
    }

    Process
    {
        try 
        {
            $SubscriptionIds = "";
            $resolver = [FixControlConfigResolver]::new($ParameterFilePath, $SubscriptionIds, $ControlIds, $true);
            if($resolver)
            {
                #currently supporting for only 1 subscription
                $fixControlParam = $resolver.GetFixControlParameters() | Select-Object -First 1
                if($fixControlParam)
                {
                    $controlFixes = [ControlSecurityFixes]::new($fixControlParam.SubscriptionContext.SubscriptionId, $PSCmdlet.MyInvocation, $fixControlParam, $resolver.ConfigFilePath);
                    if ($controlFixes) 
                    {
                        $controlFixes.Force = $Force;
                        return $controlFixes.InvokeFunction($controlFixes.ImplementFix);
                    }   
                }
            }
        }
        catch 
        {
            [EventBase]::PublishGenericException($_);
        }  
    }

    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}