Skip to content

Commit 57b59bf

Browse files
author
ocaisa
authored
Merge pull request #3632 from boegel/github_token_new_format
update validate_github_token function to accept GitHub token in new format
2 parents 15f1d82 + 5509a09 commit 57b59bf

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

easybuild/tools/github.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,14 +2156,22 @@ def validate_github_token(token, github_user):
21562156
* see if it conforms expectations (only [a-f]+[0-9] characters, length of 40)
21572157
* see if it can be used for authenticated access
21582158
"""
2159-
sha_regex = re.compile('^[0-9a-f]{40}')
2159+
# cfr. https://github.blog/2021-04-05-behind-githubs-new-authentication-token-formats/
2160+
token_regex = re.compile('^ghp_[a-zA-Z0-9]{36}$')
2161+
token_regex_old_format = re.compile('^[0-9a-f]{40}$')
21602162

21612163
# token should be 40 characters long, and only contain characters in [0-9a-f]
2162-
sanity_check = bool(sha_regex.match(token))
2164+
sanity_check = bool(token_regex.match(token))
21632165
if sanity_check:
21642166
_log.info("Sanity check on token passed")
21652167
else:
2166-
_log.warning("Sanity check on token failed; token doesn't match pattern '%s'", sha_regex.pattern)
2168+
_log.warning("Sanity check on token failed; token doesn't match pattern '%s'", token_regex.pattern)
2169+
sanity_check = bool(token_regex_old_format.match(token))
2170+
if sanity_check:
2171+
_log.info("Sanity check on token (old format) passed")
2172+
else:
2173+
_log.warning("Sanity check on token failed; token doesn't match pattern '%s'",
2174+
token_regex_old_format.pattern)
21672175

21682176
# try and determine sha of latest commit in easybuilders/easybuild-easyconfigs repo through authenticated access
21692177
sha = None
@@ -2173,6 +2181,7 @@ def validate_github_token(token, github_user):
21732181
except Exception as err:
21742182
_log.warning("An exception occurred when trying to use token for authenticated GitHub access: %s", err)
21752183

2184+
sha_regex = re.compile('^[0-9a-f]{40}$')
21762185
token_test = bool(sha_regex.match(sha or ''))
21772186
if token_test:
21782187
_log.info("GitHub token can be used for authenticated GitHub access, validation passed")

test/framework/github.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,11 @@ def test_validate_github_token(self):
547547

548548
self.assertTrue(gh.validate_github_token(self.github_token, GITHUB_TEST_ACCOUNT))
549549

550+
# if a token in the old format is available, test with that too
551+
token_old_format = os.getenv('TEST_GITHUB_TOKEN_OLD_FORMAT')
552+
if token_old_format:
553+
self.assertTrue(gh.validate_github_token(token_old_format, GITHUB_TEST_ACCOUNT))
554+
550555
def test_find_easybuild_easyconfig(self):
551556
"""Test for find_easybuild_easyconfig function"""
552557
if self.skip_github_tests:

0 commit comments

Comments
 (0)