Skip to content

core: make SSH_AUTH_SOCK absolute + handle shell expansion in SSH_AUTH_SOCK env var - #8365

Merged
sipsma merged 6 commits into
dagger:mainfrom
grouville:fix-expansion-ssh
Sep 17, 2024
Merged

sipsma merged 6 commits into
dagger:mainfrom
grouville:fix-expansion-ssh

Conversation

@grouville

@grouville grouville commented Sep 7, 2024 •

Copy link
Copy Markdown
Member

Fixes #8337 AND fixes some relative path issues

This PR addresses issues with SSH_AUTH_SOCK handling and improves test coverage:

  1. Expands shell variables in SSH_AUTH_SOCK path
  2. Make SSH_AUTH_SOCK path absolute
  3. Adds integration tests for shell expansion handling and relative path calls

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

@grouville grouville changed the title core: make SSH_AUTH_SOCK absolute + handle shell expansion in SSH_AUTH_SOCK env var core: make SSH_AUTH_SOCK absolute + handle shell expansion in SSH_AUTH_SOCK env var Sep 7, 2024
@grouville

grouville commented Sep 7, 2024 •

Copy link
Copy Markdown
Member Author

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 ?

Error: failed to get configured module: failed to get module ref kind: input: moduleSource resolve: failed to resolve git src to commit: failed to load cache key: NotFound: NotFound: rpc error: code = NotFound desc = socket /.ssh-sock not found: stat /tmp/rootfs1116108047/.ssh-sock: no such file or directory

@grouville

grouville commented Sep 11, 2024 •

Copy link
Copy Markdown
Member Author

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):

  1. New inputs from users + further investigations do confirm that the socket path cannot be stored relatively as it is right now. The relative path is not handled by UnixPathMapper at all (and confirms why the isAbs check is present when entring this function)

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
When we collect the client metadata, currently, the LexicalRelativePath function is used to generate a relative path from the current workdir (which is the workdir of dagger CLI when calling a dagger command) to the value of the SSH_AUTH_SOCK. Upon tests, I confirm that the paths returned by LexicalRelativePath are totally correct.
We store this computed relative path inside the clientMetadata into the clientMetadata.sshAuthSockPath field. One important thing to note is that this clientMetadata is passed via the context and have helper functions (setters and getters). It is used a bit everywhere in the code. This is our source of truth to locate the path of the socket when the engine will want to request it to the client, usually in a sshforward context.
But, for a reason explained right below, it occurs that relative path never worked, so, how did it bypass my / our tests ?

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 ../../.. are stripped, generating a valid absolute path: /var/run/sockXXXX.agent.

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

  1. The serialization bug is not fixed in the current state of the PR, but will be in a follow-up as it is more of an edge case.

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.
Logic:

  1. When the engine needs to have access to a forwarded socket, it sends a request to the client to do a checkAgent at a given path

  2. When doing this request, we rely on a url.URL type to encode and transmit this information. Why ? Because the path could represent 2 types of sockets: unix and tcp

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.

  1. This bug is subtle and is probably the reason why we have some flaky SSH path issues in the CI (very high odds). This is the bug that currently makes this PR red, and is where I need some external help

Some context:
This PR adds a test suite of all the encountered bugs by users as of now (apart the space encoding). When reproducing them locally, it works BUT, inside the test suite it bugs. Why ?

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
This setupNestedClient also sets up the unixPathMapper function, whose role is to map the path of the socket to a valid path in a nested context

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)
Now, the current implementation (which is in setupNestedClient) works a bit differently:

It does not collect this SSHAuthSockPath from the clientMetadata but retrieves the value of SSH_AUTH_SOCK present inside the execState
It passes it to the nested engine with w.execMD.SSHAuthSockPath (which is the correct way to forward it to the nested engine)

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 ?

cc @tiborvass @marcosnils

@gerhard gerhard left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. WDYT @jedevc ?


The only thing missing is the changelog fragment, but we can add that part of preparing the final release notes, no need to force another re-run just because of those.

@gerhard gerhard added this to the v0.13.0 milestone Sep 11, 2024
@grouville

grouville commented Sep 11, 2024 •

Copy link
Copy Markdown
Member Author

The nested buildkit path expansion is not fixed in current state of pr, just trying to fit in the release. Will be a follow-up

@gerhard gerhard modified the milestones: v0.13.0, v0.13.1 Sep 11, 2024
Comment thread engine/client/pathutil.go Outdated
return "", nil
}

// the ~ expansion is a unix convention

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expandHomeDir already exists - we should make that a public function and use that instead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure 👍

Comment thread engine/client/client.go Outdated
}
}

expandedPath, err := expandPath(sshAuthSock)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we now not do the opposite, and force this to be absolute?

@sipsma sipsma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed one thing that should be an easy fix, otherwise LGTM!

Comment thread engine/buildkit/executor_spec.go Outdated
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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected, thanks 🙏

grouville and others added 6 commits September 17, 2024 13:48
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]>
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]>
@sipsma
sipsma merged commit 7663d10 into dagger:main Sep 17, 2024
grouville pushed a commit to grouville/dagger that referenced this pull request Sep 23, 2024
These have been fixed by dagger#8365

Signed-off-by: Guillaume de Rouville <[email protected]>
jedevc pushed a commit that referenced this pull request Sep 24, 2024
These have been fixed by #8365

Signed-off-by: Guillaume de Rouville <[email protected]>
Co-authored-by: Guillaume de Rouville <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

core(private-modules): SSH_AUTH_SOCK env loading seems to break with shell expansion characters

4 participants