class Occi::Api::Client::Http::AuthnPlugins::KeystoneV2

Public Class Methods

new(base_url, env_ref, options = {}) click to toggle source
# File lib/occi/api/client/http/authn_plugins/keystone.rb, line 99
def initialize(base_url, env_ref, options = {})
  @base_url = base_url
  @env_ref = env_ref
  @options = options
end

Public Instance Methods

authenticate(tenant = nil) click to toggle source
# File lib/occi/api/client/http/authn_plugins/keystone.rb, line 117
def authenticate(tenant = nil)
  response = @env_ref.class.post(
    "#{@base_url}/tokens",
    :body => get_keystone_req(tenant),
    :headers => get_req_headers
  )
  Occi::Api::Log.debug response.inspect

  if response.success?
    @env_ref.class.headers['X-Auth-Token'] = response['access']['token']['id']
  else
    raise ::Occi::Api::Client::Errors::AuthnError,
          "Unable to get a token from Keystone, fallback failed!"
  end
end
get_first_working_tenant() click to toggle source
# File lib/occi/api/client/http/authn_plugins/keystone.rb, line 155
def get_first_working_tenant
  response = @env_ref.class.get(
    "#{@base_url}/tenants",
    :headers => get_req_headers
  )
  Occi::Api::Log.debug response.inspect

  raise ::Occi::Api::Client::Errors::AuthnError,
        "Keystone didn't return any tenants, fallback failed!" if response['tenants'].blank?

  response['tenants'].each do |tenant|
    begin
      Occi::Api::Log.debug "Authenticating for tenant #{tenant['name'].inspect}"
      authenticate(tenant['name'])

      # found a working tenant, stop looking
      break
    rescue ::Occi::Api::Client::Errors::AuthnError
      # ignoring and trying the next tenant
    end
  end
end
get_keystone_req(tenant = nil) click to toggle source
# File lib/occi/api/client/http/authn_plugins/keystone.rb, line 133
def get_keystone_req(tenant = nil)
  if @options[:original_type] == "x509"
    body = { "auth" => { "voms" => true } }
  elsif @options[:username] && @options[:password]
    body = {
      "auth" => {
        "passwordCredentials" => {
          "username" => @options[:username],
          "password" => @options[:password]
        }
      }
    }
  else
    raise ::Occi::Api::Client::Errors::AuthnError,
          "Unable to request a token from Keystone! Chosen "                    "AuthN is not supported, fallback failed!"
  end

  body['auth']['tenantName'] = tenant unless tenant.blank?
  body.to_json
end
get_req_headers() click to toggle source
# File lib/occi/api/client/http/authn_plugins/keystone.rb, line 178
def get_req_headers
  headers = @env_ref.class.headers.clone
  headers['Content-Type'] = "application/json"
  headers['Accept'] = headers['Content-Type']

  headers
end
set_auth_token(tenant = nil) click to toggle source
# File lib/occi/api/client/http/authn_plugins/keystone.rb, line 105
def set_auth_token(tenant = nil)
  if !tenant.blank?
    # get a scoped token for the specified tenant directly
    authenticate ENV['ROCCI_CLIENT_KEYSTONE_TENANT']
  else
    # get an unscoped token, use the unscoped token
    # for tenant discovery and get a scoped token
    authenticate
    get_first_working_tenant
  end
end