Chef: TypeError: no implicit conversion of nil into String in dscl_user create

Created on 2 Feb 2017  Â·  8Comments  Â·  Source: chef/chef

Description

dscl_user fails to create a user with "TypeError: no implicit conversion of nil into String" in https://github.com/chef/chef/blob/7de787be3e792a58285f089c31e2b58b995fde08/lib/chef/provider/user/dscl.rb#L321.
The problem was brought by commit https://github.com/chef/chef/commit/7de787be3e792a58285f089c31e2b58b995fde08.

         def current_home_exists?
 -          ::File.exist?("#{current_resource.home}")
 +          ::File.exist?(current_resource.home)
          end

current_resource.home is nil when the user does not exist. File.exist? now raises an exception @lamont-granquist

Chef Version

chef-client 12.18.31

Platform Version

macOS 10.12.3

Replication Case

dscl_user("dummy") do
  supports {:manage_home=>true, :non_unique=>false}
  username "dummy"
  home "/Users/dummy"
  shell "/bin/bash"
end

Client Output

* dscl_user[dummy] action create[2017-02-02T15:12:42+00:00] INFO: Processing dscl_user[dummy] action create (/var/chef/cache/cookbooks/user/providers/account.rb line 120)
...
/var/db/dslocal/nodes/Default/users/dummy.plist: file does not exist or is not readable or is not a regular file (Error Domain=NSCocoaErrorDomain Code=260 "The file “dummy.plist” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/var/db/dslocal/nodes/Default/users/dummy.plist, NSUnderlyingError=0x7fd786c0b3a0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}})
...
DEBUG: dscl_user[dummy] user does not exist
...
Error executing action `create` on resource 'dscl_user[dummy]'
TypeError
---------
no implicit conversion of nil into String

Stacktrace

TypeError: dscl_user[dummy] (/var/chef/cache/cookbooks/user/providers/account.rb line 120) had an error: TypeError: no implicit conversion of nil into String
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.18.31/lib/chef/provider/user/dscl.rb:321:in `exist?'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.18.31/lib/chef/provider/user/dscl.rb:321:in `current_home_exists?'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.18.31/lib/chef/provider/user/dscl.rb:305:in `dscl_set_home'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.18.31/lib/chef/provider/user/dscl.rb:178:in `create_user'

Most helpful comment

I hit hit issue too. I investigated this and the problem as I see it is when dscl creates a user the user's metadata doesn't have a field for the home directory because the home directory hasn't been created yet. So current_resource.home has a nil value. But the rest of the user resource is coded around the expectation that an absent home field would result in an empty string value because that's what happens on regular linux systems.

I put the following code in a libraries/provider_user_dscl.rb file in one of my cookbooks to monkeypatch around the issue. It simply sets current_resource.home to an empty string if its original value is nil.

# this fixes this bug: https://github.com/chef/chef/issues/5777
module Chef::Provider::User::DsclExtensions
  def load_current_resource
    super
    current_resource.home('') if current_resource.home.nil?
    current_resource
  end
end

class Chef
  class Provider
    class User
      class Dscl
        prepend Chef::Provider::User::DsclExtensions
      end
    end
  end
end

All 8 comments

Was caught by this as well, downgrading to chef-client 12.17.44 does not have this issue.

I just ran into this, but what's odd is when I bootstrap chef for the first time, it creates the account correctly. It's only when running chef-client manually that I see this.

I'm still experiencing this too with chef 13.0.118. Downgrading to 12.17.44 like @ryanmoon fixed this.

The user resource is severely limited by this issue. I keep hitting this issue unless I create the account during the first chef run. It can edit the account, but if the user is deleted, the entire chef run is broken from that point on and cannot be recovered unless you downgrade chef.

This needs to be fixed for macOS.

Can also confirm this issue. Been struggling with it for two days. When is the patch getting pushed?

I hit hit issue too. I investigated this and the problem as I see it is when dscl creates a user the user's metadata doesn't have a field for the home directory because the home directory hasn't been created yet. So current_resource.home has a nil value. But the rest of the user resource is coded around the expectation that an absent home field would result in an empty string value because that's what happens on regular linux systems.

I put the following code in a libraries/provider_user_dscl.rb file in one of my cookbooks to monkeypatch around the issue. It simply sets current_resource.home to an empty string if its original value is nil.

# this fixes this bug: https://github.com/chef/chef/issues/5777
module Chef::Provider::User::DsclExtensions
  def load_current_resource
    super
    current_resource.home('') if current_resource.home.nil?
    current_resource
  end
end

class Chef
  class Provider
    class User
      class Dscl
        prepend Chef::Provider::User::DsclExtensions
      end
    end
  end
end

Wow, thanks @jeremiahsnapp, that fixed it!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings