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

Implement config reload support for TLS #506

Merged
merged 6 commits into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions server/configs/reload/reload.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
debug: true # enable on reload
trace: true # enable on reload
logtime: false

# Enable TLS on reload
tls {
cert_file: "../test/configs/certs/server-cert.pem"
key_file: "../test/configs/certs/server-key.pem"
ca_file: "../test/configs/certs/ca.pem"
verify: true
}
17 changes: 17 additions & 0 deletions server/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package server

import (
"crypto/tls"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -41,6 +42,20 @@ func (d *debugOption) Apply(server *Server) {
server.Noticef("Reloaded: debug = %v", d.newValue)
}

// tlsOption implements the option interface for the `tls` setting.
type tlsOption struct {
newValue *tls.Config
}

// Apply the tls change.
func (t *tlsOption) Apply(server *Server) {
tlsRequired := t.newValue != nil
server.info.TLSRequired = tlsRequired
server.info.TLSVerify = tlsRequired && t.newValue.ClientAuth == tls.RequireAndVerifyClientCert
Copy link
Member

Choose a reason for hiding this comment

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

Why the &&?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Guard against nil pointers when the reload is disabling TLS (t.newValue == nil). Same as here.

Copy link
Member

Choose a reason for hiding this comment

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

Not clear (although maybe correct) that an assignment in Go does not always return true.. We could just do an easy "if" statement for the ClientAuth assignment and it would read much clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are not returning the value of an assignment. This is purely a boolean expression being assigned to server.info.TLSVerify. Maybe the parens used here make that more clear?

Copy link
Member

Choose a reason for hiding this comment

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

The comparison makes sense, but having assignment first here is confusing, and I used to be a C guy ;)

server.generateServerInfoJSON()
Copy link
Member

Choose a reason for hiding this comment

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

I was about to ask to surround this with server lock/unlock but I realize that this is done higher in the caller stack. So we are good here.

server.Noticef("Reloaded: tls")
}

// Reload reads the current configuration file and applies any supported
// changes. This returns an error if the server was not started with a config
// file or an option which doesn't support hot-swapping was changed.
Expand Down Expand Up @@ -99,6 +114,8 @@ func (s *Server) diffOptions(newOpts *Options) ([]option, error) {
diffOpts = append(diffOpts, &traceOption{newValue.(bool)})
case "debug":
diffOpts = append(diffOpts, &debugOption{newValue.(bool)})
case "tlsconfig":
diffOpts = append(diffOpts, &tlsOption{newValue.(*tls.Config)})
default:
// Bail out if attempting to reload any unsupported options.
return nil, fmt.Errorf("Config reload not supported for %s", field.Name)
Expand Down
22 changes: 15 additions & 7 deletions server/reload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,20 @@ func TestConfigReload(t *testing.T) {
}

// Ensure config changed.
var updatedGolden *Options = &Options{}
*updatedGolden = *golden
updatedGolden.Trace = true
updatedGolden.Debug = true
if !reflect.DeepEqual(updatedGolden, server.getOpts()) {
t.Fatalf("Options are incorrect.\nexpected: %+v\ngot: %+v",
updatedGolden, server.getOpts())
updated := server.getOpts()
if !updated.Trace {
t.Fatal("Expected Trace to be true")
}
if !updated.Debug {
t.Fatal("Expected Debug to be true")
}
if updated.TLSConfig == nil {
t.Fatal("Expected TLSConfig to be non-nil")
}
if !server.info.TLSRequired {
t.Fatal("Expected TLSRequired to be true")
}
if !server.info.TLSVerify {
t.Fatal("Expected TLSVerify to be true")
}
}