-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1524 from ODNZSL/task/nzsl-167-presign-sign-asset…
…-requests NZSL-167: Presign asset requests
- Loading branch information
Showing
9 changed files
with
220 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module Signbank | ||
class AssetURL | ||
attr_reader :asset_url | ||
|
||
class S3Adapter | ||
cattr_accessor :region, :access_key_id, :secret_access_key, :endpoint | ||
self.region = ENV.fetch('DICTIONARY_AWS_REGION', ENV.fetch('AWS_REGION', nil)) | ||
self.access_key_id = ENV.fetch('DICTIONARY_AWS_ACCESS_KEY_ID', nil) | ||
self.secret_access_key = ENV.fetch('DICTIONARY_AWS_SECRET_ACCESS_KEY', nil) | ||
self.endpoint = 's3.amazonaws.com' | ||
|
||
def initialize(asset) | ||
@asset = asset | ||
end | ||
|
||
def self.configured? | ||
region && access_key_id && secret_access_key && client | ||
end | ||
|
||
def self.client | ||
@client ||= Aws::S3::Client.new(region:, access_key_id:, | ||
secret_access_key:) | ||
rescue Aws::Errors::MissingCredentialsError, Aws::Errors::MissingRegionError | ||
nil | ||
end | ||
|
||
def bucket_name | ||
bucket_name, hostname = @asset.asset_url.host.split('.', 2) | ||
raise ArgumentError, "Invalid hostname #{@asset.asset_url.host}" unless hostname == endpoint | ||
|
||
bucket_name | ||
end | ||
|
||
def url(expires_in: 1.hour) | ||
return unless self.class.configured? | ||
|
||
object_key = @asset.asset_url.path[1..] | ||
|
||
URI.parse( | ||
Aws::S3::Object.new(bucket_name, object_key, client: self.class.client) | ||
.presigned_url(:get, expires_in: expires_in.to_i) | ||
) | ||
end | ||
end | ||
|
||
class PassthroughUrlAdapter | ||
def initialize(asset) | ||
@asset = asset | ||
end | ||
|
||
def self.configured? | ||
true | ||
end | ||
|
||
def url(*) | ||
return unless self.class.configured? | ||
|
||
@asset.asset_url | ||
end | ||
end | ||
|
||
delegate :url, to: :@adapter | ||
|
||
def initialize(asset_url, adapter: nil) | ||
@asset_url = URI.parse(asset_url) | ||
@adapter = (adapter || [S3Adapter, PassthroughUrlAdapter].find(&:configured?)).new(self) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Signbank::AssetURL do | ||
describe '#url' do | ||
context 'when using S3Adapter' do | ||
let(:asset_url) { 'https://example.s3.amazonaws.com/assets/asset.mp4' } | ||
let(:adapter) { Signbank::AssetURL::S3Adapter } | ||
|
||
it 'returns the presigned URL for the asset' do | ||
asset = Signbank::AssetURL.new(asset_url, adapter:) | ||
presigned_url = 'https://s3.amazonaws.com/bucket-name/asset.mp4?expires=1234567890' | ||
|
||
allow(adapter).to receive(:configured?).and_return(true) | ||
allow(adapter).to receive(:client).and_return(instance_double(Aws::S3::Client)) | ||
allow_any_instance_of(Aws::S3::Object).to receive(:presigned_url).and_return(presigned_url) | ||
|
||
expect(asset.url).to eq(URI.parse(presigned_url)) | ||
end | ||
|
||
it 'returns nil if S3Adapter is not configured' do | ||
asset = Signbank::AssetURL.new(asset_url, adapter:) | ||
|
||
allow(adapter).to receive(:configured?).and_return(false) | ||
|
||
expect(asset.url).to be_nil | ||
end | ||
|
||
it 'raises an error if the URL does not have the expected hostname' do | ||
asset_url = 'https://example.com/assets/asset.mp4' | ||
asset = Signbank::AssetURL.new(asset_url, adapter:) | ||
allow(adapter).to receive(:configured?).and_return(true) | ||
|
||
expect { asset.url }.to raise_error(ArgumentError) | ||
end | ||
end | ||
|
||
context 'when using PassthroughUrlAdapter' do | ||
let(:asset_url) { URI.parse('https://example.com/assets/asset.mp4') } | ||
let(:adapter) { Signbank::AssetURL::PassthroughUrlAdapter } | ||
|
||
it 'returns the original asset URL' do | ||
asset = Signbank::AssetURL.new(asset_url.to_s, adapter:) | ||
|
||
allow(adapter).to receive(:configured?).and_return(true) | ||
|
||
expect(asset.url).to eq(asset_url) | ||
end | ||
end | ||
|
||
context 'when no adapter is specified' do | ||
let(:asset_url) { URI.parse('https://example.com/assets/asset.mp4') } | ||
|
||
it 'uses the first configured adapter' do | ||
asset = Signbank::AssetURL.new(asset_url.to_s) | ||
|
||
allow(Signbank::AssetURL::S3Adapter).to receive(:configured?).and_return(false) | ||
allow(Signbank::AssetURL::PassthroughUrlAdapter).to receive(:configured?).and_return(true) | ||
|
||
expect(asset.url).to eq(asset_url) | ||
end | ||
|
||
it 'returns nil if no adapter is configured' do | ||
asset = Signbank::AssetURL.new(asset_url.to_s) | ||
|
||
allow(Signbank::AssetURL::S3Adapter).to receive(:configured?).and_return(false) | ||
allow(Signbank::AssetURL::PassthroughUrlAdapter).to receive(:configured?).and_return(false) | ||
|
||
expect(asset.url).to be_nil | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters