class Occi::Api::Client::Http::AuthnPlugins::Keystone

Constants

KEYSTONE_URI_REGEXP
KEYSTONE_VERSION_REGEXP

Public Instance Methods

authenticate(options = {}) click to toggle source
# File lib/occi/api/client/http/authn_plugins/keystone.rb, line 22
def authenticate(options = {})
  # OCCI-OS doesn't support HEAD method!
  response = @env_ref.class.get "#{@env_ref.endpoint.to_s}/-/"
  raise ::Occi::Api::Client::Errors::AuthnError,
        "Authentication failed with code #{response.code.to_s}!" unless response.success?
end
setup(options = {}) click to toggle source
# File lib/occi/api/client/http/authn_plugins/keystone.rb, line 10
def setup(options = {})
  # get Keystone URL if possible
  set_keystone_base_url

  # discover Keystone API version
  @env_ref.class.headers.delete 'X-Auth-Token'
  set_auth_token ENV['ROCCI_CLIENT_KEYSTONE_TENANT']

  raise ::Occi::Api::Client::Errors::AuthnError,
        "Unable to get a tenant from Keystone, fallback failed!" if @env_ref.class.headers['X-Auth-Token'].blank?
end

Private Instance Methods

process_headers(response) click to toggle source
# File lib/occi/api/client/http/authn_plugins/keystone.rb, line 42
def process_headers(response)
  authN_header = response.headers['www-authenticate']

  if authN_header.blank?
    raise ::Occi::Api::Client::Errors::AuthnError,
          "Response does not contain the www-authenticate header, fallback failed!"
  end

  match = KEYSTONE_URI_REGEXP.match(authN_header)
  raise ::Occi::Api::Client::Errors::AuthnError,
        "Unable to get Keystone's URL from the response, fallback failed!" unless match && match[3]

  @keystone_url = match[3]
end
set_auth_token(tenant = nil) click to toggle source
# File lib/occi/api/client/http/authn_plugins/keystone.rb, line 57
def set_auth_token(tenant = nil)
  response = @env_ref.class.get @keystone_url
  Occi::Api::Log.debug response.inspect

  raise ::Occi::Api::Client::Errors::AuthnError,
        "Unable to get Keystone API version from the response, fallback failed!" if (400..599).include?(response.code)

  # multiple choices, sort them by version id
  if response.code == 300
    versions = response['versions']['values'].sort_by { |v| v['id']}
  else
    # assume a single version
    versions = [response['version']]
  end

  versions.each do |v|
    match = KEYSTONE_VERSION_REGEXP.match(v['id'])
    raise ::Occi::Api::Client::Errors::AuthnError,
          "Unable to get Keystone API version from the response, fallback failed!" unless match && match[1]
    if match[1] == '2'
      handler_class = KeystoneV2
    elsif match[1] == '3'
      handler_class = KeystoneV3
    end
    v['links'].each do |link|
      begin
        if link['rel'] == 'self'
         keystone_url = link['href'].chomp('/')
         keystone_handler = handler_class.new(keystone_url, @env_ref, @options)
         token = keystone_handler.set_auth_token(tenant)
         # found a working keystone, stop looking
         return
        end
      rescue ::Occi::Api::Client::Errors::AuthnError
        # ignore and try with next link
      end
    end
  end
end
set_keystone_base_url() click to toggle source
# File lib/occi/api/client/http/authn_plugins/keystone.rb, line 31
def set_keystone_base_url
  response = @env_ref.class.get "#{@env_ref.endpoint.to_s}/-/"
  Occi::Api::Log.debug response.inspect

  return if response.success?
  raise ::Occi::Api::Client::Errors::AuthnError,
        "Keystone AuthN failed with #{response.code.to_s}!" unless response.code == 401

  process_headers(response)
end