root/daemons/controld/controld_metadata.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ra_param_free
  2. metadata_free
  3. metadata_cache_new
  4. metadata_cache_free
  5. metadata_cache_reset
  6. valid_version_format
  7. metadata_cache_fini
  8. ra_version_from_xml
  9. ra_param_from_xml
  10. log_ra_ocf_version
  11. metadata_cache_update
  12. controld_get_rsc_metadata

   1 /*
   2  * Copyright 2017-2021 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU General Public License version 2
   7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <stdio.h>
  13 #include <glib.h>
  14 #include <regex.h>
  15 
  16 #include <crm/crm.h>
  17 #include <crm/lrmd.h>
  18 
  19 #include <pacemaker-controld.h>
  20 
  21 #if ENABLE_VERSIONED_ATTRS
  22 static regex_t *version_format_regex = NULL;
  23 #endif
  24 
  25 static void
  26 ra_param_free(void *param)
     /* [previous][next][first][last][top][bottom][index][help] */
  27 {
  28     if (param) {
  29         struct ra_param_s *p = (struct ra_param_s *) param;
  30 
  31         if (p->rap_name) {
  32             free(p->rap_name);
  33         }
  34         free(param);
  35     }
  36 }
  37 
  38 static void
  39 metadata_free(void *metadata)
     /* [previous][next][first][last][top][bottom][index][help] */
  40 {
  41     if (metadata) {
  42         struct ra_metadata_s *md = (struct ra_metadata_s *) metadata;
  43 
  44         if (md->ra_version) {
  45             free(md->ra_version);
  46         }
  47         g_list_free_full(md->ra_params, ra_param_free);
  48         free(metadata);
  49     }
  50 }
  51 
  52 GHashTable *
  53 metadata_cache_new()
     /* [previous][next][first][last][top][bottom][index][help] */
  54 {
  55     return pcmk__strkey_table(free, metadata_free);
  56 }
  57 
  58 void
  59 metadata_cache_free(GHashTable *mdc)
     /* [previous][next][first][last][top][bottom][index][help] */
  60 {
  61     if (mdc) {
  62         crm_trace("Destroying metadata cache with %d members", g_hash_table_size(mdc));
  63         g_hash_table_destroy(mdc);
  64     }
  65 }
  66 
  67 void
  68 metadata_cache_reset(GHashTable *mdc)
     /* [previous][next][first][last][top][bottom][index][help] */
  69 {
  70     if (mdc) {
  71         crm_trace("Resetting metadata cache with %d members",
  72                   g_hash_table_size(mdc));
  73         g_hash_table_remove_all(mdc);
  74     }
  75 }
  76 
  77 #if ENABLE_VERSIONED_ATTRS
  78 static gboolean
  79 valid_version_format(const char *version)
     /* [previous][next][first][last][top][bottom][index][help] */
  80 {
  81     if (version == NULL) {
  82         return FALSE;
  83     }
  84 
  85     if (version_format_regex == NULL) {
  86         /* The OCF standard allows free-form versioning, but for our purposes of
  87          * versioned resource and operation attributes, we constrain it to
  88          * dot-separated numbers. Agents are still free to use other schemes,
  89          * but we can't determine attributes based on them.
  90          */
  91         const char *regex_string = "^[[:digit:]]+([.][[:digit:]]+)*$";
  92 
  93         version_format_regex = calloc(1, sizeof(regex_t));
  94         regcomp(version_format_regex, regex_string, REG_EXTENDED | REG_NOSUB);
  95 
  96         /* If our regex doesn't compile, it's a bug on our side, so CRM_CHECK()
  97          * will give us a core dump to catch it. Pretend the version is OK
  98          * because we don't want our mistake to break versioned attributes
  99          * (which should only ever happen in a development branch anyway).
 100          */
 101         CRM_CHECK(version_format_regex != NULL, return TRUE);
 102     }
 103 
 104     return regexec(version_format_regex, version, 0, NULL, 0) == 0;
 105 }
 106 #endif
 107 
 108 void
 109 metadata_cache_fini()
     /* [previous][next][first][last][top][bottom][index][help] */
 110 {
 111 #if ENABLE_VERSIONED_ATTRS
 112     if (version_format_regex) {
 113         regfree(version_format_regex);
 114         free(version_format_regex);
 115         version_format_regex = NULL;
 116     }
 117 #endif
 118 }
 119 
 120 #if ENABLE_VERSIONED_ATTRS
 121 static char *
 122 ra_version_from_xml(xmlNode *metadata_xml, const lrmd_rsc_info_t *rsc)
     /* [previous][next][first][last][top][bottom][index][help] */
 123 {
 124     const char *version = crm_element_value(metadata_xml, XML_ATTR_VERSION);
 125 
 126     if (version == NULL) {
 127         crm_debug("Metadata for %s:%s:%s does not specify a version",
 128                   rsc->standard, rsc->provider, rsc->type);
 129         version = PCMK_DEFAULT_AGENT_VERSION;
 130 
 131     } else if (!valid_version_format(version)) {
 132         crm_notice("%s:%s:%s metadata version has unrecognized format",
 133                   rsc->standard, rsc->provider, rsc->type);
 134         version = PCMK_DEFAULT_AGENT_VERSION;
 135 
 136     } else {
 137         crm_debug("Metadata for %s:%s:%s has version %s",
 138                   rsc->standard, rsc->provider, rsc->type, version);
 139     }
 140     return strdup(version);
 141 }
 142 #endif
 143 
 144 static struct ra_param_s *
 145 ra_param_from_xml(xmlNode *param_xml)
     /* [previous][next][first][last][top][bottom][index][help] */
 146 {
 147     const char *param_name = crm_element_value(param_xml, "name");
 148     const char *value;
 149     struct ra_param_s *p;
 150 
 151     p = calloc(1, sizeof(struct ra_param_s));
 152     if (p == NULL) {
 153         crm_crit("Could not allocate memory for resource metadata");
 154         return NULL;
 155     }
 156 
 157     p->rap_name = strdup(param_name);
 158     if (p->rap_name == NULL) {
 159         crm_crit("Could not allocate memory for resource metadata");
 160         free(p);
 161         return NULL;
 162     }
 163 
 164     value = crm_element_value(param_xml, "reloadable");
 165     if (crm_is_true(value)) {
 166         controld_set_ra_param_flags(p, ra_param_reloadable);
 167     }
 168 
 169     value = crm_element_value(param_xml, "unique");
 170     if (crm_is_true(value)) {
 171         controld_set_ra_param_flags(p, ra_param_unique);
 172     }
 173 
 174     value = crm_element_value(param_xml, "private");
 175     if (crm_is_true(value)) {
 176         controld_set_ra_param_flags(p, ra_param_private);
 177     }
 178     return p;
 179 }
 180 
 181 static void
 182 log_ra_ocf_version(const char *ra_key, const char *ra_ocf_version)
     /* [previous][next][first][last][top][bottom][index][help] */
 183 {
 184     if (pcmk__str_empty(ra_ocf_version)) {
 185         crm_warn("%s does not advertise OCF version supported", ra_key);
 186 
 187     } else if (compare_version(ra_ocf_version, "2") >= 0) {
 188         crm_warn("%s supports OCF version %s (this Pacemaker version supports "
 189                  PCMK_OCF_VERSION " and might not work properly with agent)",
 190                  ra_key, ra_ocf_version);
 191 
 192     } else if (compare_version(ra_ocf_version, PCMK_OCF_VERSION) > 0) {
 193         crm_info("%s supports OCF version %s (this Pacemaker version supports "
 194                  PCMK_OCF_VERSION " and might not use all agent features)",
 195                  ra_key, ra_ocf_version);
 196 
 197     } else {
 198         crm_debug("%s supports OCF version %s", ra_key, ra_ocf_version);
 199     }
 200 }
 201 
 202 struct ra_metadata_s *
 203 metadata_cache_update(GHashTable *mdc, lrmd_rsc_info_t *rsc,
     /* [previous][next][first][last][top][bottom][index][help] */
 204                       const char *metadata_str)
 205 {
 206     char *key = NULL;
 207     xmlNode *metadata = NULL;
 208     xmlNode *match = NULL;
 209     struct ra_metadata_s *md = NULL;
 210     bool any_private_params = false;
 211     bool ocf1_1 = false;
 212 
 213     CRM_CHECK(mdc && rsc && metadata_str, return NULL);
 214 
 215     key = crm_generate_ra_key(rsc->standard, rsc->provider, rsc->type);
 216     if (!key) {
 217         crm_crit("Could not allocate memory for resource metadata");
 218         goto err;
 219     }
 220 
 221     metadata = string2xml(metadata_str);
 222     if (!metadata) {
 223         crm_err("Metadata for %s:%s:%s is not valid XML",
 224                 rsc->standard, rsc->provider, rsc->type);
 225         goto err;
 226     }
 227 
 228     md = calloc(1, sizeof(struct ra_metadata_s));
 229     if (md == NULL) {
 230         crm_crit("Could not allocate memory for resource metadata");
 231         goto err;
 232     }
 233 
 234 #if ENABLE_VERSIONED_ATTRS
 235     md->ra_version = ra_version_from_xml(metadata, rsc);
 236 #endif
 237 
 238     if (strcmp(rsc->standard, PCMK_RESOURCE_CLASS_OCF) == 0) {
 239         xmlChar *content = NULL;
 240         xmlNode *version_element = first_named_child(metadata, "version");
 241 
 242         if (version_element != NULL) {
 243             content = xmlNodeGetContent(version_element);
 244         }
 245         log_ra_ocf_version(key, (const char *) content);
 246         if (content != NULL) {
 247             ocf1_1 = (compare_version((const char *) content, "1.1") >= 0);
 248             xmlFree(content);
 249         }
 250     }
 251 
 252     // Check supported actions
 253     match = first_named_child(metadata, "actions");
 254     for (match = first_named_child(match, "action"); match != NULL;
 255          match = crm_next_same_xml(match)) {
 256 
 257         const char *action_name = crm_element_value(match, "name");
 258 
 259         if (pcmk__str_eq(action_name, CRMD_ACTION_RELOAD_AGENT,
 260                          pcmk__str_none)) {
 261             if (ocf1_1) {
 262                 controld_set_ra_flags(md, key, ra_supports_reload_agent);
 263             } else {
 264                 crm_notice("reload-agent action will not be used with %s "
 265                            "because it does not support OCF 1.1 or later", key);
 266             }
 267 
 268         } else if (!ocf1_1 && pcmk__str_eq(action_name, CRMD_ACTION_RELOAD,
 269                                            pcmk__str_casei)) {
 270             controld_set_ra_flags(md, key, ra_supports_legacy_reload);
 271         }
 272     }
 273 
 274     // Build a parameter list
 275     match = first_named_child(metadata, "parameters");
 276     for (match = first_named_child(match, "parameter"); match != NULL;
 277          match = crm_next_same_xml(match)) {
 278 
 279         const char *param_name = crm_element_value(match, "name");
 280 
 281         if (param_name == NULL) {
 282             crm_warn("Metadata for %s:%s:%s has parameter without a name",
 283                      rsc->standard, rsc->provider, rsc->type);
 284         } else {
 285             struct ra_param_s *p = ra_param_from_xml(match);
 286 
 287             if (p == NULL) {
 288                 goto err;
 289             }
 290             if (pcmk_is_set(p->rap_flags, ra_param_private)) {
 291                 any_private_params = true;
 292             }
 293             md->ra_params = g_list_prepend(md->ra_params, p);
 294         }
 295     }
 296 
 297     /* Newer resource agents support the "private" parameter attribute to
 298      * indicate sensitive parameters. For backward compatibility with older
 299      * agents, implicitly treat a few common names as private when the agent
 300      * doesn't specify any explicitly.
 301      */
 302     if (!any_private_params) {
 303         for (GList *iter = md->ra_params; iter != NULL; iter = iter->next) {
 304             struct ra_param_s *p = iter->data;
 305 
 306             if (pcmk__str_any_of(p->rap_name, "password", "passwd", "user",
 307                                  NULL)) {
 308                 controld_set_ra_param_flags(p, ra_param_private);
 309             }
 310         }
 311     }
 312 
 313     g_hash_table_replace(mdc, key, md);
 314     free_xml(metadata);
 315     return md;
 316 
 317 err:
 318     free(key);
 319     free_xml(metadata);
 320     metadata_free(md);
 321     return NULL;
 322 }
 323 
 324 /*!
 325  * \internal
 326  * \brief Get meta-data for a resource
 327  *
 328  * \param[in] lrm_state  Use meta-data cache from this executor connection
 329  * \param[in] rsc        Resource to get meta-data for
 330  * \param[in] source     Allowed meta-data sources (bitmask of enum
 331  *                       controld_metadata_source_e values)
 332  *
 333  * \return Meta-data cache entry for given resource, or NULL if not available
 334  */
 335 struct ra_metadata_s *
 336 controld_get_rsc_metadata(lrm_state_t *lrm_state, lrmd_rsc_info_t *rsc,
     /* [previous][next][first][last][top][bottom][index][help] */
 337                           uint32_t source)
 338 {
 339     struct ra_metadata_s *metadata = NULL;
 340     char *metadata_str = NULL;
 341     char *key = NULL;
 342     int rc = pcmk_ok;
 343 
 344     CRM_CHECK((lrm_state != NULL) && (rsc != NULL), return NULL);
 345 
 346     if (pcmk_is_set(source, controld_metadata_from_cache)) {
 347         key = crm_generate_ra_key(rsc->standard, rsc->provider, rsc->type);
 348         if (key != NULL) {
 349             metadata = g_hash_table_lookup(lrm_state->metadata_cache, key);
 350             free(key);
 351         }
 352         if (metadata != NULL) {
 353             return metadata;
 354         }
 355     }
 356 
 357     if (!pcmk_is_set(source, controld_metadata_from_agent)) {
 358         return NULL;
 359     }
 360 
 361     /* For now, we always collect resource agent meta-data via a local,
 362      * synchronous, direct execution of the agent. This has multiple issues:
 363      * the executor should execute agents, not the controller; meta-data for
 364      * Pacemaker Remote nodes should be collected on those nodes, not
 365      * locally; and the meta-data call shouldn't eat into the timeout of the
 366      * real action being performed.
 367      *
 368      * These issues are planned to be addressed by having the scheduler
 369      * schedule a meta-data cache check at the beginning of each transition.
 370      * Once that is working, this block will only be a fallback in case the
 371      * initial collection fails.
 372      */
 373     rc = lrm_state_get_metadata(lrm_state, rsc->standard, rsc->provider,
 374                                 rsc->type, &metadata_str, 0);
 375     if (rc != pcmk_ok) {
 376         crm_warn("Failed to get metadata for %s (%s:%s:%s): %s",
 377                  rsc->id, rsc->standard, rsc->provider, rsc->type,
 378                  pcmk_strerror(rc));
 379         return NULL;
 380     }
 381 
 382     metadata = metadata_cache_update(lrm_state->metadata_cache, rsc,
 383                                      metadata_str);
 384     free(metadata_str);
 385     if (metadata == NULL) {
 386         crm_warn("Failed to update metadata for %s (%s:%s:%s)",
 387                  rsc->id, rsc->standard, rsc->provider, rsc->type);
 388     }
 389     return metadata;
 390 }

/* [previous][next][first][last][top][bottom][index][help] */