Skip to content

Commit

Permalink
Remove regexpi logic, Augeas 1.0.0 was released 8 years ago (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphink committed May 29, 2020
1 parent 178d6aa commit affa070
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 82 deletions.
7 changes: 1 addition & 6 deletions lib/puppet/provider/ssh_config/augeas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@
resource_path do |resource|
base = self.base_path(resource)
key = resource[:key] ? resource[:key] : resource[:name]
if supported?(:regexpi)
"#{base}/*[label()=~regexp('#{key}', 'i')]"
else
debug "Warning: Augeas >= 1.0.0 is required for case-insensitive support in ssh_config resources"
"#{base}/#{key}"
end
"#{base}/*[label()=~regexp('#{key}', 'i')]"
end

def self.base_path(resource)
Expand Down
10 changes: 2 additions & 8 deletions lib/puppet/provider/sshd_config/augeas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@
resource_path do |resource|
base = self.base_path(resource)
key = resource[:key] ? resource[:key] : resource[:name]
if supported?(:regexpi)
"#{base}/*[label()=~regexp('#{key}', 'i')]"
else
debug "Warning: Augeas >= 1.0.0 is required for case-insensitive support in sshd_config resources"
"#{base}/#{key}"
end
"#{base}/*[label()=~regexp('#{key}', 'i')]"
end

def self.base_path(resource)
Expand Down Expand Up @@ -90,8 +85,7 @@ def self.set_value(aug, base, path, label, value)
aug.set("#{path}[last()]", v)
else
# Prefer to create the node next to a commented out entry
reg_flag = supported?(:regexpi) ? ", 'i'" : ''
commented = aug.match("#{base}/#comment[.=~regexp('#{label}([^a-z\.].*)?'#{reg_flag})]")
commented = aug.match("#{base}/#comment[.=~regexp('#{label}([^a-z\.].*)?', 'i')]")
if commented.empty?
if aug.match("#{base}/Match").empty?
# insert as the last line
Expand Down
24 changes: 5 additions & 19 deletions lib/puppet/provider/sshd_config_match/augeas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@

confine :feature => :augeas

def self.regexpi_path(resource)
path = "$target/*[label()=~regexp('match', 'i') and *[label()=~regexp('condition', 'i') and count(*)=#{resource[:condition].keys.size}]"
resource[:condition].each do |c, v|
path += "[*[label()=~regexp('#{c}', 'i')]='#{v}']"
end
path += "]"
end

def self.static_path(resource)
path = "$target/Match[count(Condition/*)=#{resource[:condition].keys.size}]"
resource[:condition].each do |c, v|
Expand All @@ -31,13 +23,11 @@ def self.static_path(resource)
end

def self.path(resource)
if supported?(:regexpi)
self.regexpi_path(resource)
else
debug "Warning: Augeas >= 1.0.0 is required for case-insensitive support in ssh_config resources"
# TODO: test this?
self.static_path(resource)
path = "$target/*[label()=~regexp('match', 'i') and *[label()=~regexp('condition', 'i') and count(*)=#{resource[:condition].keys.size}]"
resource[:condition].each do |c, v|
path += "[*[label()=~regexp('#{c}', 'i')]='#{v}']"
end
path += "]"
end

resource_path do |resource|
Expand All @@ -47,11 +37,7 @@ def self.path(resource)
def self.instances
augopen do |aug,path|
resources = []
if supported?(:regexpi)
search_path = "$target/*[label()=~regexp('match', 'i')]/*[label()=~regexp('condition', 'i')]"
else
search_path = "$target/Match/Condition"
end
search_path = "$target/*[label()=~regexp('match', 'i')]/*[label()=~regexp('condition', 'i')]"

aug.match("#{search_path}").each do |hpath|
conditions = []
Expand Down
12 changes: 2 additions & 10 deletions lib/puppet/provider/sshd_config_subsystem/augeas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
confine :feature => :augeas

resource_path do |resource|
if supported?(:regexpi)
"$target/*[label()=~regexp('Subsystem', 'i')]/#{resource[:name]}"
else
"$target/Subsystem/#{resource[:name]}"
end
"$target/*[label()=~regexp('Subsystem', 'i')]/#{resource[:name]}"
end

def self.instances
Expand All @@ -46,11 +42,7 @@ def self.instances

define_aug_method!(:destroy) do |aug, resource|
key = resource[:name]
if supported?(:regexpi)
aug.rm("$target/*[label()=~regexp('Subsystem', 'i') and #{key}]")
else
aug.rm("$target/Subsystem[#{key}]")
end
aug.rm("$target/*[label()=~regexp('Subsystem', 'i') and #{key}]")
end

attr_aug_accessor(:command, :label => :resource)
Expand Down
20 changes: 1 addition & 19 deletions spec/unit/puppet/provider/ssh_config/augeas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
end
end

it "should replace settings case insensitively when on Augeas >= 1.0.0", :if => provider_class.supported?(:regexpi) do
it "should replace settings case insensitively" do
apply!(Puppet::Type.type(:ssh_config).new(
:name => "GssaPiaUthentication",
:value => "yes",
Expand All @@ -248,24 +248,6 @@
expect(aug.get("Host[.='*']/GSSAPIAuthentication")).to eq("yes")
end
end

it "should not replace settings case insensitively when on Augeas < 1.0.0" do
provider_class.stubs(:supported?).with(:post_resource_eval)
provider_class.stubs(:supported?).with(:regexpi).returns(false)
apply!(Puppet::Type.type(:ssh_config).new(
:name => "GSSAPIDeLeGateCreDentials",
:value => "yes",
:target => target,
:provider => "augeas"
))

aug_open(target, "Ssh.lns") do |aug|
expect(aug.match("Host[.='*']/GSSAPIDelegateCredentials").size).to eq(1)
expect(aug.match("Host[.='*']/GSSAPIDeLeGateCreDentials").size).to eq(1)
expect(aug.get("Host[.='*']/GSSAPIDelegateCredentials")).to eq("no")
expect(aug.get("Host[.='*']/GSSAPIDeLeGateCreDentials")).to eq("yes")
end
end
end
end

Expand Down
22 changes: 2 additions & 20 deletions spec/unit/puppet/provider/sshd_config/augeas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
')
end

it "should add it next to commented out entry with different case when on Augeas >= 1.0.0", :if => provider_class.supported?(:regexpi) do
it "should add it next to commented out entry with different case" do
apply!(Puppet::Type.type(:sshd_config).new(
:name => "usedns",
:value => "no",
Expand Down Expand Up @@ -392,7 +392,7 @@
end
end

it "should replace settings case insensitively when on Augeas >= 1.0.0", :if => provider_class.supported?(:regexpi) do
it "should replace settings case insensitively" do
apply!(Puppet::Type.type(:sshd_config).new(
:name => "PaSswordaUtheNticAtion",
:value => "no",
Expand All @@ -406,24 +406,6 @@
end
end

it "should not replace settings case insensitively when on Augeas < 1.0.0" do
provider_class.stubs(:supported?).with(:post_resource_eval)
provider_class.stubs(:supported?).with(:regexpi).returns(false)
apply!(Puppet::Type.type(:sshd_config).new(
:name => "GSSAPIauthentIcAtion",
:value => "no",
:target => target,
:provider => "augeas"
))

aug_open(target, "Sshd.lns") do |aug|
expect(aug.match("GSSAPIAuthentication").size).to eq(1)
expect(aug.match("GSSAPIauthentIcAtion").size).to eq(1)
expect(aug.get("GSSAPIAuthentication")).to eq("yes")
expect(aug.get("GSSAPIauthentIcAtion")).to eq("no")
end
end

context "when using array_append" do
it "should not remove existing values" do
apply!(Puppet::Type.type(:sshd_config).new(
Expand Down

0 comments on commit affa070

Please sign in to comment.