From cf2e70b5428b580cba52b0b2571d2b32bb6d141c Mon Sep 17 00:00:00 2001 From: telchak Date: Thu, 22 Oct 2026 23:45:58 +0200 Subject: [PATCH] Present a git token for dependencies from private repositories A git dependency from a private repository cannot be fetched by any of the uv based modules: the default base has git, but no way to give it credentials. gitToken, on pytest, uv, mypy and ty, takes a secret that the shared library hands to git as a credential helper declared through GIT_CONFIG_* variables, for gitHost only (github.com by default). uv runs the git CLI with its environment, so the helper reaches it with no config file written, and the token stays out of every layer, URL and uv.lock. Co-Authored-By: Claude Opus 5.5 Signed-off-by: telchak --- .dagger/modules/e2e/main.dang | 31 +++++++++++++++++++++++++++++++ README.md | 17 +++++++++++++++++ mypy/main.dang | 15 ++++++++++++++- pyproject/main.dang | 32 +++++++++++++++++++++++++++++++- pytest/main.dang | 15 ++++++++++++++- ty/main.dang | 15 ++++++++++++++- uv/main.dang | 15 ++++++++++++++- 7 files changed, 135 insertions(+), 5 deletions(-) diff --git a/.dagger/modules/e2e/main.dang b/.dagger/modules/e2e/main.dang index 2ec565e..6b41b37 100644 --- a/.dagger/modules/e2e/main.dang +++ b/.dagger/modules/e2e/main.dang @@ -923,6 +923,37 @@ type E2e { null } + """ + A git token reaches git's credential prompt for its own host only, and never + lands in the container's plain environment. + """ + gitTokenCheck(ws: Workspace!): Void @check { + let token = setSecret(name: "e2e-git-token", plaintext: "e2e-not-a-real-token") + let base = pyproject(markers: ["pyproject.toml"], gitToken: token).base + let ask = `| GIT_TERMINAL_PROMPT=0 git credential fill` + # Compared in the shell: stdout would come back with the secret scrubbed. + base + .withExec([ + "sh", + "-c", + `test "$(printf 'protocol=https\nhost=github.com\n\n' ` + ask + ` | grep '^password=')" = "password=$PYPROJECT_GIT_TOKEN"`, + ]) + .sync + let elsewhere = { base.withExec(["sh", "-c", `printf 'protocol=https\nhost=gitlab.com\n\n' ` + ask]).stdout } rescue { + err: Error => "refused" + } + assert(elsewhere == "refused", "the git token was offered to another host") + assert( + base.envVariable("PYPROJECT_GIT_TOKEN") == null, + "the git token is a plain environment variable", + ) + assert( + pyproject(markers: ["pyproject.toml"]).base.envVariable("GIT_CONFIG_COUNT") == null, + "a base with no git token carries a credential helper", + ) + null + } + """ mypy checks what the project's config names with `files`, rather than every file below the project root, and both checkers take extra arguments. diff --git a/README.md b/README.md index caff0ad..6896611 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,17 @@ pytest a base that has one: $ dagger settings pytest base ghcr.io/astral-sh/uv:python3.14-bookworm ``` +A git dependency from a private repository needs a token. `gitToken` takes a +secret, which git presents to `gitHost` (`github.com` by default) and to no +other host. It reaches git through a credential helper declared in `GIT_CONFIG_*` +variables, so it is written to no file, URL or `uv.lock`. In CI, a GitHub App +installation token with read access to the repository is a good fit: + +```toml +[modules.pytest.settings] +gitToken = "env://GIT_TOKEN" +``` + A failure names the step: the environment, or the tool, with its command, exit code and the end of its output. @@ -332,6 +343,8 @@ mount. | `nesting` | `true` | Let tests reach a nested Dagger engine. | | `version` | `3.14` | Python version of the default base image. | | `base` | none | Base image with Python and uv, instead of the default one. | +| `gitToken` | none | Secret git presents to `gitHost`, for private git dependencies. | +| `gitHost` | `github.com` | Host `gitToken` is presented to. | On a project: `tests`, `test`, `has-tests`, `version`, `skip`. `tests` is a collection of the project's test files, keyed by project-relative path, and its @@ -387,6 +400,8 @@ nesting = false | `auditArgs` | `[]` | Extra arguments for `uv audit`, e.g. `["--no-dev"]`. | | `version` | `3.14` | Python version of the default base image. | | `base` | none | Base image with Python and uv, instead of the default one. | +| `gitToken` | none | Secret git presents to `gitHost`, for private git dependencies. | +| `gitHost` | `github.com` | Host `gitToken` is presented to. | On a project: `audit` (a `@check`, `uv audit`), `lock` (a `@generate`, `uv lock`), `build` (`uv build`, returns `dist` with the wheel and sdist), @@ -421,6 +436,8 @@ failing it would be wrong. | `args` | `[]` | Extra arguments for every run, e.g. `["--strict"]`. | | `version` | `3.14` | Python version of the default base image. | | `base` | none | Base image with Python and uv. | +| `gitToken` | none | Secret git presents to `gitHost`, for private git dependencies. | +| `gitHost` | `github.com` | Host `gitToken` is presented to. | On a project: `type-check` (a `@check`), `has-sources`, `version`, `skip`, and for mypy `configures-files`. Both modules name the check `type-check`, so diff --git a/mypy/main.dang b/mypy/main.dang index 432c6bb..7c3df56 100644 --- a/mypy/main.dang +++ b/mypy/main.dang @@ -47,6 +47,8 @@ type Mypy { let pythonVersion: String let base: Container + let gitToken: Secret + let gitHost: String! new( """ @@ -59,6 +61,15 @@ type Mypy { """ base: Container = null, """ + HTTPS token git presents to gitHost, for a dependency sourced from a + private git repository, such as a GitHub App installation token. + """ + gitToken: Secret = null, + """ + Host gitToken is presented to, and to no other. + """ + gitHost: String! = "github.com", + """ Project roots to check. A bare pattern selects, a "!"-prefixed pattern excludes, and an exclude always wins whatever the order, so ["**", "!docs"] checks everything but docs. @@ -75,6 +86,8 @@ type Mypy { ) { self.pythonVersion = version self.base = base + self.gitToken = gitToken + self.gitHost = gitHost self.scope = scope self.defaultVersion = defaultVersion self.args = args @@ -82,7 +95,7 @@ type Mypy { } let lib: Pyproject! { - pyproject(markers: markers, version: pythonVersion, base: base) + pyproject(markers: markers, version: pythonVersion, base: base, gitToken: gitToken, gitHost: gitHost) } """ diff --git a/pyproject/main.dang b/pyproject/main.dang index d79877d..933adde 100644 --- a/pyproject/main.dang +++ b/pyproject/main.dang @@ -86,6 +86,15 @@ type Pyproject { Python interpreter and uv on PATH. Mutually exclusive with version. """ base: Container = null, + """ + HTTPS token git presents to gitHost, for a dependency sourced from a + private git repository, such as a GitHub App installation token. + """ + gitToken: Secret = null, + """ + Host gitToken is presented to, and to no other. + """ + gitHost: String! = "github.com", ) { if (markers.isEmpty) { raise "markers must not be empty: the caller decides what a project is" @@ -97,7 +106,7 @@ type Pyproject { self.exclude = exclude self.includeExtraFiles = includeExtraFiles self.version = if (base == null) { version ?? defaultVersion } else { null } - self.base = base ?? defaultBase(version ?? defaultVersion) + self.base = withGitToken(base ?? defaultBase(version ?? defaultVersion), gitToken, gitHost) self } @@ -139,6 +148,27 @@ type Pyproject { .withDirectory("/usr/local/bin", uvBinary, include: ["uv", "uvx"]) } + """ + `ctr`, with git answering `host`'s credential prompts with `token`. + + uv fetches a git dependency with the git CLI and passes its environment on, + so a credential helper declared through GIT_CONFIG_* (git 2.31+) reaches it + with no config file written. The helper reads the token from a secret + variable at fetch time: it never appears in a layer, a URL, or uv.lock. + """ + let withGitToken(ctr: Container!, token: Secret, host: String!): Container! { + if (token == null) { ctr } else { + ctr + .withSecretVariable(name: "PYPROJECT_GIT_TOKEN", secret: token) + .withEnvVariable("GIT_CONFIG_COUNT", "1") + .withEnvVariable("GIT_CONFIG_KEY_0", "credential.https://" + host + ".helper") + .withEnvVariable( + "GIT_CONFIG_VALUE_0", + `!f() { test "$1" = get && echo username=x-access-token && echo "password=$PYPROJECT_GIT_TOKEN"; }; f`, + ) + } + } + """ Projects discovered at or below the workspace cwd, plus the nearest enclosing project when the cwd is inside one but not a root itself. Paths diff --git a/pytest/main.dang b/pytest/main.dang index 6bc0abc..0ed72c3 100644 --- a/pytest/main.dang +++ b/pytest/main.dang @@ -79,6 +79,8 @@ type Pytest { let pythonVersion: String let base: Container + let gitToken: Secret + let gitHost: String! new( """ @@ -91,6 +93,15 @@ type Pytest { """ base: Container = null, """ + HTTPS token git presents to gitHost, for a dependency sourced from a + private git repository, such as a GitHub App installation token. + """ + gitToken: Secret = null, + """ + Host gitToken is presented to, and to no other. + """ + gitHost: String! = "github.com", + """ Project roots to test. A bare pattern selects, a "!"-prefixed pattern excludes, and an exclude always wins whatever the order, so ["**", "!docs"] tests everything but docs. @@ -116,6 +127,8 @@ type Pytest { ) { self.pythonVersion = version self.base = base + self.gitToken = gitToken + self.gitHost = gitHost self.scope = scope self.args = args self.defaultVersion = defaultVersion @@ -125,7 +138,7 @@ type Pytest { } let lib: Pyproject! { - pyproject(markers: markers, version: pythonVersion, base: base) + pyproject(markers: markers, version: pythonVersion, base: base, gitToken: gitToken, gitHost: gitHost) } """ diff --git a/ty/main.dang b/ty/main.dang index ec4f326..d6b40fc 100644 --- a/ty/main.dang +++ b/ty/main.dang @@ -49,6 +49,8 @@ type Ty { let pythonVersion: String let base: Container + let gitToken: Secret + let gitHost: String! new( """ @@ -61,6 +63,15 @@ type Ty { """ base: Container = null, """ + HTTPS token git presents to gitHost, for a dependency sourced from a + private git repository, such as a GitHub App installation token. + """ + gitToken: Secret = null, + """ + Host gitToken is presented to, and to no other. + """ + gitHost: String! = "github.com", + """ Project roots to check. A bare pattern selects, a "!"-prefixed pattern excludes, and an exclude always wins whatever the order, so ["**", "!docs"] checks everything but docs. @@ -77,6 +88,8 @@ type Ty { ) { self.pythonVersion = version self.base = base + self.gitToken = gitToken + self.gitHost = gitHost self.scope = scope self.defaultVersion = defaultVersion self.args = args @@ -84,7 +97,7 @@ type Ty { } let lib: Pyproject! { - pyproject(markers: markers, version: pythonVersion, base: base) + pyproject(markers: markers, version: pythonVersion, base: base, gitToken: gitToken, gitHost: gitHost) } """ diff --git a/uv/main.dang b/uv/main.dang index ece5d7f..b4278fb 100644 --- a/uv/main.dang +++ b/uv/main.dang @@ -48,6 +48,8 @@ type Uv { let pythonVersion: String let base: Container + let gitToken: Secret + let gitHost: String! new( """ @@ -60,6 +62,15 @@ type Uv { """ base: Container = null, """ + HTTPS token git presents to gitHost, for a dependency sourced from a + private git repository, such as a GitHub App installation token. + """ + gitToken: Secret = null, + """ + Host gitToken is presented to, and to no other. + """ + gitHost: String! = "github.com", + """ Project roots to lock. A bare pattern selects, a "!"-prefixed pattern excludes, and an exclude always wins whatever the order, so ["**", "!docs"] locks everything but docs. @@ -77,6 +88,8 @@ type Uv { ) { self.pythonVersion = version self.base = base + self.gitToken = gitToken + self.gitHost = gitHost self.lock = lock self.audit = audit self.auditArgs = auditArgs @@ -84,7 +97,7 @@ type Uv { } let lib: Pyproject! { - pyproject(markers: markers, version: pythonVersion, base: base) + pyproject(markers: markers, version: pythonVersion, base: base, gitToken: gitToken, gitHost: gitHost) } """