Skip to content

Commit 2e81ab9

Browse files
committed
fix: add retry for waiting on tools server
1 parent 51c1319 commit 2e81ab9

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

src/jwt-verification.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import { createRemoteJWKSet, JWTPayload, jwtVerify } from 'jose'
22
import Debug from 'debug'
33
import retry from 'async-retry'
4-
import { cleanEnv, JWT_AUDIENCE, SSO_ISSUER, SSO_JWKS_URI } from 'src/validators'
4+
import {
5+
cleanEnv,
6+
JWT_AUDIENCE,
7+
SSO_ISSUER,
8+
SSO_JWKS_URI,
9+
STARTUP_RETRY_COUNT,
10+
STARTUP_RETRY_INTERVAL_MS,
11+
} from 'src/validators'
512

613
const debug = Debug('otomi:jwt')
7-
const env = cleanEnv({ SSO_ISSUER, JWT_AUDIENCE, SSO_JWKS_URI })
14+
const env = cleanEnv({ SSO_ISSUER, JWT_AUDIENCE, SSO_JWKS_URI, STARTUP_RETRY_COUNT, STARTUP_RETRY_INTERVAL_MS })
815
const JWKS_URL = env.SSO_JWKS_URI
916

1017
// Create remote JWKS - automatically caches and refreshes keys
@@ -42,7 +49,7 @@ export async function verifyJwt(token: string): Promise<AppJWTPayload> {
4249
}
4350
} catch (error: any) {
4451
debug('JWT verification failed:', error.message)
45-
throw new Error(`JWT verification failed: ${error.message}`)
52+
throw error
4653
}
4754
}
4855

@@ -74,9 +81,9 @@ export async function waitForJwksReady(): Promise<void> {
7481
}
7582
},
7683
{
77-
retries: 300, // 10 minutes total with 2s minTimeout
78-
minTimeout: 2000,
79-
maxTimeout: 2000,
84+
retries: env.STARTUP_RETRY_COUNT,
85+
minTimeout: env.STARTUP_RETRY_INTERVAL_MS,
86+
maxTimeout: env.STARTUP_RETRY_INTERVAL_MS,
8087
},
8188
)
8289
}

src/middleware/jwt.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ export function jwtMiddleware(): RequestHandler {
8787

8888
return next()
8989
} catch (error: any) {
90-
debug('JWT verification failed:', error.message)
9190
return res.status(401).send({
9291
message: 'Unauthorized',
9392
error: `JWT verification failed: ${error.message}`,

src/utils.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import axios from 'axios'
2+
import retry from 'async-retry'
3+
import Debug from 'debug'
24
import cleanDeep, { CleanOptions } from 'clean-deep'
35
import { pathExists } from 'fs-extra'
46
import { readdir, readFile } from 'fs/promises'
@@ -7,10 +9,14 @@ import cloneDeep from 'lodash/cloneDeep'
79
import { Cluster, Dns } from 'src/otomi-models'
810
import { parse, stringify } from 'yaml'
911
import { BASEURL } from './constants'
10-
import { cleanEnv, GIT_PASSWORD } from './validators'
12+
import { cleanEnv, GIT_PASSWORD, STARTUP_RETRY_COUNT, STARTUP_RETRY_INTERVAL_MS } from './validators'
13+
14+
const debug = Debug('otomi:utils')
1115

1216
const env = cleanEnv({
1317
GIT_PASSWORD,
18+
STARTUP_RETRY_COUNT,
19+
STARTUP_RETRY_INTERVAL_MS,
1420
})
1521

1622
export function arrayToObject(array: [] = [], keyName = 'name', keyValue = 'value'): Record<string, unknown> {
@@ -85,7 +91,25 @@ const valuesSchemaEndpointUrl = `${BASEURL}/apl/schema`
8591
let valuesSchema: Record<string, any>
8692

8793
export const getValuesSchema = async (): Promise<Record<string, any>> => {
88-
const res = await axios.get(valuesSchemaEndpointUrl)
94+
debug('Fetching values schema from tools server...')
95+
96+
const res = await retry(
97+
async () => {
98+
try {
99+
return await axios.get(valuesSchemaEndpointUrl)
100+
} catch (error: any) {
101+
debug(`Tools server not ready yet (${error.code}), retrying...`)
102+
throw error
103+
}
104+
},
105+
{
106+
retries: env.STARTUP_RETRY_COUNT,
107+
minTimeout: env.STARTUP_RETRY_INTERVAL_MS,
108+
maxTimeout: env.STARTUP_RETRY_INTERVAL_MS,
109+
},
110+
)
111+
112+
debug('Values schema fetched successfully')
89113
valuesSchema = omit(res.data, ['definitions'])
90114
return valuesSchema
91115
}

src/validators.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ export const AGENT_KIND = str({
146146
desc: 'The kind for AkamaiAgent custom resources',
147147
default: 'AkamaiAgent',
148148
})
149+
export const STARTUP_RETRY_COUNT = num({
150+
desc: 'Number of retries for startup dependencies (JWKS, tools server)',
151+
default: 300,
152+
})
153+
export const STARTUP_RETRY_INTERVAL_MS = num({
154+
desc: 'Retry interval in milliseconds for startup dependencies',
155+
default: 1000,
156+
})
149157
const { env } = process
150158
export function cleanEnv<T>(validators: { [K in keyof T]: ValidatorSpec<T[K]> }, options: CleanOptions<T> = {}) {
151159
if (env.NODE_ENV === 'test') {

0 commit comments

Comments
 (0)