Skip to content

Commit ce9a6fd

Browse files
committed
(PUP-11380) Ignore authorized key purging if home doesn't exist yet
It wasn't possible to create a user and specifiy "purge_ssh_keys => true" or an explicit path because the user type relies on "generate" to create "ssh_authorized_keys" resources with "ensure" set to "absent". Ideally we would use "eval_generate" to ensure the user is created and its home directory exists before resolving the path to authorized keys. However, that's blocked on PUP-2718. This commit instead ignores the purge logic if the user's home dir doesn't exist and "purge_ssh_keys" depends on the home dir to resolve the authorized_key path. That happens in the case of purge_ssh_keys=true or set to a string or array, where the entry starts with ~/ or %h/. (cherry picked from commit e88f896)
1 parent 709ab7c commit ce9a6fd

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

lib/puppet/type/user.rb

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -746,20 +746,40 @@ def generate
746746
munge do |value|
747747
# Resolve string, boolean and symbol forms of true and false to a
748748
# single representation.
749-
test_sym = value.to_s.intern
750-
value = test_sym if [:true, :false].include? test_sym
749+
case value
750+
when :false, false, "false"
751+
[]
752+
when :true, true, "true"
753+
home = homedir
754+
home ? [ "#{home}/.ssh/authorized_keys" ] : []
755+
else
756+
# value can be a string or array - munge each value
757+
[ value ].flatten.map do |entry|
758+
authorized_keys_path(entry)
759+
end.compact
760+
end
761+
end
751762

752-
return [] if value == :false
753-
home = resource[:home] || Dir.home(resource[:name])
763+
private
754764

755-
return [ "#{home}/.ssh/authorized_keys" ] if value == :true
756-
# value is an array - munge each value
757-
[ value ].flatten.map do |entry|
758-
# make sure frozen value is duplicated by using a gsub, second mutating gsub! is then ok
759-
entry = entry.gsub(/^~\//, "#{home}/")
760-
entry.gsub!(/^%h\//, "#{home}/")
761-
entry
762-
end
765+
def homedir
766+
resource[:home] || Dir.home(resource[:name])
767+
rescue ArgumentError
768+
Puppet.debug("User '#{resource[:name]}' does not exist")
769+
nil
770+
end
771+
772+
def authorized_keys_path(entry)
773+
return entry unless entry.match?(%r{^(?:~|%h)/})
774+
775+
# if user doesn't exist (yet), ignore nonexistent homedir
776+
home = homedir
777+
return nil unless home
778+
779+
# compiler freezes "value" so duplicate using a gsub, second mutating gsub! is then ok
780+
entry = entry.gsub(%r{^~/}, "#{home}/")
781+
entry.gsub!(%r{^%h/}, "#{home}/")
782+
entry
763783
end
764784
end
765785

0 commit comments

Comments
 (0)