From c7b4488dc4833c7efb92e6a5f8328bad6b389b50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Kesim?= Date: Fri, 31 Dec 2021 12:34:29 +0100 Subject: [PATCH] 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 --- .../taler-exchange-httpd_extensions.c | 79 +++++++++++++------ src/exchange/taler-exchange-httpd_keys.c | 16 ++-- ...ler-exchange-httpd_management_extensions.c | 20 +++-- src/include/taler_extensions.h | 8 +- src/util/extensions.c | 2 +- 5 files changed, 82 insertions(+), 43 deletions(-) diff --git a/src/exchange/taler-exchange-httpd_extensions.c b/src/exchange/taler-exchange-httpd_extensions.c index 938470811..8723bebc8 100644 --- a/src/exchange/taler-exchange-httpd_extensions.c +++ b/src/exchange/taler-exchange-httpd_extensions.c @@ -28,11 +28,11 @@ #include /** - * @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); + } } - } diff --git a/src/exchange/taler-exchange-httpd_keys.c b/src/exchange/taler-exchange-httpd_keys.c index 5d7476771..57000e139 100644 --- a/src/exchange/taler-exchange-httpd_keys.c +++ b/src/exchange/taler-exchange-httpd_keys.c @@ -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 0config; } if (age_mask.mask == 0) diff --git a/src/exchange/taler-exchange-httpd_management_extensions.c b/src/exchange/taler-exchange-httpd_management_extensions.c index 61ba771ce..96b855c3c 100644 --- a/src/exchange/taler-exchange-httpd_management_extensions.c +++ b/src/exchange/taler-exchange-httpd_management_extensions.c @@ -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 ( diff --git a/src/include/taler_extensions.h b/src/include/taler_extensions.h index a7cda9fbf..199776eb7 100644 --- a/src/include/taler_extensions.h +++ b/src/include/taler_extensions.h @@ -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); diff --git a/src/util/extensions.c b/src/util/extensions.c index 32de94d64..87dd16b4d 100644 --- a/src/util/extensions.c +++ b/src/util/extensions.c @@ -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) {