Skip to content

Commit

Permalink
Merge pull request #1939 from rackspace/reauth
Browse files Browse the repository at this point in the history
[rackspace] Reauthenticate and retry request when authentication token expires
  • Loading branch information
Kyle Rames committed Jul 8, 2013
2 parents a5d39f7 + 158675d commit b039a25
Show file tree
Hide file tree
Showing 24 changed files with 382 additions and 272 deletions.
5 changes: 5 additions & 0 deletions lib/fog/rackspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ def self.authenticate(options, connection_options = {})
end
end

def self.json_response?(response)
return false unless response && response.headers
response.headers['Content-Type'] =~ %r{application/json}i ? true : false
end

def self.normalize_url(endpoint)
return nil unless endpoint
str = endpoint.chomp " "
Expand Down
41 changes: 13 additions & 28 deletions lib/fog/rackspace/block_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,40 +87,25 @@ def initialize(options = {})
@connection = Fog::Connection.new(endpoint_uri.to_s, @persistent, @connection_options)
end

def request(params)
begin
response = @connection.request(params.merge!({
:headers => {
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-Auth-Token' => auth_token
}.merge!(params[:headers] || {}),
:host => endpoint_uri.host,
:path => "#{endpoint_uri.path}/#{params[:path]}"
}))
rescue Excon::Errors::NotFound => error
raise NotFound.slurp(error, region)
rescue Excon::Errors::BadRequest => error
raise BadRequest.slurp error
rescue Excon::Errors::InternalServerError => error
raise InternalServerError.slurp error
rescue Excon::Errors::HTTPStatusError => error
raise ServiceError.slurp error
end
unless response.body.empty?
response.body = Fog::JSON.decode(response.body)
end
response
def request(params, parse_json = true, &block)
super(params, parse_json, &block)
rescue Excon::Errors::NotFound => error
raise NotFound.slurp(error, region)
rescue Excon::Errors::BadRequest => error
raise BadRequest.slurp error
rescue Excon::Errors::InternalServerError => error
raise InternalServerError.slurp error
rescue Excon::Errors::HTTPStatusError => error
raise ServiceError.slurp error
end

def authenticate
options = {
def authenticate(options={})
super({
:rackspace_api_key => @rackspace_api_key,
:rackspace_username => @rackspace_username,
:rackspace_auth_url => @rackspace_auth_url,
:connection_options => @connection_options
}
super(options)
})
end

def service_name
Expand Down
34 changes: 10 additions & 24 deletions lib/fog/rackspace/cdn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,30 +152,16 @@ def purge(file)
true
end

def request(params, parse_json = true)
begin
response = @connection.request(params.merge!({
:headers => {
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-Auth-Token' => auth_token
}.merge!(params[:headers] || {}),
:host => endpoint_uri.host,
:path => "#{endpoint_uri.path}/#{params[:path]}",
}))
rescue Excon::Errors::NotFound => error
raise Fog::Storage::Rackspace::NotFound.slurp(error, region)
rescue Excon::Errors::BadRequest => error
raise Fog::Storage::Rackspace::BadRequest.slurp error
rescue Excon::Errors::InternalServerError => error
raise Fog::Storage::Rackspace::InternalServerError.slurp error
rescue Excon::Errors::HTTPStatusError => error
raise Fog::Storage::Rackspace::ServiceError.slurp error
end
if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
response.body = Fog::JSON.decode(response.body)
end
response
def request(params, parse_json = true, &block)
super(params, parse_json, &block)
rescue Excon::Errors::NotFound => error
raise Fog::Storage::Rackspace::NotFound.slurp(error, region)
rescue Excon::Errors::BadRequest => error
raise Fog::Storage::Rackspace::BadRequest.slurp error
rescue Excon::Errors::InternalServerError => error
raise Fog::Storage::Rackspace::InternalServerError.slurp error
rescue Excon::Errors::HTTPStatusError => error
raise Fog::Storage::Rackspace::ServiceError.slurp error
end

private
Expand Down
62 changes: 17 additions & 45 deletions lib/fog/rackspace/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ def initialize(options={})
@rackspace_servicenet = options[:rackspace_servicenet]
@rackspace_auth_token = options[:rackspace_auth_token]
@rackspace_endpoint = Fog::Rackspace.normalize_url(options[:rackspace_compute_v1_url] || options[:rackspace_management_url])
@rackspace_must_reauthenticate = false
@connection_options = options[:connection_options] || {}
authenticate
Excon.defaults[:ssl_verify_peer] = false if service_net?
Expand All @@ -202,56 +201,29 @@ def reload
@connection.reset
end

def request(params)
begin
response = @connection.request(params.merge({
:headers => {
'Content-Type' => 'application/json',
'X-Auth-Token' => auth_token
}.merge!(params[:headers] || {}),
:host => endpoint_uri.host,
:path => "#{endpoint_uri.path}/#{params[:path]}",
}))
rescue Excon::Errors::Unauthorized => error
if error.response.body != 'Bad username or password' # token expiration
@rackspace_must_reauthenticate = true
authenticate
retry
else # bad credentials
raise error
end
rescue Excon::Errors::HTTPStatusError => error
raise case error
when Excon::Errors::NotFound
NotFound.slurp(error, region)
else
error
end
end
unless response.body.empty?
response.body = Fog::JSON.decode(response.body)
end
response
def request(params, parse_json = true, &block)
super(params, parse_json, &block)
rescue Excon::Errors::NotFound => error
raise NotFound.slurp(error, region)
rescue Excon::Errors::BadRequest => error
raise BadRequest.slurp error
rescue Excon::Errors::InternalServerError => error
raise InternalServerError.slurp error
rescue Excon::Errors::HTTPStatusError => error
raise ServiceError.slurp error
end


def service_net?
@rackspace_servicenet == true
end

def authenticate
if @rackspace_must_reauthenticate || @rackspace_auth_token.nil?
options = {
:rackspace_api_key => @rackspace_api_key,
:rackspace_username => @rackspace_username,
:rackspace_auth_url => @rackspace_auth_url,
:connection_options => @connection_options
}
super(options)
else
@auth_token = @rackspace_auth_token
@uri = URI.parse(@rackspace_endpoint)
end
def authenticate(options={})
super({
:rackspace_api_key => @rackspace_api_key,
:rackspace_username => @rackspace_username,
:rackspace_auth_url => @rackspace_auth_url,
:connection_options => @connection_options
})
end

def service_name
Expand Down
46 changes: 13 additions & 33 deletions lib/fog/rackspace/compute_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,45 +139,25 @@ def initialize(options = {})
@connection = Fog::Connection.new(endpoint_uri.to_s, @persistent, @connection_options)
end

def request(params)
begin
response = @connection.request(params.merge!({
:headers => {
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-Auth-Token' => auth_token
}.merge!(params[:headers] || {}),
:host => endpoint_uri.host,
:path => "#{endpoint_uri.path}/#{params[:path]}"
}))
rescue Excon::Errors::NotFound => error
raise NotFound.slurp(error, region)
rescue Excon::Errors::BadRequest => error
raise BadRequest.slurp error
rescue Excon::Errors::InternalServerError => error
raise InternalServerError.slurp error
rescue Excon::Errors::HTTPStatusError => error
raise ServiceError.slurp error
end

unless response.body.empty?
begin
response.body = Fog::JSON.decode(response.body)
rescue MultiJson::DecodeError => e
response.body = {}
end
end
response
def request(params, parse_json = true, &block)
super(params, parse_json, &block)
rescue Excon::Errors::NotFound => error
raise NotFound.slurp(error, region)
rescue Excon::Errors::BadRequest => error
raise BadRequest.slurp error
rescue Excon::Errors::InternalServerError => error
raise InternalServerError.slurp error
rescue Excon::Errors::HTTPStatusError => error
raise ServiceError.slurp error
end

def authenticate
options = {
def authenticate(options={})
super({
:rackspace_api_key => @rackspace_api_key,
:rackspace_username => @rackspace_username,
:rackspace_auth_url => @rackspace_auth_url,
:connection_options => @connection_options
}
super(options)
})
end

def service_name
Expand Down
42 changes: 13 additions & 29 deletions lib/fog/rackspace/databases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,45 +85,29 @@ def initialize(options = {})
@connection = Fog::Connection.new(endpoint_uri.to_s, @persistent, @connection_options)
end

def request(params)
begin
response = @connection.request(params.merge!({
:headers => {
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-Auth-Token' => auth_token
}.merge!(params[:headers] || {}),
:host => endpoint_uri.host,
:path => "#{endpoint_uri.path}/#{params[:path]}"
}))
rescue Excon::Errors::NotFound => error
raise NotFound.slurp(error, region)
rescue Excon::Errors::BadRequest => error
raise BadRequest.slurp error
rescue Excon::Errors::InternalServerError => error
raise InternalServerError.slurp error
rescue Excon::Errors::HTTPStatusError => error
raise ServiceError.slurp error
end
unless response.body.empty?
response.body = Fog::JSON.decode(response.body)
end
response
def request(params, parse_json = true, &block)
super(params, parse_json, &block)
rescue Excon::Errors::NotFound => error
raise NotFound.slurp(error, region)
rescue Excon::Errors::BadRequest => error
raise BadRequest.slurp error
rescue Excon::Errors::InternalServerError => error
raise InternalServerError.slurp error
rescue Excon::Errors::HTTPStatusError => error
raise ServiceError.slurp error
end

def endpoint_uri(service_endpoint_url=nil)
@uri = super(@rackspace_endpoint || service_endpoint_url, :rackspace_database_url)
end

def authenticate
options = {
def authenticate(options={})
super({
:rackspace_api_key => @rackspace_api_key,
:rackspace_username => @rackspace_username,
:rackspace_auth_url => @rackspace_auth_url,
:connection_options => @connection_options
}

super(options)
})
end

private
Expand Down
42 changes: 20 additions & 22 deletions lib/fog/rackspace/dns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ module DNS
class Rackspace < Fog::Service
include Fog::Rackspace::Errors

class ServiceError < Fog::Rackspace::Errors::ServiceError; end
class InternalServerError < Fog::Rackspace::Errors::InternalServerError; end
class BadRequest < Fog::Rackspace::Errors::BadRequest; end
class Conflict < Fog::Rackspace::Errors::Conflict; end
class ServiceUnavailable < Fog::Rackspace::Errors::ServiceUnavailable; end

class CallbackError < Fog::Errors::Error
attr_reader :response, :message, :details
def initialize(response)
Expand Down Expand Up @@ -95,9 +101,6 @@ def initialize(options={})

deprecation_warnings(options)

@connection_options[:headers] ||= {}
@connection_options[:headers].merge!({ 'Content-Type' => 'application/json', 'X-Auth-Token' => auth_token })

@persistent = options[:persistent] || false
@connection = Fog::Connection.new(endpoint_uri.to_s, @persistent, @connection_options)
end
Expand All @@ -108,25 +111,22 @@ def endpoint_uri(service_endpoint_url=nil)

private

def request(params)
#TODO - Unify code with other rackspace services
def request(params, parse_json = true, &block)
begin
response = @connection.request(params.merge!({
:path => "#{endpoint_uri.path}/#{params[:path]}"
}))
rescue Excon::Errors::BadRequest => error
raise Fog::Rackspace::Errors::BadRequest.slurp error
rescue Excon::Errors::Conflict => error
raise Fog::Rackspace::Errors::Conflict.slurp error
super(params, parse_json, &block)
rescue Excon::Errors::NotFound => error
raise NotFound.slurp(error, region)
rescue Excon::Errors::BadRequest => error
raise BadRequest.slurp error
rescue Excon::Errors::InternalServerError => error
raise InternalServerError.slurp error
rescue Excon::Errors::ServiceUnavailable => error
raise Fog::Rackspace::Errors::ServiceUnavailable.slurp error
end
unless response.body.empty?
response.body = Fog::JSON.decode(response.body)
raise ServiceUnavailable.slurp error
rescue Excon::Errors::Conflict => error
raise Conflict.slurp error
rescue Excon::Errors::HTTPStatusError => error
raise ServiceError.slurp error
end
response
end

def array_to_query_string(arr)
Expand Down Expand Up @@ -164,15 +164,13 @@ def authenticate_v1(options)
@auth_token = credentials['X-Auth-Token']
end

def authenticate
options = {
def authenticate(options={})
super({
:rackspace_api_key => @rackspace_api_key,
:rackspace_username => @rackspace_username,
:rackspace_auth_url => @rackspace_auth_url,
:connection_options => @connection_options
}

super(options)
})
end
end
end
Expand Down
Loading

0 comments on commit b039a25

Please sign in to comment.