mirror of
https://github.com/fog/fog-aws.git
synced 2022-11-09 13:50:52 -05:00
standardize region validation
* remove RegionMethods * static (but not immutable) list of regions
This commit is contained in:
parent
1add8cfc16
commit
ff252c3979
13 changed files with 43 additions and 67 deletions
|
@ -27,7 +27,6 @@ module Fog
|
|||
autoload :CredentialFetcher, File.expand_path('../aws/credential_fetcher', __FILE__)
|
||||
autoload :Errors, File.expand_path('../aws/errors', __FILE__)
|
||||
autoload :Mock, File.expand_path('../aws/mock', __FILE__)
|
||||
autoload :RegionMethods, File.expand_path('../aws/region_methods', __FILE__)
|
||||
autoload :SignatureV4, File.expand_path('../aws/signaturev4', __FILE__)
|
||||
|
||||
# Services
|
||||
|
@ -103,18 +102,18 @@ module Fog
|
|||
|
||||
def self.serialize_keys(key, value, options = {})
|
||||
case value
|
||||
when Hash
|
||||
value.each do | k, v |
|
||||
options.merge!(serialize_keys("#{key}.#{k}", v))
|
||||
end
|
||||
return options
|
||||
when Array
|
||||
value.each_with_index do | it, idx |
|
||||
options.merge!(serialize_keys("#{key}.member.#{(idx + 1)}", it))
|
||||
end
|
||||
return options
|
||||
else
|
||||
return {key => value}
|
||||
when Hash
|
||||
value.each do | k, v |
|
||||
options.merge!(serialize_keys("#{key}.#{k}", v))
|
||||
end
|
||||
return options
|
||||
when Array
|
||||
value.each_with_index do | it, idx |
|
||||
options.merge!(serialize_keys("#{key}.member.#{(idx + 1)}", it))
|
||||
end
|
||||
return options
|
||||
else
|
||||
return {key => value}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -173,16 +172,16 @@ module Fog
|
|||
|
||||
def self.signed_params(params, options = {})
|
||||
params.merge!({
|
||||
'AWSAccessKeyId' => options[:aws_access_key_id],
|
||||
'SignatureMethod' => 'HmacSHA256',
|
||||
'SignatureVersion' => '2',
|
||||
'Timestamp' => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||
'Version' => options[:version]
|
||||
})
|
||||
'AWSAccessKeyId' => options[:aws_access_key_id],
|
||||
'SignatureMethod' => 'HmacSHA256',
|
||||
'SignatureVersion' => '2',
|
||||
'Timestamp' => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||
'Version' => options[:version]
|
||||
})
|
||||
|
||||
params.merge!({
|
||||
'SecurityToken' => options[:aws_session_token]
|
||||
}) if options[:aws_session_token]
|
||||
'SecurityToken' => options[:aws_session_token]
|
||||
}) if options[:aws_session_token]
|
||||
|
||||
body = ''
|
||||
for key in params.keys.sort
|
||||
|
@ -223,5 +222,15 @@ module Fog
|
|||
return false unless response && response.headers
|
||||
response.get_header('Content-Type') =~ %r{application/.*json.*}i ? true : false
|
||||
end
|
||||
|
||||
def self.regions
|
||||
@regions ||= ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-central-1', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2', 'sa-east-1', 'cn-north-1']
|
||||
end
|
||||
|
||||
def self.validate_region!(region, host=nil)
|
||||
if (!host || host.end_with?('.amazonaws.com')) && !regions.include?(region)
|
||||
raise ArgumentError, "Unknown region: #{region.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -147,7 +147,7 @@ module Fog
|
|||
:body => body,
|
||||
:expects => 200,
|
||||
:idempotent => idempotent,
|
||||
:headers => headers,
|
||||
:headers => headers,
|
||||
:method => 'POST',
|
||||
:parser => parser
|
||||
})
|
||||
|
@ -253,9 +253,7 @@ module Fog
|
|||
setup_credentials(options)
|
||||
@region = options[:region] || 'us-east-1'
|
||||
|
||||
unless ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-central-1', 'eu-west-1', 'sa-east-1', 'us-east-1', 'us-west-1', 'us-west-2'].include?(@region)
|
||||
raise ArgumentError, "Unknown region: #{@region.inspect}"
|
||||
end
|
||||
Fog::AWS.validate_region!(@region)
|
||||
end
|
||||
|
||||
def region_data
|
||||
|
|
|
@ -52,9 +52,7 @@ module Fog
|
|||
|
||||
@region = options[:region] || 'us-east-1'
|
||||
|
||||
unless ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-central-1', 'eu-west-1', 'sa-east-1', 'us-east-1', 'us-west-1', 'us-west-2'].include?(@region)
|
||||
raise ArgumentError, "Unknown region: #{@region.inspect}"
|
||||
end
|
||||
Fog::AWS.validate_region!(@region)
|
||||
end
|
||||
|
||||
def data
|
||||
|
|
|
@ -185,7 +185,6 @@ module Fog
|
|||
}
|
||||
|
||||
include Fog::AWS::CredentialFetcher::ConnectionMethods
|
||||
include Fog::AWS::RegionMethods
|
||||
|
||||
def self.data
|
||||
@data ||= Hash.new do |hash, region|
|
||||
|
@ -315,7 +314,7 @@ module Fog
|
|||
@port = options[:port] || 443
|
||||
@scheme = options[:scheme] || 'https'
|
||||
end
|
||||
validate_aws_region(@host, @region)
|
||||
Fog::AWS.validate_region!(@region, @host)
|
||||
end
|
||||
|
||||
def region_data
|
||||
|
@ -429,7 +428,6 @@ module Fog
|
|||
|
||||
class Real
|
||||
include Fog::AWS::CredentialFetcher::ConnectionMethods
|
||||
include Fog::AWS::RegionMethods
|
||||
# Initialize connection to EC2
|
||||
#
|
||||
# ==== Notes
|
||||
|
@ -478,7 +476,7 @@ module Fog
|
|||
@scheme = options[:scheme] || 'https'
|
||||
end
|
||||
|
||||
validate_aws_region(@host, @region)
|
||||
Fog::AWS.validate_region!(@region, @host)
|
||||
@connection = Fog::XML::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", @persistent, @connection_options)
|
||||
end
|
||||
|
||||
|
|
|
@ -163,9 +163,7 @@ module Fog
|
|||
@use_iam_profile = options[:use_iam_profile]
|
||||
@region = options[:region] || 'us-east-1'
|
||||
|
||||
unless ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-central-1', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2', 'sa-east-1'].include?(@region)
|
||||
raise ArgumentError, "Unknown region: #{@region.inspect}"
|
||||
end
|
||||
Fog::AWS.validate_region!(@region)
|
||||
|
||||
setup_credentials(options)
|
||||
end
|
||||
|
|
|
@ -88,9 +88,7 @@ module Fog
|
|||
@region = options[:region] || 'us-east-1'
|
||||
setup_credentials(options)
|
||||
|
||||
unless ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-central-1', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2', 'sa-east-1'].include?(@region)
|
||||
raise ArgumentError, "Unknown region: #{@region.inspect}"
|
||||
end
|
||||
Fog::AWS.validate_region!(@region)
|
||||
end
|
||||
|
||||
def setup_credentials(options)
|
||||
|
|
|
@ -149,9 +149,7 @@ module Fog
|
|||
@aws_access_key_id = options[:aws_access_key_id]
|
||||
@region = options[:region] || 'us-east-1'
|
||||
|
||||
unless ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-central-1', 'eu-west-1', 'sa-east-1', 'us-east-1', 'us-west-1', 'us-west-2'].include?(@region)
|
||||
raise ArgumentError, "Unknown region: #{@region.inspect}"
|
||||
end
|
||||
Fog::AWS.validate_region!(@region)
|
||||
end
|
||||
|
||||
def data
|
||||
|
|
|
@ -49,9 +49,7 @@ module Fog
|
|||
@region = options[:region] || 'us-east-1'
|
||||
setup_credentials(options)
|
||||
|
||||
unless ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-central-1', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2', 'sa-east-1'].include?(@region)
|
||||
raise ArgumentError, "Unknown region: #{@region.inspect}"
|
||||
end
|
||||
Fog::AWS.validate_region!(@region)
|
||||
end
|
||||
|
||||
def setup_credentials(options)
|
||||
|
|
|
@ -49,9 +49,7 @@ module Fog
|
|||
@account_id = Fog::AWS::Mock.owner_id
|
||||
@module = "lambda"
|
||||
|
||||
unless ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-central-1', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2', 'sa-east-1'].include?(@region)
|
||||
raise ArgumentError, "Unknown region: #{@region.inspect}"
|
||||
end
|
||||
Fog::AWS.validate_region!(@region)
|
||||
end
|
||||
|
||||
def data
|
||||
|
|
|
@ -143,9 +143,7 @@ module Fog
|
|||
@use_iam_profile = options[:use_iam_profile]
|
||||
@region = options[:region] || 'us-east-1'
|
||||
|
||||
unless ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-central-1', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2', 'sa-east-1'].include?(@region)
|
||||
raise ArgumentError, "Unknown region: #{@region.inspect}"
|
||||
end
|
||||
Fog::AWS.validate_region!(@region)
|
||||
|
||||
setup_credentials(options)
|
||||
end
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
module Fog
|
||||
module AWS
|
||||
module RegionMethods
|
||||
def validate_aws_region(host, region)
|
||||
if host.end_with?('.amazonaws.com') && !['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2', 'sa-east-1', 'us-gov-west-1', 'eu-central-1'].include?(region)
|
||||
raise ArgumentError, "Unknown region: #{region.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -47,9 +47,7 @@ module Fog
|
|||
@account_id = Fog::AWS::Mock.owner_id
|
||||
@module = "sns"
|
||||
|
||||
unless ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-central-1', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2', 'sa-east-1'].include?(@region)
|
||||
raise ArgumentError, "Unknown region: #{@region.inspect}"
|
||||
end
|
||||
Fog::AWS.validate_region!(@region)
|
||||
end
|
||||
|
||||
def data
|
||||
|
|
|
@ -38,9 +38,7 @@ module Fog
|
|||
setup_credentials(options)
|
||||
@region = options[:region] || 'us-east-1'
|
||||
|
||||
unless ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-central-1', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2', 'sa-east-1'].include?(@region)
|
||||
raise ArgumentError, "Unknown region: #{@region.inspect}"
|
||||
end
|
||||
Fog::AWS.validate_region!(@region)
|
||||
end
|
||||
|
||||
def data
|
||||
|
|
Loading…
Reference in a new issue