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)



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/conjur/dsl/runner.rb', line 149

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

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#scriptObject (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

#apiObject



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

def api
  @api ||= connect
end

#api_keysObject



42
43
44
# File 'lib/conjur/dsl/runner.rb', line 42

def api_keys
  @context["api_keys"]
end

#assetsObject

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



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

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


117
118
119
120
121
122
123
# File 'lib/conjur/dsl/runner.rb', line 117

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_objectObject



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

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

#current_scopeObject

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



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

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

#current_user_scopeObject

Current scope, used for user@scope.



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

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

#executeObject



97
98
99
100
101
# File 'lib/conjur/dsl/runner.rb', line 97

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

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



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/conjur/dsl/runner.rb', line 68

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

#ownsObject



125
126
127
128
129
130
131
132
# File 'lib/conjur/dsl/runner.rb', line 125

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

#policy(id, &block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/conjur/dsl/runner.rb', line 82

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



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

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



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

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



60
61
62
63
64
65
66
# File 'lib/conjur/dsl/runner.rb', line 60

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