2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/hp'
|
2011-10-17 15:25:07 -04:00
|
|
|
require 'fog/storage'
|
|
|
|
|
2011-06-15 02:13:18 -04:00
|
|
|
module Fog
|
2011-10-17 15:25:07 -04:00
|
|
|
module Storage
|
|
|
|
class HP < Fog::Service
|
2011-06-15 02:13:18 -04:00
|
|
|
|
2013-01-22 17:28:33 -05:00
|
|
|
requires :hp_secret_key, :hp_tenant_id, :hp_avl_zone
|
|
|
|
recognizes :hp_auth_uri, :hp_cdn_ssl, :hp_cdn_uri
|
2013-01-18 15:06:41 -05:00
|
|
|
recognizes :persistent, :connection_options
|
|
|
|
recognizes :hp_use_upass_auth_style, :hp_auth_version, :user_agent
|
2013-01-22 17:28:33 -05:00
|
|
|
recognizes :hp_access_key, :hp_account_id # :hp_account_id is deprecated use hp_access_key instead
|
2011-06-15 02:10:54 -04:00
|
|
|
|
2012-06-22 12:38:00 -04:00
|
|
|
secrets :hp_secret_key
|
2011-06-15 02:13:18 -04:00
|
|
|
|
2011-10-17 15:25:07 -04:00
|
|
|
model_path 'fog/hp/models/storage'
|
2011-06-16 00:36:38 -04:00
|
|
|
model :directory
|
|
|
|
collection :directories
|
2012-10-19 01:33:20 -04:00
|
|
|
model :shared_directory
|
|
|
|
collection :shared_directories
|
2011-06-16 00:36:38 -04:00
|
|
|
model :file
|
|
|
|
collection :files
|
2012-10-19 01:33:20 -04:00
|
|
|
model :shared_file
|
|
|
|
collection :shared_files
|
2011-06-15 02:13:18 -04:00
|
|
|
|
2011-10-17 15:25:07 -04:00
|
|
|
request_path 'fog/hp/requests/storage'
|
2011-06-15 02:13:18 -04:00
|
|
|
request :delete_container
|
2011-06-15 17:59:06 -04:00
|
|
|
request :delete_object
|
2012-10-30 17:47:17 -04:00
|
|
|
request :delete_shared_object
|
2011-06-15 02:13:18 -04:00
|
|
|
request :get_container
|
|
|
|
request :get_containers
|
2011-06-15 17:59:06 -04:00
|
|
|
request :get_object
|
2012-09-26 13:01:32 -04:00
|
|
|
request :get_object_temp_url
|
2012-10-16 22:51:59 -04:00
|
|
|
request :get_shared_container
|
|
|
|
request :get_shared_object
|
2011-06-15 14:33:26 -04:00
|
|
|
request :head_container
|
|
|
|
request :head_containers
|
2011-06-15 17:59:06 -04:00
|
|
|
request :head_object
|
2012-10-16 22:51:59 -04:00
|
|
|
request :head_shared_container
|
|
|
|
request :head_shared_object
|
2011-06-15 02:13:18 -04:00
|
|
|
request :put_container
|
2011-06-15 17:59:06 -04:00
|
|
|
request :put_object
|
2012-10-19 01:33:20 -04:00
|
|
|
request :put_shared_object
|
2011-06-15 02:13:18 -04:00
|
|
|
|
|
|
|
module Utils
|
|
|
|
|
|
|
|
def cdn
|
2011-11-11 16:12:57 -05:00
|
|
|
unless @hp_cdn_uri.nil?
|
|
|
|
@cdn ||= Fog::CDN.new(
|
|
|
|
:provider => 'HP',
|
2013-01-22 17:28:33 -05:00
|
|
|
:hp_access_key => @hp_access_key,
|
2011-11-11 16:12:57 -05:00
|
|
|
:hp_secret_key => @hp_secret_key,
|
|
|
|
:hp_auth_uri => @hp_auth_uri,
|
2012-01-18 17:23:54 -05:00
|
|
|
:hp_cdn_uri => @hp_cdn_uri,
|
2012-04-16 17:18:58 -04:00
|
|
|
:hp_tenant_id => @hp_tenant_id,
|
2012-04-27 14:13:39 -04:00
|
|
|
:hp_avl_zone => @hp_avl_zone,
|
2012-04-16 17:18:58 -04:00
|
|
|
:connection_options => @connection_options
|
2011-11-11 16:12:57 -05:00
|
|
|
)
|
|
|
|
if @cdn.enabled?
|
|
|
|
@cdn
|
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
|
2011-07-13 18:11:08 -04:00
|
|
|
def url
|
|
|
|
"#{@scheme}://#{@host}:#{@port}#{@path}"
|
|
|
|
end
|
2011-07-13 19:25:47 -04:00
|
|
|
|
2012-10-16 21:48:50 -04:00
|
|
|
def public_url(container=nil, object=nil)
|
|
|
|
public_url = nil
|
|
|
|
unless container.nil?
|
|
|
|
if object.nil?
|
|
|
|
# return container public url
|
|
|
|
public_url = "#{url}/#{Fog::HP.escape(container)}"
|
|
|
|
else
|
|
|
|
# return object public url
|
|
|
|
public_url = "#{url}/#{Fog::HP.escape(container)}/#{Fog::HP.escape(object)}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
public_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def perm_to_acl(perm, users=[])
|
|
|
|
read_perm_acl = []
|
|
|
|
write_perm_acl = []
|
|
|
|
valid_public_perms = ['pr', 'pw', 'prw']
|
|
|
|
valid_account_perms = ['r', 'w', 'rw']
|
|
|
|
valid_perms = valid_public_perms + valid_account_perms
|
|
|
|
unless valid_perms.include?(perm)
|
|
|
|
raise ArgumentError.new("permission must be one of [#{valid_perms.join(', ')}]")
|
|
|
|
end
|
|
|
|
# tackle the public access differently
|
|
|
|
if valid_public_perms.include?(perm)
|
|
|
|
case perm
|
|
|
|
when "pr"
|
|
|
|
read_perm_acl = [".r:*",".rlistings"]
|
|
|
|
when "pw"
|
|
|
|
write_perm_acl = ["*"]
|
|
|
|
when "prw"
|
|
|
|
read_perm_acl = [".r:*",".rlistings"]
|
|
|
|
write_perm_acl = ["*"]
|
|
|
|
end
|
|
|
|
elsif valid_account_perms.include?(perm)
|
|
|
|
# tackle the user access differently
|
|
|
|
unless (users.nil? || users.empty?)
|
|
|
|
# return the correct acls
|
|
|
|
tenant_id = "*" # this might change later
|
|
|
|
acl_array = users.map { |u| "#{tenant_id}:#{u}" }
|
|
|
|
#acl_string = acl_array.join(',')
|
|
|
|
case perm
|
|
|
|
when "r"
|
|
|
|
read_perm_acl = acl_array
|
|
|
|
when "w"
|
|
|
|
write_perm_acl = acl_array
|
|
|
|
when "rw"
|
|
|
|
read_perm_acl = acl_array
|
|
|
|
write_perm_acl = acl_array
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return read_perm_acl, write_perm_acl
|
|
|
|
end
|
|
|
|
|
|
|
|
def perm_acl_to_header(read_perm_acl, write_perm_acl)
|
2011-07-13 19:25:47 -04:00
|
|
|
header = {}
|
2012-10-16 21:48:50 -04:00
|
|
|
if read_perm_acl.nil? && write_perm_acl.nil?
|
|
|
|
header = {'X-Container-Read' => "", 'X-Container-Write' => ""}
|
|
|
|
elsif !read_perm_acl.nil? && write_perm_acl.nil?
|
|
|
|
header = {'X-Container-Read' => "#{read_perm_acl.join(',')}", 'X-Container-Write' => ""}
|
|
|
|
elsif read_perm_acl.nil? && !write_perm_acl.nil?
|
|
|
|
header = {'X-Container-Read' => "", 'X-Container-Write' => "#{write_perm_acl.join(',')}"}
|
|
|
|
elsif !read_perm_acl.nil? && !write_perm_acl.nil?
|
|
|
|
header = {'X-Container-Read' => "#{read_perm_acl.join(',')}", 'X-Container-Write' => "#{write_perm_acl.join(',')}"}
|
2011-07-13 19:25:47 -04:00
|
|
|
end
|
|
|
|
header
|
|
|
|
end
|
2011-08-01 15:03:08 -04:00
|
|
|
|
2012-10-16 21:48:50 -04:00
|
|
|
def header_to_perm_acl(read_header=nil, write_header=nil)
|
|
|
|
read_h, write_h = nil
|
|
|
|
read_h = read_header.split(',') unless read_header.nil?
|
|
|
|
write_h = write_header.split(',') unless write_header.nil?
|
|
|
|
return read_h, write_h
|
2011-08-01 15:03:08 -04:00
|
|
|
end
|
2012-09-26 13:01:32 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-09-27 15:55:57 -04:00
|
|
|
# split up the storage uri
|
|
|
|
uri = URI.parse(@hp_storage_uri)
|
|
|
|
host = uri.host
|
|
|
|
path = uri.path
|
|
|
|
port = uri.port
|
|
|
|
scheme = uri.scheme
|
|
|
|
|
2012-09-26 13:01:32 -04:00
|
|
|
# do not encode before signature generation, encode after
|
2012-09-27 15:55:57 -04:00
|
|
|
sig_path = "#{path}/#{container}/#{object}"
|
|
|
|
encoded_path = "#{path}/#{Fog::HP.escape(container)}/#{Fog::HP.escape(object)}"
|
2012-09-26 13:01:32 -04:00
|
|
|
|
2012-09-27 15:55:57 -04:00
|
|
|
string_to_sign = "#{method}\n#{expires}\n#{sig_path}"
|
2013-01-25 11:24:47 -05:00
|
|
|
# Only works with 1.9+ Not compatible with 1.8.7
|
|
|
|
#signed_string = Digest::HMAC.hexdigest(string_to_sign, @hp_secret_key, Digest::SHA1)
|
|
|
|
# Compatible with 1.8.7 onwards
|
|
|
|
hmac = OpenSSL::HMAC.new(@hp_secret_key, OpenSSL::Digest::SHA1.new)
|
|
|
|
signed_string = hmac.update(string_to_sign).hexdigest
|
2012-09-26 13:01:32 -04:00
|
|
|
|
2013-01-22 17:28:33 -05:00
|
|
|
signature = @hp_tenant_id.to_s + ":" + @hp_access_key.to_s + ":" + signed_string
|
2012-09-26 13:01:32 -04:00
|
|
|
signature = Fog::HP.escape(signature)
|
|
|
|
|
|
|
|
# generate the temp url using the signature and expiry
|
2013-04-22 18:16:32 -04:00
|
|
|
"#{scheme}://#{host}:#{port}#{encoded_path}?temp_url_sig=#{signature}&temp_url_expires=#{expires}"
|
2012-09-26 13:01:32 -04:00
|
|
|
end
|
|
|
|
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Mock
|
|
|
|
include Utils
|
2011-07-29 11:33:28 -04:00
|
|
|
def self.acls(type)
|
|
|
|
type
|
|
|
|
end
|
2011-06-15 02:13:18 -04:00
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
2011-07-29 11:33:28 -04:00
|
|
|
hash[key] = {
|
|
|
|
:acls => {
|
|
|
|
:container => {},
|
|
|
|
:object => {}
|
|
|
|
},
|
|
|
|
:containers => {}
|
|
|
|
}
|
|
|
|
end
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
require 'mime/types'
|
2013-01-22 17:28:33 -05:00
|
|
|
# deprecate hp_account_id
|
|
|
|
if options[:hp_account_id]
|
|
|
|
Fog::Logger.deprecation(":hp_account_id is deprecated, please use :hp_access_key instead.")
|
|
|
|
@hp_access_key = options.delete(:hp_account_id)
|
|
|
|
end
|
|
|
|
@hp_access_key = options[:hp_access_key]
|
|
|
|
unless @hp_access_key
|
|
|
|
raise ArgumentError.new("Missing required arguments: hp_access_key. :hp_account_id is deprecated, please use :hp_access_key instead.")
|
|
|
|
end
|
2011-07-11 10:46:24 -04:00
|
|
|
@hp_secret_key = options[:hp_secret_key]
|
2012-09-27 15:55:57 -04:00
|
|
|
@hp_tenant_id = options[:hp_tenant_id]
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
2013-01-22 17:28:33 -05:00
|
|
|
self.class.data[@hp_access_key]
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
2013-01-22 17:28:33 -05:00
|
|
|
self.class.data.delete(@hp_access_key)
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
include Utils
|
|
|
|
attr_reader :hp_cdn_ssl
|
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
require 'mime/types'
|
2013-01-22 17:28:33 -05:00
|
|
|
# deprecate hp_account_id
|
|
|
|
if options[:hp_account_id]
|
|
|
|
Fog::Logger.deprecation(":hp_account_id is deprecated, please use :hp_access_key instead.")
|
|
|
|
options[:hp_access_key] = options.delete(:hp_account_id)
|
|
|
|
end
|
|
|
|
@hp_access_key = options[:hp_access_key]
|
|
|
|
unless @hp_access_key
|
|
|
|
raise ArgumentError.new("Missing required arguments: hp_access_key. :hp_account_id is deprecated, please use :hp_access_key instead.")
|
|
|
|
end
|
2011-07-11 10:46:24 -04:00
|
|
|
@hp_secret_key = options[:hp_secret_key]
|
2012-02-09 18:43:07 -05:00
|
|
|
@hp_auth_uri = options[:hp_auth_uri]
|
2011-10-21 15:51:40 -04:00
|
|
|
@hp_cdn_ssl = options[:hp_cdn_ssl]
|
2011-10-20 22:07:42 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2012-01-13 20:12:39 -05:00
|
|
|
### Set an option to use the style of authentication desired; :v1 or :v2 (default)
|
|
|
|
auth_version = options[:hp_auth_version] || :v2
|
2012-05-01 12:15:22 -04:00
|
|
|
### Pass the service name for object storage to the authentication call
|
|
|
|
options[:hp_service_type] = "Object Storage"
|
2012-01-18 17:23:54 -05:00
|
|
|
@hp_tenant_id = options[:hp_tenant_id]
|
2012-04-27 14:13:39 -04:00
|
|
|
@hp_avl_zone = options[:hp_avl_zone]
|
2011-06-15 02:13:18 -04:00
|
|
|
|
2012-01-13 20:12:39 -05:00
|
|
|
### Make the authentication call
|
|
|
|
if (auth_version == :v2)
|
|
|
|
# Call the control services authentication
|
|
|
|
credentials = Fog::HP.authenticate_v2(options, @connection_options)
|
2012-01-18 17:23:54 -05:00
|
|
|
# the CS service catalog returns the cdn endpoint
|
2012-01-19 14:50:43 -05:00
|
|
|
@hp_storage_uri = credentials[:endpoint_url]
|
2012-01-18 17:23:54 -05:00
|
|
|
@hp_cdn_uri = credentials[:cdn_endpoint_url]
|
2012-01-13 20:12:39 -05:00
|
|
|
else
|
|
|
|
# Call the legacy v1.0/v1.1 authentication
|
|
|
|
credentials = Fog::HP.authenticate_v1(options, @connection_options)
|
2012-01-18 17:23:54 -05:00
|
|
|
# the user sends in the cdn endpoint
|
2012-01-19 14:50:43 -05:00
|
|
|
@hp_storage_uri = options[:hp_auth_uri]
|
2012-01-18 17:23:54 -05:00
|
|
|
@hp_cdn_uri = options[:hp_cdn_uri]
|
2012-01-13 20:12:39 -05:00
|
|
|
end
|
2012-01-09 17:05:51 -05:00
|
|
|
|
2012-01-13 20:12:39 -05:00
|
|
|
@auth_token = credentials[:auth_token]
|
2012-01-18 17:23:54 -05:00
|
|
|
|
2012-01-19 14:50:43 -05:00
|
|
|
uri = URI.parse(@hp_storage_uri)
|
2012-05-08 15:08:03 -04:00
|
|
|
@host = uri.host
|
2011-06-15 02:13:18 -04:00
|
|
|
@path = uri.path
|
2011-10-20 22:07:42 -04:00
|
|
|
@persistent = options[:persistent] || false
|
2011-06-15 02:13:18 -04:00
|
|
|
@port = uri.port
|
|
|
|
@scheme = uri.scheme
|
2012-01-18 17:23:54 -05:00
|
|
|
|
2011-10-20 22:07:42 -04:00
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(params, parse_json = true, &block)
|
|
|
|
begin
|
|
|
|
response = @connection.request(params.merge!({
|
|
|
|
:headers => {
|
|
|
|
'Content-Type' => 'application/json',
|
2013-03-13 11:33:51 -04:00
|
|
|
'Accept' => 'application/json',
|
2011-06-15 02:13:18 -04:00
|
|
|
'X-Auth-Token' => @auth_token
|
|
|
|
}.merge!(params[:headers] || {}),
|
|
|
|
:host => @host,
|
|
|
|
:path => "#{@path}/#{params[:path]}",
|
|
|
|
}), &block)
|
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
|
|
|
raise case error
|
|
|
|
when Excon::Errors::NotFound
|
2011-10-17 15:25:07 -04:00
|
|
|
Fog::Storage::HP::NotFound.slurp(error)
|
2011-06-15 02:13:18 -04:00
|
|
|
else
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
|
2012-04-25 10:31:28 -04:00
|
|
|
response.body = Fog::JSON.decode(response.body)
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
2012-10-16 22:51:59 -04:00
|
|
|
# this request is used only for get_shared_container and get_shared_object calls
|
|
|
|
def shared_request(params, parse_json = true, &block)
|
|
|
|
begin
|
|
|
|
response = @connection.request(params.merge!({
|
|
|
|
:headers => {
|
|
|
|
'Content-Type' => 'application/json',
|
2013-03-13 11:33:51 -04:00
|
|
|
'Accept' => 'application/json',
|
2012-10-16 22:51:59 -04:00
|
|
|
'X-Auth-Token' => @auth_token
|
|
|
|
}.merge!(params[:headers] || {}),
|
|
|
|
:host => @host,
|
|
|
|
:path => "#{params[:path]}",
|
|
|
|
}), &block)
|
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
|
|
|
raise case error
|
|
|
|
when Excon::Errors::NotFound
|
|
|
|
Fog::Storage::HP::NotFound.slurp(error)
|
2012-10-19 01:33:20 -04:00
|
|
|
when Excon::Errors::Forbidden
|
|
|
|
Fog::HP::Errors::Forbidden.slurp(error)
|
2012-10-16 22:51:59 -04:00
|
|
|
else
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
|
2013-01-18 15:06:41 -05:00
|
|
|
response.body = Fog::JSON.decode(response.body)
|
2012-10-16 22:51:59 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|