2016-11-03 11:08:22 -04:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/csv"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2017-03-16 13:54:18 -04:00
|
|
|
swarmtypes "github.com/docker/docker/api/types/swarm"
|
2016-11-03 11:08:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// SecretOpt is a Value type for parsing secrets
|
|
|
|
type SecretOpt struct {
|
2017-03-16 13:54:18 -04:00
|
|
|
values []*swarmtypes.SecretReference
|
2016-11-03 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set a new secret value
|
|
|
|
func (o *SecretOpt) Set(value string) error {
|
|
|
|
csvReader := csv.NewReader(strings.NewReader(value))
|
|
|
|
fields, err := csvReader.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:54:18 -04:00
|
|
|
options := &swarmtypes.SecretReference{
|
|
|
|
File: &swarmtypes.SecretReferenceFileTarget{
|
|
|
|
UID: "0",
|
|
|
|
GID: "0",
|
|
|
|
Mode: 0444,
|
|
|
|
},
|
2016-11-03 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2016-11-03 15:56:05 -04:00
|
|
|
// support a simple syntax of --secret foo
|
|
|
|
if len(fields) == 1 {
|
2017-03-16 13:54:18 -04:00
|
|
|
options.File.Name = fields[0]
|
|
|
|
options.SecretName = fields[0]
|
2016-11-03 15:56:05 -04:00
|
|
|
o.values = append(o.values, options)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-03 11:08:22 -04:00
|
|
|
for _, field := range fields {
|
|
|
|
parts := strings.SplitN(field, "=", 2)
|
|
|
|
key := strings.ToLower(parts[0])
|
|
|
|
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return fmt.Errorf("invalid field '%s' must be a key=value pair", field)
|
|
|
|
}
|
|
|
|
|
|
|
|
value := parts[1]
|
|
|
|
switch key {
|
2017-01-24 09:41:45 -05:00
|
|
|
case "source", "src":
|
2017-03-16 13:54:18 -04:00
|
|
|
options.SecretName = value
|
2016-11-03 11:08:22 -04:00
|
|
|
case "target":
|
|
|
|
tDir, _ := filepath.Split(value)
|
|
|
|
if tDir != "" {
|
|
|
|
return fmt.Errorf("target must not be a path")
|
|
|
|
}
|
2017-03-16 13:54:18 -04:00
|
|
|
options.File.Name = value
|
2016-11-03 11:08:22 -04:00
|
|
|
case "uid":
|
2017-03-16 13:54:18 -04:00
|
|
|
options.File.UID = value
|
2016-11-03 11:08:22 -04:00
|
|
|
case "gid":
|
2017-03-16 13:54:18 -04:00
|
|
|
options.File.GID = value
|
2016-11-03 11:08:22 -04:00
|
|
|
case "mode":
|
|
|
|
m, err := strconv.ParseUint(value, 0, 32)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid mode specified: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:54:18 -04:00
|
|
|
options.File.Mode = os.FileMode(m)
|
2016-11-03 11:08:22 -04:00
|
|
|
default:
|
2017-03-16 13:54:18 -04:00
|
|
|
return fmt.Errorf("invalid field in secret request: %s", key)
|
2016-11-03 11:08:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:54:18 -04:00
|
|
|
if options.SecretName == "" {
|
2016-11-03 11:08:22 -04:00
|
|
|
return fmt.Errorf("source is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
o.values = append(o.values, options)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type of this option
|
|
|
|
func (o *SecretOpt) Type() string {
|
|
|
|
return "secret"
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string repr of this option
|
|
|
|
func (o *SecretOpt) String() string {
|
|
|
|
secrets := []string{}
|
|
|
|
for _, secret := range o.values {
|
2017-03-16 13:54:18 -04:00
|
|
|
repr := fmt.Sprintf("%s -> %s", secret.SecretName, secret.File.Name)
|
2016-11-03 11:08:22 -04:00
|
|
|
secrets = append(secrets, repr)
|
|
|
|
}
|
|
|
|
return strings.Join(secrets, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the secret requests
|
2017-03-16 13:54:18 -04:00
|
|
|
func (o *SecretOpt) Value() []*swarmtypes.SecretReference {
|
2016-11-03 11:08:22 -04:00
|
|
|
return o.values
|
|
|
|
}
|