2018-03-01 17:51:11 -05:00
|
|
|
package secret // import "github.com/docker/docker/integration/secret"
|
2018-02-02 17:23:12 -05:00
|
|
|
|
|
|
|
import (
|
2017-06-15 15:06:08 -04:00
|
|
|
"bytes"
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2019-08-19 09:18:42 -04:00
|
|
|
"encoding/json"
|
2018-02-08 19:45:22 -05:00
|
|
|
"sort"
|
2018-02-02 17:23:12 -05:00
|
|
|
"testing"
|
2017-06-15 15:06:08 -04:00
|
|
|
"time"
|
2018-02-02 17:23:12 -05:00
|
|
|
|
2018-02-08 19:45:22 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
2018-02-02 17:23:12 -05:00
|
|
|
swarmtypes "github.com/docker/docker/api/types/swarm"
|
2018-01-31 18:01:29 -05:00
|
|
|
"github.com/docker/docker/client"
|
2019-08-19 09:50:32 -04:00
|
|
|
"github.com/docker/docker/errdefs"
|
2018-02-09 13:13:26 -05:00
|
|
|
"github.com/docker/docker/integration/internal/swarm"
|
2017-06-15 15:06:08 -04:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2019-08-19 08:57:21 -04:00
|
|
|
"gotest.tools/v3/poll"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/skip"
|
2018-02-02 17:23:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSecretInspect(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
2018-02-02 17:23:12 -05:00
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
d := swarm.NewSwarm(t, testEnv)
|
|
|
|
defer d.Stop(t)
|
2019-08-19 08:48:02 -04:00
|
|
|
c := d.NewClientT(t)
|
|
|
|
defer c.Close()
|
2018-02-02 17:23:12 -05:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-08-19 09:18:42 -04:00
|
|
|
testName := t.Name()
|
2019-08-19 08:48:02 -04:00
|
|
|
secretID := createSecret(ctx, t, c, testName, []byte("TESTINGDATA"), nil)
|
2018-02-02 17:23:12 -05:00
|
|
|
|
2019-08-19 09:18:42 -04:00
|
|
|
insp, body, err := c.SecretInspectWithRaw(ctx, secretID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2019-08-19 09:18:42 -04:00
|
|
|
assert.Check(t, is.Equal(insp.Spec.Name, testName))
|
2018-02-02 17:23:12 -05:00
|
|
|
|
2019-08-19 09:18:42 -04:00
|
|
|
var secret swarmtypes.Secret
|
|
|
|
err = json.Unmarshal(body, &secret)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2019-08-19 09:18:42 -04:00
|
|
|
assert.Check(t, is.DeepEqual(secret, insp))
|
2018-02-08 19:45:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretList(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
2018-02-08 19:45:22 -05:00
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
d := swarm.NewSwarm(t, testEnv)
|
|
|
|
defer d.Stop(t)
|
2019-08-19 08:48:02 -04:00
|
|
|
c := d.NewClientT(t)
|
|
|
|
defer c.Close()
|
2018-02-08 19:45:22 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-08-19 10:04:56 -04:00
|
|
|
configs, err := c.SecretList(ctx, types.SecretListOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(len(configs), 0))
|
|
|
|
|
2018-06-01 06:25:00 -04:00
|
|
|
testName0 := "test0_" + t.Name()
|
|
|
|
testName1 := "test1_" + t.Name()
|
2018-02-08 19:45:22 -05:00
|
|
|
testNames := []string{testName0, testName1}
|
|
|
|
sort.Strings(testNames)
|
|
|
|
|
|
|
|
// create secret test0
|
2019-08-19 08:48:02 -04:00
|
|
|
createSecret(ctx, t, c, testName0, []byte("TESTINGDATA0"), map[string]string{"type": "test"})
|
2018-02-08 19:45:22 -05:00
|
|
|
|
|
|
|
// create secret test1
|
2019-08-19 08:48:02 -04:00
|
|
|
secret1ID := createSecret(ctx, t, c, testName1, []byte("TESTINGDATA1"), map[string]string{"type": "production"})
|
2018-02-08 19:45:22 -05:00
|
|
|
|
|
|
|
// test by `secret ls`
|
2019-08-19 08:48:02 -04:00
|
|
|
entries, err := c.SecretList(ctx, types.SecretListOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-07-17 16:37:30 -04:00
|
|
|
assert.Check(t, is.DeepEqual(secretNamesFromList(entries), testNames))
|
2018-02-08 19:45:22 -05:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
filters filters.Args
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
// test filter by name `secret ls --filter name=xxx`
|
|
|
|
{
|
|
|
|
filters: filters.NewArgs(filters.Arg("name", testName0)),
|
|
|
|
expected: []string{testName0},
|
|
|
|
},
|
|
|
|
// test filter by id `secret ls --filter id=xxx`
|
|
|
|
{
|
|
|
|
filters: filters.NewArgs(filters.Arg("id", secret1ID)),
|
|
|
|
expected: []string{testName1},
|
|
|
|
},
|
|
|
|
// test filter by label `secret ls --filter label=xxx`
|
|
|
|
{
|
|
|
|
filters: filters.NewArgs(filters.Arg("label", "type")),
|
|
|
|
expected: testNames,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filters: filters.NewArgs(filters.Arg("label", "type=test")),
|
|
|
|
expected: []string{testName0},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filters: filters.NewArgs(filters.Arg("label", "type=production")),
|
|
|
|
expected: []string{testName1},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2019-08-19 08:48:02 -04:00
|
|
|
entries, err = c.SecretList(ctx, types.SecretListOptions{
|
2018-02-08 19:45:22 -05:00
|
|
|
Filters: tc.filters,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-07-17 16:37:30 -04:00
|
|
|
assert.Check(t, is.DeepEqual(secretNamesFromList(entries), tc.expected))
|
2018-02-08 19:45:22 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createSecret(ctx context.Context, t *testing.T, client client.APIClient, name string, data []byte, labels map[string]string) string {
|
|
|
|
secret, err := client.SecretCreate(ctx, swarmtypes.SecretSpec{
|
|
|
|
Annotations: swarmtypes.Annotations{
|
|
|
|
Name: name,
|
|
|
|
Labels: labels,
|
|
|
|
},
|
|
|
|
Data: data,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, secret.ID != "")
|
2018-02-08 19:45:22 -05:00
|
|
|
return secret.ID
|
2018-02-02 17:23:12 -05:00
|
|
|
}
|
2018-02-11 16:17:44 -05:00
|
|
|
|
2018-02-22 06:25:10 -05:00
|
|
|
func TestSecretsCreateAndDelete(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
2018-02-11 16:17:44 -05:00
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
d := swarm.NewSwarm(t, testEnv)
|
|
|
|
defer d.Stop(t)
|
2019-08-19 08:48:02 -04:00
|
|
|
c := d.NewClientT(t)
|
|
|
|
defer c.Close()
|
2018-02-11 16:17:44 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-06-01 06:25:00 -04:00
|
|
|
testName := "test_secret_" + t.Name()
|
2019-08-19 08:48:02 -04:00
|
|
|
secretID := createSecret(ctx, t, c, testName, []byte("TESTINGDATA"), nil)
|
2018-02-11 16:17:44 -05:00
|
|
|
|
2019-08-19 09:50:32 -04:00
|
|
|
// create an already existing secret, daemon should return a status code of 409
|
2019-08-19 08:48:02 -04:00
|
|
|
_, err := c.SecretCreate(ctx, swarmtypes.SecretSpec{
|
2018-02-11 16:17:44 -05:00
|
|
|
Annotations: swarmtypes.Annotations{
|
|
|
|
Name: testName,
|
|
|
|
},
|
|
|
|
Data: []byte("TESTINGDATA"),
|
|
|
|
})
|
2019-08-19 09:50:32 -04:00
|
|
|
assert.Check(t, errdefs.IsConflict(err))
|
|
|
|
assert.Check(t, is.ErrorContains(err, testName))
|
2018-02-11 16:17:44 -05:00
|
|
|
|
2019-08-19 08:48:02 -04:00
|
|
|
err = c.SecretRemove(ctx, secretID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-11 16:17:44 -05:00
|
|
|
|
2019-08-19 08:48:02 -04:00
|
|
|
_, _, err = c.SecretInspectWithRaw(ctx, secretID)
|
2019-08-19 09:50:32 -04:00
|
|
|
assert.Check(t, errdefs.IsNotFound(err))
|
|
|
|
assert.Check(t, is.ErrorContains(err, secretID))
|
2018-02-11 16:17:44 -05:00
|
|
|
|
2019-08-19 09:50:32 -04:00
|
|
|
err = c.SecretRemove(ctx, "non-existing")
|
|
|
|
assert.Check(t, errdefs.IsNotFound(err))
|
|
|
|
assert.Check(t, is.ErrorContains(err, "non-existing"))
|
2018-02-22 06:25:10 -05:00
|
|
|
|
2018-06-01 06:25:00 -04:00
|
|
|
testName = "test_secret_with_labels_" + t.Name()
|
2019-08-19 08:48:02 -04:00
|
|
|
secretID = createSecret(ctx, t, c, testName, []byte("TESTINGDATA"), map[string]string{
|
2018-02-22 06:25:10 -05:00
|
|
|
"key1": "value1",
|
|
|
|
"key2": "value2",
|
|
|
|
})
|
|
|
|
|
2019-08-19 08:48:02 -04:00
|
|
|
insp, _, err := c.SecretInspectWithRaw(ctx, secretID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(insp.Spec.Name, testName))
|
|
|
|
assert.Check(t, is.Equal(len(insp.Spec.Labels), 2))
|
|
|
|
assert.Check(t, is.Equal(insp.Spec.Labels["key1"], "value1"))
|
|
|
|
assert.Check(t, is.Equal(insp.Spec.Labels["key2"], "value2"))
|
2018-02-11 16:17:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretsUpdate(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
2018-02-11 16:17:44 -05:00
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
d := swarm.NewSwarm(t, testEnv)
|
|
|
|
defer d.Stop(t)
|
2019-08-19 08:48:02 -04:00
|
|
|
c := d.NewClientT(t)
|
|
|
|
defer c.Close()
|
2018-02-11 16:17:44 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-06-01 06:25:00 -04:00
|
|
|
testName := "test_secret_" + t.Name()
|
2019-08-19 08:48:02 -04:00
|
|
|
secretID := createSecret(ctx, t, c, testName, []byte("TESTINGDATA"), nil)
|
2018-02-11 16:17:44 -05:00
|
|
|
|
2019-08-19 08:48:02 -04:00
|
|
|
insp, _, err := c.SecretInspectWithRaw(ctx, secretID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(insp.ID, secretID))
|
2018-02-11 16:17:44 -05:00
|
|
|
|
|
|
|
// test UpdateSecret with full ID
|
|
|
|
insp.Spec.Labels = map[string]string{"test": "test1"}
|
2019-08-19 08:48:02 -04:00
|
|
|
err = c.SecretUpdate(ctx, secretID, insp.Version, insp.Spec)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-11 16:17:44 -05:00
|
|
|
|
2019-08-19 08:48:02 -04:00
|
|
|
insp, _, err = c.SecretInspectWithRaw(ctx, secretID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(insp.Spec.Labels["test"], "test1"))
|
2018-02-11 16:17:44 -05:00
|
|
|
|
|
|
|
// test UpdateSecret with full name
|
|
|
|
insp.Spec.Labels = map[string]string{"test": "test2"}
|
2019-08-19 08:48:02 -04:00
|
|
|
err = c.SecretUpdate(ctx, testName, insp.Version, insp.Spec)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-11 16:17:44 -05:00
|
|
|
|
2019-08-19 08:48:02 -04:00
|
|
|
insp, _, err = c.SecretInspectWithRaw(ctx, secretID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(insp.Spec.Labels["test"], "test2"))
|
2018-02-11 16:17:44 -05:00
|
|
|
|
|
|
|
// test UpdateSecret with prefix ID
|
|
|
|
insp.Spec.Labels = map[string]string{"test": "test3"}
|
2019-08-19 08:48:02 -04:00
|
|
|
err = c.SecretUpdate(ctx, secretID[:1], insp.Version, insp.Spec)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-11 16:17:44 -05:00
|
|
|
|
2019-08-19 08:48:02 -04:00
|
|
|
insp, _, err = c.SecretInspectWithRaw(ctx, secretID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(insp.Spec.Labels["test"], "test3"))
|
2018-02-11 16:17:44 -05:00
|
|
|
|
|
|
|
// test UpdateSecret in updating Data which is not supported in daemon
|
|
|
|
// this test will produce an error in func UpdateSecret
|
|
|
|
insp.Spec.Data = []byte("TESTINGDATA2")
|
2019-08-19 08:48:02 -04:00
|
|
|
err = c.SecretUpdate(ctx, secretID, insp.Version, insp.Spec)
|
2019-08-19 09:50:32 -04:00
|
|
|
assert.Check(t, errdefs.IsInvalidParameter(err))
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, "only updates to Labels are allowed"))
|
2018-02-11 16:17:44 -05:00
|
|
|
}
|
2017-06-15 15:06:08 -04:00
|
|
|
|
|
|
|
func TestTemplatedSecret(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
2017-06-15 15:06:08 -04:00
|
|
|
d := swarm.NewSwarm(t, testEnv)
|
|
|
|
defer d.Stop(t)
|
2019-08-19 08:48:02 -04:00
|
|
|
c := d.NewClientT(t)
|
|
|
|
defer c.Close()
|
2017-06-15 15:06:08 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-06-01 06:25:00 -04:00
|
|
|
referencedSecretName := "referencedsecret_" + t.Name()
|
2017-06-15 15:06:08 -04:00
|
|
|
referencedSecretSpec := swarmtypes.SecretSpec{
|
|
|
|
Annotations: swarmtypes.Annotations{
|
2018-06-01 06:25:00 -04:00
|
|
|
Name: referencedSecretName,
|
2017-06-15 15:06:08 -04:00
|
|
|
},
|
|
|
|
Data: []byte("this is a secret"),
|
|
|
|
}
|
2019-08-19 08:48:02 -04:00
|
|
|
referencedSecret, err := c.SecretCreate(ctx, referencedSecretSpec)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err)
|
2017-06-15 15:06:08 -04:00
|
|
|
|
2018-06-01 06:25:00 -04:00
|
|
|
referencedConfigName := "referencedconfig_" + t.Name()
|
2017-06-15 15:06:08 -04:00
|
|
|
referencedConfigSpec := swarmtypes.ConfigSpec{
|
|
|
|
Annotations: swarmtypes.Annotations{
|
2018-06-01 06:25:00 -04:00
|
|
|
Name: referencedConfigName,
|
2017-06-15 15:06:08 -04:00
|
|
|
},
|
|
|
|
Data: []byte("this is a config"),
|
|
|
|
}
|
2019-08-19 08:48:02 -04:00
|
|
|
referencedConfig, err := c.ConfigCreate(ctx, referencedConfigSpec)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err)
|
2017-06-15 15:06:08 -04:00
|
|
|
|
2018-06-01 06:25:00 -04:00
|
|
|
templatedSecretName := "templated_secret_" + t.Name()
|
2017-06-15 15:06:08 -04:00
|
|
|
secretSpec := swarmtypes.SecretSpec{
|
|
|
|
Annotations: swarmtypes.Annotations{
|
2018-06-01 06:25:00 -04:00
|
|
|
Name: templatedSecretName,
|
2017-06-15 15:06:08 -04:00
|
|
|
},
|
|
|
|
Templating: &swarmtypes.Driver{
|
|
|
|
Name: "golang",
|
|
|
|
},
|
|
|
|
Data: []byte("SERVICE_NAME={{.Service.Name}}\n" +
|
|
|
|
"{{secret \"referencedsecrettarget\"}}\n" +
|
|
|
|
"{{config \"referencedconfigtarget\"}}\n"),
|
|
|
|
}
|
|
|
|
|
2019-08-19 08:48:02 -04:00
|
|
|
templatedSecret, err := c.SecretCreate(ctx, secretSpec)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err)
|
2017-06-15 15:06:08 -04:00
|
|
|
|
2018-06-01 06:25:00 -04:00
|
|
|
serviceName := "svc_" + t.Name()
|
2017-06-15 15:06:08 -04:00
|
|
|
serviceID := swarm.CreateService(t, d,
|
|
|
|
swarm.ServiceWithSecret(
|
|
|
|
&swarmtypes.SecretReference{
|
|
|
|
File: &swarmtypes.SecretReferenceFileTarget{
|
|
|
|
Name: "templated_secret",
|
|
|
|
UID: "0",
|
|
|
|
GID: "0",
|
|
|
|
Mode: 0600,
|
|
|
|
},
|
|
|
|
SecretID: templatedSecret.ID,
|
2018-06-01 06:25:00 -04:00
|
|
|
SecretName: templatedSecretName,
|
2017-06-15 15:06:08 -04:00
|
|
|
},
|
|
|
|
),
|
|
|
|
swarm.ServiceWithConfig(
|
|
|
|
&swarmtypes.ConfigReference{
|
|
|
|
File: &swarmtypes.ConfigReferenceFileTarget{
|
|
|
|
Name: "referencedconfigtarget",
|
|
|
|
UID: "0",
|
|
|
|
GID: "0",
|
|
|
|
Mode: 0600,
|
|
|
|
},
|
|
|
|
ConfigID: referencedConfig.ID,
|
2018-06-01 06:25:00 -04:00
|
|
|
ConfigName: referencedConfigName,
|
2017-06-15 15:06:08 -04:00
|
|
|
},
|
|
|
|
),
|
|
|
|
swarm.ServiceWithSecret(
|
|
|
|
&swarmtypes.SecretReference{
|
|
|
|
File: &swarmtypes.SecretReferenceFileTarget{
|
|
|
|
Name: "referencedsecrettarget",
|
|
|
|
UID: "0",
|
|
|
|
GID: "0",
|
|
|
|
Mode: 0600,
|
|
|
|
},
|
|
|
|
SecretID: referencedSecret.ID,
|
2018-06-01 06:25:00 -04:00
|
|
|
SecretName: referencedSecretName,
|
2017-06-15 15:06:08 -04:00
|
|
|
},
|
|
|
|
),
|
2018-06-01 06:25:00 -04:00
|
|
|
swarm.ServiceWithName(serviceName),
|
2017-06-15 15:06:08 -04:00
|
|
|
)
|
|
|
|
|
2019-08-19 08:57:21 -04:00
|
|
|
poll.WaitOn(t, swarm.RunningTasksCount(c, serviceID, 1), swarm.ServicePoll, poll.WithTimeout(1*time.Minute))
|
2017-06-15 15:06:08 -04:00
|
|
|
|
2019-08-19 08:57:21 -04:00
|
|
|
tasks := swarm.GetRunningTasks(t, c, serviceID)
|
|
|
|
assert.Assert(t, len(tasks) > 0, "no running tasks found for service %s", serviceID)
|
2017-06-15 15:06:08 -04:00
|
|
|
|
2019-08-19 08:57:21 -04:00
|
|
|
attach := swarm.ExecTask(t, d, tasks[0], types.ExecConfig{
|
2017-06-15 15:06:08 -04:00
|
|
|
Cmd: []string{"/bin/cat", "/run/secrets/templated_secret"},
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
})
|
|
|
|
|
2018-06-01 06:25:00 -04:00
|
|
|
expect := "SERVICE_NAME=" + serviceName + "\n" +
|
2017-06-15 15:06:08 -04:00
|
|
|
"this is a secret\n" +
|
|
|
|
"this is a config\n"
|
2018-01-17 10:49:58 -05:00
|
|
|
assertAttachedStream(t, attach, expect)
|
|
|
|
|
2019-08-19 08:57:21 -04:00
|
|
|
attach = swarm.ExecTask(t, d, tasks[0], types.ExecConfig{
|
2018-01-17 10:49:58 -05:00
|
|
|
Cmd: []string{"mount"},
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
})
|
|
|
|
assertAttachedStream(t, attach, "tmpfs on /run/secrets/templated_secret type tmpfs")
|
|
|
|
}
|
2017-06-15 15:06:08 -04:00
|
|
|
|
2018-07-17 16:37:30 -04:00
|
|
|
// Test case for 28884
|
|
|
|
func TestSecretCreateResolve(t *testing.T) {
|
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType != "linux")
|
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
d := swarm.NewSwarm(t, testEnv)
|
|
|
|
defer d.Stop(t)
|
2019-08-19 08:48:02 -04:00
|
|
|
c := d.NewClientT(t)
|
|
|
|
defer c.Close()
|
2018-07-17 16:37:30 -04:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
testName := "test_secret_" + t.Name()
|
2019-08-19 08:48:02 -04:00
|
|
|
secretID := createSecret(ctx, t, c, testName, []byte("foo"), nil)
|
2018-07-17 16:37:30 -04:00
|
|
|
|
|
|
|
fakeName := secretID
|
2019-08-19 08:48:02 -04:00
|
|
|
fakeID := createSecret(ctx, t, c, fakeName, []byte("fake foo"), nil)
|
2018-07-17 16:37:30 -04:00
|
|
|
|
2019-08-19 08:48:02 -04:00
|
|
|
entries, err := c.SecretList(ctx, types.SecretListOptions{})
|
2018-07-17 16:37:30 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(secretNamesFromList(entries), testName))
|
|
|
|
assert.Check(t, is.Contains(secretNamesFromList(entries), fakeName))
|
|
|
|
|
2019-08-19 08:48:02 -04:00
|
|
|
err = c.SecretRemove(ctx, secretID)
|
2018-07-17 16:37:30 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
// Fake one will remain
|
2019-08-19 08:48:02 -04:00
|
|
|
entries, err = c.SecretList(ctx, types.SecretListOptions{})
|
2018-07-17 16:37:30 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, is.DeepEqual(secretNamesFromList(entries), []string{fakeName}))
|
|
|
|
|
|
|
|
// Remove based on name prefix of the fake one should not work
|
|
|
|
// as search is only done based on:
|
|
|
|
// - Full ID
|
|
|
|
// - Full Name
|
|
|
|
// - Partial ID (prefix)
|
2019-08-19 08:48:02 -04:00
|
|
|
err = c.SecretRemove(ctx, fakeName[:5])
|
2018-07-17 16:37:30 -04:00
|
|
|
assert.Assert(t, nil != err)
|
2019-08-19 08:48:02 -04:00
|
|
|
entries, err = c.SecretList(ctx, types.SecretListOptions{})
|
2018-07-17 16:37:30 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, is.DeepEqual(secretNamesFromList(entries), []string{fakeName}))
|
|
|
|
|
|
|
|
// Remove based on ID prefix of the fake one should succeed
|
2019-08-19 08:48:02 -04:00
|
|
|
err = c.SecretRemove(ctx, fakeID[:5])
|
2018-07-17 16:37:30 -04:00
|
|
|
assert.NilError(t, err)
|
2019-08-19 08:48:02 -04:00
|
|
|
entries, err = c.SecretList(ctx, types.SecretListOptions{})
|
2018-07-17 16:37:30 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, is.Equal(0, len(entries)))
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:49:58 -05:00
|
|
|
func assertAttachedStream(t *testing.T, attach types.HijackedResponse, expect string) {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
_, err := stdcopy.StdCopy(buf, buf, attach.Reader)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(buf.String(), expect))
|
2017-06-15 15:06:08 -04:00
|
|
|
}
|
|
|
|
|
2018-07-17 16:37:30 -04:00
|
|
|
func secretNamesFromList(entries []swarmtypes.Secret) []string {
|
|
|
|
var values []string
|
|
|
|
for _, entry := range entries {
|
|
|
|
values = append(values, entry.Spec.Name)
|
|
|
|
}
|
|
|
|
sort.Strings(values)
|
|
|
|
return values
|
|
|
|
}
|