Skip to content

Commit 4f56bb7

Browse files
authored
feat: add possibility to specify the certification verification behaviour (influxdata#71)
1 parent d7c22e2 commit 4f56bb7

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## 1.12.0 [unreleased]
22

3+
### Features
4+
1. [#71](https://github.com/influxdata/influxdb-client-ruby/pull/71): Added possibility to specify the certification verification behaviour
5+
36
## 1.11.0 [2021-01-29]
47

58
### CI

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ client = InfluxDB2::Client.new('https://localhost:8086', 'my-token')
4848
| read_timeout | Number of seconds to wait for one block of data to be read | Integer | 10 |
4949
| max_redirect_count | Maximal number of followed HTTP redirects | Integer | 10 |
5050
| use_ssl | Turn on/off SSL for HTTP communication | bool | true |
51+
| verify_mode | Sets the flags for the certification verification at beginning of SSL/TLS session. | `OpenSSL::SSL::VERIFY_NONE` or `OpenSSL::SSL::VERIFY_PEER` | none |
5152

5253
```ruby
5354
client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',

lib/influxdb2/client/client.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class Client
4343
# @option options [Integer] :read_timeout Number of seconds to wait for one block of data to be read
4444
# @option options [Integer] :max_redirect_count Maximal number of followed HTTP redirects
4545
# @option options [bool] :use_ssl Turn on/off SSL for HTTP communication
46+
# @option options [Integer] :verify_mode Sets the flags for the certification verification
47+
# at beginning of SSL/TLS session. Could be one of `OpenSSL::SSL::VERIFY_NONE` or `OpenSSL::SSL::VERIFY_PEER`.
48+
# For more info see - https://docs.ruby-lang.org/en/3.0.0/Net/HTTP.html#verify_mode.
4649
# @option options [Logger] :logger Logger used for logging. Disable logging by set to false.
4750
# @option options [Hash] :tags Default tags which will be added to each point written by api.
4851
# the body line-protocol

lib/influxdb2/client/default_api.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ def _get(uri, limit: @max_redirect_count, headers: {})
8484
def _request(payload, uri, limit: @max_redirect_count, headers: {}, request: Net::HTTP::Post)
8585
raise InfluxError.from_message("Too many HTTP redirects. Exceeded limit: #{@max_redirect_count}") if limit.zero?
8686

87-
http = Net::HTTP.new(uri.host, uri.port)
88-
http.open_timeout = @options[:open_timeout] || DEFAULT_TIMEOUT
89-
http.write_timeout = @options[:write_timeout] || DEFAULT_TIMEOUT if Net::HTTP.method_defined? :write_timeout
90-
http.read_timeout = @options[:read_timeout] || DEFAULT_TIMEOUT
91-
http.use_ssl = @options[:use_ssl].nil? ? true : @options[:use_ssl]
87+
http = _prepare_http_client(uri)
9288

9389
request = request.new(uri.request_uri)
9490
request['Authorization'] = "Token #{@options[:token]}"
@@ -115,6 +111,16 @@ def _request(payload, uri, limit: @max_redirect_count, headers: {}, request: Net
115111
end
116112
end
117113

114+
def _prepare_http_client(uri)
115+
http = Net::HTTP.new(uri.host, uri.port)
116+
http.open_timeout = @options[:open_timeout] || DEFAULT_TIMEOUT
117+
http.write_timeout = @options[:write_timeout] || DEFAULT_TIMEOUT if Net::HTTP.method_defined? :write_timeout
118+
http.read_timeout = @options[:read_timeout] || DEFAULT_TIMEOUT
119+
http.use_ssl = @options[:use_ssl].nil? ? true : @options[:use_ssl]
120+
http.verify_mode = @options[:verify_mode] if @options[:verify_mode]
121+
http
122+
end
123+
118124
def _check_arg_type(name, value, klass)
119125
raise TypeError, "expected a #{klass.name} for #{name}; got #{value.class.name}" unless value.is_a?(klass)
120126
end

test/influxdb/default_api_test.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,33 @@ def test_supports_false
5757

5858
@api.log(:info, 'without error')
5959
end
60+
61+
def test_default_verify_mode
62+
http_client = @api.send(:_prepare_http_client, URI.parse('https://localhost:8086'))
63+
64+
refute_nil http_client
65+
assert_nil http_client.verify_mode
66+
67+
http_client.finish if http_client.started?
68+
end
69+
70+
def test_default_verify_mode_none
71+
@api = InfluxDB2::DefaultApi.new(options: { logger: @logger, verify_mode: OpenSSL::SSL::VERIFY_NONE })
72+
http_client = @api.send(:_prepare_http_client, URI.parse('https://localhost:8086'))
73+
74+
refute_nil http_client
75+
assert_equal OpenSSL::SSL::VERIFY_NONE, http_client.verify_mode
76+
77+
http_client.finish if http_client.started?
78+
end
79+
80+
def test_default_verify_mode_peer
81+
@api = InfluxDB2::DefaultApi.new(options: { logger: @logger, verify_mode: OpenSSL::SSL::VERIFY_PEER })
82+
http_client = @api.send(:_prepare_http_client, URI.parse('https://localhost:8086'))
83+
84+
refute_nil http_client
85+
assert_equal OpenSSL::SSL::VERIFY_PEER, http_client.verify_mode
86+
87+
http_client.finish if http_client.started?
88+
end
6089
end

0 commit comments

Comments
 (0)