1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-23 09:15:11 +02:00
lego/providers/http/memcached/memcached.go

61 lines
1.5 KiB
Go
Raw Normal View History

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