Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support secure connection with custom certificates #125

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ conn = Etcdv3.new(endpoints: 'https://hostname:port')
conn = Etcdv3.new(endpoints: 'https://hostname:port', user: 'root', password: 'mysecretpassword')

# Secure connection specifying custom certificates
# Coming soon...

conn = Etcdv3.new(endpoints: 'https://hostname:port', custom_certificates: {
root_ca: 'path-to-root-ca.pem',
key: 'path-to-private-key.pem',
cert: 'path-to-cert.pem'
})
```
**High Availability**

Expand Down
3 changes: 2 additions & 1 deletion lib/etcdv3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class Etcdv3
def initialize(options = {})
@options = options
@timeout = options[:command_timeout] || DEFAULT_TIMEOUT
@conn = ConnectionWrapper.new(@timeout, *sanitized_endpoints)
@custom_certificates = options[:custom_certificates]
@conn = ConnectionWrapper.new(endpoints: sanitized_endpoints, timeout: @timeout, custom_certificates: @custom_certificates)
warn "WARNING: `url` is deprecated. Please use `endpoints` instead." if @options.key?(:url)
authenticate(@options[:user], @options[:password]) if @options.key?(:user)
end
Expand Down
14 changes: 10 additions & 4 deletions lib/etcdv3/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class Connection

attr_reader :endpoint, :hostname, :handlers, :credentials

def initialize(url, timeout, metadata={})
def initialize(url, timeout, metadata={}, custom_certificates=nil)
@endpoint = URI(url)
@hostname = "#{@endpoint.hostname}:#{@endpoint.port}"
@credentials = resolve_credentials
@credentials = resolve_credentials(custom_certificates)
@timeout = timeout
@handlers = handler_map(metadata)
end
Expand All @@ -38,13 +38,19 @@ def handler_map(metadata={})
]
end

def resolve_credentials
def resolve_credentials(custom_certificates)
case @endpoint.scheme
when 'http'
:this_channel_is_insecure
when 'https'
# Use default certs for now.
GRPC::Core::ChannelCredentials.new
return GRPC::Core::ChannelCredentials.new if custom_certificates.nil?

GRPC::Core::ChannelCredentials.new(
File.read(custom_certificates[:root_ca]),
File.read(custom_certificates[:key]),
File.read(custom_certificates[:cert])
)
else
raise "Unknown scheme: #{@endpoint.scheme}"
end
Expand Down
5 changes: 3 additions & 2 deletions lib/etcdv3/connection_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ class ConnectionWrapper

attr_accessor :connection, :endpoints, :user, :password, :token, :timeout

def initialize(timeout, *endpoints)
def initialize(endpoints:, timeout:, custom_certificates: nil)
@user, @password, @token = nil, nil, nil
@timeout = timeout
@endpoints = endpoints.map{|endpoint| Etcdv3::Connection.new(endpoint, @timeout) }
@custom_certificates = custom_certificates
@endpoints = endpoints.map{|endpoint| Etcdv3::Connection.new(endpoint, @timeout, {}, @custom_certificates) }
@connection = @endpoints.first
end

Expand Down
4 changes: 2 additions & 2 deletions spec/etcdv3/connection_wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let(:endpoints) { ['http://localhost:2379', 'http://localhost:2389'] }

describe '#initialize' do
subject { Etcdv3::ConnectionWrapper.new(10, *endpoints) }
subject { Etcdv3::ConnectionWrapper.new(endpoints: endpoints, timeout: 10) }
it { is_expected.to have_attributes(user: nil, password: nil, token: nil) }
it 'sets hostnames in correct order' do
expect(subject.endpoints.map(&:hostname)).to eq(['localhost:2379', 'localhost:2389'])
Expand All @@ -16,7 +16,7 @@
end

describe "#rotate_connection_endpoint" do
subject { Etcdv3::ConnectionWrapper.new(10, *endpoints) }
subject { Etcdv3::ConnectionWrapper.new(endpoints: endpoints, timeout: 10) }
before do
subject.rotate_connection_endpoint
end
Expand Down