-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate_repository.go
232 lines (188 loc) · 5.8 KB
/
certificate_repository.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright (c) 2023 Zander Schwid & Co. LLC.
* SPDX-License-Identifier: BUSL-1.1
*/
package certmod
import (
"context"
"fmt"
"github.com/keyvalstore/store"
"github.com/sprintframework/cert"
"github.com/sprintframework/certpb"
"go.uber.org/atomic"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
"sync"
)
var (
CertBucket = "cert"
CertBucketLen = len(CertBucket)
)
type implCertificateRepository struct {
sync.Mutex
Log *zap.Logger `inject`
Store store.DataStore `inject:"bean=config-store"`
watchNum atomic.Int64
watchMap sync.Map // watchNum, configWatchContext
shuttingDown atomic.Bool
}
type zoneChangeEvent struct {
zone string
event string // UPDATE, DELETE
}
type zoneWatchContext struct {
ctx context.Context
cancelFn context.CancelFunc
ch chan<- zoneChangeEvent
}
func CertificateRepository() cert.CertificateRepository {
return &implCertificateRepository{}
}
func (t *implCertificateRepository) Destroy() error {
t.shuttingDown.Store(true)
if t.watchNum.Load() > 0 {
t.watchMap.Range(func(key, value interface{}) bool {
if wc, ok := value.(*zoneWatchContext); ok {
wc.cancelFn()
}
return true
})
}
return nil
}
func (t *implCertificateRepository) SaveSelfSigner(self *certpb.SelfSigner) error {
return t.Store.Set(context.Background()).ByKey("%s:self:%s", CertBucket, self.Name).Proto(self)
}
func (t *implCertificateRepository) FindSelfSigner(name string) (entry *certpb.SelfSigner, err error) {
entry = new(certpb.SelfSigner)
err = t.Store.Get(context.Background()).ByKey("%s:self:%s", CertBucket, name).ToProto(entry)
return
}
func (t *implCertificateRepository) ListSelfSigners(prefix string, cb func(*certpb.SelfSigner) bool) error {
return t.Store.Enumerate(context.Background()).ByPrefix("%s:self:%s", CertBucket, prefix).WithBatchSize(100).DoProto(func() proto.Message {
return new(certpb.SelfSigner)
}, func(entry *store.ProtoEntry) bool {
if v, ok := entry.Value.(*certpb.SelfSigner); ok {
return cb(v)
}
return true
})
}
func (t *implCertificateRepository) DeleteSelfSigner(name string) error {
return t.Store.Remove(context.Background()).ByKey("%s:self:%s", CertBucket, name).Do()
}
func (t *implCertificateRepository) SaveAccount(account *certpb.AcmeAccount) error {
return t.Store.Set(context.Background()).ByKey("%s:acme:%s", CertBucket, account.Email).Proto(account)
}
func (t *implCertificateRepository) FindAccount(email string) (entry *certpb.AcmeAccount, err error) {
entry = new(certpb.AcmeAccount)
err = t.Store.Get(context.Background()).ByKey("%s:acme:%s", CertBucket, email).ToProto(entry)
return
}
func (t *implCertificateRepository) ListAccounts(prefix string, cb func(*certpb.AcmeAccount) bool) error {
return t.Store.Enumerate(context.Background()).ByPrefix("%s:acme:%s", CertBucket, prefix).WithBatchSize(100).DoProto(func() proto.Message {
return new(certpb.AcmeAccount)
}, func(entry *store.ProtoEntry) bool {
if v, ok := entry.Value.(*certpb.AcmeAccount); ok {
return cb(v)
}
return true
})
}
func (t *implCertificateRepository) DeleteAccount(email string) error {
return t.Store.Remove(context.Background()).ByKey("%s:acme:%s", CertBucket, email).Do()
}
func (t *implCertificateRepository) SaveZone(zone *certpb.Zone) error {
err := t.Store.Set(context.Background()).ByKey("%s:zone:%s", CertBucket, zone.Zone).Proto(zone)
if err == nil {
t.notifyAll(zone.Zone, "UPDATE")
}
return err
}
func (t *implCertificateRepository) FindZone(zone string) (entry *certpb.Zone, err error) {
entry = new(certpb.Zone)
err = t.Store.Get(context.Background()).ByKey("%s:zone:%s", CertBucket, zone).ToProto(entry)
return
}
func (t *implCertificateRepository) ListZones(prefix string, cb func(*certpb.Zone) bool) error {
return t.Store.Enumerate(context.Background()).ByPrefix("%s:zone:%s", CertBucket, prefix).WithBatchSize(100).DoProto(func() proto.Message {
return new(certpb.Zone)
}, func(entry *store.ProtoEntry) bool {
if v, ok := entry.Value.(*certpb.Zone); ok {
return cb(v)
}
return true
})
}
func (t *implCertificateRepository) DeleteZone(zone string) error {
err := t.Store.Remove(context.Background()).ByKey("%s:zone:%s", CertBucket, zone).Do()
if err == nil {
t.notifyAll(zone, "DELETE")
}
return err
}
func (t *implCertificateRepository) notifyAll(zone, event string) {
t.watchMap.Range(func(key, value interface{}) bool {
if wc, ok := value.(*zoneWatchContext); ok {
wc.ch <- zoneChangeEvent{zone, event}
}
return true
})
}
func (t *implCertificateRepository) registerWatch(wc *zoneWatchContext) int64 {
handle := t.watchNum.Inc()
t.watchMap.Store(handle, wc)
return handle
}
func (t *implCertificateRepository) unregisterWatch(handle int64) {
t.watchMap.Delete(handle)
}
func (t *implCertificateRepository) Watch(ctx context.Context, cb func(zone, event string) bool) (cancel context.CancelFunc, err error) {
ctx, cancel = context.WithCancel(ctx)
ch := make(chan zoneChangeEvent)
wc := &zoneWatchContext{
ctx: ctx,
cancelFn: cancel,
ch: ch,
}
handle := t.registerWatch(wc)
go func() {
defer func() {
if r := recover(); r != nil {
switch v := r.(type) {
case error:
t.Log.Error("RecoverZoneWatcher", zap.Error(v))
case string:
t.Log.Error("RecoverZoneWatcher", zap.String("err", v))
default:
t.Log.Error("RecoverZoneWatcher", zap.String("err", fmt.Sprintf("%v", v)))
}
}
}()
defer func() {
t.unregisterWatch(handle)
close(ch)
}()
for {
select {
case <- ctx.Done():
return
case e := <- ch:
if !cb(e.zone, e.event) {
return
}
}
}
}()
return
}
func (t *implCertificateRepository) Backend() store.DataStore {
t.Lock()
defer t.Unlock()
return t.Store
}
func (t *implCertificateRepository) SetBackend(storage store.DataStore) {
t.Lock()
defer t.Unlock()
t.Store = storage
}