mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
first pass at query param signed s3 requests
This commit is contained in:
parent
4d8cb107a4
commit
6cfcfd1edf
6 changed files with 64 additions and 15 deletions
|
@ -42,6 +42,10 @@ module Fog
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_url(key, expires)
|
||||||
|
connection.get_object_url(bucket.name, key, expires)
|
||||||
|
end
|
||||||
|
|
||||||
def head(key, options = {})
|
def head(key, options = {})
|
||||||
data = connection.head_object(bucket.name, key, options)
|
data = connection.head_object(bucket.name, key, options)
|
||||||
object_data = {
|
object_data = {
|
||||||
|
|
|
@ -44,6 +44,21 @@ unless Fog.mocking?
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_object_url(bucket_name, object_name, expires)
|
||||||
|
unless bucket_name
|
||||||
|
raise ArgumentError.new('bucket_name is required')
|
||||||
|
end
|
||||||
|
unless object_name
|
||||||
|
raise ArgumentError.new('object_name is required')
|
||||||
|
end
|
||||||
|
url({
|
||||||
|
:headers => {},
|
||||||
|
:host => "#{bucket_name}.#{@host}",
|
||||||
|
:method => 'GET',
|
||||||
|
:path => object_name
|
||||||
|
}, expires)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -100,7 +100,33 @@ module Fog
|
||||||
|
|
||||||
def request(params)
|
def request(params)
|
||||||
params[:headers]['Date'] = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000")
|
params[:headers]['Date'] = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000")
|
||||||
|
params[:headers]['Authorization'] = "AWS #{@aws_access_key_id}:#{signature(params)}"
|
||||||
|
|
||||||
|
response = @connection.request({
|
||||||
|
:block => params[:block],
|
||||||
|
:body => params[:body],
|
||||||
|
:expects => params[:expects],
|
||||||
|
:headers => params[:headers],
|
||||||
|
:host => params[:host],
|
||||||
|
:method => params[:method],
|
||||||
|
:parser => params[:parser],
|
||||||
|
:path => params[:path],
|
||||||
|
:query => params[:query]
|
||||||
|
})
|
||||||
|
|
||||||
|
response
|
||||||
|
end
|
||||||
|
|
||||||
|
def url(params, expires)
|
||||||
|
params[:headers]['Date'] = expires.to_i
|
||||||
|
query = [params[:query]].compact
|
||||||
|
query << "AWSAccessKeyId=#{@aws_access_key_id}"
|
||||||
|
query << "Signature=#{CGI.escape(signature(params))}"
|
||||||
|
query << "Expires=#{params[:headers]['Date']}"
|
||||||
|
"http://#{params[:host]}/#{params[:path]}?#{query.join('&')}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def signature(params)
|
||||||
string_to_sign =
|
string_to_sign =
|
||||||
<<-DATA
|
<<-DATA
|
||||||
#{params[:method]}
|
#{params[:method]}
|
||||||
|
@ -145,21 +171,6 @@ DATA
|
||||||
|
|
||||||
hmac = @hmac.update(string_to_sign)
|
hmac = @hmac.update(string_to_sign)
|
||||||
signature = Base64.encode64(hmac.digest).chomp!
|
signature = Base64.encode64(hmac.digest).chomp!
|
||||||
params[:headers]['Authorization'] = "AWS #{@aws_access_key_id}:#{signature}"
|
|
||||||
|
|
||||||
response = @connection.request({
|
|
||||||
:block => params[:block],
|
|
||||||
:body => params[:body],
|
|
||||||
:expects => params[:expects],
|
|
||||||
:headers => params[:headers],
|
|
||||||
:host => params[:host],
|
|
||||||
:method => params[:method],
|
|
||||||
:parser => params[:parser],
|
|
||||||
:path => params[:path],
|
|
||||||
:query => params[:query]
|
|
||||||
})
|
|
||||||
|
|
||||||
response
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -90,6 +90,18 @@ describe 'Fog::AWS::S3::Objects' do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#get_url" do
|
||||||
|
|
||||||
|
it "should return a signed expiring url" do
|
||||||
|
file = File.open(File.dirname(__FILE__) + '/../../../lorem.txt', 'r')
|
||||||
|
object = @bucket.objects.create(:key => 'fogobjectname', :body => file)
|
||||||
|
url = @bucket.objects.get_url('fogobjectname', Time.now + 60 * 10)
|
||||||
|
open(url).read.should == File.open(File.dirname(__FILE__) + '/../../../lorem.txt', 'r').read
|
||||||
|
object.destroy
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
describe "#head" do
|
describe "#head" do
|
||||||
|
|
||||||
it "should return a Fog::AWS::S3::Object with metadata" do
|
it "should return a Fog::AWS::S3::Object with metadata" do
|
||||||
|
|
|
@ -35,6 +35,12 @@ describe 'S3.get_object' do
|
||||||
data.should == file.read
|
data.should == file.read
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should return a signed expiring url' do
|
||||||
|
url = s3.get_object_url('foggetobject', 'fog_get_object', Time.now + 60 * 10)
|
||||||
|
file = File.open(File.dirname(__FILE__) + '/../../../lorem.txt', 'r')
|
||||||
|
open(url).read.should == file.read
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
describe 'failure' do
|
describe 'failure' do
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require 'spec'
|
require 'spec'
|
||||||
|
require 'open-uri'
|
||||||
|
|
||||||
current_directory = File.dirname(__FILE__)
|
current_directory = File.dirname(__FILE__)
|
||||||
require "#{current_directory}/../lib/fog"
|
require "#{current_directory}/../lib/fog"
|
||||||
|
|
Loading…
Reference in a new issue