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

setup grpc server with auto_encrypt certs #7086

Merged
merged 4 commits into from
Jan 22, 2020
Merged

Conversation

hanshasselberg
Copy link
Member

@hanshasselberg hanshasselberg commented Jan 17, 2020

HTTPS serves auto_encrypt certificates and so should GRPC. Also introduces a flag for -https-port, which I kept wondering why it is missing...

Fixes #7076.
Fixes #6590 since grpc TLS is now configurable via existing Consul TLS configuration.

$ openssl s_client -connect localhost:8502
CONNECTED(00000005)
depth=0 CN = c1.agnt.dummy.tr.consul
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 CN = c1.agnt.dummy.tr.consul
verify error:num=21:unable to verify the first certificate
verify return:1
---
Certificate chain
 0 s:/CN=c1.agnt.dummy.tr.consul
   i:/CN=pri-d1baiug.consul.ca.95b8e492.consul
---
Server certificate
-----BEGIN CERTIFICATE-----
MIICGzCCAcGgAwIBAgIBCTAKBggqhkjOPQQDAjAwMS4wLAYDVQQDEyVwcmktZDFi
YWl1Zy5jb25zdWwuY2EuOTViOGU0OTIuY29uc3VsMB4XDTIwMDExNzIyMTc1OVoX
DTIwMDEyMDIyMTc1OVowIjEgMB4GA1UEAxMXYzEuYWdudC5kdW1teS50ci5jb25z
dWwwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATIyBEoTU0LGaJ1BC67xNyyyYR3
Na8MRfknExzh5A4HZVIPmG3mhjSh1IK65dtkZFDDJsgaqfc3Qx4yzNXizBY+o4HZ
MIHWMA4GA1UdDwEB/wQEAwIDuDAdBgNVHSUEFjAUBggrBgEFBQcDAgYIKwYBBQUH
AwEwDAYDVR0TAQH/BAIwADApBgNVHQ4EIgQg5gFBtgnK74NuVwKdAhHNUdcS1vTJ
jIk+BkywaQfJrxEwKwYDVR0jBCQwIoAg5gFBtgnK74NuVwKdAhHNUdcS1vTJjIk+
BkywaQfJrxEwPwYDVR0RBDgwNoY0c3BpZmZlOi8vZHVtbXkudHJ1c3Rkb21haW4v
YWdlbnQvY2xpZW50L2RjL2RjMS9pZC9jMTAKBggqhkjOPQQDAgNIADBFAiBVdH+D
8XYvBNpolk+1rE5wxcfwLQ2OtV4nLxsPddEqIwIhAPerRVvuWZc9TX3CgQnj3Dyw
azs8ymmYhTigJ0oghhPY

Copy link
Contributor

@freddygv freddygv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Added some suggestions inline, let me know what you think.

@@ -561,6 +569,12 @@ func (c *Configurator) VerifyServerHostname() bool {
return c.base.VerifyServerHostname || c.autoEncrypt.verifyServerHostname
}

// TransportCredentials returns credentials to be used with eg a grpc server.
func (c *Configurator) TransportCredentials() *credentials.TransportCredentials {
creds := credentials.NewTLS(&tls.Config{Certificates: []tls.Certificate{*c.Cert()}})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*c.Cert() will panic if c.Cert() returns nil.

Currently the only call to TransportCredentials does a nil check before the function call, but I'm concerned about someone else calling this later without knowing about that behavior.

Maybe it would be useful to put in a nil check in this function just in case.

Alternatively, constructing the TransportCredentials could be done in the GRPCServer function, rather than being split out into a separate method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! There is a nil check now.

return nil, err
if tlsConfigurator != nil {
if cert := tlsConfigurator.Cert(); cert != nil {
opts = append(opts, grpc.Creds(*tlsConfigurator.TransportCredentials()))
Copy link
Contributor

@freddygv freddygv Jan 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a bit odd to have TransportCredentials return a pointer, but then dereference it for grpc.Creds().

I think TransportCredentials shouldn't return a pointer, since we only need a copy.

Suggested change
opts = append(opts, grpc.Creds(*tlsConfigurator.TransportCredentials()))
opts = append(opts, grpc.Creds(tlsConfigurator.TransportCredentials()))

// TransportCredentials returns credentials to be used with eg a grpc server.
func (c *Configurator) TransportCredentials() *credentials.TransportCredentials {
creds := credentials.NewTLS(&tls.Config{Certificates: []tls.Certificate{*c.Cert()}})
return &creds
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return &creds
return creds

See comment about TransportCredentials() pointer dereference.

@hanshasselberg
Copy link
Member Author

hanshasselberg commented Jan 20, 2020

@freddygv thank you for the review! I figured it would be great if grpc connections would use the same TLS config, the other consul stuff is using. Which not only fixes the cert problem, but also takes advantage of all the existing TLS configuration bits.

The pointer things you asked about, are now gone.

@hanshasselberg hanshasselberg added the waiting-reply Waiting on response from Original Poster or another individual in the thread label Jan 20, 2020
@stale stale bot removed the waiting-reply Waiting on response from Original Poster or another individual in the thread label Jan 20, 2020
@hanshasselberg hanshasselberg added the waiting-reply Waiting on response from Original Poster or another individual in the thread label Jan 20, 2020
Copy link
Member

@lornasong lornasong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense to me! added a very minor suggestion for a comment inline.

tlsutil/config.go Show resolved Hide resolved
@stale stale bot removed the waiting-reply Waiting on response from Original Poster or another individual in the thread label Jan 21, 2020
Copy link
Contributor

@freddygv freddygv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, dropped one question inline

@@ -561,6 +567,16 @@ func (c *Configurator) VerifyServerHostname() bool {
return c.base.VerifyServerHostname || c.autoEncrypt.verifyServerHostname
}

// IncomingGRPCConfig generates a *tls.Config for incoming GRPC connections.
func (c *Configurator) IncomingGRPCConfig() *tls.Config {
c.log("IncomingGRPCConfig")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are these logs used for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am using them for debugging. They are showing the version of the config that is being used to generate the *tls.Config. Eg when you reload consul so that a new cert is being used, this log line should show a version increased by one. If it doesn't increase, something is wrong.

@hanshasselberg hanshasselberg merged commit 11a571d into master Jan 22, 2020
@hanshasselberg hanshasselberg deleted the auto_encrypt_grpc branch January 22, 2020 10:32
cbarbara-okta added a commit to cbarbara-okta/consul that referenced this pull request Mar 6, 2020
This is a backported commit based on:
  * hashicorp@11a571d
  * hashicorp#7086

To allow the grpc server used by consul connect to reload its tls info.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants