diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2018-05-09 21:53:36 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2018-05-09 21:53:36 -0700 |
commit | 96c519ef6ee88d2adc4d4a35a080400b6a4f3659 (patch) | |
tree | 3821dc23c3614c015c740942a0f08f756cda330f /lib/chef/provider/user | |
parent | 189743e30d61d213aa1f90429a2cdf72467173fb (diff) | |
download | chef-96c519ef6ee88d2adc4d4a35a080400b6a4f3659.tar.gz |
migrate logic into aix and solaris subclasses
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'lib/chef/provider/user')
-rw-r--r-- | lib/chef/provider/user/aix.rb | 71 | ||||
-rw-r--r-- | lib/chef/provider/user/solaris.rb | 71 | ||||
-rw-r--r-- | lib/chef/provider/user/useradd.rb | 5 |
3 files changed, 136 insertions, 11 deletions
diff --git a/lib/chef/provider/user/aix.rb b/lib/chef/provider/user/aix.rb index 64a088dd5c..aac21bc87a 100644 --- a/lib/chef/provider/user/aix.rb +++ b/lib/chef/provider/user/aix.rb @@ -1,5 +1,5 @@ # -# Copyright:: Copyright 2012-2016, Chef Software Inc. +# Copyright:: Copyright 2012-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,26 +14,42 @@ # See the License for the specific language governing permissions and # limitations under the License. -require "chef/provider/user/useradd" +require "chef/provider/user" class Chef class Provider class User - class Aix < Chef::Provider::User::Useradd + class Aix < Chef::Provider::User provides :user, os: "aix" provides :aix_user UNIVERSAL_OPTIONS = [[:comment, "-c"], [:gid, "-g"], [:shell, "-s"], [:uid, "-u"]].freeze def create_user - super + command = compile_command("useradd") do |useradd| + useradd.concat(universal_options) + useradd.concat(useradd_options) + end + shell_out_compact!(command) add_password end def manage_user add_password manage_home - super + return if universal_options.empty? + command = compile_command("usermod") do |u| + u.concat(universal_options) + end + shell_out_compact!(command) + end + + def remove_user + command = [ "userdel" ] + command << "-r" if new_resource.manage_home + command << "-f" if new_resource.force + command << new_resource.username + shell_out_compact!(command) end # Aix does not support -r like other unix, sytem account is created by adding to 'system' group @@ -70,6 +86,51 @@ class Chef shell_out_compact!("chuser", "account_locked=false", new_resource.username) end + def compile_command(base_command) + base_command = Array(base_command) + yield base_command + base_command << new_resource.username + base_command + end + + def universal_options + @universal_options ||= + begin + opts = [] + # magic allows UNIVERSAL_OPTIONS to be overridden in a subclass + self.class::UNIVERSAL_OPTIONS.each do |field, option| + update_options(field, option, opts) + end + if updating_home? + opts << "-d" << new_resource.home + if new_resource.manage_home + logger.trace("#{new_resource} managing the users home directory") + opts << "-m" + else + logger.trace("#{new_resource} setting home to #{new_resource.home}") + end + end + opts << "-o" if new_resource.non_unique + opts + end + end + + def update_options(field, option, opts) + return unless current_resource.send(field).to_s != new_resource.send(field).to_s + return unless new_resource.send(field) + logger.trace("#{new_resource} setting #{field} to #{new_resource.send(field)}") + opts << option << new_resource.send(field).to_s + end + + def updating_home? + # will return false if paths are equivalent + # Pathname#cleanpath does a better job than ::File::expand_path (on both unix and windows) + # ::File.expand_path("///tmp") == ::File.expand_path("/tmp") => false + # ::File.expand_path("\\tmp") => "C:/tmp" + return true if current_resource.home.nil? && new_resource.home + new_resource.home && Pathname.new(current_resource.home).cleanpath != Pathname.new(new_resource.home).cleanpath + end + private def add_password diff --git a/lib/chef/provider/user/solaris.rb b/lib/chef/provider/user/solaris.rb index 59074d5ba8..011df0aa9f 100644 --- a/lib/chef/provider/user/solaris.rb +++ b/lib/chef/provider/user/solaris.rb @@ -2,7 +2,7 @@ # Author:: Stephen Nelson-Smith (<sns@chef.io>) # Author:: Jon Ramsey (<jonathon.ramsey@gmail.com>) # Author:: Dave Eddy (<dave@daveeddy.com>) -# Copyright:: Copyright 2012-2017, Chef Software Inc. +# Copyright:: Copyright 2012-2018, Chef Software Inc. # Copyright:: Copyright 2015-2016, Dave Eddy # License:: Apache License, Version 2.0 # @@ -18,12 +18,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -require "chef/provider/user/useradd" +require "chef/provider/user" class Chef class Provider class User - class Solaris < Chef::Provider::User::Useradd + class Solaris < Chef::Provider::User provides :solaris_user provides :user, os: %w{omnios solaris2} UNIVERSAL_OPTIONS = [[:comment, "-c"], [:gid, "-g"], [:shell, "-s"], [:uid, "-u"]].freeze @@ -36,13 +36,29 @@ class Chef end def create_user - super + command = compile_command("useradd") do |useradd| + useradd.concat(universal_options) + useradd.concat(useradd_options) + end + shell_out_compact!(command) manage_password end def manage_user manage_password - super + return if universal_options.empty? + command = compile_command("usermod") do |u| + u.concat(universal_options) + end + shell_out_compact!(command) + end + + def remove_user + command = [ "userdel" ] + command << "-r" if new_resource.manage_home + command << "-f" if new_resource.force + command << new_resource.username + shell_out_compact!(command) end def check_lock @@ -64,6 +80,51 @@ class Chef shell_out_compact!("passwd", "-u", new_resource.username) end + def compile_command(base_command) + base_command = Array(base_command) + yield base_command + base_command << new_resource.username + base_command + end + + def universal_options + @universal_options ||= + begin + opts = [] + # magic allows UNIVERSAL_OPTIONS to be overridden in a subclass + self.class::UNIVERSAL_OPTIONS.each do |field, option| + update_options(field, option, opts) + end + if updating_home? + opts << "-d" << new_resource.home + if new_resource.manage_home + logger.trace("#{new_resource} managing the users home directory") + opts << "-m" + else + logger.trace("#{new_resource} setting home to #{new_resource.home}") + end + end + opts << "-o" if new_resource.non_unique + opts + end + end + + def update_options(field, option, opts) + return unless current_resource.send(field).to_s != new_resource.send(field).to_s + return unless new_resource.send(field) + logger.trace("#{new_resource} setting #{field} to #{new_resource.send(field)}") + opts << option << new_resource.send(field).to_s + end + + def updating_home? + # will return false if paths are equivalent + # Pathname#cleanpath does a better job than ::File::expand_path (on both unix and windows) + # ::File.expand_path("///tmp") == ::File.expand_path("/tmp") => false + # ::File.expand_path("\\tmp") => "C:/tmp" + return true if current_resource.home.nil? && new_resource.home + new_resource.home && Pathname.new(current_resource.home).cleanpath != Pathname.new(new_resource.home).cleanpath + end + private # Override the version from {#Useradd} because Solaris doesn't support diff --git a/lib/chef/provider/user/useradd.rb b/lib/chef/provider/user/useradd.rb index 47c0ece101..c09cc0d3a5 100644 --- a/lib/chef/provider/user/useradd.rb +++ b/lib/chef/provider/user/useradd.rb @@ -1,6 +1,6 @@ # # Author:: Adam Jacob (<adam@chef.io>) -# Copyright:: Copyright 2008-2017, Chef Software Inc. +# Copyright:: Copyright 2008-2018, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,6 +23,9 @@ class Chef class Provider class User class Useradd < Chef::Provider::User + + Chef::Log.warn("the Chef::Provider::User::Useradd provider is deprecated, please subclass Chef::Provider::User directly") + # the linux version of this has been forked off, this is the base class now of solaris and AIX and should be abandoned # and those provider should be rewritten like the linux version. |