-
Notifications
You must be signed in to change notification settings - Fork 0
/
license.go
287 lines (236 loc) · 7.51 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
/*
Copyright 2018 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"
"time"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
)
// 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)
// 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)
// SetLabels sets metadata labels
SetLabels(labels map[string]string)
// GetAccountID returns Account ID
GetAccountID() string
// CheckAndSetDefaults sets and default values and then
// verifies the constraints for License.
CheckAndSetDefaults() error
}
// NewLicense is a convenience method to to create LicenseV3.
func NewLicense(name string, spec LicenseSpecV3) (License, error) {
return &LicenseV3{
Kind: KindLicense,
Version: V3,
Metadata: Metadata{
Name: name,
Namespace: defaults.Namespace,
},
Spec: spec,
}, nil
}
// LicenseV3 represents License resource version V3
type LicenseV3 struct {
// Kind is a resource kind - always resource.
Kind string `json:"kind"`
// 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"`
}
// 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)
}
// SetTTL sets Expires header using current clock
func (c *LicenseV3) SetTTL(clock clockwork.Clock, ttl time.Duration) {
c.Metadata.SetTTL(clock, ttl)
}
// 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
}
// SetReportsUsage sets usage report
func (c *LicenseV3) SetReportsUsage(reports Bool) {
c.Spec.ReportsUsage = reports
}
// CheckAndSetDefaults verifies the constraints for License.
func (c *LicenseV3) CheckAndSetDefaults() error {
return c.Metadata.CheckAndSetDefaults()
}
// 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
}
// 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.Spec.ReportsUsage.Value() {
features = append(features, "reports usage")
}
if c.Spec.SupportsKubernetes.Value() {
features = append(features, "supports kubernetes")
}
if c.Spec.AWSProductID != "" {
features = append(features, fmt.Sprintf("is limited to AWS product ID %q", c.Spec.AWSProductID))
}
if c.Spec.AWSAccountID != "" {
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"`
// ReportsUsage is turned on when system reports usage
ReportsUsage Bool `json:"usage,omitempty"`
}
// LicenseSpecV3Template is a template for V3 License JSON schema
const LicenseSpecV3Template = `{
"type": "object",
"additionalProperties": false,
"properties": {
"account_id": {
"type": ["string"]
},
"plan_id": {
"type": ["string"]
},
"usage": {
"type": ["string", "boolean"]
},
"aws_pid": {
"type": ["string"]
},
"aws_account": {
"type": ["string"]
},
"k8s": {
"type": ["string", "boolean"]
}
}
}`
// UnmarshalLicense unmarshals License from JSON or YAML
// and validates schema
func UnmarshalLicense(bytes []byte) (License, error) {
if len(bytes) == 0 {
return nil, trace.BadParameter("missing resource data")
}
schema := fmt.Sprintf(V2SchemaTemplate, MetadataSchema, LicenseSpecV3Template, DefaultDefinitions)
var license LicenseV3
err := utils.UnmarshalWithSchema(schema, &license, bytes)
if err != nil {
return nil, trace.BadParameter(err.Error())
}
if license.Version != V3 {
return nil, trace.BadParameter("unsupported version %v, expected version %v", license.Version, V3)
}
if err := license.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return &license, nil
}
// MarshalLicense marshals role to JSON or YAML.
func MarshalLicense(license License, opts ...MarshalOption) ([]byte, error) {
return json.Marshal(license)
}