-
Notifications
You must be signed in to change notification settings - Fork 6
/
object_store.go
228 lines (179 loc) · 4.74 KB
/
object_store.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
package parameter
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"go.ketch.com/lib/orlop/v2/errors"
"reflect"
"strings"
)
// ObjectStore is an extension to read/write objects to parameter store
type ObjectStore interface {
Store
ReadObject(ctx context.Context, p string, out any) error
WriteObject(ctx context.Context, p string, in any) error
}
type objectStore struct {
store Store
}
func NewObjectStore(store Store) ObjectStore {
return &objectStore{
store: store,
}
}
func (c *objectStore) List(ctx context.Context, p string) ([]string, error) {
return c.store.List(ctx, p)
}
func (c *objectStore) Read(ctx context.Context, p string) (map[string]any, error) {
return c.store.Read(ctx, p)
}
func (c *objectStore) Write(ctx context.Context, p string, data map[string]any) (map[string]any, error) {
return c.store.Write(ctx, p, data)
}
func (c *objectStore) Delete(ctx context.Context, p string) error {
return c.store.Delete(ctx, p)
}
// ReadObject returns a secret at the given path
func (c *objectStore) ReadObject(ctx context.Context, p string, out any) error {
v := reflect.ValueOf(out)
if v.Kind() != reflect.Ptr {
panic("out must be a pointer")
}
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
sec, err := c.store.Read(ctx, p)
if err != nil {
return errors.NotFound(err)
}
if sec == nil {
return errors.NotFound(nil)
}
t := v.Type()
for n := 0; n < t.NumField(); n++ {
ft := t.Field(n)
name := ft.Name
parts := strings.Split(ft.Tag.Get("json"), ",")
if len(parts) > 0 && len(parts[0]) > 0 {
name = parts[0]
}
if val, ok := sec[name]; ok {
f := v.Field(n)
switch f.Kind() {
case reflect.String:
if s, ok := val.(string); ok {
f.SetString(s)
} else {
f.SetString(fmt.Sprintf("%v", val))
}
case reflect.Bool:
if b, ok := val.(bool); ok {
f.SetBool(b)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if j, ok := val.(json.Number); ok {
if i, err := j.Int64(); err == nil {
f.SetInt(i)
}
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if j, ok := val.(json.Number); ok {
if i, err := j.Int64(); err == nil {
f.SetUint(uint64(i))
}
}
case reflect.Float32, reflect.Float64:
if j, ok := val.(json.Number); ok {
if i, err := j.Float64(); err == nil {
f.SetFloat(i)
}
}
case reflect.Slice:
if j, ok := val.(string); ok {
b, err := base64.StdEncoding.DecodeString(j)
if err != nil {
return errors.Invalid(err)
}
f.SetBytes(b)
} else if j, ok := val.([]any); ok {
sl := reflect.MakeSlice(f.Type(), 0, len(j))
for _, sv := range j {
sl = reflect.Append(sl, reflect.ValueOf(sv))
}
f.Set(sl)
}
default:
panic(fmt.Sprintf("%s not supported", f.Type()))
}
}
}
return nil
}
// WriteObject writes secret data at the given path from an object
func (c *objectStore) WriteObject(ctx context.Context, p string, in any) error {
m := make(map[string]any)
v := reflect.ValueOf(in)
if v.Kind() != reflect.Ptr && v.Kind() != reflect.Struct {
panic("in must be a pointer or struct")
}
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
panic("in must be a pointer or struct")
}
t := v.Type()
for n := 0; n < t.NumField(); n++ {
ft := t.Field(n)
name := ft.Name
parts := strings.Split(ft.Tag.Get("json"), ",")
if len(parts) > 0 && len(parts[0]) > 0 {
name = parts[0]
}
f := v.Field(n)
switch f.Kind() {
case reflect.String:
m[name] = f.String()
case reflect.Bool:
m[name] = f.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
m[name] = f.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
m[name] = f.Uint()
case reflect.Float32, reflect.Float64:
m[name] = f.Float()
case reflect.Slice:
ftek := f.Type().Elem().Kind()
if ftek == reflect.Uint8 {
m[name] = base64.StdEncoding.EncodeToString(f.Bytes())
} else {
var sl []any
for n := 0; n < f.Len(); n++ {
fn := f.Index(n)
var fv any
switch ftek {
case reflect.String:
fv = fn.String()
case reflect.Bool:
fv = fn.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fv = fn.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
fv = fn.Uint()
case reflect.Float32, reflect.Float64:
fv = fn.Float()
}
sl = append(sl, fv)
}
m[name] = sl
}
default:
panic(fmt.Sprintf("%s not supported", f.Type()))
}
}
if _, err := c.store.Write(ctx, p, m); err != nil {
return errors.Conflict(err)
}
return nil
}