Restconf PUT different keys detected (thanks @dcornejo) and fixed

This commit is contained in:
Olof hagsand 2019-05-15 17:38:52 +02:00
parent b4f860b5ef
commit 712d8a6ff1
3 changed files with 117 additions and 4 deletions

View file

@ -175,6 +175,9 @@
### Corrected Bugs ### Corrected Bugs
* Restconf PUT different keys detected (thanks @dcornejo) and fixed
* This was accepted but shouldn't be: `PUT http://restconf/data/A=hello/B -d '{"B":"goodbye"}'`
* See RFC 8040 Sec 4.5
* Yang Enumeration including space did not generate working CLIgen code, see [Choice with space is not working in CLIgen code](https://github.com/olofhagsand/cligen/issues/24) * Yang Enumeration including space did not generate working CLIgen code, see [Choice with space is not working in CLIgen code](https://github.com/olofhagsand/cligen/issues/24)
* Fixed: [Yang submodule import prefix restrictions #60](https://github.com/clicon/clixon/issues/60) * Fixed: [Yang submodule import prefix restrictions #60](https://github.com/clicon/clixon/issues/60)
* Fixed support for multiple datanodes in a choice/case statement. Only single datanode was supported. * Fixed support for multiple datanodes in a choice/case statement. Only single datanode was supported.

View file

@ -734,6 +734,7 @@ api_data_put(clicon_handle h,
cxobj *xparent; cxobj *xparent;
cxobj *x; cxobj *x;
yang_stmt *y = NULL; yang_stmt *y = NULL;
yang_stmt *yp; /* yang parent */
yang_stmt *yspec; yang_stmt *yspec;
cxobj *xa; cxobj *xa;
char *api_path; char *api_path;
@ -869,9 +870,20 @@ api_data_put(clicon_handle h,
goto done; goto done;
goto ok; goto ok;
} }
/* If list or leaf-list, api-path keys must match data keys */ /* If list or leaf-list, api-path keys must match data keys
if (y && (yang_keyword_get(y) == Y_LIST || yang_keyword_get(y) == Y_LEAF_LIST)){ * There are two cases, either the object is the list element itself,
if (match_list_keys((yang_stmt*)y, x, xbot) < 0){ * eg xpath:obj=a data:<obj><key>b</key></obj>
* or the object is the key element:
* eg xpath:obj=a/key data:<key>b</key>
* That is why the conditional is somewhat hairy
*/
xparent = xml_parent(xbot);
if (y){
yp = yang_parent_get(y);
if (((yang_keyword_get(y) == Y_LIST || yang_keyword_get(y) == Y_LEAF_LIST) &&
match_list_keys(y, x, xbot) < 0) ||
(yp && yang_keyword_get(yp) == Y_LIST &&
match_list_keys(yp, xml_parent(x), xparent) < 0)){
if (netconf_operation_failed_xml(&xerr, "protocol", "api-path keys do not match data keys") < 0) if (netconf_operation_failed_xml(&xerr, "protocol", "api-path keys do not match data keys") < 0)
goto done; goto done;
if ((xe = xpath_first(xerr, "rpc-error")) == NULL){ if ((xe = xpath_first(xerr, "rpc-error")) == NULL){
@ -881,9 +893,10 @@ api_data_put(clicon_handle h,
if (api_return_err(h, r, xe, pretty, use_xml) < 0) if (api_return_err(h, r, xe, pretty, use_xml) < 0)
goto done; goto done;
goto ok; goto ok;
} }
} }
xparent = xml_parent(xbot);
xml_purge(xbot); xml_purge(xbot);
if (xml_addsub(xparent, x) < 0) if (xml_addsub(xparent, x) < 0)
goto done; goto done;

97
test/test_restconf_listkey.sh Executable file
View file

@ -0,0 +1,97 @@
#!/bin/bash
# Testcases for lists, key operations for netconf and restconf
# Magic line must be first in script (see README.md)
s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
APPNAME=example
cfg=$dir/conf.xml
fyang=$dir/list.yang
# <CLICON_YANG_MODULE_MAIN>example</CLICON_YANG_MODULE_MAIN>
cat <<EOF > $cfg
<clixon-config xmlns="http://clicon.org/config">
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
<CLICON_YANG_DIR>/usr/local/var</CLICON_YANG_DIR>
<CLICON_YANG_DIR>/usr/local/share/clixon</CLICON_YANG_DIR>
<CLICON_YANG_DIR>$IETFRFC</CLICON_YANG_DIR>
<CLICON_YANG_MAIN_FILE>$fyang</CLICON_YANG_MAIN_FILE>
<CLICON_RESTCONF_PRETTY>false</CLICON_RESTCONF_PRETTY>
<CLICON_SOCK>/usr/local/var/$APPNAME/$APPNAME.sock</CLICON_SOCK>
<CLICON_BACKEND_PIDFILE>$dir/restconf.pidfile</CLICON_BACKEND_PIDFILE>
<CLICON_XMLDB_DIR>/usr/local/var/$APPNAME</CLICON_XMLDB_DIR>
</clixon-config>
EOF
cat <<EOF > $fyang
module list{
yang-version 1.1;
namespace "urn:example:clixon";
prefix ex;
container c{
list a{
key b;
leaf b{
type string;
}
leaf c{
type string;
}
}
}
}
EOF
new "test params: -f $cfg"
if [ $BE -ne 0 ]; then
new "kill old backend"
sudo clixon_backend -zf $cfg
if [ $? -ne 0 ]; then
err
fi
new "start backend -s init -f $cfg"
start_backend -s init -f $cfg
fi
new "kill old restconf daemon"
sudo pkill -u www-data -f "/www-data/clixon_restconf"
new "start restconf daemon"
start_restconf -f $cfg
new "waiting"
wait_backend
wait_restconf
new "restconf PUT add entry"
expectfn 'curl -s -X PUT http://localhost/restconf/data/list:c/a=x -d {"list:a":{"b":"x","c":"0"}}' 0 ''
new "restconf PUT change regular entry"
expectfn 'curl -s -X PUT http://localhost/restconf/data/list:c/a=x -d {"list:a":{"b":"x","c":"z"}}' 0 ''
new "restconf PUT change key entry"
expectfn 'curl -s -X PUT http://localhost/restconf/data/list:c/a=x -d {"list:a":{"b":"y"}}' 0 '{"ietf-restconf:errors" : {"error": {"error-type": "protocol","error-tag": "operation-failed","error-severity": "error","error-message": "api-path keys do not match data keys"}}}'
new "restconf PUT change actual key entry"
expectfn 'curl -s -X PUT http://localhost/restconf/data/list:c/a=x/b -d {"b":"y"}' 0 '{"ietf-restconf:errors" : {"error": {"error-type": "protocol","error-tag": "operation-failed","error-severity": "error","error-message": "api-path keys do not match data keys"}}}'
exit
new "Kill restconf daemon"
stop_restconf
if [ $BE -eq 0 ]; then
exit # BE
fi
new "Kill backend"
# Check if premature kill
pid=`pgrep -u root -f clixon_backend`
if [ -z "$pid" ]; then
err "backend already dead"
fi
# kill backend
stop_backend -f $cfg
rm -rf $dir