github.com/hms58/moby@v1.13.1/runconfig/opts/opts.go (about)

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