Class: Inspec::Resources::OpaCli

Inherits:
Opa show all
Defined in:
lib/inspec/resources/opa_cli.rb

Instance Attribute Summary

Attributes inherited from JsonConfig

#params, #raw_content

Instance Method Summary collapse

Methods inherited from Opa

#result

Methods inherited from JsonConfig

#method_missing, #value

Methods included from FileReader

#read_file_content

Methods included from ObjectTraverser

#extract_value

Constructor Details

#initialize(opts = {}) ⇒ OpaCli

Returns a new instance of OpaCli.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/inspec/resources/opa_cli.rb', line 15

def initialize(opts = {})
  @opa_executable_path = opts[:opa_executable_path] || "opa" # if this path is not provided then we will assume that it's been set in the ENV PATH
  @policy = opts[:policy] || nil
  @data = opts[:data] || nil
  @query = opts[:query] || nil
  if (@policy.nil? || @policy.empty?) || (@data.nil? || @data.empty?) || (@query.nil? || @query.empty?)
    fail_resource "OPA policy, data and query are mandatory."
  end
  @content = load_result
  super(@content)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Inspec::Resources::JsonConfig

Instance Method Details

#allowObject



27
28
29
# File 'lib/inspec/resources/opa_cli.rb', line 27

def allow
  @content["result"][0]["expressions"][0]["value"] if @content["result"][0]["expressions"][0]["text"].include?("allow")
end

#resource_idObject



31
32
33
34
35
36
37
# File 'lib/inspec/resources/opa_cli.rb', line 31

def resource_id
  if @policy.nil? && @query.nil?
    "opa_cli"
  else
    "#{@policy}:#{@query}"
  end
end

#to_sObject



39
40
41
# File 'lib/inspec/resources/opa_cli.rb', line 39

def to_s
  "OPA cli"
end