|
| 1 | +""" |
| 2 | +Shared utilities for credential helpers. |
| 3 | +
|
| 4 | +Provides credential resolution and domain checking used by all helpers. |
| 5 | +Configuration is resolved via :class:`ConfigResolver` which reads from |
| 6 | +environment variables, config files, and defaults — the same sources |
| 7 | +used by the main CLI. |
| 8 | +""" |
| 9 | + |
| 10 | +import logging |
| 11 | +import os |
| 12 | + |
| 13 | +from ..core.config_resolver import ConfigResolver |
| 14 | +from ..core.credentials import CredentialContext, CredentialProviderChain |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +def resolve_credentials(debug=False): |
| 20 | + """Resolve Cloudsmith credentials using the provider chain. |
| 21 | +
|
| 22 | + Configuration (proxy, SSL, headers, etc.) is resolved automatically |
| 23 | + from environment variables and config files via :class:`ConfigResolver`. |
| 24 | +
|
| 25 | + Args: |
| 26 | + debug: Enable debug logging |
| 27 | +
|
| 28 | + Returns: |
| 29 | + CredentialResult with .api_key, or None if no credentials available |
| 30 | + """ |
| 31 | + config = ConfigResolver().resolve(debug=debug) |
| 32 | + context = CredentialContext(config=config, debug=debug) |
| 33 | + |
| 34 | + chain = CredentialProviderChain() |
| 35 | + result = chain.resolve(context) |
| 36 | + |
| 37 | + if not result or not result.api_key: |
| 38 | + return None |
| 39 | + |
| 40 | + return result |
| 41 | + |
| 42 | + |
| 43 | +def extract_hostname(url): |
| 44 | + """ |
| 45 | + Extract bare hostname from any URL format. |
| 46 | +
|
| 47 | + Handles protocols, sparse+ prefix, ports, paths, and trailing slashes. |
| 48 | +
|
| 49 | + Args: |
| 50 | + url: URL in any format (e.g., "sparse+https://cargo.cloudsmith.io/org/repo/") |
| 51 | +
|
| 52 | + Returns: |
| 53 | + str: Lowercase hostname (e.g., "cargo.cloudsmith.io") |
| 54 | + """ |
| 55 | + if not url: |
| 56 | + return "" |
| 57 | + |
| 58 | + normalized = url.lower().strip() |
| 59 | + |
| 60 | + # Remove sparse+ prefix (Cargo) |
| 61 | + if normalized.startswith("sparse+"): |
| 62 | + normalized = normalized[7:] |
| 63 | + |
| 64 | + # Remove protocol |
| 65 | + if "://" in normalized: |
| 66 | + normalized = normalized.split("://", 1)[1] |
| 67 | + |
| 68 | + # Remove userinfo (user@host) |
| 69 | + if "@" in normalized.split("/")[0]: |
| 70 | + normalized = normalized.split("@", 1)[1] |
| 71 | + |
| 72 | + # Extract hostname (before first / or :) |
| 73 | + hostname = normalized.split("/")[0].split(":")[0] |
| 74 | + |
| 75 | + return hostname |
| 76 | + |
| 77 | + |
| 78 | +def is_cloudsmith_domain(url, _credential_result=None): |
| 79 | + """ |
| 80 | + Check if a URL points to a Cloudsmith service. |
| 81 | +
|
| 82 | + Checks standard *.cloudsmith.io domains first (no auth needed). |
| 83 | + If not a standard domain, uses the provided credential result (or |
| 84 | + resolves credentials) and queries the Cloudsmith API for custom domains. |
| 85 | +
|
| 86 | + Args: |
| 87 | + url: URL or hostname to check |
| 88 | + _credential_result: Pre-resolved CredentialResult to avoid duplicate |
| 89 | + credential resolution. If None, resolves credentials internally. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + bool: True if this is a Cloudsmith domain |
| 93 | + """ |
| 94 | + hostname = extract_hostname(url) |
| 95 | + if not hostname: |
| 96 | + return False |
| 97 | + |
| 98 | + # Standard Cloudsmith domains — no auth needed |
| 99 | + if hostname.endswith("cloudsmith.io") or hostname == "cloudsmith.io": |
| 100 | + return True |
| 101 | + |
| 102 | + # Custom domains require org + auth |
| 103 | + config = ConfigResolver().resolve() |
| 104 | + org = os.environ.get("CLOUDSMITH_ORG", "").strip() |
| 105 | + if not org: |
| 106 | + return False |
| 107 | + |
| 108 | + result = _credential_result or resolve_credentials() |
| 109 | + if not result: |
| 110 | + return False |
| 111 | + |
| 112 | + from .custom_domains import get_custom_domains_for_org |
| 113 | + |
| 114 | + custom_domains = get_custom_domains_for_org(org, config, result.api_key) |
| 115 | + |
| 116 | + return hostname in [d.lower() for d in custom_domains] |
0 commit comments