Skip to content

Commit

Permalink
Display build info endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvain-morin committed Jun 19, 2024
1 parent 4e35684 commit 7410c1d
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 35 deletions.
6 changes: 2 additions & 4 deletions config.docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
};
43 changes: 43 additions & 0 deletions lib/controllers/v2/app_build_info_controller.js
Original file line number Diff line number Diff line change
@@ -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
};
2 changes: 1 addition & 1 deletion lib/controllers/v2/build_info_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
10 changes: 0 additions & 10 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" );

Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions openapi/paths/v2/app_build_info.js
Original file line number Diff line number Diff line change
@@ -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
};
};
4 changes: 3 additions & 1 deletion openapi/paths/v2/build_info.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const j2s = require( "joi-to-swagger" );
const BuildInfoController = require( "../../../lib/controllers/v2/build_info_controller" );

module.exports = sendWrapper => {
Expand All @@ -10,6 +9,9 @@ module.exports = sendWrapper => {
GET.apiDoc = {
tags: ["BuildInfo"],
summary: "Display build information",
security: [{
userJwtRequired: []
}],
responses: {
200: {
description: "Build information",
Expand Down
3 changes: 3 additions & 0 deletions openapi/schema/request/app_build_info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const Joi = require( "joi" );

module.exports = Joi.object( ).keys( {} );
22 changes: 22 additions & 0 deletions openapi/schema/response/app_build_info.js
Original file line number Diff line number Diff line change
@@ -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 );
17 changes: 0 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 7410c1d

Please sign in to comment.