-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathcommon.py
More file actions
86 lines (61 loc) · 2.22 KB
/
common.py
File metadata and controls
86 lines (61 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Shared utilities for credential helpers.
Provides domain checking used by all credential helpers.
"""
import logging
import os
logger = logging.getLogger(__name__)
def extract_hostname(url):
"""
Extract bare hostname from any URL format.
Handles protocols, sparse+ prefix, ports, paths, and trailing slashes.
Args:
url: URL in any format (e.g., "sparse+https://cargo.cloudsmith.io/org/repo/")
Returns:
str: Lowercase hostname (e.g., "cargo.cloudsmith.io")
"""
if not url:
return ""
normalized = url.lower().strip()
# Remove sparse+ prefix (Cargo)
if normalized.startswith("sparse+"):
normalized = normalized[7:]
# Remove protocol
if "://" in normalized:
normalized = normalized.split("://", 1)[1]
# Remove userinfo (user@host)
if "@" in normalized.split("/")[0]:
normalized = normalized.split("@", 1)[1]
# Extract hostname (before first / or :)
hostname = normalized.split("/")[0].split(":")[0]
return hostname
def is_cloudsmith_domain(url, session=None, api_key=None, api_host=None):
"""
Check if a URL points to a Cloudsmith service.
Checks standard *.cloudsmith.io domains first (no auth needed).
If not a standard domain, queries the Cloudsmith API for custom domains.
Args:
url: URL or hostname to check
session: Pre-configured requests.Session with proxy/SSL settings
api_key: API key for authenticating custom domain lookups
api_host: Cloudsmith API host URL
Returns:
bool: True if this is a Cloudsmith domain
"""
hostname = extract_hostname(url)
if not hostname:
return False
# Standard Cloudsmith domains — no auth needed
if hostname.endswith("cloudsmith.io") or hostname == "cloudsmith.io":
return True
# Custom domains require org + auth
org = os.environ.get("CLOUDSMITH_ORG", "").strip()
if not org:
return False
if not api_key:
return False
from .custom_domains import get_custom_domains_for_org
custom_domains = get_custom_domains_for_org(
org, session=session, api_key=api_key, api_host=api_host
)
return hostname in [d.lower() for d in custom_domains]