Repository navigation
core: make SSH_AUTH_SOCK absolute + handle shell expansion in SSH_AUTH_SOCK env var - #8365
Conversation
SSH_AUTH_SOCK absolute + handle shell expansion in SSH_AUTH_SOCK env var
|
I am actually super confused about this failure. Unable to repro locally, when running the test alone nor the TestModule suite in parallel It seems like a race, but happening on the nested exec, where the path is given by UnixPathMapper but seems to be not mounted at that given time. I don't really know how to debug these nested exec rootfs states, any idea ? |
12e2e8f to
8d9ef1c
Compare
|
So, the PR correctly fixes several bugs encountered regarding the socket path (shell expansion AND relative path issues). It just needs ONE change to be all green on the test-suite, but the logic is already here. I need some help on finding the right primitive to unlock last point though, which only touches nested executions Here’s the full context dump (other issues + the current one that the issue faces):
Given these informations, relative paths never worked, but I never saw it in my tests, nor on the first use. Some context: When running a dagger command, the client metadata is collected and sent to the engine Basically, the way I tested the setup was to rely on the path generated by my ssh-agent, which is generally located in the /var/run/sockXXXX.agent. Meaning that when I was testing, I was testing in the HOME or /tmp or any OTHER path than /var/run. This led to the generation of a path such as ../../../var/run/sockXXXX.agent Due to another serialization bug explained in the PR context above (or in the second point of the list of issues), when the server requests to the client, all the This is why only a handful of users are complaining at the moment: it is mostly users that use 1password to authenticate the socket or all the users that setup symlinks who generally have a location of the socket which is inside the HOME dir or in any subdirectories of HOME, leading to incomplete relative paths such as: ./subdir/sockXXXX.agent which becomes /subdir/sockXXXX.agent which is not valid when checking if the agent is present
Following a conversation / debug with a user today, it has been confirmed that we need to url.EscapePath the unix Path to avoid URL encoding spaces and special chars as URL chars (in the unix context). (Today, a user had its path with a space inside being converted to %20). Links are also present in the readme / first comment of the PR, to see where it is located in the codebase. This is a bug that was present since the creation of the core.Socket type, I am surprised that no one encountered it before.
However, when url.URL encodes it and when we decode it client-side with url.Parse, the unix path is cleaned, meaning that we lose all those ../.. AND we transform all the special chars as URL encoded chars, leading to those bugs in the UNIX context. This will be part of a second PR, as a follow-up, as it is a bit less critical.
Some context: Logic: There is a setupNestedClient function that sets everything for the nested execution. Inside, we forward the value of the SSH_AUTH_SOCK to the nested engine I just said above that the setupNestedClient « forwards » the value of the SSH_AUTH_SOCK , let’s focus a bit on it as this is where the bug occurs: In an ideal world, what we want is for the expanded value of SSH_AUTH_SOCK to be passed down to the nested engine. Basically, what I would expect is for the value of clientMetadata.SSHAuthSockPath to be passed down as the SSH_AUTH_SOCK of the nested engine) It does not collect this SSHAuthSockPath from the clientMetadata but retrieves the value of SSH_AUTH_SOCK present inside the execState However, it turns out that this SSH_AUTH_SOCK value, when retrieved from the execState, equals to the value of the SSH_AUTH_SOCK when the client did the os.GetEnv(« SSH_AUTH_SOCK »), so even before being transformed or made relative by LexicalRelativePath. I am not a BuildKit expert, but some testing confirmed that the seems to be only collected once by the client (at this os.getenv call). What is weirder is that I send the clientMetadata with the expanded value to the engine, so I don’t really know where this execState does collect it from (apart a cache from the os.getenv call from the client ???) Nonetheless, the fix is theoretically simple: we need to not rely on the execState to fix it but we need to forward to the nested engine the value of the clientMetadata.SSHAuthSocketPath value to the nested engine My problem is that I haven’t been able to retrieve it in this context; Any idea ? |
8d9ef1c to
aa4815a
Compare
|
|
| return "", nil | ||
| } | ||
|
|
||
| // the ~ expansion is a unix convention |
There was a problem hiding this comment.
expandHomeDir already exists - we should make that a public function and use that instead.
| } | ||
| } | ||
|
|
||
| expandedPath, err := expandPath(sshAuthSock) |
There was a problem hiding this comment.
Should we now not do the opposite, and force this to be absolute?
aa4815a to
1d49ec8
Compare
sipsma
left a comment
There was a problem hiding this comment.
Noticed one thing that should be an easy fix, otherwise LGTM!
| if v, ok := state.origEnvMap["SSH_AUTH_SOCK"]; ok { | ||
| w.execMD.SSHAuthSocketPath = v | ||
| if sockPath, ok := state.origEnvMap["SSH_AUTH_SOCK"]; ok { | ||
| if homeDir, ok := state.origEnvMap["HOME"]; ok { |
There was a problem hiding this comment.
This will result in SSH_AUTH_SOCK only working if HOME is set, even if the path doesn't have a ~ in it, right? It seems like HOME should only matter if there's a ~ in the SSH_AUTH_SOCK path.
Implementation: expand shell variables on SSH_AUTH_SOCK Signed-off-by: Guillaume de Rouville <[email protected]>
Users have been complaining of some parsing issues regarding the SSH_AUTH_SOCK. After some digging in the codebase, it seems better not to handle it relatively: 1. engine side, we URL encode https://github.com/dagger/dagger/blob/main/core/socket.go#L174-L189 the socket Path On the client side, we decode it with url.Parse() https://github.com/dagger/dagger/blob/main/engine/client/socket.go#L40 and retrieve the u.Path https://github.com/dagger/dagger/blob/main/engine/client/socket.go#L46 This operation is destructive of all relative Path: ../.test/socket becomes /.test/socket 2. In order to handle nested execs, we rely on UnixPathMapper https://github.com/dagger/dagger/blob/main/engine/client/socket.go#L48 to retrieve the socket at the path inside the rootfs. The current implementation does not seem to expect relative paths: https://github.com/dagger/dagger/blob/main/engine/buildkit/executor_spec.go#L948-L951 Signed-off-by: Guillaume de Rouville <[email protected]>
As the new ref scheme added a host dependency towards this env var, integration tests are becoming necessary to assert proper handling on our side. The first test ensures proper handling of shell expansion. This was requested by some users. Note: SSH itself doesn't seem to support it, but some tools seem to export the SSH_AUTH_SOCK with the ~. At the same time, it adds some integration tests for relative calls, to more easily handle relative path back Signed-off-by: Guillaume de Rouville <[email protected]>
Signed-off-by: Guillaume de Rouville <[email protected]>
Fixes https://docs.dagger.io/manuals/user/remote-repositories/#environment-variables-are-not-automatically-expanded and https://docs.dagger.io/manuals/user/remote-repositories/#ssh_auth_sock-paths-are-not-always-handled-correctl Signed-off-by: Guillaume de Rouville <[email protected]>
Only expand SSH_AUTH_SOCK path when it starts with a tilde (~). This allows absolute paths to work correctly without depending on HOME being set, fixing an unnecessary restriction in the previous implementation. Signed-off-by: Guillaume de Rouville <[email protected]>
1d49ec8 to
8816785
Compare
These have been fixed by dagger#8365 Signed-off-by: Guillaume de Rouville <[email protected]>
These have been fixed by #8365 Signed-off-by: Guillaume de Rouville <[email protected]> Co-authored-by: Guillaume de Rouville <[email protected]>
Fixes #8337 AND fixes some relative path issues
This PR addresses issues with SSH_AUTH_SOCK handling and improves test coverage:
https://discord.com/channels/707636530424053791/1281294189463732244/1281294189463732244
Detailed explanation for not making SSH_AUTH_SOCK path relative:
The decision to avoid making the SSH_AUTH_SOCK path relative is based on several factors:
Users have been complaining of some parsing issues regarding the SSH_AUTH_SOCK. After some digging in the codebase, it seems better not to handle it relatively:
1. Client - Host interaction
On the engine side, we URL encode the socket Path in order to forward it from the client to the engine
On the client side, we decode it with url.Parse() and retrieve the u.Path
This operation is destructive of all relative Path: ../.test/socket becomes /.test/socket
This leads to some bugs such as the one the user got in the thread
2. Nested execs
In order to handle nested execs, we rely on UnixPathMapper to retrieve the socket at the path inside the rootfs.
The current implementation does not seem to expect relative paths
cc @jedevc I am not sure about the UnixPathMapper implementation. This was the main reason I preferred to rollback to an absolute path. The first implementation relied on encoding / decoding the path with
url.PathEscape(): https://go.dev/play/p/hj8PeEwtUfL. I also added a test suite to test those relative path handling, in case UnixPathMapper is not necessarily expecting an absolute path