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

Added secret check cli command #1017

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
74 changes: 74 additions & 0 deletions cli/cmd/encore/secrets/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package secrets

import (
"context"
"encr.dev/cli/cmd/encore/cmdutil"
"encr.dev/cli/internal/platform"
"fmt"
"github.com/spf13/cobra"
"time"
)

var checkSecretsCmd = &cobra.Command{
Use: "check [envTypes...]",
Short: "Check missing secrets for specified environment types (all types by default)",
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
appSlug := cmdutil.AppSlug()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

s := secretEnvSelector{
envTypes: args,
}

envTypes := s.ParseEnvTypes()

if len(envTypes) == 0 {
envTypes = allEnvTypes
}

secrets, err := platform.ListSecretGroups(ctx, appSlug)
if err != nil {
cmdutil.Fatal(err)
}

printSecretsOverview(envTypes, envTypeLabels, secrets, nil)

missing := make(map[string]bool)

for _, s := range secrets {
d := getSecretEnvDesc(s.Groups)
if !d.hasAny {
continue
}

for _, t := range envTypes {
if t == "production" && !d.prod {
missing[s.Key] = true
}

if t == "development" && !d.dev {
missing[s.Key] = true
}

if t == "local" && !d.local {
missing[s.Key] = true
}

if t == "preview" && !d.preview {
missing[s.Key] = true
}
}
}

if len(missing) > 0 {
fmt.Println()
cmdutil.Fatalf("%d secret(s) don't have matching values in all required environment types", len(missing))
}
},
}

func init() {
secretCmd.AddCommand(checkSecretsCmd)
}
73 changes: 4 additions & 69 deletions cli/cmd/encore/secrets/list.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package secrets

import (
"bytes"
"cmp"
"context"
"fmt"
Expand Down Expand Up @@ -32,52 +31,22 @@ var listSecretCmd = &cobra.Command{
if len(args) > 0 {
keys = args
}
secrets, err := platform.ListSecretGroups(ctx, appSlug, keys)
secrets, err := platform.ListSecretGroups(ctx, appSlug, keys...)
if err != nil {
cmdutil.Fatal(err)
}

if keys == nil {
// Print secrets overview
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 0, 3, ' ', tabwriter.StripEscape)
labels := append(envTypeLabels, "Specific Envs")

_, _ = fmt.Fprint(w, "Secret Key\tProduction\tDevelopment\tLocal\tPreview\tSpecific Envs\t\n")
const (
checkYes = "\u2713"
checkNo = "\u2717"
)
for _, s := range secrets {
render := func(b bool) string {
if b {
return checkYes
} else {
return checkNo
}
}
d := getSecretEnvDesc(s.Groups)
if !d.hasAny {
continue
}
_, _ = fmt.Fprintf(w, "%s\t%v\t%v\t%v\t%v\t", s.Key,
render(d.prod), render(d.dev), render(d.local), render(d.preview))
// Render specific envs, if any
printSecretsOverview(allEnvTypes, labels, secrets, func(w *tabwriter.Writer, d secretEnvDesc) {
for i, env := range d.specific {
if i > 0 {
_, _ = fmt.Fprintf(w, ",")
}
_, _ = fmt.Fprintf(w, "%s", env.Name)
}

_, _ = fmt.Fprint(w, "\t\n")
}
_ = w.Flush()

// Add color to the checkmarks now that the table is correctly laid out.
// We can't do it before since the tabwriter will get the alignment wrong
// if we include a bunch of ANSI escape codes that it doesn't understand.
r := strings.NewReplacer(checkYes, color.GreenString(checkYes), checkNo, color.RedString(checkNo))
_, _ = r.WriteString(os.Stdout, buf.String())
})
} else {
// Specific secrets
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
Expand Down Expand Up @@ -133,37 +102,3 @@ var listSecretCmd = &cobra.Command{
func init() {
secretCmd.AddCommand(listSecretCmd)
}

type secretEnvDesc struct {
hasAny bool // if there are any non-archived groups at all
prod, dev, local, preview bool
specific []*gql.Env
}

func getSecretEnvDesc(groups []*gql.SecretGroup) secretEnvDesc {
var desc secretEnvDesc
for _, g := range groups {
if g.ArchivedAt != nil {
continue
}
desc.hasAny = true
for _, sel := range g.Selector {
switch sel := sel.(type) {
case *gql.SecretSelectorEnvType:
switch sel.Kind {
case "production":
desc.prod = true
case "development":
desc.dev = true
case "local":
desc.local = true
case "preview":
desc.preview = true
}
case *gql.SecretSelectorSpecificEnv:
desc.specific = append(desc.specific, sel.Env)
}
}
}
return desc
}
Loading