Module: VagrantPlugins::S3Auth::Util

Defined in:
lib/vagrant-s3auth/util.rb

Defined Under Namespace

Classes: NullObject

Constant Summary collapse

S3_HOST_MATCHER =
/^((?<bucket>[[:alnum:]\-\.]+).)?s3([[:alnum:]\-\.]+)?\.amazonaws\.com$/
AWS_ACCESS_KEY_ENV_VARS =

The list of environment variables that the AWS Ruby SDK searches for access keys. Sadly, there’s no better way to determine which environment variable the Ruby SDK is using without mirroring the logic ourself.

See: github.com/aws/aws-sdk-ruby/blob/ab0eb18d0ce0a515254e207dae772864c34b048d/aws-sdk-core/lib/aws-sdk-core/credential_provider_chain.rb#L42

%w(AWS_ACCESS_KEY_ID AMAZON_ACCESS_KEY_ID AWS_ACCESS_KEY).freeze
DEFAULT_REGION =
'us-east-1'.freeze
LOCATION_TO_REGION =
Hash.new { |_, key| key }.merge(
  '' => DEFAULT_REGION,
  'EU' => 'eu-west-1'
)

Class Method Summary collapse

Class Method Details

.get_bucket_region(bucket) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/vagrant-s3auth/util.rb', line 67

def self.get_bucket_region(bucket)
  LOCATION_TO_REGION[
    s3_client.get_bucket_location(bucket: bucket).location_constraint
  ]
rescue ::Aws::S3::Errors::AccessDenied
  raise Errors::BucketLocationAccessDeniedError, bucket: bucket
end

.s3_client(region = DEFAULT_REGION) ⇒ Object



32
33
34
# File 'lib/vagrant-s3auth/util.rb', line 32

def self.s3_client(region = DEFAULT_REGION)
  ::Aws::S3::Client.new(region: region)
end

.s3_credential_providerObject



75
76
77
78
79
80
# File 'lib/vagrant-s3auth/util.rb', line 75

def self.s3_credential_provider
  # Providing a NullObject here is the same as instantiating a
  # client without specifying a credentials config, like we do in
  # `self.s3_client`.
  ::Aws::CredentialProviderChain.new(NullObject.new).resolve
end

.s3_object_for(url, follow_redirect = true) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vagrant-s3auth/util.rb', line 40

def self.s3_object_for(url, follow_redirect = true)
  url = URI(url)

  if url.scheme == 's3'
    bucket = url.host
    key = url.path[1..-1]
    raise Errors::MalformedShorthandURLError, url: url unless bucket && key
  elsif match = S3_HOST_MATCHER.match(url.host)
    components = url.path.split('/').delete_if(&:empty?)
    bucket = match['bucket'] || components.shift
    key = components.join('/')
  end

  if bucket && key
    s3_resource(get_bucket_region(bucket)).bucket(bucket).object(key)
  elsif follow_redirect
    response = Net::HTTP.get_response(url) rescue nil
    if response.is_a?(Net::HTTPRedirection)
      s3_object_for(response['location'], false)
    end
  end
end

.s3_resource(region = DEFAULT_REGION) ⇒ Object



36
37
38
# File 'lib/vagrant-s3auth/util.rb', line 36

def self.s3_resource(region = DEFAULT_REGION)
  ::Aws::S3::Resource.new(client: s3_client(region))
end

.s3_url_for(method, s3_object) ⇒ Object



63
64
65
# File 'lib/vagrant-s3auth/util.rb', line 63

def self.s3_url_for(method, s3_object)
  s3_object.presigned_url(method, expires_in: 60 * 10)
end