-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Added HTTPS support via a new HTTPS Port configuration option #478
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package agent | ||
|
||
import ( | ||
"crypto/tls" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net" | ||
|
@@ -12,6 +14,7 @@ import ( | |
"time" | ||
|
||
"github.com/hashicorp/consul/consul/structs" | ||
"github.com/hashicorp/consul/tlsutil" | ||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
|
@@ -23,38 +26,132 @@ type HTTPServer struct { | |
listener net.Listener | ||
logger *log.Logger | ||
uiDir string | ||
addr string | ||
} | ||
|
||
// NewHTTPServer starts a new HTTP server to provide an interface to | ||
// NewHTTPServers starts new HTTP servers to provide an interface to | ||
// the agent. | ||
func NewHTTPServer(agent *Agent, uiDir string, enableDebug bool, logOutput io.Writer, bind string) (*HTTPServer, error) { | ||
// Create the mux | ||
mux := http.NewServeMux() | ||
func NewHTTPServers(agent *Agent, config *Config, logOutput io.Writer) ([]*HTTPServer, error) { | ||
var tlsConfig *tls.Config | ||
var list net.Listener | ||
var httpAddr *net.TCPAddr | ||
var err error | ||
var servers []*HTTPServer | ||
|
||
if config.Ports.HTTPS > 0 { | ||
httpAddr, err = config.ClientListener(config.Addresses.HTTPS, config.Ports.HTTPS) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Create listener | ||
list, err := net.Listen("tcp", bind) | ||
if err != nil { | ||
return nil, err | ||
tlsConf := &tlsutil.Config{ | ||
VerifyIncoming: config.VerifyIncoming, | ||
VerifyOutgoing: config.VerifyOutgoing, | ||
CAFile: config.CAFile, | ||
CertFile: config.CertFile, | ||
KeyFile: config.KeyFile, | ||
NodeName: config.NodeName, | ||
ServerName: config.ServerName} | ||
|
||
tlsConfig, err = tlsConf.IncomingTLSConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ln, err := net.Listen("tcp", httpAddr.String()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
list = tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, tlsConfig) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we use the tcpKeepAliveListener just for TLS? |
||
|
||
// Create the mux | ||
mux := http.NewServeMux() | ||
|
||
// Create the server | ||
srv := &HTTPServer{ | ||
agent: agent, | ||
mux: mux, | ||
listener: list, | ||
logger: log.New(logOutput, "", log.LstdFlags), | ||
uiDir: config.UiDir, | ||
addr: httpAddr.String(), | ||
} | ||
srv.registerHandlers(config.EnableDebug) | ||
|
||
// Start the server | ||
go http.Serve(list, mux) | ||
|
||
servers := make([]*HTTPServer, 1) | ||
servers[0] = srv | ||
} | ||
|
||
// Create the server | ||
srv := &HTTPServer{ | ||
agent: agent, | ||
mux: mux, | ||
listener: list, | ||
logger: log.New(logOutput, "", log.LstdFlags), | ||
uiDir: uiDir, | ||
if config.Ports.HTTP > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above comment for port |
||
httpAddr, err = config.ClientListener(config.Addresses.HTTP, config.Ports.HTTP) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to get ClientListener address:port: %v", err) | ||
} | ||
|
||
// Create non-TLS listener | ||
ln, err := net.Listen("tcp", httpAddr.String()) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to get Listen on %s: %v", httpAddr.String(), err) | ||
} | ||
|
||
list = tcpKeepAliveListener{ln.(*net.TCPListener)} | ||
|
||
// Create the mux | ||
mux := http.NewServeMux() | ||
|
||
// Create the server | ||
srv := &HTTPServer{ | ||
agent: agent, | ||
mux: mux, | ||
listener: list, | ||
logger: log.New(logOutput, "", log.LstdFlags), | ||
uiDir: config.UiDir, | ||
addr: httpAddr.String(), | ||
} | ||
srv.registerHandlers(config.EnableDebug) | ||
|
||
// Start the server | ||
go http.Serve(list, mux) | ||
|
||
if servers != nil { | ||
// we already have the https server in servers, append | ||
servers = append(servers, srv) | ||
} else { | ||
servers := make([]*HTTPServer, 1) | ||
servers[0] = srv | ||
} | ||
} | ||
srv.registerHandlers(enableDebug) | ||
|
||
// Start the server | ||
go http.Serve(list, mux) | ||
return srv, nil | ||
return servers, nil | ||
} | ||
|
||
// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted | ||
// connections. It's used by NewHttpServer so | ||
// dead TCP connections eventually go away. | ||
type tcpKeepAliveListener struct { | ||
*net.TCPListener | ||
} | ||
|
||
func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) { | ||
tc, err := ln.AcceptTCP() | ||
if err != nil { | ||
return | ||
} | ||
tc.SetKeepAlive(true) | ||
tc.SetKeepAlivePeriod(30 * time.Second) | ||
return tc, nil | ||
} | ||
|
||
// Shutdown is used to shutdown the HTTP server | ||
func (s *HTTPServer) Shutdown() { | ||
s.listener.Close() | ||
if s != nil { | ||
s.logger.Printf("[DEBUG] http: Shutting down http server(%v)", s.addr) | ||
s.listener.Close() | ||
} | ||
} | ||
|
||
// registerHandlers is used to attach our handlers to the mux | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might make tests easier if this test was for
>= 0
. That way we can just bind to port 0 for tests to get a vacant port.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left this logic the way it was for just the HTTP case before. Also, using 0 will mean that the underlying OS will use the next available dynamic port which is not what we want since HTTP/HTTPS need to be on known ports for clients to communicate with them and so it would be better not to get into that situation by keeping the test > 0 like it was already for HTTP.