mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-27 22:23:48 +03:00
acmeserver: support additional database types beside bbolt
This commit is contained in:
parent
c78ebb3d6a
commit
f94affbc39
1 changed files with 39 additions and 7 deletions
|
@ -20,6 +20,7 @@ import (
|
||||||
weakrand "math/rand"
|
weakrand "math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
@ -52,6 +53,10 @@ type Handler struct {
|
||||||
// the default ID is "local".
|
// the default ID is "local".
|
||||||
CA string `json:"ca,omitempty"`
|
CA string `json:"ca,omitempty"`
|
||||||
|
|
||||||
|
// The connection string of the database used for
|
||||||
|
// the account data of the ACME clients
|
||||||
|
Database string `json:"database,omitempty"`
|
||||||
|
|
||||||
// The lifetime for issued certificates
|
// The lifetime for issued certificates
|
||||||
Lifetime caddy.Duration `json:"lifetime,omitempty"`
|
Lifetime caddy.Duration `json:"lifetime,omitempty"`
|
||||||
|
|
||||||
|
@ -153,6 +158,12 @@ func (ash *Handler) Provision(ctx caddy.Context) error {
|
||||||
return fmt.Errorf("certificate lifetime (%s) should be less than intermediate certificate lifetime (%s)", time.Duration(ash.Lifetime), time.Duration(ca.IntermediateLifetime))
|
return fmt.Errorf("certificate lifetime (%s) should be less than intermediate certificate lifetime (%s)", time.Duration(ash.Lifetime), time.Duration(ca.IntermediateLifetime))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
repl, ok := ctx.Context.Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||||
|
if !ok {
|
||||||
|
repl = caddy.NewReplacer()
|
||||||
|
ctx.Context = context.WithValue(ctx.Context, caddy.ReplacerCtxKey, repl)
|
||||||
|
}
|
||||||
|
ash.Database = repl.ReplaceKnown(ash.Database, "")
|
||||||
database, err := ash.openDatabase()
|
database, err := ash.openDatabase()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -248,17 +259,38 @@ func (ash Handler) Cleanup() error {
|
||||||
func (ash Handler) openDatabase() (*db.AuthDB, error) {
|
func (ash Handler) openDatabase() (*db.AuthDB, error) {
|
||||||
key := ash.getDatabaseKey()
|
key := ash.getDatabaseKey()
|
||||||
database, loaded, err := databasePool.LoadOrNew(key, func() (caddy.Destructor, error) {
|
database, loaded, err := databasePool.LoadOrNew(key, func() (caddy.Destructor, error) {
|
||||||
dbFolder := filepath.Join(caddy.AppDataDir(), "acme_server", key)
|
var dsn string
|
||||||
dbPath := filepath.Join(dbFolder, "db")
|
dburl, err := url.Parse(ash.Database)
|
||||||
|
|
||||||
err := os.MkdirAll(dbFolder, 0o755)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if dburl.Scheme == "" {
|
||||||
|
dburl.Scheme = "bbolt"
|
||||||
|
}
|
||||||
|
var dbtype string
|
||||||
|
switch dburl.Scheme {
|
||||||
|
case "postgresql", "postgres", "psql":
|
||||||
|
dbtype = nosql.PostgreSQLDriver // normalize the postgres identifier
|
||||||
|
dsn = ash.Database
|
||||||
|
case "mysql":
|
||||||
|
dbtype = nosql.MySQLDriver
|
||||||
|
dsn = ash.Database
|
||||||
|
case "bbolt":
|
||||||
|
dbtype = nosql.BBoltDriver
|
||||||
|
dbFolder := filepath.Join(caddy.AppDataDir(), "acme_server", key)
|
||||||
|
dsn = filepath.Join(dbFolder, "db")
|
||||||
|
if err := os.MkdirAll(dbFolder, 0o755); err != nil {
|
||||||
return nil, fmt.Errorf("making folder for CA database: %v", err)
|
return nil, fmt.Errorf("making folder for CA database: %v", err)
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
// Although smallstep/nosql rejects unrecognized database, we
|
||||||
|
// reject them here to avoid surprises. We also reject 'badger'.
|
||||||
|
return nil, fmt.Errorf("unsupported database type: %s", dburl.Scheme)
|
||||||
|
}
|
||||||
|
|
||||||
dbConfig := &db.Config{
|
dbConfig := &db.Config{
|
||||||
Type: "bbolt",
|
Type: dbtype,
|
||||||
DataSource: dbPath,
|
DataSource: dsn,
|
||||||
}
|
}
|
||||||
database, err := db.New(dbConfig)
|
database, err := db.New(dbConfig)
|
||||||
return databaseCloser{&database}, err
|
return databaseCloser{&database}, err
|
||||||
|
|
Loading…
Reference in a new issue