Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the position parameter work properly #4

Merged
merged 1 commit into from
May 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions lib/puppet/provider/pam/augeas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,37 @@ def self.target(resource = nil)
end
end

def self.position_path (position, type)
placement, identifier, value = position.split(/ /)
key = !!value
if PAM_POSITION_ALIASES[key].has_key? identifier
expr = PAM_POSITION_ALIASES[key][identifier]
expr = key ? expr % [type, value] : expr % [type]
else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might have liked to keep the comment that was in the original piece of code here.

# if the identifier is not in the mapping
# we assume that its an xpath and so
# join everything after the placement
expr = position.split(/ /)[1..-1].join(" ")
end
return expr, placement
end

def in_position?
unless resource[:position].nil?
path, before = self.class.position_path(resource[:position], resource[:type])

if before == 'before'
mpath = "#{resource_path}[following-sibling::#{path}]"
else
mpath = "#{resource_path}[preceding-sibling::#{path}]"
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

augopen could be called here to limit the critical section.

augopen do |aug|
!aug.match(mpath).empty?
end
end
end

def self.instances
augopen do |aug|
resources = []
Expand Down Expand Up @@ -78,19 +109,10 @@ def self.instances
type = resource[:type].to_s
control = resource[:control]
position = resource[:position]
placement, identifier, value = position.split(/ /)
key = !!value
if PAM_POSITION_ALIASES[key].has_key?(identifier)
expr = PAM_POSITION_ALIASES[key][identifier]
expr = key ? expr % [type, value] : expr % [type]
else
# if the identifier is not in the mapping
# we assume that its an xpath and so
# join everything after the placement
identifier = position.split(/ /)[1..-1].join(" ")
expr = identifier
unless position.nil?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you reuse self.position_path here maybe?

expr, placement = self.position_path(position, type)
aug.insert("$target/#{expr}", path, placement == 'before')
end
aug.insert("$target/#{expr}", path, placement == 'before')
if resource[:optional] == :true
aug.touch("#{entry_path}/optional")
end
Expand Down
21 changes: 21 additions & 0 deletions spec/unit/puppet/provider/pam/augeas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,27 @@
:arguments => ["try_first_pass","retry=3","type="],})
end

describe "when reodering settings" do
it "should change the order of an entry" do
apply!(Puppet::Type.type(:pam).new(
:title => "Change the order of pam_unix.so",
:service => "system-auth",
:type => "auth",
:control => "sufficient",
:module => "pam_unix.so",
:arguments => ["nullok","try_first_pass"],
:target => target,
:provider => "augeas",
:position => "before module pam_env.so",
:ensure => "positioned"
))

aug_open(target, "Pam.lns") do |aug|
expect(aug.get("./1/module")).to eq("pam_unix.so")
end
end
end

describe "when creating settings" do
it "should create simple new entry" do
apply!(Puppet::Type.type(:pam).new(
Expand Down