summaryrefslogtreecommitdiff
path: root/spec/functional/resource/user/windows_spec.rb
blob: 6bab270f56293121dd134c4ef142ca5f893b20fe (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
# Author:: Jay Mundrawala (<jdm@chef.io>)
# Author:: Stuart Preston (<stuart@chef.io>)
#
# Copyright:: Copyright 2015-2018, Chef Software
# 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"
require "chef/mixin/shell_out"

describe Chef::Provider::User::Windows, :windows_only do
  include Chef::Mixin::ShellOut

  let(:username) { "ChefFunctionalTest" }
  let(:password) { "DummyP2ssw0rd!" }

  let(:node) do
    n = Chef::Node.new
    n.consume_external_attrs(OHAI_SYSTEM.data.dup, {})
    n
  end

  let(:events) { Chef::EventDispatch::Dispatcher.new }
  let(:logger) { double("Mixlib::Log::Child").as_null_object }
  let(:run_context) { Chef::RunContext.new(node, {}, events) }
  let(:new_resource) do
    Chef::Resource::User::WindowsUser.new(username, run_context).tap do |r|
      r.provider(Chef::Provider::User::Windows)
      r.password(password)
    end
  end

  def delete_user(u)
    shell_out("net user #{u} /delete")
  end

  def backup_secedit_policy
    backup_command = "secedit /export /cfg #{ENV["TEMP"]}\\secedit_restore.inf /areas SECURITYPOLICY"
    shell_out(backup_command)
  end

  def restore_secedit_policy
    security_database = "C:\\windows\\security\\database\\seceditnew.sdb"
    restore_command = "secedit /configure /db #{security_database} /cfg #{ENV["TEMP"]}\\secedit_restore.inf /areas SECURITYPOLICY"
    shell_out(restore_command)
  end

  def set_windows_minimum_password_length(minimum_password_length = 0)
    require "tempfile"
    temp_security_database = "C:\\windows\\security\\database\\seceditnew.sdb"
    temp_security_template = Tempfile.new(["chefpolicy", ".inf"])
    file_content = <<~EOF
      [Unicode]
      Unicode=yes
      [System Access]
      MinimumPasswordLength = #{minimum_password_length}
      PasswordComplexity = 0
      [Version]
      signature="$CHICAGO$"
      Revision=1
    EOF
    windows_template_path = temp_security_template.path.gsub("/") { "\\" }
    security_command = "secedit /configure /db #{temp_security_database} /cfg #{windows_template_path} /areas SECURITYPOLICY"
    temp_security_template.write(file_content)
    temp_security_template.close
    shell_out(security_command)
  end

  before(:all) do
    backup_secedit_policy
  end

  before(:each) do
    delete_user(username)
    allow(run_context).to receive(:logger).and_return(logger)
  end

  after(:all) do
    restore_secedit_policy
  end

  describe "action :create" do
    context "on a Windows system with a policy that requires non-blank passwords and no complexity requirements" do

      before(:all) do
        set_windows_minimum_password_length(1)
      end

      context "when a username and non-empty password are given" do
        it "creates a user" do
          new_resource.run_action(:create)
          expect(new_resource).to be_updated_by_last_action
          expect(shell_out("net user #{username}").exitstatus).to eq(0)
        end

        it "is idempotent" do
          new_resource.run_action(:create)
          new_resource.run_action(:create)
          expect(new_resource).not_to be_updated_by_last_action
        end

        it "allows changing the password" do
          new_resource.run_action(:create)
          new_resource.password(SecureRandom.uuid)
          new_resource.run_action(:create)
          expect(new_resource).to be_updated_by_last_action
        end
      end

      context "when a username and empty password are given" do
        it "does not create the specified user" do
          new_resource.password("")
          expect { new_resource.run_action(:create) }.to raise_exception(Chef::Exceptions::Win32APIError, /The password does not meet the password policy requirements/)
        end
      end
    end

    context "on a Windows system with a policy that allows blank passwords" do

      before(:all) do
        set_windows_minimum_password_length(0)
      end

      context "when a username and non-empty password are given" do
        it "creates a user" do
          new_resource.run_action(:create)
          expect(new_resource).to be_updated_by_last_action
          expect(shell_out("net user #{username}").exitstatus).to eq(0)
        end

        it "is idempotent" do
          new_resource.run_action(:create)
          new_resource.run_action(:create)
          expect(new_resource).not_to be_updated_by_last_action
        end

        it "allows changing the password" do
          new_resource.run_action(:create)
          new_resource.password(SecureRandom.uuid)
          new_resource.run_action(:create)
          expect(new_resource).to be_updated_by_last_action
        end
      end

      context "when a username and empty password are given" do
        it "creates a user" do
          new_resource.password("")
          new_resource.run_action(:create)
          expect(new_resource).to be_updated_by_last_action
          expect(shell_out("net user #{username}").exitstatus).to eq(0)
        end

        it "is idempotent" do
          new_resource.password("")
          new_resource.run_action(:create)
          new_resource.run_action(:create)
          expect(new_resource).not_to be_updated_by_last_action
        end

        it "allows changing the password from empty to a value" do
          new_resource.password("")
          new_resource.run_action(:create)
          new_resource.password(SecureRandom.uuid)
          new_resource.run_action(:create)
          expect(new_resource).to be_updated_by_last_action
        end
      end
    end

    context "with a gid specified" do
      it "warns unsupported" do
        expect(logger).to receive(:warn).with(/not implemented/)
        new_resource.gid("agroup")
        new_resource.run_action(:create)
      end
    end
  end

  describe "action :remove" do
    before do
      new_resource.run_action(:create)
    end

    it "deletes the user" do
      new_resource.run_action(:remove)
      expect(new_resource).to be_updated_by_last_action
      expect(shell_out("net user #{username}").exitstatus).to eq(2)
    end

    it "is idempotent" do
      new_resource.run_action(:remove)
      new_resource.run_action(:remove)
      expect(new_resource).not_to be_updated_by_last_action
    end
  end

  describe "action :lock" do
    before do
      new_resource.run_action(:create)
    end

    it "locks the user account" do
      new_resource.run_action(:lock)
      expect(new_resource).to be_updated_by_last_action
      expect(shell_out("net user #{username}").stdout).to match(/Account active\s*No/)
    end

    it "is idempotent" do
      new_resource.run_action(:lock)
      new_resource.run_action(:lock)
      expect(new_resource).not_to be_updated_by_last_action
    end
  end

  describe "action :unlock" do
    before do
      new_resource.run_action(:create)
      new_resource.run_action(:lock)
    end

    it "unlocks the user account" do
      new_resource.run_action(:unlock)
      expect(new_resource).to be_updated_by_last_action
      expect(shell_out("net user #{username}").stdout).to match(/Account active\s*Yes/)
    end

    it "is idempotent" do
      new_resource.run_action(:unlock)
      new_resource.run_action(:unlock)
      expect(new_resource).not_to be_updated_by_last_action
    end
  end
end