Skip to content
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

IZE-696 added explain for ize secrets pull|push|rm #534

Merged
merged 1 commit into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions internal/commands/secrets_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"strings"
"text/template"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssm"
Expand All @@ -14,13 +15,23 @@ import (
"github.com/spf13/cobra"
)

var explainSecretsPullTmpl = `
aws ssm get-parameters-by-path \
--path "/{{.Env}}/{{svc}}" \
--with-decryption \
--recursive \
--parameter-filters "Key=Type,Values=SecureString" \
--output json | jq '.Parameters | [.[] | {(.Name|capture(".*/(?<a>.*)").a): .Value}]|reduce .[] as $item ({}; . + $item)' > {{.EnvDir}}/secrets/{{svc}}.json
`

type SecretsPullOptions struct {
Config *config.Project
AppName string
Backend string
FilePath string
SecretsPath string
Force bool
Explain bool
}

func NewSecretsPullFlags(project *config.Project) *SecretsPullOptions {
Expand Down Expand Up @@ -63,6 +74,7 @@ func NewCmdSecretsPull(project *config.Project) *cobra.Command {
cmd.Flags().StringVar(&o.Backend, "backend", "ssm", "backend type (default=ssm)")
cmd.Flags().StringVar(&o.FilePath, "file", "", "file with secrets")
cmd.Flags().StringVar(&o.SecretsPath, "path", "", "path where to store secrets (/<env>/<app> by default)")
cmd.Flags().BoolVar(&o.Explain, "explain", false, "bash alternative shown")
cmd.Flags().BoolVar(&o.Force, "force", false, "allow values overwrite")

return cmd
Expand Down Expand Up @@ -91,6 +103,19 @@ func (o *SecretsPullOptions) Validate() error {
}

func (o *SecretsPullOptions) Run() error {
if o.Explain {
err := o.Config.Generate(explainSecretsPullTmpl, template.FuncMap{
"svc": func() string {
return o.AppName
},
})
if err != nil {
return err
}

return nil
}

s, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Pulling secrets for %s...", o.AppName))
if o.Backend == "ssm" {
err := o.pull(s)
Expand Down
26 changes: 26 additions & 0 deletions internal/commands/secrets_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"text/template"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand All @@ -23,8 +24,19 @@ type SecretsPushOptions struct {
FilePath string
SecretsPath string
Force bool
Explain bool
}

var explainSecretsPushTmpl = `
SERVICE_SECRETS_FILE={{.EnvDir}}/secrets/{{svc}}.json
SERVICE_SECRETS=$(cat $SERVICE_SECRETS_FILE | jq -e -r '. | keys[]')
for item in $(echo $SERVICE_SECRETS); do
aws --profile={{.AwsProfile}} ssm put-parameter --name="/{{.Env}}/{{svc}}/${item}" --value="$(cat $SERVICE_SECRETS_FILE | jq -r .$item )" --type SecureString --overwrite && \
aws --profile={{.AwsProfile}} ssm add-tags-to-resource --resource-type "Parameter" --resource-id "/{{.Env}}/{{svc}}/${item}" \
--tags "Key=Application,Value={{svc}}" "Key=EnvVarName,Value=${item}"
done
`

var secretsPushExample = templates.Examples(`
# Push secrets:

Expand Down Expand Up @@ -76,6 +88,7 @@ func NewCmdSecretsPush(project *config.Project) *cobra.Command {
cmd.Flags().StringVar(&o.Backend, "backend", "ssm", "backend type (default=ssm)")
cmd.Flags().StringVar(&o.FilePath, "file", "", "file with secrets")
cmd.Flags().StringVar(&o.SecretsPath, "path", "", "path where to store secrets (/<env>/<app> by default)")
cmd.Flags().BoolVar(&o.Explain, "explain", false, "bash alternative shown")
cmd.Flags().BoolVar(&o.Force, "force", false, "allow values overwrite")

return cmd
Expand Down Expand Up @@ -104,6 +117,19 @@ func (o *SecretsPushOptions) Validate() error {
}

func (o *SecretsPushOptions) Run() error {
if o.Explain {
err := o.Config.Generate(explainSecretsPushTmpl, template.FuncMap{
"svc": func() string {
return o.AppName
},
})
if err != nil {
return err
}

return nil
}

s, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Pushing secrets for %s...", o.AppName))
if o.Backend == "ssm" {
err := o.push(s)
Expand Down
24 changes: 24 additions & 0 deletions internal/commands/secrets_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"context"
"fmt"
"text/template"
"time"

"github.com/aws/aws-sdk-go/service/ssm"
Expand All @@ -19,8 +20,17 @@ type SecretsRemoveOptions struct {
Backend string
SecretsPath string
ui terminal.UI
Explain bool
}

var explainSecretsRmTmpl = `
aws ssm delete-parameters --names $(aws ssm get-parameters-by-path \
--path "/{{.Env}}/{{svc}}" \
--with-decryption \
--recursive \
--query "Parameters[*].Name" | jq -e -r '. | to_entries[] | .value')
`

var secretsRemoveExample = templates.Examples(`
# Remove secrets:

Expand Down Expand Up @@ -61,6 +71,7 @@ func NewCmdSecretsRemove(project *config.Project) *cobra.Command {
}

cmd.Flags().StringVar(&o.Backend, "backend", "ssm", "backend type")
cmd.Flags().BoolVar(&o.Explain, "explain", false, "bash alternative shown")
cmd.Flags().StringVar(&o.SecretsPath, "path", "", "path to secrets")

return cmd
Expand All @@ -87,6 +98,19 @@ func (o *SecretsRemoveOptions) Validate() error {
}

func (o *SecretsRemoveOptions) Run() error {
if o.Explain {
err := o.Config.Generate(explainSecretsRmTmpl, template.FuncMap{
"svc": func() string {
return o.AppName
},
})
if err != nil {
return err
}

return nil
}

s, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Removing secrets for %s...", o.AppName))
if o.Backend == "ssm" {
err := o.rm(s)
Expand Down