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 13, 2024
1 parent 804ff08 commit 4e35684
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 2 deletions.
6 changes: 4 additions & 2 deletions config.docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const {
INAT_AWS_OPENDATA_REGION,
INAT_AWS_OPENDATA_ACL,
INAT_TAXA_FILE_PATH,
INAT_SEEK_EXCEPTION_LIST_ID
INAT_SEEK_EXCEPTION_LIST_ID,
INAT_INTERNAL_IP_RANGES
} = process.env;

module.exports = {
Expand Down Expand Up @@ -69,5 +70,6 @@ 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
seekExceptionListID: INAT_SEEK_EXCEPTION_LIST_ID || 0,
internalIPs: INAT_INTERNAL_IP_RANGES || ""
};
19 changes: 19 additions & 0 deletions lib/controllers/v2/build_info_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const process = require( "process" );
const util = require( "../../util" );

const index = async req => {
if ( !util.isInternalRequest( req ) ) {
throw util.httpError( 401, "Unauthorized" );
}
const buildInfoJSON = {
git_branch: process.env.GIT_BRANCH,
git_commit: process.env.GIT_COMMIT,
image_tag: process.env.IMAGE_TAG,
build_date: process.env.BUILD_DATE
};
return buildInfoJSON;
};

module.exports = {
index
};
10 changes: 10 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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 @@ -502,6 +503,15 @@ 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 );

Check failure on line 510 in lib/util.js

View workflow job for this annotation

GitHub Actions / build-and-test / Build/Test

Unexpected string concatenation
console.log( "### clientIp = " + clientIp );

Check failure on line 511 in lib/util.js

View workflow job for this annotation

GitHub Actions / build-and-test / Build/Test

Unexpected string concatenation
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
34 changes: 34 additions & 0 deletions openapi/paths/v2/build_info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const j2s = require( "joi-to-swagger" );

Check failure on line 1 in openapi/paths/v2/build_info.js

View workflow job for this annotation

GitHub Actions / build-and-test / Build/Test

'j2s' is assigned a value but never used
const BuildInfoController = require( "../../../lib/controllers/v2/build_info_controller" );

module.exports = sendWrapper => {
async function GET( req, res ) {
const results = await BuildInfoController.index( req );
sendWrapper( req, res, null, results );
}

GET.apiDoc = {
tags: ["BuildInfo"],
summary: "Display build information",
responses: {
200: {
description: "Build information",
content: {
"application/json": {
schema: {
$ref: "#/components/schemas/BuildInfo"
}
}
}
},
default: {
$ref: "#/components/responses/Error"
}
},
"x-unpublished": true
};

return {
GET
};
};
3 changes: 3 additions & 0 deletions openapi/schema/request/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( {} );
8 changes: 8 additions & 0 deletions openapi/schema/response/build_info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Joi = require( "joi" );

module.exports = Joi.object( ).keys( {
git_branch: Joi.string( ),
git_commit: Joi.string( ),
image_tag: Joi.string( ),
build_date: Joi.string( )
} ).unknown( false );
17 changes: 17 additions & 0 deletions package-lock.json

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

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

0 comments on commit 4e35684

Please sign in to comment.