Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When SSH_AUTH_SOCK is set to a socket on the host, forward that to the VM. #64

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions environment/container/docker/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package docker
import (
_ "embed"
"fmt"
"github.com/abiosoft/colima/config"
"github.com/abiosoft/colima/util"
"os"
"path/filepath"

"github.com/abiosoft/colima/config"
"github.com/abiosoft/colima/util"
)

//go:embed socket.sh
Expand All @@ -31,10 +32,16 @@ func createSocketForwardingScript(vmUser string, sshPort int) error {

// write socket script to file
var values = struct {
SocketFile string
SSHPort int
VMUser string
}{SocketFile: socketSymlink(), SSHPort: sshPort, VMUser: vmUser}
SocketFile string
SSHPort int
VMUser string
SSHAuthSock string
}{
SocketFile: socketSymlink(),
SSHPort: sshPort,
VMUser: vmUser,
SSHAuthSock: os.Getenv("SSH_AUTH_SOCK"),
}

err := util.WriteTemplate(socketForwardingScript, scriptFile, values)
if err != nil {
Expand Down
134 changes: 125 additions & 9 deletions environment/container/docker/socket.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,126 @@
#!/usr/bin/env bash
rm -rf "{{.SocketFile}}"
ssh -p "{{.SSHPort}}" \
-l "{{.VMUser}}" \
-i ~/.lima/_config/user \
-o IdentitiesOnly=yes \
-F /dev/null \
-o NoHostAuthenticationForLocalhost=yes \
-L "{{.SocketFile}}:/var/run/docker.sock" \
-N "127.0.0.1"
#
# Forwards docker socket from the VM to the host and, when valid, {{.SSHAuthSock}} to /run/host-services/ssh-auth.sock

#######################################
# Determine whether {{.SSHAuthSock}} is set and pointing to a socket.
# Globals:
# None
# Arguments:
# None
# Returns: failure when {{.SSHAuthSock}} is either not set or not a socket.
#######################################
function ssh_auth_socket_is_valid() {
if [[ -z "{{.SSHAuthSock}}" || ! -S "{{.SSHAuthSock}}" ]]; then
return 1
fi
}

#######################################
# Prepare local sockets
# Globals:
# None
# Arguments:
# None
#######################################
function prep_local_sockets() {
rm -rf "{{.SocketFile}}"
}

#######################################
# Remove remote host_services directory
# Globals:
# None
# Arguments:
# None
#######################################
function rm_remote_host_services() {
if ! ssh_auth_socket_is_valid; then
return
fi

local -a ssh_cmd=(
ssh -p "{{.SSHPort}}"
-l "{{.VMUser}}"
-i ~/.lima/_config/user
-o IdentitiesOnly=yes
-F /dev/null
-o NoHostAuthenticationForLocalhost=yes
"127.0.0.1"
sudo rm -rf /var/run/host-services
)
"${ssh_cmd[@]}"
}

#######################################
# Install the remote host_services directory
# Globals:
# None
# Arguments:
# None
#######################################
function install_remote_host_services() {
if ! ssh_auth_socket_is_valid; then
return
fi

local -a ssh_cmd=(
ssh -p "{{.SSHPort}}"
-l "{{.VMUser}}"
-i ~/.lima/_config/user
-o IdentitiesOnly=yes
-F /dev/null
-o NoHostAuthenticationForLocalhost=yes
"127.0.0.1"
sudo install -d -o "{{.VMUser}}" /var/run/host-services
)
"${ssh_cmd[@]}"
}

#######################################
# Ensures that the remote directory for receiving the {{.SSHAuthSock}} is prepared (owned by $USER)
# Globals:
# None
# Arguments:
# None
#######################################
function prep_remote_sockets() {
rm_remote_host_services
install_remote_host_services
}

#######################################
# Forward sockets docker -> local and auth -> remote
# Globals:
# None
# Arguments:
# None
#######################################
function forward_sockets() {
local -a ssh_cmd=(
ssh -p "{{.SSHPort}}"
-l "{{.VMUser}}"
-i ~/.lima/_config/user
-o IdentitiesOnly=yes
-F /dev/null
-o NoHostAuthenticationForLocalhost=yes
-L "{{.SocketFile}}:/var/run/docker.sock"
)
if ssh_auth_socket_is_valid; then
ssh_cmd+=(
-R "/run/host-services/ssh-auth.sock:{{.SSHAuthSock}}"
)
fi

exec "${ssh_cmd[@]}" -N "127.0.0.1"
}

function main() {
prep_local_sockets
prep_remote_sockets
forward_sockets
}

if ((${#BASH_SOURCE[@]} == 1)); then
main "$@"
fi