2014-02-01 21:13:17 -05:00
require 'fog/google/core'
2011-08-31 16:52:53 -04:00
2010-09-18 13:40:48 -04:00
module Fog
2011-06-15 17:26:43 -04:00
module Storage
class Google < Fog :: Service
2010-12-16 18:31:24 -05:00
requires :google_storage_access_key_id , :google_storage_secret_access_key
2014-04-19 18:59:01 -04:00
recognizes :host , :port , :scheme , :persistent , :path_style
2010-09-18 13:40:48 -04:00
2011-08-24 14:55:35 -04:00
model_path 'fog/google/models/storage'
2010-09-18 13:40:48 -04:00
collection :directories
model :directory
collection :files
model :file
2011-08-24 14:55:35 -04:00
request_path 'fog/google/requests/storage'
2010-09-18 13:40:48 -04:00
request :copy_object
request :delete_bucket
request :delete_object
request :get_bucket
request :get_bucket_acl
request :get_object
request :get_object_acl
request :get_object_torrent
2011-06-27 17:50:32 -04:00
request :get_object_http_url
request :get_object_https_url
2010-09-18 13:40:48 -04:00
request :get_object_url
request :get_service
request :head_object
request :put_bucket
request :put_bucket_acl
request :put_object
2014-01-09 14:23:31 -05:00
request :put_object_acl
2010-09-18 13:40:48 -04:00
request :put_object_url
module Utils
2011-06-27 17:50:32 -04:00
def http_url ( params , expires )
" http:// " << host_path_query ( params , expires )
end
def https_url ( params , expires )
" https:// " << host_path_query ( params , expires )
end
2010-09-18 13:40:48 -04:00
def url ( params , expires )
2011-10-19 15:49:34 -04:00
Fog :: Logger . deprecation ( " Fog::Storage::Google => # url is deprecated, use # https_url instead [light_black]( #{ caller . first } )[/] " )
2011-06-27 17:50:32 -04:00
https_url ( params , expires )
end
private
def host_path_query ( params , expires )
2010-09-18 13:40:48 -04:00
params [ :headers ] [ 'Date' ] = expires . to_i
2011-01-26 17:38:00 -05:00
params [ :path ] = CGI . escape ( params [ :path ] ) . gsub ( '%2F' , '/' )
2010-09-18 13:40:48 -04:00
query = [ params [ :query ] ] . compact
2013-03-09 10:48:39 -05:00
query << " GoogleAccessId= #{ @google_storage_access_key_id } "
2010-09-18 13:40:48 -04:00
query << " Signature= #{ CGI . escape ( signature ( params ) ) } "
query << " Expires= #{ params [ :headers ] [ 'Date' ] } "
2011-06-27 17:50:32 -04:00
" #{ params [ :host ] } / #{ params [ :path ] } ? #{ query . join ( '&' ) } "
2010-09-18 13:40:48 -04:00
end
2013-10-20 17:28:04 -04:00
def request_params ( params )
subdomain = params [ :host ] . split ( " . #{ @host } " ) . first
2014-04-19 18:59:01 -04:00
if @path_style or subdomain !~ / ^(?!goog)(?:[a-z]| \ d(?! \ d{0,2}(?: \ . \ d{1,3}){3}$))(?:[a-z0-9]| \ .(?![ \ . \ -])| \ -(?![ \ .])){1,61}[a-z0-9]$ /
2013-10-20 17:28:04 -04:00
if subdomain =~ / _ /
# https://github.com/fog/fog/pull/1258#issuecomment-10248620.
Fog :: Logger . warning ( " fog: the specified google storage bucket name ( #{ subdomain } ) is not DNS compliant (only characters a through z, digits 0 through 9, and the hyphen). " )
else
# - Bucket names must contain only lowercase letters, numbers, dashes (-), underscores (_), and dots (.). Names containing dots require verification.
# - Bucket names must start and end with a number or letter.
# - Bucket names must contain 3 to 63 characters. Names containing dots can contain up to 222 characters, but each dot-separated component can be no longer than 63 characters.
# - Bucket names cannot be represented as an IP address in dotted-decimal notation (for example, 192.168.5.4).
# - Bucket names cannot begin with the "goog" prefix.
# - Also, for DNS compliance, you should not have a period adjacent to another period or dash. For example, ".." or "-." or ".-" are not acceptable.
2014-04-19 18:59:01 -04:00
Fog :: Logger . warning ( " fog: the specified google storage bucket name ( #{ subdomain } ) is not a valid dns name. See: https://developers.google.com/storage/docs/bucketnaming " ) unless @path_style
2013-10-20 17:28:04 -04:00
end
2014-05-26 09:44:18 -04:00
2013-10-20 17:28:04 -04:00
params [ :host ] = params [ :host ] . split ( " #{ subdomain } . " ) [ - 1 ]
if params [ :path ]
params [ :path ] = " #{ subdomain } / #{ params [ :path ] } "
else
params [ :path ] = " #{ subdomain } "
end
subdomain = nil
end
if subdomain && subdomain != @host
params [ :subdomain ] = subdomain
end
params [ :scheme ] || = @scheme
params [ :port ] || = @port
params
end
2010-09-18 13:40:48 -04:00
end
class Mock
include Utils
2010-11-18 17:17:11 -05:00
def self . acls ( type )
case type
when 'private'
2011-01-21 23:45:37 -05:00
{
2010-11-18 17:17:11 -05:00
" AccessControlList " = > [
{
" Permission " = > " FULL_CONTROL " ,
" Scope " = > { " ID " = > " 2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0 " , " type " = > " UserById " }
}
] ,
" Owner " = > { " ID " = > " 2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0 " }
}
when 'public-read'
2011-01-21 23:45:37 -05:00
{
" AccessControlList " = > [
{
" Permission " = > " FULL_CONTROL " ,
" Scope " = > { " ID " = > " 2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0 " , " type " = > " UserById " }
} ,
{
" Permission " = > " READ " ,
" Scope " = > { " type " = > " AllUsers " }
}
] ,
" Owner " = > { " ID " = > " 2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0 " }
}
2010-11-18 17:17:11 -05:00
when 'public-read-write'
2011-01-21 23:45:37 -05:00
{
" AccessControlList " = > [
{
" Permission " = > " FULL_CONTROL " ,
" Scope " = > { " ID " = > " 2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0 " , " type " = > " UserById " }
} ,
{
" Permission " = > " READ " ,
" Scope " = > { " type " = > " AllUsers " }
} ,
{
" Permission " = > " WRITE " ,
" Scope " = > { " type " = > " AllUsers " }
}
] ,
" Owner " = > { " ID " = > " 2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0 " }
}
2010-11-18 17:17:11 -05:00
when 'authenticated-read'
2011-01-21 23:45:37 -05:00
{
" AccessControlList " = > [
{
" Permission " = > " FULL_CONTROL " ,
" Scope " = > { " ID " = > " 2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0 " , " type " = > " UserById " }
} ,
{
" Permission " = > " READ " ,
" Scope " = > { " type " = > " AllAuthenticatedUsers " }
}
] ,
" Owner " = > { " ID " = > " 2744ccd10c7533bd736ad890f9dd5cab2adb27b07d500b9493f29cdc420cb2e0 " }
}
2010-11-18 17:17:11 -05:00
end
end
2010-09-18 13:40:48 -04:00
def self . data
@data || = Hash . new do | hash , key |
hash [ key ] = {
2010-11-18 18:54:54 -05:00
:acls = > {
:bucket = > { } ,
:object = > { }
} ,
2010-09-18 13:40:48 -04:00
:buckets = > { }
}
end
end
2011-05-19 18:35:33 -04:00
def self . reset
@data = nil
end
2010-09-18 13:40:48 -04:00
def initialize ( options = { } )
@google_storage_access_key_id = options [ :google_storage_access_key_id ]
2011-03-10 14:16:55 -05:00
end
2011-05-19 18:35:33 -04:00
def data
self . class . data [ @google_storage_access_key_id ]
2011-05-19 10:05:33 -04:00
end
2011-03-10 14:16:55 -05:00
def reset_data
self . class . data . delete ( @google_storage_access_key_id )
2010-09-18 13:40:48 -04:00
end
def signature ( params )
" foo "
end
end
class Real
include Utils
2010-09-24 09:43:34 -04:00
# Initialize connection to Google Storage
2010-09-18 13:40:48 -04:00
#
# ==== Notes
# options parameter must include values for :google_storage_access_key_id and
# :google_storage_secret_access_key in order to create a connection
#
# ==== Examples
2010-09-24 09:43:34 -04:00
# google_storage = Storage.new(
2010-09-18 13:40:48 -04:00
# :google_storage_access_key_id => your_google_storage_access_key_id,
# :google_storage_secret_access_key => your_google_storage_secret_access_key
# )
#
# ==== Parameters
# * options<~Hash> - config arguments for connection. Defaults to {}.
#
# ==== Returns
2010-09-24 09:43:34 -04:00
# * Storage object with connection to google.
2010-09-18 13:40:48 -04:00
def initialize ( options = { } )
2011-02-16 20:25:50 -05:00
require 'fog/core/parser'
2010-09-18 13:40:48 -04:00
@google_storage_access_key_id = options [ :google_storage_access_key_id ]
@google_storage_secret_access_key = options [ :google_storage_secret_access_key ]
2011-09-12 11:01:48 -04:00
@connection_options = options [ :connection_options ] || { }
2010-09-18 13:40:48 -04:00
@hmac = Fog :: HMAC . new ( 'sha1' , @google_storage_secret_access_key )
2012-09-11 20:58:06 -04:00
@host = options [ :host ] || 'storage.googleapis.com'
2012-06-21 07:56:46 -04:00
@persistent = options . fetch ( :persistent , true )
2011-09-12 11:01:48 -04:00
@port = options [ :port ] || 443
@scheme = options [ :scheme ] || 'https'
2014-04-19 18:59:01 -04:00
@path_style = options [ :path_style ] || false
2010-09-18 13:40:48 -04:00
end
def reload
2013-10-20 17:28:04 -04:00
@connection . reset if @connection
2010-09-18 13:40:48 -04:00
end
def signature ( params )
string_to_sign =
<<-DATA
#{params[:method]}
#{params[:headers]['Content-MD5']}
#{params[:headers]['Content-Type']}
#{params[:headers]['Date']}
DATA
google_headers , canonical_google_headers = { } , ''
for key , value in params [ :headers ]
2010-09-18 15:57:37 -04:00
if key [ 0 .. 6 ] == 'x-goog-'
2010-09-18 13:40:48 -04:00
google_headers [ key ] = value
end
end
2010-09-18 15:57:37 -04:00
2010-09-18 13:40:48 -04:00
google_headers = google_headers . sort { | x , y | x [ 0 ] < = > y [ 0 ] }
for key , value in google_headers
canonical_google_headers << " #{ key } : #{ value } \n "
end
string_to_sign << " #{ canonical_google_headers } "
canonical_resource = " / "
2013-10-20 17:28:04 -04:00
if subdomain = params . delete ( :subdomain )
2010-09-18 13:40:48 -04:00
canonical_resource << " #{ CGI . escape ( subdomain ) . downcase } / "
end
canonical_resource << " #{ params [ :path ] } "
canonical_resource << '?'
for key in ( params [ :query ] || { } ) . keys
2012-10-15 19:00:55 -04:00
if [ 'acl' , 'cors' , 'location' , 'logging' , 'requestPayment' , 'torrent' , 'versions' , 'versioning' ] . include? ( key )
2010-09-18 13:40:48 -04:00
canonical_resource << " #{ key } & "
end
end
canonical_resource . chop!
string_to_sign << " #{ canonical_resource } "
signed_string = @hmac . sign ( string_to_sign )
2012-05-14 14:58:42 -04:00
Base64 . encode64 ( signed_string ) . chomp!
2010-09-18 13:40:48 -04:00
end
2011-02-08 13:06:44 -05:00
2013-10-20 17:28:04 -04:00
def connection ( scheme , host , port )
uri = " #{ scheme } :// #{ host } : #{ port } "
if @persistent
unless uri == @connection_uri
@connection_uri = uri
reload
@connection = nil
end
else
@connection = nil
end
2014-02-26 19:50:35 -05:00
@connection || = Fog :: XML :: Connection . new ( uri , @persistent , @connection_options )
2013-10-20 17:28:04 -04:00
end
2011-02-08 13:06:44 -05:00
private
def request ( params , & block )
2013-10-20 17:28:04 -04:00
params = request_params ( params )
scheme = params . delete ( :scheme )
host = params . delete ( :host )
port = params . delete ( :port )
2011-02-08 13:06:44 -05:00
params [ :headers ] [ 'Date' ] = Fog :: Time . now . to_date_header
params [ :headers ] [ 'Authorization' ] = " GOOG1 #{ @google_storage_access_key_id } : #{ signature ( params ) } "
2013-10-20 17:28:04 -04:00
connection ( scheme , host , port ) . request ( params , & block )
2011-02-08 13:06:44 -05:00
end
2010-09-18 13:40:48 -04:00
end
end
end
end