| import atexit |
| import base64 |
| import errno |
| import os |
| import sys |
| import uuid |
| from os.path import dirname, isdir, isfile |
| from threading import RLock |
| from typing import List, Optional |
|
|
| DPREFIX = os.environ.get("ANACONDA_ANON_USAGE_DEBUG_PREFIX") or "" |
| DEBUG = bool(os.environ.get("ANACONDA_ANON_USAGE_DEBUG")) or DPREFIX |
|
|
| |
| |
| |
| |
| |
| |
| DEFERRED = [] |
|
|
| |
| |
| |
| CACHE = {} |
| LOCK = RLock() |
|
|
| |
| |
| READ_CHAOS = os.environ.get("ANACONDA_ANON_USAGE_READ_CHAOS") or "" |
| |
| |
| WRITE_CHAOS = os.environ.get("ANACONDA_ANON_USAGE_WRITE_CHAOS") or "" |
| |
| WRITE_NEWLINE = False |
|
|
| WRITE_SUCCESS = 0 |
| WRITE_DEFER = 1 |
| WRITE_FAIL = 2 |
|
|
| |
| MIN_ENTROPY = 128 |
| |
| |
| TOKEN_LENGTH = (MIN_ENTROPY - 1) // 6 + 1 |
|
|
|
|
| def cached(func): |
| def call_if_needed(*args, **kwargs): |
| global CACHE |
| key = (func.__name__, args, tuple(kwargs.items())) |
| if key not in CACHE: |
| with LOCK: |
| |
| |
| |
| if key not in CACHE: |
| CACHE[key] = func(*args, **kwargs) |
| return CACHE[key] |
|
|
| return call_if_needed |
|
|
|
|
| def _cache_clear(*args): |
| global CACHE |
| global __nodestr |
| if not args: |
| CACHE.clear() |
| else: |
| CACHE = {k: v for k, v in CACHE.items() if k[0] not in args} |
|
|
|
|
| def _debug(s, *args, error=False): |
| if error and not DEBUG: |
| |
| |
| |
| |
| |
| |
| from conda.base.context import context |
|
|
| error = not context.json |
| if error or DEBUG: |
| print((DPREFIX + s) % args, file=sys.stderr) |
|
|
|
|
| def _random_token(what="random"): |
| |
| |
| data = os.urandom((TOKEN_LENGTH * 6 - 1) // 8 + 1) |
| result = base64.urlsafe_b64encode(data).decode("ascii")[:TOKEN_LENGTH] |
| _debug("Generated %s token: %s", what, result) |
| return result |
|
|
|
|
| def _final_attempt(): |
| """ |
| Called upon the graceful exit from conda, this attempts to |
| write an environment token that was deferred because the |
| environment directory was not yet available. |
| """ |
| global DEFERRED |
| for must_exist, fpath, token, what in DEFERRED: |
| _write_attempt(must_exist, fpath, token) |
|
|
|
|
| atexit.register(_final_attempt) |
|
|
|
|
| def _write_attempt(must_exist, fpath, client_token, emulate_fail=False): |
| """ |
| Attempt to write the token to the given location. |
| Return True with success, False otherwise. |
| """ |
| if must_exist and not isdir(must_exist): |
| _debug("Directory not ready: %s", must_exist) |
| return WRITE_DEFER |
| try: |
| if emulate_fail: |
| raise OSError(errno.EROFS, "Testing permissions issues") |
| os.makedirs(dirname(fpath), exist_ok=True) |
| if WRITE_NEWLINE: |
| client_token = client_token + "\n# Test comment" |
| with open(fpath, "w") as fp: |
| fp.write(client_token) |
| _debug("Token saved: %s", fpath) |
| return WRITE_SUCCESS |
| except Exception as exc: |
| |
| |
| if getattr(exc, "errno", None) in (errno.EACCES, errno.EPERM, errno.EROFS): |
| _debug("No write permissions; cannot write token") |
| else: |
| _debug( |
| "Unexpected error writing token file:\n path: %s\n exception: %s", |
| fpath, |
| exc, |
| error=True, |
| ) |
| return WRITE_FAIL |
|
|
|
|
| def _deferred_exists( |
| fpath: str, what: str, deferred_writes: List = DEFERRED |
| ) -> Optional[str]: |
| """ |
| Check if the deferred token write exists in the DEFERRED write array. |
| If the path must already exist, this helper function determines |
| if the token will be written in the future. |
| |
| Args: |
| fpath: The file path to check for. |
| what: The type of token to check for. |
| deferred_tokens: The list of deferred tokens to check. |
| |
| Returns: |
| The token if it exists, otherwise None. |
| """ |
| for _, fp, token, w in deferred_writes: |
| if fp == fpath and w == what: |
| return token |
|
|
|
|
| def _read_file(fpath, what, read_only=False, single_line=False): |
| """ |
| Implements the saved token functionality. If the specified |
| file exists, and contains a token with the right format, |
| return it. Otherwise, generate a new one and save it in |
| this location. If that fails, return an empty string. |
| """ |
| global DEFERRED |
|
|
| |
| deferred_token = _deferred_exists(fpath, what) |
| if deferred_token: |
| _debug("Returning deferred %s: %s", what, deferred_token) |
| return deferred_token |
|
|
| _debug("%s path: %s", what.capitalize(), fpath) |
| if what[0] in READ_CHAOS: |
| _debug("Pretending %s is not present", what) |
| elif not isfile(fpath): |
| _debug("%s file is not present", what) |
| else: |
| try: |
| with open(fpath) as fp: |
| data = fp.read() |
| if single_line: |
| |
| data = data.strip() |
| if data: |
| data = data.splitlines()[0] |
| _debug("Retrieved %s: %s", what, data) |
| return data |
| except Exception as exc: |
| _debug("Unexpected error reading: %s\n %s", fpath, exc, error=True) |
|
|
|
|
| def _get_node_str(): |
| """ |
| Returns a base64-encoded representation of the host ID |
| as determined by uuid.getnode(). |
| """ |
| val = uuid._unix_getnode() or uuid._windll_getnode() |
| |
| if not val and getattr(uuid, "_generate_time_safe", None): |
| val = uuid.UUID(bytes=uuid._generate_time_safe()[0]).node |
| if val: |
| val = val.to_bytes(6, byteorder=sys.byteorder) |
| val = base64.urlsafe_b64encode(val) |
| val = val.decode("ascii") |
| return val |
|
|
|
|
| def _saved_token(fpath, what, must_exist=None, read_only=False, node_tie=False): |
| """ |
| Implements the saved token functionality. If the specified |
| file exists, and contains a token with the right format, |
| return it. Otherwise, generate a new one and save it in |
| this location. If that fails, return an empty string. |
| """ |
| global DEFERRED |
| what = what + " token" |
| regenerate = resave = False |
| client_token = _read_file(fpath, what, single_line=True) or "" |
| |
| client_token, *xtra = client_token.split(" ", 1) |
| if len(client_token) < TOKEN_LENGTH: |
| if client_token: |
| _debug("Regenerating %s due to short length", what) |
| regenerate = True |
| if node_tie: |
| |
| |
| |
| |
| |
| |
| current_node = _get_node_str() |
| npath = fpath + "_host" |
| saved_node = _read_file(npath, "Host id", single_line=True) or "" |
| true_node = saved_node or (xtra[0] if xtra else None) |
| if regenerate or not true_node: |
| pass |
| elif true_node != current_node: |
| _debug("Regenerating %s due to hostID change", what) |
| regenerate = True |
| elif xtra: |
| _debug("Stripping host ID from %s file", what) |
| resave = True |
| else: |
| _debug("Host ID match confirmed for %s", what) |
| if saved_node != current_node: |
| action = ( |
| ("Migrating" if true_node == current_node else "Updating") |
| if true_node |
| else "Saving" |
| ) |
| current_node = current_node or "" |
| _debug("%s host ID: %s", action, current_node) |
| if _write_attempt(False, npath, current_node, False) == WRITE_DEFER: |
| DEFERRED.append((False, npath, current_node, "Host ID")) |
| if regenerate or resave: |
| if read_only: |
| return "" |
| if regenerate: |
| client_token = _random_token() |
| status = _write_attempt(must_exist, fpath, client_token, what[0] in WRITE_CHAOS) |
| if status == WRITE_FAIL: |
| _debug("Returning blank %s", what) |
| return "" |
| elif status == WRITE_DEFER: |
| |
| |
| _debug("Deferring %s write", what) |
| DEFERRED.append((must_exist, fpath, client_token, what)) |
| return client_token |
|
|