From 3c2ce636e731b36fd3d2f1ec3167d6417d47c5f9 Mon Sep 17 00:00:00 2001 From: J Date: Wed, 24 Feb 2021 22:32:20 +0000 Subject: [PATCH] interface: storing the gcp token --- pkg/interface/src/logic/api/gcp.ts | 27 +++++++++++++++++++++----- pkg/interface/src/logic/store/store.ts | 4 +++- pkg/interface/src/types/gcp-state.ts | 2 +- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/pkg/interface/src/logic/api/gcp.ts b/pkg/interface/src/logic/api/gcp.ts index 4069ec0384..05791db22c 100644 --- a/pkg/interface/src/logic/api/gcp.ts +++ b/pkg/interface/src/logic/api/gcp.ts @@ -4,21 +4,38 @@ import {GcpToken} from '../types/gcp-state'; export default class GcpApi extends BaseApi { + #running = false; + startRefreshLoop() { - // TODO: don't allow more than one - this.refreshLoop(); + if (this.#running) { + console.error('GcpApi startRefreshLoop: already running'); + } else { + this.#running = true; + this.refreshLoop(); + } } private refreshLoop() { - console.log("refreshing GCP token"); + console.log("GcpApi refreshLoop"); this.refreshToken().then(({accessKey, expiresIn}: GcpToken) => { - console.log("new token: ", accessKey); + console.log("GcpApi new token"); + // XX maybe bad? + this.store.state.gcp.accessKey = accessKey; + const timeout = this.refreshInterval(expiresIn); + console.log("GcpApi refreshing in", timeout); setTimeout(() => { this.refreshLoop(); - }, expiresIn); + }, timeout); }); } + private refreshInterval(expiresIn: number) { + // Give ourselves 30 seconds for processing delays, but never refresh + // sooner than 10 minute from now. (The expiry window should be about an + // hour.) + return Math.max(600_000, expiresIn - 30_000); + } + private async refreshToken() { const token = await this.spider('noun', 'gcp-token', 'get-gcp-token', {}); return token['gcp-token']; diff --git a/pkg/interface/src/logic/store/store.ts b/pkg/interface/src/logic/store/store.ts index cd06ae91cb..17d5e497ef 100644 --- a/pkg/interface/src/logic/store/store.ts +++ b/pkg/interface/src/logic/store/store.ts @@ -71,7 +71,9 @@ export default class GlobalStore extends BaseStore { }, weather: {}, userLocation: null, - gcp: {}, + gcp: { + accessKey: null + }, s3: { configuration: { buckets: new Set(), diff --git a/pkg/interface/src/types/gcp-state.ts b/pkg/interface/src/types/gcp-state.ts index 8d24f12c17..41ebe12501 100644 --- a/pkg/interface/src/types/gcp-state.ts +++ b/pkg/interface/src/types/gcp-state.ts @@ -4,5 +4,5 @@ export interface GcpToken { }; export interface GcpState { - token: GcpToken; + accessKey: string; };