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

Switch off open-uri for community site downloads #1697

Merged
merged 2 commits into from May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 4 additions & 35 deletions lib/berkshelf/community_rest.rb
@@ -1,4 +1,4 @@
require "open-uri"
require "berkshelf/streaming_file_adapter"
require "retryable"
require "mixlib/archive"

Expand Down Expand Up @@ -80,7 +80,7 @@ def initialize(uri = V1_API, options = {})
interval: @retry_interval,
exceptions: [Faraday::Error::TimeoutError]

b.adapter :httpclient
b.use Berkshelf::StreamingFileAdapter
end

super(api_uri, options)
Expand Down Expand Up @@ -189,44 +189,13 @@ def stream(target)
local = Tempfile.new("community-rest-stream")
local.binmode

Retryable.retryable(tries: retries, on: OpenURI::HTTPError, sleep: retry_interval) do
open(target, "rb", open_uri_options(target)) do |remote|
local.write(remote.read)
end
Retryable.retryable(tries: retries, on: Faraday::Error::ConnectionFailed, sleep: retry_interval) do
get(target, nil, streaming_file: local)
end

local
ensure
local.close(false) unless local.nil?
end

private

def open_uri_options(target)
options = {}
options.merge!(headers)
options.merge!(open_uri_proxy_options(target))
options.merge!(ssl_verify_mode: ssl_verify_mode)
end

def open_uri_proxy_options(target)
proxy_uri = URI.parse(target).find_proxy()
return {} if proxy_uri.nil?

proxy = Faraday::ProxyOptions.from(proxy_uri)
if proxy && proxy[:user] && proxy[:password]
{ proxy_http_basic_authentication: [ proxy[:uri], proxy[:user], proxy[:password] ] }
else
{ proxy: proxy[:uri] }
end
end

def ssl_verify_mode
if Berkshelf::Config.instance.ssl.verify.nil? || Berkshelf::Config.instance.ssl.verify
OpenSSL::SSL::VERIFY_PEER
else
OpenSSL::SSL::VERIFY_NONE
end
end
end
end
2 changes: 1 addition & 1 deletion lib/berkshelf/downloader.rb
Expand Up @@ -63,7 +63,7 @@ def try_download(source, name, version)

case remote_cookbook.location_type
when :opscode, :supermarket
options = {}
options = { ssl: source.options[:ssl] }
if source.type == :artifactory
options[:headers] = { "X-Jfrog-Art-Api" => source.options[:api_key] }
end
Expand Down
22 changes: 22 additions & 0 deletions lib/berkshelf/streaming_file_adapter.rb
@@ -0,0 +1,22 @@
require "faraday/adapter/net_http"

module Berkshelf
class StreamingFileAdapter < Faraday::Adapter::NetHttp
def call(env)
env[:streaming_file] = env[:request_headers].delete(:streaming_file) if env[:request_headers] && env[:request_headers][:streaming_file]
super
end

def perform_request(http, env)
if env[:streaming_file]
http.request(create_request(env)) do |response|
response.read_body do |chunk|
env[:streaming_file].write(chunk) if response.code == "200"
end
end
else
super
end
end
end
end
14 changes: 8 additions & 6 deletions spec/unit/berkshelf/downloader_spec.rb
Expand Up @@ -39,43 +39,45 @@ module Berkshelf

it "supports the 'opscode' location type" do
allow(source).to receive(:type) { :supermarket }
allow(source).to receive(:options) { { ssl: {} } }
allow(remote_cookbook).to receive(:location_type) { :opscode }
allow(remote_cookbook).to receive(:location_path) { "http://api.opscode.com" }
rest = double("community-rest")
expect(CommunityREST).to receive(:new).with("http://api.opscode.com", {}) { rest }
expect(CommunityREST).to receive(:new).with("http://api.opscode.com", { ssl: {} }) { rest }
expect(rest).to receive(:download).with(name, version)
subject.try_download(source, name, version)
end

it "supports the 'supermarket' location type" do
allow(source).to receive(:type) { :supermarket }
allow(source).to receive(:options) { { ssl: {} } }
allow(remote_cookbook).to receive(:location_type) { :supermarket }
allow(remote_cookbook).to receive(:location_path) { "http://api.supermarket.com" }
rest = double("community-rest")
expect(CommunityREST).to receive(:new).with("http://api.supermarket.com", {}) { rest }
expect(CommunityREST).to receive(:new).with("http://api.supermarket.com", { ssl: {} }) { rest }
expect(rest).to receive(:download).with(name, version)
subject.try_download(source, name, version)
end

context "with an artifactory source" do
it "supports the 'opscode' location type" do
allow(source).to receive(:type) { :artifactory }
allow(source).to receive(:options) { { api_key: "secret" } }
allow(source).to receive(:options) { { api_key: "secret", ssl: {} } }
allow(remote_cookbook).to receive(:location_type) { :opscode }
allow(remote_cookbook).to receive(:location_path) { "http://artifactory/" }
rest = double("community-rest")
expect(CommunityREST).to receive(:new).with("http://artifactory/", { headers: { "X-Jfrog-Art-Api" => "secret" } }) { rest }
expect(CommunityREST).to receive(:new).with("http://artifactory/", { ssl: {}, headers: { "X-Jfrog-Art-Api" => "secret" } }) { rest }
expect(rest).to receive(:download).with(name, version)
subject.try_download(source, name, version)
end

it "supports the 'supermarket' location type" do
allow(source).to receive(:type) { :artifactory }
allow(source).to receive(:options) { { api_key: "secret" } }
allow(source).to receive(:options) { { api_key: "secret", ssl: {} } }
allow(remote_cookbook).to receive(:location_type) { :supermarket }
allow(remote_cookbook).to receive(:location_path) { "http://artifactory/" }
rest = double("community-rest")
expect(CommunityREST).to receive(:new).with("http://artifactory/", { headers: { "X-Jfrog-Art-Api" => "secret" } }) { rest }
expect(CommunityREST).to receive(:new).with("http://artifactory/", { ssl: {}, headers: { "X-Jfrog-Art-Api" => "secret" } }) { rest }
expect(rest).to receive(:download).with(name, version)
subject.try_download(source, name, version)
end
Expand Down