-
Notifications
You must be signed in to change notification settings - Fork 70
Add Secret Store support #717
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
12 commits
Select commit
Hold shift + click to select a range
a881457
Add Secret Store support
awilliams-fastly 714ec6c
Remove extraneous comment.
awilliams-fastly 8a61c42
Re-order fields for consistency and readability
awilliams-fastly cddf27a
Consistent formatting.
awilliams-fastly 0c85bf4
Remove else block
awilliams-fastly 1883923
Add TODO
awilliams-fastly cc4e9ec
Comment exported function.
awilliams-fastly 24bd156
Change formatting and ordering for consistency with other commands
awilliams-fastly a6080b0
Use 'describe' instead of 'get' for consistency
awilliams-fastly 8e6d490
Fix error text
awilliams-fastly 57fbb1f
Consider `auto-yes` in list cmds; add RegisterFlagInt to Base
awilliams-fastly a4d5bb0
fix comment typo
awilliams-fastly 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
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
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
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 |
|---|---|---|
|
|
@@ -74,6 +74,8 @@ objectstore | |
| pops | ||
| profile | ||
| purge | ||
| secret-store | ||
| secret-store-entry | ||
| service | ||
| service-auth | ||
| service-version | ||
|
|
||
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
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,127 @@ | ||
| package secretstore | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
|
|
||
| "github.com/fastly/cli/pkg/cmd" | ||
| "github.com/fastly/cli/pkg/config" | ||
| fsterr "github.com/fastly/cli/pkg/errors" | ||
| "github.com/fastly/cli/pkg/manifest" | ||
| "github.com/fastly/cli/pkg/text" | ||
| "github.com/fastly/go-fastly/v7/fastly" | ||
| ) | ||
|
|
||
| const ( | ||
| // Maximum secret length, as defined at https://developer.fastly.com/reference/api/secret-store. | ||
| maxSecretKiB = 64 | ||
| maxSecretLen = maxSecretKiB * 1024 | ||
| ) | ||
|
|
||
| // NewCreateSecretCommand returns a usable command registered under the parent. | ||
| func NewCreateSecretCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *CreateSecretCommand { | ||
| c := CreateSecretCommand{ | ||
| Base: cmd.Base{ | ||
| Globals: globals, | ||
| }, | ||
| manifest: data, | ||
| } | ||
|
|
||
| c.CmdClause = parent.Command("create", "Create a new secret within specified store") | ||
|
|
||
| // Required. | ||
| c.RegisterFlag(secretNameFlag(&c.Input.Name)) // --name | ||
| c.RegisterFlag(storeIDFlag(&c.Input.ID)) // --store-id | ||
|
|
||
| // Optional. | ||
| c.RegisterFlag(secretFileFlag(&c.secretFile)) // --file | ||
| c.RegisterFlagBool(c.jsonFlag()) // --json | ||
| c.RegisterFlagBool(secretStdinFlag(&c.secretSTDIN)) // --stdin | ||
|
|
||
| return &c | ||
| } | ||
|
|
||
| // CreateSecretCommand calls the Fastly API to create an appropriate resource. | ||
| type CreateSecretCommand struct { | ||
| cmd.Base | ||
| jsonOutput | ||
|
|
||
| Input fastly.CreateSecretInput | ||
| manifest manifest.Data | ||
| secretFile string | ||
| secretSTDIN bool | ||
| } | ||
|
|
||
| var errMultipleSecretValue = fsterr.RemediationError{ | ||
| Inner: fmt.Errorf("invalid flag combination, --file and --stdin"), | ||
| Remediation: "Use one of --file or --stdin flag", | ||
| } | ||
|
|
||
| var errNoSTDINData = fsterr.RemediationError{ | ||
| Inner: fmt.Errorf("unable to read from STDIN"), | ||
| Remediation: "Provide data to STDIN, or use --file to read from a file", | ||
| } | ||
|
|
||
| var errMaxSecretLength = fsterr.RemediationError{ | ||
| Inner: fmt.Errorf("max secret size exceeded"), | ||
| Remediation: fmt.Sprintf("Maximum secret size is %dKiB", maxSecretKiB), | ||
| } | ||
|
|
||
| // Exec invokes the application logic for the command. | ||
| func (cmd *CreateSecretCommand) Exec(in io.Reader, out io.Writer) error { | ||
| if cmd.Globals.Verbose() && cmd.jsonOutput.enabled { | ||
| return fsterr.ErrInvalidVerboseJSONCombo | ||
| } | ||
| if cmd.secretFile != "" && cmd.secretSTDIN { | ||
| return errMultipleSecretValue | ||
| } | ||
|
|
||
| // Read secret's value: either from STDIN, a file, or prompt. | ||
| switch { | ||
| case cmd.secretSTDIN: | ||
| // Determine if 'in' has data available. | ||
| if in == nil || text.IsTTY(in) { | ||
| return errNoSTDINData | ||
| } | ||
| var buf bytes.Buffer | ||
| if _, err := buf.ReadFrom(in); err != nil { | ||
| return err | ||
| } | ||
| cmd.Input.Secret = buf.Bytes() | ||
|
|
||
| case cmd.secretFile != "": | ||
| var err error | ||
| if cmd.Input.Secret, err = os.ReadFile(cmd.secretFile); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| default: | ||
| secret, err := text.InputSecure(out, "Secret: ", in) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| cmd.Input.Secret = []byte(secret) | ||
| } | ||
|
|
||
| if len(cmd.Input.Secret) > maxSecretLen { | ||
| return errMaxSecretLength | ||
| } | ||
|
|
||
| o, err := cmd.Globals.APIClient.CreateSecret(&cmd.Input) | ||
| if err != nil { | ||
| cmd.Globals.ErrLog.Add(err) | ||
| return err | ||
| } | ||
|
|
||
| // TODO: Use this approach across the code base. | ||
| if ok, err := cmd.WriteJSON(out, o); ok { | ||
| return err | ||
| } | ||
|
|
||
| text.Success(out, "Created secret %s in store %s (digest %s)", o.Name, cmd.Input.ID, hex.EncodeToString(o.Digest)) | ||
|
|
||
| return nil | ||
| } | ||
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,62 @@ | ||
| package secretstore | ||
|
|
||
| import ( | ||
| "io" | ||
|
|
||
| "github.com/fastly/cli/pkg/cmd" | ||
| "github.com/fastly/cli/pkg/config" | ||
| fsterr "github.com/fastly/cli/pkg/errors" | ||
| "github.com/fastly/cli/pkg/manifest" | ||
| "github.com/fastly/cli/pkg/text" | ||
| "github.com/fastly/go-fastly/v7/fastly" | ||
| ) | ||
|
|
||
| // NewCreateStoreCommand returns a usable command registered under the parent. | ||
| func NewCreateStoreCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *CreateStoreCommand { | ||
| c := CreateStoreCommand{ | ||
| Base: cmd.Base{ | ||
| Globals: globals, | ||
| }, | ||
| manifest: data, | ||
| } | ||
|
|
||
| c.CmdClause = parent.Command("create", "Create a new secret store") | ||
|
|
||
| // Required. | ||
| c.RegisterFlag(storeNameFlag(&c.Input.Name)) // --name | ||
|
|
||
| // Optional. | ||
| c.RegisterFlagBool(c.jsonFlag()) // --json | ||
|
|
||
| return &c | ||
| } | ||
|
|
||
| // CreateStoreCommand calls the Fastly API to create an appropriate resource. | ||
| type CreateStoreCommand struct { | ||
| cmd.Base | ||
| jsonOutput | ||
|
|
||
| Input fastly.CreateSecretStoreInput | ||
| manifest manifest.Data | ||
| } | ||
|
|
||
| // Exec invokes the application logic for the command. | ||
| func (cmd *CreateStoreCommand) Exec(_ io.Reader, out io.Writer) error { | ||
| if cmd.Globals.Verbose() && cmd.jsonOutput.enabled { | ||
| return fsterr.ErrInvalidVerboseJSONCombo | ||
| } | ||
|
|
||
| o, err := cmd.Globals.APIClient.CreateSecretStore(&cmd.Input) | ||
| if err != nil { | ||
| cmd.Globals.ErrLog.Add(err) | ||
| return err | ||
| } | ||
|
|
||
| if ok, err := cmd.WriteJSON(out, o); ok { | ||
| return err | ||
| } | ||
|
|
||
| text.Success(out, "Created secret store %s (name %s)", o.ID, o.Name) | ||
|
|
||
| return nil | ||
| } |
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,73 @@ | ||
| package secretstore | ||
|
|
||
| import ( | ||
| "io" | ||
|
|
||
| "github.com/fastly/cli/pkg/cmd" | ||
| "github.com/fastly/cli/pkg/config" | ||
| fsterr "github.com/fastly/cli/pkg/errors" | ||
| "github.com/fastly/cli/pkg/manifest" | ||
| "github.com/fastly/cli/pkg/text" | ||
| "github.com/fastly/go-fastly/v7/fastly" | ||
| ) | ||
|
|
||
| // NewDeleteSecretCommand returns a usable command registered under the parent. | ||
| func NewDeleteSecretCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DeleteSecretCommand { | ||
| c := DeleteSecretCommand{ | ||
| Base: cmd.Base{ | ||
| Globals: globals, | ||
| }, | ||
| manifest: data, | ||
| } | ||
|
|
||
| c.CmdClause = parent.Command("delete", "Delete a secret") | ||
|
|
||
| // Required. | ||
| c.RegisterFlag(secretNameFlag(&c.Input.Name)) // --name | ||
| c.RegisterFlag(storeIDFlag(&c.Input.ID)) // --store-id | ||
|
|
||
| // Optional. | ||
| c.RegisterFlagBool(c.jsonFlag()) // --json | ||
|
|
||
| return &c | ||
| } | ||
|
|
||
| // DeleteSecretCommand calls the Fastly API to delete an appropriate resource. | ||
| type DeleteSecretCommand struct { | ||
| cmd.Base | ||
| jsonOutput | ||
|
|
||
| Input fastly.DeleteSecretInput | ||
| manifest manifest.Data | ||
| } | ||
|
|
||
| // Exec invokes the application logic for the command. | ||
| func (cmd *DeleteSecretCommand) Exec(_ io.Reader, out io.Writer) error { | ||
| if cmd.Globals.Verbose() && cmd.jsonOutput.enabled { | ||
| return fsterr.ErrInvalidVerboseJSONCombo | ||
| } | ||
|
|
||
| err := cmd.Globals.APIClient.DeleteSecret(&cmd.Input) | ||
| if err != nil { | ||
| cmd.Globals.ErrLog.Add(err) | ||
| return err | ||
| } | ||
|
|
||
| if cmd.jsonOutput.enabled { | ||
| o := struct { | ||
| Name string `json:"name"` | ||
| ID string `json:"store_id"` | ||
| Deleted bool `json:"deleted"` | ||
| }{ | ||
| cmd.Input.Name, | ||
| cmd.Input.ID, | ||
| true, | ||
| } | ||
| _, err := cmd.WriteJSON(out, o) | ||
| return err | ||
| } | ||
|
|
||
| text.Success(out, "Deleted secret %s from store %s", cmd.Input.Name, cmd.Input.ID) | ||
|
|
||
| return nil | ||
| } |
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.