Skip to content

Commit 2a7b3ad

Browse files
Merge pull request #8840 from joshcooper/backport_user_ssh_11380
(PUP-11380) Backport user type changes from main
2 parents 1675a6c + ce9a6fd commit 2a7b3ad

File tree

5 files changed

+56
-211
lines changed

5 files changed

+56
-211
lines changed

acceptance/tests/resource/user/should_correctly_ensure_depending_resources.rb

Lines changed: 0 additions & 43 deletions
This file was deleted.

acceptance/tests/resource/user/should_manage_purge_ssh_keys_for_new_user.rb

Lines changed: 0 additions & 86 deletions
This file was deleted.

lib/puppet/type/user.rb

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ def delimiter
692692
defaultto false
693693
end
694694

695-
def eval_generate
695+
def generate
696696
if !self[:purge_ssh_keys].empty?
697697
if Puppet::Type.type(:ssh_authorized_key).nil?
698698
warning _("Ssh_authorized_key type is not available. Cannot purge SSH keys.")
@@ -742,6 +742,45 @@ def eval_generate
742742
end
743743
raise ArgumentError, _("purge_ssh_keys must be true, false, or an array of file names, not %{value}") % { value: value.inspect }
744744
end
745+
746+
munge do |value|
747+
# Resolve string, boolean and symbol forms of true and false to a
748+
# single representation.
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
762+
763+
private
764+
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
783+
end
745784
end
746785

747786
newproperty(:loginclass, :required_features => :manages_loginclass) do
@@ -763,7 +802,7 @@ def eval_generate
763802
# @see generate
764803
# @api private
765804
def find_unmanaged_keys
766-
munged_unmanaged_keys.
805+
self[:purge_ssh_keys].
767806
select { |f| File.readable?(f) }.
768807
map { |f| unknown_keys_in_file(f) }.
769808
flatten.each do |res|
@@ -775,41 +814,6 @@ def find_unmanaged_keys
775814
end
776815
end
777816

778-
def munged_unmanaged_keys
779-
value = self[:purge_ssh_keys]
780-
781-
# Resolve string, boolean and symbol forms of true and false to a
782-
# single representation.
783-
test_sym = value.to_s.intern
784-
value = test_sym if [:true, :false].include? test_sym
785-
786-
return [] if value == :false
787-
788-
home = self[:home]
789-
begin
790-
home ||= provider.home
791-
rescue
792-
Puppet.debug("User '#{self[:name]}' does not exist")
793-
end
794-
795-
if home.to_s.empty? || !Dir.exist?(home.to_s)
796-
if value == :true || [ value ].flatten.any? { |v| v.start_with?('~/', '%h/') }
797-
Puppet.debug("User '#{self[:name]}' has no home directory set to purge ssh keys from.")
798-
return []
799-
end
800-
end
801-
802-
return [ "#{home}/.ssh/authorized_keys" ] if value == :true
803-
804-
# value is an array - munge each value
805-
[ value ].flatten.map do |entry|
806-
# make sure frozen value is duplicated by using a gsub, second mutating gsub! is then ok
807-
entry = entry.gsub(/^~\//, "#{home}/")
808-
entry.gsub!(/^%h\//, "#{home}/")
809-
entry
810-
end
811-
end
812-
813817
# Parse an ssh authorized keys file superficially, extract the comments
814818
# on the keys. These are considered names of possible ssh_authorized_keys
815819
# resources. Keys that are managed by the present catalog are ignored.

spec/unit/transaction/additional_resource_generator_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,21 @@ class container {
479479
"Notify[goodbye]"))
480480
end
481481

482+
it "sets resources_failed_to_generate to true if resource#generate raises an exception" do
483+
catalog = compile_to_ral(<<-MANIFEST)
484+
user { 'foo':
485+
ensure => present,
486+
}
487+
MANIFEST
488+
489+
allow(catalog.resource("User[foo]")).to receive(:generate).and_raise(RuntimeError)
490+
relationship_graph = relationship_graph_for(catalog)
491+
generator = Puppet::Transaction::AdditionalResourceGenerator.new(catalog, relationship_graph, prioritizer)
492+
generator.generate_additional_resources(catalog.resource("User[foo]"))
493+
494+
expect(generator.resources_failed_to_generate).to be_truthy
495+
end
496+
482497
def relationships_after_generating(manifest, resource_to_generate)
483498
catalog = compile_to_ral(manifest)
484499
generate_resources_in(catalog, nil, resource_to_generate)

spec/unit/type/user_spec.rb

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -174,51 +174,6 @@ def self.instances; []; end
174174
end
175175
end
176176

177-
describe "when managing the purge_ssh_keys property" do
178-
context "with valid input" do
179-
it "should support a :true value" do
180-
expect { described_class.new(:name => 'foo', :purge_ssh_keys => :true) }.to_not raise_error
181-
end
182-
183-
it "should support a :false value" do
184-
expect { described_class.new(:name => 'foo', :purge_ssh_keys => :false) }.to_not raise_error
185-
end
186-
187-
it "should support a String value" do
188-
expect { described_class.new(:name => 'foo', :purge_ssh_keys => File.expand_path('home/foo/.ssh/authorized_keys')) }.to_not raise_error
189-
end
190-
191-
it "should support an Array value" do
192-
expect { described_class.new(:name => 'foo', :purge_ssh_keys => [File.expand_path('home/foo/.ssh/authorized_keys'),
193-
File.expand_path('custom/authorized_keys')]) }.to_not raise_error
194-
end
195-
end
196-
197-
context "with faulty input" do
198-
it "should raise error for relative path" do
199-
expect { described_class.new(:name => 'foo', :purge_ssh_keys => 'home/foo/.ssh/authorized_keys') }.to raise_error(Puppet::ResourceError,
200-
/Paths to keyfiles must be absolute/ )
201-
end
202-
203-
it "should raise error for invalid type" do
204-
expect { described_class.new(:name => 'foo', :purge_ssh_keys => :invalid) }.to raise_error(Puppet::ResourceError,
205-
/purge_ssh_keys must be true, false, or an array of file names/ )
206-
end
207-
208-
it "should raise error for array with relative path" do
209-
expect { described_class.new(:name => 'foo', :purge_ssh_keys => ['home/foo/.ssh/authorized_keys',
210-
File.expand_path('custom/authorized_keys')]) }.to raise_error(Puppet::ResourceError,
211-
/Paths to keyfiles must be absolute/ )
212-
end
213-
214-
it "should raise error for array with invalid type" do
215-
expect { described_class.new(:name => 'foo', :purge_ssh_keys => [:invalid,
216-
File.expand_path('custom/authorized_keys')]) }.to raise_error(Puppet::ResourceError,
217-
/Each entry for purge_ssh_keys must be a string/ )
218-
end
219-
end
220-
end
221-
222177
describe "when managing the uid property" do
223178
it "should convert number-looking strings into actual numbers" do
224179
expect(described_class.new(:name => 'foo', :uid => '50')[:uid]).to eq(50)

0 commit comments

Comments
 (0)