From e571779f5aac5ca13b5019467cb2dec76a3ab533 Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Sun, 24 Jun 2012 11:58:32 +0100 Subject: [PATCH] support fog's new use_iam_profile option --- lib/dragonfly/data_storage/s3data_store.rb | 17 ++++++++++++----- .../data_storage/s3_data_store_spec.rb | 10 ++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/dragonfly/data_storage/s3data_store.rb b/lib/dragonfly/data_storage/s3data_store.rb index 7e7ee66e3..a539a3820 100644 --- a/lib/dragonfly/data_storage/s3data_store.rb +++ b/lib/dragonfly/data_storage/s3data_store.rb @@ -12,6 +12,7 @@ class S3DataStore configurable_attr :access_key_id configurable_attr :secret_access_key configurable_attr :region + configurable_attr :use_iam_profile configurable_attr :use_filesystem, true configurable_attr :storage_headers, {'x-amz-acl' => 'public-read'} configurable_attr :url_scheme, 'http' @@ -33,6 +34,7 @@ def initialize(opts={}) self.access_key_id = opts[:access_key_id] self.secret_access_key = opts[:secret_access_key] self.region = opts[:region] + self.use_iam_profile = opts[:use_iam_profile] end def store(temp_object, opts={}) @@ -96,12 +98,13 @@ def domain def storage @storage ||= begin - storage = Fog::Storage.new( + storage = Fog::Storage.new({ :provider => 'AWS', :aws_access_key_id => access_key_id, :aws_secret_access_key => secret_access_key, - :region => region - ) + :region => region, + :use_iam_profile => use_iam_profile + }.reject {|name, option| option.nil?}) storage.sync_clock storage end @@ -118,8 +121,12 @@ def bucket_exists? def ensure_configured unless @configured - [:bucket_name, :access_key_id, :secret_access_key].each do |attr| - raise NotConfigured, "You need to configure #{self.class.name} with #{attr}" if send(attr).nil? + if use_iam_profile + raise NotConfigured, "You need to configure #{self.class.name} with #{attr}" if bucket_name.nil? + else + [:bucket_name, :access_key_id, :secret_access_key].each do |attr| + raise NotConfigured, "You need to configure #{self.class.name} with #{attr}" if send(attr).nil? + end end @configured = true end diff --git a/spec/dragonfly/data_storage/s3_data_store_spec.rb b/spec/dragonfly/data_storage/s3_data_store_spec.rb index 8b5139b45..43defaf5a 100644 --- a/spec/dragonfly/data_storage/s3_data_store_spec.rb +++ b/spec/dragonfly/data_storage/s3_data_store_spec.rb @@ -176,6 +176,16 @@ @data_store.secret_access_key = nil proc{ @data_store.retrieve('asdf') }.should raise_error(Dragonfly::Configurable::NotConfigured) end + + if !enabled #this will fail since the specs are not running on an ec2 instance with an iam role defined + it 'should allow missing secret key and access key on store if iam profiles are allowed' do + @data_store.use_iam_profile = true + @data_store.secret_access_key = nil + @data_store.access_key_id = nil + proc{ @data_store.store(@temp_object) }.should_not raise_error(Dragonfly::Configurable::NotConfigured) + end + end + end describe "autocreating the bucket" do