Skip to content

Commit

Permalink
iptables: some simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
srenatus committed Feb 9, 2016
1 parent 401d559 commit 0be123f
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions lib/resources/iptables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class IpTables < Inspec.resource(1)
"

def initialize(params = {})
@table = params[:table] || nil
@chain = params[:chain] || nil
@table = params[:table]
@chain = params[:chain]

# we're done if we are on linux
return if inspec.os.linux?
Expand All @@ -43,29 +43,26 @@ def initialize(params = {})
end

def has_rule?(rule = nil, _table = nil, _chain = nil)
found = false
retrieve_rules.each { |line|
# checks if the rule is part of the ruleset
# for now, we expect an excact match
found = true if line.casecmp(rule) == 0
}
found
# checks if the rule is part of the ruleset
# for now, we expect an exact match
retrieve_rules.any? { |line| line.casecmp(rule) == 0 }
end

def retrieve_rules
return @iptables_cache if defined?(@iptables_cache)
return @iptables_cache if @iptables_cache

# construct iptables command to read all rules
@table.nil? ? table_cmd = '' : table_cmd = " -t #{@table} "
@chain.nil? ? chain_cmd = '' : chain_cmd = " #{@chain}"
cmd = inspec.command(format('iptables %s -S %s', table_cmd, chain_cmd).strip)
table_cmd = "-t #{@table}" if @table
iptables_cmd = format('iptables %s -S %s', table_cmd, @chain).strip

cmd = inspec.command(iptables_cmd)
return [] if cmd.exit_status.to_i != 0

# split rules, returns array or rules
@iptables_cache = cmd.stdout.chomp.split("\n")
@iptables_cache = cmd.stdout.chomp.split('\n')
end

def to_s
format('Iptables %s %s', @table.nil? ? '' : "table: #{@table}", @chain.nil? ? '' : "chain: #{@chain}").strip
format('Iptables %s %s', @table && "table: #{@table}", @chain && "chain: #{@chain}").strip
end
end

0 comments on commit 0be123f

Please sign in to comment.