-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
plugin_data.go
233 lines (201 loc) · 6.4 KB
/
plugin_data.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
230
231
232
233
/*
Copyright 2020 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package types
import (
"fmt"
"time"
"github.com/gravitational/trace"
)
// PluginData is used by plugins to store per-resource state. An instance of PluginData
// corresponds to a resource which may be managed by one or more plugins. Data is stored
// as a mapping of the form `plugin -> key -> val`, effectively giving each plugin its own
// key-value store. Importantly, an instance of PluginData can only be created for a resource
// which currently exist, and automatically expires shortly after the corresponding resource.
// Currently, only the AccessRequest resource is supported.
type PluginData interface {
Resource
// Entries gets all entries.
Entries() map[string]*PluginDataEntry
// Update attempts to apply an update.
Update(params PluginDataUpdateParams) error
}
// NewPluginData configures a new PluginData instance associated
// with the supplied resource name (currently, this must be the
// name of an access request).
func NewPluginData(resourceName string, resourceKind string) (PluginData, error) {
data := PluginDataV3{
SubKind: resourceKind,
Metadata: Metadata{
Name: resourceName,
},
Spec: PluginDataSpecV3{
Entries: make(map[string]*PluginDataEntry),
},
}
if err := data.CheckAndSetDefaults(); err != nil {
return nil, err
}
return &data, nil
}
// GetKind returns resource kind
func (r *PluginDataV3) GetKind() string {
return r.Kind
}
// GetSubKind returns resource subkind
func (r *PluginDataV3) GetSubKind() string {
return r.SubKind
}
// SetSubKind sets resource subkind
func (r *PluginDataV3) SetSubKind(subKind string) {
r.SubKind = subKind
}
// GetVersion gets resource version
func (r *PluginDataV3) GetVersion() string {
return r.Version
}
// GetName gets resource name
func (r *PluginDataV3) GetName() string {
return r.Metadata.Name
}
// SetName sets resource name
func (r *PluginDataV3) SetName(name string) {
r.Metadata.Name = name
}
// Expiry returns object expiry setting
func (r *PluginDataV3) Expiry() time.Time {
return r.Metadata.Expiry()
}
// SetExpiry sets expiry time for the object
func (r *PluginDataV3) SetExpiry(expiry time.Time) {
r.Metadata.SetExpiry(expiry)
}
// GetMetadata gets the resource metadata
func (r *PluginDataV3) GetMetadata() Metadata {
return r.Metadata
}
// GetResourceID returns resource ID
func (r *PluginDataV3) GetResourceID() int64 {
return r.Metadata.GetID()
}
// SetResourceID sets resource ID
func (r *PluginDataV3) SetResourceID(id int64) {
r.Metadata.SetID(id)
}
func (r *PluginDataV3) String() string {
return fmt.Sprintf("PluginData(kind=%s,resource=%s,entries=%d)", r.GetSubKind(), r.GetName(), len(r.Spec.Entries))
}
// setStaticFields sets static resource header and metadata fields.
func (r *PluginDataV3) setStaticFields() {
r.Kind = KindPluginData
r.Version = V3
}
// CheckAndSetDefaults checks and sets default values for PluginData.
func (r *PluginDataV3) CheckAndSetDefaults() error {
r.setStaticFields()
if err := r.Metadata.CheckAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
if r.SubKind == "" {
return trace.BadParameter("plugin data missing subkind")
}
return nil
}
// Entries returns the PluginData entires
func (r *PluginDataV3) Entries() map[string]*PluginDataEntry {
if r.Spec.Entries == nil {
r.Spec.Entries = make(map[string]*PluginDataEntry)
}
return r.Spec.Entries
}
// Update updates the PluginData
func (r *PluginDataV3) Update(params PluginDataUpdateParams) error {
// See #3286 for a complete discussion of the design constraints at play here.
if params.Kind != r.GetSubKind() {
return trace.BadParameter("resource kind mismatch in update params")
}
if params.Resource != r.GetName() {
return trace.BadParameter("resource name mismatch in update params")
}
// If expectations were given, ensure that they are met before continuing
if params.Expect != nil {
if err := r.checkExpectations(params.Plugin, params.Expect); err != nil {
return trace.Wrap(err)
}
}
// Ensure that Entries has been initialized
if r.Spec.Entries == nil {
r.Spec.Entries = make(map[string]*PluginDataEntry, 1)
}
// Ensure that the specific Plugin has been initialized
if r.Spec.Entries[params.Plugin] == nil {
r.Spec.Entries[params.Plugin] = &PluginDataEntry{
Data: make(map[string]string, len(params.Set)),
}
}
entry := r.Spec.Entries[params.Plugin]
for key, val := range params.Set {
// Keys which are explicitly set to the empty string are
// treated as DELETE operations.
if val == "" {
delete(entry.Data, key)
continue
}
entry.Data[key] = val
}
// Its possible that this update was simply clearing all data;
// if that is the case, remove the entry.
if len(entry.Data) == 0 {
delete(r.Spec.Entries, params.Plugin)
}
return nil
}
// checkExpectations verifies that the data for `plugin` matches the expected
// state described by `expect`. This function implements the behavior of the
// `PluginDataUpdateParams.Expect` mapping.
func (r *PluginDataV3) checkExpectations(plugin string, expect map[string]string) error {
var entry *PluginDataEntry
if r.Spec.Entries != nil {
entry = r.Spec.Entries[plugin]
}
if entry == nil {
// If no entry currently exists, then the only expectation that can
// match is one which only specifies fields which shouldn't exist.
for key, val := range expect {
if val != "" {
return trace.CompareFailed("expectations not met for field %q", key)
}
}
return nil
}
for key, val := range expect {
if entry.Data[key] != val {
return trace.CompareFailed("expectations not met for field %q", key)
}
}
return nil
}
// Match returns true if the PluginData given matches the filter
func (f *PluginDataFilter) Match(data PluginData) bool {
if f.Kind != "" && f.Kind != data.GetSubKind() {
return false
}
if f.Resource != "" && f.Resource != data.GetName() {
return false
}
if f.Plugin != "" {
if _, ok := data.Entries()[f.Plugin]; !ok {
return false
}
}
return true
}