Skip to content

feat: PAT support -- private git support on http / https - #8805

Merged
sipsma merged 16 commits into
dagger:mainfrom
grouville:pat-support
Nov 7, 2024
Merged

sipsma merged 16 commits into
dagger:mainfrom
grouville:pat-support

Conversation

@grouville

@grouville grouville commented Oct 28, 2024 •

Copy link
Copy Markdown
Member

Fixes #7990

This is the PR introducing PAT support auth.

Implementation

As requested on the issue by the community, we rely on the git credential to request for authentication:

  • this allows us to maintain the same auth experience as your local git CLI
  • When needed, the engine requests the host for the git-credential
  • Your local gitconfig sets up the git credential auth you want

This gives userland the power to configure auth however they want, with their preferred auth tool

How does this work

git introduces a standard: git credential which is queryable from the git CLI:

echo -e "protocol=https\nhost=github.com\npath=dagger/dagger\n" | git credential fill

The engine is directly querying this command on the user's host, via the CLI and parses the output of the command ; it then injects it back inside its session, safely, as a secret

How can you test

Please break this PR as you would, especially try to see if this maps your usual auth pattern (personal and enterprise environment):

$ cd ~/dagger
$ ./hack/dev # wait until it finishes
$ ./hack/with-dev bash # jump into the dev version shell
$ dagger version $ shall output dagger v0.13.7-...
$ dagger -m private-module functions

git credential managers

There are a lot of git credential managers out there, all mapping with the git credential standard. They can also be chained, and this will allow you to manage the caching side of those tokens client-side

TODO:

cc @nipuna-perera @sheldonhull

@raghavendramallela

Copy link
Copy Markdown

There are other configs in .gitconfig too…. like http.postBuffer https.proxy etc… can these be included?

@grouville

Copy link
Copy Markdown
Member Author

There are other configs in .gitconfig too…. like http.postBuffer https.proxy etc… can these be included?

I think this is more related to #7787 ? What you want is to propagate those .gitconfig inside the engine, from what I understand ? What use do you have of those options ?

@raghavendramallela

Copy link
Copy Markdown

hi @grouville , if i query:

dagger core git --url privgit.com -vvvvvv
✔ connect 0.4s
  ✔ starting engine 0.0s
  ✔ connecting to engine 0.1s
  ✔ starting session 0.1s
  ✔ subscribing to telemetry 0.1s
    ✔ consuming /v1/traces 0.1s
    ✔ consuming /v1/logs 0.0s
    ✔ consuming /v1/metrics 0.0s
✔ initialize 0.4s
  ✔ cache request: mkfile /schema.json 0.0s
  ✔ load cache: mkfile /schema.json 0.0s
  ✔ cache request: blob://sha256:fa3709c3d62221111c45ca4bbe0d3a942e18fae76e0bfd225694b40480d40d88 0.0s
  ✔ load cache: blob://sha256:fa3709c3d62221111c45ca4bbe0d3a942e18fae76e0bfd225694b40480d40d88 0.0s
  ✔ blob(digest: "sha256:fa3709c3d62221111c45ca4bbe0d3a942e18fae76e0bfd225694b40480d40d88", mediaType: "application/vnd.oci.image.layer.v1.tar", size: 167936, uncompressed: "sha256:fa3709c3d62221111c45ca4bbe0d3a942e18fae76e0bfd225694b40480d40d88"): Directory! = xxh3:71f907b752f0adac 0.0s
  ✔ Directory.file(path: "schema.json"): File! = xxh3:ffef5cf427d85037 0.0s
✔ prepare 0.0s
✔ git(url: "privgit.com"): GitRepository! = xxh3:0321a4028fb013f5 0.0s
  ✔ host: Host! = xxh3:ff73c85214b8f75c 0.0s
  ✔ Host.__internalSocket(accessor: "2f746d702f7373682d383254536f633846494149712f6167656e742e313537363539b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad"): Socket! = xxh3:868707ad72e3f5af 0.0s
✘ GitRepository.tags: [String!]! = xxh3:a4bc5136ba5c3a61 10.0s
! git command failed: exit status 128

Error: response from query: input: git.tags resolve: git command failed: exit status 128
stdout: 
stderr: fatal: unable to access 'https://privgit.com/': Could not resolve host: privgit.com

but, privgit.com is resolvable via HTTPS_PROXY env var - git cli, or https.proxy in .gitconfig .
PS: i have initiated the engine container with the proxy variable HTTPS_PROXY already:

podman exec -it dengine env | grep HTTPS_PROXY
HTTPS_PROXY=http://proxyserver.com:443

@sheldonhull

Copy link
Copy Markdown

I normally install the binary via Aqua.
for testing this PR out directly do I run go install daggerrepo@branchname and or is there a doc with best approach?

@grouville

Copy link
Copy Markdown
Member Author

I normally install the binary via Aqua. for testing this PR out directly do I run go install daggerrepo@branchname and or is there a doc with best approach?

Hi @sheldonhull,
No, you need to checkout this PR locally and build the dev version of the engine. I explained how to do it above, in the How can you test section. But, if you have any issue, I have published the engine to ghcr.io. Nipuna cannot build the dev version (https://discord.com/channels/707636530424053791/1300550470783205478/1300550473194930260), so I published it to get his feedback

@grouville
grouville force-pushed the pat-support branch 4 times, most recently from 0d5b420 to 1cd78da Compare November 4, 2024 02:18
@grouville
grouville marked this pull request as ready for review November 4, 2024 02:42
@grouville
grouville requested review from aluzzardi, jedevc, sipsma and vito and removed request for jedevc and vito November 4, 2024 17:42

@vito vito 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.

Overall approach and implementation seems great 👍 - just a couple of comments, otherwise lgtm!

Comment thread core/schema/git.go Outdated
Args: []dagql.NamedInput{
{
Name: "name",
Value: dagql.NewString("gitAuthtoken"),

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.

Not sure if this is still the case, but iirc these secret names are session-wide. So if you use two separate private repos in the same session it seems like these might clobber. Maybe just set a name that's a hash of the plaintext, like we do for the CLI?

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.

@sipsma, are these session-wide ?

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.

Secrets are stored per-client so that sort of clobbering shouldn't be an issue.

The only extremely obscure case I can think of that would be impacted is:

  1. You're using an SDK directly on the host (i.e. not just dagger call)
  2. You set your own secret that also happens to be named gitAuthtoken

Not worth agonizing over, but yeah we could just set name to a sha256 of the plaintext to squash any possibility of that happening.

return nil, err
}

isBitbucket := u.Host == "bitbucket.org"

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.

Do we need to worry about any other hosts, like we do with GitHub enterprise? 🤔

@sheldonhull sheldonhull Nov 6, 2024 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

my host name could be different for azure devops based on a combination of the protocol with http or ssh, as well as the way dev.azure.com and visualstudio.com are both valid addresses (due to legacy url).

I don't have a ton of time, but if you get me an idea of what would help I can draft something out to help enhance this with dev.azure.com processing too

I use git-credential-manager for authorization still if that matters.

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.

Mmh, I do agree that we could miss some edge cases, but here it was changes that were part of #8778. Basically, the core git implementation was not passing the PAT properly, as bitbucket has another way of passing the auth tokens.

We could have some edge cases on github enterprise and some azure, but I added some tests https://github.com/grouville/dagger/blob/a924e0e9b054ece63086badc9c84142cd0bb11d0/core/integration/git_test.go. I tested azure, and github enterprise has already being used by users. I do think that this should be ok ; if it breaks we should then extend I agree 👍

@grouville grouville added this to the v0.14.0 milestone Nov 6, 2024
message GitCredentialResponse {
oneof result {
CredentialInfo credential = 1;
ErrorInfo error = 2;

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.

nit (can be changed later): I think the gRPC way of doing this is to return an actual error from the function, rather returning nil as an error with a custom error field in the response


// Handle token authentication
if s.token {
if s.credHelper {

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.

nit: is there a way to get this from the client config instead?

as @vito mentioned above, we might have a problem with GH enterprise as well

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

Looking good so far!

Comment thread core/schema/git.go Outdated
Comment on lines +186 to +187
// 5. Handle PAT auth
var authToken *core.Secret = nil

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.

nit, not a blocker: do you think it would make sense to skip this if the user has an SSH auth socket setup? Slightly nice if so since there's a little bit of expensive work to retrieve the auth token that we may as well skip if we are just going to use the auth socket.

If there's a use case for having both in place or this is otherwise easier said than done, then no biggie

@grouville grouville Nov 7, 2024 •

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.

I actually implemented a change, following your comment.

Now, we first parse the ref's scheme, and, based on that, the logic is now:

  1. if the person set an SSH socket explicitely, then load the socket
    2.It not,
    2.1 - if the ref's scheme is SSH, then load the SSH socket
    2.2 - If the ref's scheme is HTTP(s), then try the PAT thing

The reason I wouldn't skip if the user has an SSH auth socket setup with this example:

dagger -m ssh://module-ref --dir https://github.com/grouville/daggerverse-private.git

Skipping the PAT when we can load the SSH token enforces users to be consistent on the ref types they use. I do agree that this has a cost, but it seems to be preferable, wdyt ?

Based on the ref's scheme, we use the corresponding auth type -- userland can then cache the tokens with their credential helpers if they want fast auth against them

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.

Sure that LGTM!

Comment thread core/schema/git.go Outdated
URLs: []string{remote.Remote},
})

_, err := repo.List(&git.ListOptions{Auth: nil})

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.

nit: should probably use ListContext so we can cancel the request if the call is canceled

Comment thread core/schema/git.go Outdated
Args: []dagql.NamedInput{
{
Name: "name",
Value: dagql.NewString("gitAuthtoken"),

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.

Secrets are stored per-client so that sort of clobbering shouldn't be an issue.

The only extremely obscure case I can think of that would be impacted is:

  1. You're using an SDK directly on the host (i.e. not just dagger call)
  2. You set your own secret that also happens to be named gitAuthtoken

Not worth agonizing over, but yeah we could just set name to a sha256 of the plaintext to squash any possibility of that happening.


// TestGitCredentialErrors verifies Git authentication for private modules across different providers
// using host-specific PATs and isolated Git credentials per test.
func (GitCredentialSuite) TestGitCredentialErrors(ctx context.Context, t *testctx.T) {

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.

Could we get one more test case that's similar to the existing ones except that it calls a module and passes a Directory as an arg that's from a git repo? e.g. something like:

dagger call test --dir https://github.com/grouville/daggerverse-private.git

and make sure ops on the git dir work as expected in the module (can list the dir or something like that)?

That's just one tricky case where the resolution with git needs to happen within the CLI+auth-token and after should be usable from modules even if they don't have access to the auth provider directly.

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.

Synced up offline about this, but Guillaume added the test and hit a bug. Was a super subtle thing around secrets needing to appear in the dagql Call DAG in order to be passed through args to functions, pushed fix here w/ more deets: e86d0d4

Guillaume de Rouville added 6 commits November 7, 2024 20:45
The GitCredentialAttachable establishes the client - engine
communication.

It represents the scheme on which we query the Git PAT infos

Signed-off-by: Guillaume de Rouville <[email protected]>
Implements the logic to query the git CLI to retrieve the auth
informations:
1. query the git credential fill with protocol / host / path
2. Parse output

We ensure that the git CLI is present on the host, that error occured on
the process

Signed-off-by: Guillaume de Rouville <[email protected]>
Bind everything to test auth

Signed-off-by: Guillaume de Rouville <[email protected]>
Bind the auth to the git API + print a bit everywhere to fix bugs:
1. race on the gitcredentials client accessor (fixed with the shell
   script) + the mutex
2. check if secret creation works

It works

Signed-off-by: Guillaume de Rouville <[email protected]>
Remove the Authenticate endpoint, which was used as a stopgap to advance
on the implementation

I also refactored the gitcredential to simplify it without changing the
logic

Signed-off-by: Guillaume de Rouville <[email protected]>
some cleanup

This commit ensures that the PAT support is only available to client originating the session. This is an easy way to ensure isolation of the sessions, but the integ tests won't be able to use nested execs

It also does some debug cleaning

Signed-off-by: Guillaume de Rouville <[email protected]>
Guillaume de Rouville and others added 8 commits November 7, 2024 20:45
Only fail silently when git credentials haven't been found. We could
extend the logic to alway fail silently, but need to ask feedback around
that

Signed-off-by: Guillaume de Rouville <[email protected]>
Signed-off-by: Guillaume de Rouville <[email protected]>
This tests our proto / host interaction, to ensure that we correctly
handle all unexpected host configuration when querying the `git
credential fill` command on the user's host

Signed-off-by: Guillaume de Rouville <[email protected]>
Fixes some lint errors + checks if a ref is public prior requesting a
git token

Signed-off-by: Guillaume de Rouville <[email protected]>
Add integration tests to verify Git authentication for private modules
across different providers (GitHub, GitLab, BitBucket). Tests use
isolated Git credentials per host to enable parallel execution.

Each test:
- Sets up local Git credentials using provider-specific PATs
- Verifies access to private modules using `dagger functions`
- Runs in isolation using temporary Git configs

Also includes negative test case to verify authentication failures.

Signed-off-by: Guillaume de Rouville <[email protected]>
Apply recommendations:
1. SHA256 the added PAT so that naming never conflicts across sessions
   or with an SDK setting that name
2. Use ListContext so we can cancel the request if the call is canceled

Signed-off-by: Guillaume de Rouville <[email protected]>
Only retrieve the PAT via the git credentials when the ref is http /
same for ssh (unless the socket is specifically passed).

Signed-off-by: Guillaume de Rouville <[email protected]>
Adds the test case to pass a private repo as a directory

Signed-off-by: Guillaume de Rouville <[email protected]>
In order for a function to be granted selective access to a secret, the
secret needs to appear in the dagql Call. Previously the secret was just
set on a field in the returned struct, now it is in the returned
dagql.Instance and thus passes to function clients through args.

Signed-off-by: Erik Sipsma <[email protected]>

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

🚀

@sipsma

sipsma commented Nov 7, 2024

Copy link
Copy Markdown
Contributor

@grouville approved 🎉 but just realized we should have a changelog for this (docs can be separate). Should be good to merge once you add that

@sipsma sipsma added the needs/changelog Pull requests that require a changelog entry label Nov 7, 2024
@grouville

Copy link
Copy Markdown
Member Author

realized

Doing it rn 🙏

Signed-off-by: Guillaume de Rouville <[email protected]>
Comment thread core/schema/git.go
}

func (s *gitSchema) git(ctx context.Context, parent *core.Query, args gitArgs) (*core.GitRepository, error) {
func (s *gitSchema) git(ctx context.Context, parent dagql.Instance[*core.Query], args gitArgs) (inst dagql.Instance[*core.GitRepository], _ error) {

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.

@grouville suggestion for a follow-up - is there a way to break down some of this logic into separate functions, maybe even into core/git.go itself?

The comments here really help, but if we could use function calls to help show the structure that would be even better 🎉

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.

Yeah sure 👍 I saw a lot of monolithic functions, so wasn't sure ; but 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs/changelog Pull requests that require a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🖐️ Load Dagger module from private Github repository via PAT auth.

7 participants