Skip to content

Commit d320c7c

Browse files
authored
Authenticate site -> catalog requests (#1414)
1 parent 1275884 commit d320c7c

File tree

5 files changed

+63
-10
lines changed

5 files changed

+63
-10
lines changed

cloud-build/deploy-main.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ substitutions:
1313
_IMAGE_URL_CATALOG: us.gcr.io/${PROJECT_ID}/main/catalog:${SHORT_SHA}
1414
_IMAGE_URL_SITE: us.gcr.io/${PROJECT_ID}/main/site:${SHORT_SHA}
1515
_IMAGE_CACHE_TTL: 168h # 1 week
16-
_CATALOG_GRAPHQL_URL: https://${_TAG}---catalog-khswqo4xea-wl.a.run.app
16+
_CATALOG_SERVER_AUTH_ID: https://catalog-khswqo4xea-wl.a.run.app
17+
_CATALOG_GRAPHQL_URL: https://${_TAG}---catalog-khswqo4xea-wl.a.run.app/graphql
1718

1819
steps:
1920
# Build catalog Docker image.
@@ -82,7 +83,7 @@ steps:
8283
- --concurrency=200
8384
- --min-instances=1
8485
- --max-instances=1000
85-
- --update-env-vars=CATALOG_GRAPHQL_URL=${_CATALOG_GRAPHQL_URL}
86+
- --update-env-vars=CATALOG_GRAPHQL_URL=${_CATALOG_GRAPHQL_URL},CATALOG_SERVER_AUTH_ID=${_CATALOG_SERVER_AUTH_ID}
8687

8788
# Route traffic to new catalog revision.
8889
- id: route-catalog

cloud-build/deploy-pr.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ substitutions:
1313
_IMAGE_URL_CATALOG: us.gcr.io/${PROJECT_ID}/pr/catalog:${SHORT_SHA}
1414
_IMAGE_URL_SITE: us.gcr.io/${PROJECT_ID}/pr/site:${SHORT_SHA}
1515
_IMAGE_CACHE_TTL: 168h # 1 week
16-
_CATALOG_GRAPHQL_URL: https://${_TAG}---catalog-khswqo4xea-wl.a.run.app
16+
_CATALOG_SERVER_AUTH_ID: https://catalog-khswqo4xea-wl.a.run.app
17+
_CATALOG_GRAPHQL_URL: https://${_TAG}---catalog-khswqo4xea-wl.a.run.app/graphql
1718

1819
steps:
1920
# Build catalog Docker image.
@@ -82,4 +83,4 @@ steps:
8283
- --concurrency=default # Unlimited
8384
- --min-instances=0
8485
- --max-instances=1
85-
- --update-env-vars=CATALOG_GRAPHQL_URL=${_CATALOG_GRAPHQL_URL}
86+
- --update-env-vars=CATALOG_GRAPHQL_URL=${_CATALOG_GRAPHQL_URL},CATALOG_SERVER_AUTH_ID=${_CATALOG_SERVER_AUTH_ID}

package-lock.json

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/site-server/package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
},
6868
"start:dev": {
6969
"command": "node --enable-source-maps ./lib/dev-server.js",
70+
"env": {
71+
"CATALOG_GRAPHQL_URL": "http://localhost:6451/graphql"
72+
},
7073
"service": {
7174
"readyWhen": {
7275
"lineMatches": "Web Dev Server started"
@@ -79,7 +82,14 @@
7982
},
8083
"start:prod": {
8184
"command": "node --enable-source-maps ./lib/prod-server.js",
82-
"service": true,
85+
"env": {
86+
"CATALOG_GRAPHQL_URL": "http://localhost:6451/graphql"
87+
},
88+
"service": {
89+
"readyWhen": {
90+
"lineMatches": "serving"
91+
}
92+
},
8393
"files": [],
8494
"dependencies": [
8595
"build:prod"
@@ -106,6 +116,7 @@
106116
"@types/marked": "^4.0.8",
107117
"@web/dev-server": "^0.1.34",
108118
"@webcomponents/internal-site-content": "^0.0.0",
119+
"google-auth-library": "^8.7.0",
109120
"koa": "^2.13.4",
110121
"koa-conditional-get": "^3.0.0",
111122
"koa-etag": "^4.0.0",

packages/site-server/src/lib/catalog/graphql.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,50 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import {ApolloClient, InMemoryCache} from '@apollo/client/core/index.js';
7+
import {
8+
ApolloClient,
9+
HttpLink,
10+
InMemoryCache,
11+
} from '@apollo/client/core/index.js';
12+
import {GoogleAuth} from 'google-auth-library';
813

9-
const CATALOG_GRAPHQL_URL =
10-
process.env['CATALOG_GRAPHQL_URL'] || `http://localhost:6451/graphql`;
14+
const CATALOG_GRAPHQL_URL = process.env['CATALOG_GRAPHQL_URL'];
15+
if (!CATALOG_GRAPHQL_URL) {
16+
throw new Error('CATALOG_GRAPHQL_URL must be set');
17+
}
18+
19+
let linkFetch: typeof fetch | undefined = undefined;
20+
if (process.env['K_SERVICE']) {
21+
// We're on Cloud Run, as opposed to local, so our cross-service requests need
22+
// to be authenticated. The K_SERVICE environment variable is set by Cloud
23+
// Run, see
24+
// https://cloud.google.com/run/docs/reference/container-contract#env-vars.
25+
const CATALOG_SERVER_AUTH_ID = process.env['CATALOG_SERVER_AUTH_ID'];
26+
if (!CATALOG_SERVER_AUTH_ID) {
27+
throw new Error('CATALOG_SERVER_AUTH_ID must be set');
28+
}
29+
const auth = new GoogleAuth();
30+
linkFetch = async (
31+
input: RequestInfo | URL,
32+
init?: RequestInit | undefined
33+
): Promise<Response> => {
34+
const authClient = await auth.getIdTokenClient(CATALOG_SERVER_AUTH_ID);
35+
const authHeaders = await authClient.getRequestHeaders();
36+
const headers = {
37+
...(init?.headers ?? {}),
38+
...authHeaders,
39+
};
40+
return fetch(input, {
41+
...(init ?? {}),
42+
headers,
43+
});
44+
};
45+
}
1146

1247
export const client = new ApolloClient({
13-
uri: CATALOG_GRAPHQL_URL,
48+
link: new HttpLink({
49+
uri: CATALOG_GRAPHQL_URL,
50+
fetch: linkFetch,
51+
}),
1452
cache: new InMemoryCache(),
1553
});

0 commit comments

Comments
 (0)