Basic validation errors.

This commit is contained in:
Valentin Bartenev
2017-10-10 19:46:58 +03:00
parent e4bea2c75c
commit 1c6d4d8cff
3 changed files with 259 additions and 85 deletions

View File

@@ -9,13 +9,15 @@
#define _NXT_CONF_INCLUDED_
#define NXT_CONF_NULL 0x01
#define NXT_CONF_BOOLEAN 0x02
#define NXT_CONF_INTEGER 0x04
#define NXT_CONF_NUMBER 0x08
#define NXT_CONF_STRING 0x10
#define NXT_CONF_ARRAY 0x20
#define NXT_CONF_OBJECT 0x40
typedef enum {
NXT_CONF_NULL = 0,
NXT_CONF_BOOLEAN,
NXT_CONF_INTEGER,
NXT_CONF_NUMBER,
NXT_CONF_STRING,
NXT_CONF_ARRAY,
NXT_CONF_OBJECT,
} nxt_conf_type_t;
typedef struct nxt_conf_value_s nxt_conf_value_t;
@@ -57,6 +59,13 @@ typedef struct {
} nxt_conf_json_pretty_t;
typedef struct {
nxt_conf_value_t *conf;
nxt_mp_t *pool;
nxt_str_t error;
} nxt_conf_validation_t;
nxt_uint_t nxt_conf_type(nxt_conf_value_t *value);
nxt_conf_value_t *nxt_conf_get_path(nxt_conf_value_t *value, nxt_str_t *path);
@@ -88,7 +97,7 @@ u_char *nxt_conf_json_print(u_char *p, nxt_conf_value_t *value,
void nxt_conf_json_position(u_char *start, u_char *pos, nxt_uint_t *line,
nxt_uint_t *column);
nxt_int_t nxt_conf_validate(nxt_conf_value_t *value);
nxt_int_t nxt_conf_validate(nxt_conf_validation_t *vldt);
void nxt_conf_get_string(nxt_conf_value_t *value, nxt_str_t *str);

View File

@@ -10,35 +10,41 @@
typedef struct {
nxt_str_t name;
nxt_uint_t type;
nxt_int_t (*validator)(nxt_conf_value_t *conf, nxt_conf_value_t *value,
void *data);
void *data;
nxt_str_t name;
nxt_conf_type_t type;
nxt_int_t (*validator)(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data);
void *data;
} nxt_conf_vldt_object_t;
typedef nxt_int_t (*nxt_conf_vldt_member_t)(nxt_conf_value_t *conf,
typedef nxt_int_t (*nxt_conf_vldt_member_t)(nxt_conf_validation_t *vldt,
nxt_str_t *name,
nxt_conf_value_t *value);
typedef nxt_int_t (*nxt_conf_vldt_system_t)(nxt_conf_value_t *conf, char *name);
typedef nxt_int_t (*nxt_conf_vldt_system_t)(nxt_conf_validation_t *vldt,
char *name);
static nxt_int_t nxt_conf_vldt_listener(nxt_conf_value_t *conf, nxt_str_t *name,
nxt_conf_value_t *value);
static nxt_int_t nxt_conf_vldt_app_name(nxt_conf_value_t *conf,
static nxt_int_t nxt_conf_vldt_type(nxt_conf_validation_t *vldt,
nxt_str_t *name, nxt_conf_value_t *value, nxt_conf_type_t type);
static nxt_int_t nxt_conf_vldt_error(nxt_conf_validation_t *vldt,
const char *fmt, ...);
static nxt_int_t nxt_conf_vldt_listener(nxt_conf_validation_t *vldt,
nxt_str_t *name, nxt_conf_value_t *value);
static nxt_int_t nxt_conf_vldt_app_name(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data);
static nxt_int_t nxt_conf_vldt_app(nxt_conf_value_t *conf, nxt_str_t *name,
nxt_conf_value_t *value);
static nxt_int_t nxt_conf_vldt_object(nxt_conf_value_t *conf,
static nxt_int_t nxt_conf_vldt_app(nxt_conf_validation_t *vldt,
nxt_str_t *name, nxt_conf_value_t *value);
static nxt_int_t nxt_conf_vldt_object(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data);
static nxt_int_t nxt_conf_vldt_object_iterator(nxt_conf_value_t *conf,
static nxt_int_t nxt_conf_vldt_object_iterator(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data);
static nxt_int_t nxt_conf_vldt_system(nxt_conf_value_t *conf,
static nxt_int_t nxt_conf_vldt_system(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data);
static nxt_int_t nxt_conf_vldt_user(nxt_conf_value_t *conf, char *name);
static nxt_int_t nxt_conf_vldt_group(nxt_conf_value_t *conf, char *name);
static nxt_int_t nxt_conf_vldt_user(nxt_conf_validation_t *vldt, char *name);
static nxt_int_t nxt_conf_vldt_group(nxt_conf_validation_t *vldt, char *name);
static nxt_conf_vldt_object_t nxt_conf_vldt_root_members[] = {
@@ -217,26 +223,100 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_go_members[] = {
nxt_int_t
nxt_conf_validate(nxt_conf_value_t *value)
nxt_conf_validate(nxt_conf_validation_t *vldt)
{
if (nxt_conf_type(value) != NXT_CONF_OBJECT) {
nxt_int_t ret;
ret = nxt_conf_vldt_type(vldt, NULL, vldt->conf, NXT_CONF_OBJECT);
if (ret != NXT_OK) {
return ret;
}
return nxt_conf_vldt_object(vldt, vldt->conf, nxt_conf_vldt_root_members);
}
static nxt_int_t
nxt_conf_vldt_type(nxt_conf_validation_t *vldt, nxt_str_t *name,
nxt_conf_value_t *value, nxt_conf_type_t type)
{
nxt_uint_t value_type;
static const char *type_name[] = {
"a null",
"a boolean",
"an integer",
"a number",
"a string",
"an array",
"an object"
};
value_type = nxt_conf_type(value);
if (value_type == type) {
return NXT_OK;
}
if (name == NULL) {
return nxt_conf_vldt_error(vldt,
"The configuration must be %s, not %s.",
type_name[type], type_name[value_type]);
}
return nxt_conf_vldt_error(vldt,
"The \"%V\" value must be %s, not %s.",
name, type_name[type], type_name[value_type]);
}
static nxt_int_t
nxt_conf_vldt_error(nxt_conf_validation_t *vldt, const char *fmt, ...)
{
u_char *p, *end;
size_t size;
va_list args;
u_char error[NXT_MAX_ERROR_STR];
va_start(args, fmt);
end = nxt_vsprintf(error, error + NXT_MAX_ERROR_STR, fmt, args);
va_end(args);
size = end - error;
p = nxt_mp_nget(vldt->pool, size);
if (p == NULL) {
return NXT_ERROR;
}
return nxt_conf_vldt_object(value, value, nxt_conf_vldt_root_members);
nxt_memcpy(p, error, size);
vldt->error.length = size;
vldt->error.start = p;
return NXT_DECLINED;
}
static nxt_int_t
nxt_conf_vldt_listener(nxt_conf_value_t *conf, nxt_str_t *name,
nxt_conf_vldt_listener(nxt_conf_validation_t *vldt, nxt_str_t *name,
nxt_conf_value_t *value)
{
return nxt_conf_vldt_object(conf, value, nxt_conf_vldt_listener_members);
nxt_int_t ret;
ret = nxt_conf_vldt_type(vldt, name, value, NXT_CONF_OBJECT);
if (ret != NXT_OK) {
return ret;
}
return nxt_conf_vldt_object(vldt, value, nxt_conf_vldt_listener_members);
}
static nxt_int_t
nxt_conf_vldt_app_name(nxt_conf_value_t *conf, nxt_conf_value_t *value,
nxt_conf_vldt_app_name(nxt_conf_validation_t *vldt, nxt_conf_value_t *value,
void *data)
{
nxt_str_t name;
@@ -244,28 +324,35 @@ nxt_conf_vldt_app_name(nxt_conf_value_t *conf, nxt_conf_value_t *value,
static nxt_str_t apps_str = nxt_string("applications");
apps = nxt_conf_get_object_member(conf, &apps_str, NULL);
nxt_conf_get_string(value, &name);
apps = nxt_conf_get_object_member(vldt->conf, &apps_str, NULL);
if (nxt_slow_path(apps == NULL)) {
return NXT_ERROR;
goto error;
}
nxt_conf_get_string(value, &name);
app = nxt_conf_get_object_member(apps, &name, NULL);
if (nxt_slow_path(app == NULL)) {
return NXT_ERROR;
goto error;
}
return NXT_OK;
error:
return nxt_conf_vldt_error(vldt, "Listening socket is assigned for "
"a non existing application \"%V\".",
&name);
}
static nxt_int_t
nxt_conf_vldt_app(nxt_conf_value_t *conf, nxt_str_t *name,
nxt_conf_vldt_app(nxt_conf_validation_t *vldt, nxt_str_t *name,
nxt_conf_value_t *value)
{
nxt_int_t ret;
nxt_str_t type;
nxt_thread_t *thread;
nxt_conf_value_t *type_value;
@@ -279,14 +366,23 @@ nxt_conf_vldt_app(nxt_conf_value_t *conf, nxt_str_t *name,
nxt_conf_vldt_go_members,
};
type_value = nxt_conf_get_object_member(value, &type_str, NULL);
ret = nxt_conf_vldt_type(vldt, name, value, NXT_CONF_OBJECT);
if (nxt_slow_path(type_value == NULL)) {
return NXT_ERROR;
if (ret != NXT_OK) {
return ret;
}
if (nxt_conf_type(type_value) != NXT_CONF_STRING) {
return NXT_ERROR;
type_value = nxt_conf_get_object_member(value, &type_str, NULL);
if (type_value == NULL) {
return nxt_conf_vldt_error(vldt,
"Application must have the \"type\" property set.");
}
ret = nxt_conf_vldt_type(vldt, &type_str, type_value, NXT_CONF_STRING);
if (ret != NXT_OK) {
return ret;
}
nxt_conf_get_string(type_value, &type);
@@ -295,21 +391,25 @@ nxt_conf_vldt_app(nxt_conf_value_t *conf, nxt_str_t *name,
lang = nxt_app_lang_module(thread->runtime, &type);
if (lang == NULL) {
return NXT_ERROR;
return nxt_conf_vldt_error(vldt,
"The module to run \"%V\" is not found "
"among the available application modules.",
&type);
}
return nxt_conf_vldt_object(conf, value, members[lang->type]);
return nxt_conf_vldt_object(vldt, value, members[lang->type]);
}
static nxt_int_t
nxt_conf_vldt_object(nxt_conf_value_t *conf, nxt_conf_value_t *value,
nxt_conf_vldt_object(nxt_conf_validation_t *vldt, nxt_conf_value_t *value,
void *data)
{
uint32_t index;
nxt_int_t ret;
nxt_str_t name;
nxt_conf_value_t *member;
nxt_conf_vldt_object_t *vldt;
nxt_conf_vldt_object_t *vals;
index = 0;
@@ -320,26 +420,31 @@ nxt_conf_vldt_object(nxt_conf_value_t *conf, nxt_conf_value_t *value,
return NXT_OK;
}
vldt = data;
vals = data;
for ( ;; ) {
if (vldt->name.length == 0) {
return NXT_ERROR;
if (vals->name.length == 0) {
return nxt_conf_vldt_error(vldt, "Unknown parameter \"%V\".",
&name);
}
if (!nxt_strstr_eq(&vldt->name, &name)) {
vldt++;
if (!nxt_strstr_eq(&vals->name, &name)) {
vals++;
continue;
}
if (nxt_conf_type(member) != vldt->type) {
return NXT_ERROR;
ret = nxt_conf_vldt_type(vldt, &name, member, vals->type);
if (ret != NXT_OK) {
return ret;
}
if (vldt->validator != NULL
&& vldt->validator(conf, member, vldt->data) != NXT_OK)
{
return NXT_ERROR;
if (vals->validator != NULL) {
ret = vals->validator(vldt, member, vals->data);
if (ret != NXT_OK) {
return ret;
}
}
break;
@@ -349,10 +454,11 @@ nxt_conf_vldt_object(nxt_conf_value_t *conf, nxt_conf_value_t *value,
static nxt_int_t
nxt_conf_vldt_object_iterator(nxt_conf_value_t *conf, nxt_conf_value_t *value,
void *data)
nxt_conf_vldt_object_iterator(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data)
{
uint32_t index;
nxt_int_t ret;
nxt_str_t name;
nxt_conf_value_t *member;
nxt_conf_vldt_member_t validator;
@@ -367,24 +473,26 @@ nxt_conf_vldt_object_iterator(nxt_conf_value_t *conf, nxt_conf_value_t *value,
return NXT_OK;
}
if (validator(conf, &name, member) != NXT_OK) {
return NXT_ERROR;
ret = validator(vldt, &name, member);
if (ret != NXT_OK) {
return ret;
}
}
}
static nxt_int_t
nxt_conf_vldt_system(nxt_conf_value_t *conf, nxt_conf_value_t *value,
nxt_conf_vldt_system(nxt_conf_validation_t *vldt, nxt_conf_value_t *value,
void *data)
{
size_t length;
nxt_str_t name;
nxt_conf_vldt_system_t vldt;
nxt_conf_vldt_system_t validator;
char string[32];
/* The cast is required by Sun C. */
vldt = (nxt_conf_vldt_system_t) data;
validator = (nxt_conf_vldt_system_t) data;
nxt_conf_get_string(value, &name);
@@ -393,12 +501,12 @@ nxt_conf_vldt_system(nxt_conf_value_t *conf, nxt_conf_value_t *value,
nxt_cpystrn((u_char *) string, name.start, length);
return vldt(conf, string);
return validator(vldt, string);
}
static nxt_int_t
nxt_conf_vldt_user(nxt_conf_value_t *conf, char *user)
nxt_conf_vldt_user(nxt_conf_validation_t *vldt, char *user)
{
struct passwd *pwd;
@@ -410,12 +518,16 @@ nxt_conf_vldt_user(nxt_conf_value_t *conf, char *user)
return NXT_OK;
}
if (nxt_errno == 0) {
return nxt_conf_vldt_error(vldt, "User \"%s\" is not found.", user);
}
return NXT_ERROR;
}
static nxt_int_t
nxt_conf_vldt_group(nxt_conf_value_t *conf, char *group)
nxt_conf_vldt_group(nxt_conf_validation_t *vldt, char *group)
{
struct group *grp;
@@ -427,5 +539,9 @@ nxt_conf_vldt_group(nxt_conf_value_t *conf, char *group)
return NXT_OK;
}
if (nxt_errno == 0) {
return nxt_conf_vldt_error(vldt, "Group \"%s\" is not found.", group);
}
return NXT_ERROR;
}

View File

@@ -31,7 +31,7 @@ typedef struct {
nxt_conf_value_t *conf;
u_char *title;
u_char *detail;
nxt_str_t detail;
ssize_t offset;
nxt_uint_t line;
nxt_uint_t column;
@@ -115,10 +115,12 @@ nxt_int_t
nxt_controller_start(nxt_task_t *task, void *data)
{
nxt_mp_t *mp;
nxt_int_t ret;
nxt_str_t *json;
nxt_runtime_t *rt;
nxt_conf_value_t *conf;
nxt_event_engine_t *engine;
nxt_conf_validation_t vldt;
nxt_http_fields_hash_t *hash;
rt = task->thread->runtime;
@@ -162,15 +164,35 @@ nxt_controller_start(nxt_task_t *task, void *data)
return NXT_OK;
}
if (nxt_slow_path(nxt_conf_validate(conf) != NXT_OK)) {
nxt_log(task, NXT_LOG_ALERT,
"failed to restore previous configuration: "
"invalid configuration");
nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
nxt_mp_destroy(mp);
return NXT_OK;
vldt.pool = nxt_mp_create(1024, 128, 256, 32);
if (nxt_slow_path(vldt.pool == NULL)) {
return NXT_ERROR;
}
vldt.conf = conf;
ret = nxt_conf_validate(&vldt);
if (nxt_slow_path(ret != NXT_OK)) {
if (ret == NXT_DECLINED) {
nxt_log(task, NXT_LOG_ALERT,
"the previous configuration is invalid: %V", &vldt.error);
nxt_mp_destroy(vldt.pool);
nxt_mp_destroy(mp);
return NXT_OK;
}
/* ret == NXT_ERROR */
return NXT_ERROR;
}
nxt_mp_destroy(vldt.pool);
nxt_controller_conf.root = conf;
nxt_controller_conf.pool = mp;
@@ -740,6 +762,7 @@ nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
nxt_buf_mem_t *mbuf;
nxt_conf_op_t *ops;
nxt_conf_value_t *value;
nxt_conf_validation_t vldt;
nxt_conf_json_error_t error;
nxt_controller_response_t resp;
@@ -797,7 +820,8 @@ nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
resp.status = 400;
resp.title = (u_char *) "Invalid JSON.";
resp.detail = error.detail;
resp.detail.length = nxt_strlen(error.detail);
resp.detail.start = error.detail;
resp.offset = error.pos - mbuf->pos;
nxt_conf_json_position(mbuf->pos, error.pos,
@@ -828,9 +852,23 @@ nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
}
}
if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
vldt.conf = value;
vldt.pool = c->mem_pool;
rc = nxt_conf_validate(&vldt);
if (nxt_slow_path(rc != NXT_OK)) {
nxt_mp_destroy(mp);
goto invalid_conf;
if (rc == NXT_DECLINED) {
resp.detail = vldt.error;
goto invalid_conf;
}
/* rc == NXT_ERROR */
goto alloc_fail;
}
rc = nxt_controller_conf_send(task, value,
@@ -898,9 +936,23 @@ nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
goto alloc_fail;
}
if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
vldt.conf = value;
vldt.pool = c->mem_pool;
rc = nxt_conf_validate(&vldt);
if (nxt_slow_path(rc != NXT_OK)) {
nxt_mp_destroy(mp);
goto invalid_conf;
if (rc == NXT_DECLINED) {
resp.detail = vldt.error;
goto invalid_conf;
}
/* rc == NXT_ERROR */
goto alloc_fail;
}
rc = nxt_controller_conf_send(task, value,
@@ -1100,7 +1152,7 @@ nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req,
if (value == NULL) {
n = 1
+ (resp->detail != NULL)
+ (resp->detail.length != 0)
+ (resp->status >= 400 && resp->offset != -1);
value = nxt_conf_create_object(c->mem_pool, n);
@@ -1122,13 +1174,10 @@ nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req,
n = 0;
if (resp->detail != NULL) {
str.length = nxt_strlen(resp->detail);
str.start = resp->detail;
if (resp->detail.length != 0) {
n++;
nxt_conf_set_member_string(value, &detail_str, &str, n);
nxt_conf_set_member_string(value, &detail_str, &resp->detail, n);
}
if (resp->status >= 400 && resp->offset != -1) {