Class: Inspec::Resources::Mailalias

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

Instance Method Summary collapse

Constructor Details

#initialize(alias_key) ⇒ Mailalias

Returns a new instance of Mailalias.



19
20
21
22
# File 'lib/inspec/resources/mail_alias.rb', line 19

def initialize(alias_key)
  skip_resource "The `mail_alias` resource is not yet available on your OS." unless inspec.os.unix?
  @alias_key = alias_key
end

Instance Method Details

#aliased_to?(alias_value) ⇒ Boolean

aliased_to matcher checks if the given alias_value is set to the initialized alias_key

Returns:

  • (Boolean)

Raises:



35
36
37
38
39
40
41
42
43
44
# File 'lib/inspec/resources/mail_alias.rb', line 35

def aliased_to?(alias_value)
  # /etc/aliases if the file where the alias and its value(s) are stored
  cmd = inspec.command("cat /etc/aliases | grep '^#{@alias_key}:'")
  raise Inspec::Exceptions::ResourceFailed, "#{@alias_key} is not a valid key in the aliases" if cmd.exit_status.to_i != 0

  # in general aliases file contains : separated values like alias_key : alias_value1, alias_value2
  alias_values_combined = cmd.stdout.split(":").map(&:strip)[1]
  alias_values_splitted = alias_values_combined.split(",").map(&:strip)
  alias_values_splitted.include?(alias_value)
end

#resource_idObject

resource_id is used in reporting engines to uniquely identify the individual resource.



25
26
27
# File 'lib/inspec/resources/mail_alias.rb', line 25

def resource_id
  "#{@alias_key}"
end

#to_sObject

resource appearance in test reports.



30
31
32
# File 'lib/inspec/resources/mail_alias.rb', line 30

def to_s
  "mail_alias #{resource_id}"
end