Skip to content

Commit

Permalink
support stripping auth from pypi packages
Browse files Browse the repository at this point in the history
  • Loading branch information
croth1 committed Feb 9, 2023
1 parent 94c4c42 commit 9e45cdf
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
19 changes: 10 additions & 9 deletions conda_lock/conda_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,14 @@
DEFAULT_FILES = [pathlib.Path("environment.yml")]

# Captures basic auth credentials, if they exists, in the second capture group.
AUTH_PATTERN = re.compile(r"^(https?:\/\/)(.*:.*@)?(.*)")
AUTH_PATTERN = re.compile(r"^(# pip .* @ )?(https?:\/\/)(.*:.*@)?(.*)")

# Do not substitute in comments, but do substitute in pip installable packages
# with the pattern: # pip package @ url.
PKG_PATTERN = re.compile(r"(^[^#@].*|^# pip .*)")

# Captures the domain in the second group.
DOMAIN_PATTERN = re.compile(r"^(https?:\/\/)?([^\/]+)(.*)")
DOMAIN_PATTERN = re.compile(r"^(# pip .* @ )?(https?:\/\/)?([^\/]+)(.*)")

# Captures the platform in the first group.
PLATFORM_PATTERN = re.compile(r"^# platform: (.*)$")
Expand Down Expand Up @@ -913,11 +917,8 @@ def _add_auth_to_line(line: str, auth: Dict[str, str]) -> str:


def _add_auth_to_lockfile(lockfile: str, auth: Dict[str, str]) -> str:
# do not substitute in comments, but do substitute in pip installable packages
# with the pattern: # pip package @ url.
pkg_pattern = re.compile(r"(^[^#@].*|^# pip .*)")
lockfile_with_auth = "\n".join(
_add_auth_to_line(line, auth) if pkg_pattern.match(line) else line
_add_auth_to_line(line, auth) if PKG_PATTERN.match(line) else line
for line in lockfile.strip().split("\n")
)
if lockfile.endswith("\n"):
Expand All @@ -933,17 +934,17 @@ def _add_auth(lockfile: str, auth: Dict[str, str]) -> Iterator[pathlib.Path]:


def _strip_auth_from_line(line: str) -> str:
return AUTH_PATTERN.sub(r"\1\3", line)
return AUTH_PATTERN.sub(r"\1\2\4", line)


def _extract_domain(line: str) -> str:
return DOMAIN_PATTERN.sub(r"\2", line)
return DOMAIN_PATTERN.sub(r"\3", line)


def _strip_auth_from_lockfile(lockfile: str) -> str:
lockfile_lines = lockfile.strip().split("\n")
stripped_lockfile_lines = tuple(
_strip_auth_from_line(line) if line[0] not in ("#", "@") else line
_strip_auth_from_line(line) if PKG_PATTERN.match(line) else line
for line in lockfile_lines
)
stripped_domains = sorted(
Expand Down
1 change: 1 addition & 0 deletions tests/test-lockfile/no-auth.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
http://a.mychannel.cloud/mypackage
http://b.mychannel.cloud/mypackage
http://c.mychannel.cloud/mypackage
# pip mypackage @ http://d.mychannel.cloud/mypackage
1 change: 1 addition & 0 deletions tests/test-lockfile/test.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
http://user:password@a.mychannel.cloud/mypackage
http://b.mychannel.cloud/mypackage
http://user:password@c.mychannel.cloud/mypackage
# pip mypackage @ http://user:password@d.mychannel.cloud/mypackage
1 change: 1 addition & 0 deletions tests/test-stripped-lockfile/no-auth.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
http://a.mychannel.cloud/mypackage
http://b.mychannel.cloud/mypackage
http://c.mychannel.cloud/mypackage
# pip mypackage @ http://d.mychannel.cloud/mypackage

0 comments on commit 9e45cdf

Please sign in to comment.