Skip to content

Commit c9312e8

Browse files
authored
Skip SSO Authorization (#2847)
* fix(dcm-backend): skip SSO token exchange when credentials are not configured When clientId or clientSecret are absent (empty string), getTokenFromApi now returns an empty token instead of attempting an SSO request that would always fail with a 502. The proxy handler skips the Authorization header when no token is present, forwarding requests unauthenticated to open API gateways. Made-with: Cursor * Replace tag latest with main * fix Docker Build * Make Propery Optional in config.d.ts file
1 parent 259d3b2 commit c9312e8

8 files changed

Lines changed: 45 additions & 40 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@red-hat-developer-hub/backstage-plugin-dcm-backend': patch
3+
---
4+
5+
Fix 502 error when SSO credentials are not configured.
6+
7+
The backend proxy now skips the SSO token exchange when `clientId` or
8+
`clientSecret` are absent, forwarding requests to the API gateway without
9+
an Authorization header instead of failing with "Failed to obtain upstream
10+
access token."

workspaces/dcm/Dockerfile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
# ── Stage 1: build ────────────────────────────────────────────────────────────
16-
FROM node:20-bookworm-slim AS build
16+
FROM node:22-bookworm-slim AS build
1717

1818
RUN corepack enable
1919

@@ -39,11 +39,11 @@ RUN yarn workspace app build
3939
RUN yarn workspace backend build
4040

4141
# ── Stage 2: production image ─────────────────────────────────────────────────
42-
FROM node:20-bookworm-slim
42+
FROM node:22-bookworm-slim
4343

4444
RUN corepack enable \
4545
&& apt-get update && apt-get install -y --no-install-recommends \
46-
python3 make g++ \
46+
python3 make g++ g++-13 \
4747
&& rm -rf /var/lib/apt/lists/*
4848

4949
WORKDIR /app
@@ -60,7 +60,8 @@ RUN node -e " \
6060
if (pkg.scripts) pkg.scripts.postinstall = 'true'; \
6161
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2)); \
6262
" \
63-
&& YARN_ENABLE_IMMUTABLE_INSTALLS=false \
63+
&& CXX=g++-13 \
64+
YARN_ENABLE_IMMUTABLE_INSTALLS=false \
6465
YARN_CACHE_FOLDER=/root/.yarn/berry/cache \
6566
yarn workspaces focus --all --production \
6667
&& rm -rf "$(yarn cache dir)"

workspaces/dcm/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"private": true,
55
"engines": {
6-
"node": "18 || 20"
6+
"node": "18 || 20 || 22"
77
},
88
"scripts": {
99
"start": "concurrently -c auto -n \"fe,be\" -p \"{name}:{pid}\" \"yarn start-app\" \"yarn start-backend\"",
@@ -56,6 +56,8 @@
5656
"typescript": "~5.3.0"
5757
},
5858
"resolutions": {
59+
"isolated-vm": "6.0.2",
60+
"better-sqlite3": "^12.0.0",
5961
"@types/react": "^18",
6062
"@types/react-dom": "^18",
6163
"fsevents": "~2.3.2",

workspaces/dcm/plugins/dcm-backend/config.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ export interface Config {
4242
*
4343
* @visibility secret
4444
*/
45-
clientId: string;
45+
clientId?: string;
4646

4747
/**
4848
* SSO client secret used to obtain a bearer token for upstream API calls.
4949
*
5050
* @visibility secret
5151
*/
52-
clientSecret: string;
52+
clientSecret?: string;
5353
};
5454
}

workspaces/dcm/plugins/dcm-backend/src/routes/proxy.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,16 @@ export function createDcmProxy(options: RouterOptions) {
7575
}
7676

7777
const requestHeaders: Record<string, string> = {
78-
Authorization: `Bearer ${tokenResult.accessToken}`,
7978
Accept: (req.headers.accept as string) || 'application/json',
8079
};
8180

81+
// Only attach the Authorization header when an SSO token was obtained.
82+
// When clientId/clientSecret are not configured the token is empty and
83+
// the request is forwarded without auth (open/unauthenticated gateway).
84+
if (tokenResult.accessToken) {
85+
requestHeaders.Authorization = `Bearer ${tokenResult.accessToken}`;
86+
}
87+
8288
// Forward Content-Type for requests that carry a body
8389
if (req.headers['content-type']) {
8490
requestHeaders['Content-Type'] = req.headers['content-type'] as string;

workspaces/dcm/plugins/dcm-backend/src/util/tokenUtil.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,20 @@ export const getTokenFromApi = async (
5151
return cachedToken;
5252
}
5353

54+
const clientId = config.getOptionalString('dcm.clientId') ?? '';
55+
const clientSecret = config.getOptionalString('dcm.clientSecret') ?? '';
56+
57+
if (!clientId || !clientSecret) {
58+
logger.debug(
59+
'DCM token: clientId/clientSecret not configured — skipping SSO token exchange',
60+
);
61+
return { accessToken: '', expiresAt: 0 };
62+
}
63+
5464
logger.info('DCM token: requesting new access token from SSO');
5565

5666
const ssoBaseUrl =
5767
config.getOptionalString('dcm.ssoBaseUrl') ?? DEFAULT_SSO_BASE_URL;
58-
const clientId = config.getString('dcm.clientId');
59-
const clientSecret = config.getString('dcm.clientSecret');
6068

6169
const tokenUrl = `${ssoBaseUrl}/auth/realms/redhat-external/protocol/openid-connect/token`;
6270
const body = new URLSearchParams({

workspaces/dcm/scripts/generate-image.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,13 @@ function backstage-image {
219219
"$_pocker" build \
220220
"${volume_args[@]+"${volume_args[@]}"}" \
221221
--tag "$image_tag" \
222-
--tag "$REGISTRY_URL/$ORG_ID/$REPO:latest" \
222+
--tag "$REGISTRY_URL/$ORG_ID/$REPO:main" \
223223
"$workspace_dir"
224224

225225
if $do_push; then
226226
echo "Pushing $image_tag"
227227
"$_pocker" push "$image_tag"
228-
"$_pocker" push "$REGISTRY_URL/$ORG_ID/$REPO:latest"
228+
"$_pocker" push "$REGISTRY_URL/$ORG_ID/$REPO:main"
229229
else
230230
echo "Image built. Run with --push to push to the registry, or use:"
231231
echo " $_pocker push $image_tag"

workspaces/dcm/yarn.lock

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17275,17 +17275,6 @@ __metadata:
1727517275
languageName: node
1727617276
linkType: hard
1727717277

17278-
"better-sqlite3@npm:^11.0.0":
17279-
version: 11.10.0
17280-
resolution: "better-sqlite3@npm:11.10.0"
17281-
dependencies:
17282-
bindings: "npm:^1.5.0"
17283-
node-gyp: "npm:latest"
17284-
prebuild-install: "npm:^7.1.1"
17285-
checksum: 10/5e4c7437c4fe6033335a79c82974d7ab29f33c51c36f48b73e87e087d21578468575de1c56a7badd4f76f17255e25abefddaeacf018e5eeb9e0cb8d6e3e4a5e1
17286-
languageName: node
17287-
linkType: hard
17288-
1728917278
"better-sqlite3@npm:^12.0.0":
1729017279
version: 12.6.2
1729117280
resolution: "better-sqlite3@npm:12.6.2"
@@ -17297,17 +17286,6 @@ __metadata:
1729717286
languageName: node
1729817287
linkType: hard
1729917288

17300-
"better-sqlite3@npm:^9.0.0":
17301-
version: 9.6.0
17302-
resolution: "better-sqlite3@npm:9.6.0"
17303-
dependencies:
17304-
bindings: "npm:^1.5.0"
17305-
node-gyp: "npm:latest"
17306-
prebuild-install: "npm:^7.1.1"
17307-
checksum: 10/06b3d95221071a06c2e22a9746d9b7049c0bce7962e5e3290ccf088fffbf4d4d52868f0d98b8ae2565fe33b1adab89823145f23c6f6eb63ecc4fc1b883f9082c
17308-
languageName: node
17309-
linkType: hard
17310-
1731117289
"bfj@npm:^8.0.0":
1731217290
version: 8.0.0
1731317291
resolution: "bfj@npm:8.0.0"
@@ -24358,13 +24336,13 @@ __metadata:
2435824336
languageName: node
2435924337
linkType: hard
2436024338

24361-
"isolated-vm@npm:^5.0.1":
24362-
version: 5.0.4
24363-
resolution: "isolated-vm@npm:5.0.4"
24339+
"isolated-vm@npm:6.0.2":
24340+
version: 6.0.2
24341+
resolution: "isolated-vm@npm:6.0.2"
2436424342
dependencies:
2436524343
node-gyp: "npm:latest"
24366-
prebuild-install: "npm:^7.1.2"
24367-
checksum: 10/f48e69ecf907645711d0a372cb6adb28cf72499e34b6e008ed597994bfd90d41dd11dc478a41fc21a25aaef424ab5a95a372286e4daf7f61e231d028c0fd64ec
24344+
prebuild-install: "npm:^7.1.3"
24345+
checksum: 10/74e97f13678023bf81141a6fb5c91bc179073a024e7f0a568af60d876b781b15b11e02d4012558e7d583e38a553ccccff70fd02645ed5d7bed2150dc3921fa64
2436824346
languageName: node
2436924347
linkType: hard
2437024348

@@ -29705,7 +29683,7 @@ __metadata:
2970529683
languageName: node
2970629684
linkType: hard
2970729685

29708-
"prebuild-install@npm:^7.1.1, prebuild-install@npm:^7.1.2":
29686+
"prebuild-install@npm:^7.1.1, prebuild-install@npm:^7.1.3":
2970929687
version: 7.1.3
2971029688
resolution: "prebuild-install@npm:7.1.3"
2971129689
dependencies:

0 commit comments

Comments
 (0)