Class: Chef::Provisioning::GoogleDriver::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/provisioning/google_driver/credentials.rb

Overview

Access various forms of Google credentials TODO: load credentials from metadata server when provisioning from a GCE machine

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCredentials

Returns a new instance of Credentials.



8
9
10
# File 'lib/chef/provisioning/google_driver/credentials.rb', line 8

def initialize
  @credentials = {}
end

Class Method Details

.from_hash(h) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/chef/provisioning/google_driver/credentials.rb', line 20

def self.from_hash(h)
  credentials = self.new
  h.each do |k, v|
    credentials[k] = v
  end
  credentials.validate!
  credentials
end

Instance Method Details

#[](name) ⇒ Object



12
13
14
# File 'lib/chef/provisioning/google_driver/credentials.rb', line 12

def [](name)
  @credentials[name]
end

#[]=(name, value) ⇒ Object



16
17
18
# File 'lib/chef/provisioning/google_driver/credentials.rb', line 16

def []=(name, value)
  @credentials[name] = value
end

#load_defaultsObject

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/chef/provisioning/google_driver/credentials.rb', line 56

def load_defaults
  raise NotImplementedError
end

#validate!Object

Validates whether the key settings are present in the credential object and keys are in the correct format. If no client_email is specified, method will try to load the client_email from the json key.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef/provisioning/google_driver/credentials.rb', line 31

def validate!
  unless self[:p12_key_path] || self[:json_key_path]
    raise "Google key path is missing. Options provided: #{self.inspect}"
  end
  if self[:json_key_path]
    json_key_hash = JSON.load(File.open(self[:json_key_path]))

    unless self[:google_client_email]
      # Try to load client_email from json if not present
      if json_key_hash["client_email"]
        self[:google_client_email] = json_key_hash["client_email"]
      else
        raise "google_client_email must be specified"
      end
    end

    raise "Invalid Google JSON key, no private key" unless json_key_hash.include?("private_key")
  elsif self[:p12_key_path]
    raise "p12 key doesn't exist in the path specified" unless File.exist?(self[:p12_key_path])
    raise "google_client_email must be specified" unless self[:google_client_email]
  else
    raise "json_key_path or p12_key_path is missing. Options provided: #{self}"
  end
end