Skip to content
This repository was archived by the owner on Sep 7, 2022. It is now read-only.

Commit 5f59e88

Browse files
committed
Add support for ACME_CHALLENGE_DIR environment variable
1 parent 2c58819 commit 5f59e88

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

cmd/acme/cert.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io/ioutil"
2222
"net"
2323
"net/http"
24+
"os"
2425
"path/filepath"
2526
"time"
2627

@@ -167,14 +168,18 @@ func authz(client *acme.Client, zurl, domain string) error {
167168
if certManual {
168169
// manual challenge response
169170
tok := fmt.Sprintf("%s.%s", chal.Token, acme.JWKThumbprint(&client.Key.PublicKey))
170-
file, err := challengeFile(domain, tok)
171+
file, err := challengeFile(chal.Token, domain, tok)
171172
if err != nil {
172173
return err
173174
}
174-
fmt.Printf("Copy %s to ROOT/.well-known/acme-challenge/%s of %s and press enter.\n",
175-
file, chal.Token, domain)
176-
var x string
177-
fmt.Scanln(&x)
175+
176+
// If ACME_CHALLENGE_DIR is not set, ask user to move challenge file manually
177+
if os.Getenv("ACME_CHALLENGE_DIR") == "" {
178+
fmt.Printf("Copy %s to ROOT/.well-known/acme-challenge/%s of %s and press enter.\n",
179+
file, chal.Token, domain)
180+
var x string
181+
fmt.Scanln(&x)
182+
}
178183
} else {
179184
// auto, via local server
180185
go http.Serve(ln, client.HTTP01Handler(chal.Token))
@@ -215,11 +220,27 @@ func pollCert(url string) [][]byte {
215220
}
216221
}
217222

218-
func challengeFile(domain, content string) (string, error) {
219-
f, err := ioutil.TempFile("", domain)
223+
func challengeFile(challengeFilename, domain, content string) (string, error) {
224+
// If ACME_CHALLENGE_DIR is set, place the challenge into it.
225+
// Otherwise, create a temporary file
226+
var f *os.File
227+
var err error
228+
if os.Getenv("ACME_CHALLENGE_DIR") != "" {
229+
// Create .well-known directory
230+
challengeDir := filepath.Join(os.Getenv("ACME_CHALLENGE_DIR"), ".well-known/acme-challenge")
231+
err = os.MkdirAll(challengeDir, 0755)
232+
if err != nil {
233+
return "", err
234+
}
235+
236+
f, err = os.Create(filepath.Join(challengeDir, challengeFilename))
237+
} else {
238+
f, err = ioutil.TempFile("", domain)
239+
}
220240
if err != nil {
221241
return "", err
222242
}
243+
223244
_, err = fmt.Fprint(f, content)
224245
if err1 := f.Close(); err1 != nil && err == nil {
225246
err = err1

0 commit comments

Comments
 (0)