From ce140dc4e5b39fb73eaed95e8da681f75d716deb Mon Sep 17 00:00:00 2001 From: Kyle Rames Date: Tue, 19 Feb 2013 14:43:06 -0600 Subject: [PATCH] [rackspace|storage] updating to use 2.0 authentication endpoints --- lib/fog/rackspace/identity.rb | 11 ++- .../models/identity/service_catalog.rb | 75 +++++++++++++++++++ lib/fog/rackspace/storage.rb | 43 +++++++++-- .../models/identity/service_catalog_tests.rb | 74 ++++++++++++++++++ tests/rackspace/storage_tests.rb | 53 +++++++++++++ 5 files changed, 247 insertions(+), 9 deletions(-) create mode 100644 lib/fog/rackspace/models/identity/service_catalog.rb create mode 100644 tests/rackspace/models/identity/service_catalog_tests.rb create mode 100644 tests/rackspace/storage_tests.rb diff --git a/lib/fog/rackspace/identity.rb b/lib/fog/rackspace/identity.rb index 911c7b86a..01c560374 100644 --- a/lib/fog/rackspace/identity.rb +++ b/lib/fog/rackspace/identity.rb @@ -18,6 +18,7 @@ module Fog collection :credentials model :tenant collection :tenants + model :service_catalog request_path 'fog/rackspace/requests/identity' request :create_token @@ -33,15 +34,20 @@ module Fog request :delete_user class Mock + attr_reader :service_catalog + def request Fog::Mock.not_implemented end end class Real + attr_reader :service_catalog, :auth_token + def initialize(options={}) @rackspace_username = options[:rackspace_username] @rackspace_api_key = options[:rackspace_api_key] + @rackspace_region = options[:rackspace_region] @rackspace_auth_url = options[:rackspace_auth_url] || US_ENDPOINT uri = URI.parse(@rackspace_auth_url) @@ -55,7 +61,7 @@ module Fog authenticate end - + def request(params) begin parameters = params.merge!({ @@ -71,9 +77,10 @@ module Fog response end end - + def authenticate data = self.create_token(@rackspace_username, @rackspace_api_key).body + @service_catalog = ServiceCatalog.from_response(self, data) @auth_token = data['access']['token']['id'] end end diff --git a/lib/fog/rackspace/models/identity/service_catalog.rb b/lib/fog/rackspace/models/identity/service_catalog.rb new file mode 100644 index 000000000..75ac8b6e1 --- /dev/null +++ b/lib/fog/rackspace/models/identity/service_catalog.rb @@ -0,0 +1,75 @@ +require 'fog/core/model' + +module Fog + module Rackspace + class Identity + class ServiceCatalog < Fog::Model + attr_reader :catalog + + def initialize(attributes) + @service = attributes.delete(:service) + @catalog = {} + end + + def services + catalog.keys + end + + def get_endpoints(service_type) + service_type = service_type.is_a?(String) ? service_type.to_sym : service_type + catalog[service_type] + end + + def get_endpoint(service_type, region=nil) + endpoint = get_endpoints(service_type) + raise "Unable to locate endpoint for service #{service_type}" unless endpoint + + return endpoint if endpoint.is_a?(String) + + unless region + regions = endpoint.collect { |k,v| ":#{k}" }.join(", ") + raise "There are multiple endpoints avaliable for #{service_type}. Please specify one of the following regions: #{regions}." + end + region = region.is_a?(String) ? region.to_sym : region + get_endpoints(service_type)[region] + end + + def reload + @service.authenticate + @service.service_catalog + end + + def self.from_response(service, hash) + service_catalog = ServiceCatalog.new :service => service + services = hash["access"]["serviceCatalog"] + services.each do |serv| + name = serv["name"].to_sym + service_catalog.add_endpoints(name, serv) + end + service_catalog + end + + def add_endpoints(service_name, hash) + endpoints = hash["endpoints"] + if endpoints.size == 1 + catalog[service_name] = endpoints[0]["publicURL"].freeze + else + catalog[service_name] = endpoints_from_array(endpoints) + end + end + + private + def endpoints_from_array(endpoints) + hash = {} + endpoints.each do |endpoint| + region = endpoint["region"].downcase.to_sym + url = endpoint["publicURL"].freeze + hash[region] = url + end + hash + end + + end + end + end +end \ No newline at end of file diff --git a/lib/fog/rackspace/storage.rb b/lib/fog/rackspace/storage.rb index 0141c9630..d95b51487 100644 --- a/lib/fog/rackspace/storage.rb +++ b/lib/fog/rackspace/storage.rb @@ -6,7 +6,7 @@ module Fog class Rackspace < Fog::Service requires :rackspace_api_key, :rackspace_username - recognizes :rackspace_auth_url, :rackspace_servicenet, :rackspace_cdn_ssl, :persistent + recognizes :rackspace_auth_url, :rackspace_servicenet, :rackspace_cdn_ssl, :persistent, :rackspace_region recognizes :rackspace_temp_url_key model_path 'fog/rackspace/models/storage' @@ -46,9 +46,23 @@ module Fog end end + + module Base + private + + def authentication_method + if @rackspace_auth_url && @rackspace_auth_url =~ /v1(\.\d)?\w*$/ + :authenticate_v1 + else + :authenticate_v2 + end + end + + end class Mock include Utils + include Base def self.data @data ||= Hash.new do |hash, key| @@ -78,6 +92,8 @@ module Fog class Real include Utils + include Base + attr_reader :rackspace_cdn_ssl def initialize(options={}) @@ -89,6 +105,7 @@ module Fog @rackspace_servicenet = options[:rackspace_servicenet] @rackspace_auth_token = options[:rackspace_auth_token] @rackspace_storage_url = options[:rackspace_storage_url] + @rackspace_region = options[:rackspace_region] || :dfw @rackspace_temp_url_key = options[:rackspace_temp_url_key] @rackspace_must_reauthenticate = false @connection_options = options[:connection_options] || {} @@ -96,7 +113,7 @@ module Fog @persistent = options[:persistent] || false Excon.defaults[:ssl_verify_peer] = false if options[:rackspace_servicenet] == true @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options) - end + end def reload @connection.reset @@ -135,27 +152,39 @@ module Fog end private - + 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 - } - credentials = Fog::Rackspace.authenticate(options, @connection_options) - @auth_token = credentials['X-Auth-Token'] - uri = URI.parse(credentials['X-Storage-Url']) + } + uri = self.send authentication_method, options else @auth_token = @rackspace_auth_token uri = URI.parse(@rackspace_storage_url) end + @host = @rackspace_servicenet == true ? "snet-#{uri.host}" : uri.host @path = uri.path @port = uri.port @scheme = uri.scheme end + def authenticate_v2(options) + @identity_service = Fog::Rackspace::Identity.new(options) + @auth_token = @identity_service.auth_token + url = @identity_service.service_catalog.get_endpoint(:cloudFiles, @rackspace_region) + URI.parse url + end + + def authenticate_v1(options) + credentials = Fog::Rackspace.authenticate(options, @connection_options) + @auth_token = credentials['X-Auth-Token'] + uri = URI.parse(credentials['X-Storage-Url']) + end + end end end diff --git a/tests/rackspace/models/identity/service_catalog_tests.rb b/tests/rackspace/models/identity/service_catalog_tests.rb new file mode 100644 index 000000000..ccddf7f96 --- /dev/null +++ b/tests/rackspace/models/identity/service_catalog_tests.rb @@ -0,0 +1,74 @@ +require 'fog/rackspace/models/identity/service_catalog' + +Shindo.tests('Fog::Rackspace::ServiceCatalog | users', ['rackspace']) do + + tests('#from_response') do + before_hash = {"access"=>{"token"=>{"expires"=>"2013-02-20T10:31:00.000-06:00", "tenant"=>{"name"=>"777", "id"=>"777"}, "id"=>"6ca10877-7c50-4a5c-b58f-004d835c39c3"}, + "serviceCatalog"=>[{"type"=>"volume", "endpoints"=>[{"region"=>"DFW", "tenantId"=>"777", "publicURL"=>"https://dfw.blockstorage.api.rackspacecloud.com/v1/777"}, + {"region"=>"ORD", "tenantId"=>"777", "publicURL"=>"https://ord.blockstorage.api.rackspacecloud.com/v1/777"}], "name"=>"cloudBlockStorage"}, + {"type"=>"rax:load-balancer", "endpoints"=>[{"region"=>"ORD", "tenantId"=>"777", "publicURL"=>"https://ord.loadbalancers.api.rackspacecloud.com/v1.0/777"}, + {"region"=>"DFW", "tenantId"=>"777", "publicURL"=>"https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/777"}], "name"=>"cloudLoadBalancers"}, + {"type"=>"object-store", "endpoints"=>[{"internalURL"=>"https://snet-storage101.dfw1.clouddrive.com/v1/Mosso777", "region"=>"DFW", + "tenantId"=>"Mosso777", + "publicURL"=>"https://storage101.dfw1.clouddrive.com/v1/Mosso777"}, + {"internalURL"=>"https://snet-storage101.ord1.clouddrive.com/v1/Mosso777", "region"=>"ORD", + "tenantId"=>"Mosso777", + "publicURL"=>"https://storage101.ord1.clouddrive.com/v1/Mosso777"}], "name"=>"cloudFiles"}, {"type"=>"rax:database", + "endpoints"=>[{"region"=>"DFW", "tenantId"=>"777", "publicURL"=>"https://dfw.databases.api.rackspacecloud.com/v1.0/777"}, {"region"=>"ORD", "tenantId"=>"777", + "publicURL"=>"https://ord.databases.api.rackspacecloud.com/v1.0/777"}], "name"=>"cloudDatabases"}, {"type"=>"rax:dns", "endpoints"=>[{"tenantId"=>"777", + "publicURL"=>"https://dns.api.rackspacecloud.com/v1.0/777"}], "name"=>"cloudDNS"}, {"type"=>"compute", "endpoints"=>[{"versionId"=>"1.0", "tenantId"=>"777", + "versionList"=>"https://servers.api.rackspacecloud.com/", "versionInfo"=>"https://servers.api.rackspacecloud.com/v1.0", + "publicURL"=>"https://servers.api.rackspacecloud.com/v1.0/777"}], "name"=>"cloudServers"}, {"type"=>"compute", "endpoints"=>[{"region"=>"DFW", "versionId"=>"2", + "tenantId"=>"777", "versionList"=>"https://dfw.servers.api.rackspacecloud.com/", "versionInfo"=>"https://dfw.servers.api.rackspacecloud.com/v2", + "publicURL"=>"https://dfw.servers.api.rackspacecloud.com/v2/777"}, {"region"=>"ORD", "versionId"=>"2", "tenantId"=>"777", + "versionList"=>"https://ord.servers.api.rackspacecloud.com/", "versionInfo"=>"https://ord.servers.api.rackspacecloud.com/v2", + "publicURL"=>"https://ord.servers.api.rackspacecloud.com/v2/777"}], "name"=>"cloudServersOpenStack"}, {"type"=>"rax:monitor", "endpoints"=>[{"tenantId"=>"777", + "publicURL"=>"https://monitoring.api.rackspacecloud.com/v1.0/777"}], "name"=>"cloudMonitoring"}, {"type"=>"rax:object-cdn", "endpoints"=>[{"region"=>"DFW", + "tenantId"=>"Mosso777", "publicURL"=>"https://cdn1.clouddrive.com/v1/Mosso777"}, + {"region"=>"ORD", "tenantId"=>"Mosso777", + "publicURL"=>"https://cdn2.clouddrive.com/v1/Mosso777"}], "name"=>"cloudFilesCDN"}], "user"=>{"roles"=>[{"description"=>"User Admin + Role.", "name"=>"identity:user-admin", "id"=>"3"}], "name"=>"joe-racker", "RAX-AUTH:defaultRegion"=>"", "id"=>"TK421"}}} + + after_hash = {:cloudServers=>"https://servers.api.rackspacecloud.com/v1.0/777", :cloudServersOpenStack=>{:dfw=>"https://dfw.servers.api.rackspacecloud.com/v2/777", :ord=>"https://ord.servers.api.rackspacecloud.com/v2/777"}, :cloudFiles=>{:dfw=>"https://storage101.dfw1.clouddrive.com/v1/Mosso777", :ord=>"https://storage101.ord1.clouddrive.com/v1/Mosso777"}, :cloudBlockStorage=>{:dfw=>"https://dfw.blockstorage.api.rackspacecloud.com/v1/777", :ord=>"https://ord.blockstorage.api.rackspacecloud.com/v1/777"}, :cloudMonitoring=>"https://monitoring.api.rackspacecloud.com/v1.0/777", :cloudLoadBalancers=>{:dfw=>"https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/777", :ord=>"https://ord.loadbalancers.api.rackspacecloud.com/v1.0/777"}, :cloudFilesCDN=>{:dfw=>"https://cdn1.clouddrive.com/v1/Mosso777", :ord=>"https://cdn2.clouddrive.com/v1/Mosso777"}, :cloudDatabases=>{:dfw=>"https://dfw.databases.api.rackspacecloud.com/v1.0/777", :ord=>"https://ord.databases.api.rackspacecloud.com/v1.0/777"}, :cloudDNS=>"https://dns.api.rackspacecloud.com/v1.0/777"} + @service_catalog = Fog::Rackspace::Identity::ServiceCatalog.from_response(nil, before_hash) + returns(after_hash) { @service_catalog.catalog } + end + + tests('services') do + services = [:cloudBlockStorage, :cloudLoadBalancers, :cloudFiles, :cloudDatabases, :cloudDNS, :cloudServers, :cloudServersOpenStack, :cloudMonitoring, :cloudFilesCDN] + returns(services) { @service_catalog.services } + end + + tests('get_endpoints') do + endpoints = {:dfw=>"https://dfw.servers.api.rackspacecloud.com/v2/777", :ord=>"https://ord.servers.api.rackspacecloud.com/v2/777"} + returns(endpoints) { @service_catalog.get_endpoints(:cloudServersOpenStack) } + returns(endpoints) { @service_catalog.get_endpoints('cloudServersOpenStack') } + returns(nil) { @service_catalog.get_endpoints('non-existent') } + end + + tests('get_endpoint') do + tests('service with mulitple endpoints') do + returns("https://dfw.servers.api.rackspacecloud.com/v2/777") { @service_catalog.get_endpoint(:cloudServersOpenStack, :dfw) } + returns("https://ord.servers.api.rackspacecloud.com/v2/777") { @service_catalog.get_endpoint(:cloudServersOpenStack, :ord) } + returns("https://dfw.servers.api.rackspacecloud.com/v2/777") { @service_catalog.get_endpoint(:cloudServersOpenStack, 'dfw') } + returns("https://dfw.servers.api.rackspacecloud.com/v2/777") { @service_catalog.get_endpoint('cloudServersOpenStack', 'dfw') } + end + + tests('with one endpoint') do + returns("https://monitoring.api.rackspacecloud.com/v1.0/777") { @service_catalog.get_endpoint(:cloudMonitoring, 'dfw') } + end + + tests('error conditions') do + raises(RuntimeError) { @service_catalog.get_endpoint(:cloudServersOpenStack) } + returns(nil) { @service_catalog.get_endpoint(:cloudServersOpenStack, :sat) } + raises(RuntimeError) { @service_catalog.get_endpoint('non-existent') } + end + end + + tests('reload').succeeds do + pending if Fog.mocking? + + catalog = Fog::Identity[:rackspace].service_catalog + catalog.reload + end +end \ No newline at end of file diff --git a/tests/rackspace/storage_tests.rb b/tests/rackspace/storage_tests.rb new file mode 100644 index 000000000..14f641d60 --- /dev/null +++ b/tests/rackspace/storage_tests.rb @@ -0,0 +1,53 @@ +Shindo.tests('Rackspace | Storage', ['rackspace']) do + + def assert_method(url, method) + @service.instance_variable_set "@rackspace_auth_url", method + returns(method) { @service.send :authentication_method } + end + + tests('#authentication_method') do + @service = Fog::Storage::Rackspace.new + assert_method nil, :authenticate_v2 + + assert_method 'https://identity.api.rackspacecloud.com/v1', :authenticate_v1 + assert_method 'https://identity.api.rackspacecloud.com/v1.1', :authenticate_v1 + assert_method 'https://identity.api.rackspacecloud.com/v2.0', :authenticate_v2 + + assert_method 'https://lon.identity.api.rackspacecloud.com/v1', :authenticate_v1 + assert_method 'https://lon.identity.api.rackspacecloud.com/v1.1', :authenticate_v1 + assert_method 'https://lon.identity.api.rackspacecloud.com/v2.0', :authenticate_v2 + end + + tests('legacy authentication') do + pending if Fog.mocking? + @service = Fog::Storage::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v1.0' + + tests('variables populated') do + returns(false, "auth token populated") { @service.instance_variable_get("@auth_token").nil? } + returns(false, "path populated") { @service.instance_variable_get("@path").nil? } + returns(true, "identity_service was not used") { @service.instance_variable_get("@identity_service").nil? } + end + end + + tests('current authentation') do + pending if Fog.mocking? + @service = Fog::Storage::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0' + + tests('variables populated') do + returns(false, "auth token populated") { @service.instance_variable_get("@auth_token").nil? } + returns(false, "path populated") { @service.instance_variable_get("@path").nil? } + returns(false, "identity service was used") { @service.instance_variable_get("@identity_service").nil? } + end + tests('dfw region') do + @service = Fog::Storage::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0', :rackspace_region => :dfw + returns(true) { (@service.instance_variable_get("@host") =~ /dfw\d/) != nil } + end + tests('ord region') do + @service = Fog::Storage::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0', :rackspace_region => :ord + returns(true) { (@service.instance_variable_get("@host") =~ /ord\d/) != nil } + + end + end + + +end