Skip to content

Commit

Permalink
Merge pull request #37 from Rigor/rs-better-querystrings
Browse files Browse the repository at this point in the history
Introduce a new pattern for passing querystring values as a hash
  • Loading branch information
dengqinsi authored Mar 11, 2019
2 parents 919dd13 + d7b2cfd commit 6f4d171
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
25 changes: 22 additions & 3 deletions lib/fog/aliyun/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,20 @@ def defaultAliyunUri(action, sigNonce, time)
'?Format=JSON&AccessKeyId=' + @aliyun_accesskey_id + '&Action=' + action + '&SignatureMethod=HMAC-SHA1&RegionId=' + @aliyun_region_id + '&SignatureNonce=' + sigNonce + '&SignatureVersion=1.0&Version=2014-05-26&Timestamp=' + urlTimeFormat
end

def defaultAliyunQueryParameters(action, sigNonce, time)
{
Format: 'JSON',
AccessKeyId: @aliyun_accesskey_id,
Action: action,
SignatureMethod: 'HMAC-SHA1',
RegionId: @aliyun_region_id,
SignatureNonce: sigNonce,
SignatureVersion: '1.0',
Version: '2014-05-26',
Timestamp: time.strftime('%Y-%m-%dT%H:%M:%SZ')
}
end

def defaultAliyunVPCUri(action, sigNonce, time)
parTimeFormat = time.strftime('%Y-%m-%dT%H:%M:%SZ')
urlTimeFormat = URI.encode(parTimeFormat, ':')
Expand Down Expand Up @@ -399,7 +413,14 @@ def defalutVPCParameters(action, sigNonce, time)
end

# compute signature
# This method should be considered deprecated and replaced with sign_without_encoding, which is better for using querystring hashes and not
# building querystrings with string concatination.
def sign(accessKeySecret, parameters)
signature = sign_without_encoding(accessKeySecret, parameters)
URI.encode(signature, '/[^!*\'()\;?:@#&%=+$,{}[]<>`" ')
end

def sign_without_encoding(accessKeySecret, parameters)
sortedParameters = parameters.sort
canonicalizedQueryString = ''
sortedParameters.each do |k, v|
Expand All @@ -414,9 +435,7 @@ def sign(accessKeySecret, parameters)
digest = OpenSSL::HMAC.digest(digVer, key, stringToSign)
signature = Base64.encode64(digest)
signature[-1] = ''
encodedSig = URI.encode(signature, '/[^!*\'()\;?:@#&%=+$,{}[]<>`" ')

encodedSig
signature
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fog/aliyun/models/compute/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Server < Fog::Compute::Server
attribute :expired_at, aliases: 'ExpiredTime'

def image
requires image_id
requires :image_id
Fog::Compute::Aliyun::Image.new(service: service).all(imageId: image_id)[0]
end

Expand Down
2 changes: 1 addition & 1 deletion lib/fog/aliyun/requests/compute/list_server_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def get_instance_type(cpuCount, memorySize)
)

_InstanceTypeId = nil
_InstanceTypeList = Fog::JSON.decode(response.body)['InstanceTypes']['InstanceType']
_InstanceTypeList = response.body['InstanceTypes']['InstanceType']
_InstanceTypeList.each do |instance_type|
next unless (instance_type['CpuCoreCount'] == cpuCount) && (instance_type['MemorySize'] == memorySize)
_InstanceTypeId = instance_type['InstanceTypeId']
Expand Down
18 changes: 9 additions & 9 deletions lib/fog/aliyun/requests/compute/list_servers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def list_servers(options = {})
_time = Time.new.utc

_parameters = defalutParameters(_action, _sigNonce, _time)
_pathURL = defaultAliyunUri(_action, _sigNonce, _time)
_query_parameters = defaultAliyunQueryParameters(_action, _sigNonce, _time)

_InstanceId = options[:instanceId]
_VpcId = options[:vpcId]
Expand All @@ -22,35 +22,35 @@ def list_servers(options = {})
unless _InstanceId.nil?
_InstanceStr = "[\"#{_InstanceId}\"]"
_parameters['InstanceIds'] = _InstanceStr
_pathURL += '&InstanceIds=' + _InstanceStr
_query_parameters[:InstanceIds] = _InstanceStr
end

unless _VpcId.nil?
_parameters['VpcId'] = _VpcId
_pathURL += '&VpcId=' + _VpcId
_query_parameters[:VpcId] = _VpcId
end

unless _SecurityGroupId.nil?
_parameters['SecurityGroupId'] = _SecurityGroupId
_pathURL += '&SecurityGroupId=' + _SecurityGroupId
_query_parameters[:SecurityGroupId] = _SecurityGroupId
end

unless _PageNumber.nil?
_parameters['PageNumber'] = _PageNumber
_pathURL += '&PageNumber=' + _PageNumber
_query_parameters[:PageNumber] = _PageNumber
end

_PageSize ||= '50'
_parameters['PageSize'] = _PageSize
_pathURL += '&PageSize=' + _PageSize
_query_parameters[:PageSize] = _PageSize

_signature = sign(@aliyun_accesskey_secret, _parameters)
_pathURL += '&Signature=' + _signature
_signature = sign_without_encoding(@aliyun_accesskey_secret, _parameters)
_query_parameters[:Signature] = _signature

request(
expects: [200, 203],
method: 'GET',
path: _pathURL
query: _query_parameters
)
end
end
Expand Down

0 comments on commit 6f4d171

Please sign in to comment.