forked from TrueCloudLab/lego
Relocating provider to lego/providers/http/webroot
This commit is contained in:
parent
eb773f17d2
commit
7ebad0e886
3 changed files with 52 additions and 49 deletions
|
@ -4,7 +4,6 @@ import (
|
|||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -56,48 +55,3 @@ func TestHTTPChallengeInvalidPort(t *testing.T) {
|
|||
t.Errorf("Solve error: got %q, want suffix %q", err.Error(), want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPChallengeWebRoot(t *testing.T) {
|
||||
privKey, _ := generatePrivateKey(rsakey, 512)
|
||||
j := &jws{privKey: privKey.(*rsa.PrivateKey)}
|
||||
clientChallenge := challenge{Type: HTTP01, Token: "http1"}
|
||||
mockValidate := func(_ *jws, _, _ string, chlng challenge) error {
|
||||
challengeFilePath := "webroot/.well-known/acme-challenge/" + chlng.Token
|
||||
|
||||
if _, err := os.Stat(challengeFilePath); os.IsNotExist(err) {
|
||||
t.Error("Challenge file was not created in webroot")
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadFile(challengeFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dataStr := string(data)
|
||||
|
||||
if dataStr != chlng.KeyAuthorization {
|
||||
t.Errorf("Challenge file content: got %q, want %q", dataStr, chlng.KeyAuthorization)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
solver := &httpChallenge{jws: j, validate: mockValidate, provider: &HTTPProviderWebroot{path: "webroot"}}
|
||||
|
||||
os.MkdirAll("webroot/.well-known/acme-challenge", 0777)
|
||||
if err := solver.Solve(clientChallenge, "localhost:23457"); err != nil {
|
||||
t.Errorf("Solve error: got %v, want nil", err)
|
||||
}
|
||||
defer os.RemoveAll("webroot")
|
||||
}
|
||||
|
||||
func TestHTTPChallengeWebRootInvalidPath(t *testing.T) {
|
||||
privKey, _ := generatePrivateKey(rsakey, 128)
|
||||
j := &jws{privKey: privKey.(*rsa.PrivateKey)}
|
||||
clientChallenge := challenge{Type: HTTP01, Token: "http2"}
|
||||
solver := &httpChallenge{jws: j, validate: stubValidate, provider: &HTTPProviderWebroot{path: "/invalid-\000-path"}}
|
||||
|
||||
if err := solver.Solve(clientChallenge, "localhost:123456"); err == nil {
|
||||
t.Errorf("Solve error: got %v, want error", err)
|
||||
} else if want := "Could not create required directories in webroot"; !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("Solve error: got %q, want content %q", err.Error(), want)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
package acme
|
||||
// Package webroot implements a HTTP provider for solving the HTTP-01 challenge using web server's root path.
|
||||
package webroot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/xenolf/lego/acme"
|
||||
)
|
||||
|
||||
// HTTPProviderWebroot implements ChallengeProvider for `http-01` challenge
|
||||
|
@ -29,7 +32,7 @@ func NewHTTPProviderWebroot(path string) (*HTTPProviderWebroot, error) {
|
|||
func (w *HTTPProviderWebroot) Present(domain, token, keyAuth string) error {
|
||||
var err error
|
||||
|
||||
challengeFilePath := path.Join(w.path, HTTP01ChallengePath(token))
|
||||
challengeFilePath := path.Join(w.path, acme.HTTP01ChallengePath(token))
|
||||
err = os.MkdirAll(path.Dir(challengeFilePath), 0777)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Could not create required directories in webroot for HTTP challenge -> %v", err)
|
||||
|
@ -46,7 +49,7 @@ func (w *HTTPProviderWebroot) Present(domain, token, keyAuth string) error {
|
|||
// CleanUp removes the file created for the challenge
|
||||
func (w *HTTPProviderWebroot) CleanUp(domain, token, keyAuth string) error {
|
||||
var err error
|
||||
err = os.Remove(path.Join(w.path, HTTP01ChallengePath(token)))
|
||||
err = os.Remove(path.Join(w.path, acme.HTTP01ChallengePath(token)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Could not remove file in webroot after HTTP challenge -> %v", err)
|
||||
}
|
46
providers/http/webroot/webroot_test.go
Normal file
46
providers/http/webroot/webroot_test.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package webroot
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHTTPProviderWebRoot(t *testing.T) {
|
||||
webroot := "webroot"
|
||||
domain := "domain"
|
||||
token := "token"
|
||||
keyAuth := "keyAuth"
|
||||
challengeFilePath := webroot + "/.well-known/acme-challenge/" + token
|
||||
|
||||
os.MkdirAll(webroot+"/.well-known/acme-challenge", 0777)
|
||||
defer os.RemoveAll(webroot)
|
||||
|
||||
provider, err := NewHTTPProviderWebroot(webroot)
|
||||
if err != nil {
|
||||
t.Errorf("Webroot provider error: got %v, want nil", err)
|
||||
}
|
||||
|
||||
err = provider.Present(domain, token, keyAuth)
|
||||
if err != nil {
|
||||
t.Errorf("Webroot provider present() error: got %v, want nil", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(challengeFilePath); os.IsNotExist(err) {
|
||||
t.Error("Challenge file was not created in webroot")
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadFile(challengeFilePath)
|
||||
if err != nil {
|
||||
t.Errorf("Webroot provider ReadFile() error: got %v, want nil", err)
|
||||
}
|
||||
dataStr := string(data)
|
||||
if dataStr != keyAuth {
|
||||
t.Errorf("Challenge file content: got %q, want %q", dataStr, keyAuth)
|
||||
}
|
||||
|
||||
err = provider.CleanUp(domain, token, keyAuth)
|
||||
if err != nil {
|
||||
t.Errorf("Webroot provider CleanUp() error: got %v, want nil", err)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue