Cion-lab commited on
Commit
dc56217
·
verified ·
1 Parent(s): e6aee19

add p1 credential-path probe (read-only) + param_count

Browse files
Files changed (1) hide show
  1. probes/p1_credential_path.py +97 -0
probes/p1_credential_path.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Phase 1 CPU probe -- can a Kaggle session act as its own credential store?
2
+ #
3
+ # Why this matters (D-002): §2 permits the HF token to come "from the environment or a secret store",
4
+ # and forbids it reaching a public artifact -- including job logs and any code mirrored to the Hub.
5
+ # The obvious fallback (paste the token into the kernel source) puts it in a Kaggle-held revision and
6
+ # in anything that later mirrors the kernel, so it is the last option, not the first.
7
+ #
8
+ # Kaggle already injects KAGGLE_API_V1_TOKEN / KAGGLE_DATA_PROXY_TOKEN / KAGGLE_USER_SECRETS_TOKEN into
9
+ # internet-enabled sessions (observed in probe p0_cpu_probe). If the Kaggle API is usable *inside* a
10
+ # job, a **private** Kaggle dataset can hold the HF token and be mounted as a data source, which is a
11
+ # genuine secret store: the token then never appears in kernel source, logs, or the public code repo.
12
+ #
13
+ # This probe is READ-ONLY. It lists and reports; it creates nothing and writes no secret. Zero GPU quota.
14
+ # No credentials in this file.
15
+
16
+ import json
17
+ import os
18
+ import subprocess
19
+ import sys
20
+ import time
21
+
22
+ R = {}
23
+
24
+
25
+ def run(cmd, timeout=180):
26
+ try:
27
+ p = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=timeout)
28
+ return {"rc": p.returncode, "out": p.stdout[:1500], "err": p.stderr[:900]}
29
+ except Exception as e:
30
+ return {"error": f"{type(e).__name__}: {e}"[:200]}
31
+
32
+
33
+ def present_and_nonempty(name):
34
+ v = os.environ.get(name)
35
+ return {"present": v is not None, "length": len(v) if v else 0}
36
+
37
+
38
+ # Never print a token value -- report only that it exists and how long it is.
39
+ R["injected_tokens"] = {n: present_and_nonempty(n) for n in [
40
+ "KAGGLE_API_V1_TOKEN", "KAGGLE_DATA_PROXY_TOKEN", "KAGGLE_USER_SECRETS_TOKEN",
41
+ "KAGGLE_DATA_PROXY_URL", "KAGGLE_GRPC_DATA_PROXY_URL", "KAGGLE_KERNEL_INTEGRATIONS",
42
+ "HF_TOKEN", "HUGGING_FACE_HUB_TOKEN"]}
43
+
44
+ R["whoami_env"] = run("env | sed 's/=.*/=<redacted>/' | sort | head -60", 60)
45
+
46
+ # 1. Is the Kaggle API reachable from inside the container at all?
47
+ R["kaggle_cli_preinstalled"] = run("python -m pip show kaggle 2>/dev/null | head -3", 60)
48
+ t = time.monotonic()
49
+ R["kaggle_cli_install"] = run(
50
+ "python -m pip install --quiet --disable-pip-version-check --no-input kaggle 2>&1 | tail -5", 300)
51
+ R["kaggle_cli_install"]["seconds"] = round(time.monotonic() - t, 1)
52
+
53
+ # Does it authenticate off the injected credentials, with no ~/.kaggle/kaggle.json written?
54
+ # Redacted through sed on purpose: any credential-shaped run of characters is scrubbed before it can
55
+ # reach stdout, because stdout here becomes a retained kernel log.
56
+ REDACT = " | sed -E 's/[A-Za-z0-9_-]{16,}/<redacted>/g'"
57
+ R["kaggle_auth_status"] = run(f"kaggle --version {REDACT}", 60)
58
+ R["kaggle_whoami"] = run(f"kaggle config view 2>&1 {REDACT}", 60)
59
+
60
+ # A read that requires a real authenticated principal. If these return rows, the in-job API works.
61
+ R["kaggle_datasets_list"] = run("kaggle datasets list -v --page-size 5 2>&1", 180)
62
+ R["kaggle_kernels_mine"] = run("kaggle kernels list --mine --page-size 5 2>&1", 180)
63
+
64
+ # 2. The user-secrets harness, if Kaggle exposes it here, is the cleaner store of the two.
65
+ R["secrets_dir"] = run("ls -la /kaggle/input/ 2>&1 | head -20", 60)
66
+ R["secrets_harness"] = {
67
+ "exists": os.path.exists("/kaggle/input/.secrets/harness_util.py"),
68
+ "listing": run("ls -la /kaggle/input/.secrets/ 2>&1 | head", 60)}
69
+
70
+ # 3. Can a job write to the Hub anonymously at all? (It should not be able to.)
71
+ def hub_write_probe():
72
+ import urllib.request
73
+ import urllib.error
74
+
75
+ url = "https://huggingface.co/api/models/Cion-lab/ounce100m-code/commit/main"
76
+ req = urllib.request.Request(url, data=b"{}", method="POST",
77
+ headers={"Content-Type": "application/json"})
78
+ try:
79
+ with urllib.request.urlopen(req, timeout=30) as r:
80
+ return {"unexpectedly_ok": True, "status": r.status}
81
+ except urllib.error.HTTPError as e:
82
+ return {"expected_denied": True, "status": e.code, "reason": e.reason}
83
+ except Exception as e:
84
+ return {"error": f"{type(e).__name__}: {e}"[:200]}
85
+
86
+
87
+ R["anonymous_hub_write"] = hub_write_probe()
88
+
89
+ # 4. Does the image already carry an HF credential path we could populate without a token in source?
90
+ R["hf_cache_state"] = {
91
+ "HF_HOME": os.environ.get("HF_HOME"),
92
+ "token_file_exists": os.path.exists(os.path.expanduser("~/.cache/huggingface/token")),
93
+ "listing": run("ls -la /root/.cache/huggingface/ 2>&1 | head", 60)}
94
+
95
+ print("PROBE_JSON_BEGIN")
96
+ print(json.dumps(R, indent=1, default=str))
97
+ print("PROBE_JSON_END")