Browse code

support labels for secrets upon creation; review updates

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>

Evan Hazlett authored on 2016/11/04 06:01:54
Showing 9 changed files
... ...
@@ -26,5 +26,5 @@ type SecretReferenceFileTarget struct {
26 26
 type SecretReference struct {
27 27
 	SecretID   string
28 28
 	SecretName string
29
-	Target     SecretReferenceFileTarget
29
+	Target     *SecretReferenceFileTarget
30 30
 }
... ...
@@ -9,29 +9,37 @@ import (
9 9
 	"github.com/docker/docker/api/types/swarm"
10 10
 	"github.com/docker/docker/cli"
11 11
 	"github.com/docker/docker/cli/command"
12
+	"github.com/docker/docker/opts"
13
+	runconfigopts "github.com/docker/docker/runconfig/opts"
12 14
 	"github.com/spf13/cobra"
13 15
 )
14 16
 
15 17
 type createOptions struct {
16
-	name string
18
+	name   string
19
+	labels opts.ListOpts
17 20
 }
18 21
 
19 22
 func newSecretCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
20
-	return &cobra.Command{
23
+	createOpts := createOptions{
24
+		labels: opts.NewListOpts(runconfigopts.ValidateEnv),
25
+	}
26
+
27
+	cmd := &cobra.Command{
21 28
 		Use:   "create [name]",
22 29
 		Short: "Create a secret using stdin as content",
23
-		Args:  cli.ExactArgs(1),
30
+		Args:  cli.RequiresMinArgs(1),
24 31
 		RunE: func(cmd *cobra.Command, args []string) error {
25
-			opts := createOptions{
26
-				name: args[0],
27
-			}
28
-
29
-			return runSecretCreate(dockerCli, opts)
32
+			createOpts.name = args[0]
33
+			return runSecretCreate(dockerCli, createOpts)
30 34
 		},
31 35
 	}
36
+	flags := cmd.Flags()
37
+	flags.VarP(&createOpts.labels, "label", "l", "Secret labels")
38
+
39
+	return cmd
32 40
 }
33 41
 
34
-func runSecretCreate(dockerCli *command.DockerCli, opts createOptions) error {
42
+func runSecretCreate(dockerCli *command.DockerCli, options createOptions) error {
35 43
 	client := dockerCli.Client()
36 44
 	ctx := context.Background()
37 45
 
... ...
@@ -42,7 +50,8 @@ func runSecretCreate(dockerCli *command.DockerCli, opts createOptions) error {
42 42
 
43 43
 	spec := swarm.SecretSpec{
44 44
 		Annotations: swarm.Annotations{
45
-			Name: opts.name,
45
+			Name:   options.name,
46
+			Labels: runconfigopts.ConvertKVStringsToMap(options.labels.GetAll()),
46 47
 		},
47 48
 		Data: secretData,
48 49
 	}
... ...
@@ -19,7 +19,7 @@ func parseSecrets(client client.APIClient, requestedSecrets []*types.SecretReque
19 19
 	for _, secret := range requestedSecrets {
20 20
 		secretRef := &swarmtypes.SecretReference{
21 21
 			SecretName: secret.Source,
22
-			Target: swarmtypes.SecretReferenceFileTarget{
22
+			Target: &swarmtypes.SecretReferenceFileTarget{
23 23
 				Name: secret.Target,
24 24
 				UID:  secret.UID,
25 25
 				GID:  secret.GID,
... ...
@@ -108,7 +108,7 @@ func secretReferencesFromGRPC(sr []*swarmapi.SecretReference) []*types.SecretRef
108 108
 		refs = append(refs, &types.SecretReference{
109 109
 			SecretID:   s.SecretID,
110 110
 			SecretName: s.SecretName,
111
-			Target: types.SecretReferenceFileTarget{
111
+			Target: &types.SecretReferenceFileTarget{
112 112
 				Name: target.Name,
113 113
 				UID:  target.UID,
114 114
 				GID:  target.GID,
... ...
@@ -172,13 +172,13 @@ func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
172 172
 	}
173 173
 
174 174
 	for _, s := range c.Secrets {
175
+		targetPath := filepath.Clean(s.Target)
175 176
 		// ensure that the target is a filename only; no paths allowed
176
-		tDir, tPath := filepath.Split(s.Target)
177
-		if tDir != "" {
178
-			return fmt.Errorf("error creating secret: secret must not have a path")
177
+		if targetPath != filepath.Base(targetPath) {
178
+			return fmt.Errorf("error creating secret: secret must not be a path")
179 179
 		}
180 180
 
181
-		fPath := filepath.Join(localMountPath, tPath)
181
+		fPath := filepath.Join(localMountPath, targetPath)
182 182
 		if err := os.MkdirAll(filepath.Dir(fPath), 0700); err != nil {
183 183
 			return errors.Wrap(err, "error creating secret mount path")
184 184
 		}
... ...
@@ -19,6 +19,9 @@ keywords: ["secret, create"]
19 19
 Usage:  docker secret create [NAME]
20 20
 
21 21
 Create a secret using stdin as content
22
+Options:
23
+      --help         Print usage
24
+  -l, --label list   Secret labels (default [])
22 25
 ```
23 26
 
24 27
 Creates a secret using standard input for the secret content. You must run this
... ...
@@ -29,14 +32,45 @@ command on a manager node.
29 29
 ### Create a secret
30 30
 
31 31
 ```bash
32
-$ cat ssh-dev | docker secret create ssh-dev
32
+$ cat secret.json | docker secret create secret.json
33 33
 mhv17xfe3gh6xc4rij5orpfds
34 34
 
35 35
 $ docker secret ls
36
-ID                          NAME                CREATED                                   UPDATED                                   SIZE
37
-mhv17xfe3gh6xc4rij5orpfds   ssh-dev             2016-10-27 23:25:43.909181089 +0000 UTC   2016-10-27 23:25:43.909181089 +0000 UTC   1679
36
+ID                          NAME                    CREATED                                   UPDATED                                   SIZE
37
+mhv17xfe3gh6xc4rij5orpfds   secret.json             2016-10-27 23:25:43.909181089 +0000 UTC   2016-10-27 23:25:43.909181089 +0000 UTC   1679
38 38
 ```
39 39
 
40
+### Create a secret with labels
41
+
42
+```bash
43
+$ cat secret.json | docker secret create secret.json --label env=dev --label rev=20161102
44
+jtn7g6aukl5ky7nr9gvwafoxh
45
+
46
+$ docker secret inspect secret.json
47
+[
48
+    {
49
+        "ID": "jtn7g6aukl5ky7nr9gvwafoxh",
50
+        "Version": {
51
+            "Index": 541
52
+        },
53
+        "CreatedAt": "2016-11-03T20:54:12.924766548Z",
54
+        "UpdatedAt": "2016-11-03T20:54:12.924766548Z",
55
+        "Spec": {
56
+            "Name": "secret.json",
57
+            "Labels": {
58
+                "env": "dev",
59
+                "rev": "20161102"
60
+            },
61
+            "Data": null
62
+        },
63
+        "Digest": "sha256:4212a44b14e94154359569333d3fc6a80f6b9959dfdaff26412f4b2796b1f387",
64
+        "SecretSize": 1679
65
+    }
66
+]
67
+
68
+```
69
+
70
+
40 71
 ## Related information
41 72
 
42 73
 * [secret inspect](secret_inspect.md)
... ...
@@ -37,7 +37,7 @@ describes all the details of the format.
37 37
 
38 38
 ## Examples
39 39
 
40
-### Inspecting a secret  by name or ID
40
+### Inspecting a secret by name or ID
41 41
 
42 42
 You can inspect a secret, either by its *name*, or *ID*
43 43
 
... ...
@@ -45,12 +45,12 @@ For example, given the following secret:
45 45
 
46 46
 ```bash
47 47
 $ docker secret ls
48
-ID                          NAME                CREATED                                   UPDATED                                   SIZE
49
-mhv17xfe3gh6xc4rij5orpfds   ssh-dev             2016-10-27 23:25:43.909181089 +0000 UTC   2016-10-27 23:25:43.909181089 +0000 UTC   1679
48
+ID                          NAME                    CREATED                                   UPDATED                                   SIZE
49
+mhv17xfe3gh6xc4rij5orpfds   secret.json             2016-10-27 23:25:43.909181089 +0000 UTC   2016-10-27 23:25:43.909181089 +0000 UTC   1679
50 50
 ```
51 51
 
52 52
 ```bash
53
-$ docker secret inspect mhv17xfe3gh6xc4rij5orpfds
53
+$ docker secret inspect secret.json
54 54
 [
55 55
     {
56 56
         "ID": "mhv17xfe3gh6xc4rij5orpfds",
... ...
@@ -60,7 +60,7 @@ $ docker secret inspect mhv17xfe3gh6xc4rij5orpfds
60 60
         "CreatedAt": "2016-10-27T23:25:43.909181089Z",
61 61
         "UpdatedAt": "2016-10-27T23:25:43.909181089Z",
62 62
         "Spec": {
63
-            "Name": "ssh-dev",
63
+            "Name": "secret.json",
64 64
             "Data": null
65 65
         },
66 66
         "Digest": "sha256:8281c6d924520986e3c6af23ed8926710a611c90339db582c2a9ac480ba622b7",
... ...
@@ -33,8 +33,8 @@ On a manager node:
33 33
 
34 34
 ```bash
35 35
 $ docker secret ls
36
-ID                          NAME                CREATED                                   UPDATED                                   SIZE
37
-mhv17xfe3gh6xc4rij5orpfds   ssh-dev             2016-10-27 23:25:43.909181089 +0000 UTC   2016-10-27 23:25:43.909181089 +0000 UTC   1679
36
+ID                          NAME                    CREATED                                   UPDATED                                   SIZE
37
+mhv17xfe3gh6xc4rij5orpfds   secret.json             2016-10-27 23:25:43.909181089 +0000 UTC   2016-10-27 23:25:43.909181089 +0000 UTC   1679
38 38
 ```
39 39
 ## Related information
40 40
 
... ...
@@ -33,7 +33,7 @@ targeting a manager node.
33 33
 This example removes a secret:
34 34
 
35 35
 ```bash
36
-$ docker secret rm sapth4csdo5b6wz2p5uimh5xg
36
+$ docker secret rm secret.json
37 37
 sapth4csdo5b6wz2p5uimh5xg
38 38
 ```
39 39