Class: Inspec::Resources::Ip6Tables

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/resources/ip6tables.rb

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Ip6Tables

Returns a new instance of Ip6Tables.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/inspec/resources/ip6tables.rb', line 32

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

  # we're done if we are on linux
  return if inspec.os.linux?

  # ensures, all calls are aborted for non-supported os
  @ip6tables_cache = []
  skip_resource "The `ip6tables` resource is not supported on your OS yet."
end

Instance Method Details

#has_rule?(rule = nil, _table = nil, _chain = nil) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/inspec/resources/ip6tables.rb', line 44

def has_rule?(rule = nil, _table = nil, _chain = nil)
  # 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

#retrieve_rulesObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/inspec/resources/ip6tables.rb', line 50

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

  # construct ip6tables command to read all rules
  bin = find_ip6tables_or_error
  table_cmd = "-t #{@table}" if @table
  ip6tables_cmd = format("%s %s -S %s", bin, table_cmd, @chain).strip

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

  # split rules, returns array or rules
  @ip6tables_cache = cmd.stdout.split("\n").map(&:strip)
end

#to_sObject



65
66
67
# File 'lib/inspec/resources/ip6tables.rb', line 65

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