Skip to content

Commit dadc7a9

Browse files
feat: propagate git PAT to ~/.netrc for dependency authentication
When using PAT authentication, write the token to ~/.netrc so that git operations triggered by uv sync (e.g. fetching private git dependencies listed in pyproject.toml) can authenticate using the same credentials as the main repository clone. Refs: SUPPORT-15344 Co-Authored-By: Zora Jelínková <zora.jelinkova@keboola.com>
1 parent 2145095 commit dadc7a9

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

src/source_git.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import subprocess
44
import sys
55
from pathlib import Path
6+
from urllib.parse import urlparse
67

78
from keboola.component.exceptions import UserException
89

@@ -43,8 +44,20 @@ def _set_up_token_auth(self) -> None:
4344
self.repo_auth_url = self.git_cfg.url.replace(
4445
"https://", f"https://x-token-auth:{self.git_cfg.encrypted_token}@"
4546
)
47+
self._set_up_netrc(self.git_cfg.url, self.git_cfg.encrypted_token)
4648
logging.info("Git token authentication set up for HTTPS URL.")
4749

50+
@staticmethod
51+
def _set_up_netrc(repo_url: str, token: str) -> None:
52+
parsed = urlparse(repo_url)
53+
if not parsed.hostname:
54+
return
55+
netrc_path = Path.home() / ".netrc"
56+
entry = f"machine {parsed.hostname}\nlogin x-token-auth\npassword {token}\n"
57+
with open(netrc_path, "a") as f:
58+
f.write(entry)
59+
os.chmod(netrc_path, 0o600)
60+
4861
def _set_up_ssh_command(self) -> None:
4962
if not self.git_cfg.ssh_keys.keys.encrypted_private:
5063
if self.git_cfg.auth == AuthEnum.SSH:

0 commit comments

Comments
 (0)