Skip to content

Commit

Permalink
Merge pull request containers#12586 from jmguzik/secret-cmd
Browse files Browse the repository at this point in the history
Add secret list --filter to cli
  • Loading branch information
openshift-merge-robot authored Dec 14, 2021
2 parents 1884bd8 + 50501f4 commit a0894b5
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 11 deletions.
21 changes: 18 additions & 3 deletions cmd/podman/common/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func getImages(cmd *cobra.Command, toComplete string) ([]string, cobra.ShellComp
return suggestions, cobra.ShellCompDirectiveNoFileComp
}

func getSecrets(cmd *cobra.Command, toComplete string) ([]string, cobra.ShellCompDirective) {
func getSecrets(cmd *cobra.Command, toComplete string, cType completeType) ([]string, cobra.ShellCompDirective) {
suggestions := []string{}

engine, err := setupContainerEngine(cmd)
Expand All @@ -224,7 +224,13 @@ func getSecrets(cmd *cobra.Command, toComplete string) ([]string, cobra.ShellCom
}

for _, s := range secrets {
if strings.HasPrefix(s.Spec.Name, toComplete) {
// works the same as in getNetworks
if ((len(toComplete) > 1 && cType == completeDefault) ||
cType == completeIDs) && strings.HasPrefix(s.ID, toComplete) {
suggestions = append(suggestions, s.ID[0:12])
}
// include name in suggestions
if cType != completeIDs && strings.HasPrefix(s.Spec.Name, toComplete) {
suggestions = append(suggestions, s.Spec.Name)
}
}
Expand Down Expand Up @@ -470,7 +476,7 @@ func AutocompleteSecrets(cmd *cobra.Command, args []string, toComplete string) (
if !validCurrentCmdLine(cmd, args, toComplete) {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return getSecrets(cmd, toComplete)
return getSecrets(cmd, toComplete, completeDefault)
}

func AutocompleteSecretCreate(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down Expand Up @@ -1283,6 +1289,15 @@ func AutocompleteVolumeFilters(cmd *cobra.Command, args []string, toComplete str
return completeKeyValues(toComplete, kv)
}

// AutocompleteSecretFilters - Autocomplete secret ls --filter options.
func AutocompleteSecretFilters(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
kv := keyValueCompletion{
"name=": func(s string) ([]string, cobra.ShellCompDirective) { return getSecrets(cmd, s, completeNames) },
"id=": func(s string) ([]string, cobra.ShellCompDirective) { return getSecrets(cmd, s, completeIDs) },
}
return completeKeyValues(toComplete, kv)
}

// AutocompleteCheckpointCompressType - Autocomplete checkpoint compress type options.
// -> "gzip", "none", "zstd"
func AutocompleteCheckpointCompressType(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
16 changes: 15 additions & 1 deletion cmd/podman/secrets/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/containers/common/pkg/completion"
"github.com/containers/common/pkg/report"
"github.com/containers/podman/v3/cmd/podman/common"
"github.com/containers/podman/v3/cmd/podman/parse"
"github.com/containers/podman/v3/cmd/podman/registry"
"github.com/containers/podman/v3/cmd/podman/validate"
"github.com/containers/podman/v3/pkg/domain/entities"
Expand All @@ -32,6 +33,7 @@ var (
type listFlagType struct {
format string
noHeading bool
filter []string
}

func init() {
Expand All @@ -44,14 +46,26 @@ func init() {
formatFlagName := "format"
flags.StringVar(&listFlag.format, formatFlagName, "{{.ID}}\t{{.Name}}\t{{.Driver}}\t{{.CreatedAt}}\t{{.UpdatedAt}}\t\n", "Format volume output using Go template")
_ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(entities.SecretInfoReport{}))
filterFlagName := "filter"
flags.StringSliceVarP(&listFlag.filter, filterFlagName, "f", []string{}, "Filter secret output")
_ = lsCmd.RegisterFlagCompletionFunc(filterFlagName, common.AutocompleteSecretFilters)
flags.BoolVar(&listFlag.noHeading, "noheading", false, "Do not print headers")
}

func ls(cmd *cobra.Command, args []string) error {
responses, err := registry.ContainerEngine().SecretList(context.Background(), entities.SecretListRequest{})
var err error
lsOpts := entities.SecretListRequest{}

lsOpts.Filters, err = parse.FilterArgumentsIntoFilters(listFlag.filter)
if err != nil {
return err
}

responses, err := registry.ContainerEngine().SecretList(context.Background(), lsOpts)
if err != nil {
return err
}

listed := make([]*entities.SecretListReport, 0, len(responses))
for _, response := range responses {
listed = append(listed, &entities.SecretListReport{
Expand Down
13 changes: 6 additions & 7 deletions cmd/podman/volumes/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"fmt"
"os"
"strings"

"github.com/containers/common/pkg/completion"
"github.com/containers/common/pkg/report"
"github.com/containers/podman/v3/cmd/podman/common"
"github.com/containers/podman/v3/cmd/podman/parse"
"github.com/containers/podman/v3/cmd/podman/registry"
"github.com/containers/podman/v3/cmd/podman/validate"
"github.com/containers/podman/v3/libpod/define"
Expand Down Expand Up @@ -64,19 +64,18 @@ func init() {
}

func list(cmd *cobra.Command, args []string) error {
var err error
if cliOpts.Quiet && cmd.Flag("format").Changed {
return errors.New("quiet and format flags cannot be used together")
}
if len(cliOpts.Filter) > 0 {
lsOpts.Filter = make(map[string][]string)
}
for _, f := range cliOpts.Filter {
filterSplit := strings.SplitN(f, "=", 2)
if len(filterSplit) < 2 {
return errors.Errorf("filter input must be in the form of filter=value: %s is invalid", f)
}
lsOpts.Filter[filterSplit[0]] = append(lsOpts.Filter[filterSplit[0]], filterSplit[1])
lsOpts.Filter, err = parse.FilterArgumentsIntoFilters(cliOpts.Filter)
if err != nil {
return err
}

responses, err := registry.ContainerEngine().VolumeList(context.Background(), lsOpts)
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions cmd/podman/volumes/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func prune(cmd *cobra.Command, args []string) error {
return err
}
pruneOptions.Filters, err = parse.FilterArgumentsIntoFilters(filter)
if err != nil {
return err
}
if !force {
reader := bufio.NewReader(os.Stdin)
fmt.Println("WARNING! This will remove all volumes not used by at least one container. The following volumes will be removed:")
Expand Down
13 changes: 13 additions & 0 deletions docs/source/markdown/podman-secret-ls.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,24 @@ Format secret output using Go template.

Omit the table headings from the listing of secrets. .

#### **--filter**, **-f**=*filter=value*

Filter output based on conditions given.
Multiple filters can be given with multiple uses of the --filter option.

Valid filters are listed below:

| **Filter** | **Description** |
| ---------- | ----------------------------------------------------------------- |
| name | [Name] Secret name (accepts regex) |
| id | [ID] Full or partial secret ID |

## EXAMPLES

```
$ podman secret ls
$ podman secret ls --format "{{.Name}}"
$ podman secret ls --filter name=confidential
```

## SEE ALSO
Expand Down
49 changes: 49 additions & 0 deletions test/e2e/secret_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package integration

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -145,6 +146,54 @@ var _ = Describe("Podman secret", func() {

})

It("podman secret ls with filters", func() {
secretFilePath := filepath.Join(podmanTest.TempDir, "secret")
err := ioutil.WriteFile(secretFilePath, []byte("mysecret"), 0755)
Expect(err).To(BeNil())

secret1 := "Secret1"
secret2 := "Secret2"

session := podmanTest.Podman([]string{"secret", "create", secret1, secretFilePath})
session.WaitWithDefaultTimeout()
secrID1 := session.OutputToString()
Expect(session).Should(Exit(0))

session = podmanTest.Podman([]string{"secret", "create", secret2, secretFilePath})
session.WaitWithDefaultTimeout()
secrID2 := session.OutputToString()
Expect(session).Should(Exit(0))

session = podmanTest.Podman([]string{"secret", "create", "Secret3", secretFilePath})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

list := podmanTest.Podman([]string{"secret", "ls", "--filter", fmt.Sprintf("name=%s", secret1)})
list.WaitWithDefaultTimeout()
Expect(list).Should(Exit(0))
Expect(list.OutputToStringArray()).To(HaveLen(2), ContainSubstring(secret1))

list = podmanTest.Podman([]string{"secret", "ls", "--filter", fmt.Sprintf("name=%s", secret2)})
list.WaitWithDefaultTimeout()
Expect(list).Should(Exit(0))
Expect(list.OutputToStringArray()).To(HaveLen(2), ContainSubstring(secret2))

list = podmanTest.Podman([]string{"secret", "ls", "--filter", fmt.Sprintf("id=%s", secrID1)})
list.WaitWithDefaultTimeout()
Expect(list).Should(Exit(0))
Expect(list.OutputToStringArray()).To(HaveLen(2), ContainSubstring(secrID1))

list = podmanTest.Podman([]string{"secret", "ls", "--filter", fmt.Sprintf("id=%s", secrID2)})
list.WaitWithDefaultTimeout()
Expect(list).Should(Exit(0))
Expect(list.OutputToStringArray()).To(HaveLen(2), ContainSubstring(secrID2))

list = podmanTest.Podman([]string{"secret", "ls", "--filter", fmt.Sprintf("name=%s,name=%s", secret1, secret2)})
list.WaitWithDefaultTimeout()
Expect(list).Should(Exit(0))
Expect(list.OutputToStringArray()).To(HaveLen(3), ContainSubstring(secret1), ContainSubstring(secret2))
})

It("podman secret ls with Go template", func() {
secretFilePath := filepath.Join(podmanTest.TempDir, "secret")
err := ioutil.WriteFile(secretFilePath, []byte("mysecret"), 0755)
Expand Down

0 comments on commit a0894b5

Please sign in to comment.