-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add service-account api-keys commands #938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4aa6ce0
feat: add service-account api-keys commands
mbevc1 90c8f8e
fix: enforce new keys feature and address feedback
mbevc1 bf048c5
fix: be moreo verbose about partial revocation failures
mbevc1 e83ac79
fix: remove erorr colouring, use logger consistently and remove hard-…
mbevc1 151b470
chore: remove unnecessary comments
mbevc1 98ee4a1
chore: print which keys were revoked
mbevc1 19b4e79
chore: consistent erorr handling when rotating keys
mbevc1 a919e0c
feat: refactor to verb-first structure
mbevc1 0b107f2
refactor: remove pre-restructure api-key files superseded by verb-fir…
mbevc1 fe5bdeb
refactor: switch to rotate verb
mbevc1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "strconv" | ||
| "time" | ||
| ) | ||
|
|
||
| // apiKeyResponse models the JSON returned by the create and rotate endpoints. | ||
| // The key value is only ever returned once, at creation/rotation time. | ||
| type apiKeyResponse struct { | ||
| Id string `json:"id"` | ||
| Key string `json:"key"` | ||
| Description string `json:"description"` | ||
| CreatedAt float64 `json:"created_at"` | ||
| ExpiresAt float64 `json:"expires_at"` | ||
| GracePeriodExpiresAt float64 `json:"grace_period_expires_at,omitempty"` | ||
| } | ||
|
|
||
| // apiKeyMetadata models a single entry returned by the list endpoint. The list | ||
| // endpoint returns metadata only — the secret key value is never included. | ||
| type apiKeyMetadata struct { | ||
| Id string `json:"id"` | ||
| Description string `json:"description"` | ||
| CreatedAt float64 `json:"created_at"` | ||
| ExpiresAt float64 `json:"expires_at"` | ||
| LastUsedAt float64 `json:"last_used_at"` | ||
| } | ||
|
|
||
| // parseExpiresAt converts a user-supplied --expires-at value into a Unix | ||
| // (epoch-second) timestamp. It accepts a bare epoch integer, or one of the | ||
| // date/time layouts below (interpreted as UTC). An empty string returns 0. | ||
| func parseExpiresAt(value string) (int64, error) { | ||
| if value == "" { | ||
| return 0, nil | ||
| } | ||
|
|
||
| if epoch, err := strconv.ParseInt(value, 10, 64); err == nil { | ||
| return epoch, nil | ||
| } | ||
|
|
||
| formats := []string{ | ||
| "2006-1-2", | ||
| "2006-1-2 15:04:05", | ||
| time.RFC3339, | ||
| } | ||
| for _, format := range formats { | ||
| if t, err := time.Parse(format, value); err == nil { | ||
| return t.UTC().Unix(), nil | ||
| } | ||
| } | ||
|
|
||
| return 0, fmt.Errorf("invalid --expires-at value %q: expected an epoch timestamp or a date like '2006-01-02', '2006-01-02 15:04:05', or an RFC3339 timestamp", value) | ||
| } | ||
|
|
||
| // printApiKeyAsTable renders a single api key (the create response) as a table. | ||
| func printApiKeyAsTable(raw string, out io.Writer, page int) error { | ||
| var key apiKeyResponse | ||
| if err := json.Unmarshal([]byte(raw), &key); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| rows, err := apiKeyTableRows(key) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| tabFormattedPrint(out, []string{}, rows) | ||
| return nil | ||
| } | ||
|
|
||
| // printApiKeysAsTable renders one or more api keys (the rotate response) as | ||
| // table blocks separated by a blank line. | ||
| func printApiKeysAsTable(raw string, out io.Writer, page int) error { | ||
| var keys []apiKeyResponse | ||
| if err := json.Unmarshal([]byte(raw), &keys); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for i, key := range keys { | ||
| if i > 0 { | ||
| if _, err := fmt.Fprintln(out); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| rows, err := apiKeyTableRows(key) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| tabFormattedPrint(out, []string{}, rows) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // optionalTimestamp formats an epoch timestamp, returning "N/A" when it is | ||
| // unset (nil, or a zero value meaning "never"/"not set"). | ||
| func optionalTimestamp(epoch interface{}) (string, error) { | ||
| switch v := epoch.(type) { | ||
| case nil: | ||
| return "N/A", nil | ||
| case float64: | ||
| if v == 0 { | ||
| return "N/A", nil | ||
| } | ||
| case int64: | ||
| if v == 0 { | ||
| return "N/A", nil | ||
| } | ||
| } | ||
| return formattedTimestamp(epoch, false) | ||
| } | ||
|
|
||
| // apiKeyTableRows builds the key:value rows describing a single api key. | ||
| func apiKeyTableRows(key apiKeyResponse) ([]string, error) { | ||
| createdAt, err := formattedTimestamp(key.CreatedAt, false) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| expiresAt, err := optionalTimestamp(key.ExpiresAt) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| rows := []string{} | ||
| rows = append(rows, fmt.Sprintf("ID:\t%s", key.Id)) | ||
| rows = append(rows, fmt.Sprintf("Key:\t%s", key.Key)) | ||
| rows = append(rows, fmt.Sprintf("Description:\t%s", key.Description)) | ||
| rows = append(rows, fmt.Sprintf("Created At:\t%s", createdAt)) | ||
| rows = append(rows, fmt.Sprintf("Expires At:\t%s", expiresAt)) | ||
| if key.GracePeriodExpiresAt != 0 { | ||
| gracePeriodExpiresAt, err := formattedTimestamp(key.GracePeriodExpiresAt, false) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| rows = append(rows, fmt.Sprintf("Old Key Valid Until:\t%s", gracePeriodExpiresAt)) | ||
| } | ||
| return rows, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.