feat: implement secret ls

Closes coop-cloud/go-abra#54.
This commit is contained in:
decentral1se 2021-08-30 17:02:08 +02:00
parent 89b5f12fb1
commit 15651822f1
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
2 changed files with 104 additions and 11 deletions

View File

@ -30,7 +30,7 @@
- [ ] `generate` (WIP: decentral1se)
- [ ] `insert` (WIP: decentral1se)
- [ ] `rm` (WIP: decentral1se)
- [ ] `ls` (WIP: decentral1se)
- [x] `ls`
- [x] `undeploy`
- [ ] `volume`
- [ ] `ls` (WIP: knoflook)

View File

@ -1,10 +1,18 @@
package app
import (
"context"
"errors"
"fmt"
"strconv"
abraFormatter "coopcloud.tech/abra/cli/formatter"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/client"
"coopcloud.tech/abra/config"
"coopcloud.tech/abra/secret"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
@ -17,17 +25,102 @@ var allSecretsFlag = &cli.BoolFlag{
Usage: "Generate all secrets",
}
// TODO: Replicating what the bash abra does might be hard
// with the mix of subcommands and flags
var appSecretCommand = &cli.Command{
Name: "secret",
Flags: []cli.Flag{allSecretsFlag, internal.PassFlag},
var appSecretGenerateCommand = &cli.Command{
Name: "generate",
Usage: "Generate secrets",
Action: func(c *cli.Context) error {
password, err := secret.GeneratePassphrases(1)
if err != nil {
logrus.Fatal(err)
}
fmt.Println(password)
return nil
},
}
var appSecretInsertCommand = &cli.Command{
Name: "insert",
Usage: "Insert secret",
Action: func(c *cli.Context) error {
return nil
},
}
var appSecretRmCommand = &cli.Command{
Name: "remove",
Usage: "Remove a secret",
Aliases: []string{"rm"},
Action: func(c *cli.Context) error {
return nil
},
}
var appSecretLsCommand = &cli.Command{
Name: "list",
Usage: "List all secrets",
Aliases: []string{"ls"},
Action: func(c *cli.Context) error {
appName := c.Args().First()
if appName == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided"))
}
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
secrets := secret.ReadSecretEnvVars(appEnv.Env)
tableCol := []string{"Name", "Version", "Generated Name", "Created On Server"}
table := abraFormatter.CreateTable(tableCol)
host := appFiles[appName].Server
ctx := context.Background()
cl, err := client.NewClientWithContext(host)
if err != nil {
logrus.Fatal(err)
}
filters := filters.NewArgs()
filters.Add("name", appEnv.StackName())
secretList, err := cl.SecretList(ctx, types.SecretListOptions{Filters: filters})
if err != nil {
logrus.Fatal(err)
}
remoteSecretNames := make(map[string]bool)
for _, cont := range secretList {
remoteSecretNames[cont.Spec.Annotations.Name] = true
}
for sec := range secrets {
createdRemote := false
secretName := secret.ParseSecretEnvVarName(sec)
secVal, err := secret.ParseSecretEnvVarValue(secrets[sec])
if err != nil {
logrus.Fatal(err)
}
secretRemoteName := fmt.Sprintf("%s_%s_%s", appEnv.StackName(), secretName, secVal.Version)
if _, ok := remoteSecretNames[secretRemoteName]; ok {
createdRemote = true
}
tableRow := []string{secretName, secVal.Version, secretRemoteName, strconv.FormatBool(createdRemote)}
table.Append(tableRow)
}
table.Render()
return nil
},
}
var appSecretCommand = &cli.Command{
Name: "secret",
Usage: "Manage app secrets",
ArgsUsage: "<command>",
Subcommands: []*cli.Command{
appSecretGenerateCommand,
appSecretInsertCommand,
appSecretRmCommand,
appSecretLsCommand,
},
}