You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by lu...@apache.org on 2010/07/09 01:35:58 UTC

svn commit: r962198 - in /incubator/deltacloud/trunk/server/libexec/lib: deltacloud/drivers/ec2/ deltacloud/drivers/mock/ deltacloud/drivers/opennebula/ deltacloud/drivers/rackspace/ deltacloud/drivers/rhevm/ deltacloud/drivers/rimu/ sinatra/

Author: lutter
Date: Thu Jul  8 23:35:57 2010
New Revision: 962198

URL: http://svn.apache.org/viewvc?rev=962198&view=rev
Log:
Add a helper for transparent lazy authentication

The lazy_auth helper transparently triggers an authentication request when
a driver needs username or password. With that, there's no need for drivers
to check if the username/password have been set.

Added:
    incubator/deltacloud/trunk/server/libexec/lib/sinatra/lazy_auth.rb
Modified:
    incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/ec2/ec2_driver.rb
    incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/mock/mock_driver.rb
    incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/opennebula/opennebula_driver.rb
    incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/rackspace/rackspace_driver.rb
    incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
    incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/rimu/rimu_hosting_client.rb

Modified: incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/ec2/ec2_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/ec2/ec2_driver.rb?rev=962198&r1=962197&r2=962198&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/ec2/ec2_driver.rb (original)
+++ incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/ec2/ec2_driver.rb Thu Jul  8 23:35:57 2010
@@ -272,10 +272,7 @@ class EC2Driver < Deltacloud::BaseDriver
   private
 
   def new_client(credentials)
-    if ( credentials[:name].nil? || credentials[:password].nil? || credentials[:name] == '' || credentials[:password] == '' )
-      raise Deltacloud::AuthException.new
-    end
-    RightAws::Ec2.new(credentials[:name], credentials[:password], :cache=>false )
+    RightAws::Ec2.new(credentials.user, credentials.password, :cache=>false )
   end
 
   def convert_image(ec2_image)

Modified: incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/mock/mock_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/mock/mock_driver.rb?rev=962198&r1=962197&r2=962198&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/mock/mock_driver.rb (original)
+++ incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/mock/mock_driver.rb Thu Jul  8 23:35:57 2010
@@ -123,7 +123,7 @@ class MockDriver < Deltacloud::BaseDrive
     images = filter_on( images, :id, opts )
     images = filter_on( images, :architecture, opts )
     if ( opts && opts[:owner_id] == 'self' )
-      images = images.select{|e| e.owner_id == credentials[:name] }
+      images = images.select{|e| e.owner_id == credentials.user }
     else
       images = filter_on( images, :owner_id, opts )
     end
@@ -139,7 +139,7 @@ class MockDriver < Deltacloud::BaseDrive
     instances = []
     Dir[ "#{STORAGE_ROOT}/instances/*.yml" ].each do |instance_file|
       instance = YAML.load( File.read( instance_file ) )
-      if ( instance[:owner_id] == credentials[:name] )
+      if ( instance[:owner_id] == credentials.user )
         instance[:id] = File.basename( instance_file, ".yml" )
         instance[:actions] = instance_actions_for( instance[:state] )
         instances << Instance.new( instance )
@@ -173,7 +173,7 @@ class MockDriver < Deltacloud::BaseDrive
       :name=>name,
       :state=>'RUNNING',
       :image_id=>image_id,
-      :owner_id=>credentials[:name],
+      :owner_id=>credentials.user,
       :public_addresses=>["#{image_id}.#{next_id}.public.com"],
       :private_addresses=>["#{image_id}.#{next_id}.private.com"],
       :flavor_id=>flavor_id,
@@ -234,7 +234,7 @@ class MockDriver < Deltacloud::BaseDrive
     volumes = []
     Dir[ "#{STORAGE_ROOT}/storage_volumes/*.yml" ].each do |storage_volume_file|
       storage_volume = YAML.load( File.read( storage_volume_file ) )
-      if ( storage_volume[:owner_id] == credentials[:name] )
+      if ( storage_volume[:owner_id] == credentials.user )
         storage_volume[:id] = File.basename( storage_volume_file, ".yml" )
         volumes << StorageVolume.new( storage_volume )
       end
@@ -252,7 +252,7 @@ class MockDriver < Deltacloud::BaseDrive
     snapshots = []
     Dir[ "#{STORAGE_ROOT}/storage_snapshots/*.yml" ].each do |storage_snapshot_file|
       storage_snapshot = YAML.load( File.read( storage_snapshot_file ) )
-      if ( storage_snapshot[:owner_id] == credentials[:name] )
+      if ( storage_snapshot[:owner_id] == credentials.user )
         storage_snapshot[:id] = File.basename( storage_snapshot_file, ".yml" )
         snapshots << StorageSnapshot.new( storage_snapshot )
       end
@@ -264,11 +264,11 @@ class MockDriver < Deltacloud::BaseDrive
   private
 
   def check_credentials(credentials)
-    if ( credentials[:name] != 'mockuser' )
+    if ( credentials.user != 'mockuser' )
       raise Deltacloud::AuthException.new
     end
 
-    if ( credentials[:password] != 'mockpassword' )
+    if ( credentials.password != 'mockpassword' )
       raise Deltacloud::AuthException.new
     end
   end

Modified: incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/opennebula/opennebula_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/opennebula/opennebula_driver.rb?rev=962198&r1=962197&r2=962198&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/opennebula/opennebula_driver.rb (original)
+++ incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/opennebula/opennebula_driver.rb Thu Jul  8 23:35:57 2010
@@ -178,10 +178,7 @@ class OpennebulaDriver < Deltacloud::Bas
   private
 
   def new_client(credentials)
-	if ( credentials[:name].nil? || credentials[:password].nil? || credentials[:name] == '' || credentials[:password] == '' )
-		raise Deltacloud::AuthException.new
-	end
-	OCCIClient::Client.new(nil,	credentials[:name], credentials[:password], false)
+	OCCIClient::Client.new(nil,	credentials.user, credentials.password, false)
   end
 
 
@@ -193,7 +190,7 @@ class OpennebulaDriver < Deltacloud::Bas
 		:id=>diskhash['ID'].text,
 		:name=>diskhash['NAME'].text,
 		:description=>diskhash['NAME'].text,
-		:owner_id=>credentials[:name],
+		:owner_id=>credentials.user,
 		:architecture=>'Any architecture',
 	} )
   end
@@ -216,7 +213,7 @@ class OpennebulaDriver < Deltacloud::Bas
 
 	Instance.new( {
 		:id=>computehash['ID'].text,
-		:owner_id=>credentials[:name],
+		:owner_id=>credentials.user,
 		:name=>computehash['NAME'].text,
 		:image_id=>imageid,
 		:flavor_id=>flavor,

Modified: incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/rackspace/rackspace_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/rackspace/rackspace_driver.rb?rev=962198&r1=962197&r2=962198&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/rackspace/rackspace_driver.rb (original)
+++ incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/rackspace/rackspace_driver.rb Thu Jul  8 23:35:57 2010
@@ -127,10 +127,7 @@ class RackspaceDriver < Deltacloud::Base
 
 
   def new_client(credentials)
-    if ( credentials[:name].nil? || credentials[:password].nil? || credentials[:name] == '' || credentials[:password] == '' )
-      raise Deltacloud::AuthException.new
-    end
-    RackspaceClient.new(credentials[:name], credentials[:password])
+    RackspaceClient.new(credentials.name, credentials.password)
   end
 
   define_instance_states do

Modified: incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/rhevm/rhevm_driver.rb?rev=962198&r1=962197&r2=962198&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/rhevm/rhevm_driver.rb (original)
+++ incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/rhevm/rhevm_driver.rb Thu Jul  8 23:35:57 2010
@@ -59,11 +59,7 @@ class RHEVMDriver < Deltacloud::BaseDriv
   end
 
   def genArgString(credentials, args)
-    if ( credentials[:name].nil? || credentials[:password].nil? || credentials[:name] == '' || credentials[:password] == '' )
-      raise Deltacloud::AuthException.new
-    end
-    puts CONFIG["domain"]
-    commonArgs = [SCRIPT_DIR_ARG, credentials[:name], credentials[:password], CONFIG["domain"]]
+    commonArgs = [SCRIPT_DIR_ARG, credentials.name, credentials.password, CONFIG["domain"]]
     commonArgs.concat(args)
     commonArgs.join(" ")
   end

Modified: incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/rimu/rimu_hosting_client.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/rimu/rimu_hosting_client.rb?rev=962198&r1=962197&r2=962198&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/rimu/rimu_hosting_client.rb (original)
+++ incubator/deltacloud/trunk/server/libexec/lib/deltacloud/drivers/rimu/rimu_hosting_client.rb Thu Jul  8 23:35:57 2010
@@ -31,10 +31,8 @@ class RimuHostingClient
     @uri = URI.parse(baseuri)
     @service = Net::HTTP.new(@uri.host, @uri.port)
     @service.use_ssl = true
-    if(credentials[:password].nil? || credentials[:password] == "")
-      @auth = nil
-    else
-      @auth = "rimuhosting apikey=%s" % [credentials[:password]]  
+    if credentials.provided?
+      @auth = "rimuhosting apikey=#{credentials.password}"
     end
 
   end

Added: incubator/deltacloud/trunk/server/libexec/lib/sinatra/lazy_auth.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/libexec/lib/sinatra/lazy_auth.rb?rev=962198&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/libexec/lib/sinatra/lazy_auth.rb (added)
+++ incubator/deltacloud/trunk/server/libexec/lib/sinatra/lazy_auth.rb Thu Jul  8 23:35:57 2010
@@ -0,0 +1,56 @@
+require 'sinatra/base'
+
+# Lazy Basic HTTP authentication. Authentication is only forced when the
+# credentials are actually needed.
+module Sinatra
+  module LazyAuth
+    class LazyCredentials
+      def initialize(app)
+        @app = app
+        @provided = false
+      end
+
+      def user
+        credentials!
+        @user
+      end
+
+      def password
+        credentials!
+        @password
+      end
+
+      def provided?
+        @provided
+      end
+
+      private
+      def credentials!
+        unless provided?
+          auth = Rack::Auth::Basic::Request.new(@app.request.env)
+          unless auth.provided? && auth.basic? && auth.credentials
+            @app.authorize!
+          end
+          @user = auth.credentials[0]
+          @password = auth.credentials[1]
+          @provided = true
+        end
+      end
+
+    end
+
+    def authorize!
+      r = "#{DRIVER}-deltacloud@#{HOSTNAME}"
+      response['WWW-Authenticate'] = %(Basic realm="#{r}")
+      throw(:halt, [401, "Not authorized\n"])
+    end
+
+    # Request the current user's credentials. Actual credentials are only
+    # requested when an attempt is made to get the user name or password
+    def credentials
+      LazyCredentials.new(self)
+    end
+  end
+
+  helpers LazyAuth
+end