From 7410c1dc71af63a8ff9aa0d82f551a4e6b5b86a8 Mon Sep 17 00:00:00 2001 From: sylvain-morin Date: Wed, 19 Jun 2024 18:41:27 +0200 Subject: [PATCH] Display build info endpoint --- config.docker.js | 6 +-- .../v2/app_build_info_controller.js | 43 +++++++++++++++++++ lib/controllers/v2/build_info_controller.js | 2 +- lib/util.js | 10 ----- openapi/paths/v2/app_build_info.js | 36 ++++++++++++++++ openapi/paths/v2/build_info.js | 4 +- openapi/schema/request/app_build_info.js | 3 ++ openapi/schema/response/app_build_info.js | 22 ++++++++++ package-lock.json | 17 -------- package.json | 3 +- 10 files changed, 111 insertions(+), 35 deletions(-) create mode 100644 lib/controllers/v2/app_build_info_controller.js create mode 100644 openapi/paths/v2/app_build_info.js create mode 100644 openapi/schema/request/app_build_info.js create mode 100644 openapi/schema/response/app_build_info.js diff --git a/config.docker.js b/config.docker.js index c4438a77..1b805709 100644 --- a/config.docker.js +++ b/config.docker.js @@ -21,8 +21,7 @@ const { INAT_AWS_OPENDATA_REGION, INAT_AWS_OPENDATA_ACL, INAT_TAXA_FILE_PATH, - INAT_SEEK_EXCEPTION_LIST_ID, - INAT_INTERNAL_IP_RANGES + INAT_SEEK_EXCEPTION_LIST_ID } = process.env; module.exports = { @@ -70,6 +69,5 @@ module.exports = { host: INAT_ES_HOST ? `http://${INAT_ES_HOST}:9200` : "http://localhost:9200" }, cacheDir: "/home/inaturalist/api/cache", - seekExceptionListID: INAT_SEEK_EXCEPTION_LIST_ID || 0, - internalIPs: INAT_INTERNAL_IP_RANGES || "" + seekExceptionListID: INAT_SEEK_EXCEPTION_LIST_ID || 0 }; diff --git a/lib/controllers/v2/app_build_info_controller.js b/lib/controllers/v2/app_build_info_controller.js new file mode 100644 index 00000000..aa8d6e78 --- /dev/null +++ b/lib/controllers/v2/app_build_info_controller.js @@ -0,0 +1,43 @@ +const { build_info } = require( "inaturalistjs" ); +const process = require( "process" ); +const fetch = require( "node-fetch" ); +const InaturalistAPI = require( "../../inaturalist_api" ); +const util = require( "../../util" ); +const config = require( "../../../config" ); + +const index = async req => { + if ( !req.userSession || ( req.userSession && !req.userSession.isAdmin ) ) { + throw util.httpError( 401, "Unauthorized" ); + } + // Get Rails Build Info + const railsBuildInfoJSON = await InaturalistAPI.iNatJSWrap( build_info.get, req ); + // Get API Build Info + const apiBuildInfoJSON = { + git_branch: process.env.GIT_BRANCH, + git_commit: process.env.GIT_COMMIT, + image_tag: process.env.IMAGE_TAG, + build_date: process.env.BUILD_DATE + }; + // Get Vision Build Info + let visionBuildInfoJSON = {}; + try { + const response = await fetch( `${config.imageProcesing.tensorappURL}/build_info` ); + if ( !response.ok ) { + throw util.httpError( 500, "Error" ); + } + visionBuildInfoJSON = await response.json( ); + } catch ( error ) { + throw util.httpError( 500, "Error" ); + } + // Application Build Info + const appBuildInfoJSON = { + rails: railsBuildInfoJSON, + api: apiBuildInfoJSON, + vision: visionBuildInfoJSON + }; + return appBuildInfoJSON; +}; + +module.exports = { + index +}; diff --git a/lib/controllers/v2/build_info_controller.js b/lib/controllers/v2/build_info_controller.js index b4f479ae..283fdc6f 100644 --- a/lib/controllers/v2/build_info_controller.js +++ b/lib/controllers/v2/build_info_controller.js @@ -2,7 +2,7 @@ const process = require( "process" ); const util = require( "../../util" ); const index = async req => { - if ( !util.isInternalRequest( req ) ) { + if ( !req.userSession || ( req.userSession && !req.userSession.isAdmin ) ) { throw util.httpError( 401, "Unauthorized" ); } const buildInfoJSON = { diff --git a/lib/util.js b/lib/util.js index 84db5155..72a32034 100644 --- a/lib/util.js +++ b/lib/util.js @@ -2,7 +2,6 @@ const _ = require( "lodash" ); const moment = require( "moment" ); const md5 = require( "md5" ); -const ipRangeCheck = require( "ip-range-check" ); const config = require( "../config" ); const Logstasher = require( "./logstasher" ); @@ -503,15 +502,6 @@ const util = class util { return true; } - static isInternalRequest( req ) { - const internalIPs = config.internalIPs || ""; - const allowedRanges = internalIPs.split( "," ); - const clientIp = req.headers["x-forwarded-for"] || req.connection.remoteAddress; - console.log( "### internalIPs = " + allowedRanges ); - console.log( "### clientIp = " + clientIp ); - return allowedRanges.some( range => ipRangeCheck( clientIp, range ) ); - } - // Utility to look up IDs from UUIDs to forward requests to the v1 API static async uuidsToSerialIds( req, model ) { // eslint-disable-next-line global-require diff --git a/openapi/paths/v2/app_build_info.js b/openapi/paths/v2/app_build_info.js new file mode 100644 index 00000000..ba8421e0 --- /dev/null +++ b/openapi/paths/v2/app_build_info.js @@ -0,0 +1,36 @@ +const AppBuildInfoController = require( "../../../lib/controllers/v2/app_build_info_controller" ); + +module.exports = sendWrapper => { + async function GET( req, res ) { + const results = await AppBuildInfoController.index( req ); + sendWrapper( req, res, null, results ); + } + + GET.apiDoc = { + tags: ["AppBuildInfo"], + summary: "Display application build information", + security: [{ + userJwtRequired: [] + }], + responses: { + 200: { + description: "Application build information", + content: { + "application/json": { + schema: { + $ref: "#/components/schemas/AppBuildInfo" + } + } + } + }, + default: { + $ref: "#/components/responses/Error" + } + }, + "x-unpublished": true + }; + + return { + GET + }; +}; diff --git a/openapi/paths/v2/build_info.js b/openapi/paths/v2/build_info.js index a56f5388..7efe8249 100644 --- a/openapi/paths/v2/build_info.js +++ b/openapi/paths/v2/build_info.js @@ -1,4 +1,3 @@ -const j2s = require( "joi-to-swagger" ); const BuildInfoController = require( "../../../lib/controllers/v2/build_info_controller" ); module.exports = sendWrapper => { @@ -10,6 +9,9 @@ module.exports = sendWrapper => { GET.apiDoc = { tags: ["BuildInfo"], summary: "Display build information", + security: [{ + userJwtRequired: [] + }], responses: { 200: { description: "Build information", diff --git a/openapi/schema/request/app_build_info.js b/openapi/schema/request/app_build_info.js new file mode 100644 index 00000000..c79897cb --- /dev/null +++ b/openapi/schema/request/app_build_info.js @@ -0,0 +1,3 @@ +const Joi = require( "joi" ); + +module.exports = Joi.object( ).keys( {} ); diff --git a/openapi/schema/response/app_build_info.js b/openapi/schema/response/app_build_info.js new file mode 100644 index 00000000..87484938 --- /dev/null +++ b/openapi/schema/response/app_build_info.js @@ -0,0 +1,22 @@ +const Joi = require( "joi" ); + +module.exports = Joi.object( ).keys( { + rails: Joi.object( ).keys( { + git_branch: Joi.string( ), + git_commit: Joi.string( ), + image_tag: Joi.string( ), + build_date: Joi.string( ) + } ), + api: Joi.object( ).keys( { + git_branch: Joi.string( ), + git_commit: Joi.string( ), + image_tag: Joi.string( ), + build_date: Joi.string( ) + } ), + vision: Joi.object( ).keys( { + git_branch: Joi.string( ), + git_commit: Joi.string( ), + image_tag: Joi.string( ), + build_date: Joi.string( ) + } ) +} ).unknown( false ); diff --git a/package-lock.json b/package-lock.json index 92db5282..3221f46e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34,7 +34,6 @@ "handlebars": "^4.7.7", "inaturalistjs": "github:inaturalist/inaturalistjs", "intl": "^1.2.5", - "ip-range-check": "^0.2.0", "joi": "^17.5.0", "joi-to-swagger": "^6.1.1", "js-yaml": "^4.1.0", @@ -7077,14 +7076,6 @@ "node": ">= 0.10" } }, - "node_modules/ip-range-check": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/ip-range-check/-/ip-range-check-0.2.0.tgz", - "integrity": "sha512-oaM3l/3gHbLlt/tCWLvt0mj1qUaI+STuRFnUvARGCujK9vvU61+2JsDpmkMzR4VsJhuFXWWgeKKVnwwoFfzCqw==", - "dependencies": { - "ipaddr.js": "^1.0.1" - } - }, "node_modules/ipaddr.js": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", @@ -16972,14 +16963,6 @@ "sprintf-js": "1.1.2" } }, - "ip-range-check": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/ip-range-check/-/ip-range-check-0.2.0.tgz", - "integrity": "sha512-oaM3l/3gHbLlt/tCWLvt0mj1qUaI+STuRFnUvARGCujK9vvU61+2JsDpmkMzR4VsJhuFXWWgeKKVnwwoFfzCqw==", - "requires": { - "ipaddr.js": "^1.0.1" - } - }, "ipaddr.js": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", diff --git a/package.json b/package.json index 5e8c1a03..30236c45 100644 --- a/package.json +++ b/package.json @@ -41,9 +41,8 @@ "geoip-lite": "^1.4.3", "h3-js": "^4.1.0", "handlebars": "^4.7.7", - "inaturalistjs": "github:inaturalist/inaturalistjs", + "inaturalistjs": "github:inaturalist/inaturalistjs#0828cb8fb22736ffe78b737edea793405a158ce8", "intl": "^1.2.5", - "ip-range-check": "^0.2.0", "joi": "^17.5.0", "joi-to-swagger": "^6.1.1", "js-yaml": "^4.1.0",