Files
stack/components/fctl/cmd/webhooks/secret.go

80 lines
2.3 KiB
Go

package webhooks
import (
fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/v2/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/v2/pkg/models/shared"
"github.com/pkg/errors"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
type ChangeSecretStore struct {
Secret string `json:"secret"`
ID string `json:"id"`
}
type ChangeSecretWebhookController struct {
store *ChangeSecretStore
}
var _ fctl.Controller[*ChangeSecretStore] = (*ChangeSecretWebhookController)(nil)
func NewDefaultChangeSecretStore() *ChangeSecretStore {
return &ChangeSecretStore{
Secret: "",
ID: "",
}
}
func NewChangeSecretWebhookController() *ChangeSecretWebhookController {
return &ChangeSecretWebhookController{
store: NewDefaultChangeSecretStore(),
}
}
func (c *ChangeSecretWebhookController) GetStore() *ChangeSecretStore {
return c.store
}
func (c *ChangeSecretWebhookController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
store := fctl.GetStackStore(cmd.Context())
if !fctl.CheckStackApprobation(cmd, store.Stack(), "You are about to change a webhook secret") {
return nil, fctl.ErrMissingApproval
}
secret := ""
if len(args) > 1 {
secret = args[1]
}
response, err := store.Client().Webhooks.V1.
ChangeConfigSecret(cmd.Context(), operations.ChangeConfigSecretRequest{
ConfigChangeSecret: &shared.ConfigChangeSecret{
Secret: secret,
},
ID: args[0],
})
if err != nil {
return nil, errors.Wrap(err, "changing secret")
}
c.store.ID = response.ConfigResponse.Data.ID
c.store.Secret = response.ConfigResponse.Data.Secret
return c, nil
}
func (c *ChangeSecretWebhookController) Render(cmd *cobra.Command, args []string) error {
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln(
"Config '%s' updated successfully with new secret", c.store.ID)
return nil
}
func NewChangeSecretCommand() *cobra.Command {
return fctl.NewCommand("change-secret <config-id> <secret>",
fctl.WithShortDescription("Change the signing secret of a config. You can bring your own secret. If not passed or empty, a secret is automatically generated. The format is a string of bytes of size 24, base64 encoded. (larger size after encoding)"),
fctl.WithConfirmFlag(),
fctl.WithAliases("cs"),
fctl.WithArgs(cobra.RangeArgs(1, 2)),
fctl.WithController[*ChangeSecretStore](NewChangeSecretWebhookController()),
)
}