Skip to content

Commit

Permalink
add region support to db calls
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Mar 13, 2024
1 parent 771433f commit 3387898
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-clocks-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Fix an issue where short-lived tokens were not being authorized
24 changes: 19 additions & 5 deletions packages/db/src/core/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { homedir } from 'node:os';
import { join } from 'node:path';
import { pathToFileURL } from 'node:url';
import { MISSING_PROJECT_ID_ERROR, MISSING_SESSION_ID_ERROR } from './errors.js';
import { getAstroStudioEnv, getAstroStudioUrl, safeFetch } from './utils.js';
import { getAstroStudioEnv, getAstroStudioUrl, getRemoteDatabaseUrl, safeFetch } from './utils.js';

export const SESSION_LOGIN_FILE = pathToFileURL(join(homedir(), '.astro', 'session-token'));
export const PROJECT_ID_FILE = pathToFileURL(join(process.cwd(), '.astro', 'link'));
Expand All @@ -26,19 +26,31 @@ class ManagedLocalAppToken implements ManagedAppToken {
class ManagedRemoteAppToken implements ManagedAppToken {
token: string;
session: string;
region: string | undefined;
projectId: string;
ttl: number;
renewTimer: NodeJS.Timeout | undefined;

static async getRegionCode(): Promise<string | undefined> {
const pingResponse = await safeFetch(new URL(`${getRemoteDatabaseUrl()}/ping`), {
method: 'POST',
});
const requestId = pingResponse.headers.get('Fly-Request-Id');
const regionId = requestId?.match(/[^-]*$/)?.[0] || undefined;
return regionId;
}


static async create(sessionToken: string, projectId: string) {
const region = await ManagedRemoteAppToken.getRegionCode();
const response = await safeFetch(
new URL(`${getAstroStudioUrl()}/auth/cli/token-create`),
{
method: 'POST',
headers: new Headers({
Authorization: `Bearer ${sessionToken}`,
}),
body: JSON.stringify({ projectId }),
body: JSON.stringify({ projectId, region }),
},
(res) => {
throw new Error(`Failed to create token: ${res.status} ${res.statusText}`);
Expand All @@ -49,20 +61,22 @@ class ManagedRemoteAppToken implements ManagedAppToken {
return new ManagedRemoteAppToken({
token: shortLivedAppToken,
session: sessionToken,
region,
projectId,
ttl,
});
}

constructor(options: { token: string; session: string; projectId: string; ttl: number }) {
constructor(options: { token: string; session: string; region: string | undefined; projectId: string; ttl: number }) {
this.token = options.token;
this.session = options.session;
this.region = options.region;
this.projectId = options.projectId;
this.ttl = options.ttl;
this.renewTimer = setTimeout(() => this.renew(), (1000 * 60 * 5) / 2);
}

private async fetch(url: string, body: unknown) {
private async fetch(url: string, body: Record<string, unknown>) {
return safeFetch(
`${getAstroStudioUrl()}${url}`,
{
Expand All @@ -71,7 +85,7 @@ class ManagedRemoteAppToken implements ManagedAppToken {
Authorization: `Bearer ${this.session}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
body: JSON.stringify({...body, region: this.region}),
},
() => {
throw new Error(`Failed to fetch ${url}.`);
Expand Down

0 comments on commit 3387898

Please sign in to comment.