-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
327 lines (273 loc) · 7.97 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
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
318
319
320
321
322
323
324
325
326
327
/*
Copyright 2015 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 services
import (
"encoding/json"
"fmt"
"strings"
"sync"
"github.com/gravitational/trace"
)
const (
// DefaultAPIGroup is a default group of permissions API,
// lets us to add different permission types
DefaultAPIGroup = "gravitational.io/teleport"
// ActionRead grants read access (get, list)
ActionRead = "read"
// ActionWrite allows to write (create, update, delete)
ActionWrite = "write"
// Wildcard is a special wildcard character matching everything
Wildcard = "*"
// KindNamespace is a namespace
KindNamespace = "namespace"
// KindUser is a user resource
KindUser = "user"
// KindKeyPair is a public/private key pair
KindKeyPair = "key_pair"
// KindHostCert is a host certificate
KindHostCert = "host_cert"
// KindRole is a role resource
KindRole = "role"
// KindOIDC is oidc connector resource
KindOIDC = "oidc"
// KindOIDCReques is oidc auth request resource
KindOIDCRequest = "oidc_request"
// KindSession is a recorded session resource
KindSession = "session"
// KindWebSession is a web session resource
KindWebSession = "web_session"
// KindEvent is structured audit logging event
KindEvent = "event"
// KindAuthServer is auth server resource
KindAuthServer = "auth_server"
// KindProxy is proxy resource
KindProxy = "proxy"
// KindNode is node resource
KindNode = "node"
// KindToken is a provisioning token resource
KindToken = "token"
// KindCertAuthority is a certificate authority resource
KindCertAuthority = "cert_authority"
// KindReverseTunnel is a reverse tunnel connection
KindReverseTunnel = "tunnel"
// KindOIDCConnector is a OIDC connector resource
KindOIDCConnector = "oidc"
// KindAuthPreference is the type of authentication for this cluster.
KindClusterAuthPreference = "cluster_auth_preference"
// KindAuthPreference is the type of authentication for this cluster.
MetaNameClusterAuthPreference = "cluster-auth-preference"
// KindUniversalSecondFactor is a type of second factor authentication.
KindUniversalSecondFactor = "universal_second_factor"
// MetaNameUniversalSecondFactor is a type of second factor authentication.
MetaNameUniversalSecondFactor = "universal-second-factor"
// KindTrustedCluster is a resource that contains trusted cluster configuration.
KindTrustedCluster = "trusted_cluster"
// V2 is our current version
V2 = "v2"
// V1 is our first version
// resources were not explicitly versioned at that point
V1 = "v1"
)
func collectOptions(opts []MarshalOption) (*MarshalConfig, error) {
var cfg MarshalConfig
for _, o := range opts {
if err := o(&cfg); err != nil {
return nil, trace.Wrap(err)
}
}
return &cfg, nil
}
// MarshalConfig specify marshalling options
type MarshalConfig struct {
// Version specifies particular version we should marshal resources with
Version string
}
// GetVersion returns explicitly provided version or sets latest as default
func (m *MarshalConfig) GetVersion() string {
if m.Version == "" {
return V2
}
return m.Version
}
// MarshalOption sets marshalling option
type MarshalOption func(c *MarshalConfig) error
// WithVersion sets marshal version
func WithVersion(v string) MarshalOption {
return func(c *MarshalConfig) error {
switch v {
case V1, V2:
c.Version = v
return nil
default:
return trace.BadParameter("version '%v' is not supported", v)
}
}
}
// marshalerMutex is a mutex for resource marshalers/unmarshalers
var marshalerMutex sync.RWMutex
// V2SchemaTemplate is a template JSON Schema for V2 style objects
const V2SchemaTemplate = `{
"type": "object",
"additionalProperties": false,
"required": ["kind", "spec", "metadata", "version"],
"properties": {
"kind": {"type": "string"},
"version": {"type": "string", "default": "v2"},
"metadata": %v,
"spec": %v
}
}`
// MetadataSchema is a schema for resource metadata
const MetadataSchema = `{
"type": "object",
"additionalProperties": false,
"default": {},
"required": ["name"],
"properties": {
"name": {"type": "string"},
"namespace": {"type": "string", "default": "default"},
"description": {"type": "string"},
"labels": {
"type": "object",
"patternProperties": {
"^[a-zA-Z/.0-9_]$": { "type": "string" }
}
}
}
}`
// UnknownResource is used to detect resources
type UnknownResource struct {
ResourceHeader
// Raw is raw representation of the resource
Raw []byte
}
// ResorceHeader is a shared resource header
type ResourceHeader struct {
// Kind is a resource kind - always resource
Kind string `json:"kind"`
// Version is a resource version
Version string `json:"version"`
// Metadata is Role metadata
Metadata Metadata `json:"metadata"`
}
// UnmarshalJSON unmarshals header and captures raw state
func (u *UnknownResource) UnmarshalJSON(raw []byte) error {
var h ResourceHeader
if err := json.Unmarshal(raw, &h); err != nil {
return trace.Wrap(err)
}
u.Raw = make([]byte, len(raw))
u.ResourceHeader = h
copy(u.Raw, raw)
return nil
}
// Metadata is resource metadata
type Metadata struct {
// Name is an object name
Name string `json:"name"`
// Namespace is object namespace
Namespace string `json:"namespace"`
// Description is object description
Description string `json:"description,omitempty"`
// Labels is a set of labels
Labels map[string]string `json:"labels,omitempty"`
}
// Check checks validity of all parameters and sets defaults
func (m *Metadata) Check() error {
if m.Name == "" {
return trace.BadParameter("missing parameter Name")
}
return nil
}
// ParseShortcut parses resource shortcut
func ParseShortcut(in string) (string, error) {
if in == "" {
return "", trace.BadParameter("missing resource name")
}
switch strings.ToLower(in) {
case "roles":
return KindRole, nil
case "namespaces", "ns":
return KindNamespace, nil
case "auth_servers", "auth":
return KindAuthServer, nil
case "proxies":
return KindProxy, nil
case "nodes":
return KindNode, nil
case "oidc":
return KindOIDCConnector, nil
case "users":
return KindUser, nil
case "cert_authorities", "cas":
return KindCertAuthority, nil
case "reverse_tunnels", "rts":
return KindReverseTunnel, nil
case "trusted_cluster", "tc":
return KindTrustedCluster, nil
case "cluster_authentication_preferences", "cap":
return KindClusterAuthPreference, nil
case "universal_second_factor", "u2f":
return KindUniversalSecondFactor, nil
}
return "", trace.BadParameter("unsupported resource: %v", in)
}
// ParseRef parses resource reference eg daemonsets/ds1
func ParseRef(ref string) (*Ref, error) {
if ref == "" {
return nil, trace.BadParameter("missing value")
}
parts := strings.FieldsFunc(ref, isDelimiter)
switch len(parts) {
case 1:
shortcut, err := ParseShortcut(parts[0])
if err != nil {
return nil, trace.Wrap(err)
}
return &Ref{Kind: shortcut}, nil
case 2:
shortcut, err := ParseShortcut(parts[0])
if err != nil {
return nil, trace.Wrap(err)
}
return &Ref{Kind: shortcut, Name: parts[1]}, nil
}
return nil, trace.BadParameter("failed to parse '%v'", ref)
}
// isDelimiter returns true if rune is space or /
func isDelimiter(r rune) bool {
switch r {
case '\t', ' ', '/':
return true
}
return false
}
// Ref is a resource refernece
type Ref struct {
Kind string
Name string
}
func (r *Ref) IsEmtpy() bool {
return r.Name == ""
}
func (r *Ref) Set(v string) error {
out, err := ParseRef(v)
if err != nil {
return err
}
*r = *out
return nil
}
func (r *Ref) String() string {
return fmt.Sprintf("%v/%v", r.Kind, r.Name)
}