-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcredentials.go
More file actions
568 lines (512 loc) · 16.7 KB
/
credentials.go
File metadata and controls
568 lines (512 loc) · 16.7 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"text/tabwriter"
"time"
)
// TokenMeta holds metadata about a token fetched.
type TokenMeta struct {
Login string `json:"login"`
Expires string `json:"expires"`
Scopes string `json:"scopes"`
RateLimitRemaining int `json:"rate_limit_remaining"`
RateLimitMax int `json:"rate_limit_max"`
RateLimitReset int64 `json:"rate_limit_reset"`
}
// TokenMetadata wraps the metadata response for a single token.
type TokenMetadata struct {
Success bool `json:"success"`
Data *TokenMeta `json:"data,omitempty"`
Message string `json:"message,omitempty"`
}
// CredToken represents a token credential (TokenEntry in the API spec).
type CredToken struct {
ID string `json:"id"`
URLPrefix string `json:"urlprefix"`
Value string `json:"value,omitempty"`
Metadata *TokenMetadata `json:"metadata,omitempty"`
}
// CredSSH represents an SSH credential (SSHCredEntry in the API spec).
type CredSSH struct {
KnownHost string `json:"known_host"`
PrivateKey string `json:"private_key,omitempty"`
}
// CredGitHubApp represents a GitHub App credential (GitHubAppEntry in the API spec).
type CredGitHubApp struct {
ID string `json:"id"`
URLPrefix string `json:"urlprefix"`
PrivateKey string `json:"privateKey,omitempty"`
}
// Credentials is the full credentials payload returned by GET /api/v1/sysconfig/credentials.
type Credentials struct {
Tokens map[string]CredToken `json:"tokens"`
SSHCreds []CredSSH `json:"sshcreds"`
GitHubApps map[string]CredGitHubApp `json:"githubApps"`
}
// addCredentialsRequest is the body for POST /api/v1/sysconfig/credentials.
type addCredentialsRequest struct {
Tokens map[string]CredToken `json:"tokens,omitempty"`
GitHubApps map[string]CredGitHubApp `json:"githubApps,omitempty"`
}
// updateCredentialsRequest is the body for PUT /api/v1/sysconfig/credentials.
// SSHCreds performs a full replacement of all SSH credentials.
type updateCredentialsRequest struct {
Tokens map[string]CredToken `json:"tokens,omitempty"`
GitHubApps map[string]CredGitHubApp `json:"githubApps,omitempty"`
SSHCreds []CredSSH `json:"sshcreds,omitempty"`
}
// deleteCredentialsRequest is the body for DELETE /api/v1/sysconfig/credentials.
type deleteCredentialsRequest struct {
Tokens []string `json:"tokens,omitempty"`
GitHubApps []string `json:"githubApps,omitempty"`
}
// credentialsPath is the API endpoint for all credential operations.
const credentialsPath = "/api/v1/sysconfig/credentials"
// fetchCredentials fetches all credentials from the API.
func fetchCredentials(server string) (*Credentials, error) {
token, err := ensureValidToken()
if err != nil {
return nil, fmt.Errorf("authentication required: %w", err)
}
req, err := http.NewRequest("GET", "https://"+server+credentialsPath, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+token.IDToken)
req.Header.Set("Accept", "application/json")
resp, err := (&http.Client{Timeout: 30 * time.Second}).Do(req)
if err != nil {
return nil, fmt.Errorf("failed to make request: %w", err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request failed (status %d): %s", resp.StatusCode, string(body))
}
var creds Credentials
if err := json.Unmarshal(body, &creds); err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}
return &creds, nil
}
// doWriteCredentials marshals payload and sends it with the given method to the credentials endpoint.
func doWriteCredentials(server, method string, payload interface{}) error {
token, err := ensureValidToken()
if err != nil {
return fmt.Errorf("authentication required: %w", err)
}
reqBody, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("failed to marshal request: %w", err)
}
req, err := http.NewRequest(method, "https://"+server+credentialsPath, bytes.NewReader(reqBody))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+token.IDToken)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := (&http.Client{Timeout: 30 * time.Second}).Do(req)
if err != nil {
return fmt.Errorf("failed to make request: %w", err)
}
defer resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("request failed (status %d): %s", resp.StatusCode, string(respBody))
}
return nil
}
// sshHostList returns the current SSH credentials with private keys stripped.
// Used as the starting point for full-replacement PUT requests.
func sshHostList(creds *Credentials) []CredSSH {
list := make([]CredSSH, len(creds.SSHCreds))
for i, s := range creds.SSHCreds {
list[i] = CredSSH{KnownHost: s.KnownHost}
}
return list
}
// toDataURL encodes raw bytes as a data URL.
func toDataURL(data []byte) string {
return "data:application/octet-stream;base64," + base64.StdEncoding.EncodeToString(data)
}
// readFileAsDataURL reads a file and returns it as a data URL (base64-encoded).
func readFileAsDataURL(path string) (string, error) {
data, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("failed to read file %s: %w", path, err)
}
return toDataURL(data), nil
}
// readJSONInput returns raw JSON from the first positional arg or from stdin.
func readJSONInput(args []string) ([]byte, error) {
if len(args) > 0 && args[0] != "-" {
return []byte(args[0]), nil
}
return io.ReadAll(os.Stdin)
}
// resolvePrivateKey returns a data-URL encoded private key from either an
// inline PEM string or a file path. Returns "" when neither is provided.
func resolvePrivateKey(privateKey, privateKeyFile string) (string, error) {
switch {
case privateKeyFile != "":
return readFileAsDataURL(privateKeyFile)
case privateKey != "":
return toDataURL([]byte(privateKey)), nil
default:
return "", nil
}
}
// AddTokenInput is the JSON schema accepted by "jh admin credential add token".
type AddTokenInput struct {
Name string `json:"name"`
URL string `json:"url"`
Value string `json:"value"`
}
// AddSSHInput is the JSON schema accepted by "jh admin credential add ssh".
type AddSSHInput struct {
HostKey string `json:"host_key"`
PrivateKey string `json:"private_key"`
PrivateKeyFile string `json:"private_key_file"`
}
// AddGitHubAppInput is the JSON schema accepted by "jh admin credential add github-app".
type AddGitHubAppInput struct {
AppID string `json:"app_id"`
URL string `json:"url"`
PrivateKey string `json:"private_key"`
PrivateKeyFile string `json:"private_key_file"`
}
// UpdateTokenInput is the JSON schema accepted by "jh admin credential update token".
type UpdateTokenInput struct {
Name string `json:"name"`
URL string `json:"url"`
Value string `json:"value"`
}
// UpdateSSHInput is the JSON schema accepted by "jh admin credential update ssh".
type UpdateSSHInput struct {
Index int `json:"index"`
HostKey string `json:"host_key"`
PrivateKey string `json:"private_key"`
PrivateKeyFile string `json:"private_key_file"`
}
// UpdateGitHubAppInput is the JSON schema accepted by "jh admin credential update github-app".
type UpdateGitHubAppInput struct {
AppID string `json:"app_id"`
URL string `json:"url"`
PrivateKey string `json:"private_key"`
PrivateKeyFile string `json:"private_key_file"`
}
func listCredentials(server string, verbose bool) error {
creds, err := fetchCredentials(server)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
// Tokens
fmt.Printf("Tokens (%d total):\n\n", len(creds.Tokens))
const indent = " "
if len(creds.Tokens) > 0 {
if verbose {
fmt.Fprintln(w, indent+"NAME\tURL\tVALUE\tACCOUNT\tEXPIRES\tSCOPES\tRATE LIMIT")
} else {
fmt.Fprintln(w, indent+"NAME\tURL\tVALUE")
}
for name, tok := range creds.Tokens {
if verbose {
account, expires, scopes, rateLimit := "", "", "", ""
if tok.Metadata != nil {
meta := tok.Metadata
if meta.Success && meta.Data != nil {
account = meta.Data.Login
expires = meta.Data.Expires
scopes = meta.Data.Scopes
resetTime := time.Unix(meta.Data.RateLimitReset, 0).In(time.Local)
rateLimit = fmt.Sprintf("%d/%d (resets %s)", meta.Data.RateLimitRemaining, meta.Data.RateLimitMax, resetTime.Format("2006-01-02 15:04:05 MST"))
} else if meta.Message != "" {
account = "error: " + meta.Message
}
}
fmt.Fprintf(w, indent+"%s\t%s\t%s\t%s\t%s\t%s\t%s\n", name, tok.URLPrefix, tok.Value, account, expires, scopes, rateLimit)
} else {
fmt.Fprintf(w, indent+"%s\t%s\t%s\n", name, tok.URLPrefix, tok.Value)
}
}
w.Flush()
}
// SSH Keys
fmt.Printf("\nSSH Keys (%d total):\n\n", len(creds.SSHCreds))
if len(creds.SSHCreds) > 0 {
if verbose {
fmt.Fprintln(w, indent+"#\tHOST KEY")
} else {
fmt.Fprintln(w, indent+"#\tHOST")
}
for i, ssh := range creds.SSHCreds {
host := ssh.KnownHost
if !verbose {
if fields := strings.Fields(host); len(fields) > 0 {
host = fields[0]
}
}
fmt.Fprintf(w, indent+"%d\t%s\n", i+1, host)
}
w.Flush()
}
// GitHub Apps
fmt.Printf("\nGitHub Apps (%d total):\n\n", len(creds.GitHubApps))
if len(creds.GitHubApps) > 0 {
fmt.Fprintln(w, indent+"APP ID\tURL")
for id, app := range creds.GitHubApps {
fmt.Fprintf(w, indent+"%s\t%s\n", id, app.URLPrefix)
}
w.Flush()
}
return nil
}
func addCredentialToken(server string, jsonData []byte) error {
var input AddTokenInput
if err := json.Unmarshal(jsonData, &input); err != nil {
return fmt.Errorf("invalid JSON: %w", err)
}
if input.Name == "" {
return fmt.Errorf("missing required field: name")
}
if input.URL == "" {
return fmt.Errorf("missing required field: url")
}
if input.Value == "" {
return fmt.Errorf("missing required field: value")
}
err := doWriteCredentials(server, "POST", &addCredentialsRequest{
Tokens: map[string]CredToken{
input.Name: {ID: input.Name, URLPrefix: input.URL, Value: input.Value},
},
})
if err != nil {
return err
}
fmt.Printf("Token %q added successfully\n", input.Name)
return nil
}
func addCredentialSSH(server string, jsonData []byte) error {
var input AddSSHInput
if err := json.Unmarshal(jsonData, &input); err != nil {
return fmt.Errorf("invalid JSON: %w", err)
}
if input.HostKey == "" {
return fmt.Errorf("missing required field: host_key")
}
if input.PrivateKey != "" && input.PrivateKeyFile != "" {
return fmt.Errorf("specify either private_key or private_key_file, not both")
}
creds, err := fetchCredentials(server)
if err != nil {
return err
}
encoded, err := resolvePrivateKey(input.PrivateKey, input.PrivateKeyFile)
if err != nil {
return err
}
newSSH := CredSSH{KnownHost: input.HostKey, PrivateKey: encoded}
err = doWriteCredentials(server, "PUT", &updateCredentialsRequest{
SSHCreds: append(sshHostList(creds), newSSH),
})
if err != nil {
return err
}
fmt.Println("SSH credential added successfully")
return nil
}
func addCredentialGitHubApp(server string, jsonData []byte) error {
var input AddGitHubAppInput
if err := json.Unmarshal(jsonData, &input); err != nil {
return fmt.Errorf("invalid JSON: %w", err)
}
if input.AppID == "" {
return fmt.Errorf("missing required field: app_id")
}
if input.URL == "" {
return fmt.Errorf("missing required field: url")
}
if input.PrivateKey != "" && input.PrivateKeyFile != "" {
return fmt.Errorf("specify either private_key or private_key_file, not both")
}
encoded, err := resolvePrivateKey(input.PrivateKey, input.PrivateKeyFile)
if err != nil {
return err
}
err = doWriteCredentials(server, "POST", &addCredentialsRequest{
GitHubApps: map[string]CredGitHubApp{
input.AppID: {ID: input.AppID, URLPrefix: input.URL, PrivateKey: encoded},
},
})
if err != nil {
return err
}
fmt.Printf("GitHub App %q added successfully\n", input.AppID)
return nil
}
func updateCredentialToken(server string, jsonData []byte) error {
var input UpdateTokenInput
if err := json.Unmarshal(jsonData, &input); err != nil {
return fmt.Errorf("invalid JSON: %w", err)
}
if input.Name == "" {
return fmt.Errorf("missing required field: name")
}
if input.URL == "" && input.Value == "" {
return fmt.Errorf("nothing to update: provide at least one of url or value")
}
creds, err := fetchCredentials(server)
if err != nil {
return err
}
existing, ok := creds.Tokens[input.Name]
if !ok {
return fmt.Errorf("token %q not found", input.Name)
}
urlPrefix := existing.URLPrefix
if input.URL != "" {
urlPrefix = input.URL
}
err = doWriteCredentials(server, "PUT", &updateCredentialsRequest{
Tokens: map[string]CredToken{
input.Name: {ID: input.Name, URLPrefix: urlPrefix, Value: input.Value},
},
})
if err != nil {
return err
}
fmt.Printf("Token %q updated successfully\n", input.Name)
return nil
}
func updateCredentialSSH(server string, jsonData []byte) error {
var input UpdateSSHInput
if err := json.Unmarshal(jsonData, &input); err != nil {
return fmt.Errorf("invalid JSON: %w", err)
}
if input.Index <= 0 {
return fmt.Errorf("missing or invalid required field: index (must be >= 1)")
}
if input.HostKey == "" && input.PrivateKey == "" && input.PrivateKeyFile == "" {
return fmt.Errorf("nothing to update: provide at least one of host_key, private_key, or private_key_file")
}
if input.PrivateKey != "" && input.PrivateKeyFile != "" {
return fmt.Errorf("specify either private_key or private_key_file, not both")
}
creds, err := fetchCredentials(server)
if err != nil {
return err
}
idx := input.Index - 1
if idx >= len(creds.SSHCreds) {
return fmt.Errorf("SSH key #%d not found (only %d exist)", input.Index, len(creds.SSHCreds))
}
encoded, err := resolvePrivateKey(input.PrivateKey, input.PrivateKeyFile)
if err != nil {
return err
}
updatedSSH := sshHostList(creds)
if input.HostKey != "" {
updatedSSH[idx].KnownHost = input.HostKey
}
if encoded != "" {
updatedSSH[idx].PrivateKey = encoded
}
err = doWriteCredentials(server, "PUT", &updateCredentialsRequest{
SSHCreds: updatedSSH,
})
if err != nil {
return err
}
fmt.Printf("SSH key #%d updated successfully\n", input.Index)
return nil
}
func updateCredentialGitHubApp(server string, jsonData []byte) error {
var input UpdateGitHubAppInput
if err := json.Unmarshal(jsonData, &input); err != nil {
return fmt.Errorf("invalid JSON: %w", err)
}
if input.AppID == "" {
return fmt.Errorf("missing required field: app_id")
}
if input.URL == "" && input.PrivateKey == "" && input.PrivateKeyFile == "" {
return fmt.Errorf("nothing to update: provide at least one of url, private_key, or private_key_file")
}
if input.PrivateKey != "" && input.PrivateKeyFile != "" {
return fmt.Errorf("specify either private_key or private_key_file, not both")
}
creds, err := fetchCredentials(server)
if err != nil {
return err
}
existing, ok := creds.GitHubApps[input.AppID]
if !ok {
return fmt.Errorf("GitHub App %q not found", input.AppID)
}
encoded, err := resolvePrivateKey(input.PrivateKey, input.PrivateKeyFile)
if err != nil {
return err
}
urlPrefix := existing.URLPrefix
if input.URL != "" {
urlPrefix = input.URL
}
err = doWriteCredentials(server, "PUT", &updateCredentialsRequest{
GitHubApps: map[string]CredGitHubApp{
input.AppID: {ID: input.AppID, URLPrefix: urlPrefix, PrivateKey: encoded},
},
})
if err != nil {
return err
}
fmt.Printf("GitHub App %q updated successfully\n", input.AppID)
return nil
}
func deleteCredentialToken(server, name string) error {
err := doWriteCredentials(server, "DELETE", &deleteCredentialsRequest{
Tokens: []string{name},
})
if err != nil {
return err
}
fmt.Printf("Token %q deleted successfully\n", name)
return nil
}
func deleteCredentialSSH(server string, index int) error {
creds, err := fetchCredentials(server)
if err != nil {
return err
}
idx := index - 1
if idx < 0 || idx >= len(creds.SSHCreds) {
return fmt.Errorf("SSH key #%d not found (only %d exist)", index, len(creds.SSHCreds))
}
hosts := sshHostList(creds)
updatedSSH := append(hosts[:idx], hosts[idx+1:]...)
err = doWriteCredentials(server, "PUT", &updateCredentialsRequest{
SSHCreds: updatedSSH,
})
if err != nil {
return err
}
fmt.Printf("SSH key #%d deleted successfully\n", index)
return nil
}
func deleteCredentialGitHubApp(server, appID string) error {
err := doWriteCredentials(server, "DELETE", &deleteCredentialsRequest{
GitHubApps: []string{appID},
})
if err != nil {
return err
}
fmt.Printf("GitHub App %q deleted successfully\n", appID)
return nil
}