-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
resource.go
236 lines (197 loc) · 5.75 KB
/
resource.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
234
235
236
/*
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 (
"regexp"
"time"
"github.com/gravitational/teleport/api/v7/defaults"
"github.com/gravitational/teleport/api/v7/utils"
"github.com/gravitational/trace"
)
// Resource represents common properties for all resources.
type Resource interface {
// GetKind returns resource kind
GetKind() string
// GetSubKind returns resource subkind
GetSubKind() string
// SetSubKind sets resource subkind
SetSubKind(string)
// GetVersion returns resource version
GetVersion() string
// GetName returns the name of the resource
GetName() string
// SetName sets the name of the resource
SetName(string)
// Expiry returns object expiry setting
Expiry() time.Time
// SetExpiry sets object expiry
SetExpiry(time.Time)
// GetMetadata returns object metadata
GetMetadata() Metadata
// GetResourceID returns resource ID
GetResourceID() int64
// SetResourceID sets resource ID
SetResourceID(int64)
// CheckAndSetDefaults validates the Resource and sets any empty fields to
// default values.
CheckAndSetDefaults() error
}
// ResourceWithSecrets includes additional properties which must
// be provided by resources which *may* contain secrets.
type ResourceWithSecrets interface {
Resource
// WithoutSecrets returns an instance of the resource which
// has had all secrets removed. If the current resource has
// already had its secrets removed, this may be a no-op.
WithoutSecrets() Resource
}
// ResourceWithOrigin provides information on the origin of the resource
// (defaults, config-file, dynamic).
type ResourceWithOrigin interface {
Resource
// Origin returns the origin value of the resource.
Origin() string
// SetOrigin sets the origin value of the resource.
SetOrigin(string)
}
// GetVersion returns resource version
func (h *ResourceHeader) GetVersion() string {
return h.Version
}
// GetResourceID returns resource ID
func (h *ResourceHeader) GetResourceID() int64 {
return h.Metadata.ID
}
// SetResourceID sets resource ID
func (h *ResourceHeader) SetResourceID(id int64) {
h.Metadata.ID = id
}
// GetName returns the name of the resource
func (h *ResourceHeader) GetName() string {
return h.Metadata.Name
}
// SetName sets the name of the resource
func (h *ResourceHeader) SetName(v string) {
h.Metadata.SetName(v)
}
// Expiry returns object expiry setting
func (h *ResourceHeader) Expiry() time.Time {
return h.Metadata.Expiry()
}
// SetExpiry sets object expiry
func (h *ResourceHeader) SetExpiry(t time.Time) {
h.Metadata.SetExpiry(t)
}
// GetMetadata returns object metadata
func (h *ResourceHeader) GetMetadata() Metadata {
return h.Metadata
}
// GetKind returns resource kind
func (h *ResourceHeader) GetKind() string {
return h.Kind
}
// GetSubKind returns resource subkind
func (h *ResourceHeader) GetSubKind() string {
return h.SubKind
}
// SetSubKind sets resource subkind
func (h *ResourceHeader) SetSubKind(s string) {
h.SubKind = s
}
func (h *ResourceHeader) CheckAndSetDefaults() error {
if h.Kind == "" {
return trace.BadParameter("resource has an empty Kind field")
}
if h.Version == "" {
return trace.BadParameter("resource has an empty Version field")
}
return trace.Wrap(h.Metadata.CheckAndSetDefaults())
}
// GetID returns resource ID
func (m *Metadata) GetID() int64 {
return m.ID
}
// SetID sets resource ID
func (m *Metadata) SetID(id int64) {
m.ID = id
}
// GetMetadata returns object metadata
func (m *Metadata) GetMetadata() Metadata {
return *m
}
// GetName returns the name of the resource
func (m *Metadata) GetName() string {
return m.Name
}
// SetName sets the name of the resource
func (m *Metadata) SetName(name string) {
m.Name = name
}
// SetExpiry sets expiry time for the object
func (m *Metadata) SetExpiry(expires time.Time) {
m.Expires = &expires
}
// Expiry returns object expiry setting.
func (m *Metadata) Expiry() time.Time {
if m.Expires == nil {
return time.Time{}
}
return *m.Expires
}
// Origin returns the origin value of the resource.
func (m *Metadata) Origin() string {
if m.Labels == nil {
return ""
}
return m.Labels[OriginLabel]
}
// SetOrigin sets the origin value of the resource.
func (m *Metadata) SetOrigin(origin string) {
if m.Labels == nil {
m.Labels = map[string]string{}
}
m.Labels[OriginLabel] = origin
}
// CheckAndSetDefaults checks validity of all parameters and sets defaults
func (m *Metadata) CheckAndSetDefaults() error {
if m.Name == "" {
return trace.BadParameter("missing parameter Name")
}
if m.Namespace == "" {
m.Namespace = defaults.Namespace
}
// adjust expires time to UTC if it's set
if m.Expires != nil {
utils.UTC(m.Expires)
}
for key := range m.Labels {
if !IsValidLabelKey(key) {
return trace.BadParameter("invalid label key: %q", key)
}
}
// Check the origin value.
if m.Origin() != "" {
if !utils.SliceContainsStr(OriginValues, m.Origin()) {
return trace.BadParameter("invalid origin value %q, must be one of %v", m.Origin(), OriginValues)
}
}
return nil
}
// LabelPattern is a regexp that describes a valid label key
const LabelPattern = `^[a-zA-Z/.0-9_*-]+$`
var validLabelKey = regexp.MustCompile(LabelPattern)
// IsValidLabelKey checks if the supplied string matches the
// label key regexp.
func IsValidLabelKey(s string) bool {
return validLabelKey.MatchString(s)
}