forked from noobaa/noobaa-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnsfs_schema_utils.js
More file actions
136 lines (120 loc) · 4.88 KB
/
Copy pathnsfs_schema_utils.js
File metadata and controls
136 lines (120 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Copyright (C) 2023 NooBaa */
'use strict';
const _ = require('lodash');
const RpcError = require('../rpc/rpc_error');
const { default: Ajv } = require('ajv');
const ajv = new Ajv({ verbose: true, allErrors: true });
const { KEYWORDS } = require('../util/schema_keywords');
const common_api = require('../api/common_api');
const schema_utils = require('../util/schema_utils');
const config = require('../../config');
ajv.addKeyword(KEYWORDS.methods);
ajv.addKeyword(KEYWORDS.doc);
ajv.addKeyword(KEYWORDS.date);
ajv.addKeyword(KEYWORDS.idate);
ajv.addKeyword(KEYWORDS.objectid);
ajv.addKeyword(KEYWORDS.binary);
ajv.addKeyword(KEYWORDS.wrapper);
ajv.addSchema(common_api);
const bucket_schema = require('../server/system_services/schemas/nsfs_bucket_schema');
const account_schema = require('../server/system_services/schemas/nsfs_account_schema');
const nsfs_config_schema = require('../server/system_services/schemas/nsfs_config_schema');
const log_schema = require('../server/system_services/schemas/log_schema');
_.each(common_api.definitions, schema => {
schema_utils.strictify(schema, {
additionalProperties: false
});
});
schema_utils.strictify(bucket_schema, {
additionalProperties: false
});
schema_utils.strictify(account_schema, {
additionalProperties: false
});
schema_utils.strictify(nsfs_config_schema, {});
const validate_account = ajv.compile(account_schema);
const validate_bucket = ajv.compile(bucket_schema);
const validate_nsfs_config = ajv.compile(nsfs_config_schema);
const validate_logging = ajv.compile(log_schema);
/**
* validate_account_schema validates an account object against the NC NSFS account schema
* Same schema used for account (root account), IAM user and IAM role
* @param {object} account
*/
function validate_account_schema(account) {
const valid = validate_account(account);
if (!valid) {
const first_err = validate_account.errors[0];
const err_msg = first_err.message ? create_schema_err_msg(first_err) : undefined;
if (config.NC_DISABLE_SCHEMA_CHECK === true) return warn_invalid_schema('account', account, err_msg);
throw new RpcError('INVALID_SCHEMA', err_msg);
}
}
/**
* validate_bucket_schema validates a bucket object against the NC NSFS bucket schema
* @param {object} bucket
*/
function validate_bucket_schema(bucket) {
const valid = validate_bucket(bucket);
if (!valid) {
const first_err = validate_bucket.errors[0];
const err_msg = first_err.message ? create_schema_err_msg(first_err) : undefined;
if (config.NC_DISABLE_SCHEMA_CHECK === true) return warn_invalid_schema('bucket', bucket, err_msg);
throw new RpcError('INVALID_SCHEMA', err_msg);
}
}
/**
* validate_nsfs_config_schema validates a config object against the NC NSFS config schema
* @param {object} nsfs_config
*/
function validate_nsfs_config_schema(nsfs_config) {
const valid = validate_nsfs_config(nsfs_config);
if (!valid) {
const first_err = validate_nsfs_config.errors[0];
const err_msg = first_err.message ? create_schema_err_msg(first_err) : undefined;
if (nsfs_config.NC_DISABLE_SCHEMA_CHECK === true ||
(nsfs_config.NC_DISABLE_SCHEMA_CHECK === undefined && config.NC_DISABLE_SCHEMA_CHECK === true)) {
return warn_invalid_schema('nsfs_config', nsfs_config, err_msg);
}
throw new RpcError('INVALID_SCHEMA', err_msg);
}
}
/**
* warn_invalid_schema warns of invalid schema
* @param {string} type
* @param {Object} invalid_schema
* @param {string} err_msg
*/
function warn_invalid_schema(type, invalid_schema, err_msg) {
console.warn(`nsfs_schema_utils ${type} ${invalid_schema} is invalid, schema check is disabled, skipping - err=${err_msg}`);
}
/**
* validate_log_schema validates a logging object against the NC NSFS log schema
* @param {object} log
*/
function validate_log_schema(log) {
const valid = validate_logging(log);
if (!valid) {
const first_err = validate_logging.errors[0];
const err_msg = first_err.message ? create_schema_err_msg(first_err) : undefined;
if (config.NC_DISABLE_SCHEMA_CHECK === true) return warn_invalid_schema('logging', log, err_msg);
throw new RpcError('INVALID_SCHEMA', err_msg);
}
}
/**
* create_schema_err_msg would use the original error message we got from avj
* and adds additional info in case we have it
* @param {object} err
*/
function create_schema_err_msg(err) {
if (!err || !err.message) return;
const err_msg = [err.message];
if (err.params) err_msg.push(JSON.stringify(err.params));
if (err.instancePath) err_msg.push(JSON.stringify(err.instancePath));
return err_msg.join(' | ');
}
//EXPORTS
exports.validate_account_schema = validate_account_schema;
exports.validate_bucket_schema = validate_bucket_schema;
exports.validate_nsfs_config_schema = validate_nsfs_config_schema;
exports.validate_log_schema = validate_log_schema;