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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package vaultmemory
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"sync"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/component"
contractvault "github.com/johnandersen777/socialweb-computer-kcp/internal/contract/vault"
"github.com/johnandersen777/socialweb-computer-kcp/internal/marketidentity/atproto"
"github.com/johnandersen777/socialweb-computer-kcp/internal/vault/openbao"
)
var ErrNotFound = fmt.Errorf("memory vault: secret not found: %w", openbao.ErrNotFound)
var ErrInvalidRef = errors.New("memory vault: a KV call needs a mount and a path")
type kvKey struct {
mount string
path string
}
type entry struct {
data map[string]any
version int
}
type Store struct {
mu sync.RWMutex
entries map[kvKey]entry
onChange func()
}
func (s *Store) SetOnChange(fn func()) {
s.mu.Lock()
s.onChange = fn
s.mu.Unlock()
}
var _ contractvault.KV = (*Store)(nil)
func NewStore() *Store { return &Store{entries: map[kvKey]entry{}} }
func (s *Store) Read(ctx context.Context, mount, path string, version int) (map[string]any, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
k, err := kvKeyOf(mount, path)
if err != nil {
return nil, err
}
s.mu.RLock()
e, ok := s.entries[k]
s.mu.RUnlock()
if !ok || (version > 0 && version != e.version) {
return nil, fmt.Errorf("memory vault: %s/%s: %w", k.mount, k.path, ErrNotFound)
}
return cloneData(e.data), nil
}
func (s *Store) Write(ctx context.Context, mount, path string, data map[string]any) error {
if err := ctx.Err(); err != nil {
return err
}
k, err := kvKeyOf(mount, path)
if err != nil {
return err
}
s.mu.Lock()
if s.entries == nil {
s.entries = map[kvKey]entry{}
}
s.entries[k] = entry{data: cloneData(data), version: s.entries[k].version + 1}
fn := s.onChange
s.mu.Unlock()
if fn != nil {
fn()
}
return nil
}
func (s *Store) Delete(ctx context.Context, mount, path string) error {
if err := ctx.Err(); err != nil {
return err
}
k, err := kvKeyOf(mount, path)
if err != nil {
return err
}
s.mu.Lock()
delete(s.entries, k)
fn := s.onChange
s.mu.Unlock()
if fn != nil {
fn()
}
return nil
}
func (s *Store) List(ctx context.Context, mount, path string) ([]string, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
mount = strings.Trim(strings.TrimSpace(mount), "/")
path = strings.Trim(strings.TrimSpace(path), "/")
if mount == "" {
return nil, ErrInvalidRef
}
prefix := path
if prefix != "" {
prefix += "/"
}
seen := map[string]struct{}{}
s.mu.RLock()
for k := range s.entries {
if k.mount != mount || !strings.HasPrefix(k.path, prefix) {
continue
}
rest := strings.TrimPrefix(k.path, prefix)
if rest == "" {
continue
}
if i := strings.IndexByte(rest, '/'); i >= 0 {
rest = rest[:i+1]
}
seen[rest] = struct{}{}
}
s.mu.RUnlock()
out := make([]string, 0, len(seen))
for name := range seen {
out = append(out, name)
}
sort.Strings(out)
return out, nil
}
type StoreEntry struct {
Mount string `json:"mount"`
Path string `json:"path"`
Version int `json:"version"`
Data map[string]any `json:"data"`
}
type StoreSnapshot struct {
Entries []StoreEntry `json:"entries"`
}
func (s *Store) Snapshot() StoreSnapshot {
s.mu.RLock()
defer s.mu.RUnlock()
out := StoreSnapshot{Entries: make([]StoreEntry, 0, len(s.entries))}
for k, e := range s.entries {
out.Entries = append(out.Entries, StoreEntry{
Mount: k.mount, Path: k.path, Version: e.version, Data: cloneData(e.data),
})
}
sort.Slice(out.Entries, func(i, j int) bool {
if out.Entries[i].Mount != out.Entries[j].Mount {
return out.Entries[i].Mount < out.Entries[j].Mount
}
return out.Entries[i].Path < out.Entries[j].Path
})
return out
}
func (s *Store) Restore(snapshot StoreSnapshot) {
entries := make(map[kvKey]entry, len(snapshot.Entries))
for _, e := range snapshot.Entries {
k, err := kvKeyOf(e.Mount, e.Path)
if err != nil {
continue
}
version := e.Version
if version < 1 {
version = 1
}
entries[k] = entry{data: cloneData(e.Data), version: version}
}
s.mu.Lock()
s.entries = entries
s.mu.Unlock()
}
type Secrets struct {
store *Store
}
var _ atproto.SecretStore = (*Secrets)(nil)
func NewSecrets(store *Store) *Secrets {
if store == nil {
store = NewStore()
}
return &Secrets{store: store}
}
func (s *Secrets) Get(ctx context.Context, ref commonv1.SecretRef) (map[string]any, error) {
data, err := s.store.Read(ctx, ref.Mount, ref.Path, 0)
if errors.Is(err, ErrNotFound) {
return nil, atproto.ErrSecretNotFound
}
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, atproto.ErrSecretNotFound
}
return data, nil
}
func (s *Secrets) Put(ctx context.Context, ref commonv1.SecretRef, data map[string]any) error {
return s.store.Write(ctx, ref.Mount, ref.Path, data)
}
func (s *Secrets) Delete(ctx context.Context, ref commonv1.SecretRef) error {
return s.store.Delete(ctx, ref.Mount, ref.Path)
}
type Credentials struct {
store *Store
}
var (
_ component.CredentialResolver = (*Credentials)(nil)
_ component.CredentialWriter = (*Credentials)(nil)
_ component.CredentialDeleter = (*Credentials)(nil)
)
func NewCredentials(store *Store) *Credentials {
if store == nil {
store = NewStore()
}
return &Credentials{store: store}
}
func (c *Credentials) Resolve(ctx context.Context, ref component.CredentialRef) (component.Credential, error) {
if err := ref.Check(); err != nil {
return component.Credential{}, err
}
data, err := c.store.Read(ctx, ref.Mount, ref.Path, ref.Version)
if err != nil {
return component.Credential{}, fmt.Errorf("memory vault: reading %s/%s: %w", ref.Mount, ref.Path, err)
}
if len(data) == 0 {
return component.Credential{}, fmt.Errorf("memory vault: %s/%s: %w", ref.Mount, ref.Path, ErrNotFound)
}
return component.Credential{Ref: ref, Data: filterKeys(data, ref.Keys)}, nil
}
func (c *Credentials) WriteCredential(ctx context.Context, ref component.CredentialRef, data map[string]any) error {
if err := ref.Check(); err != nil {
return err
}
if data == nil {
return errors.New("memory vault: a credential write needs data")
}
return c.store.Write(ctx, ref.Mount, ref.Path, data)
}
func (c *Credentials) DeleteCredential(ctx context.Context, ref component.CredentialRef) error {
if err := ref.Check(); err != nil {
return err
}
return c.store.Delete(ctx, ref.Mount, ref.Path)
}
func kvKeyOf(mount, path string) (kvKey, error) {
mount = strings.Trim(strings.TrimSpace(mount), "/")
path = strings.Trim(strings.TrimSpace(path), "/")
path = strings.TrimPrefix(strings.TrimPrefix(path, mount+"/data/"), mount+"/metadata/")
if mount == "" || path == "" {
return kvKey{}, ErrInvalidRef
}
return kvKey{mount: mount, path: path}, nil
}
func filterKeys(data map[string]any, keys []string) map[string]any {
if len(keys) == 0 {
return data
}
out := make(map[string]any, len(keys))
for _, key := range keys {
if value, ok := data[key]; ok {
out[key] = value
}
}
return out
}
func cloneData(in map[string]any) map[string]any {
out := make(map[string]any, len(in))
for k, v := range in {
out[k] = cloneValue(v)
}
return out
}
func cloneValue(v any) any {
switch x := v.(type) {
case []byte:
return append([]byte(nil), x...)
case map[string]any:
return cloneData(x)
case []any:
out := make([]any, len(x))
for i, item := range x {
out[i] = cloneValue(item)
}
return out
default:
return v
}
}