mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #28716 from ehazlett/fix-multi-secret-inspect
Update secret inspect to support IDs
This commit is contained in:
commit
7c6f001248
4 changed files with 107 additions and 40 deletions
|
@ -9,18 +9,18 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type inspectOptions struct {
|
type inspectOptions struct {
|
||||||
name string
|
names []string
|
||||||
format string
|
format string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSecretInspectCommand(dockerCli *command.DockerCli) *cobra.Command {
|
func newSecretInspectCommand(dockerCli *command.DockerCli) *cobra.Command {
|
||||||
opts := inspectOptions{}
|
opts := inspectOptions{}
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "inspect [name]",
|
Use: "inspect SECRET [SECRET]",
|
||||||
Short: "Inspect a secret",
|
Short: "Inspect a secret",
|
||||||
Args: cli.ExactArgs(1),
|
Args: cli.RequiresMinArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
opts.name = args[0]
|
opts.names = args
|
||||||
return runSecretInspect(dockerCli, opts)
|
return runSecretInspect(dockerCli, opts)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -33,23 +33,13 @@ func runSecretInspect(dockerCli *command.DockerCli, opts inspectOptions) error {
|
||||||
client := dockerCli.Client()
|
client := dockerCli.Client()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// attempt to lookup secret by name
|
ids, err := getCliRequestedSecretIDs(ctx, client, opts.names)
|
||||||
secrets, err := getSecretsByName(ctx, client, []string{opts.name})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
getRef := func(id string) (interface{}, []byte, error) {
|
||||||
id := opts.name
|
|
||||||
for _, s := range secrets {
|
|
||||||
if s.Spec.Annotations.Name == opts.name {
|
|
||||||
id = s.ID
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getRef := func(name string) (interface{}, []byte, error) {
|
|
||||||
return client.SecretInspectWithRaw(ctx, id)
|
return client.SecretInspectWithRaw(ctx, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
return inspect.Inspect(dockerCli.Out(), []string{id}, opts.format, getRef)
|
return inspect.Inspect(dockerCli.Out(), ids, opts.format, getRef)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,17 +10,17 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type removeOptions struct {
|
type removeOptions struct {
|
||||||
ids []string
|
names []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSecretRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
|
func newSecretRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
|
||||||
return &cobra.Command{
|
return &cobra.Command{
|
||||||
Use: "rm [id]",
|
Use: "rm SECRET [SECRET]",
|
||||||
Short: "Remove a secret",
|
Short: "Remove a secret",
|
||||||
Args: cli.RequiresMinArgs(1),
|
Args: cli.RequiresMinArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
opts := removeOptions{
|
opts := removeOptions{
|
||||||
ids: args,
|
names: args,
|
||||||
}
|
}
|
||||||
return runSecretRemove(dockerCli, opts)
|
return runSecretRemove(dockerCli, opts)
|
||||||
},
|
},
|
||||||
|
@ -31,32 +31,14 @@ func runSecretRemove(dockerCli *command.DockerCli, opts removeOptions) error {
|
||||||
client := dockerCli.Client()
|
client := dockerCli.Client()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// attempt to lookup secret by name
|
ids, err := getCliRequestedSecretIDs(ctx, client, opts.names)
|
||||||
secrets, err := getSecretsByName(ctx, client, opts.ids)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ids := opts.ids
|
|
||||||
|
|
||||||
names := make(map[string]int)
|
|
||||||
for _, id := range ids {
|
|
||||||
names[id] = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(secrets) > 0 {
|
|
||||||
ids = []string{}
|
|
||||||
|
|
||||||
for _, s := range secrets {
|
|
||||||
if _, ok := names[s.Spec.Annotations.Name]; ok {
|
|
||||||
ids = append(ids, s.ID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
if err := client.SecretRemove(ctx, id); err != nil {
|
if err := client.SecretRemove(ctx, id); err != nil {
|
||||||
return err
|
fmt.Fprintf(dockerCli.Out(), "WARN: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintln(dockerCli.Out(), id)
|
fmt.Fprintln(dockerCli.Out(), id)
|
||||||
|
|
|
@ -18,3 +18,30 @@ func getSecretsByName(ctx context.Context, client client.APIClient, names []stri
|
||||||
Filters: args,
|
Filters: args,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCliRequestedSecretIDs(ctx context.Context, client client.APIClient, names []string) ([]string, error) {
|
||||||
|
ids := names
|
||||||
|
|
||||||
|
// attempt to lookup secret by name
|
||||||
|
secrets, err := getSecretsByName(ctx, client, ids)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
lookup := make(map[string]struct{})
|
||||||
|
for _, id := range ids {
|
||||||
|
lookup[id] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(secrets) > 0 {
|
||||||
|
ids = []string{}
|
||||||
|
|
||||||
|
for _, s := range secrets {
|
||||||
|
if _, ok := lookup[s.Spec.Annotations.Name]; ok {
|
||||||
|
ids = append(ids, s.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ids, nil
|
||||||
|
}
|
||||||
|
|
68
integration-cli/docker_cli_secret_inspect_test.go
Normal file
68
integration-cli/docker_cli_secret_inspect_test.go
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types/swarm"
|
||||||
|
"github.com/docker/docker/pkg/integration/checker"
|
||||||
|
"github.com/go-check/check"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *DockerSwarmSuite) TestSecretInspect(c *check.C) {
|
||||||
|
d := s.AddDaemon(c, true, true)
|
||||||
|
|
||||||
|
testName := "test_secret"
|
||||||
|
id := d.createSecret(c, swarm.SecretSpec{
|
||||||
|
swarm.Annotations{
|
||||||
|
Name: testName,
|
||||||
|
},
|
||||||
|
[]byte("TESTINGDATA"),
|
||||||
|
})
|
||||||
|
c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("secrets: %s", id))
|
||||||
|
|
||||||
|
secret := d.getSecret(c, id)
|
||||||
|
c.Assert(secret.Spec.Name, checker.Equals, testName)
|
||||||
|
|
||||||
|
out, err := d.Cmd("secret", "inspect", testName)
|
||||||
|
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||||
|
|
||||||
|
var secrets []swarm.Secret
|
||||||
|
c.Assert(json.Unmarshal([]byte(out), &secrets), checker.IsNil)
|
||||||
|
c.Assert(secrets, checker.HasLen, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *DockerSwarmSuite) TestSecretInspectMultiple(c *check.C) {
|
||||||
|
d := s.AddDaemon(c, true, true)
|
||||||
|
|
||||||
|
testNames := []string{
|
||||||
|
"test0",
|
||||||
|
"test1",
|
||||||
|
}
|
||||||
|
for _, n := range testNames {
|
||||||
|
id := d.createSecret(c, swarm.SecretSpec{
|
||||||
|
swarm.Annotations{
|
||||||
|
Name: n,
|
||||||
|
},
|
||||||
|
[]byte("TESTINGDATA"),
|
||||||
|
})
|
||||||
|
c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("secrets: %s", id))
|
||||||
|
|
||||||
|
secret := d.getSecret(c, id)
|
||||||
|
c.Assert(secret.Spec.Name, checker.Equals, n)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"secret",
|
||||||
|
"inspect",
|
||||||
|
}
|
||||||
|
args = append(args, testNames...)
|
||||||
|
out, err := d.Cmd(args...)
|
||||||
|
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||||
|
|
||||||
|
var secrets []swarm.Secret
|
||||||
|
c.Assert(json.Unmarshal([]byte(out), &secrets), checker.IsNil)
|
||||||
|
c.Assert(secrets, checker.HasLen, 2)
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue