Class: Conjur::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/conjur/conjurenv.rb

Defined Under Namespace

Classes: ConjurTempfile, ConjurVariable, CustomTag

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Env

Returns a new instance of Env.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/conjur/conjurenv.rb', line 63

def initialize(options={})
  raise ":file and :yaml options can not be provided together" if ( options.has_key?(:file) and options.has_key?(:yaml) )

  yaml = if options.has_key?(:yaml) 
    raise ":yaml option should be non-empty string" unless options[:yaml].kind_of?(String)
    raise ":yaml option should be non-empty string" if options[:yaml].empty?
    options[:yaml]
  elsif options.has_key?(:file)
    raise ":file option should be non-empty string" unless options[:file].kind_of?(String)
    raise ":file option should be non-empty string" if options[:file].empty?
    File.read(options[:file])
  else
    raise "either :file or :yaml option is mandatory"
  end
  parse_arguments = [ yaml ]
  parse_arguments << options[:substitutions] if options[:substitutions]
  @definition = parse(*parse_arguments)
end

Instance Method Details

#check(api) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/conjur/conjurenv.rb', line 116

def check(api)
  Hash[ 
    @definition.map { |k,v| 

      status = if v.respond_to? :conjur_id
                 if api.resource("variable:"+v.conjur_id).permitted?(:execute)
                   :available   
                 else
                   :unavailable
                 end
               else
                 :literal
               end
 
      [ k, status ]
    }
  ]
end

#obtain(api) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/conjur/conjurenv.rb', line 102

def obtain(api)
  runtime_environment={}
  variable_ids= @definition.values.map { |v| v.conjur_id rescue nil }.compact
  conjur_values=api.variable_values(variable_ids)
  @definition.each { |environment_name, reference| 
    runtime_environment[environment_name]= if reference.respond_to?(:evaluate)
                                              reference.evaluate( conjur_values[reference.conjur_id] )
                                           else
                                              reference # is a literal value
                                           end
  }
  return runtime_environment
end

#parse(yaml, substitutions = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/conjur/conjurenv.rb', line 82

def parse(yaml, substitutions = {})
  YAML.add_tag("!var", ConjurVariable)
  YAML.add_tag("!tmp", ConjurTempfile)
  definition = YAML.load(yaml)
  raise "Definition should be a Hash" unless definition.kind_of?(Hash)
  # convert fixnums to literals -- to make definitions of e.g. ports more convenient
  definition.keys.select { |k| definition[k].kind_of? Fixnum }.each { |k| definition[k]="#{definition[k]}" }
  bad_types = definition.values.select { |v| not (v.kind_of?(String) or v.kind_of?(CustomTag)) }.map {|v| v.class}.uniq
  raise "Definition can not include values of types: #{bad_types}" unless bad_types.empty?
  definition.inject({}) do |memo,e| 
    key, value = e
    substitutions.each do |k,v|
      value.gsub! k, v
    end
    memo[key] = value
    memo
  end
  definition
end