#!/bin/bash # Authentication and authorization APPNAME=example # include err() and new() functions and creates $dir . ./lib.sh cfg=$dir/conf_yang.xml fyang=$dir/test.yang fyangerr=$dir/err.yang cat < $cfg $cfg /usr/local/share/$APPNAME/yang $APPNAME /usr/local/lib/$APPNAME/clispec /usr/local/lib/$APPNAME/restconf /usr/local/lib/$APPNAME/cli $APPNAME /usr/local/var/$APPNAME/$APPNAME.sock /usr/local/var/$APPNAME/$APPNAME.pidfile 1 /usr/local/var/$APPNAME /usr/local/lib/xmldb/text.so false EOF cat < $fyang module $APPNAME{ prefix ex; leaf basic_auth{ description "Basic user / password authentication as in HTTP basic auth"; type boolean; default false; } list auth { description "user / password entries. Valid if basic_auth=true"; key user; leaf user{ description "User name"; type string; } leaf password{ description "Password"; type string; } } } EOF # kill old backend (if any) new "kill old backend" sudo clixon_backend -zf $cfg -y $fyang if [ $? -ne 0 ]; then err fi new "start backend -s init -f $cfg -y $fyang" # start new backend sudo clixon_backend -s init -f $cfg -y $fyang if [ $? -ne 0 ]; then err fi new "kill old restconf daemon" sudo pkill -u www-data clixon_restconf new "start restconf daemon" sudo start-stop-daemon -S -q -o -b -x /www-data/clixon_restconf -d /www-data -c www-data -- -f $cfg # -D sleep 1 new2 "auth get" expecteq "$(curl -sS -X GET http://localhost/restconf/data)" '{"data": null} ' new "auth set authentication config" expecteof "$clixon_netconf -qf $cfg -y $fyang" "truefoobar]]>]]>" "^]]>]]>$" expecteof "$clixon_netconf -qf $cfg -y $fyang" "]]>]]>" "^]]>]]>$" new2 "auth get (access denied)" expecteq "$(curl -sS -X GET http://localhost/restconf/data)" "access-denied The requested URL /restconf/data was unauthorized." new2 "auth get (access)" expecteq "$(curl -u foo:bar -sS -X GET http://localhost/restconf/data)" '{"data": {"basic_auth": true,"auth": [{"user": "foo","password": "bar"}]}} ' new "Kill restconf daemon" sudo pkill -u www-data clixon_restconf pid=`pgrep clixon_backend` if [ -z "$pid" ]; then err "backend already dead" fi # kill backend sudo clixon_backend -zf $cfg if [ $? -ne 0 ]; then err "kill backend" fi rm -rf $dir