17
17
from jupyter_server .utils import url_path_join
18
18
import tornado
19
19
import subprocess
20
- from datetime import datetime
21
- from datetime import datetime
22
20
from cachetools import TTLCache
21
+ import datetime
23
22
24
23
from google .cloud .jupyter_config .config import gcp_kernel_gateway_url
25
24
@@ -46,14 +45,20 @@ def get_cached_credentials():
46
45
if process .returncode == 0 :
47
46
config_data = json .loads (output )
48
47
credentials = {
49
- 'project_id' : config_data ['configuration' ]['properties' ]['core' ]['project' ],
50
- 'region_id' : config_data ['configuration' ]['properties' ]['compute' ]['region' ],
51
- 'access_token' : config_data ['credential' ]['access_token' ]
52
- }
48
+ 'project_id' : config_data ['configuration' ]['properties' ]['core' ]['project' ],
49
+ 'region_id' : config_data ['configuration' ]['properties' ]['compute' ]['region' ],
50
+ 'access_token' : config_data ['credential' ]['access_token' ]
51
+ }
53
52
54
53
token_expiry = config_data ['credential' ]['token_expiry' ]
55
- utc_datetime = datetime .strptime (token_expiry , '%Y-%m-%dT%H:%M:%SZ' )
56
- ttl_seconds = max (0 , (utc_datetime - datetime .utcnow ()).total_seconds ())
54
+ utc_datetime = datetime .datetime .strptime (token_expiry , '%Y-%m-%dT%H:%M:%SZ' )
55
+ current_utc_datetime = datetime .datetime .utcnow ()
56
+ expiry_timedelta = utc_datetime - current_utc_datetime
57
+ expiry_seconds = expiry_timedelta .total_seconds ()
58
+ if expiry_seconds > 1000 :
59
+ ttl_seconds = 1000
60
+ else :
61
+ ttl_seconds = expiry_seconds
57
62
credentials_cache = TTLCache (maxsize = 1 , ttl = ttl_seconds )
58
63
credentials_cache ['credentials' ] = credentials
59
64
return credentials
@@ -66,41 +71,41 @@ def get_cached_credentials():
66
71
process = subprocess .Popen (cmd , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
67
72
project , error = process .communicate ()
68
73
if project == '' :
69
- credentials = {
70
- 'project_id' : '' ,
71
- 'region_id' : '' ,
72
- 'access_token' : '' ,
73
- 'config_error' : 1 ,
74
- 'login_error' : 0
75
- }
74
+ credentials = {
75
+ 'project_id' : '' ,
76
+ 'region_id' : '' ,
77
+ 'access_token' : '' ,
78
+ 'config_error' : 1 ,
79
+ 'login_error' : 0
80
+ }
76
81
else :
77
82
cmd = "gcloud config get-value compute/region"
78
83
process = subprocess .Popen (cmd , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
79
84
region , error = process .communicate ()
80
85
if region == '' :
81
86
credentials = {
82
- 'project_id' : '' ,
83
- 'region_id' : '' ,
84
- 'access_token' : '' ,
85
- 'config_error' : 1 ,
86
- 'login_error' : 0
87
- }
87
+ 'project_id' : '' ,
88
+ 'region_id' : '' ,
89
+ 'access_token' : '' ,
90
+ 'config_error' : 1 ,
91
+ 'login_error' : 0
92
+ }
88
93
else :
89
94
credentials = {
90
- 'project_id' : '' ,
91
- 'region_id' : '' ,
92
- 'access_token' : '' ,
93
- 'config_error' : 0 ,
94
- 'login_error' : 1
95
- }
95
+ 'project_id' : '' ,
96
+ 'region_id' : '' ,
97
+ 'access_token' : '' ,
98
+ 'config_error' : 0 ,
99
+ 'login_error' : 1
100
+ }
96
101
else :
97
102
credentials = {
98
- 'project_id' : '' ,
99
- 'region_id' : '' ,
100
- 'access_token' : '' ,
101
- 'config_error' : 1 ,
102
- 'login_error' : 0
103
- }
103
+ 'project_id' : '' ,
104
+ 'region_id' : '' ,
105
+ 'access_token' : '' ,
106
+ 'config_error' : 1 ,
107
+ 'login_error' : 0
108
+ }
104
109
credentials_cache = TTLCache (maxsize = 1 , ttl = 5 )
105
110
credentials_cache ['credentials' ] = credentials
106
111
return credentials
0 commit comments