Skip to content

Commit

Permalink
Added self.position_path and in_position? methods so that :position p…
Browse files Browse the repository at this point in the history
…arameter would work.
  • Loading branch information
mmarod authored and Michael Marod committed May 12, 2015
1 parent d170b5e commit 50667f4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
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
# 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

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?
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

0 comments on commit 50667f4

Please sign in to comment.