Framework/Core/SVT/Services/EventHub.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
#using namespace Microsoft.Azure.Commands.EventHub.Models
Set-StrictMode -Version Latest 
class EventHub: SVTBase
{       
    hidden [PSObject[]] $NamespacePolicies = @();
    hidden [PSObject[]] $EventHubs = @();
    hidden [HashTable] $EHChildAccessPolicies = @{};

    EventHub([string] $subscriptionId, [string] $resourceGroupName, [string] $resourceName): 
        Base($subscriptionId, $resourceGroupName, $resourceName) 
    { 
        $this.GetEventHubDetails();
        $this.GetEHAccessPolicies();
    }

    EventHub([string] $subscriptionId, [SVTResource] $svtResource): 
        Base($subscriptionId, $svtResource) 
    { 
        $this.GetEventHubDetails();
        $this.GetEHAccessPolicies();
    }

    hidden [void] GetEventHubDetails()
    {
        if (-not $this.NamespacePolicies) {
            try
            {
                $this.NamespacePolicies = (Get-AzEventHubAuthorizationRule -ResourceGroupName $this.ResourceContext.ResourceGroupName `
                        -NamespaceName $this.ResourceContext.ResourceName | Select-Object Id, Name, Rights)
            }
            catch
            {
                # This block is intentionally left blank to handle exception while fetching namespace access policy
            }
            
        }

        if (-not $this.EventHubs) {
            try
            {
                $this.EventHubs = Get-AzEventHub -ResourceGroupName $this.ResourceContext.ResourceGroupName -NamespaceName $this.ResourceContext.ResourceName
            }
            catch
            {
                # This block is intentionally left blank to handle exception while fetching eventhub of namespace
            }
        }
    }

    hidden [void] GetEHAccessPolicies()
    {
        #region "Event Hub"
        
        if(($this.EventHubs|Measure-Object).count -gt 0)
        {
            foreach ($eventHub in $this.EventHubs)
            {
                try
                {
                    $eventHubPolicies = Get-AzEventHubAuthorizationRule -ResourceGroupName $this.ResourceContext.ResourceGroupName `
                                        -NamespaceName $this.ResourceContext.ResourceName -EventHubName $eventHub.Name

                    $this.EHChildAccessPolicies.Add($eventHub, ($eventHubPolicies | Select-Object Id, Name, Rights))    
                }
                catch
                {
                    # This block is intentionally left blank to handle exception while fetching eventhub access policies
                }
            }
        }
        
        #endregion
    }

    hidden [ControlResult[]] CheckEventHubRootPolicy([ControlResult] $controlResult)
    {
        $isControlFailed = $false
        
        #region "NameSpace"

        if(($this.NamespacePolicies | Measure-Object).count -gt 1)
        {
            $controlResult.AddMessage([VerificationResult]::Failed, [MessageData]::new("Authorization rules for Event Hub namespace - ["+ $this.ResourceContext.ResourceName +"]. All the authorization rules except 'RootManageSharedAccessKey' must be removed from namespace level. Also validate that 'RootManageSharedAccessKey' authorization rule must not be used at Event Hub level to send and receive messages.", 
                $this.NamespacePolicies));       
        }
        else
        {
            $controlResult.AddMessage([VerificationResult]::Verify, [MessageData]::new("Authorization rules for namespace - ["+ $this.ResourceContext.ResourceName +"]. Validate that these rules must not be used at Event Hub level to send and receive messages.", 
                $this.NamespacePolicies));       
        }

        $controlResult.SetStateData("Authorization rules for namespace entities", $this.NamespacePolicies);

        #endregion

        return $controlResult;
    }

    hidden [ControlResult[]] CheckEventHubAuthorizationRule([ControlResult] $controlResult)
    {
        $isControlFailed = $false
        $fullPermissionEventHubs = @();
        $noPolicyEventHubs = @();

        #region "Event Hub"
        
        if(($this.EventHubs|Measure-Object).count -gt 0)
        {
            foreach ($eventHub in $this.EventHubs)
            {
                if($this.EHChildAccessPolicies.ContainsKey($eventHub) -and ($this.EHChildAccessPolicies[$eventHub] |Measure-Object).count -gt 0)
                {
                    foreach ($policy in $this.EHChildAccessPolicies[$eventHub])
                    {
                        if(($policy.Rights | Measure-Object).count -gt 1)
                        {
                            $fullPermissionEventHubs += $policy
                            $isControlFailed = $true
                        }
                    }
                }
                else
                {
                    $noPolicyEventHubs += $eventHub.Name
                    $isControlFailed = $true
                }
            }

            if($isControlFailed)
            {
                $controlResult.VerificationResult = [VerificationResult]::Failed;

                $faliedClients = New-Object -TypeName PSObject 
                if(($fullPermissionEventHubs | Measure-Object).count -gt 0)
                {
                    $faliedClients | Add-Member -NotePropertyName FailedEventHub -NotePropertyValue $fullPermissionEventHubs
                    $controlResult.AddMessage([MessageData]::new("Validate the authorization rules for the Event Hub are defined with limited permissions.", $fullPermissionEventHubs));
                }
                if(($noPolicyEventHubs | Measure-Object).count -gt 0)
                {
                    if([Helpers]::CheckMember($faliedClients,"FailedEventHub"))
                    {
                        $faliedClients.FailedEventHub += $noPolicyEventHubs
                    }
                    else
                    {
                        $faliedClients | Add-Member -NotePropertyName FailedEventHub -NotePropertyValue $noPolicyEventHubs
                    }
                    
                    $controlResult.AddMessage([MessageData]::new("No Authorization rules defined for following Event Hub. Either Event Hub is not in use or namespace level access policy is used. Applications (senders/receivers) must not use access policies defined at Service Bus namespace level.", $noPolicyEventHubs));
                }

                $controlResult.SetStateData("Access policy with minimum required permission must be defined for the Event Hub", $faliedClients);
            }
            else
            {
                $controlResult.AddMessage([VerificationResult]::Passed, [MessageData]::new("Authorization rules for the Event Hub are defined at correct entity level and with limited permissions."));
            }
        }
        else
        {
            $controlResult.AddMessage([VerificationResult]::Passed, [MessageData]::new("Event Hub not available in namespace - ["+ $this.ResourceContext.ResourceName +"]"));
        }
        
        #endregion

        return $controlResult;
    }
}