-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be51792
commit a3341e3
Showing
2 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import type { Express } from 'express'; | ||
import { adminOnlyAuth } from '../auth/middleware.js'; | ||
import rateLimit from 'express-rate-limit'; | ||
import getLogger from '../../logger.js'; | ||
import { getEnv } from '../../env.js'; | ||
import { generateJwt, decodeJwt } from '../auth/functions.js'; | ||
|
||
export default (app: Express) => { | ||
const logger = getLogger('packages/hasura/hasura-events'); | ||
const { RATE_LIMITER_LOGIN_MAX } = getEnv(); | ||
|
||
const refreshLimiter = rateLimit({ | ||
legacyHeaders: false, | ||
max: RATE_LIMITER_LOGIN_MAX, | ||
standardHeaders: true, | ||
windowMs: 15 * 60 * 1000, // 15 minutes | ||
}); | ||
|
||
/** | ||
* @swagger | ||
* /modelExtraction: | ||
* post: | ||
* security: | ||
* - bearerAuth: [] | ||
* consumes: | ||
* - application/json | ||
* produces: | ||
* - application/json | ||
* parameters: | ||
* - in: header | ||
* name: x-hasura-role | ||
* schema: | ||
* type: string | ||
* required: false | ||
* requestBody: | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* missionModelId: | ||
* type: integer | ||
* responses: | ||
* 200: | ||
* description: ExtractionResponse | ||
* 403: | ||
* description: Unauthorized error | ||
* 401: | ||
* description: Unauthenticated error | ||
* summary: Request extraction of a Mission Model's JAR | ||
* tags: | ||
* - Hasura | ||
*/ | ||
app.post('/modelExtraction', refreshLimiter, adminOnlyAuth, async (req, res) => { | ||
const { jwtPayload } = decodeJwt(req.get('authorization')); | ||
const username = jwtPayload?.username as string; | ||
|
||
const { body } = req; | ||
const { missionModelId } = body; | ||
|
||
// Invoke endpoints using the Hasura Metadata API | ||
const { HASURA_METADATA_API_URL: metadataURL } = getEnv(); | ||
|
||
// Generate a temporary token that has Hasura Admin access | ||
const tempToken = generateJwt(username, 'admin', ['admin'], '10s'); | ||
|
||
const headers = { | ||
Authorization: `Bearer ${tempToken}`, | ||
'Content-Type': 'application/json', | ||
'x-hasura-role': 'admin', | ||
'x-hasura-user-id': username, | ||
}; | ||
const payload = { id: missionModelId }; | ||
|
||
const activityTypesRQ = { | ||
args: { | ||
name: 'refreshActivityTypes', | ||
payload, | ||
source: 'Aerie', | ||
}, | ||
type: 'pg_invoke_event_trigger', | ||
}; | ||
const modelParametersRQ = { | ||
args: { | ||
name: 'refreshModelParameters', | ||
payload, | ||
source: 'Aerie', | ||
}, | ||
type: 'pg_invoke_event_trigger', | ||
}; | ||
const resourceTypesRQ = { | ||
args: { | ||
name: 'refreshResourceTypes', | ||
payload, | ||
source: 'Aerie', | ||
}, | ||
type: 'pg_invoke_event_trigger', | ||
}; | ||
|
||
const refreshActivityTypes = fetch(metadataURL, { | ||
body: JSON.stringify(activityTypesRQ), | ||
headers, | ||
method: 'POST', | ||
}); | ||
const refreshModelParameters = fetch(metadataURL, { | ||
body: JSON.stringify(modelParametersRQ), | ||
headers, | ||
method: 'POST', | ||
}); | ||
const refreshResourceTypes = fetch(metadataURL, { | ||
body: JSON.stringify(resourceTypesRQ), | ||
headers, | ||
method: 'POST', | ||
}); | ||
|
||
// Awaiting all requests in parallel | ||
const [activityTypeResp, modelParameterResp, resourceTypeResp] = await Promise.all([ | ||
refreshActivityTypes, | ||
refreshModelParameters, | ||
refreshResourceTypes, | ||
]); | ||
|
||
logger.info(`POST /modelExtraction: Extraction triggered for model: ${missionModelId}`); | ||
|
||
res.json({ | ||
message: `Extraction triggered for model: ${missionModelId}`, | ||
response: { | ||
activity_types: await activityTypeResp.json(), | ||
model_parameters: await modelParameterResp.json(), | ||
resource_types: await resourceTypeResp.json(), | ||
}, | ||
}); | ||
}); | ||
}; |