1- require 'rest-client'
21require 'base64'
32require 'nats_sync/nats_auth_config'
43require 'open3'
54
65module 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 } \n Error: #{ output } " )
51+ raise ( "Cannot execute: #{ nats_command } , Status Code: #{ status } \n Error: #{ 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 )
0 commit comments