forked from juju/description
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remotesecrets.go
191 lines (167 loc) · 5.21 KB
/
remotesecrets.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
// Copyright 2023 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/schema"
"github.com/rs/xid"
)
// RemoteSecret represents consumer info for a remote secret.
type RemoteSecret interface {
ID() string
SourceUUID() string
Consumer() (names.Tag, error)
Label() string
CurrentRevision() int
LatestRevision() int
Validate() error
}
type remoteSecrets struct {
Version int `yaml:"version"`
RemoteSecrets_ []*remoteSecret `yaml:"remote-secrets"`
}
type remoteSecret struct {
ID_ string `yaml:"id"`
SourceUUID_ string `yaml:"source-uuid"`
Consumer_ string `yaml:"consumer"`
Label_ string `yaml:"label"`
CurrentRevision_ int `yaml:"current-revision"`
LatestRevision_ int `yaml:"latest-revision"`
}
// RemoteSecretArgs is an argument struct used to create a
// new internal remote secret type that supports the remote
// secret interface.
type RemoteSecretArgs struct {
ID string
SourceUUID string
Consumer names.Tag
Label string
CurrentRevision int
LatestRevision int
}
func newRemoteSecret(args RemoteSecretArgs) *remoteSecret {
s := &remoteSecret{
ID_: args.ID,
SourceUUID_: args.SourceUUID,
Label_: args.Label,
CurrentRevision_: args.CurrentRevision,
LatestRevision_: args.LatestRevision,
}
if args.Consumer != nil {
s.Consumer_ = args.Consumer.String()
}
return s
}
// ID implements RemoteSecret.
func (i *remoteSecret) ID() string {
return i.ID_
}
// SourceUUID implements RemoteSecret.
func (i *remoteSecret) SourceUUID() string {
return i.SourceUUID_
}
// Consumer implements RemoteSecret.
func (i *remoteSecret) Consumer() (names.Tag, error) {
if i.Consumer_ == "" {
return nil, nil
}
tag, err := names.ParseTag(i.Consumer_)
if err != nil {
return nil, errors.Trace(err)
}
return tag, nil
}
// Label implements RemoteSecret.
func (i *remoteSecret) Label() string {
return i.Label_
}
// CurrentRevision implements RemoteSecret.
func (i *remoteSecret) CurrentRevision() int {
return i.CurrentRevision_
}
// LatestRevision implements RemoteSecret.
func (i *remoteSecret) LatestRevision() int {
return i.LatestRevision_
}
// Validate implements RemoteSecret.
func (i *remoteSecret) Validate() error {
if i.ID_ == "" {
return errors.NotValidf("remote secret missing id")
}
if _, err := xid.FromString(i.ID_); err != nil {
return errors.Wrap(err, errors.NotValidf("remote secret ID %q", i.ID_))
}
if _, err := i.Consumer(); err != nil {
return errors.Wrap(err, errors.NotValidf("remote secret %q invalid consumer", i.ID_))
}
return nil
}
func importRemoteSecrets(source map[string]interface{}) ([]*remoteSecret, error) {
checker := versionedChecker("remote-secrets")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "remote secret version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
sourceList := valid["remote-secrets"].([]interface{})
return importRemoteSecretList(sourceList, version)
}
func importRemoteSecretList(sourceList []interface{}, version int) ([]*remoteSecret, error) {
getFields, ok := remoteSecretFieldsFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
result := make([]*remoteSecret, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for remote secret %d, %T", i, value)
}
secretConsumer, err := importRemoteSecret(source, version, getFields)
if err != nil {
return nil, errors.Annotatef(err, "remote secret %d", i)
}
result = append(result, secretConsumer)
}
return result, nil
}
var remoteSecretFieldsFuncs = map[int]fieldsFunc{
1: remoteSecretV1Fields,
}
func remoteSecretV1Fields() (schema.Fields, schema.Defaults) {
fields := schema.Fields{
"id": schema.String(),
"source-uuid": schema.String(),
"consumer": schema.String(),
"label": schema.String(),
"current-revision": schema.Int(),
"latest-revision": schema.Int(),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"label": schema.Omit,
}
return fields, defaults
}
func importRemoteSecret(source map[string]interface{}, importVersion int, fieldFunc func() (schema.Fields, schema.Defaults)) (*remoteSecret, error) {
fields, defaults := fieldFunc()
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "remote secrets v%d schema check failed", importVersion)
}
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
consumer := &remoteSecret{
ID_: valid["id"].(string),
SourceUUID_: valid["source-uuid"].(string),
Consumer_: valid["consumer"].(string),
Label_: valid["label"].(string),
CurrentRevision_: int(valid["current-revision"].(int64)),
LatestRevision_: int(valid["latest-revision"].(int64)),
}
return consumer, nil
}