class Ubiq::ConfigCredentials

Class to load a credentials file or the default and read the credentials from either a supplied profile or use the default

Public Class Methods

new(config_file, profile) click to toggle source
# File lib/ubiq/credentials.rb, line 32
def initialize(config_file, profile)
  # If config file is not found
  if !config_file.nil? && !File.exist?(config_file)
    raise RuntimeError, "Unable to open config file #{config_file} or contains missing values"
  end

  if config_file.nil?
    config_file = '~/.ubiq/credentials'
  end

  # If config file is found
  if File.exist?(File.expand_path(config_file))
    @creds = load_config_file(config_file, profile)
  end
end

Public Instance Methods

get_attributes() click to toggle source
# File lib/ubiq/credentials.rb, line 48
def get_attributes
  return @creds
end
load_config_file(file, profile) click to toggle source
# File lib/ubiq/credentials.rb, line 52
def load_config_file(file, profile)
  config = ConfigParser.new(File.expand_path(file))

  # Create empty dictionaries for the default and supplied profile
  p = {}
  d = {}

  # get the default profile if there is one
  if config['default'].present?
    d = config['default']
  end

  if !d.key?('SERVER')
    d['SERVER'] = Ubiq::UBIQ_HOST
  end

  # get the supplied profile if there is one
  if config[profile].present?
    p = config[profile]
  end

  # Use given profile if it is available, otherwise use default.
  access_key_id = p.key?('ACCESS_KEY_ID') ? p['ACCESS_KEY_ID'] : d['ACCESS_KEY_ID']
  secret_signing_key = p.key?('SECRET_SIGNING_KEY') ? p['SECRET_SIGNING_KEY'] : d['SECRET_SIGNING_KEY']
  secret_crypto_access_key = p.key?('SECRET_CRYPTO_ACCESS_KEY') ? p['SECRET_CRYPTO_ACCESS_KEY'] : d['SECRET_CRYPTO_ACCESS_KEY']
  host = p.key?('SERVER') ? p['SERVER'] : d['SERVER']

  # If the provided host does not contain http protocol then add to it
  if !host.include?('http://') && !host.include?('https://')
    host = 'https://' + host
  end

  return CredentialsInfo.new(access_key_id, secret_signing_key, secret_crypto_access_key, host).set_attributes
end