github.com/endocode/docker@v1.4.2-0.20160113120958-46eb4700391e/runconfig/opts/opts.go (about) 1 package opts 2 3 import ( 4 "fmt" 5 fopts "github.com/docker/docker/opts" 6 "net" 7 "os" 8 "strings" 9 ) 10 11 // ValidateAttach validates that the specified string is a valid attach option. 12 func ValidateAttach(val string) (string, error) { 13 s := strings.ToLower(val) 14 for _, str := range []string{"stdin", "stdout", "stderr"} { 15 if s == str { 16 return s, nil 17 } 18 } 19 return val, fmt.Errorf("valid streams are STDIN, STDOUT and STDERR") 20 } 21 22 // ValidateEnv validates an environment variable and returns it. 23 // If no value is specified, it returns the current value using os.Getenv. 24 // 25 // As on ParseEnvFile and related to #16585, environment variable names 26 // are not validate what so ever, it's up to application inside docker 27 // to validate them or not. 28 func ValidateEnv(val string) (string, error) { 29 arr := strings.Split(val, "=") 30 if len(arr) > 1 { 31 return val, nil 32 } 33 if !doesEnvExist(val) { 34 return val, nil 35 } 36 return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil 37 } 38 39 func doesEnvExist(name string) bool { 40 for _, entry := range os.Environ() { 41 parts := strings.SplitN(entry, "=", 2) 42 if parts[0] == name { 43 return true 44 } 45 } 46 return false 47 } 48 49 // ValidateExtraHost validates that the specified string is a valid extrahost and returns it. 50 // ExtraHost are in the form of name:ip where the ip has to be a valid ip (ipv4 or ipv6). 51 func ValidateExtraHost(val string) (string, error) { 52 // allow for IPv6 addresses in extra hosts by only splitting on first ":" 53 arr := strings.SplitN(val, ":", 2) 54 if len(arr) != 2 || len(arr[0]) == 0 { 55 return "", fmt.Errorf("bad format for add-host: %q", val) 56 } 57 if _, err := fopts.ValidateIPAddress(arr[1]); err != nil { 58 return "", fmt.Errorf("invalid IP address in add-host: %q", arr[1]) 59 } 60 return val, nil 61 } 62 63 // ValidateMACAddress validates a MAC address. 64 func ValidateMACAddress(val string) (string, error) { 65 _, err := net.ParseMAC(strings.TrimSpace(val)) 66 if err != nil { 67 return "", err 68 } 69 return val, nil 70 }