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
This commit is contained in:
parent
44473f954f
commit
107b7419b7
16
api/server/acme/acme.go
Normal file
16
api/server/acme/acme.go
Normal file
@ -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)
|
||||||
|
}
|
22
api/server/acme/autocert/autocert.go
Normal file
22
api/server/acme/autocert/autocert.go
Normal file
@ -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{}
|
||||||
|
}
|
15
api/server/acme/autocert/autocert_test.go
Normal file
15
api/server/acme/autocert/autocert_test.go
Normal file
@ -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())
|
||||||
|
}
|
||||||
|
}
|
@ -11,7 +11,6 @@ import (
|
|||||||
"github.com/gorilla/handlers"
|
"github.com/gorilla/handlers"
|
||||||
"github.com/micro/go-micro/api/server"
|
"github.com/micro/go-micro/api/server"
|
||||||
"github.com/micro/go-micro/util/log"
|
"github.com/micro/go-micro/util/log"
|
||||||
"golang.org/x/crypto/acme/autocert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type httpServer struct {
|
type httpServer struct {
|
||||||
@ -55,7 +54,7 @@ func (s *httpServer) Start() error {
|
|||||||
|
|
||||||
if s.opts.EnableACME {
|
if s.opts.EnableACME {
|
||||||
// should we check the address to make sure its using :443?
|
// 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 {
|
} else if s.opts.EnableTLS && s.opts.TLSConfig != nil {
|
||||||
l, err = tls.Listen("tcp", s.address, s.opts.TLSConfig)
|
l, err = tls.Listen("tcp", s.address, s.opts.TLSConfig)
|
||||||
} else {
|
} else {
|
||||||
|
@ -2,26 +2,35 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/api/server/acme"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Option func(o *Options)
|
type Option func(o *Options)
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
EnableACME bool
|
EnableACME bool
|
||||||
|
ACMEProvider acme.Provider
|
||||||
EnableTLS bool
|
EnableTLS bool
|
||||||
ACMEHosts []string
|
ACMEHosts []string
|
||||||
TLSConfig *tls.Config
|
TLSConfig *tls.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func EnableACME(b bool) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.EnableACME = b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func ACMEHosts(hosts ...string) Option {
|
func ACMEHosts(hosts ...string) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.ACMEHosts = hosts
|
o.ACMEHosts = hosts
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func EnableACME(b bool) Option {
|
func ACMEProvider(p acme.Provider) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.EnableACME = b
|
o.ACMEProvider = p
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user