2017-02-11 06:53:49 +00:00
|
|
|
// Package memcached implements a HTTP provider for solving the HTTP-01 challenge using memcached
|
|
|
|
// in combination with a webserver.
|
2019-03-11 16:56:48 +00:00
|
|
|
package memcached
|
2016-10-24 09:03:18 +00:00
|
|
|
|
|
|
|
import (
|
2020-02-27 18:14:46 +00:00
|
|
|
"errors"
|
2016-10-24 09:03:18 +00:00
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
|
2019-07-30 19:19:32 +00:00
|
|
|
"github.com/go-acme/lego/v3/challenge/http01"
|
2016-10-24 09:03:18 +00:00
|
|
|
"github.com/rainycape/memcache"
|
|
|
|
)
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// HTTPProvider implements HTTPProvider for `http-01` challenge.
|
2018-05-30 17:53:04 +00:00
|
|
|
type HTTPProvider struct {
|
2016-10-24 09:03:18 +00:00
|
|
|
hosts []string
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewMemcachedProvider returns a HTTPProvider instance with a configured webroot path.
|
2018-05-30 17:53:04 +00:00
|
|
|
func NewMemcachedProvider(hosts []string) (*HTTPProvider, error) {
|
2016-10-24 09:03:18 +00:00
|
|
|
if len(hosts) == 0 {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("no memcached hosts provided")
|
2016-10-24 09:03:18 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
c := &HTTPProvider{
|
2016-10-24 09:03:18 +00:00
|
|
|
hosts: hosts,
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Present makes the token available at `HTTP01ChallengePath(token)` by creating a file in the given webroot path.
|
2018-05-30 17:53:04 +00:00
|
|
|
func (w *HTTPProvider) Present(domain, token, keyAuth string) error {
|
2016-10-24 09:03:18 +00:00
|
|
|
var errs []error
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
challengePath := path.Join("/", http01.ChallengePath(token))
|
2016-10-24 09:03:18 +00:00
|
|
|
for _, host := range w.hosts {
|
|
|
|
mc, err := memcache.New(host)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
continue
|
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
_ = mc.Add(&memcache.Item{
|
2016-10-24 09:03:18 +00:00
|
|
|
Key: challengePath,
|
|
|
|
Value: []byte(keyAuth),
|
|
|
|
Expiration: 60,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) == len(w.hosts) {
|
2020-03-20 21:53:09 +00:00
|
|
|
return fmt.Errorf("unable to store key in any of the memcache hosts: %v", errs)
|
2016-10-24 09:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// CleanUp removes the file created for the challenge.
|
2018-05-30 17:53:04 +00:00
|
|
|
func (w *HTTPProvider) CleanUp(domain, token, keyAuth string) error {
|
2016-10-24 09:03:18 +00:00
|
|
|
// Memcached will clean up itself, that's what expiration is for.
|
|
|
|
return nil
|
|
|
|
}
|