Skip to content

Commit 64147e3

Browse files
committed
Adds custom ACME providers joohoi#310
1 parent bb086da commit 64147e3

File tree

4 files changed

+50
-49
lines changed

4 files changed

+50
-49
lines changed

README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ $ dig -t txt @auth.example.org d420c923-bbd7-4056-ab64-c3ca54c9b3cf.auth.example
228228

229229
## Configuration
230230

231-
```bash
231+
```toml
232232
[general]
233233
# DNS interface. Note that systemd-resolved may reserve port 53 on 127.0.0.53
234234
# In this case acme-dns will error out and you will need to define the listening interface
@@ -240,7 +240,7 @@ protocol = "both"
240240
domain = "auth.example.org"
241241
# zone name server
242242
nsname = "auth.example.org"
243-
# admin email address, where @ is substituted with .
243+
# admin email address, where @ is substituted with .
244244
nsadmin = "admin.example.org"
245245
# predefined records served in addition to the TXT
246246
records = [
@@ -267,13 +267,15 @@ ip = "0.0.0.0"
267267
disable_registration = false
268268
# listen port, eg. 443 for default HTTPS
269269
port = "443"
270-
# possible values: "letsencrypt", "letsencryptstaging", "cert", "none"
270+
# possible values: "letsencrypt", "letsencryptstaging", "custom", "cert", "none"
271271
tls = "letsencryptstaging"
272272
# only used if tls = "cert"
273273
tls_cert_privkey = "/etc/tls/example.org/privkey.pem"
274274
tls_cert_fullchain = "/etc/tls/example.org/fullchain.pem"
275-
# only used if tls = "letsencrypt"
275+
# only used if tls = "letsencrypt", "letsencryptstaging", or "custom"
276276
acme_cache_dir = "api-certs"
277+
# only used if tls = "custom"
278+
acme_dir = "https://acme-v02.example.com/directory"
277279
# optional e-mail address to which Let's Encrypt will send expiration notices for the API's cert
278280
notification_email = ""
279281
# CORS AllowOrigins, wildcards can be used
@@ -397,4 +399,4 @@ If you have an idea for improvement, please open an new issue or feel free to wr
397399

398400
## License
399401

400-
acme-dns is released under the [MIT License](http://www.opensource.org/licenses/MIT).
402+
acme-dns is released under the [MIT License](http://www.opensource.org/licenses/MIT).

config.cfg

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@ ip = "0.0.0.0"
3636
disable_registration = false
3737
# listen port, eg. 443 for default HTTPS
3838
port = "443"
39-
# possible values: "letsencrypt", "letsencryptstaging", "cert", "custom", "none"
39+
# possible values: "letsencrypt", "letsencryptstaging", "custom", "cert", "none"
4040
tls = "letsencryptstaging"
4141
# only used if tls = "cert"
4242
tls_cert_privkey = "/etc/tls/example.org/privkey.pem"
4343
tls_cert_fullchain = "/etc/tls/example.org/fullchain.pem"
4444
# only used if tls = "custom"
4545
acme_server = "https://my.acme.server"
46-
# only used if tls = "letsencrypt" or "custom"
46+
# only used if tls = "letsencrypt", "letsencryptstaging" or "custom"
4747
acme_cache_dir = "api-certs"
48+
# only used if tls = "custom"
49+
acme_dir = "https://acme-v02.example.com/directory"
4850
# optional e-mail address to which Let's Encrypt will send expiration notices for the API's cert
4951
notification_email = ""
5052
# CORS AllowOrigins, wildcards can be used

main.go

+13-26
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,16 @@ func startHTTPAPI(errChan chan error, config DNSConfig, dnsservers []*DNSServer)
153153
// Set up certmagic for getting certificate for acme-dns api
154154
certmagic.DefaultACME.DNS01Solver = &provider
155155
certmagic.DefaultACME.Agreed = true
156-
if Config.API.TLS == "letsencrypt" {
156+
switch config.API.TLS {
157+
case TlsTypeLetsEncrypt:
157158
certmagic.DefaultACME.CA = certmagic.LetsEncryptProductionCA
158-
} else {
159+
case TlsTypeAcmeCustom:
160+
certmagic.DefaultACME.CA = config.API.ACMEDir
161+
case TlsTypeLetsEncryptStaging:
162+
default:
159163
certmagic.DefaultACME.CA = certmagic.LetsEncryptStagingCA
160164
}
161-
if Config.API.TLS == "custom" {
162-
certmagic.DefaultACME.CA = Config.API.ACMEDomain
163-
}
164-
certmagic.DefaultACME.Email = Config.API.NotificationEmail
165+
certmagic.DefaultACME.Email = Config.API.ACMENotificationEmail
165166
magicConf := certmagic.NewDefault()
166167
magicConf.Storage = &storage
167168
magicConf.DefaultServerName = Config.General.Domain
@@ -174,24 +175,10 @@ func startHTTPAPI(errChan chan error, config DNSConfig, dnsservers []*DNSServer)
174175

175176
magic := certmagic.New(magicCache, *magicConf)
176177
var err error
177-
switch Config.API.TLS {
178-
case "letsencryptstaging":
179-
err = magic.ManageAsync(context.Background(), []string{Config.General.Domain})
180-
if err != nil {
181-
errChan <- err
182-
return
183-
}
184-
cfg.GetCertificate = magic.GetCertificate
185-
186-
srv := &http.Server{
187-
Addr: host,
188-
Handler: c.Handler(api),
189-
TLSConfig: cfg,
190-
ErrorLog: stdlog.New(logwriter, "", 0),
191-
}
192-
log.WithFields(log.Fields{"host": host, "domain": Config.General.Domain}).Info("Listening HTTPS")
193-
err = srv.ListenAndServeTLS("", "")
194-
case "letsencrypt", "custom":
178+
switch config.API.TLS {
179+
case TlsTypeLetsEncrypt:
180+
case TlsTypeLetsEncryptStaging:
181+
case TlsTypeAcmeCustom:
195182
err = magic.ManageAsync(context.Background(), []string{Config.General.Domain})
196183
if err != nil {
197184
errChan <- err
@@ -206,15 +193,15 @@ func startHTTPAPI(errChan chan error, config DNSConfig, dnsservers []*DNSServer)
206193
}
207194
log.WithFields(log.Fields{"host": host, "domain": Config.General.Domain}).Info("Listening HTTPS")
208195
err = srv.ListenAndServeTLS("", "")
209-
case "cert":
196+
case TlsTypeCert:
210197
srv := &http.Server{
211198
Addr: host,
212199
Handler: c.Handler(api),
213200
TLSConfig: cfg,
214201
ErrorLog: stdlog.New(logwriter, "", 0),
215202
}
216203
log.WithFields(log.Fields{"host": host}).Info("Listening HTTPS")
217-
err = srv.ListenAndServeTLS(Config.API.TLSCertFullchain, Config.API.TLSCertPrivkey)
204+
err = srv.ListenAndServeTLS(config.API.TLSCertFullchain, config.API.TLSCertPrivkey)
218205
default:
219206
log.WithFields(log.Fields{"host": host}).Info("Listening HTTP")
220207
err = http.ListenAndServe(host, c.Handler(api))

types.go

+26-16
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,30 @@ type dbsettings struct {
3737
Connection string
3838
}
3939

40+
const (
41+
TlsTypeLetsEncrypt = "letsencrypt"
42+
TlsTypeLetsEncryptStaging = "letsencryptstaging"
43+
TlsTypeAcmeCustom = "custom"
44+
TlsTypeCert = "cert"
45+
TlsTypeNone = "none"
46+
)
47+
4048
// API config
4149
type httpapi struct {
42-
Domain string `toml:"api_domain"`
43-
IP string
44-
DisableRegistration bool `toml:"disable_registration"`
45-
AutocertPort string `toml:"autocert_port"`
46-
Port string `toml:"port"`
47-
TLS string
48-
TLSCertPrivkey string `toml:"tls_cert_privkey"`
49-
TLSCertFullchain string `toml:"tls_cert_fullchain"`
50-
ACMEDomain string `toml:"acme_domain"`
51-
ACMECacheDir string `toml:"acme_cache_dir"`
52-
NotificationEmail string `toml:"notification_email"`
53-
CorsOrigins []string
54-
UseHeader bool `toml:"use_header"`
55-
HeaderName string `toml:"header_name"`
50+
Domain string `toml:"api_domain"`
51+
IP string
52+
DisableRegistration bool `toml:"disable_registration"`
53+
AutocertPort string `toml:"autocert_port"`
54+
Port string `toml:"port"`
55+
TLS string
56+
TLSCertPrivkey string `toml:"tls_cert_privkey"`
57+
TLSCertFullchain string `toml:"tls_cert_fullchain"`
58+
ACMECacheDir string `toml:"acme_cache_dir"`
59+
ACMEDir string `toml:"acme_dir"`
60+
ACMENotificationEmail string `toml:"notification_email"`
61+
CorsOrigins []string
62+
UseHeader bool `toml:"use_header"`
63+
HeaderName string `toml:"header_name"`
5664
}
5765

5866
// Logging config
@@ -64,7 +72,7 @@ type logconfig struct {
6472
}
6573

6674
type acmedb struct {
67-
Mutex sync.Mutex
75+
sync.Mutex
6876
DB *sql.DB
6977
}
7078

@@ -77,4 +85,6 @@ type database interface {
7785
GetBackend() *sql.DB
7886
SetBackend(*sql.DB)
7987
Close()
80-
}
88+
Lock()
89+
Unlock()
90+
}

0 commit comments

Comments
 (0)