Skip to content

Commit 0136b40

Browse files
committed
NATSSync uses Net:HTTP instead of RestClient
The latter has not had updates in several years where as `Net::HTTP` is a core ruby library.
1 parent 424558d commit 0136b40

File tree

12 files changed

+33
-74
lines changed

12 files changed

+33
-74
lines changed

src/Gemfile.lock

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ PATH
5454
json
5555
logging
5656
openssl
57-
rest-client
5857
rufus-scheduler
5958

6059
PATH
@@ -151,7 +150,6 @@ GEM
151150
docile (1.4.1)
152151
dogapi (1.45.0)
153152
multi_json
154-
domain_name (0.6.20240107)
155153
drb (2.2.1)
156154
et-orbi (1.2.11)
157155
tzinfo
@@ -167,9 +165,6 @@ GEM
167165
et-orbi (~> 1, >= 1.2.11)
168166
raabro (~> 1.4)
169167
hashdiff (1.1.2)
170-
http-accept (1.7.0)
171-
http-cookie (1.0.7)
172-
domain_name (~> 0.5)
173168
httpclient (2.8.3)
174169
i18n (1.14.6)
175170
concurrent-ruby (~> 1.0)
@@ -186,10 +181,6 @@ GEM
186181
membrane (1.1.0)
187182
method_source (1.1.0)
188183
metrics (0.12.1)
189-
mime-types (3.6.0)
190-
logger
191-
mime-types-data (~> 3.2015)
192-
mime-types-data (3.2024.1105)
193184
mini_portile2 (2.8.8)
194185
minitar (1.0.2)
195186
minitest (5.25.1)
@@ -204,7 +195,6 @@ GEM
204195
timeout
205196
net-smtp (0.5.0)
206197
net-protocol
207-
netrc (0.11.0)
208198
nio4r (2.7.4)
209199
openssl (3.2.0)
210200
ostruct (0.6.1)
@@ -244,11 +234,6 @@ GEM
244234
rainbow (3.1.1)
245235
rake (13.2.1)
246236
regexp_parser (2.9.2)
247-
rest-client (2.1.0)
248-
http-accept (>= 1.7.0, < 2.0)
249-
http-cookie (>= 1.0.2, < 2.0)
250-
mime-types (>= 1.16, < 4.0)
251-
netrc (~> 0.8)
252237
rexml (3.3.9)
253238
riemann-client (1.2.1)
254239
beefcake (>= 1.0.0)

src/bosh-nats-sync/bosh-nats-sync.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ Gem::Specification.new do |spec|
2626
spec.add_dependency 'logging'
2727
spec.add_dependency 'openssl'
2828
spec.add_dependency 'rufus-scheduler'
29-
spec.add_dependency 'rest-client'
3029

3130
spec.add_development_dependency 'rack-test'
3231
spec.add_development_dependency 'rspec'

src/bosh-nats-sync/lib/nats_sync/users_sync.rb

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
require 'rest-client'
21
require 'base64'
32
require 'nats_sync/nats_auth_config'
43
require 'open3'
54

65
module NATSSync
76
class UsersSync
7+
HTTP_SUCCESS = "200"
8+
89
def initialize(nats_config_file_path, bosh_config, nats_server_executable, nats_server_pid_file)
910
@nats_config_file_path = nats_config_file_path
1011
@bosh_config = bosh_config
@@ -42,15 +43,13 @@ def execute_users_sync
4243
end
4344

4445
def self.reload_nats_server_config(nats_server_executable, nats_server_pid_file)
45-
output, status = Open3.capture2e("#{nats_server_executable} --signal reload=#{nats_server_pid_file}")
46+
nats_command = "#{nats_server_executable} --signal reload=#{nats_server_pid_file}"
47+
48+
output, status = Open3.capture2e(nats_command)
4649

47-
# rubocop:disable Style/GuardClause
48-
# rubocop:disable Layout/LineLength
4950
unless status.success?
50-
raise("Cannot execute: #{nats_server_executable} --signal reload=#{nats_server_pid_file}, Status Code: #{status} \nError: #{output}")
51+
raise("Cannot execute: #{nats_command}, Status Code: #{status}\nError: #{output}")
5152
end
52-
# rubocop:enable Style/GuardClause
53-
# rubocop:enable Layout/LineLength
5453
end
5554

5655
private
@@ -72,28 +71,33 @@ def nats_file_hash
7271
Digest::MD5.file(@nats_config_file_path).hexdigest
7372
end
7473

75-
def call_bosh_api(endpoint)
76-
auth_header = create_authentication_header
77-
NATSSync.logger.debug 'auth_header is empty, next REST call could fail' if auth_header.nil? || auth_header.empty?
78-
response = RestClient::Request.execute(
79-
url: @bosh_config['url'] + endpoint,
80-
method: :get,
81-
headers: { 'Authorization' => auth_header },
82-
verify_ssl: false,
83-
)
74+
def parsed_uri_for(api_path:)
75+
URI.parse("#{@bosh_config['url']}#{api_path}")
76+
end
77+
78+
def bosh_api_response_body(api_path, auth: true)
79+
parsed_uri = parsed_uri_for(api_path: api_path)
80+
81+
response =
82+
Net::HTTP.new(parsed_uri.host, parsed_uri.port).tap do |http|
83+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
84+
end.get(parsed_uri.request_uri, build_headers(auth: auth))
85+
8486
NATSSync.logger.debug(response.inspect)
85-
raise("Cannot access: #{endpoint}, Status Code: #{response.code}, #{response.body}") unless response.code == 200
87+
unless response.code == HTTP_SUCCESS
88+
raise("Cannot access: #{api_path}, Status Code: #{response.code}, #{response.body}")
89+
end
8690

8791
response.body
8892
end
8993

9094
def query_all_deployments
91-
deployments_json = JSON.parse(call_bosh_api('/deployments'))
95+
deployments_json = JSON.parse(bosh_api_response_body('/deployments'))
9296
deployments_json.map { |deployment| deployment['name'] }
9397
end
9498

9599
def get_vms_by_deployment(deployment)
96-
JSON.parse(call_bosh_api("/deployments/#{deployment}/vms"))
100+
JSON.parse(bosh_api_response_body("/deployments/#{deployment}/vms"))
97101
end
98102

99103
def query_all_running_vms
@@ -103,27 +107,21 @@ def query_all_running_vms
103107
vms
104108
end
105109

106-
def call_bosh_api_no_auth(endpoint)
107-
response = RestClient::Request.execute(
108-
url: @bosh_config['url'] + endpoint,
109-
method: :get,
110-
verify_ssl: false,
111-
)
112-
NATSSync.logger.debug(response.inspect)
113-
raise("Cannot access: #{endpoint}, Status Code: #{response.code}, #{response.body}") unless response.code == 200
114-
115-
response.body
116-
end
117-
118110
def info
119111
return @director_info if @director_info
120-
body = call_bosh_api_no_auth('/info')
121112

122-
@director_info = JSON.parse(body)
113+
@director_info = JSON.parse(bosh_api_response_body('/info', auth: false))
123114
end
124115

125-
def create_authentication_header
126-
NATSSync::AuthProvider.new(info, @bosh_config).auth_header
116+
def build_headers(auth: true)
117+
if auth
118+
auth_header = "#{NATSSync::AuthProvider.new(info, @bosh_config).auth_header}"
119+
NATSSync.logger.debug 'auth_header is empty, next REST call could fail' if auth_header.empty?
120+
121+
{ 'Authorization' => auth_header }
122+
else
123+
{}
124+
end
127125
end
128126

129127
def write_nats_config_file(vms, director_subject, hm_subject)

src/bosh-nats-sync/spec/nats_sync/users_sync_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require 'spec_helper'
22
require 'nats_sync/users_sync'
3-
require 'rest-client'
43

54
module NATSSync
65
describe UsersSync do

src/spec/spec_helper.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
require 'tmpdir'
1111
require 'tempfile'
1212
require 'yaml'
13-
require 'restclient'
1413

1514
require 'bosh/director'
1615
require 'nats/client'

src/vendor/cache/domain_name-0.6.20240107.gem

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/vendor/cache/http-accept-1.7.0.gem

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/vendor/cache/http-cookie-1.0.7.gem

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/vendor/cache/mime-types-3.6.0.gem

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/vendor/cache/mime-types-data-3.2024.1105.gem

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)