summaryrefslogtreecommitdiff
path: root/spec/unit/resource/windows_firewall_rule_spec.rb
blob: 629e91d74f9b2adff3ede456678b2535c23cbf3b (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# Author:: Tor Magnus Rakvåg (tor.magnus@outlook.com)
# Copyright:: 2018, Intility AS
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "spec_helper"

describe Chef::Resource::WindowsFirewallRule do
  let(:resource) { Chef::Resource::WindowsFirewallRule.new("rule") }
  let(:provider) { resource.provider_for_action(:enable) }

  it "has a resource name of :windows_firewall_rule" do
    expect(resource.resource_name).to eql(:windows_firewall_rule)
  end

  it "the name_property is 'rule_name'" do
    expect(resource.rule_name).to eql("rule")
  end

  it "the default action is :create" do
    expect(resource.action).to eql([:create])
  end

  it "supports :create and :delete actions" do
    expect { resource.action :create }.not_to raise_error
    expect { resource.action :delete }.not_to raise_error
  end

  it "the rule_name property accepts strings" do
    resource.rule_name("rule2")
    expect(resource.rule_name).to eql("rule2")
  end

  it "the description property accepts strings" do
    resource.description("firewall rule")
    expect(resource.description).to eql("firewall rule")
  end

  it "the local_address property accepts strings" do
    resource.local_address("192.168.1.1")
    expect(resource.local_address).to eql("192.168.1.1")
  end

  it "the local_port property accepts integers" do
    resource.local_port(8080)
    expect(resource.local_port).to eql(["8080"])
  end

  it "the local_port property accepts strings" do
    resource.local_port("8080")
    expect(resource.local_port).to eql(["8080"])
  end

  it "the local_port property accepts comma separated lists without spaces" do
    resource.local_port("8080,8081")
    expect(resource.local_port).to eql(%w{8080 8081})
  end

  it "the local_port property accepts comma separated lists with spaces" do
    resource.local_port("8080, 8081")
    expect(resource.local_port).to eql(%w{8080 8081})
  end

  it "the local_port property accepts arrays and coerces to a sorta array of strings" do
    resource.local_port([8081, 8080])
    expect(resource.local_port).to eql(%w{8080 8081})
  end

  it "the remote_address property accepts strings" do
    resource.remote_address("8.8.4.4")
    expect(resource.remote_address).to eql("8.8.4.4")
  end

  it "the remote_port property accepts strings" do
    resource.remote_port("8081")
    expect(resource.remote_port).to eql(["8081"])
  end

  it "the remote_port property accepts integers" do
    resource.remote_port(8081)
    expect(resource.remote_port).to eql(["8081"])
  end

  it "the remote_port property accepts comma separated lists without spaces" do
    resource.remote_port("8080,8081")
    expect(resource.remote_port).to eql(%w{8080 8081})
  end

  it "the remote_port property accepts comma separated lists with spaces" do
    resource.remote_port("8080, 8081")
    expect(resource.remote_port).to eql(%w{8080 8081})
  end

  it "the remote_port property accepts arrays and coerces to a sorta array of strings" do
    resource.remote_port([8081, 8080])
    expect(resource.remote_port).to eql(%w{8080 8081})
  end

  it "the direction property accepts :inbound and :outbound" do
    resource.direction(:inbound)
    expect(resource.direction).to eql(:inbound)
    resource.direction(:outbound)
    expect(resource.direction).to eql(:outbound)
  end

  it "the direction property coerces strings to symbols" do
    resource.direction("Inbound")
    expect(resource.direction).to eql(:inbound)
  end

  it "the protocol property accepts strings" do
    resource.protocol("TCP")
    expect(resource.protocol).to eql("TCP")
  end

  it "the firewall_action property accepts :allow, :block and :notconfigured" do
    resource.firewall_action(:allow)
    expect(resource.firewall_action).to eql(:allow)
    resource.firewall_action(:block)
    expect(resource.firewall_action).to eql(:block)
    resource.firewall_action(:notconfigured)
    expect(resource.firewall_action).to eql(:notconfigured)
  end

  it "the firewall_action property coerces strings to symbols" do
    resource.firewall_action("Allow")
    expect(resource.firewall_action).to eql(:allow)
  end

  it "the profile property accepts :public, :private, :domain, :any and :notapplicable" do
    resource.profile(:public)
    expect(resource.profile).to eql(:public)
    resource.profile(:private)
    expect(resource.profile).to eql(:private)
    resource.profile(:domain)
    expect(resource.profile).to eql(:domain)
    resource.profile(:any)
    expect(resource.profile).to eql(:any)
    resource.profile(:notapplicable)
    expect(resource.profile).to eql(:notapplicable)
  end

  it "the profile property coerces strings to symbols" do
    resource.profile("Public")
    expect(resource.profile).to eql(:public)
  end

  it "the program property accepts strings" do
    resource.program("C:/Test/test.exe")
    expect(resource.program).to eql("C:/Test/test.exe")
  end

  it "the service property accepts strings" do
    resource.service("Spooler")
    expect(resource.service).to eql("Spooler")
  end

  it "the interface_type property accepts :any, :wireless, :wired and :remoteaccess" do
    resource.interface_type(:any)
    expect(resource.interface_type).to eql(:any)
    resource.interface_type(:wireless)
    expect(resource.interface_type).to eql(:wireless)
    resource.interface_type(:wired)
    expect(resource.interface_type).to eql(:wired)
    resource.interface_type(:remoteaccess)
    expect(resource.interface_type).to eql(:remoteaccess)
  end

  it "the interface_type property coerces strings to symbols" do
    resource.interface_type("Any")
    expect(resource.interface_type).to eql(:any)
  end

  it "the enabled property accepts true and false" do
    resource.enabled(true)
    expect(resource.enabled).to eql(true)
    resource.enabled(false)
    expect(resource.enabled).to eql(false)
  end

  it "aliases :localip to :local_address" do
    resource.localip("192.168.30.30")
    expect(resource.local_address).to eql("192.168.30.30")
  end

  it "aliases :remoteip to :remote_address" do
    resource.remoteip("8.8.8.8")
    expect(resource.remote_address).to eql("8.8.8.8")
  end

  it "aliases :localport to :local_port" do
    resource.localport("80")
    expect(resource.local_port).to eql(["80"])
  end

  it "aliases :remoteport to :remote_port" do
    resource.remoteport("8080")
    expect(resource.remote_port).to eql(["8080"])
  end

  it "aliases :interfacetype to :interface_type" do
    resource.interfacetype(:any)
    expect(resource.interface_type).to eql(:any)
  end

  describe "#firewall_command" do
    before do
      resource.rule_name("test_rule")
    end

    context "#new" do
      it "build a minimal command" do
        expect(provider.firewall_command("New")).to eql("New-NetFirewallRule -Name 'test_rule' -DisplayName 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets a description" do
        resource.description("New description")
        expect(provider.firewall_command("New")).to eql("New-NetFirewallRule -Name 'test_rule' -DisplayName 'test_rule' -Description 'New description' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets LocalAddress" do
        resource.local_address("127.0.0.1")
        expect(provider.firewall_command("New")).to eql("New-NetFirewallRule -Name 'test_rule' -DisplayName 'test_rule' -Description 'Firewall rule' -LocalAddress '127.0.0.1' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets LocalPort" do
        resource.local_port("80")
        expect(provider.firewall_command("New")).to eql("New-NetFirewallRule -Name 'test_rule' -DisplayName 'test_rule' -Description 'Firewall rule' -LocalPort 80 -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets RemoteAddress" do
        resource.remote_address("8.8.8.8")
        expect(provider.firewall_command("New")).to eql("New-NetFirewallRule -Name 'test_rule' -DisplayName 'test_rule' -Description 'Firewall rule' -RemoteAddress '8.8.8.8' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets RemotePort" do
        resource.remote_port("443")
        expect(provider.firewall_command("New")).to eql("New-NetFirewallRule -Name 'test_rule' -DisplayName 'test_rule' -Description 'Firewall rule' -RemotePort 443 -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets Direction" do
        resource.direction(:outbound)
        expect(provider.firewall_command("New")).to eql("New-NetFirewallRule -Name 'test_rule' -DisplayName 'test_rule' -Description 'Firewall rule' -Direction 'outbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets Protocol" do
        resource.protocol("UDP")
        expect(provider.firewall_command("New")).to eql("New-NetFirewallRule -Name 'test_rule' -DisplayName 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'UDP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets Action" do
        resource.firewall_action(:block)
        expect(provider.firewall_command("New")).to eql("New-NetFirewallRule -Name 'test_rule' -DisplayName 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'TCP' -Action 'block' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets Profile" do
        resource.profile(:private)
        expect(provider.firewall_command("New")).to eql("New-NetFirewallRule -Name 'test_rule' -DisplayName 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'private' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets Program" do
        resource.program("C:/calc.exe")
        expect(provider.firewall_command("New")).to eql("New-NetFirewallRule -Name 'test_rule' -DisplayName 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -Program 'C:/calc.exe' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets Service" do
        resource.service("Spooler")
        expect(provider.firewall_command("New")).to eql("New-NetFirewallRule -Name 'test_rule' -DisplayName 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -Service 'Spooler' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets InterfaceType" do
        resource.interface_type(:wired)
        expect(provider.firewall_command("New")).to eql("New-NetFirewallRule -Name 'test_rule' -DisplayName 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'wired' -Enabled 'true'")
      end

      it "sets Enabled" do
        resource.enabled(false)
        expect(provider.firewall_command("New")).to eql("New-NetFirewallRule -Name 'test_rule' -DisplayName 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'false'")
      end

      it "sets all properties" do
        resource.rule_name("test_rule_the_second")
        resource.description("some other rule")
        resource.local_address("192.168.40.40")
        resource.local_port("80")
        resource.remote_address("8.8.4.4")
        resource.remote_port("8081")
        resource.direction(:outbound)
        resource.protocol("UDP")
        resource.firewall_action(:notconfigured)
        resource.profile(:domain)
        resource.program('%WINDIR%\System32\lsass.exe')
        resource.service("SomeService")
        resource.interface_type(:remoteaccess)
        resource.enabled(false)
        expect(provider.firewall_command("New")).to eql("New-NetFirewallRule -Name 'test_rule_the_second' -DisplayName 'test_rule_the_second' -Description 'some other rule' -LocalAddress '192.168.40.40' -LocalPort 80 -RemoteAddress '8.8.4.4' -RemotePort 8081 -Direction 'outbound' -Protocol 'UDP' -Action 'notconfigured' -Profile 'domain' -Program '%WINDIR%\\System32\\lsass.exe' -Service 'SomeService' -InterfaceType 'remoteaccess' -Enabled 'false'")
      end
    end

    context "#set" do
      it "build a minimal command" do
        expect(provider.firewall_command("Set")).to eql("Set-NetFirewallRule -Name 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets a description" do
        resource.description("New description")
        expect(provider.firewall_command("Set")).to eql("Set-NetFirewallRule -Name 'test_rule' -Description 'New description' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets LocalAddress" do
        resource.local_address("127.0.0.1")
        expect(provider.firewall_command("Set")).to eql("Set-NetFirewallRule -Name 'test_rule' -Description 'Firewall rule' -LocalAddress '127.0.0.1' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets LocalPort" do
        resource.local_port("80")
        expect(provider.firewall_command("Set")).to eql("Set-NetFirewallRule -Name 'test_rule' -Description 'Firewall rule' -LocalPort 80 -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets RemoteAddress" do
        resource.remote_address("8.8.8.8")
        expect(provider.firewall_command("Set")).to eql("Set-NetFirewallRule -Name 'test_rule' -Description 'Firewall rule' -RemoteAddress '8.8.8.8' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets RemotePort" do
        resource.remote_port("443")
        expect(provider.firewall_command("Set")).to eql("Set-NetFirewallRule -Name 'test_rule' -Description 'Firewall rule' -RemotePort 443 -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets Direction" do
        resource.direction(:outbound)
        expect(provider.firewall_command("Set")).to eql("Set-NetFirewallRule -Name 'test_rule' -Description 'Firewall rule' -Direction 'outbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets Protocol" do
        resource.protocol("UDP")
        expect(provider.firewall_command("Set")).to eql("Set-NetFirewallRule -Name 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'UDP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets Action" do
        resource.firewall_action(:block)
        expect(provider.firewall_command("Set")).to eql("Set-NetFirewallRule -Name 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'TCP' -Action 'block' -Profile 'any' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets Profile" do
        resource.profile(:private)
        expect(provider.firewall_command("Set")).to eql("Set-NetFirewallRule -Name 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'private' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets Program" do
        resource.program("C:/calc.exe")
        expect(provider.firewall_command("Set")).to eql("Set-NetFirewallRule -Name 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -Program 'C:/calc.exe' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets Service" do
        resource.service("Spooler")
        expect(provider.firewall_command("Set")).to eql("Set-NetFirewallRule -Name 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -Service 'Spooler' -InterfaceType 'any' -Enabled 'true'")
      end

      it "sets InterfaceType" do
        resource.interface_type(:wired)
        expect(provider.firewall_command("Set")).to eql("Set-NetFirewallRule -Name 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'wired' -Enabled 'true'")
      end

      it "sets Enabled" do
        resource.enabled(false)
        expect(provider.firewall_command("Set")).to eql("Set-NetFirewallRule -Name 'test_rule' -Description 'Firewall rule' -Direction 'inbound' -Protocol 'TCP' -Action 'allow' -Profile 'any' -InterfaceType 'any' -Enabled 'false'")
      end

      it "sets all properties" do
        resource.rule_name("test_rule_the_second")
        resource.description("some other rule")
        resource.local_address("192.168.40.40")
        resource.local_port("80")
        resource.remote_address("8.8.4.4")
        resource.remote_port("8081")
        resource.direction(:outbound)
        resource.protocol("UDP")
        resource.firewall_action(:notconfigured)
        resource.profile(:domain)
        resource.program('%WINDIR%\System32\lsass.exe')
        resource.service("SomeService")
        resource.interface_type(:remoteaccess)
        resource.enabled(false)
        expect(provider.firewall_command("Set")).to eql("Set-NetFirewallRule -Name 'test_rule_the_second' -Description 'some other rule' -LocalAddress '192.168.40.40' -LocalPort 80 -RemoteAddress '8.8.4.4' -RemotePort 8081 -Direction 'outbound' -Protocol 'UDP' -Action 'notconfigured' -Profile 'domain' -Program '%WINDIR%\\System32\\lsass.exe' -Service 'SomeService' -InterfaceType 'remoteaccess' -Enabled 'false'")
      end
    end
  end
end