qemu-e2k/tests/test-authz-listfile.c

197 lines
5.8 KiB
C
Raw Normal View History

authz: add QAuthZListFile object type for a file access control list Add a QAuthZListFile object type that implements the QAuthZ interface. This built-in implementation is a proxy around the QAuthZList object type, initializing it from an external file, and optionally, automatically reloading it whenever it changes. To create an instance of this object via the QMP monitor, the syntax used would be: { "execute": "object-add", "arguments": { "qom-type": "authz-list-file", "id": "authz0", "props": { "filename": "/etc/qemu/vnc.acl", "refresh": true } } } If "refresh" is "yes", inotify is used to monitor the file, automatically reloading changes. If an error occurs during reloading, all authorizations will fail until the file is next successfully loaded. The /etc/qemu/vnc.acl file would contain a JSON representation of a QAuthZList object { "rules": [ { "match": "fred", "policy": "allow", "format": "exact" }, { "match": "bob", "policy": "allow", "format": "exact" }, { "match": "danb", "policy": "deny", "format": "glob" }, { "match": "dan*", "policy": "allow", "format": "exact" }, ], "policy": "deny" } This sets up an authorization rule that allows 'fred', 'bob' and anyone whose name starts with 'dan', except for 'danb'. Everyone unmatched is denied. The object can be loaded on the comand line using -object authz-list-file,id=authz0,filename=/etc/qemu/vnc.acl,refresh=yes Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2018-05-11 13:19:59 +02:00
/*
* QEMU list authorization object tests
*
* Copyright (c) 2018 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include "qemu/osdep.h"
#include "qemu/main-loop.h"
#include "qemu/module.h"
authz: add QAuthZListFile object type for a file access control list Add a QAuthZListFile object type that implements the QAuthZ interface. This built-in implementation is a proxy around the QAuthZList object type, initializing it from an external file, and optionally, automatically reloading it whenever it changes. To create an instance of this object via the QMP monitor, the syntax used would be: { "execute": "object-add", "arguments": { "qom-type": "authz-list-file", "id": "authz0", "props": { "filename": "/etc/qemu/vnc.acl", "refresh": true } } } If "refresh" is "yes", inotify is used to monitor the file, automatically reloading changes. If an error occurs during reloading, all authorizations will fail until the file is next successfully loaded. The /etc/qemu/vnc.acl file would contain a JSON representation of a QAuthZList object { "rules": [ { "match": "fred", "policy": "allow", "format": "exact" }, { "match": "bob", "policy": "allow", "format": "exact" }, { "match": "danb", "policy": "deny", "format": "glob" }, { "match": "dan*", "policy": "allow", "format": "exact" }, ], "policy": "deny" } This sets up an authorization rule that allows 'fred', 'bob' and anyone whose name starts with 'dan', except for 'danb'. Everyone unmatched is denied. The object can be loaded on the comand line using -object authz-list-file,id=authz0,filename=/etc/qemu/vnc.acl,refresh=yes Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2018-05-11 13:19:59 +02:00
#include "authz/listfile.h"
static char *workdir;
static gchar *qemu_authz_listfile_test_save(const gchar *name,
const gchar *cfg)
{
gchar *path = g_strdup_printf("%s/default-deny.cfg", workdir);
GError *gerr = NULL;
if (!g_file_set_contents(path, cfg, -1, &gerr)) {
g_printerr("Unable to save config %s: %s\n",
path, gerr->message);
g_error_free(gerr);
g_free(path);
rmdir(workdir);
abort();
}
return path;
}
static void test_authz_default_deny(void)
{
gchar *file = qemu_authz_listfile_test_save(
"default-deny.cfg",
"{ \"policy\": \"deny\" }");
Error *local_err = NULL;
QAuthZListFile *auth = qauthz_list_file_new("auth0",
file, false,
&local_err);
unlink(file);
g_free(file);
g_assert(local_err == NULL);
g_assert(!qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
object_unparent(OBJECT(auth));
}
static void test_authz_default_allow(void)
{
gchar *file = qemu_authz_listfile_test_save(
"default-allow.cfg",
"{ \"policy\": \"allow\" }");
Error *local_err = NULL;
QAuthZListFile *auth = qauthz_list_file_new("auth0",
file, false,
&local_err);
unlink(file);
g_free(file);
g_assert(local_err == NULL);
g_assert(qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
object_unparent(OBJECT(auth));
}
static void test_authz_explicit_deny(void)
{
gchar *file = qemu_authz_listfile_test_save(
"explicit-deny.cfg",
"{ \"rules\": [ "
" { \"match\": \"fred\","
" \"policy\": \"deny\","
" \"format\": \"exact\" } ],"
" \"policy\": \"allow\" }");
Error *local_err = NULL;
QAuthZListFile *auth = qauthz_list_file_new("auth0",
file, false,
&local_err);
unlink(file);
g_free(file);
g_assert(local_err == NULL);
g_assert(!qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
object_unparent(OBJECT(auth));
}
static void test_authz_explicit_allow(void)
{
gchar *file = qemu_authz_listfile_test_save(
"explicit-allow.cfg",
"{ \"rules\": [ "
" { \"match\": \"fred\","
" \"policy\": \"allow\","
" \"format\": \"exact\" } ],"
" \"policy\": \"deny\" }");
Error *local_err = NULL;
QAuthZListFile *auth = qauthz_list_file_new("auth0",
file, false,
&local_err);
unlink(file);
g_free(file);
g_assert(local_err == NULL);
g_assert(qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
object_unparent(OBJECT(auth));
}
static void test_authz_complex(void)
{
gchar *file = qemu_authz_listfile_test_save(
"complex.cfg",
"{ \"rules\": [ "
" { \"match\": \"fred\","
" \"policy\": \"allow\","
" \"format\": \"exact\" },"
" { \"match\": \"bob\","
" \"policy\": \"allow\","
" \"format\": \"exact\" },"
" { \"match\": \"dan\","
" \"policy\": \"deny\","
" \"format\": \"exact\" },"
" { \"match\": \"dan*\","
" \"policy\": \"allow\","
" \"format\": \"glob\" } ],"
" \"policy\": \"deny\" }");
Error *local_err = NULL;
QAuthZListFile *auth = qauthz_list_file_new("auth0",
file, false,
&local_err);
unlink(file);
g_free(file);
g_assert(local_err == NULL);
g_assert(qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
g_assert(qauthz_is_allowed(QAUTHZ(auth), "bob", &error_abort));
g_assert(!qauthz_is_allowed(QAUTHZ(auth), "dan", &error_abort));
g_assert(qauthz_is_allowed(QAUTHZ(auth), "danb", &error_abort));
object_unparent(OBJECT(auth));
}
int main(int argc, char **argv)
{
int ret;
GError *gerr = NULL;
g_test_init(&argc, &argv, NULL);
module_call_init(MODULE_INIT_QOM);
workdir = g_dir_make_tmp("qemu-test-authz-listfile-XXXXXX",
&gerr);
if (!workdir) {
g_printerr("Unable to create temporary dir: %s\n",
gerr->message);
g_error_free(gerr);
abort();
}
g_test_add_func("/auth/list/default/deny", test_authz_default_deny);
g_test_add_func("/auth/list/default/allow", test_authz_default_allow);
g_test_add_func("/auth/list/explicit/deny", test_authz_explicit_deny);
g_test_add_func("/auth/list/explicit/allow", test_authz_explicit_allow);
g_test_add_func("/auth/list/complex", test_authz_complex);
ret = g_test_run();
rmdir(workdir);
g_free(workdir);
return ret;
}