Skip to content
This repository has been archived by the owner on Aug 25, 2023. It is now read-only.

Commit

Permalink
Merge pull request #48 from aholovko/issue-47
Browse files Browse the repository at this point in the history
fix: make TLS optional
  • Loading branch information
fqutishat authored Aug 31, 2020
2 parents 49dab4a + 3a8e216 commit a583732
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
14 changes: 9 additions & 5 deletions cmd/kms-rest/startcmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,19 @@ const (
var logger = log.New("hub-kms/startcmd")

type server interface {
ListenAndServeTLS(host, certFile, keyFile string, router http.Handler) error
ListenAndServe(host, certFile, keyFile string, router http.Handler) error
}

// HTTPServer represents an actual HTTP server implementation.
type HTTPServer struct{}

// ListenAndServeTLS starts the server using the standard Go HTTPS implementation.
func (s *HTTPServer) ListenAndServeTLS(host, certFile, keyFile string, router http.Handler) error {
return http.ListenAndServeTLS(host, certFile, keyFile, router)
// ListenAndServe starts the server using the standard Go HTTP/HTTPS implementation.
func (s *HTTPServer) ListenAndServe(host, certFile, keyFile string, router http.Handler) error {
if certFile != "" && keyFile != "" {
return http.ListenAndServeTLS(host, certFile, keyFile, router)
}

return http.ListenAndServe(host, router)
}

// GetStartCmd returns the Cobra start command.
Expand Down Expand Up @@ -293,7 +297,7 @@ func startKmsService(parameters *kmsRestParameters, srv server) error {

logger.Infof("starting KMS service on host %s", parameters.hostURL)

return srv.ListenAndServeTLS(
return srv.ListenAndServe(
parameters.hostURL,
parameters.tlsParams.serveCertPath,
parameters.tlsParams.serveKeyPath,
Expand Down
21 changes: 15 additions & 6 deletions cmd/kms-rest/startcmd/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,24 @@ const (

type mockServer struct{}

func (s *mockServer) ListenAndServeTLS(host, certFile, keyFile string, router http.Handler) error {
func (s *mockServer) ListenAndServe(host, certFile, keyFile string, router http.Handler) error {
return nil
}

func TestListenAndServeTLS(t *testing.T) {
var w HTTPServer
err := w.ListenAndServeTLS("wronghost", "", "", nil)
require.Error(t, err)
require.Contains(t, err.Error(), "address wronghost: missing port in address")
func TestListenAndServe(t *testing.T) {
t.Run("test wrong host", func(t *testing.T) {
var w HTTPServer
err := w.ListenAndServe("wronghost", "", "", nil)
require.Error(t, err)
require.Contains(t, err.Error(), "address wronghost: missing port in address")
})

t.Run("test invalid key file", func(t *testing.T) {
var w HTTPServer
err := w.ListenAndServe("localhost:8080", "test.key", "test.cert", nil)
require.Error(t, err)
require.Contains(t, err.Error(), "open test.key: no such file or directory")
})
}

func TestStartCmdContents(t *testing.T) {
Expand Down

0 comments on commit a583732

Please sign in to comment.