Skip to content

Commit a7262b7

Browse files
fix: added use_proxy flag to get_artifact_url (#807)
1 parent 54b6ab4 commit a7262b7

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

src/taskgraph/util/parameterization.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ def repl(match):
9090
f"task '{label}' has no dependency named '{dependency}'"
9191
)
9292

93-
return get_artifact_url(task_id, artifact_name)
93+
use_proxy = False
94+
if not artifact_name.startswith("public/"):
95+
use_proxy = True
96+
97+
return get_artifact_url(task_id, artifact_name, use_proxy=use_proxy)
9498

9599
return ARTIFACT_REFERENCE_PATTERN.sub(repl, val)
96100

src/taskgraph/util/taskcluster.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333

3434

3535
@functools.lru_cache(maxsize=None)
36-
def get_root_url():
37-
if "TASKCLUSTER_PROXY_URL" in os.environ:
36+
def get_root_url(block_proxy=False):
37+
if "TASKCLUSTER_PROXY_URL" in os.environ and not block_proxy:
3838
logger.debug(
3939
"Using taskcluster-proxy at {}".format(os.environ["TASKCLUSTER_PROXY_URL"])
4040
)
@@ -140,8 +140,22 @@ def get_session():
140140
return requests_retry_session(retries=5)
141141

142142

143-
def get_artifact_url(task_id, path):
144-
artifact_tmpl = liburls.api(get_root_url(), "queue", "v1", "task/{}/artifacts/{}")
143+
def get_artifact_url(task_id, path, use_proxy=False):
144+
if use_proxy:
145+
try:
146+
url = liburls.normalize_root_url(os.environ["TASKCLUSTER_PROXY_URL"])
147+
except KeyError:
148+
if "TASK_ID" not in os.environ:
149+
raise RuntimeError(
150+
"taskcluster-proxy is not available when not executing in a task"
151+
)
152+
else:
153+
raise RuntimeError("taskcluster-proxy is not enabled for this task")
154+
155+
else:
156+
url = get_root_url(block_proxy=True)
157+
158+
artifact_tmpl = liburls.api(url, "queue", "v1", "task/{}/artifacts/{}")
145159
return artifact_tmpl.format(task_id, path)
146160

147161

test/test_util_taskcluster.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,41 @@ def test_get_root_url(monkeypatch):
6767

6868

6969
def test_get_artifact_url(monkeypatch):
70+
tc.get_root_url.cache_clear()
7071
task_id = "abc"
7172
path = "public/log.txt"
73+
expected = "https://tc.example.com/api/queue/v1/task/abc/artifacts/public/log.txt"
74+
expected_proxy = (
75+
"https://taskcluster-proxy.net/api/queue/v1/task/abc/artifacts/public/log.txt"
76+
)
7277

73-
# Test with default root URL
78+
# Test with default root URL (no proxy)
7479
tc.get_root_url.cache_clear()
75-
expected = "https://tc.example.com/api/queue/v1/task/abc/artifacts/public/log.txt"
7680
assert tc.get_artifact_url(task_id, path) == expected
7781

78-
# Test with proxy URL (takes priority)
82+
# Test that proxy URL is NOT used by default (since use_proxy defaults to False)
7983
monkeypatch.setenv("TASKCLUSTER_PROXY_URL", "https://taskcluster-proxy.net")
8084
tc.get_root_url.cache_clear()
81-
expected_proxy = (
82-
"https://taskcluster-proxy.net/api/queue/v1/task/abc/artifacts/public/log.txt"
85+
assert tc.get_artifact_url(task_id, path) == expected
86+
87+
# Test with use_proxy=True (should use proxy URL)
88+
assert tc.get_artifact_url(task_id, path, use_proxy=True) == expected_proxy
89+
90+
# Test with use_proxy=True but no proxy available (not in a task)
91+
monkeypatch.delenv("TASKCLUSTER_PROXY_URL")
92+
monkeypatch.delenv("TASK_ID", raising=False)
93+
tc.get_root_url.cache_clear()
94+
with pytest.raises(RuntimeError) as exc:
95+
tc.get_artifact_url(task_id, path, use_proxy=True)
96+
assert "taskcluster-proxy is not available when not executing in a task" in str(
97+
exc.value
8398
)
84-
assert tc.get_artifact_url(task_id, path) == expected_proxy
99+
100+
# Test with use_proxy=True but proxy not enabled (in a task without proxy)
101+
monkeypatch.setenv("TASK_ID", "some-task-id")
102+
with pytest.raises(RuntimeError) as exc:
103+
tc.get_artifact_url(task_id, path, use_proxy=True)
104+
assert "taskcluster-proxy is not enabled for this task" in str(exc.value)
85105

86106

87107
def test_get_artifact(responses, root_url):

0 commit comments

Comments
 (0)