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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to start & stop ssh-agent process #16761

Merged
merged 6 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/out/register"
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/minikube/sshagent"
"k8s.io/minikube/pkg/minikube/style"
)

Expand Down Expand Up @@ -93,6 +94,9 @@ var hostAndDirsDeleter = func(api libmachine.API, cc *config.ClusterConfig, prof
if err := killMountProcess(); err != nil {
out.FailureT("Failed to kill mount process: {{.error}}", out.V{"error": err})
}
if err := sshagent.Stop(profileName); err != nil {
out.FailureT("Failed to stop ssh-agent process: {{.error}}", out.V{"error": err})
}

deleteHosts(api, cc)

Expand Down
87 changes: 62 additions & 25 deletions cmd/minikube/cmd/docker-env.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,36 @@ var dockerEnvTCPTmpl = fmt.Sprintf(
"{{ if .NoProxyVar }}"+
"{{ .Prefix }}{{ .NoProxyVar }}{{ .Delimiter }}{{ .NoProxyValue }}{{ .Suffix }}"+
"{{ end }}"+
"{{ if .SSHAuthSock }}"+
"{{ .Prefix }}%s{{ .Delimiter }}{{ .SSHAuthSock }}{{ .Suffix }}"+
"{{ end }}"+
"{{ if .SSHAgentPID }}"+
"{{ .Prefix }}%s{{ .Delimiter }}{{ .SSHAgentPID }}{{ .Suffix }}"+
"{{ end }}"+
"{{ .UsageHint }}",
constants.DockerTLSVerifyEnv,
constants.DockerHostEnv,
constants.DockerCertPathEnv,
constants.ExistingDockerTLSVerifyEnv,
constants.ExistingDockerHostEnv,
constants.ExistingDockerCertPathEnv,
constants.MinikubeActiveDockerdEnv)
constants.MinikubeActiveDockerdEnv,
constants.SSHAuthSock,
constants.SSHAgentPID)
var dockerEnvSSHTmpl = fmt.Sprintf(
spowelljr marked this conversation as resolved.
Show resolved Hide resolved
"{{ .Prefix }}%s{{ .Delimiter }}{{ .DockerHost }}{{ .Suffix }}"+
"{{ .Prefix }}%s{{ .Delimiter }}{{ .MinikubeDockerdProfile }}{{ .Suffix }}"+
"{{ if .SSHAuthSock }}"+
"{{ .Prefix }}%s{{ .Delimiter }}{{ .SSHAuthSock }}{{ .Suffix }}"+
"{{ end }}"+
"{{ if .SSHAgentPID }}"+
"{{ .Prefix }}%s{{ .Delimiter }}{{ .SSHAgentPID }}{{ .Suffix }}"+
"{{ end }}"+
"{{ .UsageHint }}",
constants.DockerHostEnv,
constants.MinikubeActiveDockerdEnv)
constants.MinikubeActiveDockerdEnv,
constants.SSHAuthSock,
constants.SSHAgentPID)

// DockerShellConfig represents the shell config for Docker
type DockerShellConfig struct {
Expand All @@ -99,6 +115,9 @@ type DockerShellConfig struct {
ExistingDockerCertPath string
ExistingDockerHost string
ExistingDockerTLSVerify string

SSHAuthSock string
SSHAgentPID string
}

var (
Expand Down Expand Up @@ -142,6 +161,9 @@ func dockerShellCfgSet(ec DockerEnvConfig, envMap map[string]string) *DockerShel

s.MinikubeDockerdProfile = envMap[constants.MinikubeActiveDockerdEnv]

s.SSHAuthSock = envMap[constants.SSHAuthSock]
s.SSHAgentPID = envMap[constants.SSHAgentPID]

if ec.noProxy {
noProxyVar, noProxyValue := defaultNoProxyGetter.GetNoProxyVar()

Expand Down Expand Up @@ -316,18 +338,20 @@ docker-cli install instructions: https://minikube.sigs.k8s.io/docs/tutorials/doc

hostIP := co.CP.IP.String()
ec := DockerEnvConfig{
EnvConfig: sh,
profile: cname,
driver: driverName,
ssh: sshHost,
hostIP: hostIP,
port: port,
certsDir: localpath.MakeMiniPath("certs"),
noProxy: noProxy,
username: d.GetSSHUsername(),
hostname: hostname,
sshport: sshport,
keypath: d.GetSSHKeyPath(),
EnvConfig: sh,
profile: cname,
driver: driverName,
ssh: sshHost,
hostIP: hostIP,
port: port,
certsDir: localpath.MakeMiniPath("certs"),
noProxy: noProxy,
username: d.GetSSHUsername(),
hostname: hostname,
sshport: sshport,
keypath: d.GetSSHKeyPath(),
sshAuthSock: co.Config.SSHAuthSock,
sshAgentPID: co.Config.SSHAgentPID,
}

dockerPath, err := exec.LookPath("docker")
Expand Down Expand Up @@ -371,17 +395,19 @@ docker-cli install instructions: https://minikube.sigs.k8s.io/docs/tutorials/doc
// DockerEnvConfig encapsulates all external inputs into shell generation for Docker
type DockerEnvConfig struct {
shell.EnvConfig
profile string
driver string
ssh bool
hostIP string
port int
certsDir string
noProxy bool
username string
hostname string
sshport int
keypath string
profile string
driver string
ssh bool
hostIP string
port int
certsDir string
noProxy bool
username string
hostname string
sshport int
keypath string
sshAuthSock string
sshAgentPID int
}

// dockerSetScript writes out a shell-compatible 'docker-env' script
Expand Down Expand Up @@ -497,11 +523,18 @@ func sshURL(username string, hostname string, port int) string {

// dockerEnvVars gets the necessary docker env variables to allow the use of minikube's docker daemon
func dockerEnvVars(ec DockerEnvConfig) map[string]string {
agentPID := strconv.Itoa(ec.sshAgentPID)
// set agentPID to nil value if not set
if agentPID == "0" {
agentPID = ""
}
envTCP := map[string]string{
constants.DockerTLSVerifyEnv: "1",
constants.DockerHostEnv: dockerURL(ec.hostIP, ec.port),
constants.DockerCertPathEnv: ec.certsDir,
constants.MinikubeActiveDockerdEnv: ec.profile,
constants.SSHAuthSock: ec.sshAuthSock,
constants.SSHAgentPID: agentPID,
}
envSSH := map[string]string{
constants.DockerHostEnv: sshURL(ec.username, ec.hostname, ec.sshport),
Expand Down Expand Up @@ -532,6 +565,8 @@ func dockerEnvNames(ec DockerEnvConfig) []string {
constants.DockerHostEnv,
constants.DockerCertPathEnv,
constants.MinikubeActiveDockerdEnv,
constants.SSHAuthSock,
constants.SSHAgentPID,
}

if ec.noProxy {
Expand All @@ -550,6 +585,8 @@ func dockerEnvVarsList(ec DockerEnvConfig) []string {
fmt.Sprintf("%s=%s", constants.DockerHostEnv, dockerURL(ec.hostIP, ec.port)),
fmt.Sprintf("%s=%s", constants.DockerCertPathEnv, ec.certsDir),
fmt.Sprintf("%s=%s", constants.MinikubeActiveDockerdEnv, ec.profile),
fmt.Sprintf("%s=%s", constants.SSHAuthSock, ec.sshAuthSock),
fmt.Sprintf("%s=%d", constants.SSHAgentPID, ec.sshAgentPID),
}
}

Expand Down
48 changes: 43 additions & 5 deletions cmd/minikube/cmd/docker-env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export MINIKUBE_ACTIVE_DOCKERD="dockerdriver"
unset DOCKER_HOST;
unset DOCKER_CERT_PATH;
unset MINIKUBE_ACTIVE_DOCKERD;
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
`,
nil,
},
Expand All @@ -81,6 +83,8 @@ export MINIKUBE_ACTIVE_DOCKERD="dockerdriver"
unset DOCKER_HOST;
unset DOCKER_CERT_PATH;
unset MINIKUBE_ACTIVE_DOCKERD;
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
`,
nil,
},
Expand All @@ -101,6 +105,8 @@ export MINIKUBE_ACTIVE_DOCKERD="bash"
unset DOCKER_HOST;
unset DOCKER_CERT_PATH;
unset MINIKUBE_ACTIVE_DOCKERD;
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
`,
nil,
},
Expand All @@ -121,6 +127,8 @@ export MINIKUBE_ACTIVE_DOCKERD="ipv6"
unset DOCKER_HOST;
unset DOCKER_CERT_PATH;
unset MINIKUBE_ACTIVE_DOCKERD;
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
`,
nil,
},
Expand All @@ -141,6 +149,8 @@ set -gx MINIKUBE_ACTIVE_DOCKERD "fish";
set -e DOCKER_HOST;
set -e DOCKER_CERT_PATH;
set -e MINIKUBE_ACTIVE_DOCKERD;
set -e SSH_AUTH_SOCK;
set -e SSH_AGENT_PID;
`,
nil,
},
Expand All @@ -161,6 +171,8 @@ $Env:MINIKUBE_ACTIVE_DOCKERD = "powershell"
Remove-Item Env:\\DOCKER_HOST
Remove-Item Env:\\DOCKER_CERT_PATH
Remove-Item Env:\\MINIKUBE_ACTIVE_DOCKERD
Remove-Item Env:\\SSH_AUTH_SOCK
Remove-Item Env:\\SSH_AGENT_PID
`,
nil,
},
Expand All @@ -181,6 +193,8 @@ REM @FOR /f "tokens=*" %i IN ('minikube -p cmd docker-env --shell cmd') DO @%i
SET DOCKER_HOST=
SET DOCKER_CERT_PATH=
SET MINIKUBE_ACTIVE_DOCKERD=
SET SSH_AUTH_SOCK=
SET SSH_AGENT_PID=
`,
nil,
},
Expand All @@ -200,6 +214,8 @@ SET MINIKUBE_ACTIVE_DOCKERD=
(setenv "DOCKER_HOST" nil)
(setenv "DOCKER_CERT_PATH" nil)
(setenv "MINIKUBE_ACTIVE_DOCKERD" nil)
(setenv "SSH_AUTH_SOCK" nil)
(setenv "SSH_AGENT_PID" nil)
`,
nil,
},
Expand All @@ -222,6 +238,8 @@ export NO_PROXY="127.0.0.1"
unset DOCKER_HOST;
unset DOCKER_CERT_PATH;
unset MINIKUBE_ACTIVE_DOCKERD;
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
unset NO_PROXY;
`,
nil,
Expand All @@ -245,6 +263,8 @@ export no_proxy="127.0.0.1"
unset DOCKER_HOST;
unset DOCKER_CERT_PATH;
unset MINIKUBE_ACTIVE_DOCKERD;
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
unset no_proxy;
`,
nil,
Expand All @@ -267,6 +287,8 @@ $Env:no_proxy = "192.168.0.1"
Remove-Item Env:\\DOCKER_HOST
Remove-Item Env:\\DOCKER_CERT_PATH
Remove-Item Env:\\MINIKUBE_ACTIVE_DOCKERD
Remove-Item Env:\\SSH_AUTH_SOCK
Remove-Item Env:\\SSH_AGENT_PID
Remove-Item Env:\\no_proxy
`,
nil,
Expand All @@ -290,6 +312,8 @@ export NO_PROXY="192.168.0.1,10.0.0.4,127.0.0.1"
unset DOCKER_HOST;
unset DOCKER_CERT_PATH;
unset MINIKUBE_ACTIVE_DOCKERD;
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
unset NO_PROXY;
`,
nil,
Expand All @@ -308,23 +332,29 @@ MINIKUBE_ACTIVE_DOCKERD=noneshell
DOCKER_HOST
DOCKER_CERT_PATH
MINIKUBE_ACTIVE_DOCKERD
SSH_AUTH_SOCK
SSH_AGENT_PID
`,
nil,
},
{
"none",
"text",
DockerEnvConfig{profile: "nonetext", driver: "docker", hostIP: "127.0.0.1", port: 32842, certsDir: "/certs"},
DockerEnvConfig{profile: "nonetext", driver: "docker", hostIP: "127.0.0.1", port: 32842, certsDir: "/certs", sshAuthSock: "/var/folders/9l/6wpxv6wd1b901m1146r579wc00rqw3/T//ssh-KCQt1sNqrCPI/agent.29227", sshAgentPID: 29228},
nil,
`DOCKER_TLS_VERIFY=1
DOCKER_HOST=tcp://127.0.0.1:32842
DOCKER_CERT_PATH=/certs
MINIKUBE_ACTIVE_DOCKERD=nonetext
SSH_AUTH_SOCK=/var/folders/9l/6wpxv6wd1b901m1146r579wc00rqw3/T//ssh-KCQt1sNqrCPI/agent.29227
SSH_AGENT_PID=29228
`,
`DOCKER_TLS_VERIFY
DOCKER_HOST
DOCKER_CERT_PATH
MINIKUBE_ACTIVE_DOCKERD
SSH_AUTH_SOCK
SSH_AGENT_PID
`,
[]cmp.Option{
cmpopts.AcyclicTransformer("SplitLines", func(s string) []string {
Expand All @@ -338,19 +368,23 @@ MINIKUBE_ACTIVE_DOCKERD
{
"none",
"json",
DockerEnvConfig{profile: "nonejson", driver: "docker", hostIP: "127.0.0.1", port: 32842, certsDir: "/certs"},
DockerEnvConfig{profile: "nonejson", driver: "docker", hostIP: "127.0.0.1", port: 32842, certsDir: "/certs", sshAuthSock: "/var/folders/9l/6wpxv6wd1b901m1146r579wc00rqw3/T//ssh-KCQt1sNqrCPI/agent.29227", sshAgentPID: 29228},
nil,
`{
"DOCKER_TLS_VERIFY": "1",
"DOCKER_HOST": "tcp://127.0.0.1:32842",
"DOCKER_CERT_PATH": "/certs",
"MINIKUBE_ACTIVE_DOCKERD": "nonejson"
"MINIKUBE_ACTIVE_DOCKERD": "nonejson",
"SSH_AUTH_SOCK": "/var/folders/9l/6wpxv6wd1b901m1146r579wc00rqw3/T//ssh-KCQt1sNqrCPI/agent.29227",
"SSH_AGENT_PID": "29228"
}`,
`[
"DOCKER_TLS_VERIFY",
"DOCKER_HOST",
"DOCKER_CERT_PATH",
"MINIKUBE_ACTIVE_DOCKERD"
"MINIKUBE_ACTIVE_DOCKERD",
"SSH_AUTH_SOCK",
"SSH_AGENT_PID"
]`,
[]cmp.Option{
cmp.FilterValues(func(x, y string) bool {
Expand All @@ -367,17 +401,21 @@ MINIKUBE_ACTIVE_DOCKERD
{
"none",
"yaml",
DockerEnvConfig{profile: "noneyaml", driver: "docker", hostIP: "127.0.0.1", port: 32842, certsDir: "/certs"},
DockerEnvConfig{profile: "noneyaml", driver: "docker", hostIP: "127.0.0.1", port: 32842, certsDir: "/certs", sshAuthSock: "/var/folders/9l/6wpxv6wd1b901m1146r579wc00rqw3/T//ssh-KCQt1sNqrCPI/agent.29227", sshAgentPID: 29228},
nil,
`DOCKER_TLS_VERIFY: "1"
DOCKER_HOST: tcp://127.0.0.1:32842
DOCKER_CERT_PATH: /certs
MINIKUBE_ACTIVE_DOCKERD: noneyaml
SSH_AUTH_SOCK: /var/folders/9l/6wpxv6wd1b901m1146r579wc00rqw3/T//ssh-KCQt1sNqrCPI/agent.29227
SSH_AGENT_PID: "29228"
`,
`- DOCKER_TLS_VERIFY
- DOCKER_HOST
- DOCKER_CERT_PATH
- MINIKUBE_ACTIVE_DOCKERD
- SSH_AUTH_SOCK
- SSH_AGENT_PID
`,
[]cmp.Option{
cmpopts.AcyclicTransformer("ParseYAML", func(in string) (out interface{}) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/minikube/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ type ClusterConfig struct {
SocketVMnetClientPath string
SocketVMnetPath string
StaticIP string
SSHAuthSock string
SSHAgentPID int
}

// KubernetesConfig contains the parameters used to configure the VM Kubernetes.
Expand Down
4 changes: 4 additions & 0 deletions pkg/minikube/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ const (
// MinikubeActiveDockerdEnv holds the docker daemon which user's shell is pointing at
// value would be profile or empty if pointing to the user's host daemon.
MinikubeActiveDockerdEnv = "MINIKUBE_ACTIVE_DOCKERD"
// SSHAuthSock is used for docker-env
SSHAuthSock = "SSH_AUTH_SOCK"
// SSHAgentPID is used for docker-env
SSHAgentPID = "SSH_AGENT_PID"
// PodmanVarlinkBridgeEnv is used for podman settings
PodmanVarlinkBridgeEnv = "PODMAN_VARLINK_BRIDGE"
// PodmanContainerHostEnv is used for podman settings
Expand Down