Skip to content

Commit

Permalink
add support for aliases in s3 credentials YAML (kreeti#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbhawsingka authored Feb 18, 2020
1 parent b9563df commit 253bdc9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/paperclip/storage/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,9 @@ def copy_to_local_file(style, local_dest_path)
def find_credentials(creds)
case creds
when File
YAML::safe_load(ERB.new(File.read(creds.path)).result)
YAML::safe_load(ERB.new(File.read(creds.path)).result, [], [], true)
when String, Pathname
YAML::safe_load(ERB.new(File.read(creds)).result)
YAML::safe_load(ERB.new(File.read(creds)).result, [], [], true)
when Hash
creds
when NilClass
Expand Down
53 changes: 53 additions & 0 deletions spec/paperclip/storage/s3_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,32 @@ class DummyCredentialProvider; end
end
end

context "with S3 credentials supplied as Pathname and aliases being set" do
before do
ENV["S3_KEY"] = "pathname_key"
ENV["S3_BUCKET"] = "pathname_bucket"
ENV["S3_SECRET"] = "pathname_secret"

rails_env("test") do
rebuild_model aws2_add_region.merge storage: :s3,
s3_credentials: Pathname.new(fixture_file("aws_s3.yml"))

Dummy.delete_all
@dummy = Dummy.new
end
end

it "parses the credentials" do
assert_equal "pathname_bucket", @dummy.avatar.bucket_name

assert_equal "pathname_key",
@dummy.avatar.s3_bucket.client.config.access_key_id

assert_equal "pathname_secret",
@dummy.avatar.s3_bucket.client.config.secret_access_key
end
end

context "with S3 credentials in a YAML file" do
before do
ENV["S3_KEY"] = "env_key"
Expand Down Expand Up @@ -1466,6 +1492,33 @@ class DummyCredentialProvider; end
end
end

context "with S3 credentials in a YAML file and aliases being set" do
before do
ENV["S3_KEY"] = "env_key"
ENV["S3_BUCKET"] = "env_bucket"
ENV["S3_SECRET"] = "env_secret"

rails_env("test") do
rebuild_model aws2_add_region.merge storage: :s3,
s3_credentials: File.new(fixture_file("aws_s3.yml"))

Dummy.delete_all

@dummy = Dummy.new
end
end

it "runs the file through ERB" do
assert_equal "env_bucket", @dummy.avatar.bucket_name

assert_equal "env_key",
@dummy.avatar.s3_bucket.client.config.access_key_id

assert_equal "env_secret",
@dummy.avatar.s3_bucket.client.config.secret_access_key
end
end

context "S3 Permissions" do
context "defaults to :public_read" do
before do
Expand Down
13 changes: 13 additions & 0 deletions spec/support/fixtures/aws_s3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
default: &default
acl: public-read
development:
<<: *default
key: 54321
production:
<<: *default
key: 12345
test:
<<: *default
bucket: <%= ENV['S3_BUCKET'] %>
access_key_id: <%= ENV['S3_KEY'] %>
secret_access_key: <%= ENV['S3_SECRET'] %>

0 comments on commit 253bdc9

Please sign in to comment.