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

Add support for ACME_CHALLENGE_DIR environment variable #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions cmd/acme/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"log"
"net"
"net/http"
"os"
"path/filepath"
"time"

Expand Down Expand Up @@ -177,14 +178,18 @@ func authz(ctx context.Context, client *acme.Client, domain string) error {
return err
}
tok := fmt.Sprintf("%s.%s", chal.Token, thumb)
file, err := challengeFile(domain, tok)
file, err := challengeFile(chal.Token, domain, tok)
if err != nil {
return err
}
fmt.Printf("Copy %s to ROOT/.well-known/acme-challenge/%s of %s and press enter.\n",
file, chal.Token, domain)
var x string
fmt.Scanln(&x)

// If ACME_CHALLENGE_DIR is not set, ask user to move challenge file manually
if os.Getenv("ACME_CHALLENGE_DIR") == "" {
fmt.Printf("Copy %s to ROOT/.well-known/acme-challenge/%s of %s and press enter.\n",
file, chal.Token, domain)
var x string
fmt.Scanln(&x)
}
} else {
// auto, via local server
val, err := client.HTTP01ChallengeResponse(chal.Token)
Expand All @@ -203,11 +208,27 @@ func authz(ctx context.Context, client *acme.Client, domain string) error {
return err
}

func challengeFile(domain, content string) (string, error) {
f, err := ioutil.TempFile("", domain)
func challengeFile(challengeFilename, domain, content string) (string, error) {
// If ACME_CHALLENGE_DIR is set, place the challenge into it.
// Otherwise, create a temporary file
var f *os.File
var err error
if os.Getenv("ACME_CHALLENGE_DIR") != "" {
// Create .well-known directory
challengeDir := filepath.Join(os.Getenv("ACME_CHALLENGE_DIR"), ".well-known/acme-challenge")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if ACME_CHALLENGE_DIR (or a cmd line flag) already contains .well-known/ or .well-known/acme-challenge? I can see it being even more useful if I didn't have to specify .well-known/acme-challenge every time.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was more convenient to automatically append .well-known/challenge. The protocol requires this exact path and having to specify it manually seems error prone.

Not sure what you mean though: Do you mean it's a good thing to specify the ful path manually, or would you rather keep it automatic?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean to append it automatically unless already present in the path. But what you're saying makes sense, I agree. It's important this is documented, e.g. "challenge dir should point to the root of your website".

err = os.MkdirAll(challengeDir, 0755)
if err != nil {
return "", err
}

f, err = os.Create(filepath.Join(challengeDir, challengeFilename))
} else {
f, err = ioutil.TempFile("", domain)
}
if err != nil {
return "", err
}

_, err = fmt.Fprint(f, content)
if err1 := f.Close(); err1 != nil && err == nil {
err = err1
Expand Down