[Ovirt-devel] [PATCH] Add a method to log into the OVirt server

David Lutterkort lutter at redhat.com
Fri Aug 15 23:53:19 UTC 2008


Before we can do anything else, we need to go to the login controller and
get a session cookie.

Unfortunatly, this code requires some monkey-patching of
ActiveResource::Connection to store the session cookie in the connection's
headers.

This patch should be applied after the series of patches I sent yesterday.

Signed-off-by: David Lutterkort <lutter at redhat.com>
---
 wui/client/README             |    7 ++++-
 wui/client/examples/script.rb |    2 +
 wui/client/lib/ovirt.rb       |   47 ++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/wui/client/README b/wui/client/README
index 4bdce27..ee1db15 100644
--- a/wui/client/README
+++ b/wui/client/README
@@ -10,5 +10,8 @@ The server is specified with a URL of the form
   http://USER:PASSWORD@HOST/ovirt
 
 This requires that the server is configured to allow HTTP authentication,
-since there are no mechanisms in the API to forward krb5 tickets. You can
-also try and connect to Mongrel running on HOST:3000 directly.
+since there are no mechanisms in the API to forward krb5 tickets.
+
+Before calling any other method on the API, you need to call
+  OVirt::Base::site = "http://USER:PASSWORD@HOST/ovirt"
+  OVirt::Base::login
diff --git a/wui/client/examples/script.rb b/wui/client/examples/script.rb
index 2103ad7..1485535 100755
--- a/wui/client/examples/script.rb
+++ b/wui/client/examples/script.rb
@@ -58,6 +58,8 @@ EOF
     exit 1
 end
 
+OVirt::Base.login
+
 # Get a single host by name
 host = OVirt::Host.find_by_hostname("node3.priv.ovirt.org")
 puts "#{host.uuid} has id #{host.id}"
diff --git a/wui/client/lib/ovirt.rb b/wui/client/lib/ovirt.rb
index 48739f4..ce8c310 100644
--- a/wui/client/lib/ovirt.rb
+++ b/wui/client/lib/ovirt.rb
@@ -2,8 +2,53 @@ require 'pp'
 require 'rubygems'
 require 'activeresource'
 
+class ActiveResource::Connection
+  attr_accessor :session
+
+  alias_method :old_default_header, :default_header
+
+  def default_header
+    old_default_header
+    @default_header ||= {}
+    if session
+      @default_header["Cookie"] = session
+    end
+    @default_header
+  end
+end
+
 module OVirt
-    class Base < ActiveResource::Base ; end
+    class Base < ActiveResource::Base
+      def self.login
+        # Do the double-redirect handshake. We don't go directly
+        # to the login page, since we don't want to hardcode
+        # its location
+        response = get_redirected(prefix)
+        uri = URI::parse(response["Location"])
+        response = get_redirected(uri.path)
+        unless connection.session = session_cookie(response)
+          raise "Authentication failed"
+        end
+      end
+
+      private
+      def self.session_cookie(response)
+        if cookies = response.get_fields("Set-Cookie")
+          cookies.find { |cookie|
+            cookie.split(";")[0].split("=")[0] == "_ovirt_session_id"
+          }
+        end
+      end
+
+      def self.get_redirected(path)
+        begin
+          # We can't use HEAD since it's not in Rails 2.0.2
+          connection.get(path)
+        rescue ActiveResource::Redirection => e
+          return e.response
+        end
+      end
+    end
 
     class HardwarePool < Base
         def self.find_by_path(path)
-- 
1.5.5.1




More information about the ovirt-devel mailing list