Improved validation of the "action" object.

Now it enforces the mutual exclusivity of "pass", "proxy", and "share" options.
This commit is contained in:
Valentin Bartenev
2020-03-03 20:37:47 +03:00
parent f99d20ad39
commit a60f856ce2
2 changed files with 41 additions and 18 deletions

View File

@@ -323,17 +323,27 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_match_members[] = {
}; };
static nxt_conf_vldt_object_t nxt_conf_vldt_action_members[] = { static nxt_conf_vldt_object_t nxt_conf_vldt_pass_action_members[] = {
{ nxt_string("pass"), { nxt_string("pass"),
NXT_CONF_VLDT_STRING, NXT_CONF_VLDT_STRING,
&nxt_conf_vldt_pass, &nxt_conf_vldt_pass,
NULL }, NULL },
NXT_CONF_VLDT_END
};
static nxt_conf_vldt_object_t nxt_conf_vldt_share_action_members[] = {
{ nxt_string("share"), { nxt_string("share"),
NXT_CONF_VLDT_STRING, NXT_CONF_VLDT_STRING,
NULL, NULL,
NULL }, NULL },
NXT_CONF_VLDT_END
};
static nxt_conf_vldt_object_t nxt_conf_vldt_proxy_action_members[] = {
{ nxt_string("proxy"), { nxt_string("proxy"),
NXT_CONF_VLDT_STRING, NXT_CONF_VLDT_STRING,
&nxt_conf_vldt_proxy, &nxt_conf_vldt_proxy,
@@ -912,30 +922,45 @@ static nxt_int_t
nxt_conf_vldt_action(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, nxt_conf_vldt_action(nxt_conf_validation_t *vldt, nxt_conf_value_t *value,
void *data) void *data)
{ {
nxt_int_t ret; nxt_uint_t i;
nxt_conf_value_t *pass_value, *share_value, *proxy_value; nxt_conf_value_t *action;
nxt_conf_vldt_object_t *members;
static nxt_str_t pass_str = nxt_string("pass"); static struct {
static nxt_str_t share_str = nxt_string("share"); nxt_str_t name;
static nxt_str_t proxy_str = nxt_string("proxy"); nxt_conf_vldt_object_t *members;
ret = nxt_conf_vldt_object(vldt, value, nxt_conf_vldt_action_members); } actions[] = {
{ nxt_string("pass"), nxt_conf_vldt_pass_action_members },
{ nxt_string("share"), nxt_conf_vldt_share_action_members },
{ nxt_string("proxy"), nxt_conf_vldt_proxy_action_members },
};
if (ret != NXT_OK) { members = NULL;
return ret;
for (i = 0; i < nxt_nitems(actions); i++) {
action = nxt_conf_get_object_member(value, &actions[i].name, NULL);
if (action == NULL) {
continue;
}
if (members != NULL) {
return nxt_conf_vldt_error(vldt, "The \"action\" object must have "
"just one of \"pass\", \"share\" or "
"\"proxy\" options set.");
}
members = actions[i].members;
} }
pass_value = nxt_conf_get_object_member(value, &pass_str, NULL); if (members == NULL) {
share_value = nxt_conf_get_object_member(value, &share_str, NULL);
proxy_value = nxt_conf_get_object_member(value, &proxy_str, NULL);
if (pass_value == NULL && share_value == NULL && proxy_value == NULL) {
return nxt_conf_vldt_error(vldt, "The \"action\" object must have " return nxt_conf_vldt_error(vldt, "The \"action\" object must have "
"either \"pass\" or \"share\" or " "either \"pass\", \"share\", or "
"\"proxy\" option set."); "\"proxy\" option set.");
} }
return NXT_OK; return nxt_conf_vldt_object(vldt, value, members);
} }

View File

@@ -288,8 +288,6 @@ class TestRouting(TestApplicationProto):
) )
def test_routes_route_pass_absent(self): def test_routes_route_pass_absent(self):
self.skip_alerts.append(r'failed to apply new conf')
self.assertIn( self.assertIn(
'error', 'error',
self.conf([{"match": {"method": "GET"}, "action": {}}], 'routes'), self.conf([{"match": {"method": "GET"}, "action": {}}], 'routes'),