/
licenses.go
266 lines (248 loc) · 6.39 KB
/
licenses.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
package cli
import (
"encoding/json"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"golang.org/x/xerrors"
"github.com/coder/coder/cli/clibase"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
)
var jwtRegexp = regexp.MustCompile(`^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$`)
func (r *RootCmd) licenses() *clibase.Cmd {
cmd := &clibase.Cmd{
Short: "Add, delete, and list licenses",
Use: "licenses",
Aliases: []string{"license"},
Handler: func(inv *clibase.Invocation) error {
return inv.Command.HelpHandler(inv)
},
Children: []*clibase.Cmd{
r.licenseAdd(),
r.licensesList(),
r.licenseDelete(),
},
}
return cmd
}
func (r *RootCmd) licenseAdd() *clibase.Cmd {
var (
filename string
license string
debug bool
)
client := new(codersdk.Client)
cmd := &clibase.Cmd{
Use: "add [-f file | -l license]",
Short: "Add license to Coder deployment",
Middleware: clibase.Chain(
clibase.RequireNArgs(0),
r.InitClient(client),
),
Handler: func(inv *clibase.Invocation) error {
var err error
switch {
case filename != "" && license != "":
return xerrors.New("only one of (--file, --license) may be specified")
case filename == "" && license == "":
license, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: "Paste license:",
Secret: true,
Validate: validJWT,
})
if err != nil {
return err
}
case filename != "" && license == "":
var r io.Reader
if filename == "-" {
r = inv.Stdin
} else {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
r = f
}
lb, err := io.ReadAll(r)
if err != nil {
return err
}
license = string(lb)
}
license = strings.Trim(license, " \n")
err = validJWT(license)
if err != nil {
return err
}
licResp, err := client.AddLicense(
inv.Context(),
codersdk.AddLicenseRequest{License: license},
)
if err != nil {
return err
}
if debug {
enc := json.NewEncoder(inv.Stdout)
enc.SetIndent("", " ")
return enc.Encode(licResp)
}
_, _ = fmt.Fprintf(inv.Stdout, "License with ID %d added\n", licResp.ID)
return nil
},
}
cmd.Options = clibase.OptionSet{
{
Flag: "file",
FlagShorthand: "f",
Description: "Load license from file.",
Value: clibase.StringOf(&filename),
},
{
Flag: "license",
FlagShorthand: "l",
Description: "License string.",
Value: clibase.StringOf(&license),
},
{
Flag: "debug",
Description: "Output license claims for debugging.",
Value: clibase.BoolOf(&debug),
},
}
return cmd
}
func validJWT(s string) error {
if jwtRegexp.MatchString(s) {
return nil
}
return xerrors.New("Invalid license")
}
func (r *RootCmd) licensesList() *clibase.Cmd {
type tableLicense struct {
ID int32 `table:"id,default_sort"`
UUID uuid.UUID `table:"uuid" format:"uuid"`
UploadedAt time.Time `table:"uploaded_at" format:"date-time"`
// Features is the formatted string for the license claims.
// Used for the table view.
Features string `table:"features"`
ExpiresAt time.Time `table:"expires_at" format:"date-time"`
Trial bool `table:"trial"`
}
formatter := cliui.NewOutputFormatter(
cliui.ChangeFormatterData(
cliui.TableFormat([]tableLicense{}, []string{"UUID", "Expires At", "Uploaded At", "Features"}),
func(data any) (any, error) {
list, ok := data.([]codersdk.License)
if !ok {
return nil, xerrors.Errorf("invalid data type %T", data)
}
out := make([]tableLicense, 0, len(list))
for _, lic := range list {
var formattedFeatures string
features, err := lic.FeaturesClaims()
if err != nil {
formattedFeatures = xerrors.Errorf("invalid license: %w", err).Error()
} else {
var strs []string
if lic.AllFeaturesClaim() {
// If all features are enabled, just include that
strs = append(strs, "all features")
} else {
for k, v := range features {
if v > 0 {
// Only include claims > 0
strs = append(strs, fmt.Sprintf("%s=%v", k, v))
}
}
}
formattedFeatures = strings.Join(strs, ", ")
}
// If this returns an error, a zero time is returned.
exp, _ := lic.ExpiresAt()
out = append(out, tableLicense{
ID: lic.ID,
UUID: lic.UUID,
UploadedAt: lic.UploadedAt,
Features: formattedFeatures,
ExpiresAt: exp,
Trial: lic.Trial(),
})
}
return out, nil
}),
cliui.ChangeFormatterData(cliui.JSONFormat(), func(data any) (any, error) {
list, ok := data.([]codersdk.License)
if !ok {
return nil, xerrors.Errorf("invalid data type %T", data)
}
for i := range list {
humanExp, err := list[i].ExpiresAt()
if err == nil {
list[i].Claims[codersdk.LicenseExpiryClaim+"_human"] = humanExp.Format(time.RFC3339)
}
}
return list, nil
}),
)
client := new(codersdk.Client)
cmd := &clibase.Cmd{
Use: "list",
Short: "List licenses (including expired)",
Aliases: []string{"ls"},
Middleware: clibase.Chain(
clibase.RequireNArgs(0),
r.InitClient(client),
),
Handler: func(inv *clibase.Invocation) error {
licenses, err := client.Licenses(inv.Context())
if err != nil {
return err
}
// Ensure that we print "[]" instead of "null" when there are no licenses.
if licenses == nil {
licenses = make([]codersdk.License, 0)
}
out, err := formatter.Format(inv.Context(), licenses)
if err != nil {
return err
}
_, err = fmt.Fprintln(inv.Stdout, out)
return err
},
}
formatter.AttachOptions(&cmd.Options)
return cmd
}
func (r *RootCmd) licenseDelete() *clibase.Cmd {
client := new(codersdk.Client)
cmd := &clibase.Cmd{
Use: "delete <id>",
Short: "Delete license by ID",
Aliases: []string{"del"},
Middleware: clibase.Chain(
clibase.RequireNArgs(1),
r.InitClient(client),
),
Handler: func(inv *clibase.Invocation) error {
id, err := strconv.ParseInt(inv.Args[0], 10, 32)
if err != nil {
return xerrors.Errorf("license ID must be an integer: %s", inv.Args[0])
}
err = client.DeleteLicense(inv.Context(), int32(id))
if err != nil {
return err
}
_, _ = fmt.Fprintf(inv.Stdout, "License with ID %d deleted\n", id)
return nil
},
}
return cmd
}