Skip to content

Implement a TCP-based trampoline mechanism - #11510

Merged
sergiou87 merged 19 commits into
developmentfrom
trampoline-server-playground
Feb 17, 2021
Merged

sergiou87 merged 19 commits into
developmentfrom
trampoline-server-playground

Conversation

@sergiou87

Copy link
Copy Markdown
Member

Description

This PR adds support for a replacement of our current GIT_ASKPASS trampoline script, that not only is a bit slow, but also causes some issues on Windows.

With this new trampoline, we get replace the old script with:

  1. A generic "data forwarder" that is invoked by git, takes the command line arguments passed by git and the relevant environment variables and sends them to the GitHub Desktop app. This is the trampoline client.
  2. Specific "command handlers" that run inside and are part of the GitHub Desktop app. These will compute an output that will be sent back to the trampoline client, which will print it in stdout for git to consume it.

image

Trampoline server

Regarding the server side of this trampoline mechanism:

  • Starts listening to incoming TCP connections lazily. This not only makes sure the server is not running until it's needed, but it also makes easier handling connection errors: if, for some reason, the server fails and shuts itself down, it will be automagically restarted the next time it's needed.
    Right now, "it's needed" means whenever the user performs a remote git operation that needs authentication (i.e. that needs GIT_ASKPASS).
  • The port used to listen for these TCP connections is chosen by the OS. There is no hardcoded port to prevent running into EADDRINUSE errors when the port is already in use.
  • In order to increase security, the server side will generate short-lived tokens that will be used to make sure no third party binary is trying to impersonate the trampoline. These tokens will be passed via environment variable (DESKTOP_TRAMPOLINE_TOKEN) to the trampoline client, which will have to send it back to the server along with the rest of the info. The server will verify the token, compute the command output, and then revoke the token so it can't be reused.

Trampoline client

The trampoline client basically receives the server port in a DESKTOP_PORT environment variable, connects to that TCP port, and then sends the following (in the same order):

  1. The number of command line arguments (argc - 1).
  2. The command line arguments (argv, except argv[0] which is the program name).
  3. The number of valid environment variables to send.
  4. The (valid) environment variables found in the previous step.

After that, the client will read all the data coming from the server until the connection is closed. Whatever is read at this point will be printed in stdout, so git can consume it.

For a more exhaustive review of this work, I'd recommend to take a look at the specific desktop-trampoline implementation, more specifically the files in https://github.com/desktop/desktop-trampoline/tree/main/src

Release notes

Notes: [Improved] Remote Git operations are now faster and less prone to errors on Windows.

Comment thread app/src/lib/git/branch.ts Outdated
env: await envForRemoteOperation(account, remoteUrl),
expectedErrors: new Set<DugiteError>([DugiteError.BranchDeletionFailed]),
})
const result = await withTrampolineEnvForRemoteOperation(

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.

All these files under app/src/lib/git just change a bit because of a design decision: just like we do in other parts of the app (withPushPullFetch or withIsCommitting), withTrampolineEnvForRemoteOperation will wrap a git operation that might need to use the trampoline. The purpose of withTrampolineEnvForRemoteOperation is to generate a trampoline token before invoking git, and revoking it right after that.

env => {
return git(args, repository.path, 'checkoutBranch', {
...opts,
env: merge(opts.env, env),

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.

Commands that also have a progressCallback need this additional step of merging the env provided by withTrampolineEnvForRemoteOperation and the one coming from that progress callback.

}

return undefined
}

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.

Comment thread script/build.ts
desktopTrampolineFile
),
path.resolve(desktopTrampolineDir, desktopTrampolineFile)
)

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.

This snippet here makes sure the desktop-trampoline binary is copied from the node_modules folder to the app bundle.

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.

Will the exe binary need to be signed on Windows in order to be operational?

@sergiou87 sergiou87 Feb 1, 2021 •

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.

Not sure if it's necessary, but definitely something we want (and it's required on macOS). It's in my TODO list for tomorrow 😄

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.

FYI @mislav I can confirm the new binary is signed for both macOS and Windows without any extra effort 😌 🎉

@sergiou87
sergiou87 marked this pull request as ready for review February 1, 2021 17:39
@sergiou87
sergiou87 requested a review from tidy-dev February 1, 2021 17:39
this.close()
})

this.server.listen(0, '127.0.0.1', async () => {

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.

Will the TCP server be accessible to other computers on the network?

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.

We definitely don't want it to be, and that '127.0.0.1' should restrict it to connections in the same machine (or that's my hope, at least 😂 ).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Let me test and see if it's really keeping the connections to local or if I can use vpn from my company to connect

@sergiou87
sergiou87 force-pushed the trampoline-server-playground branch 2 times, most recently from 367c243 to 0e3874d Compare February 2, 2021 11:39
@sergiou87
sergiou87 force-pushed the trampoline-server-playground branch from 2b19b62 to 636fbb0 Compare February 2, 2021 12:37
tidy-dev
tidy-dev previously approved these changes Feb 2, 2021

@tidy-dev tidy-dev 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.

Looks great! ✨

It basically prevents forwarding the DESKTOP_PORT env variable, since it's not necessary.
@sergiou87
sergiou87 dismissed a stale review via cec4e42 February 17, 2021 10:43
@sergiou87
sergiou87 requested a review from tidy-dev February 17, 2021 10:49
@sergiou87

Copy link
Copy Markdown
Member Author

@tidy-dev I updated the branch with the latest development and also bumped desktop-trampoline to v0.9.2 to include some safe guard options: desktop/desktop-trampoline#1

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.

4 participants