Skip to content

Commit 3d5e300

Browse files
committed
Revert "Use configured director cert in bosh-monitor, nats-sync"
This reverts commit 421d34d.
1 parent c70527f commit 3d5e300

20 files changed

Lines changed: 113 additions & 303 deletions

File tree

src/bosh-director/lib/bosh/director/config_server/auth_http_client.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ def auth_retryable
5353
end
5454

5555
def set_cert_store(ca_cert_path)
56-
if ca_cert_path && File.file?(ca_cert_path) && !File.zero?(ca_cert_path)
56+
if ca_cert_path && File.exist?(ca_cert_path) && !File.read(ca_cert_path).strip.empty?
5757
@http.ca_file = ca_cert_path
58+
else
59+
cert_store = OpenSSL::X509::Store.new
60+
cert_store.set_default_paths
61+
@http.cert_store = cert_store
5862
end
5963
end
6064
end

src/bosh-director/lib/bosh/director/config_server/uaa_auth_provider.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ class UAAToken
2323
def initialize(client_id, client_secret, uaa_url, ca_cert_path, logger)
2424
options = {}
2525

26-
if File.file?(ca_cert_path) && !File.zero?(ca_cert_path)
26+
if File.exist?(ca_cert_path) && !File.read(ca_cert_path).strip.empty?
2727
options[:ssl_ca_file] = ca_cert_path
28+
else
29+
cert_store = OpenSSL::X509::Store.new
30+
cert_store.set_default_paths
31+
options[:ssl_cert_store] = cert_store
2832
end
2933

3034
@uaa_url = uaa_url

src/bosh-director/spec/unit/bosh/director/config_server/auth_http_client_spec.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,21 @@
2323

2424
describe '#initialize' do
2525
context 'ssl is setup' do
26-
shared_examples 'does not configure cert_store' do
26+
shared_examples 'cert_store' do
27+
store_double = nil
28+
2729
before do
2830
allow(http_client).to receive(:use_ssl=).with(true)
2931
allow(http_client).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
32+
33+
store_double = instance_double(OpenSSL::X509::Store)
34+
allow(store_double).to receive(:set_default_paths)
35+
allow(OpenSSL::X509::Store).to receive(:new).and_return(store_double)
3036
end
3137

32-
it 'does not set cert_store, falling back to default trust store' do
33-
expect(http_client).not_to receive(:cert_store=)
34-
expect(http_client).not_to receive(:ca_file=)
35-
expect(OpenSSL::X509::Store).not_to receive(:new)
38+
it 'uses default cert_store' do
39+
expect(http_client).to receive(:cert_store=)
40+
expect(store_double).to receive(:set_default_paths)
3641

3742
subject
3843
end
@@ -43,17 +48,17 @@
4348
config_server_hash['ca_cert_path'] = nil
4449
end
4550

46-
it_behaves_like 'does not configure cert_store'
51+
it_behaves_like 'cert_store'
4752
end
4853

4954
context 'ca_cert file exists and is empty' do
5055
before do
5156
config_server_hash['ca_cert_path'] = '/root/cert.crt'
52-
allow(File).to receive(:file?).with('/root/cert.crt').and_return(true)
53-
allow(File).to receive(:zero?).with('/root/cert.crt').and_return(true)
57+
allow(File).to receive(:exist?).and_return(true)
58+
allow(File).to receive(:read).and_return('')
5459
end
5560

56-
it_behaves_like 'does not configure cert_store'
61+
it_behaves_like 'cert_store'
5762
end
5863
end
5964
end
@@ -62,6 +67,7 @@
6267
before do
6368
allow(http_client).to receive(:use_ssl=).with(true)
6469
allow(http_client).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
70+
allow(http_client).to receive(:cert_store=)
6571
end
6672

6773
it 'should add "Authorization" header and call through to actual http client' do
@@ -91,6 +97,7 @@
9197
before do
9298
allow(http_client).to receive(:use_ssl=).with(true)
9399
allow(http_client).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
100+
allow(http_client).to receive(:cert_store=)
94101
end
95102

96103
it 'should add "Authorization" header and call through to actual http client' do

src/bosh-director/spec/unit/bosh/director/config_server/uaa_auth_provider_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
let(:expiration_time) { Time.now.to_i + 3600 }
2424

2525
before do
26-
allow(File).to receive(:file?).with('fake-ca-cert-path').and_return(true)
27-
allow(File).to receive(:zero?).with('fake-ca-cert-path').and_return(false)
26+
allow(File).to receive(:exist?).with('fake-ca-cert-path').and_return(true)
27+
allow(File).to receive(:read).with('fake-ca-cert-path').and_return('test')
2828

2929
allow(CF::UAA::TokenIssuer).to receive(:new).with(
3030
uaa_url, 'fake-client', 'fake-client-secret', { :ssl_ca_file => 'fake-ca-cert-path' }

src/bosh-monitor/lib/bosh/monitor.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ module Monitor
1919

2020
# Helpers
2121
require 'bosh/monitor/yaml_helper'
22-
require 'bosh/monitor/ssl_helpers'
2322

2423
# Basic blocks
2524
require 'bosh/monitor/agent'

src/bosh-monitor/lib/bosh/monitor/auth_provider.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@ def uaa_token_header(uaa_url)
3232
end
3333

3434
class UAAToken
35-
include Bosh::Monitor::SSLHelpers
36-
3735
EXPIRATION_DEADLINE_IN_SECONDS = 60
3836

3937
def initialize(client_id, client_secret, uaa_url, ca_cert_file_path, logger)
4038
options = {}
4139

42-
if configured_ca_cert?(ca_cert_file_path.to_s)
43-
options[:ssl_ca_file] = ca_cert_file_path.to_s
40+
if File.exist?(ca_cert_file_path) && !File.read(ca_cert_file_path).strip.empty?
41+
options[:ssl_ca_file] = ca_cert_file_path
42+
else
43+
cert_store = OpenSSL::X509::Store.new
44+
cert_store.set_default_paths
45+
options[:ssl_cert_store] = cert_store
4446
end
4547

4648
@uaa_token_issuer = CF::UAA::TokenIssuer.new(

src/bosh-monitor/lib/bosh/monitor/director.rb

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
require 'async/http/internet/instance'
2-
require 'openssl'
32

43
module Bosh::Monitor
54
class Director
6-
include SSLHelpers
7-
85
def initialize(options, logger)
96
@options = options
107
@logger = logger
@@ -57,13 +54,9 @@ def perform_request(method, request_path, options = {})
5754
headers = {}
5855
headers['authorization'] = auth_provider.auth_header unless options.fetch(:no_login, false)
5956

60-
async_endpoint =
61-
if parsed_endpoint.scheme == 'https'
62-
ssl_context = ssl_context_for_peer_verification(@options['ca_cert'].to_s)
63-
Async::HTTP::Endpoint.parse(parsed_endpoint.to_s, ssl_context: ssl_context)
64-
else
65-
Async::HTTP::Endpoint.parse(parsed_endpoint.to_s)
66-
end
57+
ssl_context = OpenSSL::SSL::SSLContext.new
58+
ssl_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_NONE)
59+
async_endpoint = Async::HTTP::Endpoint.parse(parsed_endpoint.to_s, ssl_context: ssl_context)
6760
response = Async::HTTP::Internet.send(method.to_sym, async_endpoint, headers)
6861

6962
body = response.read
@@ -81,7 +74,7 @@ def perform_request(method, request_path, options = {})
8174
def info
8275
body, status = perform_request(:get, '/info', no_login: true)
8376

84-
raise DirectorError, "Cannot get status from director at #{endpoint}/info: #{status} #{body}" if status != 200
77+
raise DirectorError, "Cannot get status from director at #{http.req.uri}: #{status} #{body}" if status != 200
8578

8679
parse_json(body, Hash)
8780
end

src/bosh-monitor/lib/bosh/monitor/plugins/event_logger.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def process(alert)
6565

6666
request[:proxy] = options['http_proxy'] if options['http_proxy']
6767

68-
send_http_post_request(@url.to_s, request, @director_options['ca_cert'])
68+
send_http_post_request(@url.to_s, request)
6969
end
7070

7171
private
@@ -79,7 +79,7 @@ def director_info
7979

8080
director_info_url = @url.dup
8181
director_info_url.path = '/info'
82-
body, status = send_http_get_request_synchronous(director_info_url.to_s, @director_options['ca_cert'])
82+
body, status = send_http_get_request_synchronous(director_info_url.to_s)
8383
return nil if status != 200
8484

8585
@director_info = JSON.parse(body)

src/bosh-monitor/lib/bosh/monitor/plugins/http_request_helper.rb

Lines changed: 28 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,36 @@
22
require 'async/http/internet/instance'
33
require 'async/http/proxy'
44
require 'net/http'
5-
require 'openssl'
65

76
module Bosh::Monitor::Plugins
87
module HttpRequestHelper
9-
include Bosh::Monitor::SSLHelpers
10-
11-
def send_http_put_request(uri, request, ca_cert = nil)
8+
def send_http_put_request(uri, request)
129
logger.debug("sending HTTP PUT to: #{uri}")
13-
process_async_http_request(
14-
method: :put,
15-
uri: uri,
16-
headers: request.fetch(:head, {}),
17-
body: request.fetch(:body, nil),
18-
proxy: request.fetch(:proxy, nil),
19-
ca_cert: ca_cert,
20-
)
10+
process_async_http_request(method: :put, uri: uri, headers: request.fetch(:head, {}), body: request.fetch(:body, nil), proxy: request.fetch(:proxy, nil))
2111
end
2212

23-
def send_http_post_request(uri, request, ca_cert = nil)
13+
def send_http_post_request(uri, request)
2414
logger.debug("sending HTTP POST to: #{uri}")
25-
process_async_http_request(
26-
method: :post,
27-
uri: uri,
28-
headers: request.fetch(:head, {}),
29-
body: request.fetch(:body, nil),
30-
proxy: request.fetch(:proxy, nil),
31-
ca_cert: ca_cert,
32-
)
15+
process_async_http_request(method: :post, uri: uri, headers: request.fetch(:head, {}), body: request.fetch(:body, nil), proxy: request.fetch(:proxy, nil))
3316
end
3417

35-
def send_http_get_request_synchronous(uri, ca_cert = nil, headers = nil)
18+
def send_http_get_request_synchronous(uri, headers = nil)
3619
parsed_uri = URI.parse(uri.to_s)
3720

3821
# we are interested in response, so send sync request
3922
logger.debug("Sending GET request to #{parsed_uri}")
4023

41-
net_http = sync_client(parsed_uri, ca_cert)
24+
net_http = sync_client(parsed_uri, OpenSSL::SSL::VERIFY_NONE)
4225

4326
response = net_http.get(parsed_uri.request_uri, headers)
4427

4528
[response.body, response.code.to_i]
4629
end
4730

48-
def send_http_post_request_synchronous_with_tls_verify_peer(uri, request, ca_cert = nil)
31+
def send_http_post_request_synchronous_with_tls_verify_peer(uri, request)
4932
parsed_uri = URI.parse(uri.to_s)
5033

51-
net_http = sync_client(parsed_uri, ca_cert, request.fetch(:proxy, nil))
34+
net_http = sync_client(parsed_uri, OpenSSL::SSL::VERIFY_PEER)
5235

5336
response = net_http.post(parsed_uri.request_uri, request[:body])
5437

@@ -57,40 +40,27 @@ def send_http_post_request_synchronous_with_tls_verify_peer(uri, request, ca_cer
5740

5841
private
5942

60-
def resolved_proxy_uri(parsed_uri, explicit_proxy_string)
61-
explicit = explicit_proxy_string.to_s.strip
62-
return URI.parse(explicit) unless explicit.empty?
63-
64-
parsed_uri.find_proxy
65-
end
66-
67-
def sync_client(parsed_uri, ca_cert, explicit_proxy = nil)
43+
def sync_client(parsed_uri, ssl_verify_mode)
6844
net_http = Net::HTTP.new(parsed_uri.host, parsed_uri.port)
69-
if parsed_uri.scheme == 'https'
70-
net_http.use_ssl = true
71-
configure_net_http_tls!(net_http, ca_cert)
72-
end
73-
74-
unless (proxy_uri = resolved_proxy_uri(parsed_uri, explicit_proxy)).nil?
75-
net_http.proxy_address = proxy_uri.host
76-
net_http.proxy_port = proxy_uri.port
77-
net_http.proxy_user = proxy_uri.user
78-
net_http.proxy_pass = proxy_uri.password
45+
net_http.use_ssl = (parsed_uri.scheme == 'https')
46+
net_http.verify_mode = ssl_verify_mode
47+
48+
env_proxy = parsed_uri.find_proxy
49+
unless env_proxy.nil?
50+
net_http.proxy_address = env_proxy.host
51+
net_http.proxy_port = env_proxy.port
52+
net_http.proxy_user = env_proxy.user
53+
net_http.proxy_pass = env_proxy.password
7954
end
8055

8156
net_http
8257
end
8358

84-
def configure_net_http_tls!(net_http, ca_cert_path)
85-
net_http.verify_mode = OpenSSL::SSL::VERIFY_PEER
86-
net_http.ca_file = ca_cert_path.to_s if configured_ca_cert?(ca_cert_path)
87-
end
88-
89-
def process_async_http_request(method:, uri:, headers: {}, body: nil, proxy: nil, ca_cert: nil)
59+
def process_async_http_request(method:, uri:, headers: {}, body: nil, proxy: nil)
9060
name = self.class.name
9161
started = Time.now
9262

93-
endpoint = create_async_endpoint(uri: uri, proxy: proxy, ca_cert: ca_cert)
63+
endpoint = create_async_endpoint(uri: uri, proxy: proxy)
9464
response = Async::HTTP::Internet.send(method, endpoint, headers, body)
9565

9666
# Explicitly read the response stream to ensure the connection fully closes
@@ -105,19 +75,17 @@ def process_async_http_request(method:, uri:, headers: {}, body: nil, proxy: nil
10575
response.close if response
10676
end
10777

108-
def create_async_endpoint(uri:, proxy:, ca_cert: nil)
78+
def create_async_endpoint(uri:, proxy:)
10979
parsed_uri = URI.parse(uri.to_s)
80+
env_proxy = parsed_uri.find_proxy
11081

111-
endpoint =
112-
if parsed_uri.scheme == 'https'
113-
ssl_context = ssl_context_for_peer_verification(ca_cert)
114-
Async::HTTP::Endpoint.parse(uri.to_s, ssl_context: ssl_context)
115-
else
116-
Async::HTTP::Endpoint.parse(uri.to_s)
117-
end
82+
ssl_context = OpenSSL::SSL::SSLContext.new
83+
ssl_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_NONE)
84+
endpoint = Async::HTTP::Endpoint.parse(uri).with(ssl_context: ssl_context)
11885

119-
unless (proxy_uri = resolved_proxy_uri(parsed_uri, proxy)).nil?
120-
client = Async::HTTP::Client.new(Async::HTTP::Endpoint.parse(proxy_uri.to_s))
86+
if proxy || env_proxy
87+
proxy_uri = proxy || "http://#{env_proxy.host}:#{env_proxy.port}"
88+
client = Async::HTTP::Client.new(Async::HTTP::Endpoint.parse(proxy_uri))
12189
proxy = Async::HTTP::Proxy.new(client, "#{parsed_uri.host}:#{parsed_uri.port}")
12290
endpoint = proxy.wrap_endpoint(endpoint)
12391
end

src/bosh-monitor/lib/bosh/monitor/plugins/resurrector.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def process(alert)
8686
title: 'Scan unresponsive VMs',
8787
summary: 'Notifying Director to scan instances: '\
8888
"#{pretty_str(jobs_to_instances_resurrection_enabled)}; #{state.summary}")
89-
send_http_put_request(url.to_s, request, @director_options['ca_cert'])
89+
send_http_put_request(url.to_s, request)
9090
end
9191

9292
unless jobs_to_instances_resurrection_disabled.empty?
@@ -116,7 +116,7 @@ def scan_and_fix_already_queued_or_processing?(deployment_name)
116116
'Content-Type' => 'application/json',
117117
}
118118
url.query = URI.encode_www_form({ deployment: deployment_name, state: 'queued,processing', verbose: 2 })
119-
body, status = send_http_get_request_synchronous(url.to_s, @director_options['ca_cert'], headers)
119+
body, status = send_http_get_request_synchronous(url.to_s, headers)
120120

121121
# Getting the current tasks may fail. In a situation where the director is already dealing with lots of scan and fix tasks,
122122
# we may want to postpone adding another one to the queue to give the director time to deal with the currently scheduled tasks.
@@ -142,7 +142,7 @@ def director_info
142142

143143
url = @uri.dup
144144
url.path = '/info'
145-
body, status = send_http_get_request_synchronous(url.to_s, @director_options['ca_cert'])
145+
body, status = send_http_get_request_synchronous(url.to_s)
146146
return nil if status != 200
147147

148148
@director_info = JSON.parse(body)

0 commit comments

Comments
 (0)