-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add TLS support for metric beat http server #11482
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,11 +19,13 @@ package http | |||||
|
||||||
import ( | ||||||
"context" | ||||||
"fmt" | ||||||
"io/ioutil" | ||||||
"net" | ||||||
"net/http" | ||||||
"strconv" | ||||||
|
||||||
"github.com/elastic/beats/libbeat/common" | ||||||
"github.com/elastic/beats/libbeat/common/transport/tlscommon" | ||||||
"github.com/elastic/beats/libbeat/logp" | ||||||
"github.com/elastic/beats/metricbeat/helper/server" | ||||||
"github.com/elastic/beats/metricbeat/mb" | ||||||
|
@@ -57,6 +59,11 @@ func NewHttpServer(mb mb.BaseMetricSet) (server.Server, error) { | |||||
return nil, err | ||||||
} | ||||||
|
||||||
tlsConfig, err := tlscommon.LoadTLSServerConfig(config.TLS) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
ctx, cancel := context.WithCancel(context.Background()) | ||||||
h := &HttpServer{ | ||||||
done: make(chan struct{}), | ||||||
|
@@ -66,21 +73,32 @@ func NewHttpServer(mb mb.BaseMetricSet) (server.Server, error) { | |||||
} | ||||||
|
||||||
httpServer := &http.Server{ | ||||||
Addr: fmt.Sprintf("%s:%d", config.Host, config.Port), | ||||||
Addr: net.JoinHostPort(config.Host, strconv.Itoa(int(config.Port))), | ||||||
Handler: http.HandlerFunc(h.handleFunc), | ||||||
} | ||||||
if tlsConfig != nil { | ||||||
httpServer.TLSConfig = tlsConfig.BuildModuleConfig(config.Host) | ||||||
} | ||||||
h.server = httpServer | ||||||
|
||||||
return h, nil | ||||||
} | ||||||
|
||||||
func (h *HttpServer) Start() error { | ||||||
go func() { | ||||||
|
||||||
logp.Info("Starting http server on %s", h.server.Addr) | ||||||
err := h.server.ListenAndServe() | ||||||
if err != nil && err != http.ErrServerClosed { | ||||||
logp.Critical("Unable to start HTTP server due to error: %v", err) | ||||||
if h.server.TLSConfig != nil { | ||||||
logp.Info("Starting HTTPS server on %s", h.server.Addr) | ||||||
//certificate is already loaded. That's why the parameters are empty | ||||||
err := h.server.ListenAndServeTLS("", "") | ||||||
if err != nil && err != http.ErrServerClosed { | ||||||
logp.Critical("Unable to start HTTPS server due to error: %v", err) | ||||||
} | ||||||
} else { | ||||||
logp.Info("Starting HTTP server on %s", h.server.Addr) | ||||||
err := h.server.ListenAndServe() | ||||||
if err != nil && err != http.ErrServerClosed { | ||||||
logp.Critical("Unable to start HTTP server due to error: %v", err) | ||||||
} | ||||||
} | ||||||
}() | ||||||
|
||||||
|
@@ -130,6 +148,11 @@ func (h *HttpServer) handleFunc(writer http.ResponseWriter, req *http.Request) { | |||||
|
||||||
case "GET": | ||||||
writer.WriteHeader(http.StatusOK) | ||||||
writer.Write([]byte("HTTP Server accepts data via POST")) | ||||||
if h.server.TLSConfig != nil && h.server.TLSConfig.Certificates != nil { | ||||||
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.
Suggested change
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. This can't be done. I've tested and for some reason, HTTP server creates a TLSConfig after calling ListenAndServe(), so the HTTP server will print the wrong message if we remove that, and HTTP tests will fail. 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. Oh, OK. Then I'm glad you tested it! |
||||||
writer.Write([]byte("HTTPS Server accepts data via POST")) | ||||||
} else { | ||||||
writer.Write([]byte("HTTP Server accepts data via POST")) | ||||||
} | ||||||
|
||||||
} | ||||||
} |
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.
Is it HTTP here?
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.
Hi Kaiyan, it is HTTPS.
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.
Hummmm... there is this logp.Critical that is not adjusted.. thanks i will change it now