Skip to content

Commit 53c677f

Browse files
committed
Authentication for metrics and version endpoint
Signed-off-by: naveenpaul1 <[email protected]>
1 parent 05da04a commit 53c677f

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

docs/NooBaaNonContainerized/ConfigFileCustomizations.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ Warning: After setting this configuration, NooBaa will skip schema validations a
449449
"LOG_TO_STDERR_ENABLED": false
450450
3. systemctl restart noobaa
451451
```
452-
### 30. Notification log directory
452+
### 31. Notification log directory
453453
* <u>Key</u> `NOTIFICATION_LOG_DIR`
454454
* <u>Type</u> String
455455
* <u>Default</u> empty
@@ -462,7 +462,7 @@ Warning: After setting this configuration, NooBaa will skip schema validations a
462462
"NOTIFICATION_LOG_DIR": "/etc/notif"
463463
3. systemctl restart noobaa
464464
465-
### 31. Prometheus HTTP enable flag -
465+
### 32. Prometheus HTTP enable flag -
466466
* <u>Key</u>: `ALLOW_HTTP_METRICS`
467467
* <u>Type</u>: Boolean
468468
* <u>Default</u>: true
@@ -476,7 +476,7 @@ Warning: After setting this configuration, NooBaa will skip schema validations a
476476
3. systemctl restart noobaa
477477
```
478478
479-
### 32. Prometheus HTTPS enable flag -
479+
### 33. Prometheus HTTPS enable flag -
480480
* <u>Key</u>: `ALLOW_HTTPS_METRICS`
481481
* <u>Type</u>: Boolean
482482
* <u>Default</u>: true
@@ -490,7 +490,7 @@ Warning: After setting this configuration, NooBaa will skip schema validations a
490490
3. systemctl restart noobaa
491491
```
492492
493-
### 33. Notification space monitor frequency flag -
493+
### 34. Notification space monitor frequency flag -
494494
* <u>Key</u>: `NOTIFICATION_REQ_PER_SPACE_CHECK`
495495
* <u>Type</u>: Positive integer
496496
* <u>Default</u>: 0
@@ -504,7 +504,7 @@ Warning: After setting this configuration, NooBaa will skip schema validations a
504504
3. systemctl restart noobaa
505505
```
506506
507-
### 34. Notification space monitor threshold flag -
507+
### 35. Notification space monitor threshold flag -
508508
* <u>Key</u>: `NOTIFICATION_SPACE_CHECK_THRESHOLD`
509509
* <u>Type</u>: Number
510510
* <u>Default</u>: 0.1
@@ -518,7 +518,7 @@ Warning: After setting this configuration, NooBaa will skip schema validations a
518518
3. systemctl restart noobaa
519519
```
520520
521-
### 34. Dynamic supplemental groups allocation flag -
521+
### 36. Dynamic supplemental groups allocation flag -
522522
* <u>Key</u>: `NSFS_ENABLE_DYNAMIC_SUPPLEMENTAL_GROUPS`
523523
* <u>Type</u>: boolean
524524
* <u>Default</u>: true

src/server/analytic_services/prometheus_reporting.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ async function start_server(
6969
return;
7070
}
7171
const metrics_request_handler = async (req, res) => {
72+
// TODO: This is a temporary condition to avoid the token authentication for metrics,
73+
// Need to add authentication for NSFS NC also after confirming flow with the Scale team
74+
if (!process.env.NC_NSFS_NO_DB_ENV) {
75+
// Authorize bearer token metrics endpoint
76+
http_utils.authorize_bearer(req, res, [ "metrics-auth", "admin" ]);
77+
}
7278
// Serve all metrics on the root path for system that do have one or more fork running.
7379
if (fork_enabled) {
7480
// we would like this part to be first as clusterMetrics might fail.

src/server/web_server.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ async function get_log_level_handler(req, res) {
218218
}
219219

220220
async function get_version_handler(req, res) {
221+
// Authorize bearer token version endpoint
222+
http_utils.authorize_bearer(req, res, [ "metrics-auth", "admin" ]);
221223
const { status, version } = await getVersion(req.url);
222224
if (version) res.send(version);
223225
if (status !== 200) res.status(status);

src/util/http_utils.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,43 @@ function set_response_headers_from_request(req, res) {
946946
if (req.query['response-expires']) res.setHeader('Expires', req.query['response-expires']);
947947
}
948948

949+
/**
950+
* autheticate jwt token for prometheus metrics and version request
951+
* @param {nb.S3Request} req
952+
* @param {nb.S3Response} res
953+
* @param {string[]} roles
954+
*/
955+
function authorize_bearer(req, res, roles) {
956+
let token = req.headers.authorization;
957+
try {
958+
if (!token) {
959+
dbg.error('Missing authorization header : ', token);
960+
throw new S3Error(S3Error.AccessDenied);
961+
}
962+
if (!token.includes('Bearer ')) {
963+
dbg.error('JWT token is missing the Bearer prefix, ', token);
964+
throw new S3Error(S3Error.AccessDenied);
965+
}
966+
token = token.split(' ')[1];
967+
const decoded = jwt_utils.authorize_jwt_token(token);
968+
// Role 'metrics-auth' is used in operator RPC call,
969+
// If needs to change update operator RPC call first
970+
if (!roles.includes(decoded.role)) {
971+
dbg.error('Bearer token authorization failed : ', token);
972+
throw new S3Error(S3Error.AccessDenied);
973+
}
974+
} catch (err) {
975+
dbg.error('JWT verification failed for token : ', token, err);
976+
res.writeHead(403, { 'Content-Type': 'application/json' });
977+
const reply = JSON.stringify({
978+
error: 'AccessDenied',
979+
message: err.message,
980+
}, null, 2);
981+
res.end(reply);
982+
return false;
983+
}
984+
}
985+
949986
exports.parse_url_query = parse_url_query;
950987
exports.parse_client_ip = parse_client_ip;
951988
exports.get_md_conditions = get_md_conditions;
@@ -984,3 +1021,4 @@ exports.CONTENT_TYPE_APP_JSON = CONTENT_TYPE_APP_JSON;
9841021
exports.CONTENT_TYPE_APP_XML = CONTENT_TYPE_APP_XML;
9851022
exports.CONTENT_TYPE_APP_FORM_URLENCODED = CONTENT_TYPE_APP_FORM_URLENCODED;
9861023
exports.set_response_headers_from_request = set_response_headers_from_request;
1024+
exports.authorize_bearer = authorize_bearer;

src/util/jwt_utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ function make_internal_auth_token(object = {}, jwt_options = {}) {
2727
return make_auth_token(object, jwt_options);
2828
}
2929

30+
/**
31+
* authorize jwt token by verifying it against the jwt secret
32+
* @param {string} token
33+
*/
3034
function authorize_jwt_token(token) {
3135
try {
3236
return jwt.verify(token, get_jwt_secret());

0 commit comments

Comments
 (0)