Skip to content

Commit d722cda

Browse files
committed
chore(5692): revise usr login credentials validity check
1 parent 7444e68 commit d722cda

File tree

6 files changed

+39
-17
lines changed

6 files changed

+39
-17
lines changed

app/app/api/private-cloud/products/[licencePlate]/usage-metrics/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import createApiHandler from '@/core/api-handler';
55
import { OkResponse, UnauthorizedResponse } from '@/core/responses';
66
import { Cluster, ResourceRequestsEnv } from '@/prisma/client';
77
import { models } from '@/services/db';
8-
import { getPodMetrics } from '@/services/k8s/metrics';
8+
import { getUsageMetrics } from '@/services/k8s/metrics';
99
import { getPathParamSchema } from '../schema';
1010

1111
const queryParamSchema = z.object({
@@ -33,7 +33,7 @@ export const GET = apiHandler(async ({ queryParams, pathParams, session }) => {
3333
cluster = 'KLAB';
3434
}
3535

36-
const metrics = await getPodMetrics(
36+
const metrics = await getUsageMetrics(
3737
licencePlate,
3838
environmentLongNames[environment] as keyof ResourceRequestsEnv,
3939
cluster,

app/app/api/token-check/route.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ import { validateAllServiceAccountTokens } from './validations/service-account-t
99

1010
export const POST = createApiHandler({})(async () => {
1111
const [
12-
metricsTokens,
1312
keycloakServiceAccountCredentials,
1413
keycloakUserLoginCredentials,
14+
metricsTokens,
1515
serviceAccountTokens,
1616
chesCredentials,
1717
msGraphCredentials,
1818
] = await Promise.all([
19-
validateAllMetricsReaderTokens(),
2019
validateKeycloakServiceAccount(),
2120
validateKeycloakUserLogin(),
21+
validateAllMetricsReaderTokens(),
2222
validateAllServiceAccountTokens(),
2323
validateChesCredentials(),
2424
validateMsGraphCredentials(),
2525
]);
2626

2727
return NextResponse.json({
28-
metricsTokens,
2928
keycloakServiceAccountCredentials,
3029
keycloakUserLoginCredentials,
30+
metricsTokens,
3131
serviceAccountTokens,
3232
chesCredentials,
3333
msGraphCredentials,

app/app/api/token-check/validations/helpers.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import KcAdminClient from '@keycloak/keycloak-admin-client';
12
import { AuthorizationV1Api } from '@kubernetes/client-node';
3+
import { KEYCLOAK_ADMIN_CLIENT_ID, KEYCLOAK_ADMIN_CLIENT_SECRET } from '@/config';
24
import { Cluster } from '@/prisma/client';
35
import { configureKubeConfig } from '@/services/k8s/helpers';
46

@@ -45,3 +47,27 @@ export async function validateClientCredentials({ tokenUrl, clientId, clientSecr
4547
const token = await getClientCredentialsToken(tokenUrl, params);
4648
return Boolean(token);
4749
}
50+
51+
export async function validateKeycloakUserClientId(clientId: string) {
52+
try {
53+
const authUrl = `${process.env.AUTH_SERVER_URL}/realms/${process.env.AUTH_RELM}/protocol/openid-connect/auth`;
54+
55+
const params = new URLSearchParams({
56+
client_id: clientId,
57+
response_type: 'code',
58+
scope: 'openid profile email',
59+
redirect_uri: 'https://registry.developer.gov.bc.ca/',
60+
state: 'test',
61+
nonce: 'test',
62+
});
63+
64+
const res = await fetch(`${authUrl}?${params.toString()}`, {
65+
method: 'GET',
66+
redirect: 'manual',
67+
});
68+
69+
return res.status === 200 || res.status === 302;
70+
} catch {
71+
return false;
72+
}
73+
}

app/app/api/token-check/validations/keycloak-service-account-credentials.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { AUTH_SERVER_URL, AUTH_RELM, KEYCLOAK_ADMIN_CLIENT_ID, KEYCLOAK_ADMIN_CLIENT_SECRET } from '@/config';
1+
import { KEYCLOAK_ADMIN_CLIENT_ID, KEYCLOAK_ADMIN_CLIENT_SECRET } from '@/config';
22
import { validateClientCredentials } from './helpers';
33

44
export async function validateKeycloakServiceAccount() {
5+
const tokenUrl = `${process.env.AUTH_SERVER_URL}/realms/${process.env.AUTH_RELM}/protocol/openid-connect/token`;
6+
57
return await validateClientCredentials({
6-
tokenUrl: `${AUTH_SERVER_URL}/realms/${AUTH_RELM}/protocol/openid-connect/token`,
8+
tokenUrl: tokenUrl,
79
clientId: KEYCLOAK_ADMIN_CLIENT_ID,
810
clientSecret: KEYCLOAK_ADMIN_CLIENT_SECRET,
911
});
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
import { AUTH_RESOURCE, AUTH_SECRET } from '@/config';
2-
import { validateClientCredentials } from './helpers';
1+
import { AUTH_RESOURCE } from '@/config';
2+
import { validateKeycloakUserClientId } from './helpers';
33

44
export async function validateKeycloakUserLogin() {
5-
const tokenUrl = `${process.env.AUTH_SERVER_URL}/realms/${process.env.AUTH_RELM}/protocol/openid-connect/token`;
6-
7-
return await validateClientCredentials({
8-
tokenUrl,
9-
clientId: AUTH_RESOURCE,
10-
clientSecret: AUTH_SECRET,
11-
});
5+
return await validateKeycloakUserClientId(AUTH_RESOURCE);
126
}

app/services/k8s/metrics/usage-metrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async function getLastTwoWeeksAvgUsage(cluster: Cluster, namespace: string, podN
2121

2222
// Match memory usage for the same container
2323
const memoryUsageItem = usageMemory.find((memItem) => memItem.metric.container === containerName);
24-
const memoryUsage = memoryUsageItem && memoryUsageItem.value ? parseFloat(memoryUsageItem.value[1]) : 0;
24+
const memoryUsage = memoryUsageItem?.value ? parseFloat(memoryUsageItem.value[1]) : 0;
2525

2626
return {
2727
containerName,

0 commit comments

Comments
 (0)