2019-10-15 21:32:20 +03:00
|
|
|
package certmagic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2019-10-17 18:31:02 +03:00
|
|
|
"errors"
|
2019-10-15 21:32:20 +03:00
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-05-13 17:00:13 +03:00
|
|
|
"github.com/caddyserver/certmagic"
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/store"
|
|
|
|
"github.com/unistack-org/micro/v3/sync"
|
2019-10-15 21:32:20 +03:00
|
|
|
)
|
|
|
|
|
2019-10-16 14:58:14 +03:00
|
|
|
// File represents a "File" that will be stored in store.Store - the contents and last modified time
|
|
|
|
type File struct {
|
2019-10-15 21:32:20 +03:00
|
|
|
// last modified time
|
2019-10-16 14:58:14 +03:00
|
|
|
LastModified time.Time
|
|
|
|
// Contents
|
|
|
|
Contents []byte
|
2019-10-15 21:32:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// storage is an implementation of certmagic.Storage using micro's sync.Map and store.Store interfaces.
|
|
|
|
// As certmagic storage expects a filesystem (with stat() abilities) we have to implement
|
|
|
|
// the bare minimum of metadata.
|
|
|
|
type storage struct {
|
2020-04-11 13:02:06 +03:00
|
|
|
lock sync.Sync
|
2019-10-15 21:32:20 +03:00
|
|
|
store store.Store
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storage) Lock(key string) error {
|
2020-04-11 13:02:06 +03:00
|
|
|
return s.lock.Lock(key, sync.LockTTL(10*time.Minute))
|
2019-10-15 21:32:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storage) Unlock(key string) error {
|
2020-04-11 13:02:06 +03:00
|
|
|
return s.lock.Unlock(key)
|
2019-10-15 21:32:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storage) Store(key string, value []byte) error {
|
2019-10-16 14:58:14 +03:00
|
|
|
f := File{
|
|
|
|
LastModified: time.Now(),
|
|
|
|
Contents: value,
|
2019-10-15 21:32:20 +03:00
|
|
|
}
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
e := gob.NewEncoder(buf)
|
|
|
|
if err := e.Encode(f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r := &store.Record{
|
|
|
|
Key: key,
|
|
|
|
Value: buf.Bytes(),
|
|
|
|
}
|
|
|
|
return s.store.Write(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storage) Load(key string) ([]byte, error) {
|
2019-10-17 18:31:02 +03:00
|
|
|
if !s.Exists(key) {
|
|
|
|
return nil, certmagic.ErrNotExist(errors.New(key + " doesn't exist"))
|
|
|
|
}
|
2019-10-15 21:32:20 +03:00
|
|
|
records, err := s.store.Read(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(records) != 1 {
|
|
|
|
return nil, fmt.Errorf("ACME Storage: multiple records matched key %s", key)
|
|
|
|
}
|
|
|
|
b := bytes.NewBuffer(records[0].Value)
|
|
|
|
d := gob.NewDecoder(b)
|
2019-10-16 14:58:14 +03:00
|
|
|
var f File
|
2019-10-15 21:32:20 +03:00
|
|
|
err = d.Decode(&f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-16 14:58:14 +03:00
|
|
|
return f.Contents, nil
|
2019-10-15 21:32:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storage) Delete(key string) error {
|
|
|
|
return s.store.Delete(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storage) Exists(key string) bool {
|
2019-12-03 22:59:44 +03:00
|
|
|
if _, err := s.store.Read(key); err != nil {
|
2019-10-15 21:32:20 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storage) List(prefix string, recursive bool) ([]string, error) {
|
2020-03-12 16:41:30 +03:00
|
|
|
keys, err := s.store.List()
|
2019-10-15 21:32:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-03 22:59:44 +03:00
|
|
|
|
|
|
|
//nolint:prealloc
|
2019-10-15 21:32:20 +03:00
|
|
|
var results []string
|
2020-03-12 16:41:30 +03:00
|
|
|
for _, k := range keys {
|
|
|
|
if strings.HasPrefix(k, prefix) {
|
|
|
|
results = append(results, k)
|
2019-10-15 21:32:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if recursive {
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
keysMap := make(map[string]bool)
|
|
|
|
for _, key := range results {
|
|
|
|
dir := strings.Split(strings.TrimPrefix(key, prefix+"/"), "/")
|
|
|
|
keysMap[dir[0]] = true
|
|
|
|
}
|
|
|
|
results = make([]string, 0)
|
|
|
|
for k := range keysMap {
|
|
|
|
results = append(results, path.Join(prefix, k))
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storage) Stat(key string) (certmagic.KeyInfo, error) {
|
|
|
|
records, err := s.store.Read(key)
|
|
|
|
if err != nil {
|
|
|
|
return certmagic.KeyInfo{}, err
|
|
|
|
}
|
|
|
|
if len(records) != 1 {
|
|
|
|
return certmagic.KeyInfo{}, fmt.Errorf("ACME Storage: multiple records matched key %s", key)
|
|
|
|
}
|
|
|
|
b := bytes.NewBuffer(records[0].Value)
|
|
|
|
d := gob.NewDecoder(b)
|
2019-10-16 14:58:14 +03:00
|
|
|
var f File
|
2019-10-15 21:32:20 +03:00
|
|
|
err = d.Decode(&f)
|
|
|
|
if err != nil {
|
|
|
|
return certmagic.KeyInfo{}, err
|
|
|
|
}
|
|
|
|
return certmagic.KeyInfo{
|
|
|
|
Key: key,
|
2019-10-16 14:58:14 +03:00
|
|
|
Modified: f.LastModified,
|
|
|
|
Size: int64(len(f.Contents)),
|
2019-10-15 21:32:20 +03:00
|
|
|
IsTerminal: false,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-10-17 18:31:02 +03:00
|
|
|
|
|
|
|
// NewStorage returns a certmagic.Storage backed by a go-micro/lock and go-micro/store
|
2020-04-11 13:02:06 +03:00
|
|
|
func NewStorage(lock sync.Sync, store store.Store) certmagic.Storage {
|
2019-10-17 18:31:02 +03:00
|
|
|
return &storage{
|
|
|
|
lock: lock,
|
|
|
|
store: store,
|
|
|
|
}
|
|
|
|
}
|