-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathoperationsValidation.js
More file actions
261 lines (232 loc) · 7.29 KB
/
Copy pathoperationsValidation.js
File metadata and controls
261 lines (232 loc) · 7.29 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
'use strict';
const Joi = require('joi');
const fs = require('fs-extra');
const path = require('path');
const validator = require('../validation/validationWrapper.ts');
const hdbTerms = require('../utility/hdbTerms.ts');
const hdbLogger = require('../utility/logging/harper_logger.ts');
const configUtils = require('../config/configUtils.js');
const { hdbErrors } = require('../utility/errors/hdbError.ts');
const { HDB_ERROR_MSGS } = hdbErrors;
// File name can only be alphanumeric, dash and underscores
const PROJECT_FILE_NAME_REGEX = /^[a-zA-Z0-9-_]+$/;
module.exports = {
getDropCustomFunctionValidator,
setCustomFunctionValidator,
addComponentValidator,
dropCustomFunctionProjectValidator,
packageComponentValidator,
deployComponentValidator,
setComponentFileValidator,
getComponentFileValidator,
dropComponentFileValidator,
};
/**
* Check to see if a project dir exists in the custom functions dir.
* @param checkExists - determine if validator returns error if exists or vice versa
* @param project
* @param helpers
* @returns {*}
*/
function checkProjectExists(checkExists, project, helpers) {
try {
const cfDir = configUtils.getConfigPath(hdbTerms.CONFIG_PARAMS.COMPONENTSROOT);
const projectDir = path.join(cfDir, project);
if (!fs.existsSync(projectDir)) {
if (checkExists) {
return helpers.message(HDB_ERROR_MSGS.NO_PROJECT);
}
return project;
}
if (checkExists) {
return project;
}
return helpers.message(HDB_ERROR_MSGS.PROJECT_EXISTS);
} catch (err) {
hdbLogger.error(err);
return helpers.message(HDB_ERROR_MSGS.VALIDATION_ERR);
}
}
function checkFilePath(path, helpers) {
if (path.includes('..')) return helpers.message('Invalid file path');
return path;
}
/**
* Check the custom functions dir to see if a file exists.
* @param project
* @param type
* @param file
* @param helpers
* @returns {*}
*/
function checkFileExists(project, type, file, helpers) {
try {
const cfDir = configUtils.getConfigPath(hdbTerms.CONFIG_PARAMS.COMPONENTSROOT);
const filePath = path.join(cfDir, project, type, file + '.js');
if (!fs.existsSync(filePath)) {
return helpers.message(HDB_ERROR_MSGS.NO_FILE);
}
return file;
} catch (err) {
hdbLogger.error(err);
return helpers.message(HDB_ERROR_MSGS.VALIDATION_ERR);
}
}
/**
* Used to validate getCustomFunction and dropCustomFunction
* @param req
* @returns {*}
*/
function getDropCustomFunctionValidator(req) {
const getFuncSchema = Joi.object({
project: Joi.string()
.pattern(PROJECT_FILE_NAME_REGEX)
.custom(checkProjectExists.bind(null, true))
.required()
.messages({ 'string.pattern.base': HDB_ERROR_MSGS.BAD_PROJECT_NAME }),
type: Joi.string().valid('helpers', 'routes').required(),
file: Joi.string()
.pattern(PROJECT_FILE_NAME_REGEX)
.custom(checkFileExists.bind(null, req.project, req.type))
.custom(checkFilePath)
.required()
.messages({ 'string.pattern.base': HDB_ERROR_MSGS.BAD_FILE_NAME }),
});
return validator.validateBySchema(req, getFuncSchema);
}
/**
* Validate setCustomFunction requests.
* @param req
* @returns {*}
*/
function setCustomFunctionValidator(req) {
const setFuncSchema = Joi.object({
project: Joi.string()
.pattern(PROJECT_FILE_NAME_REGEX)
.custom(checkProjectExists.bind(null, true))
.required()
.messages({ 'string.pattern.base': HDB_ERROR_MSGS.BAD_PROJECT_NAME }),
type: Joi.string().valid('helpers', 'routes').required(),
file: Joi.string().custom(checkFilePath).required(),
function_content: Joi.string().required(),
});
return validator.validateBySchema(req, setFuncSchema);
}
/**
* Validate set_component_file requests.
* @param req
* @returns {*}
*/
function setComponentFileValidator(req) {
const setCompSchema = Joi.object({
project: Joi.string()
.pattern(PROJECT_FILE_NAME_REGEX)
.required()
.messages({ 'string.pattern.base': HDB_ERROR_MSGS.BAD_PROJECT_NAME }),
file: Joi.string().custom(checkFilePath).required(),
payload: Joi.string().allow('').optional(),
encoding: Joi.string().valid('utf8', 'ASCII', 'binary', 'hex', 'base64', 'utf16le', 'latin1', 'ucs2').optional(),
});
return validator.validateBySchema(req, setCompSchema);
}
function dropComponentFileValidator(req) {
const dropCompSchema = Joi.object({
project: Joi.string()
.pattern(PROJECT_FILE_NAME_REGEX)
.required()
.messages({ 'string.pattern.base': HDB_ERROR_MSGS.BAD_PROJECT_NAME }),
file: Joi.string().custom(checkFilePath).optional(),
});
return validator.validateBySchema(req, dropCompSchema);
}
function getComponentFileValidator(req) {
const getCompSchema = Joi.object({
project: Joi.string().required(),
file: Joi.string().custom(checkFilePath).required(),
encoding: Joi.string().valid('utf8', 'ASCII', 'binary', 'hex', 'base64', 'utf16le', 'latin1', 'ucs2').optional(),
});
return validator.validateBySchema(req, getCompSchema);
}
/**
* Validate addCustomFunctionProject requests.
* @param req
* @returns {*}
*/
function addComponentValidator(req) {
const addFuncSchema = Joi.object({
project: Joi.string()
.pattern(PROJECT_FILE_NAME_REGEX)
.custom(checkProjectExists.bind(null, false))
.required()
.messages({ 'string.pattern.base': HDB_ERROR_MSGS.BAD_PROJECT_NAME }),
template: Joi.string().optional(),
install_command: Joi.string().optional(),
install_timeout: Joi.number().optional(),
install_allow_scripts: Joi.boolean().optional(),
});
return validator.validateBySchema(req, addFuncSchema);
}
/**
* Validate dropCustomFunctionProject requests.
* @param req
* @returns {*}
*/
function dropCustomFunctionProjectValidator(req) {
const dropFuncSchema = Joi.object({
project: Joi.string()
.pattern(PROJECT_FILE_NAME_REGEX)
.custom(checkProjectExists.bind(null, true))
.required()
.messages({ 'string.pattern.base': HDB_ERROR_MSGS.BAD_PROJECT_NAME }),
});
return validator.validateBySchema(req, dropFuncSchema);
}
/**
* Validate packageCustomFunctionProject requests.
* @param req
* @returns {*}
*/
function packageComponentValidator(req) {
const packageProjSchema = Joi.object({
project: Joi.string()
.pattern(PROJECT_FILE_NAME_REGEX)
.required()
.messages({ 'string.pattern.base': HDB_ERROR_MSGS.BAD_PROJECT_NAME }),
skip_node_modules: Joi.boolean(),
skip_symlinks: Joi.boolean(),
});
return validator.validateBySchema(req, packageProjSchema);
}
/**
* Validate deployComponent requests.
* @param req
* @returns {*}
*/
function deployComponentValidator(req) {
const deployProjSchema = Joi.object({
project: Joi.string()
.pattern(PROJECT_FILE_NAME_REGEX)
.required()
.messages({ 'string.pattern.base': HDB_ERROR_MSGS.BAD_PROJECT_NAME }),
package: Joi.string().optional(),
restart: Joi.alternatives().try(Joi.boolean(), Joi.string().valid('rolling')).optional(),
install_command: Joi.string().optional(),
install_timeout: Joi.number().optional(),
install_allow_scripts: Joi.boolean().optional(),
force: Joi.boolean().optional(),
// Transient private-registry auth: never persisted, never replicated. Used only for this
// node's npm pack/install during the deploy.
registryAuth: Joi.array()
.items(
Joi.object({
registry: Joi.string().required(),
token: Joi.string().required(),
scope: Joi.string()
.pattern(/^@[a-z0-9-_.]+$/)
.optional(),
})
)
.optional(),
});
return validator.validateBySchema(req, deployProjSchema);
}