secrets: support simple syntax --secret foo

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
Evan Hazlett 2016-11-03 15:56:05 -04:00 committed by Vincent Demeester
parent 20525b5695
commit 7180c8afed
2 changed files with 80 additions and 1 deletions

View File

@ -32,6 +32,14 @@ func (o *SecretOpt) Set(value string) error {
Mode: 0444,
}
// support a simple syntax of --secret foo
if len(fields) == 1 {
options.Source = fields[0]
options.Target = fields[0]
o.values = append(o.values, options)
return nil
}
for _, field := range fields {
parts := strings.SplitN(field, "=", 2)
key := strings.ToLower(parts[0])
@ -62,9 +70,13 @@ func (o *SecretOpt) Set(value string) error {
options.Mode = os.FileMode(m)
default:
if len(fields) == 1 && value == "" {
} else {
return fmt.Errorf("invalid field in secret request: %s", key)
}
}
}
if options.Source == "" {
return fmt.Errorf("source is required")

67
opts/secret_test.go Normal file
View File

@ -0,0 +1,67 @@
package opts
import (
"os"
"testing"
"github.com/docker/docker/pkg/testutil/assert"
)
func TestSecretOptionsSimple(t *testing.T) {
var opt SecretOpt
testCase := "app-secret"
assert.NilError(t, opt.Set(testCase))
reqs := opt.Value()
assert.Equal(t, len(reqs), 1)
req := reqs[0]
assert.Equal(t, req.Source, "app-secret")
assert.Equal(t, req.Target, "app-secret")
assert.Equal(t, req.UID, "0")
assert.Equal(t, req.GID, "0")
}
func TestSecretOptionsSourceTarget(t *testing.T) {
var opt SecretOpt
testCase := "source=foo,target=testing"
assert.NilError(t, opt.Set(testCase))
reqs := opt.Value()
assert.Equal(t, len(reqs), 1)
req := reqs[0]
assert.Equal(t, req.Source, "foo")
assert.Equal(t, req.Target, "testing")
}
func TestSecretOptionsCustomUidGid(t *testing.T) {
var opt SecretOpt
testCase := "source=foo,target=testing,uid=1000,gid=1001"
assert.NilError(t, opt.Set(testCase))
reqs := opt.Value()
assert.Equal(t, len(reqs), 1)
req := reqs[0]
assert.Equal(t, req.Source, "foo")
assert.Equal(t, req.Target, "testing")
assert.Equal(t, req.UID, "1000")
assert.Equal(t, req.GID, "1001")
}
func TestSecretOptionsCustomMode(t *testing.T) {
var opt SecretOpt
testCase := "source=foo,target=testing,uid=1000,gid=1001,mode=0444"
assert.NilError(t, opt.Set(testCase))
reqs := opt.Value()
assert.Equal(t, len(reqs), 1)
req := reqs[0]
assert.Equal(t, req.Source, "foo")
assert.Equal(t, req.Target, "testing")
assert.Equal(t, req.UID, "1000")
assert.Equal(t, req.GID, "1001")
assert.Equal(t, req.Mode, os.FileMode(0444))
}