forked from gravitational/teleport
/
license.go
366 lines (306 loc) · 10.3 KB
/
license.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
Copyright 2018-2019 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"
"strings"
"time"
"github.com/gravitational/trace"
)
// License defines teleport License Information
type License interface {
Resource
// GetReportsUsage returns true if teleport cluster reports usage
// to control plane
GetReportsUsage() Bool
// SetReportsUsage sets usage report
SetReportsUsage(Bool)
// GetCloud returns true if teleport cluster is hosted by Gravitational
GetCloud() Bool
// SetCloud sets cloud flag
SetCloud(Bool)
// GetAWSProductID returns product id that limits usage to AWS instance
// with a similar product ID
GetAWSProductID() string
// SetAWSProductID sets AWS product ID
SetAWSProductID(string)
// GetAWSAccountID limits usage to AWS instance within account ID
GetAWSAccountID() string
// SetAWSAccountID sets AWS account ID that will be limiting
// usage to AWS instance
SetAWSAccountID(accountID string)
// GetSupportsKubernetes returns kubernetes support flag
GetSupportsKubernetes() Bool
// SetSupportsKubernetes sets kubernetes support flag
SetSupportsKubernetes(Bool)
// GetSupportsApplicationAccess returns application access support flag
GetSupportsApplicationAccess() Bool
// SetSupportsApplicationAccess sets application access support flag
SetSupportsApplicationAccess(Bool)
// GetSupportsDatabaseAccess returns database access support flag
GetSupportsDatabaseAccess() Bool
// SetSupportsDatabaseAccess sets database access support flag
SetSupportsDatabaseAccess(Bool)
// GetSupportsDesktopAccess returns desktop access support flag
GetSupportsDesktopAccess() Bool
// SetSupportsDesktopAccess sets desktop access support flag
SetSupportsDesktopAccess(Bool)
// GetTrial returns the trial flag.
// Note: This is not applicable to Cloud licenses
GetTrial() Bool
// SetTrial sets the trial flag.
// Note: This is not applicable to Cloud licenses
SetTrial(Bool)
// SetLabels sets metadata labels
SetLabels(labels map[string]string)
// GetAccountID returns Account ID.
// Note: This is not applicable to all Cloud licenses
GetAccountID() string
}
// NewLicense is a convenience method to create LicenseV3.
func NewLicense(name string, spec LicenseSpecV3) (License, error) {
l := &LicenseV3{
Metadata: Metadata{
Name: name,
},
Spec: spec,
}
if err := l.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return l, nil
}
// LicenseV3 represents License resource version V3
type LicenseV3 struct {
// Kind is a resource kind - always resource.
Kind string `json:"kind"`
// SubKind is a resource sub kind
SubKind string `json:"sub_kind,omitempty"`
// Version is a resource version.
Version string `json:"version"`
// Metadata is metadata about the resource.
Metadata Metadata `json:"metadata"`
// Spec is the specification of the resource.
Spec LicenseSpecV3 `json:"spec"`
}
// GetVersion returns resource version
func (c *LicenseV3) GetVersion() string {
return c.Version
}
// GetSubKind returns resource sub kind
func (c *LicenseV3) GetSubKind() string {
return c.SubKind
}
// SetSubKind sets resource subkind
func (c *LicenseV3) SetSubKind(s string) {
c.SubKind = s
}
// GetKind returns resource kind
func (c *LicenseV3) GetKind() string {
return c.Kind
}
// GetResourceID returns resource ID
func (c *LicenseV3) GetResourceID() int64 {
return c.Metadata.ID
}
// SetResourceID sets resource ID
func (c *LicenseV3) SetResourceID(id int64) {
c.Metadata.ID = id
}
// GetName returns the name of the resource
func (c *LicenseV3) GetName() string {
return c.Metadata.Name
}
// SetLabels sets metadata labels
func (c *LicenseV3) SetLabels(labels map[string]string) {
c.Metadata.Labels = labels
}
// GetLabels returns metadata labels
func (c *LicenseV3) GetLabels() map[string]string {
return c.Metadata.Labels
}
// SetName sets the name of the resource
func (c *LicenseV3) SetName(name string) {
c.Metadata.Name = name
}
// Expiry returns object expiry setting
func (c *LicenseV3) Expiry() time.Time {
return c.Metadata.Expiry()
}
// SetExpiry sets object expiry
func (c *LicenseV3) SetExpiry(t time.Time) {
c.Metadata.SetExpiry(t)
}
// GetMetadata returns object metadata
func (c *LicenseV3) GetMetadata() Metadata {
return c.Metadata
}
// GetReportsUsage returns true if teleport cluster reports usage
// to control plane
func (c *LicenseV3) GetReportsUsage() Bool {
return c.Spec.ReportsUsage
}
// GetCloud returns true if teleport cluster is hosted by Gravitational
func (c *LicenseV3) GetCloud() Bool {
return c.Spec.Cloud
}
// SetCloud sets cloud flag
func (c *LicenseV3) SetCloud(cloud Bool) {
c.Spec.Cloud = cloud
}
// SetReportsUsage sets usage report
func (c *LicenseV3) SetReportsUsage(reports Bool) {
c.Spec.ReportsUsage = reports
}
// setStaticFields sets static resource header and metadata fields.
func (c *LicenseV3) setStaticFields() {
c.Kind = KindLicense
c.Version = V3
}
// CheckAndSetDefaults verifies the constraints for License.
func (c *LicenseV3) CheckAndSetDefaults() error {
c.setStaticFields()
if err := c.Metadata.CheckAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
return nil
}
// GetAWSProductID returns product ID that limits usage to AWS instance
// with a similar product ID
func (c *LicenseV3) GetAWSProductID() string {
return c.Spec.AWSProductID
}
// SetAWSProductID sets AWS product ID
func (c *LicenseV3) SetAWSProductID(pid string) {
c.Spec.AWSProductID = pid
}
// GetAccountID sets AWS product ID
func (c *LicenseV3) GetAccountID() string {
return c.Spec.AccountID
}
// GetAWSAccountID limits usage to AWS instance within account ID
func (c *LicenseV3) GetAWSAccountID() string {
return c.Spec.AWSAccountID
}
// SetAWSAccountID sets AWS account ID that will be limiting
// usage to AWS instance
func (c *LicenseV3) SetAWSAccountID(accountID string) {
c.Spec.AWSAccountID = accountID
}
// GetSupportsKubernetes returns kubernetes support flag
func (c *LicenseV3) GetSupportsKubernetes() Bool {
return c.Spec.SupportsKubernetes
}
// SetSupportsKubernetes sets kubernetes support flag
func (c *LicenseV3) SetSupportsKubernetes(supportsK8s Bool) {
c.Spec.SupportsKubernetes = supportsK8s
}
// GetSupportsApplicationAccess returns application access support flag
func (c *LicenseV3) GetSupportsApplicationAccess() Bool {
// For backward compatibility return true if app access flag isn't set,
// or it will stop working for all users who are already using it and
// were issued licenses without this flag.
if c.Spec.SupportsApplicationAccess == nil {
return Bool(true)
}
return *c.Spec.SupportsApplicationAccess
}
// SetSupportsApplicationAccess sets application access support flag
func (c *LicenseV3) SetSupportsApplicationAccess(value Bool) {
c.Spec.SupportsApplicationAccess = &value
}
// GetSupportsDatabaseAccess returns database access support flag
func (c *LicenseV3) GetSupportsDatabaseAccess() Bool {
return c.Spec.SupportsDatabaseAccess
}
// SetSupportsDatabaseAccess sets database access support flag
func (c *LicenseV3) SetSupportsDatabaseAccess(value Bool) {
c.Spec.SupportsDatabaseAccess = value
}
// GetSupportsDesktopAccess returns desktop access support flag
func (c *LicenseV3) GetSupportsDesktopAccess() Bool {
return c.Spec.SupportsDesktopAccess
}
// SetSupportsDesktopAccess sets desktop access support flag
func (c *LicenseV3) SetSupportsDesktopAccess(value Bool) {
c.Spec.SupportsDesktopAccess = value
}
// GetTrial returns the trial flag
func (c *LicenseV3) GetTrial() Bool {
return c.Spec.Trial
}
// SetTrial sets the trial flag
func (c *LicenseV3) SetTrial(value Bool) {
c.Spec.Trial = value
}
// String represents a human readable version of license enabled features
func (c *LicenseV3) String() string {
var features []string
if !c.Expiry().IsZero() {
features = append(features, fmt.Sprintf("expires at %v", c.Expiry()))
}
if c.GetTrial() {
features = append(features, "is trial")
}
if c.GetReportsUsage() {
features = append(features, "reports usage")
}
if c.GetSupportsKubernetes() {
features = append(features, "supports kubernetes")
}
if c.GetSupportsApplicationAccess() {
features = append(features, "supports application access")
}
if c.GetSupportsDatabaseAccess() {
features = append(features, "supports database access")
}
if c.GetSupportsDesktopAccess() {
features = append(features, "supports desktop access")
}
if c.GetCloud() {
features = append(features, "is hosted by Gravitational")
}
if c.GetAWSProductID() != "" {
features = append(features, fmt.Sprintf("is limited to AWS product ID %q", c.Spec.AWSProductID))
}
if c.GetAWSAccountID() != "" {
features = append(features, fmt.Sprintf("is limited to AWS account ID %q", c.Spec.AWSAccountID))
}
if len(features) == 0 {
return ""
}
return strings.Join(features, ",")
}
// LicenseSpecV3 is the actual data we care about for LicenseV3.
type LicenseSpecV3 struct {
// AccountID is a customer account ID
AccountID string `json:"account_id,omitempty"`
// AWSProductID limits usage to AWS instance with a product ID
AWSProductID string `json:"aws_pid,omitempty"`
// AWSAccountID limits usage to AWS instance within account ID
AWSAccountID string `json:"aws_account,omitempty"`
// SupportsKubernetes turns kubernetes support on or off
SupportsKubernetes Bool `json:"k8s"`
// SupportsApplicationAccess turns application access on or off
// Note it's a pointer for backward compatibility
SupportsApplicationAccess *Bool `json:"app,omitempty"`
// SupportsDatabaseAccess turns database access on or off
SupportsDatabaseAccess Bool `json:"db,omitempty"`
// SupportsDesktopAccess turns desktop access on or off
SupportsDesktopAccess Bool `json:"desktop,omitempty"`
// ReportsUsage turns usage reporting on or off
ReportsUsage Bool `json:"usage,omitempty"`
// Cloud is turned on when teleport is hosted by Gravitational
Cloud Bool `json:"cloud,omitempty"`
Trial Bool `json:"trial,omitempty"`
}