Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration option for federated credentials #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Then add a new initalizer with your AWS credentials:
S3DirectUpload.config do |c|
c.access_key_id = "" # your access key id
c.secret_access_key = "" # your secret access key
c.session_token = "" # your session token (for federated credentials)
c.bucket = "" # your bucket name
c.region = nil # region prefix of your bucket url (optional), eg. "s3-eu-west-1"
c.url = nil # S3 API endpoint (optional), eg. "https://#{c.bucket}.s3.amazonaws.com/"
Expand Down
2 changes: 1 addition & 1 deletion lib/s3_direct_upload/config_aws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module S3DirectUpload
class Config
include Singleton

ATTRIBUTES = [:access_key_id, :secret_access_key, :bucket, :prefix_to_clean, :region, :url]
ATTRIBUTES = [:access_key_id, :secret_access_key, :session_token, :bucket, :prefix_to_clean, :region, :url]

attr_accessor *ATTRIBUTES
end
Expand Down
5 changes: 4 additions & 1 deletion lib/s3_direct_upload/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def initialize(options)
@options = options.reverse_merge(
aws_access_key_id: S3DirectUpload.config.access_key_id,
aws_secret_access_key: S3DirectUpload.config.secret_access_key,
aws_session_token: S3DirectUpload.config.session_token,
bucket: S3DirectUpload.config.bucket,
region: S3DirectUpload.config.region || "s3",
url: S3DirectUpload.config.url,
Expand Down Expand Up @@ -52,7 +53,8 @@ def fields
:policy => policy,
:signature => signature,
:success_action_status => "201",
'X-Requested-With' => 'xhr'
'X-Requested-With' => 'xhr',
'x-amz-security-token' => @options[:aws_session_token]
}
end

Expand All @@ -75,6 +77,7 @@ def policy_data
["starts-with", "$utf8", ""],
["starts-with", "$key", @options[:key_starts_with]],
["starts-with", "$x-requested-with", ""],
["starts-with", "$x-amz-security-token", ""],
["content-length-range", 0, @options[:max_file_size]],
["starts-with","$content-type", @options[:content_type_starts_with] ||""],
{bucket: @options[:bucket]},
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/s3_direct_upload.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace :s3_direct_upload do
require 'thread'
require 'fog'

s3 = Fog::Storage::AWS.new(aws_access_key_id: S3DirectUpload.config.access_key_id, aws_secret_access_key: S3DirectUpload.config.secret_access_key)
s3 = Fog::Storage::AWS.new(aws_access_key_id: S3DirectUpload.config.access_key_id, aws_secret_access_key: S3DirectUpload.config.secret_access_key, aws_session_token: S3DirectUpload.config.session_token)
bucket = S3DirectUpload.config.bucket
prefix = S3DirectUpload.config.prefix_to_clean || "uploads/#{2.days.ago.strftime('%Y%m%d')}"

Expand Down
7 changes: 7 additions & 0 deletions spec/helpers/form_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
s3_uploader.policy_data[:conditions].should include ["starts-with", "$content-type", ""]
end
end

describe "starts-with $x-amz-security-token" do
it "is defaults to an empty string" do
s3_uploader = S3DirectUpload::UploadHelper::S3Uploader.new({})
s3_uploader.policy_data[:conditions].should include ["starts-with", "$x-amz-security-token", ""]
end
end
end

end