Framework/Core/SVT/Services/ServiceBus.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#using namespace Microsoft.Azure.Commands.ServiceBus.Models
Set-StrictMode -Version Latest 
class ServiceBus: SVTBase
{       
    hidden [PSObject[]] $NameSpacePolicies;
    hidden [PSObject[]] $Queues;
    hidden [PSObject[]] $Topics;

    ServiceBus([string] $subscriptionId, [string] $resourceGroupName, [string] $resourceName): 
        Base($subscriptionId, $resourceGroupName, $resourceName) 
    { 
    }

    ServiceBus([string] $subscriptionId, [SVTResource] $svtResource): 
        Base($subscriptionId, $svtResource) 
    { 
        $this.GetServiceBusDetails();
    }

    hidden [void] GetServiceBusDetails()
    {
        if (-not $this.NameSpacePolicies) {
            $this.NameSpacePolicies = Get-AzureRmServiceBusAuthorizationRule -ResourceGroup $this.ResourceContext.ResourceGroupName `
                        -NamespaceName $this.ResourceContext.ResourceName
        }
        
        # Get All Queues
        if (-not $this.Queues) {
            $this.Queues = Get-AzureRmServiceBusQueue -ResourceGroup $this.ResourceContext.ResourceGroupName -NamespaceName $this.ResourceContext.ResourceName
        }

        if (-not $this.Topics) {
            $this.Topics = Get-AzureRmServiceBusTopic -ResourceGroup $this.ResourceContext.ResourceGroupName -NamespaceName $this.ResourceContext.ResourceName
        }
    }

    hidden [ControlResult[]] CheckServiceBusRootPolicy([ControlResult] $controlResult)
    {
        [ControlResult[]] $resultControlResultList = @()

        #region "NameSpace"
        [ControlResult] $childControlResult = [ControlResult]@{
                            #ChildResourceName = $this.ResourceContext.ResourceName;
                        };

        $childControlResult.SetStateData("Authorization rules for Namespace", $this.NameSpacePolicies);

        $childControlResult.AddMessage([VerificationResult]::Verify, [MessageData]::new("Authorization rules for Namespace - ["+ $this.ResourceContext.ResourceName +"]. Validate that these rules must not be used at Queue/Topic level to send and receive messages.", 
                $this.NameSpacePolicies));   
        
        $resultControlResultList += $childControlResult

        #endregion

        #region "Queue"
        
        if(($this.Queues|Measure-Object).count -gt 0)
        {
            foreach ($queue in $this.Queues)
            {
                [ControlResult] $childControlResult = [ControlResult]@{
                        ChildResourceName = $queue.Name;
                    };

                $queuePolicies = Get-AzureRmServiceBusAuthorizationRule -ResourceGroup $this.ResourceContext.ResourceGroupName `
                                        -NamespaceName $this.ResourceContext.ResourceName -Queue $queue.Name

                if(($queuePolicies|Measure-Object).count -gt 0)
                {
                    $childControlResult.AddMessage([VerificationResult]::Verify, [MessageData]::new("Validate that Queue - ["+ $queue.Name +"] must not use access policies defined at Service Bus namespace level."));
                }
                else
                {
                    $childControlResult.AddMessage([VerificationResult]::Failed, [MessageData]::new("No Authorization rules defined for Queue - ["+ $queue.Name +"]. Applications (senders/receivers) must not use access policies defined at Service Bus namespace level."));
                }
                $resultControlResultList += $childControlResult
            }
        }
        else
        {
            $controlResult.AddMessage([MessageData]::new("Queue not available in Namespace - ["+ $this.ResourceContext.ResourceName +"]"));
        }
        
        #endregion
           
        #region "Topic"
        
        if(($this.Topics|Measure-Object).count -gt 0)
        {
            foreach ($topic in $this.Topics)
            {
                [ControlResult] $childControlResult = [ControlResult]@{
                        ChildResourceName = $topic.Name;
                    };

                $topicPolicies = Get-AzureRmServiceBusAuthorizationRule -ResourceGroup $this.ResourceContext.ResourceGroupName `
                                        -NamespaceName $this.ResourceContext.ResourceName -Topic $topic.Name

                if(($topicPolicies|Measure-Object).count -gt 0)
                {
                    $childControlResult.AddMessage([VerificationResult]::Verify, [MessageData]::new("Validate that Topic - ["+ $topic.Name +"] must not use access policies defined at Service Bus namespace level."));
                }
                else
                {
                    $childControlResult.AddMessage([VerificationResult]::Failed, [MessageData]::new("No Authorization rules defined for Topic - ["+ $topic.Name +"]. Applications (senders/receivers) must not use access policies defined at Service Bus namespace level."));
                }
                $resultControlResultList += $childControlResult
            }
        }
        else
        {
            $controlResult.AddMessage([MessageData]::new("Topics not available in Namespace - ["+ $this.ResourceContext.ResourceName +"]"));
        }

        #endregion

        return $resultControlResultList;
    }

    hidden [ControlResult[]] CheckServiceBusAuthorizationRule([ControlResult] $controlResult)
    {
        [ControlResult[]] $resultControlResultList = @()

        #region "NameSpace"
        [ControlResult] $childControlResult = [ControlResult]@{
                            #ChildResourceName = $this.ResourceContext.ResourceName;
                        };

        $childControlResult.SetStateData("Authorization rules for Namespace", $this.NameSpacePolicies);
        $childControlResult.AddMessage([VerificationResult]::Verify, [MessageData]::new("Authorization rules for Namespace - ["+ $this.ResourceContext.ResourceName +"]. Validate that these rules are defined at correct entity level and with more limited permissions.", 
                $this.NameSpacePolicies));   

        $resultControlResultList += $childControlResult
        #endregion

        #region "Queue"
        
        if(($this.Queues|Measure-Object).count -gt 0)
        {
            foreach ($queue in $this.Queues)
            {
                [ControlResult] $childControlResult = [ControlResult]@{
                        ChildResourceName = $queue.Name;
                    };

                $queuePolicies = Get-AzureRmServiceBusAuthorizationRule -ResourceGroup $this.ResourceContext.ResourceGroupName `
                                        -NamespaceName $this.ResourceContext.ResourceName -Queue $queue.Name

                if(($queuePolicies|Measure-Object).count -gt 0)
                {
                    $childControlResult.SetStateData("Authorization rules for Queue:" + $queue.Name , $queuePolicies);
                    $childControlResult.AddMessage([VerificationResult]::Verify, [MessageData]::new("Authorization rules for Queue - ["+ $queue.Name +"]. Validate that these rules are defined at correct entity level and with more limited permissions.", $queuePolicies));
                }
                else
                {
                    $childControlResult.AddMessage([VerificationResult]::Passed, [MessageData]::new("No Authorization rules defined for Queue - ["+ $queue.Name +"]."));
                }
                $resultControlResultList += $childControlResult
            }
        }
        else
        {
            $controlResult.AddMessage([MessageData]::new("Queue not available in Namespace - ["+ $this.ResourceContext.ResourceName +"]"));
        }
        
        #endregion
           
        #region "Topic"
        
        if(($this.Topics|Measure-Object).count -gt 0)
        {
            foreach ($topic in $this.Topics)
            {
                [ControlResult] $childControlResult = [ControlResult]@{
                        ChildResourceName = $topic.Name;
                    };

                $topicPolicies = Get-AzureRmServiceBusAuthorizationRule -ResourceGroup $this.ResourceContext.ResourceGroupName `
                                        -NamespaceName $this.ResourceContext.ResourceName -Topic $topic.Name

                if(($topicPolicies|Measure-Object).count -gt 0)
                {
                    $childControlResult.SetStateData("Authorization rules for Topic:" + $topic.Name , $topicPolicies);
                    $childControlResult.AddMessage([VerificationResult]::Verify, [MessageData]::new("Authorization rules for Topic - ["+ $topic.Name +"]. Validate that these rules are defined at correct entity level and with more limited permissions.", $topicPolicies));
                }
                else
                {
                    $childControlResult.AddMessage([VerificationResult]::Passed, [MessageData]::new("No Authorization rules defined for Topic - ["+ $topic.Name +"]."));
                }
                $resultControlResultList += $childControlResult
            }
        }
        else
        {
            $controlResult.AddMessage([MessageData]::new("Topics not available in Namespace - ["+ $this.ResourceContext.ResourceName +"]"));
        }

        #endregion

        return $resultControlResultList;
    }
}