Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
costPluginPermissions,
} from '@red-hat-developer-hub/plugin-cost-management-common/permissions';
import { AuthorizeResult } from '@backstage/plugin-permission-common';
import { getTokenFromApi } from '../util/tokenUtil';
import { getTokenFromApi, SsoAuthenticationError } from '../util/tokenUtil';
import { DEFAULT_COST_MANAGEMENT_PROXY_BASE_URL } from '../util/constant';
import { resolveActor, emitAuditLog } from '../util/auditLog';

Expand Down Expand Up @@ -410,6 +410,15 @@ export const secureProxy: (options: RouterOptions) => RequestHandler =
return res.send(await upstreamResponse.text());
} catch (error) {
options.logger.error('Secure proxy error', error);

if (error instanceof SsoAuthenticationError) {
return res.status(502).json({
error:
'Unable to authenticate with the hybrid cloud console. ' +
'Please check your service account credentials (clientId/clientSecret) in the RHDH Cost Management configuration.',
});
}

return res.status(500).json({ error: 'Internal proxy error' });
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ import assert from 'assert';
import { RouterOptions } from '../models/RouterOptions';
import { DEFAULT_SSO_BASE_URL } from './constant';

export class SsoAuthenticationError extends Error {
constructor(message: string, public readonly statusCode: number) {
super(message);
this.name = 'SsoAuthenticationError';
}
}

// Cache key for token storage
const TOKEN_CACHE_KEY = 'sso_access_token';

Expand Down Expand Up @@ -100,7 +107,18 @@ export const getTokenFromApi = async (options: RouterOptions) => {

logger.info(`Token cached, expires in ${expires_in} seconds`);
} else {
throw new Error(rhSsoResponse.statusText);
let detail = '';
try {
const body = await rhSsoResponse.json();
detail = body.error_description || body.error || '';
} catch {
// response may not be JSON
}
const message = detail
? `SSO authentication failed: ${detail}`
: `SSO authentication failed with status ${rhSsoResponse.status} (${rhSsoResponse.statusText})`;
logger.error(message);
throw new SsoAuthenticationError(message, rhSsoResponse.status);
}

return accessToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ export class CostManagementSlimClient implements CostManagementSlimApi {
});

if (!response.ok) {
throw new Error(response.statusText);
let message = response.statusText;
try {
const body = (await response.json()) as any;
if (body.error) message = body.error;
} catch {
// response may not be JSON
}
throw new Error(message);
}

// Get the response data
Expand Down Expand Up @@ -518,7 +525,14 @@ export class CostManagementSlimClient implements CostManagementSlimApi {
});

if (!response.ok) {
throw new Error(response.statusText);
let message = response.statusText;
try {
const body = (await response.json()) as any;
if (body.error) message = body.error;
} catch {
// response may not be JSON
}
throw new Error(message);
}

return {
Expand All @@ -544,7 +558,14 @@ export class CostManagementSlimClient implements CostManagementSlimApi {
});

if (!response.ok) {
throw new Error(response.statusText);
let message = response.statusText;
try {
const body = (await response.json()) as any;
if (body.error) message = body.error;
} catch {
// response may not be JSON
}
throw new Error(message);
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ export class OptimizationsClient implements OptimizationsApi {
);

if (!response.ok) {
throw new Error(response.statusText);
let message = response.statusText;
try {
const body = (await response.json()) as any;
if (body.error) message = body.error;
} catch {
// response may not be JSON
}
throw new Error(message);
}

return {
Expand Down Expand Up @@ -133,7 +140,14 @@ export class OptimizationsClient implements OptimizationsApi {
);

if (!response.ok) {
throw new Error(response.statusText);
let message = response.statusText;
try {
const body = (await response.json()) as any;
if (body.error) message = body.error;
} catch {
// response may not be JSON
}
throw new Error(message);
}

return {
Expand Down
Loading