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> #include <jansson.h>
/** /**
* @brief implements the TALER_Extension.parse_config interface. * @brief implements the TALER_Extension.parse_and_set_config interface.
*/ */
enum GNUNET_GenericReturnValue static enum GNUNET_GenericReturnValue
TALER_Extension_AgeRestriction_parse_config (struct TALER_Extension *this, const age_restriction_parse_and_set_config (struct TALER_Extension *this,
json_t *config) const json_t *config)
{ {
enum GNUNET_GenericReturnValue ret; enum GNUNET_GenericReturnValue ret;
struct TALER_AgeMask mask = {0}; 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. * @brief implements the TALER_Extension.config_to_json interface.
*/ */
json_t * static json_t *
TALER_Extension_AgeRestriction_config_to_json (const struct age_restriction_config_to_json (const struct TALER_Extension *this)
TALER_Extension *this)
{ {
const struct TALER_AgeMask *mask; const struct TALER_AgeMask *mask;
if (NULL == this || TALER_Extension_AgeRestriction != this->type) 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 */ /* The extension for age restriction */
static struct TALER_Extension extension_ageRestriction = { static struct TALER_Extension extension_age_restriction = {
.type = TALER_Extension_AgeRestriction, .type = TALER_Extension_AgeRestriction,
.name = "age_restriction", .name = "age_restriction",
.critical = false, .critical = false,
.config = NULL, // disabled per default .config = NULL, // disabled per default
.parse_config = &TALER_Extension_AgeRestriction_parse_config, .test_config = &age_restriction_test_config,
.config_to_json = &TALER_Extension_AgeRestriction_config_to_json, .parse_and_set_config = &age_restriction_parse_and_set_config,
.config_to_json = &age_restriction_config_to_json,
}; };
/* TODO: The extension for peer2peer */ /* TODO: The extension for peer2peer */
@ -93,8 +103,9 @@ static struct TALER_Extension extension_peer2peer = {
.name = "peer2peer", .name = "peer2peer",
.critical = false, .critical = false,
.config = NULL, // disabled per default .config = NULL, // disabled per default
.parse_config = NULL, // TODO oec .test_config = NULL, // TODO
.config_to_json = NULL, // TODO oec .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 = GNUNET_new_array (TALER_Extension_Max + 1,
struct TALER_Extension *); 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_Peer2Peer] = &extension_peer2peer;
list[TALER_Extension_Max] = NULL; list[TALER_Extension_Max] = NULL;
@ -157,29 +168,53 @@ extension_update_event_cb (void *cls,
return; return;
} }
// Get the config from the database as string
{ {
char *config; char *config_str;
enum GNUNET_DB_QueryStatus qs; 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, qs = TEH_plugin->get_extension_config (TEH_plugin->cls,
TEH_extensions[type]->name, // FIXME oec extension->name,
&config); &config_str);
if (qs < 0) if (qs < 0)
{ {
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Couldn't get extension config\n"); "Couldn't get extension config\n");
GNUNET_break (0);
return; return;
} }
/* // Parse the string as JSON
* TODO oec: config = json_loads (config_str, JSON_DECODE_ANY, &err);
* - parse string as json if (NULL == config)
* - update global 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 * Looks up the AGE_RESTRICTED setting for a denomination in the config and
* returns the age restriction (mask) accordingly. * 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 * @param section_name Section in the configuration for the particular
* denomination. * denomination.
*/ */
@ -748,15 +744,13 @@ load_age_mask (const char*section_name)
{ {
static const struct TALER_AgeMask null_mask = {0}; static const struct TALER_AgeMask null_mask = {0};
struct TALER_AgeMask age_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 */ // Get the age mask from the extension, if configured
if (TALER_Extension_OK != TALER_get_age_mask (TEH_cfg, &age_mask)) if (NULL != age_ext->config)
{ {
GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, age_mask = *(struct TALER_AgeMask *) age_ext->config;
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;
} }
if (age_mask.mask == 0) if (age_mask.mask == 0)

View File

@ -115,6 +115,15 @@ set_extensions (void *cls,
enum GNUNET_DB_QueryStatus qs; enum GNUNET_DB_QueryStatus qs;
char *config; 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); config = json_dumps (ext->config, JSON_COMPACT | JSON_SORT_KEYS);
if (NULL == config) if (NULL == config)
{ {
@ -128,7 +137,7 @@ set_extensions (void *cls,
qs = TEH_plugin->set_extension_config ( qs = TEH_plugin->set_extension_config (
TEH_plugin->cls, TEH_plugin->cls,
TEH_extensions[ext->type]->name, // FIXME oec TEH_extensions[ext->type]->name,
config, config,
sig); sig);
@ -143,7 +152,7 @@ set_extensions (void *cls,
"save extension configuration"); "save extension configuration");
} }
/* success, trigger event */ /* Success, trigger event */
{ {
enum TALER_Extension_Type *type = &sec->extensions[i].type; enum TALER_Extension_Type *type = &sec->extensions[i].type;
struct GNUNET_DB_EventHeaderP ev = { struct GNUNET_DB_EventHeaderP ev = {
@ -214,7 +223,7 @@ TEH_handler_management_post_extensions (
connection, connection,
MHD_HTTP_BAD_REQUEST, MHD_HTTP_BAD_REQUEST,
TALER_EC_GENERIC_PARAMETER_MALFORMED, 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, GNUNET_log (GNUNET_ERROR_TYPE_INFO,
@ -257,7 +266,7 @@ TEH_handler_management_post_extensions (
} }
/* 2. Make sure name refers to a supported extension */ /* 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 (const struct
TALER_Extension **) TALER_Extension **)
TEH_extensions, TEH_extensions,
@ -314,8 +323,7 @@ TEH_handler_management_post_extensions (
} }
/* 5. Make sure the config is sound */ /* 5. Make sure the config is sound */
if (GNUNET_OK != extension->parse_config (NULL /* only verify */, if (GNUNET_OK != extension->test_config (sec.extensions[i].config))
sec.extensions[i].config))
{ {
GNUNET_JSON_parse_free (ext_spec); GNUNET_JSON_parse_free (ext_spec);
ret = TALER_MHD_reply_with_error ( ret = TALER_MHD_reply_with_error (

View File

@ -53,8 +53,10 @@ struct TALER_Extension
bool critical; bool critical;
void *config; void *config;
enum GNUNET_GenericReturnValue (*parse_config)(struct TALER_Extension *this, enum GNUNET_GenericReturnValue (*test_config)(const json_t *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); 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 * @return GNUNET_OK if extension was found, GNUNET_NO otherwise
*/ */
enum GNUNET_GenericReturnValue 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 **extensions,
const struct TALER_Extension **ext); const struct TALER_Extension **ext);

View File

@ -24,7 +24,7 @@
#include "stdint.h" #include "stdint.h"
enum GNUNET_GenericReturnValue 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 **extensions,
const struct TALER_Extension **ext) const struct TALER_Extension **ext)
{ {