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

Use partial for Ruby HTTP libraries #3539

Merged
merged 4 commits into from
Aug 2, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,14 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("rubocop.mustache", "", ".rubocop.yml"));
supportingFiles.add(new SupportingFile("travis.mustache", "", ".travis.yml"));
supportingFiles.add(new SupportingFile("gemspec.mustache", "", gemName + ".gemspec"));
supportingFiles.add(new SupportingFile("configuration.mustache", gemFolder, "configuration.rb"));

if (TYPHOEUS.equals(getLibrary())) {
supportingFiles.add(new SupportingFile("api_client.mustache", gemFolder, "api_client.rb"));
supportingFiles.add(new SupportingFile("configuration.mustache", gemFolder, "configuration.rb"));
supportingFiles.add(new SupportingFile("Gemfile.lock.mustache", "", "Gemfile.lock"));
} else if (FARADAY.equals(getLibrary())) {
supportingFiles.add(new SupportingFile("faraday_api_client.mustache", gemFolder, "api_client.rb"));
supportingFiles.add(new SupportingFile("faraday_configuration.mustache", gemFolder, "configuration.rb"));
additionalProperties.put("isFaraday", Boolean.TRUE);
} else {
throw new RuntimeException("Invalid HTTP library " + getLibrary() + ". Only faraday, typhoeus are supported.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,41 +71,12 @@ module {{moduleName}}
# @return [true, false]
attr_accessor :client_side_validation

### TLS/SSL setting
# Set this to false to skip verifying SSL certificate when calling API from https server.
# Default to true.
#
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
#
# @return [true, false]
attr_accessor :verify_ssl

### TLS/SSL setting
# Set this to false to skip verifying SSL host name
# Default to true.
#
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
#
# @return [true, false]
attr_accessor :verify_ssl_host

### TLS/SSL setting
# Set this to customize the certificate file to verify the peer.
#
# @return [String] the path to the certificate file
#
# @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
# https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
attr_accessor :ssl_ca_cert

### TLS/SSL setting
# Client certificate file (for client certificate)
attr_accessor :cert_file

### TLS/SSL setting
# Client private key file (for client certificate)
attr_accessor :key_file

{{^isFaraday}}
{{> configuration_tls_typhoeus_partial}}
{{/isFaraday}}
{{#isFaraday}}
{{> configuration_tls_faraday_partial}}
{{/isFaraday}}
# Set this to customize parameters encoding of array parameter with multi collectionFormat.
# Default to nil.
#
Expand All @@ -125,11 +96,20 @@ module {{moduleName}}
@api_key_prefix = {}
@timeout = 0
@client_side_validation = true
{{#isFaraday}}
@ssl_verify = true
@ssl_verify_mode = nil
@ssl_ca_file = nil
@ssl_client_cert = nil
@ssl_client_key = nil
{{/isFaraday}}
{{^isFaraday}}
@verify_ssl = true
Copy link
Contributor

Choose a reason for hiding this comment

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

You should only include the TLS settings for Typhoeus when not isFaraday.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nevemind. I misread.

@verify_ssl_host = true
@params_encoding = nil
@cert_file = nil
@key_file = nil
{{/isFaraday}}
@debugging = false
@inject_format = false
@force_ending_format = false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### TLS/SSL setting
# Set this to false to skip verifying SSL certificate when calling API from https server.
# Default to true.
#
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
#
# @return [true, false]
attr_accessor :ssl_verify

### TLS/SSL setting
# Any `OpenSSL::SSL::` constant (see https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL.html)
#
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
#
attr_accessor :ssl_verify_mode

### TLS/SSL setting
# Set this to customize the certificate file to verify the peer.
#
# @return [String] the path to the certificate file
attr_accessor :ssl_ca_file

### TLS/SSL setting
# Client certificate file (for client certificate)
attr_accessor :ssl_client_cert

### TLS/SSL setting
# Client private key file (for client certificate)
attr_accessor :ssl_client_key
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
### TLS/SSL setting
# Set this to false to skip verifying SSL certificate when calling API from https server.
# Default to true.
#
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
#
# @return [true, false]
attr_accessor :verify_ssl

### TLS/SSL setting
# Set this to false to skip verifying SSL host name
# Default to true.
#
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
#
# @return [true, false]
attr_accessor :verify_ssl_host

### TLS/SSL setting
# Set this to customize the certificate file to verify the peer.
#
# @return [String] the path to the certificate file
#
# @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
# https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
attr_accessor :ssl_ca_cert

### TLS/SSL setting
# Client certificate file (for client certificate)
attr_accessor :cert_file

### TLS/SSL setting
# Client private key file (for client certificate)
attr_accessor :key_file
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ module {{moduleName}}

update_params_for_auth! header_params, query_params, opts[:auth_names]



req_opts = {
:method => http_method,
:headers => header_params,
Expand All @@ -118,7 +116,6 @@ module {{moduleName}}
:verbose => @config.debugging
}


if [:post, :patch, :put, :delete].include?(http_method)
req_body = build_request_body(header_params, form_params, opts[:body])
req_opts.update :body => req_body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,23 @@ def self.default
# @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
# the data deserialized from response body (could be nil), response status code and response headers.
def call_api(http_method, path, opts = {})
connection = Faraday.new(:url => config.base_url) do |conn|
ssl_options = {
:ca_file => @config.ssl_ca_file,
:verify => @config.ssl_verify,
:verify => @config.ssl_verify_mode,
:client_cert => @config.ssl_client_cert,
:client_key => @config.ssl_client_key
}

connection = Faraday.new(:url => config.base_url, :ssl => ssl_options) do |conn|
conn.basic_auth(config.username, config.password)
if opts[:header_params]["Content-Type"] == "multipart/form-data"
conn.request :multipart
conn.request :url_encoded
end
conn.adapter(Faraday.default_adapter)
end

begin
response = connection.public_send(http_method.to_sym.downcase) do |req|
build_request(http_method, path, req, opts)
Expand Down Expand Up @@ -106,25 +115,15 @@ def build_request(http_method, path, request, opts = {})

update_params_for_auth! header_params, query_params, opts[:auth_names]

# set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
_verify_ssl_host = @config.verify_ssl_host ? 2 : 0

req_opts = {
:method => http_method,
:headers => header_params,
:params => query_params,
:params_encoding => @config.params_encoding,
:timeout => @config.timeout,
:ssl_verifypeer => @config.verify_ssl,
:ssl_verifyhost => _verify_ssl_host,
:sslcert => @config.cert_file,
:sslkey => @config.key_file,
:verbose => @config.debugging
}

# set custom cert, if provided
req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert

if [:post, :patch, :put, :delete].include?(http_method)
req_body = build_request_body(header_params, form_params, opts[:body])
req_opts.update :body => req_body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,28 @@ class Configuration
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
#
# @return [true, false]
attr_accessor :verify_ssl
attr_accessor :ssl_verify

### TLS/SSL setting
# Set this to false to skip verifying SSL host name
# Default to true.
# Any `OpenSSL::SSL::` constant (see https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL.html)
#
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
#
# @return [true, false]
attr_accessor :verify_ssl_host
attr_accessor :ssl_verify_mode

### TLS/SSL setting
# Set this to customize the certificate file to verify the peer.
#
# @return [String] the path to the certificate file
#
# @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
# https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
attr_accessor :ssl_ca_cert
attr_accessor :ssl_ca_file

### TLS/SSL setting
# Client certificate file (for client certificate)
attr_accessor :cert_file
attr_accessor :ssl_client_cert

### TLS/SSL setting
# Client private key file (for client certificate)
attr_accessor :key_file
attr_accessor :ssl_client_key

# Set this to customize parameters encoding of array parameter with multi collectionFormat.
# Default to nil.
Expand All @@ -133,11 +128,11 @@ def initialize
@api_key_prefix = {}
@timeout = 0
@client_side_validation = true
@verify_ssl = true
@verify_ssl_host = true
@params_encoding = nil
@cert_file = nil
@key_file = nil
@ssl_verify = true
@ssl_verify_mode = nil
@ssl_ca_file = nil
@ssl_client_cert = nil
@ssl_client_key = nil
@debugging = false
@inject_format = false
@force_ending_format = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rspec', '~> 3.6', '>= 3.6.0'
s.add_development_dependency 'vcr', '~> 3.0', '>= 3.0.1'
s.add_development_dependency 'webmock', '~> 1.24', '>= 1.24.3'
s.add_development_dependency 'autotest', '~> 4.4', '>= 4.4.6'
s.add_development_dependency 'autotest-rails-pure', '~> 4.1', '>= 4.1.2'
s.add_development_dependency 'autotest-growl', '~> 0.2', '>= 0.2.16'
s.add_development_dependency 'autotest-fsevent', '~> 0.2', '>= 0.2.12'

s.files = `find *`.split("\n").uniq.sort.select { |f| !f.empty? }
s.test_files = `find spec/*`.split("\n")
Expand Down