@marcel91200 / caddy Mirror · read-only / commits / 51e3fdba
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

caddytls: Save email with account if not already specified

I'm pretty sure this fixes a bug when the default email is used...

Matthew Holt committed Feb 10, 2021 at 19:49 UTC 51e3fdba7738e577b5ccfbad5bd7712fa594afc0
3 files changed +23 -15
go.mod
+1 -1
@@ -6,7 +6,7 @@ require (
6 github.com/Masterminds/sprig/v3 v3.1.0
7 github.com/alecthomas/chroma v0.8.2
8 github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a
9 - github.com/caddyserver/certmagic v0.12.1-0.20210126230115-267fdad76a0f
9 + github.com/caddyserver/certmagic v0.12.1-0.20210211020017-ebb8d8b435b4
10 github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac
11 github.com/go-chi/chi v4.1.2+incompatible
12 github.com/google/cel-go v0.6.0
go.sum
+2 -2
@@ -99,8 +99,8 @@ github.com/bombsimon/wsl/v2 v2.0.0/go.mod h1:mf25kr/SqFEPhhcxW1+7pxzGlW+hIl/hYTK
99 github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
100 github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g=
101 github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s=
102 -github.com/caddyserver/certmagic v0.12.1-0.20210126230115-267fdad76a0f h1:uJoft/gLxPvKq+ojfq3k7w8deji/xt/1RSWN7OAk6Ng=
103 -github.com/caddyserver/certmagic v0.12.1-0.20210126230115-267fdad76a0f/go.mod h1:CUPfwomVXGCyV77EQbR3v7H4tGJ4pX16HATeR55rqws=
102 +github.com/caddyserver/certmagic v0.12.1-0.20210211020017-ebb8d8b435b4 h1:YPHanayqEADEHFxGui7lqQ0tx4rypJaD2y4Y7Tip/ks=
103 +github.com/caddyserver/certmagic v0.12.1-0.20210211020017-ebb8d8b435b4/go.mod h1:CUPfwomVXGCyV77EQbR3v7H4tGJ4pX16HATeR55rqws=
104 github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
105 github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
106 github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
modules/caddytls/zerosslissuer.go
+20 -12
@@ -68,16 +68,20 @@ func (iss *ZeroSSLIssuer) Provision(ctx caddy.Context) error {
68 return iss.ACMEIssuer.Provision(ctx)
69 }
70
71 -func (iss *ZeroSSLIssuer) newAccountCallback(ctx context.Context, am *certmagic.ACMEManager, _ acme.Account) error {
71 +// newAccountCallback generates EAB if not already provided. It also sets a valid default contact on the account if not set.
72 +func (iss *ZeroSSLIssuer) newAccountCallback(ctx context.Context, am *certmagic.ACMEManager, acct acme.Account) (acme.Account, error) {
73 if am.ExternalAccount != nil {
73 - return nil
74 + return acct, nil
75 }
76 var err error
76 - am.ExternalAccount, err = iss.generateEABCredentials(ctx)
77 - return err
77 + am.ExternalAccount, acct, err = iss.generateEABCredentials(ctx, acct)
78 + return acct, err
79 }
80
80 -func (iss *ZeroSSLIssuer) generateEABCredentials(ctx context.Context) (*acme.EAB, error) {
81 +// generateEABCredentials generates EAB credentials using the API key if provided,
82 +// otherwise using the primary contact email on the issuer. If an email is not set
83 +// on the issuer, a default generic email is used.
84 +func (iss *ZeroSSLIssuer) generateEABCredentials(ctx context.Context, acct acme.Account) (*acme.EAB, acme.Account, error) {
85 var endpoint string
86 var body io.Reader
87
@@ -86,7 +90,7 @@ func (iss *ZeroSSLIssuer) generateEABCredentials(ctx context.Context) (*acme.EAB
90 if iss.APIKey != "" {
91 apiKey := caddy.NewReplacer().ReplaceAll(iss.APIKey, "")
92 if apiKey == "" {
89 - return nil, fmt.Errorf("missing API key: '%v'", iss.APIKey)
93 + return nil, acct, fmt.Errorf("missing API key: '%v'", iss.APIKey)
94 }
95 qs := url.Values{"access_key": []string{apiKey}}
96 endpoint = fmt.Sprintf("%s/eab-credentials?%s", zerosslAPIBase, qs.Encode())
@@ -96,6 +100,10 @@ func (iss *ZeroSSLIssuer) generateEABCredentials(ctx context.Context) (*acme.EAB
100 iss.logger.Warn("missing email address for ZeroSSL; it is strongly recommended to set one for next time")
101 email = "caddy@zerossl.com" // special email address that preserves backwards-compat, but which black-holes dashboard features, oh well
102 }
103 + if len(acct.Contact) == 0 {
104 + // we borrow the email from config or the default email, so ensure it's saved with the account
105 + acct.Contact = []string{"mailto:" + email}
106 + }
107 endpoint = zerosslAPIBase + "/eab-credentials-email"
108 form := url.Values{"email": []string{email}}
109 body = strings.NewReader(form.Encode())
@@ -103,7 +111,7 @@ func (iss *ZeroSSLIssuer) generateEABCredentials(ctx context.Context) (*acme.EAB
111
112 req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, body)
113 if err != nil {
106 - return nil, fmt.Errorf("forming request: %v", err)
114 + return nil, acct, fmt.Errorf("forming request: %v", err)
115 }
116 if body != nil {
117 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
@@ -112,7 +120,7 @@ func (iss *ZeroSSLIssuer) generateEABCredentials(ctx context.Context) (*acme.EAB
120
121 resp, err := http.DefaultClient.Do(req)
122 if err != nil {
115 - return nil, fmt.Errorf("performing EAB credentials request: %v", err)
123 + return nil, acct, fmt.Errorf("performing EAB credentials request: %v", err)
124 }
125 defer resp.Body.Close()
126
@@ -127,14 +135,14 @@ func (iss *ZeroSSLIssuer) generateEABCredentials(ctx context.Context) (*acme.EAB
135 }
136 err = json.NewDecoder(resp.Body).Decode(&result)
137 if err != nil {
130 - return nil, fmt.Errorf("decoding API response: %v", err)
138 + return nil, acct, fmt.Errorf("decoding API response: %v", err)
139 }
140 if result.Error.Code != 0 {
133 - return nil, fmt.Errorf("failed getting EAB credentials: HTTP %d: %s (code %d)",
141 + return nil, acct, fmt.Errorf("failed getting EAB credentials: HTTP %d: %s (code %d)",
142 resp.StatusCode, result.Error.Type, result.Error.Code)
143 }
144 if resp.StatusCode != http.StatusOK {
137 - return nil, fmt.Errorf("failed getting EAB credentials: HTTP %d", resp.StatusCode)
145 + return nil, acct, fmt.Errorf("failed getting EAB credentials: HTTP %d", resp.StatusCode)
146 }
147
148 iss.logger.Info("generated EAB credentials", zap.String("key_id", result.EABKID))
@@ -142,7 +150,7 @@ func (iss *ZeroSSLIssuer) generateEABCredentials(ctx context.Context) (*acme.EAB
150 return &acme.EAB{
151 KeyID: result.EABKID,
152 MACKey: result.EABHMACKey,
145 - }, nil
153 + }, acct, nil
154 }
155
156 // initialize modifies the template for the underlying ACMEManager