From 107b7419b7830e8af99fa0f4fb6da35479ddcff8 Mon Sep 17 00:00:00 2001 From: Jake Sanders Date: Wed, 9 Oct 2019 16:42:05 +0100 Subject: [PATCH] Start abstracting away the ACME provider (#830) * Start abstracting away the ACME provider * Move ACME to interface with sub-package implementations * Addressing comments * Library -> Provider * Missed a couple of Library -> Provider * One more Library -> Provider * remove constants --- api/server/acme/acme.go | 16 ++++++++++++++++ api/server/acme/autocert/autocert.go | 22 ++++++++++++++++++++++ api/server/acme/autocert/autocert_test.go | 15 +++++++++++++++ api/server/http/http.go | 3 +-- api/server/options.go | 21 +++++++++++++++------ 5 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 api/server/acme/acme.go create mode 100644 api/server/acme/autocert/autocert.go create mode 100644 api/server/acme/autocert/autocert_test.go diff --git a/api/server/acme/acme.go b/api/server/acme/acme.go new file mode 100644 index 00000000..0bf3698d --- /dev/null +++ b/api/server/acme/acme.go @@ -0,0 +1,16 @@ +// Package acme abstracts away various ACME libraries +package acme + +import ( + "errors" + "net" +) + +var ( + ErrProviderNotImplemented = errors.New("Provider not implemented") +) + +// Provider is a ACME provider interface +type Provider interface { + NewListener(...string) (net.Listener, error) +} diff --git a/api/server/acme/autocert/autocert.go b/api/server/acme/autocert/autocert.go new file mode 100644 index 00000000..9a760baf --- /dev/null +++ b/api/server/acme/autocert/autocert.go @@ -0,0 +1,22 @@ +// Package autocert is the ACME interpreter from golang.org/x/crypto/acme/autocert +package autocert + +import ( + "net" + + "github.com/micro/go-micro/api/server/acme" + "golang.org/x/crypto/acme/autocert" +) + +// autoCertACME is the ACME provider from golang.org/x/crypto/acme/autocert +type autocertProvider struct{} + +// NewListener implements acme.Provider +func (a *autocertProvider) NewListener(ACMEHosts ...string) (net.Listener, error) { + return autocert.NewListener(ACMEHosts...), nil +} + +// New returns an autocert acme.Provider +func New() acme.Provider { + return &autocertProvider{} +} diff --git a/api/server/acme/autocert/autocert_test.go b/api/server/acme/autocert/autocert_test.go new file mode 100644 index 00000000..4b8ab27c --- /dev/null +++ b/api/server/acme/autocert/autocert_test.go @@ -0,0 +1,15 @@ +package autocert + +import ( + "testing" +) + +func TestAutocert(t *testing.T) { + l := New() + if _, ok := l.(*autocertProvider); !ok { + t.Error("New() didn't return an autocertProvider") + } + if _, err := l.NewListener(); err != nil { + t.Error(err.Error()) + } +} diff --git a/api/server/http/http.go b/api/server/http/http.go index 0990dd8f..91d16339 100644 --- a/api/server/http/http.go +++ b/api/server/http/http.go @@ -11,7 +11,6 @@ import ( "github.com/gorilla/handlers" "github.com/micro/go-micro/api/server" "github.com/micro/go-micro/util/log" - "golang.org/x/crypto/acme/autocert" ) type httpServer struct { @@ -55,7 +54,7 @@ func (s *httpServer) Start() error { if s.opts.EnableACME { // should we check the address to make sure its using :443? - l = autocert.NewListener(s.opts.ACMEHosts...) + l, err = s.opts.ACMEProvider.NewListener(s.opts.ACMEHosts...) } else if s.opts.EnableTLS && s.opts.TLSConfig != nil { l, err = tls.Listen("tcp", s.address, s.opts.TLSConfig) } else { diff --git a/api/server/options.go b/api/server/options.go index cd47562f..b94c3da8 100644 --- a/api/server/options.go +++ b/api/server/options.go @@ -2,15 +2,24 @@ package server import ( "crypto/tls" + + "github.com/micro/go-micro/api/server/acme" ) type Option func(o *Options) type Options struct { - EnableACME bool - EnableTLS bool - ACMEHosts []string - TLSConfig *tls.Config + EnableACME bool + ACMEProvider acme.Provider + EnableTLS bool + ACMEHosts []string + TLSConfig *tls.Config +} + +func EnableACME(b bool) Option { + return func(o *Options) { + o.EnableACME = b + } } func ACMEHosts(hosts ...string) Option { @@ -19,9 +28,9 @@ func ACMEHosts(hosts ...string) Option { } } -func EnableACME(b bool) Option { +func ACMEProvider(p acme.Provider) Option { return func(o *Options) { - o.EnableACME = b + o.ACMEProvider = p } }