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

Be able to set an SSL verify mode on faraday #982

Merged
merged 3 commits into from
Feb 1, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/octokit/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def client_without_redirects(options = {})
conn_opts[:url] = @api_endpoint
conn_opts[:builder] = @middleware.dup if @middleware
conn_opts[:proxy] = @proxy if @proxy
conn_opts[:ssl] = { :verify_mode => @ssl_verify_mode } if @ssl_verify_mode
conn = Faraday.new(conn_opts) do |http|
if basic_authenticated?
http.basic_auth(@login, @password)
Expand Down
6 changes: 5 additions & 1 deletion lib/octokit/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ module Configurable
# @!attribute proxy
# @see https://github.com/lostisland/faraday
# @return [String] URI for proxy server
# @!attribute ssl_verify_mode
# @see https://github.com/lostisland/faraday
# @return [String] SSL verify mode for ssl connections
# @!attribute user_agent
# @return [String] Configure User-Agent header for requests.
# @!attribute web_endpoint
Expand All @@ -53,7 +56,7 @@ module Configurable
attr_accessor :access_token, :auto_paginate, :bearer_token, :client_id,
:client_secret, :default_media_type, :connection_options,
:middleware, :netrc, :netrc_file,
:per_page, :proxy, :user_agent
:per_page, :proxy, :ssl_verify_mode, :user_agent
attr_writer :password, :web_endpoint, :api_endpoint, :login,
:management_console_endpoint, :management_console_password

Expand All @@ -80,6 +83,7 @@ def keys
:per_page,
:password,
:proxy,
:ssl_verify_mode,
:user_agent,
:web_endpoint
]
Expand Down
1 change: 1 addition & 0 deletions lib/octokit/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def sawyer_options
conn_opts = @connection_options
conn_opts[:builder] = @middleware if @middleware
conn_opts[:proxy] = @proxy if @proxy
conn_opts[:ssl] = { :verify_mode => @ssl_verify_mode } if @ssl_verify_mode
opts[:faraday] = Faraday.new(conn_opts)

opts
Expand Down
9 changes: 9 additions & 0 deletions lib/octokit/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ def proxy
ENV['OCTOKIT_PROXY']
end

# Default SSL verify mode from ENV
# @return [Integer]
def ssl_verify_mode
# 0 is OpenSSL::SSL::VERIFY_NONE
# 1 is OpenSSL::SSL::SSL_VERIFY_PEER
# the standard default for SSL is SSL_VERIFY_PEER which requires a server certificate check on the client
ENV['OCTOKIT_SSL_VERIFY_MODE'] || 1
end

# Default User-Agent header string from ENV or {USER_AGENT}
# @return [String]
def user_agent
Expand Down
11 changes: 11 additions & 0 deletions spec/octokit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,17 @@
conn = Octokit.client.send(:agent).instance_variable_get(:"@conn")
expect(conn.proxy[:uri].to_s).to eq('http://proxy.example.com')
end
it "sets an ssl verify mode" do
Octokit.configure do |config|
config.ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE
end
conn = Octokit.client.send(:agent).instance_variable_get(:"@conn")
expect(conn.ssl[:verify_mode]).to eq(OpenSSL::SSL::VERIFY_NONE)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test here to ensure the expected default remains unchanged?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, good call

it "ensures ssl verify mode is set to default when no override provided" do
conn = Octokit.client.send(:agent).instance_variable_get(:"@conn")
expect(conn.ssl[:verify_mode]).to eq(OpenSSL::SSL::VERIFY_PEER)
end
it "passes along request headers for POST" do
headers = {"X-GitHub-Foo" => "bar"}
root_request = stub_post("/").
Expand Down