1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-12 08:23:58 +02:00
go-micro/plugins/acme/certmagic/storage.go
Jerry 4929a7c16e
update etcd version (#2186)
Remove missing gRPC example from README.md (#2112)

Delete docker.yml

Delete Dockerfile

update plugins version & remove replace (#2118)

* update memory registry plugins version & remove replace

* update plugins version & remove replace

Co-authored-by: 申法宽 <shenfakuan@163.com>

update client/grpc plugins version & remove replace (#2119)

* update memory registry plugins version & remove replace

* update plugins version & remove replace

* update plugins/client/grpc/v3 version

Co-authored-by: 申法宽 <shenfakuan@163.com>

update etcd version (#2120)

update mod version

update

update pulgin registry mod version (#2121)

* update etcd version

* update mod version

* update

fix store delete

support for tls on http plugin (#2126)

improve code quality (#2128)

* Fix inefficient string comparison

* Fix unnecessary calls to Printf

* Canonicalize header key

* Replace `t.Sub(time.Now())` with `time.Until`

* Remove unnecessary blank (_) identifier

* Remove unnecessary use of slice

* Remove unnecessary comparison with bool

Update README.md

Update README.md

remove network package

update quic go mod

remove indirects

update etcd mod version

Update registry plugins mod version (#2130)

* update etcd version

* update mod version

* update

* update etcd mod version

Update README.md

Update README.md

Update README.md

fixing etcd stack in getToken (#2145)

when provide username and password, etcd will try to get auth token from server
if server is unavailble, etcd client will stack in
when dial timeout is set, it will return err instead of stack in

Update README.md

add http demo; http client can call http server; http client can call rpc server (#2149)

Add etcd to default registries when plugin is loaded (#2150)

Co-authored-by: Andrew Jones <andrew@gotoblink.com>

Update README.md

make rpcClient compatible with 32bit arm systems (#2156)

On ARM, 386, and 32-bit MIPS, it is the caller's responsibility to
arrange for 64-bit alignment of 64-bit words accessed
atomically. Only the first word in an allocated struct can
be relied upon to be 64-bit aligned.

optimize the process of switching grpc error to micro error (#2158)

Fix util/log/log.Infof format didn't work (#2160)

Co-authored-by: Cui Gang <cuigang@yunpbx.com>

fixing string field contains invalid UTF-8 issue (#2164)

fix k8s api memory leak (#2166)

fix http No release Broker (#2167)

* Update http.go

Exit before deregister is executed

* Create http.go

Exit before deregister is executed

fix: "Solve the problem that the resources have not been fully released due to early exit" (#2168)

* Update http.go

Exit before deregister is executed

* Create http.go

Exit before deregister is executed

* Solve the problem that the resources have not been fully released due to early exit

* Optimize some code

* Optimize some code

fix service default logger (#2171)

* Update http.go

Exit before deregister is executed

* Create http.go

Exit before deregister is executed

* Solve the problem that the resources have not been fully released due to early exit

* Optimize some code

* Optimize some code

* Optimize some code

* fix service default logger

Update README.md

get k8s pod (#2173)

Update README.md

fix:field (#2176)

* get k8s pod

* fix: filed

* field

Update README.md

add rmq message properties (#2177)

Co-authored-by: dtitov <dtitov@might24.ru>

Update README.md

grpc server add RegisterCheck (#2178)

fix 404 bug (#2179)

fix undefined: err (#2181)

Add registry and config/source plugins based on nacos/v2 (#2182)

* Add registry plugins implement by nacos/v2

* Add config/source plugins implement by nacos/v2

support hystrix fallback (#2183)

Windows event log plugin (#2180)

* add rmq message properties

* eventlog start

* start eventlog

* windows event logger

* readme

* readme

Co-authored-by: dtitov <dtitov@might24.ru>

support etcd auth with env args (#2184)

* support etcd auth with env args
set default registry address with env arg instead of 127.0.0.1

* fixing MICRO_REGISTRY_ADDRESS may empty issue

update mod version
2021-06-29 13:40:54 +01:00

149 lines
3.2 KiB
Go

package certmagic
import (
"bytes"
"context"
"encoding/gob"
"errors"
"fmt"
"path"
"strings"
"time"
"github.com/asim/go-micro/v3/store"
"github.com/asim/go-micro/v3/sync"
"github.com/caddyserver/certmagic"
)
// File represents a "File" that will be stored in store.Store - the contents and last modified time
type File struct {
// last modified time
LastModified time.Time
// Contents
Contents []byte
}
// 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 {
lock sync.Sync
store store.Store
}
func (s *storage) Lock(ctx context.Context, key string) error {
return s.lock.Lock(key, sync.LockTTL(10*time.Minute))
}
func (s *storage) Unlock(key string) error {
return s.lock.Unlock(key)
}
func (s *storage) Store(key string, value []byte) error {
f := File{
LastModified: time.Now(),
Contents: value,
}
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) {
if !s.Exists(key) {
return nil, certmagic.ErrNotExist(errors.New(key + " doesn't exist"))
}
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)
var f File
err = d.Decode(&f)
if err != nil {
return nil, err
}
return f.Contents, nil
}
func (s *storage) Delete(key string) error {
return s.store.Delete(key)
}
func (s *storage) Exists(key string) bool {
if _, err := s.store.Read(key); err != nil {
return false
}
return true
}
func (s *storage) List(prefix string, recursive bool) ([]string, error) {
keys, err := s.store.List()
if err != nil {
return nil, err
}
//nolint:prealloc
var results []string
for _, k := range keys {
if strings.HasPrefix(k, prefix) {
results = append(results, k)
}
}
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)
var f File
err = d.Decode(&f)
if err != nil {
return certmagic.KeyInfo{}, err
}
return certmagic.KeyInfo{
Key: key,
Modified: f.LastModified,
Size: int64(len(f.Contents)),
IsTerminal: false,
}, nil
}
// NewStorage returns a certmagic.Storage backed by a go-micro/lock and go-micro/store
func NewStorage(lock sync.Sync, store store.Store) certmagic.Storage {
return &storage{
lock: lock,
store: store,
}
}