Skip to content

[results-processor] Provide GitHub token for api.github.com #4025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions results-processor/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import time
import traceback
import zipfile
from urllib.parse import urlparse
from urllib.parse import urlparse, urlsplit

import requests
from google.cloud import datastore
Expand All @@ -28,6 +28,9 @@ class Processor(object):
USERNAME = '_processor'
# Timeout waiting for remote HTTP servers to respond
TIMEOUT_WAIT = 10
# GitHub API metadata
GITHUB_API_VERSION = '2022-11-28'
GITHUB_API_HOSTNAME = 'api.github.com'

def __init__(self):
# Delay creating Datastore.client so that tests don't need creds.
Expand Down Expand Up @@ -112,6 +115,15 @@ def known_extension(path):
return e
return None

def _secret(self, token_name):
_log.info('Reading secret: %s', token_name)
key = self.datastore.key('Token', token_name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all paths that trigger this code are guarded by _internal_only. Which ensures that '/api/results/process' is only called by the task queue. So we are good. But any other code starts to use this in the future, we probably want to have some more control over reading this secret when hitting '/api/results/process'. Or at least some more logging to tell help.

The other thing that helps: We are only doing GET requests with the token, so the damage is limited.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Figuring out how to pass that through to the processor (which doesn't inherently rely on Flask/main/etc. today) seems kinda difficult. We can certainly add logging, though?

Copy link
Collaborator

@jcscottiii jcscottiii Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a follow up issue for this? We can address that part later.

Something around logging which uploader is accessing the secret.

After you add the issue and the lint errors, feel free to merge.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #4044

return self.datastore.get(key)['secret']

@property
def _github_token(self):
return self._secret('github-wpt-fyi-bot-token')

def _download_gcs(self, gcs):
assert gcs.startswith('gs://')
ext = self.known_extension(gcs)
Expand All @@ -123,15 +135,31 @@ def _download_gcs(self, gcs):

def _download_http(self, url):
assert url.startswith('http://') or url.startswith('https://')
_log.debug("Downloading %s", url)
_log.debug('Downloading %s', url)
extra_headers = None
if urlsplit(url).hostname == self.GITHUB_API_HOSTNAME:
extra_headers = {
'Authorization': 'Bearer ' + self._github_token,
'X-GitHub-Api-Version': self.GITHUB_API_VERSION,
}
try:
r = requests.get(url, stream=True, timeout=self.TIMEOUT_WAIT)
r = requests.get(
url,
headers=extra_headers,
stream=True,
timeout=self.TIMEOUT_WAIT,
)
r.raise_for_status()
except requests.RequestException:
# Sleep 1 second and retry.
time.sleep(1)
try:
r = requests.get(url, stream=True, timeout=self.TIMEOUT_WAIT)
r = requests.get(
url,
headers=extra_headers,
stream=True,
timeout=self.TIMEOUT_WAIT,
)
r.raise_for_status()
except requests.Timeout:
_log.error("Timed out fetching: %s", url)
Expand Down