Skip to content

Commit de8552f

Browse files
authored
[Fix] Fix the issue that token will be outdated after 24 hours (#8386)
co-authorized by: jianquanye@microsoft.com Approach What is the motivation for this PR? Fix the issue that token will be outdated after 24 hours How did you do it? Refresh the token periodically.
1 parent 8e3b006 commit de8552f

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

.azure-pipelines/test_plan.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import sys
88
import time
9+
from datetime import datetime, timedelta
910

1011
import requests
1112
import yaml
@@ -14,6 +15,7 @@
1415
__metaclass__ = type
1516
PR_TEST_SCRIPTS_FILE = "pr_test_scripts.yaml"
1617
TOLERATE_HTTP_EXCEPTION_TIMES = 20
18+
TOKEN_EXPIRE_HOURS = 6
1719

1820

1921
class TestPlanStatus(Enum):
@@ -142,11 +144,21 @@ def __init__(self, url, tenant_id=None, client_id=None, client_secret=None):
142144
self.tenant_id = tenant_id
143145
self.client_id = client_id
144146
self.client_secret = client_secret
145-
self.token = None
147+
self.with_auth = False
148+
self._token = None
149+
self._token_generate_time = None
146150
if self.tenant_id and self.client_id and self.client_secret:
147-
self._get_token(url)
151+
self.with_auth = True
152+
self.get_token()
153+
154+
def get_token(self):
155+
token_generate_time_valid = \
156+
self._token_generate_time is not None and \
157+
(datetime.utcnow() - self._token_generate_time) < timedelta(hours=TOKEN_EXPIRE_HOURS)
158+
159+
if self._token is not None and token_generate_time_valid:
160+
return self._token
148161

149-
def _get_token(self, testbed_tools_url):
150162
token_url = "https://login.microsoftonline.com/{}/oauth2/v2.0/token".format(self.tenant_id)
151163
headers = {
152164
"Content-Type": "application/x-www-form-urlencoded"
@@ -156,13 +168,15 @@ def _get_token(self, testbed_tools_url):
156168
"grant_type": "client_credentials",
157169
"client_id": self.client_id,
158170
"client_secret": self.client_secret,
159-
"scope": get_scope(testbed_tools_url)
171+
"scope": get_scope(self.url)
160172
}
161173
try:
162174
resp = requests.post(token_url, headers=headers, data=payload, timeout=10).json()
163-
self.token = resp["access_token"]
164-
except Exception as e:
165-
raise Exception("Get token failed with exception: {}".format(repr(e)))
175+
self._token = resp["access_token"]
176+
self._token_generate_time = datetime.utcnow()
177+
return self._token
178+
except Exception as exception:
179+
raise Exception("Get token failed with exception: {}".format(repr(exception)))
166180

167181
def create(self, topology, test_plan_name="my_test_plan", deploy_mg_extra_params="", kvm_build_id="",
168182
min_worker=1, max_worker=2, pr_id="unknown", output=None,
@@ -234,7 +248,7 @@ def create(self, topology, test_plan_name="my_test_plan", deploy_mg_extra_params
234248
})
235249
print('Creating test plan with payload: {}'.format(payload))
236250
headers = {
237-
"Authorization": "Bearer {}".format(self.token),
251+
"Authorization": "Bearer {}".format(self.get_token()),
238252
"scheduler-site": "PRTest",
239253
"Content-Type": "application/json"
240254
}
@@ -268,7 +282,7 @@ def cancel(self, test_plan_id):
268282

269283
payload = json.dumps({})
270284
headers = {
271-
"Authorization": "Bearer {}".format(self.token),
285+
"Authorization": "Bearer {}".format(self.get_token()),
272286
"scheduler-site": "PRTest",
273287
"Content-Type": "application/json"
274288
}
@@ -295,8 +309,8 @@ def poll(self, test_plan_id, interval=60, timeout=-1, expected_state=""):
295309
headers = {
296310
"Content-Type": "application/json"
297311
}
298-
if self.token:
299-
headers["Authorization"] = "Bearer {}".format(self.token)
312+
if self.with_auth:
313+
headers["Authorization"] = "Bearer {}".format(self.get_token())
300314
start_time = time.time()
301315
http_exception_times = 0
302316
while (timeout < 0 or (time.time() - start_time) < timeout):

0 commit comments

Comments
 (0)