Class: Vagrant::Util::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-s3auth/extension/downloader.rb

Instance Method Summary collapse

Instance Method Details

#execute_curl_with_s3auth(options, subprocess_options, &data_proc) ⇒ Object Also known as: execute_curl



45
46
47
48
49
# File 'lib/vagrant-s3auth/extension/downloader.rb', line 45

def execute_curl_with_s3auth(options, subprocess_options, &data_proc)
  execute_curl_without_s3auth(options, subprocess_options, &data_proc)
rescue Errors::DownloaderError => e
  s3auth_download(options, subprocess_options, &data_proc) || (raise e)
end

#s3auth_download(options, subprocess_options, &data_proc) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vagrant-s3auth/extension/downloader.rb', line 11

def s3auth_download(options, subprocess_options, &data_proc)
  # The URL sent to curl is always the last argument. We have to rely
  # on this implementation detail because we need to hook into both
  # HEAD and GET requests.
  url = options.last

  s3_object = S3Auth::Util.s3_object_for(url)
  return unless s3_object

  @logger.info("s3auth: Discovered S3 URL: #{@source}")
  @logger.debug("s3auth: Bucket: #{s3_object.bucket.name.inspect}")
  @logger.debug("s3auth: Key: #{s3_object.key.inspect}")

  method = options.any? { |o| o == '-I' } ? :head : :get

  @logger.info("s3auth: Generating signed URL for #{method.upcase}")

  url.replace(S3Auth::Util.s3_url_for(method, s3_object).to_s)

  execute_curl_without_s3auth(options, subprocess_options, &data_proc)
rescue Errors::DownloaderError => e
  if e.message =~ /403 Forbidden/
    e.message << "\n\n"
    e.message << I18n.t('vagrant_s3auth.errors.box_download_forbidden',
      access_key: ENV['AWS_ACCESS_KEY_ID'],
      bucket: s3_object && s3_object.bucket.name)
  end
  raise
rescue ::Aws::Errors::MissingCredentialsError
  raise S3Auth::Errors::MissingCredentialsError
rescue ::Aws::Errors::ServiceError => e
  raise S3Auth::Errors::S3APIError, error: e
end