Skip to content

Commit dae0f99

Browse files
committed
Raise TlsError that inherits from Riak::Error in tls init
1 parent a25202f commit dae0f99

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/riak/client/beefcake/socket.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'openssl'
22
require 'r509/cert/validator'
33
require 'riak/client/beefcake/messages'
4+
require 'riak/errors/connection_error'
45

56
module Riak
67
class Client
@@ -90,17 +91,17 @@ def start_tls
9091
def validate_session
9192
if @auth[:verify_hostname] &&
9293
!OpenSSL::SSL::verify_certificate_identity(riak_cert.cert, @host)
93-
raise t("ssl.cert_host_mismatch")
94+
raise TlsError.new t("ssl.cert_host_mismatch")
9495
end
9596

9697
unless riak_cert.valid?
97-
raise t("ssl.cert_not_valid")
98+
raise TlsError.new t("ssl.cert_not_valid")
9899
end
99100

100101
validator = R509::Cert::Validator.new riak_cert
101102

102103
unless validator.validate(ocsp: !!@auth[:ocsp], crl: !!@auth[:crl])
103-
raise t("ssl.cert_revoked")
104+
raise TlsError.new t("ssl.cert_revoked")
104105
end
105106
end
106107

@@ -131,7 +132,7 @@ def write_message(code, message='')
131132

132133
def read_message
133134
header = @sock.read 5
134-
raise SocketError, "Unexpected EOF during TLS init" if header.nil?
135+
raise TlsError.new(t('ssl.eof_during_init')) if header.nil?
135136
len, code = header.unpack 'NC'
136137
decode = BeefcakeMessageCodes[code]
137138
return decode, '' if len == 1
@@ -148,7 +149,12 @@ def expect_message(expected_code)
148149
candidate_code, message = read_message
149150
return message if expected_code == candidate_code
150151

151-
raise "Wanted #{expected_code.inspect}, got #{candidate_code.inspect} and #{message.inspect}"
152+
raise TlsError.new(t('ssl.unexpected_during_init',
153+
expected: expected_code.inspect,
154+
actual: candidate_code.inspect,
155+
body: message.inspect
156+
))
157+
152158
end
153159
end
154160
end

lib/riak/locale/en.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ en:
7171
cert_host_mismatch: "The presented SSL/TLS certificate did not match the hostname."
7272
cert_not_in_valid_range: "The presented SSL/TLS certificate is either expired or premature."
7373
cert_revoked: "The presented SSL/TLS certificate has been revoked."
74+
eof_during_init: "Unexpected EOF during SSL/TLS initialization."
75+
unexpected_during_init: "Expected %{expected}, got %{actual} with body %{body} during SSL/TLS initialization."
7476
stale_write_prevented: "Stale write prevented by client."
7577
stored_function_invalid: "function must have :bucket and :key when a hash"
7678
streaming_bucket_list_without_block: "Streaming bucket list was requested but no block was given."

0 commit comments

Comments
 (0)