From 271bc985fa057e3297249061af5ac91463d349ba Mon Sep 17 00:00:00 2001 From: Rupak Ganguly Date: Wed, 26 Sep 2012 13:01:32 -0400 Subject: [PATCH] Add object temp url generation capability with mock support. --- .../requests/storage/get_object_temp_url.rb | 34 +++++++++++++++++++ lib/fog/hp/storage.rb | 27 +++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 lib/fog/hp/requests/storage/get_object_temp_url.rb diff --git a/lib/fog/hp/requests/storage/get_object_temp_url.rb b/lib/fog/hp/requests/storage/get_object_temp_url.rb new file mode 100644 index 000000000..e803ec625 --- /dev/null +++ b/lib/fog/hp/requests/storage/get_object_temp_url.rb @@ -0,0 +1,34 @@ +module Fog + module Storage + class HP + + class Real + + # Generate a temporary url for an object + # + # ==== Parameters + # * container<~String> - Name of container + # * object<~String> - Name of object + # * expires<~Integer> - Time the temporary url expire in secs. + # * method<~String> - Allowed HTTP method GET, PUT, HEAD only + def get_object_temp_url(container, object, expires, method) + generate_object_temp_url(container, object, expires, method) + end + + end + + class Mock # :nodoc:all + + def get_object_temp_url(container, object, expires, method) + @scheme = "https" + @host = "swift-cluster.example.com" + @port = "443" + @path = "/v1/account" + + generate_object_temp_url(container, object, expires, method) + end + end + + end + end +end \ No newline at end of file diff --git a/lib/fog/hp/storage.rb b/lib/fog/hp/storage.rb index 21fd3e8e6..7e1b81caa 100644 --- a/lib/fog/hp/storage.rb +++ b/lib/fog/hp/storage.rb @@ -20,6 +20,7 @@ module Fog request :get_container request :get_containers request :get_object + request :get_object_temp_url request :head_container request :head_containers request :head_object @@ -81,6 +82,32 @@ module Fog acl = "public-read-write" end end + + def generate_object_temp_url(container, object, expires_secs, method) + return unless (container && object && expires_secs && method) + + # POST not allowed + allowed_methods = %w{GET PUT HEAD} + unless allowed_methods.include?(method) + raise ArgumentError.new("Invalid method '#{method}' specified. Valid methods are: #{allowed_methods.join(', ')}") + end + + expires = (Time.now + expires_secs.to_i).to_i + + # do not encode before signature generation, encode after + path = "#{@path}/#{container}/#{object}" + encoded_path = "#{@path}/#{Fog::HP.escape(container)}/#{Fog::HP.escape(object)}" + + string_to_sign = "#{method}\n#{expires}\n#{path}" + signed_string = Digest::HMAC.hexdigest(string_to_sign, @hp_secret_key, Digest::SHA1) + + signature = @hp_account_id.to_s + ":" + signed_string + signature = Fog::HP.escape(signature) + + # generate the temp url using the signature and expiry + temp_url = "#{@scheme}://#{@host}:#{@port}#{encoded_path}?temp_url_sig=#{signature}&temp_url_expires=#{expires}" + end + end class Mock