Skip to content

Commit 3811f41

Browse files
authored
Support private repos (#13)
* Temporarily print branch * Attempt to get and print fake secret * Add rootUrl in attempt to fix secrets fetch error * Try mimicking taskcluster-gha ts code for getting secrets * Try another way * Try gather_secrets from decision task to prevent leaks * Try even earlier * Still no * Ah, probably was missing filtered print * Experiment * Test sanity by doing it the wrong way * Try again * Revert "Try again" This reverts commit db5d1c8. * Revert "Test sanity by doing it the wrong way" This reverts commit bc904dd. * Try this way * Attempt to get commit message with authentication * Python understandably didn't like that * Sanity check * Use API url * Diff cleanup * Remove test print * Attempt to get build config with authentication * Add comment about necessity for secrets and preventing accidental leaks * Refactor and test still secure * Remove test * Fix config fetch secret * Attempt refactor and verify security * Don't repeat getting github token, move to utils * Don't print full commit message in addition to shortened one * Revert "Temporarily print branch" This reverts commit 0183e08.
1 parent f226694 commit 3811f41

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

decisionlib.py

+22-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
import utils
3030
import yaml
3131

32+
# The decision task needs access to secrets in order to support private repos.
33+
# Replace standard print with filtered_print and gather_secrets (as in runner.py)
34+
# to prevent accidental secrets leaking
35+
from utils import github_token
36+
from runner import filtered_print, gather_secrets
37+
print = filtered_print
38+
gather_secrets()
3239

3340
# Public API
3441
__all__ = [
@@ -107,13 +114,15 @@ def index_path(self):
107114
def commit_message(self):
108115
if self._commit_message is None:
109116
print("Getting commit message")
110-
print(
111-
f"https://github.com/{os.environ['REPO_FULL_NAME']}/commit/{self.git_sha}.patch"
112-
)
113-
commit = requests.get(
114-
f"https://github.com/{os.environ['REPO_FULL_NAME']}/commit/{self.git_sha}.patch"
115-
).text
116-
print(commit)
117+
url = f"https://api.github.com/repos/{os.environ['REPO_FULL_NAME']}/commits/{self.git_sha}"
118+
print(url)
119+
120+
headers = {
121+
"Authorization": f"token {github_token()}",
122+
"Accept": "application/vnd.github.v3.patch",
123+
}
124+
commit = requests.get(url, headers=headers).text
125+
# print(commit)
117126
self._commit_message = commit.split("diff --git a/")[0]
118127
print(self._commit_message)
119128
return self._commit_message
@@ -122,8 +131,12 @@ def commit_message(self):
122131
def tc_config(self):
123132
if self._tc_config is None:
124133
try:
125-
config = requests.get(
126-
f"https://raw.githubusercontent.com/{os.environ['REPO_FULL_NAME']}/{self.git_sha}/.build-config.yml").text
134+
url = f"https://raw.githubusercontent.com/{os.environ['REPO_FULL_NAME']}/{self.git_sha}/.build-config.yml"
135+
headers = {
136+
"Authorization": f"token {github_token()}",
137+
"Accept": "application/vnd.github.v3.raw",
138+
}
139+
config = requests.get(url, headers=headers).text
127140
self._tc_config = yaml.load(config, Loader=yaml.FullLoader)
128141
except yaml.YAMLError:
129142
raise

utils.py

+14
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,17 @@ def create_extra_artifact(path: str, content: bytes, public=False):
6969
result = loop.run_until_complete(coro)
7070
loop.close()
7171
return result
72+
73+
74+
def secrets():
75+
client = taskcluster.Secrets({
76+
"rootUrl": os.environ["TASKCLUSTER_PROXY_URL"]
77+
})
78+
secrets = client.get("divvun")
79+
loadedSecrets = secrets["secret"]
80+
return loadedSecrets
81+
82+
83+
def github_token():
84+
sec = secrets()
85+
return sec["github"]["token"]

0 commit comments

Comments
 (0)