Skip to content
Draft
33 changes: 31 additions & 2 deletions lib/omnibus/fetchers/net_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ def download_url
if Config.use_s3_caching
S3Cache.url_for(self)
elsif Config.use_internal_sources && !source[:internal]
raise InternalSourceMissing.new(self)
log.warn(log_key) { "Internal source missing for #{name}; falling back to external source URL." }
source[:url]
else
source[:url]
end
Expand All @@ -161,6 +162,22 @@ def download_url
#
# @return [void]
#
# def download
# log.warn(log_key) { source[:warning] } if source.key?(:warning)

# options = {}

# if source[:unsafe]
# log.warn(log_key) { "Permitting unsafe redirects!" }
# options[:allow_unsafe_redirects] = true
# end

# # Set the cookie if one was given
# options["Cookie"] = source[:cookie] if source[:cookie]
# options["Authorization"] = source[:authorization] if source[:authorization]

# download_file!(download_url, downloaded_file, options)
# end
def download
log.warn(log_key) { source[:warning] } if source.key?(:warning)

Expand All @@ -175,7 +192,19 @@ def download
options["Cookie"] = source[:cookie] if source[:cookie]
options["Authorization"] = source[:authorization] if source[:authorization]

download_file!(download_url, downloaded_file, options)
begin
# Attempt download from internal or S3 source URL
download_file!(download_url, downloaded_file, options)
rescue OpenURI::HTTPError => e
# If 404 comes from internal source, fallback to external URL
if e.io.status[0] == "404" && Config.use_internal_sources && source[:internal]
log.warn(log_key) { "Internal source returned 404 Not Found for #{name}, falling back to external source URL." }
download_file!(source[:url], downloaded_file, options)
else
# Re-raise other errors
raise
end
end
end

#
Expand Down
12 changes: 8 additions & 4 deletions spec/functional/fetchers/net_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,15 @@ def stub_ohai(options = {}, &block); end
end
end

context "when use_internal_sources is true and no internal source url" do
before { Omnibus::Config.use_internal_sources(true) }
context "when use_internal_sources is true but internal source is missing" do
before do
Config.use_internal_sources(true)
end

it "raises an exception" do
expect { fetch! }.to raise_error(InternalSourceMissing)
it "logs a warning and falls back to the external url" do
expect(subject).to receive(:log).and_call_original
url = subject.send(:download_url)
expect(url).to eq(source[:url])
end
end

Expand Down