age restriction progress

- DB-Event handler loads config from DB and sets it in extension
- TALER_Extension.parse_config now .parse_and_set_config and
  .test_config
This commit is contained in:
Özgür Kesim 2021-12-31 12:34:29 +01:00
parent 9978b25912
commit c7b4488dc4
Signed by: oec
GPG Key ID: 3D76A56D79EDD9D7
5 changed files with 82 additions and 43 deletions

View File

@ -28,11 +28,11 @@
#include <jansson.h>
/**
* @brief implements the TALER_Extension.parse_config interface.
* @brief implements the TALER_Extension.parse_and_set_config interface.
*/
enum GNUNET_GenericReturnValue
TALER_Extension_AgeRestriction_parse_config (struct TALER_Extension *this, const
json_t *config)
static enum GNUNET_GenericReturnValue
age_restriction_parse_and_set_config (struct TALER_Extension *this,
const json_t *config)
{
enum GNUNET_GenericReturnValue ret;
struct TALER_AgeMask mask = {0};
@ -55,12 +55,21 @@ TALER_Extension_AgeRestriction_parse_config (struct TALER_Extension *this, const
}
/**
* @brief implements the TALER_Extension.test_config interface.
*/
static enum GNUNET_GenericReturnValue
age_restriction_test_config (const json_t *config)
{
return age_restriction_parse_and_set_config (NULL, config);
}
/**
* @brief implements the TALER_Extension.config_to_json interface.
*/
json_t *
TALER_Extension_AgeRestriction_config_to_json (const struct
TALER_Extension *this)
static json_t *
age_restriction_config_to_json (const struct TALER_Extension *this)
{
const struct TALER_AgeMask *mask;
if (NULL == this || TALER_Extension_AgeRestriction != this->type)
@ -78,13 +87,14 @@ TALER_Extension_AgeRestriction_config_to_json (const struct
/* The extension for age restriction */
static struct TALER_Extension extension_ageRestriction = {
static struct TALER_Extension extension_age_restriction = {
.type = TALER_Extension_AgeRestriction,
.name = "age_restriction",
.critical = false,
.config = NULL, // disabled per default
.parse_config = &TALER_Extension_AgeRestriction_parse_config,
.config_to_json = &TALER_Extension_AgeRestriction_config_to_json,
.test_config = &age_restriction_test_config,
.parse_and_set_config = &age_restriction_parse_and_set_config,
.config_to_json = &age_restriction_config_to_json,
};
/* TODO: The extension for peer2peer */
@ -93,8 +103,9 @@ static struct TALER_Extension extension_peer2peer = {
.name = "peer2peer",
.critical = false,
.config = NULL, // disabled per default
.parse_config = NULL, // TODO oec
.config_to_json = NULL, // TODO oec
.test_config = NULL, // TODO
.parse_and_set_config = NULL, // TODO
.config_to_json = NULL, // TODO
};
@ -107,7 +118,7 @@ get_known_extensions ()
struct TALER_Extension **list = GNUNET_new_array (TALER_Extension_Max + 1,
struct TALER_Extension *);
list[TALER_Extension_AgeRestriction] = &extension_ageRestriction;
list[TALER_Extension_AgeRestriction] = &extension_age_restriction;
list[TALER_Extension_Peer2Peer] = &extension_peer2peer;
list[TALER_Extension_Max] = NULL;
@ -157,29 +168,53 @@ extension_update_event_cb (void *cls,
return;
}
// Get the config from the database as string
{
char *config;
char *config_str;
enum GNUNET_DB_QueryStatus qs;
struct TALER_Extension *extension;
json_error_t err;
json_t *config;
enum GNUNET_GenericReturnValue ret;
// TODO: make this a safe lookup
extension = TEH_extensions[type];
qs = TEH_plugin->get_extension_config (TEH_plugin->cls,
TEH_extensions[type]->name, // FIXME oec
&config);
extension->name,
&config_str);
if (qs < 0)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Couldn't get extension config\n");
GNUNET_break (0);
return;
}
/*
* TODO oec:
* - parse string as json
* - update global config
*/
// Parse the string as JSON
config = json_loads (config_str, JSON_DECODE_ANY, &err);
if (NULL == config)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to parse config for extension `%s' as JSON: %s (%s)\n",
extension->name,
err.text,
err.source);
GNUNET_break (0);
return;
}
// Call the parser for the extension
ret = extension->parse_and_set_config (extension, config);
if (GNUNET_OK != ret)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Couldn't parse configuration for extension %s from the database",
extension->name);
GNUNET_break (0);
}
}
}

View File

@ -736,10 +736,6 @@ destroy_key_helpers (struct HelperState *hs)
* Looks up the AGE_RESTRICTED setting for a denomination in the config and
* returns the age restriction (mask) accordingly.
*
* FIXME: The mask is currently taken from the config. However, It MUST come
* from the database where it has been persisted after a signed call to the
* /management/extension API (TODO).
*
* @param section_name Section in the configuration for the particular
* denomination.
*/
@ -748,15 +744,13 @@ load_age_mask (const char*section_name)
{
static const struct TALER_AgeMask null_mask = {0};
struct TALER_AgeMask age_mask = {0};
struct TALER_Extension *age_ext =
TEH_extensions[TALER_Extension_AgeRestriction];
/* FIXME-oec: get age_mask from database, not from config */
if (TALER_Extension_OK != TALER_get_age_mask (TEH_cfg, &age_mask))
// Get the age mask from the extension, if configured
if (NULL != age_ext->config)
{
GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
TALER_EXTENSION_SECTION_AGE_RESTRICTION,
"AGE_GROUPS",
"must be of form a:b:...:n:m, where 0<a<b<...<n<m<32\n");
return null_mask;
age_mask = *(struct TALER_AgeMask *) age_ext->config;
}
if (age_mask.mask == 0)

View File

@ -115,6 +115,15 @@ set_extensions (void *cls,
enum GNUNET_DB_QueryStatus qs;
char *config;
/* Sanity check.
* TODO: replace with general API to retrieve the extension-handler
*/
if (0 > ext->type || TALER_Extension_Max <= ext->type)
{
GNUNET_break (0);
return GNUNET_DB_STATUS_HARD_ERROR;
}
config = json_dumps (ext->config, JSON_COMPACT | JSON_SORT_KEYS);
if (NULL == config)
{
@ -128,7 +137,7 @@ set_extensions (void *cls,
qs = TEH_plugin->set_extension_config (
TEH_plugin->cls,
TEH_extensions[ext->type]->name, // FIXME oec
TEH_extensions[ext->type]->name,
config,
sig);
@ -143,7 +152,7 @@ set_extensions (void *cls,
"save extension configuration");
}
/* success, trigger event */
/* Success, trigger event */
{
enum TALER_Extension_Type *type = &sec->extensions[i].type;
struct GNUNET_DB_EventHeaderP ev = {
@ -214,7 +223,7 @@ TEH_handler_management_post_extensions (
connection,
MHD_HTTP_BAD_REQUEST,
TALER_EC_GENERIC_PARAMETER_MALFORMED,
"arrays extensions and extensions_sigs are not of equal size");
"arrays extensions and extensions_sigs are not of the same size");
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
@ -257,7 +266,7 @@ TEH_handler_management_post_extensions (
}
/* 2. Make sure name refers to a supported extension */
if (GNUNET_OK != TALER_get_extension_by_name (name,
if (GNUNET_OK != TALER_extension_get_by_name (name,
(const struct
TALER_Extension **)
TEH_extensions,
@ -314,8 +323,7 @@ TEH_handler_management_post_extensions (
}
/* 5. Make sure the config is sound */
if (GNUNET_OK != extension->parse_config (NULL /* only verify */,
sec.extensions[i].config))
if (GNUNET_OK != extension->test_config (sec.extensions[i].config))
{
GNUNET_JSON_parse_free (ext_spec);
ret = TALER_MHD_reply_with_error (

View File

@ -53,8 +53,10 @@ struct TALER_Extension
bool critical;
void *config;
enum GNUNET_GenericReturnValue (*parse_config)(struct TALER_Extension *this,
const json_t *config);
enum GNUNET_GenericReturnValue (*test_config)(const json_t *config);
enum GNUNET_GenericReturnValue (*parse_and_set_config)(struct
TALER_Extension *this,
const json_t *config);
json_t *(*config_to_json)(const struct TALER_Extension *this);
};
@ -71,7 +73,7 @@ struct TALER_Extension
* @return GNUNET_OK if extension was found, GNUNET_NO otherwise
*/
enum GNUNET_GenericReturnValue
TALER_get_extension_by_name (const char *name,
TALER_extension_get_by_name (const char *name,
const struct TALER_Extension **extensions,
const struct TALER_Extension **ext);

View File

@ -24,7 +24,7 @@
#include "stdint.h"
enum GNUNET_GenericReturnValue
TALER_get_extension_by_name (const char *name,
TALER_extension_get_by_name (const char *name,
const struct TALER_Extension **extensions,
const struct TALER_Extension **ext)
{