Skip to content

Simplify and clarify how to open a GraphQL session #3830

Closed

Description

@shykes

Problem

In order for a client library or tool to communicate with the Dagger Engine, it must open a GraphQL session. When using a Dagger SDK this is abstracted away. However some developers need to target the GraphQL API without the help of an SDK. At the moment this is a painful experience because, although Dagger's GraphQL implementation is standard, the procedure for opening a session is specific to Dagger - and it is quite painful to understand and implement.

Specifically, the procedure for opening a graphql session has 3 problems:

  1. It's not documented: a client developer must ask questions and read other implementations to learn it
  2. It's misleading: learning the current interface creates misconceptions about Dagge's architecture.
  3. It's too much work to implement: when multiplied by many clients down the line, even small increments in implementation effort can make a difference.

People affected

As noted above:

some developers need to target the GraphQL API without the help of an SDK

This includes:

  • Developers using languages for which a Dagger SDK is not yet available
  • Developers of unofficial SDKs and exec wrappers
  • Contributors to Dagger SDKs
  • Contributors to the Dagger CLI, specifically the dagger run wrapper

Additionally, some people may want to understand the process for opening a GraphQL session, even if they themselves don't need to implement it. This includes:

  • Power users who wish to understand how to operate Dagger in depth before using it in production
  • People looking to setup Dagger in a client-server mode, and being misled by the $DAGGER_HOST environment variable used by the SDKs.

Solution

Define a new "Dagger Session Interface" (DSI?) that 1) is well documented, 2) helps understand Dagger's architecture, and 3) is as easy to implement as possible.

Design proposal

For a client to open a session to a Dagger Engine, it must implement the Dagger Session Interface (DSI). There are two levels of implementations for DSI: basic, and advanced.

DSI-basic: no bootstrap

DSI-basic defines the simplest possible way to open a session to a Dagger engine. It can typically be implemented in 10 lines of code or less. The tradeoff is that the client cannot be executed standalone: the operator must execute it in the context of a dagger run command. For example: dagger run python mytool.py, or dagger go run mypipeline.go.

To implement DSI-basic, follow the steps below:

graph TD;

subgraph DSI-basic
start[Start]
if_env_set(Are $DAGGER_SESSION_PORT and $DAGGER_SESSION_TOKEN set?)
env_set_connect[connect to tcp://localhost:$DAGGER_SESSION_PORT, as user $DAGGER_SESSION_TOKEN]
error[Error]
success[Success]
end

start --> if_env_set
if_env_set --yes--> env_set_connect --> success
if_env_set --no--> error
Loading

DSI-advanced: with bootstrap

DSI-advanced extends DSI-basic with a bootstrap feature: if a session is not already open, then DSI-advanced specifies how to automatically open one, with no additional dependencies. A client implementing DSI-advanced can be executed standalone: it does not need to be wrapped with dagger run, and the Dagger CLI does not need to be installed. The tradeoff is that DSI-advanced is more complex to implement.

graph TD;

subgraph DSI-advanced

subgraph DSI-basic
start[Start]
if_env_set(Are $DAGGER_SESSION_PORT and $DAGGER_SESSION_TOKEN set?)
env_set_connect[connect to tcp://localhost:$DAGGER_SESSION_PORT, as user $DAGGER_SESSION_TOKEN]
success[Success]
end

start --> if_env_set
if_env_set --yes--> env_set_connect --> success

if_cli_installed(Is Dagger CLI installed in $PATH?)
cli_auto_install[Download the latest dagger CLI]
cli_session[Execute 'dagger session']
cli_read_env[Read json-encoded environment object from standard output of 'dagger session', export to environment variables]

if_env_set --no--> if_cli_installed
if_cli_installed --yes--> cli_session --> cli_read_env --> env_set_connect
if_cli_installed --no--> cli_auto_install --> cli_session

end
Loading

Open questions & issues

DAGGER_SESSION_PORT or DAGGER_SESSION_URL?

Since the session must always be accessible as a tcp port on localhost only, there are two options for the client to determine the URL:

Option 1: _PORT Option 2: _URL
How to assemble session URL http://localhost:$DAGGER_SESSION_PORT $DAGGER_SESSION_URL
In current proposal? Yes No
Pros Makes it more clear that the session is local only; no confusion with $DAGGER_HOST; less opportunity for ill-advised hacks that will only end in tears Less characters to type for client developers; more flexibility for unknown future use cases
Cons More characters to type for client developers; less flexibility for unknown future use cases Less clear that the session is local only; confusion with $DAGGER_HOST; more opportunity for ill-advised hacks that will only end in tears

Which CLI to download

In DSI-advanced, the client must download the Dagger CLI. But which version?

  • Currently, clients in official SDKs specify an exact version
  • How does an official client developer decide when to upgrade the hardcoded version, and to what value?
  • What about everyone else? Should they also specify a version? If so, which version and how/when to change it? If not: then what?

Could dagger listen be folded into dagger session?

Currently these two commands are different (but hidden). They are close enough in functionality, though, that the possibility of merging them is intriguing.

Implementation Progress

Spun off longer-term follow ups:

Activity

  1. shykes commented on Nov 15, 2022

    @shykes
    Author
  2. sipsma commented on Nov 15, 2022

    @sipsma
  3. sipsma commented on Nov 15, 2022

    @sipsma
  4. shykes commented on Nov 15, 2022

    @shykes
    Author
  5. shykes commented on Nov 15, 2022

    @shykes
    Author
  6. sipsma commented on Nov 15, 2022

    @sipsma
  7. sipsma commented on Nov 15, 2022

    @sipsma
  8. shykes commented on Nov 15, 2022

    @shykes
    Author
  9. sipsma commented on Nov 15, 2022

    @sipsma
  10. aluzzardi commented on Nov 15, 2022

    @aluzzardi
  11. shykes commented on Nov 15, 2022

    @shykes
    Author
  12. marcosnils commented on Nov 15, 2022

    @marcosnils
  13. sipsma commented on Nov 15, 2022

    @sipsma
  14. helderco commented on Nov 15, 2022

    @helderco
  15. marcosnils commented on Nov 16, 2022

    @marcosnils
  16. 26 remaining items

  17. sipsma commented on Nov 30, 2022

    @sipsma
    Contributor

    @shykes @aluzzardi @vito @marcosnils posted the latest summary in my previous comment. Any other concerns left? If not, I'll break this down into subtasks (including as just discussed w/ Marcos updating the CLI to use the engine directly rather than the go sdk).

    This all also ties heavily into OX, but I'm gonna make separate issues for more proposals there as it starts to branch too far out from this issue.

  18. shykes commented on Dec 1, 2022

    @shykes
    ContributorAuthor

    There was another round of discussion and updates yesterday:

    Thanks for the summary! Some thoughts below. I also updated the top comment to be up-to-date with my understanding.

    1. Avoid unix socks+npipes.

    👍

    1. As mentioned above, use http basic auth with a token unique to the session to protect against anyone scanning localhost and connecting to the session

    👍

     2. Anyone with capabilities to sniff localhost traffic (either granted to their process or via a setcap binary) can obtain the token
    

    👍 Agreed, that is a security downside that we should acknowledge and document. I do believe that having an unprivileged local user that 1) may be untrusted, 2) has sniffing capability, and 3) is not root, is incredibly unlikely. A shop that cares about that risk would likely either make sure non-root doesn't have sniffing capability (seems like a no-brainer to me), or restrict local access to the machine. (Unprivileged, untrusted users that can sniff local traffic are bad news regardless of dagger).

     3. SDKs will have to do a tiny bit extra work to securely obtain the token over the stdout of the engine session subprocess
    

    👍 + 🙋🏻‍♂️

    I updated the proposal to specify how that should be encoded. I went for a json object because that seemed the simplest to implement and also extend in the future. But we can bikeshed.

    Latest Summary

    I updated the diagrams in the top comment, can you re-check that we're on the same page on the flow?

    DAGGER_SESSION_URL SDKs Points to an endpoint on which an existing session is being served Fallthrough to check _EXPERIMENTAL_DAGGER_CLI_BIN http://localhost:34567

    🙋🏻‍♂️ I left DAGGER_SESSION_PORT in my proposal, and added an "open question" section to help us discuss and decide. I do think _PORT is better.

    DAGGER_SESSION_TOKEN SDKs An HTTP basic auth username Fallthrough to obtain from CLI engine-session subprocess some-random-uuid

    👍 + 🙋🏻‍♂️ No issue here. One question out of curiosity: will it cause confusion that the secret is passed as a username instead of password? Is that common practice for access tokens? Since the original purpose was to avoid "weirdness" I thought I'd ask.

    _EXPERIMENTAL_DAGGER_CLI_BIN SDKs Points to a CLI binary to use to start the engine-session Pull CLI from S3 to local cache dir, use that /usr/bin/dagger
    _EXPERIMENTAL_DAGGER_RUNNER_HOST CLI Points to a runner, schema indicates how to connect to it. Essentially our new BUILDKIT_HOST Pull and run engine image in docker docker-container://my-dagger-engine

    👍 I think those will be superseded by flags & configuration for dagger run soon enough, but that's why we prefixed them with _EXPERIMENTAL, so no objection :)

  19. sipsma commented on Dec 2, 2022

    @sipsma
    Contributor

    I updated the proposal to specify how that should be encoded. I went for a json object because that seemed the simplest to implement and also extend in the future. But we can bikeshed.

    JSON SGTM

    One question out of curiosity: will it cause confusion that the secret is passed as a username instead of password? Is that common practice for access tokens?

    I initially did this because it's what Stripe does: https://stripe.com/docs/api/authentication?lang=curl

    And then some subsequent googling suggested that it's mostly arbitrary whether it's (empty username, non-empty password) or (non-empty username, empty password) but occasionally some systems (client or server-side) want you to never have an empty username, so username-only is a fine default.

    I guess if we ever want this process to serve multiple sessions (not sure that we do, but who knows) it also makes some logical sense in that, with a lot of hand waving, you could then say that a session is closer to a user (i.e. scoped data) than a password.

    I left DAGGER_SESSION_PORT in my proposal, and added an "open question" section to help us discuss and decide. I do think _PORT is better.

    I'm fine with this. Someday we'll have to deal with some minor annoyance around forcing the engine session to listen on both ipv4 and ipv6 loopback addresses (https://gist.github.com/aojea/94f6f483173641647c731f582e52f0b0) since we can't just tell the client which one to connect to when it's only the port, but that's fine, obscure and probably easy to address.

    Other than that it's mostly arbitrary for me since we can start w/ port and expand if needed in the future. I guess it's less one-way-door-y in that sense.

    cc @marcosnils in case you have any objections to switch to DAGGER_SESSION_PORT

    Which CLI to download

    Broad answer is that it's when we've done an engine upgrade and they have tested that their client works with it. For more specifics, I think we should split that off into a follow up issue (one of several that I'll be making once we're in agreement on the rest)

    Could dagger listen be folded into dagger session?

    Yeah I think they will, I'm just waiting til I actually get to the implementation to confirm we can combine them since there's a small risk they have some subtle incompatibility in the interface (in which case session can just stay hidden indefinitely, no major harm done).

    I updated the diagrams in the top comment, can you re-check that we're on the same page on the flow?

    Yep, my only extreme nit is that in DSI advanced we don't need to "export to environment variables"

  20. shykes commented on Dec 2, 2022

    @shykes
    ContributorAuthor

    One question out of curiosity: will it cause confusion that the secret is passed as a username instead of password? Is that common practice for access tokens?

    I initially did this because it's what Stripe does: https://stripe.com/docs/api/authentication?lang=curl

    And then some subsequent googling suggested that it's mostly arbitrary whether it's (empty username, non-empty password) or (non-empty username, empty password) but occasionally some systems (client or server-side) want you to never have an empty username, so username-only is a fine default.

    I guess if we ever want this process to serve multiple sessions (not sure that we do, but who knows) it also makes some logical sense in that, with a lot of hand waving, you could then say that a session is closer to a user (i.e. scoped data) than a password.

    👍 Makes sense, thanks for the details.

    Which CLI to download

    Broad answer is that it's when we've done an engine upgrade and they have tested that their client works with it. For more specifics, I think we should split that off into a follow up issue (one of several that I'll be making once we're in agreement on the rest)

    👍

    Could dagger listen be folded into dagger session?

    Yeah I think they will, I'm just waiting til I actually get to the implementation to confirm we can combine them since there's a small risk they have some subtle incompatibility in the interface (in which case session can just stay hidden indefinitely, no major harm done).

    👍

    I updated the diagrams in the top comment, can you re-check that we're on the same page on the flow?

    Yep, my only extreme nit is that in DSI advanced we don't need to "export to environment variables"

    You’re absolutely right, even worse it’s an unnecessary pollution of the client’s environment. I can’t remember why it seemed like a good idea a few hours ago. I blame sleep deprivation 😅

  21. RealHarshThakur commented on Dec 2, 2022

    @RealHarshThakur

    _EXPERIMENTAL_DAGGER_RUNNER_HOST CLI

    @sipsma Does this mean that when we do a dagger.Connect(...) , there would be a parameter to configure where the remote runner is?

    docker-container://my-dagger-engine

    I'm trying to avoid using Docker. Is it possible to make it TCP-based similar to how BUILDKIT_HOST is? Or if it is the Dagger engine that's being exposed, HTTP-based?

    PS: I'm stuck on v0.3.x of Dagger since remote buildkit worked with BUILDKIT_HOST and dagger.Connect(..)
    inferred it

  22. sipsma commented on Dec 2, 2022

    @sipsma
    Contributor

    @RealHarshThakur

    Does this mean that when we do a dagger.Connect(...) , there would be a parameter to configure where the remote runner is?

    For now we're thinking of an environment variable that is set when calling dagger.Connect, will that work for you? We are trying to avoid putting experimental, unstable parameters in our arguments to dagger.Connect, but if an env var creates hardship let us know.

    I'm trying to avoid using Docker. Is it possible to make it TCP-based similar to how BUILDKIT_HOST is? Or if it is the Dagger engine that's being exposed, HTTP-based?

    You should be able to avoid Docker, however you will need to run the dagger engine somewhere and that engine is distributed as a container image. While we haven't tested it, I can't image why it fundamentally wouldn't work with non-docker container runtimes (podman/lima/etc.).

    The caveat (for now anyways) is that if you don't want to use docker you will need to set up the container yourself somewhere ahead of time and then set _EXPERIMENTAL_DAGGER_RUNNER_HOST to point to it. We should be able to support tcp:// addresses pretty easily for that though.

    Does that sounds like it'd work for you?

  23. RealHarshThakur commented on Dec 2, 2022

    @RealHarshThakur

    Env var works :)

    can't image why it fundamentally wouldn't work with non-docker container runtimes (podman/lima/etc.).

    I plan to run it with containerd (orchestrate it with K8s in future, maybe). IIRC, Podman/lima work with docker:// but containerd doesn't.

    We should be able to support tcp:// addresses pretty easily for that though.

    Yep, that sounds great.
    Would the image be a bundled Dagger engine+buildkit daemon? If so, would it be similar to load balancing and scaling Buildkit so cache is still shared?

  24. shykes commented on Dec 6, 2022

    @shykes
    ContributorAuthor

    _EXPERIMENTAL_DAGGER_RUNNER_HOST CLI Points to a runner, schema indicates how to connect to it. Essentially our new BUILDKIT_HOST Pull and run engine image in docker docker-container://my-dagger-engine

    👍 I think those will be superseded by flags & configuration for dagger run soon enough,

    Just double-checking that we’re on the same page on this one @sipsma @RealHarshThakur @aluzzardi .

    Given the general trend of making dagger run the primary mechanism for operator control, I think a logical conclusion is that configuration of remote engine will follow the same path. This would mean (in the future) deprecating _EXPERIMENTAL_DAGGER_RUNNER_HOST in favor of dagger run & associated config flags/files/env vars.

    but that's why we prefixed them with _EXPERIMENTAL, so no objection :)

    Still relevant :)

  25. RealHarshThakur commented on Dec 6, 2022

    @RealHarshThakur

    Yeah, works for me.

  26. added
    area/engine
    on Dec 6, 2022
  27. self-assigned this
    on Dec 7, 2022
  28. sipsma commented on Dec 7, 2022

    @sipsma
    Contributor

    Sent out the first part of implementing all this: #4119

    I also updated the description of this issue w/ the task list needed to implement it fully (cc @shykes). I considered breaking each task into a separate issue but I don't really see much benefit since they need to be done in quick succession of one another, not really left in the backlog. Happy to break into separate issues if others prefer though.

  29. mircubed commented on Dec 13, 2022

    @mircubed
    Contributor
  30. added this to the milestone on Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/engine

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions