File: memcached.go

package info (click to toggle)
golang-github-xenolf-lego 4.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,080 kB
  • sloc: xml: 533; makefile: 128; sh: 18
file content (61 lines) | stat: -rw-r--r-- 1,528 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Package memcached implements a HTTP provider for solving the HTTP-01 challenge using memcached
// in combination with a webserver.
package memcached

import (
	"errors"
	"fmt"
	"path"

	"github.com/go-acme/lego/v4/challenge/http01"
	"github.com/rainycape/memcache"
)

// HTTPProvider implements HTTPProvider for `http-01` challenge.
type HTTPProvider struct {
	hosts []string
}

// NewMemcachedProvider returns a HTTPProvider instance with a configured webroot path.
func NewMemcachedProvider(hosts []string) (*HTTPProvider, error) {
	if len(hosts) == 0 {
		return nil, errors.New("no memcached hosts provided")
	}

	c := &HTTPProvider{
		hosts: hosts,
	}

	return c, nil
}

// Present makes the token available at `HTTP01ChallengePath(token)` by creating a file in the given webroot path.
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
		}
		_ = mc.Add(&memcache.Item{
			Key:        challengePath,
			Value:      []byte(keyAuth),
			Expiration: 60,
		})
	}

	if len(errs) == len(w.hosts) {
		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.
func (w *HTTPProvider) CleanUp(domain, token, keyAuth string) error {
	// Memcached will clean up itself, that's what expiration is for.
	return nil
}