Class: Conjur::DSL::Runner

Inherits:
Object
  • Object
show all
Includes:
IdentifierManipulation
Defined in:
lib/conjur/dsl/runner.rb

Overview

Entry point for the Conjur DSL.

Methods are available in two categories: name scoping and asset building.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IdentifierManipulation

#conjur_account, #full_resource_id, #get_kind_and_id_from_args

Constructor Details

#initialize(script, filename = nil) ⇒ Runner

Returns a new instance of Runner.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/conjur/dsl/runner.rb', line 13

def initialize(script, filename = nil)
  @context = {
    "env" => Conjur.env,
    "stack" => Conjur.stack,
    "account" => Conjur.,
    "api_keys" => {}
  }
  @script = script
  @filename = filename
  @api = nil
  @scopes = Array.new
  @owners = Array.new
  @objects = Array.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (protected)



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/conjur/dsl/runner.rb', line 154

def method_missing(sym, *args, &block)
  if create_compatible_args?(args) && api.respond_to?(sym)
    id = args[0]
    id = qualify_id(id, sym)
    find_or_create sym, id, args[1] || {}, &block
  elsif current_object && current_object.respond_to?(sym)
    current_object.send(sym, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#context ⇒ Object

Returns the value of attribute context.



11
12
13
# File 'lib/conjur/dsl/runner.rb', line 11

def context
  @context
end

#filename ⇒ Object (readonly)

Returns the value of attribute filename.



11
12
13
# File 'lib/conjur/dsl/runner.rb', line 11

def filename
  @filename
end

#script ⇒ Object (readonly)

Returns the value of attribute script.



11
12
13
# File 'lib/conjur/dsl/runner.rb', line 11

def script
  @script
end

Instance Method Details

#api ⇒ Object



39
40
41
# File 'lib/conjur/dsl/runner.rb', line 39

def api
  @api ||= connect
end

#api_keys ⇒ Object



47
48
49
# File 'lib/conjur/dsl/runner.rb', line 47

def api_keys
  @context["api_keys"]
end

#assets ⇒ Object

Provides a hash to export various application specific asset ids (or anything else you want)



35
36
37
# File 'lib/conjur/dsl/runner.rb', line 35

def assets
  @context['assets'] ||= {}
end

#create_variable(id = nil, options = {}, &block) ⇒ Object

purpose and existence of this method are unobvious for model designer just "variable" in DSL works fine through method_missing is this method OBSOLETED ? https://basecamp.com/1949725/projects/4268938-api-version-4-x/todos/84972543-low-variable



122
123
124
125
126
127
128
# File 'lib/conjur/dsl/runner.rb', line 122

def create_variable id = nil, options = {}, &block
  options[:id] = id if id
  mime_type = options.delete(:mime_type) || 'text/plain'
  kind = options.delete(:kind) || 'secret'
  var = api.create_variable(mime_type, kind, options)
  do_object var, &block
end

#current_object ⇒ Object



51
52
53
# File 'lib/conjur/dsl/runner.rb', line 51

def current_object
  !@objects.empty? ? @objects.last : nil
end

#current_scope ⇒ Object

Current scope, used as a path/delimited/prefix to a role or resource id.



56
57
58
# File 'lib/conjur/dsl/runner.rb', line 56

def current_scope
  !@scopes.empty? ? @scopes.join('/') : nil
end

#current_user_scope ⇒ Object

Current scope, used for user@scope.



61
62
63
# File 'lib/conjur/dsl/runner.rb', line 61

def current_user_scope
  current_scope ? current_scope.gsub(/[^\w]/, '-') : nil
end

#execute ⇒ Object



102
103
104
105
106
# File 'lib/conjur/dsl/runner.rb', line 102

def execute
  args = [ script ]
  args << filename if filename
  instance_eval(*args)
end

#namespace(ns = nil, &block) ⇒ Object Also known as: model



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/conjur/dsl/runner.rb', line 73

def namespace ns = nil, &block
  if block_given?
    ns ||= context["namespace"]
    if ns.nil?
      require 'conjur/api/variables'
      ns = context["namespace"] = api.create_variable("text/plain", "namespace").id
    end
    do_scope ns, &block
    context
  else
    @scopes[0]
  end
end

#owner=(owner) ⇒ Object



28
29
30
31
# File 'lib/conjur/dsl/runner.rb', line 28

def owner=(owner)
  raise "Owner should only be set once" unless @owners.empty?
  @owners.push owner
end

#owns ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/conjur/dsl/runner.rb', line 130

def owns
  @owners.push current_object
  begin
    yield
  ensure
    @owners.pop
  end
end

#policy(id, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/conjur/dsl/runner.rb', line 87

def policy id, &block
  self.role "policy", id do |role|
    context["policy"] = role.identifier
    self.owns do
      self.resource "policy", id do
        scope id do
          block.call if block_given?
        end
      end
    end
  end
end

#resource(kind, id, options = {}, &block) ⇒ Object



108
109
110
111
# File 'lib/conjur/dsl/runner.rb', line 108

def resource kind, id, options = {}, &block
  id = full_resource_id([kind, qualify_id(id, kind) ].join(':'))
  find_or_create :resource, id, options, &block
end

#role(kind, id, options = {}, &block) ⇒ Object



113
114
115
116
# File 'lib/conjur/dsl/runner.rb', line 113

def role kind, id, options = {}, &block
  id = full_resource_id([ kind, qualify_id(id, kind) ].join(':'))
  find_or_create :role, id, options, &block
end

#scope(name = nil, &block) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/conjur/dsl/runner.rb', line 65

def scope name = nil, &block
  if name != nil
    do_scope name, &block
  else
    current_scope
  end
end