Skip to content

Commit 60f8883

Browse files
magreenblattsylvestre
authored andcommitted
feat: Parse GCS token expire time (#2415)
1 parent 8f0af50 commit 60f8883

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/cache/gcs.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
use chrono::{DateTime, Utc};
1617
use crate::cache::CacheMode;
1718
use crate::errors::*;
1819
use opendal::Operator;
@@ -102,10 +103,29 @@ impl GoogleTokenLoad for TaskClusterTokenLoader {
102103

103104
debug!("gcs: token load succeeded for scope: {}", &self.scope);
104105

105-
// TODO: we can parse expire time instead using hardcode 1 hour.
106+
// Default to 1 hour.
107+
let mut expires_in_usize = 3600;
108+
109+
// Parse ISO_INSTANT date/time format, e.g. "2011-12-03T10:15:30Z"
110+
match DateTime::parse_from_rfc3339(&resp.expire_time) {
111+
Ok(datetime) => {
112+
let delta = datetime.timestamp() - Utc::now().timestamp();
113+
if delta > 0 {
114+
expires_in_usize = delta as usize;
115+
} else {
116+
debug!("gcs: invalid (past dated) token expire_time: {}", &resp.expire_time);
117+
}
118+
},
119+
Err(e) => {
120+
debug!("gcs: failed to parse token expire_time: {}", e);
121+
}
122+
};
123+
124+
debug!("gcs: token expires in {} seconds", expires_in_usize);
125+
106126
Ok(Some(GoogleToken::new(
107127
&resp.access_token,
108-
3600,
128+
expires_in_usize,
109129
&self.scope,
110130
)))
111131
} else {

0 commit comments

Comments
 (0)