github.com/nektos/act@v0.2.63-0.20240520024548-8acde99bfa9c/pkg/container/docker_socket.go (about)

     1  package container
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	log "github.com/sirupsen/logrus"
    10  )
    11  
    12  var CommonSocketLocations = []string{
    13  	"/var/run/docker.sock",
    14  	"/run/podman/podman.sock",
    15  	"$HOME/.colima/docker.sock",
    16  	"$XDG_RUNTIME_DIR/docker.sock",
    17  	"$XDG_RUNTIME_DIR/podman/podman.sock",
    18  	`\\.\pipe\docker_engine`,
    19  	"$HOME/.docker/run/docker.sock",
    20  }
    21  
    22  // returns socket URI or false if not found any
    23  func socketLocation() (string, bool) {
    24  	if dockerHost, exists := os.LookupEnv("DOCKER_HOST"); exists {
    25  		return dockerHost, true
    26  	}
    27  
    28  	for _, p := range CommonSocketLocations {
    29  		if _, err := os.Lstat(os.ExpandEnv(p)); err == nil {
    30  			if strings.HasPrefix(p, `\\.\`) {
    31  				return "npipe://" + filepath.ToSlash(os.ExpandEnv(p)), true
    32  			}
    33  			return "unix://" + filepath.ToSlash(os.ExpandEnv(p)), true
    34  		}
    35  	}
    36  
    37  	return "", false
    38  }
    39  
    40  // This function, `isDockerHostURI`, takes a string argument `daemonPath`. It checks if the
    41  // `daemonPath` is a valid Docker host URI. It does this by checking if the scheme of the URI (the
    42  // part before "://") contains only alphabetic characters. If it does, the function returns true,
    43  // indicating that the `daemonPath` is a Docker host URI. If it doesn't, or if the "://" delimiter
    44  // is not found in the `daemonPath`, the function returns false.
    45  func isDockerHostURI(daemonPath string) bool {
    46  	if protoIndex := strings.Index(daemonPath, "://"); protoIndex != -1 {
    47  		scheme := daemonPath[:protoIndex]
    48  		if strings.IndexFunc(scheme, func(r rune) bool {
    49  			return (r < 'a' || r > 'z') && (r < 'A' || r > 'Z')
    50  		}) == -1 {
    51  			return true
    52  		}
    53  	}
    54  	return false
    55  }
    56  
    57  type SocketAndHost struct {
    58  	Socket string
    59  	Host   string
    60  }
    61  
    62  func GetSocketAndHost(containerSocket string) (SocketAndHost, error) {
    63  	log.Debugf("Handling container host and socket")
    64  
    65  	// Prefer DOCKER_HOST, don't override it
    66  	dockerHost, hasDockerHost := socketLocation()
    67  	socketHost := SocketAndHost{Socket: containerSocket, Host: dockerHost}
    68  
    69  	// ** socketHost.Socket cases **
    70  	// Case 1: User does _not_ want to mount a daemon socket (passes a dash)
    71  	// Case 2: User passes a filepath to the socket; is that even valid?
    72  	// Case 3: User passes a valid socket; do nothing
    73  	// Case 4: User omitted the flag; set a sane default
    74  
    75  	// ** DOCKER_HOST cases **
    76  	// Case A: DOCKER_HOST is set; use it, i.e. do nothing
    77  	// Case B: DOCKER_HOST is empty; use sane defaults
    78  
    79  	// Set host for sanity's sake, when the socket isn't useful
    80  	if !hasDockerHost && (socketHost.Socket == "-" || !isDockerHostURI(socketHost.Socket) || socketHost.Socket == "") {
    81  		// Cases: 1B, 2B, 4B
    82  		socket, found := socketLocation()
    83  		socketHost.Host = socket
    84  		hasDockerHost = found
    85  	}
    86  
    87  	// A - (dash) in socketHost.Socket means don't mount, preserve this value
    88  	// otherwise if socketHost.Socket is a filepath don't use it as socket
    89  	// Exit early if we're in an invalid state (e.g. when no DOCKER_HOST and user supplied "-", a dash or omitted)
    90  	if !hasDockerHost && socketHost.Socket != "" && !isDockerHostURI(socketHost.Socket) {
    91  		// Cases: 1B, 2B
    92  		// Should we early-exit here, since there is no host nor socket to talk to?
    93  		return SocketAndHost{}, fmt.Errorf("DOCKER_HOST was not set, couldn't be found in the usual locations, and the container daemon socket ('%s') is invalid", socketHost.Socket)
    94  	}
    95  
    96  	// Default to DOCKER_HOST if set
    97  	if socketHost.Socket == "" && hasDockerHost {
    98  		// Cases: 4A
    99  		log.Debugf("Defaulting container socket to DOCKER_HOST")
   100  		socketHost.Socket = socketHost.Host
   101  	}
   102  	// Set sane default socket location if user omitted it
   103  	if socketHost.Socket == "" {
   104  		// Cases: 4B
   105  		socket, _ := socketLocation()
   106  		// socket is empty if it isn't found, so assignment here is at worst a no-op
   107  		log.Debugf("Defaulting container socket to default '%s'", socket)
   108  		socketHost.Socket = socket
   109  	}
   110  
   111  	// Exit if both the DOCKER_HOST and socket are fulfilled
   112  	if hasDockerHost {
   113  		// Cases: 1A, 2A, 3A, 4A
   114  		if !isDockerHostURI(socketHost.Socket) {
   115  			// Cases: 1A, 2A
   116  			log.Debugf("DOCKER_HOST is set, but socket is invalid '%s'", socketHost.Socket)
   117  		}
   118  		return socketHost, nil
   119  	}
   120  
   121  	// Set a sane DOCKER_HOST default if we can
   122  	if isDockerHostURI(socketHost.Socket) {
   123  		// Cases: 3B
   124  		log.Debugf("Setting DOCKER_HOST to container socket '%s'", socketHost.Socket)
   125  		socketHost.Host = socketHost.Socket
   126  		// Both DOCKER_HOST and container socket are valid; short-circuit exit
   127  		return socketHost, nil
   128  	}
   129  
   130  	// Here there is no DOCKER_HOST _and_ the supplied container socket is not a valid URI (either invalid or a file path)
   131  	// Cases: 2B <- but is already handled at the top
   132  	// I.e. this path should never be taken
   133  	return SocketAndHost{}, fmt.Errorf("no DOCKER_HOST and an invalid container socket '%s'", socketHost.Socket)
   134  }