summaryrefslogtreecommitdiffstats
path: root/lib/pengine/pe_digest.c
blob: 546a2a763e50b2cc96675f685a58d0c9f0e050f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
 * Copyright 2004-2023 the Pacemaker project contributors
 *
 * The version control history for this file may have further details.
 *
 * This source code is licensed under the GNU Lesser General Public License
 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
 */

#include <crm_internal.h>

#include <glib.h>
#include <stdbool.h>

#include <crm/crm.h>
#include <crm/msg_xml.h>
#include <crm/common/xml.h>
#include <crm/common/xml_internal.h>
#include <crm/pengine/internal.h>
#include "pe_status_private.h"

extern bool pcmk__is_daemon;

/*!
 * \internal
 * \brief Free an operation digest cache entry
 *
 * \param[in,out] ptr  Pointer to cache entry to free
 *
 * \note The argument is a gpointer so this can be used as a hash table
 *       free function.
 */
void
pe__free_digests(gpointer ptr)
{
    op_digest_cache_t *data = ptr;

    if (data != NULL) {
        free_xml(data->params_all);
        free_xml(data->params_secure);
        free_xml(data->params_restart);

        free(data->digest_all_calc);
        free(data->digest_restart_calc);
        free(data->digest_secure_calc);

        free(data);
    }
}

// Return true if XML attribute name is not substring of a given string
static bool
attr_not_in_string(xmlAttrPtr a, void *user_data)
{
    bool filter = false;
    char *name = crm_strdup_printf(" %s ", (const char *) a->name);

    if (strstr((const char *) user_data, name) == NULL) {
        crm_trace("Filtering %s (not found in '%s')",
                  (const char *) a->name, (const char *) user_data);
        filter = true;
    }
    free(name);
    return filter;
}

// Return true if XML attribute name is substring of a given string
static bool
attr_in_string(xmlAttrPtr a, void *user_data)
{
    bool filter = false;
    char *name = crm_strdup_printf(" %s ", (const char *) a->name);

    if (strstr((const char *) user_data, name) != NULL) {
        crm_trace("Filtering %s (found in '%s')",
                  (const char *) a->name, (const char *) user_data);
        filter = true;
    }
    free(name);
    return filter;
}

/*!
 * \internal
 * \brief Add digest of all parameters to a digest cache entry
 *
 * \param[out]    data         Digest cache entry to modify
 * \param[in,out] rsc          Resource that action was for
 * \param[in]     node         Node action was performed on
 * \param[in]     params       Resource parameters evaluated for node
 * \param[in]     task         Name of action performed
 * \param[in,out] interval_ms  Action's interval (will be reset if in overrides)
 * \param[in]     xml_op       Unused
 * \param[in]     op_version   CRM feature set to use for digest calculation
 * \param[in]     overrides    Key/value table to override resource parameters
 * \param[in,out] scheduler    Scheduler data
 */
static void
calculate_main_digest(op_digest_cache_t *data, pcmk_resource_t *rsc,
                      const pcmk_node_t *node, GHashTable *params,
                      const char *task, guint *interval_ms,
                      const xmlNode *xml_op, const char *op_version,
                      GHashTable *overrides, pcmk_scheduler_t *scheduler)
{
    xmlNode *action_config = NULL;

    data->params_all = create_xml_node(NULL, XML_TAG_PARAMS);

    /* REMOTE_CONTAINER_HACK: Allow Pacemaker Remote nodes to run containers
     * that themselves are Pacemaker Remote nodes
     */
    (void) pe__add_bundle_remote_name(rsc, scheduler, data->params_all,
                                      XML_RSC_ATTR_REMOTE_RA_ADDR);

    if (overrides != NULL) {
        // If interval was overridden, reset it
        const char *interval_s = g_hash_table_lookup(overrides, CRM_META "_"
                                                     XML_LRM_ATTR_INTERVAL);

        if (interval_s != NULL) {
            long long value_ll;

            if ((pcmk__scan_ll(interval_s, &value_ll, 0LL) == pcmk_rc_ok)
                && (value_ll >= 0) && (value_ll <= G_MAXUINT)) {
                *interval_ms = (guint) value_ll;
            }
        }

        // Add overrides to list of all parameters
        g_hash_table_foreach(overrides, hash2field, data->params_all);
    }

    // Add provided instance parameters
    g_hash_table_foreach(params, hash2field, data->params_all);

    // Find action configuration XML in CIB
    action_config = pcmk__find_action_config(rsc, task, *interval_ms, true);

    /* Add action-specific resource instance attributes to the digest list.
     *
     * If this is a one-time action with action-specific instance attributes,
     * enforce a restart instead of reload-agent in case the main digest doesn't
     * match, even if the restart digest does. This ensures any changes of the
     * action-specific parameters get applied for this specific action, and
     * digests calculated for the resulting history will be correct. Default the
     * result to RSC_DIGEST_RESTART for the case where the main digest doesn't
     * match.
     */
    params = pcmk__unpack_action_rsc_params(action_config, node->details->attrs,
                                            scheduler);
    if ((*interval_ms == 0) && (g_hash_table_size(params) > 0)) {
        data->rc = pcmk__digest_restart;
    }
    g_hash_table_foreach(params, hash2field, data->params_all);
    g_hash_table_destroy(params);

    // Add action meta-attributes
    params = pcmk__unpack_action_meta(rsc, node, task, *interval_ms,
                                      action_config);
    g_hash_table_foreach(params, hash2metafield, data->params_all);
    g_hash_table_destroy(params);

    pcmk__filter_op_for_digest(data->params_all);

    data->digest_all_calc = calculate_operation_digest(data->params_all,
                                                       op_version);
}

// Return true if XML attribute name is a Pacemaker-defined fencing parameter
static bool
is_fence_param(xmlAttrPtr attr, void *user_data)
{
    return pcmk_stonith_param((const char *) attr->name);
}

/*!
 * \internal
 * \brief Add secure digest to a digest cache entry
 *
 * \param[out] data        Digest cache entry to modify
 * \param[in]  rsc         Resource that action was for
 * \param[in]  params      Resource parameters evaluated for node
 * \param[in]  xml_op      XML of operation in CIB status (if available)
 * \param[in]  op_version  CRM feature set to use for digest calculation
 * \param[in]  overrides   Key/value hash table to override resource parameters
 */
static void
calculate_secure_digest(op_digest_cache_t *data, const pcmk_resource_t *rsc,
                        GHashTable *params, const xmlNode *xml_op,
                        const char *op_version, GHashTable *overrides)
{
    const char *class = crm_element_value(rsc->xml, XML_AGENT_ATTR_CLASS);
    const char *secure_list = NULL;
    bool old_version = (compare_version(op_version, "3.16.0") < 0);

    if (xml_op == NULL) {
        secure_list = " passwd password user ";
    } else {
        secure_list = crm_element_value(xml_op, XML_LRM_ATTR_OP_SECURE);
    }

    if (old_version) {
        data->params_secure = create_xml_node(NULL, XML_TAG_PARAMS);
        if (overrides != NULL) {
            g_hash_table_foreach(overrides, hash2field, data->params_secure);
        }

        g_hash_table_foreach(params, hash2field, data->params_secure);

    } else {
        // Start with a copy of all parameters
        data->params_secure = copy_xml(data->params_all);
    }

    if (secure_list != NULL) {
        pcmk__xe_remove_matching_attrs(data->params_secure, attr_in_string,
                                       (void *) secure_list);
    }
    if (old_version
        && pcmk_is_set(pcmk_get_ra_caps(class),
                       pcmk_ra_cap_fence_params)) {
        /* For stonith resources, Pacemaker adds special parameters,
         * but these are not listed in fence agent meta-data, so with older
         * versions of DC, the controller will not hash them. That means we have
         * to filter them out before calculating our hash for comparison.
         */
        pcmk__xe_remove_matching_attrs(data->params_secure, is_fence_param,
                                       NULL);
    }
    pcmk__filter_op_for_digest(data->params_secure);

    /* CRM_meta_timeout *should* be part of a digest for recurring operations.
     * However, with older versions of DC, the controller does not add timeout
     * to secure digests, because it only includes parameters declared by the
     * resource agent.
     * Remove any timeout that made it this far, to match.
     */
    if (old_version) {
        xml_remove_prop(data->params_secure, CRM_META "_" XML_ATTR_TIMEOUT);
    }

    data->digest_secure_calc = calculate_operation_digest(data->params_secure,
                                                          op_version);
}

/*!
 * \internal
 * \brief Add restart digest to a digest cache entry
 *
 * \param[out] data        Digest cache entry to modify
 * \param[in]  xml_op      XML of operation in CIB status (if available)
 * \param[in]  op_version  CRM feature set to use for digest calculation
 *
 * \note This function doesn't need to handle overrides because it starts with
 *       data->params_all, which already has overrides applied.
 */
static void
calculate_restart_digest(op_digest_cache_t *data, const xmlNode *xml_op,
                         const char *op_version)
{
    const char *value = NULL;

    // We must have XML of resource operation history
    if (xml_op == NULL) {
        return;
    }

    // And the history must have a restart digest to compare against
    if (crm_element_value(xml_op, XML_LRM_ATTR_RESTART_DIGEST) == NULL) {
        return;
    }

    // Start with a copy of all parameters
    data->params_restart = copy_xml(data->params_all);

    // Then filter out reloadable parameters, if any
    value = crm_element_value(xml_op, XML_LRM_ATTR_OP_RESTART);
    if (value != NULL) {
        pcmk__xe_remove_matching_attrs(data->params_restart, attr_not_in_string,
                                       (void *) value);
    }

    value = crm_element_value(xml_op, XML_ATTR_CRM_VERSION);
    data->digest_restart_calc = calculate_operation_digest(data->params_restart,
                                                           value);
}

/*!
 * \internal
 * \brief Create a new digest cache entry with calculated digests
 *
 * \param[in,out] rsc          Resource that action was for
 * \param[in]     task         Name of action performed
 * \param[in,out] interval_ms  Action's interval (will be reset if in overrides)
 * \param[in]     node         Node action was performed on
 * \param[in]     xml_op       XML of operation in CIB status (if available)
 * \param[in]     overrides    Key/value table to override resource parameters
 * \param[in]     calc_secure  Whether to calculate secure digest
 * \param[in,out] scheduler    Scheduler data
 *
 * \return Pointer to new digest cache entry (or NULL on memory error)
 * \note It is the caller's responsibility to free the result using
 *       pe__free_digests().
 */
op_digest_cache_t *
pe__calculate_digests(pcmk_resource_t *rsc, const char *task,
                      guint *interval_ms, const pcmk_node_t *node,
                      const xmlNode *xml_op, GHashTable *overrides,
                      bool calc_secure, pcmk_scheduler_t *scheduler)
{
    op_digest_cache_t *data = calloc(1, sizeof(op_digest_cache_t));
    const char *op_version = NULL;
    GHashTable *params = NULL;

    if (data == NULL) {
        return NULL;
    }

    data->rc = pcmk__digest_match;

    if (xml_op != NULL) {
        op_version = crm_element_value(xml_op, XML_ATTR_CRM_VERSION);
    }

    if (op_version == NULL && scheduler != NULL && scheduler->input != NULL) {
        op_version = crm_element_value(scheduler->input, XML_ATTR_CRM_VERSION);
    }

    if (op_version == NULL) {
        op_version = CRM_FEATURE_SET;
    }

    params = pe_rsc_params(rsc, node, scheduler);
    calculate_main_digest(data, rsc, node, params, task, interval_ms, xml_op,
                          op_version, overrides, scheduler);
    if (calc_secure) {
        calculate_secure_digest(data, rsc, params, xml_op, op_version,
                                overrides);
    }
    calculate_restart_digest(data, xml_op, op_version);
    return data;
}

/*!
 * \internal
 * \brief Calculate action digests and store in node's digest cache
 *
 * \param[in,out] rsc          Resource that action was for
 * \param[in]     task         Name of action performed
 * \param[in]     interval_ms  Action's interval
 * \param[in,out] node         Node action was performed on
 * \param[in]     xml_op       XML of operation in CIB status (if available)
 * \param[in]     calc_secure  Whether to calculate secure digest
 * \param[in,out] scheduler    Scheduler data
 *
 * \return Pointer to node's digest cache entry
 */
static op_digest_cache_t *
rsc_action_digest(pcmk_resource_t *rsc, const char *task, guint interval_ms,
                  pcmk_node_t *node, const xmlNode *xml_op,
                  bool calc_secure, pcmk_scheduler_t *scheduler)
{
    op_digest_cache_t *data = NULL;
    char *key = pcmk__op_key(rsc->id, task, interval_ms);

    data = g_hash_table_lookup(node->details->digest_cache, key);
    if (data == NULL) {
        data = pe__calculate_digests(rsc, task, &interval_ms, node, xml_op,
                                     NULL, calc_secure, scheduler);
        CRM_ASSERT(data != NULL);
        g_hash_table_insert(node->details->digest_cache, strdup(key), data);
    }
    free(key);
    return data;
}

/*!
 * \internal
 * \brief Calculate operation digests and compare against an XML history entry
 *
 * \param[in,out] rsc        Resource to check
 * \param[in]     xml_op     Resource history XML
 * \param[in,out] node       Node to use for digest calculation
 * \param[in,out] scheduler  Scheduler data
 *
 * \return Pointer to node's digest cache entry, with comparison result set
 */
op_digest_cache_t *
rsc_action_digest_cmp(pcmk_resource_t *rsc, const xmlNode *xml_op,
                      pcmk_node_t *node, pcmk_scheduler_t *scheduler)
{
    op_digest_cache_t *data = NULL;
    guint interval_ms = 0;

    const char *op_version;
    const char *task = crm_element_value(xml_op, XML_LRM_ATTR_TASK);
    const char *digest_all;
    const char *digest_restart;

    CRM_ASSERT(node != NULL);

    op_version = crm_element_value(xml_op, XML_ATTR_CRM_VERSION);
    digest_all = crm_element_value(xml_op, XML_LRM_ATTR_OP_DIGEST);
    digest_restart = crm_element_value(xml_op, XML_LRM_ATTR_RESTART_DIGEST);

    crm_element_value_ms(xml_op, XML_LRM_ATTR_INTERVAL_MS, &interval_ms);
    data = rsc_action_digest(rsc, task, interval_ms, node, xml_op,
                             pcmk_is_set(scheduler->flags,
                                         pcmk_sched_sanitized),
                             scheduler);

    if (digest_restart && data->digest_restart_calc && strcmp(data->digest_restart_calc, digest_restart) != 0) {
        pe_rsc_info(rsc, "Parameters to %ums-interval %s action for %s on %s "
                         "changed: hash was %s vs. now %s (restart:%s) %s",
                    interval_ms, task, rsc->id, pe__node_name(node),
                    pcmk__s(digest_restart, "missing"),
                    data->digest_restart_calc,
                    op_version,
                    crm_element_value(xml_op, XML_ATTR_TRANSITION_MAGIC));
        data->rc = pcmk__digest_restart;

    } else if (digest_all == NULL) {
        /* it is unknown what the previous op digest was */
        data->rc = pcmk__digest_unknown;

    } else if (strcmp(digest_all, data->digest_all_calc) != 0) {
        /* Given a non-recurring operation with extra parameters configured,
         * in case that the main digest doesn't match, even if the restart
         * digest matches, enforce a restart rather than a reload-agent anyway.
         * So that it ensures any changes of the extra parameters get applied
         * for this specific operation, and the digests calculated for the
         * resulting lrm_rsc_op will be correct.
         * Preserve the implied rc pcmk__digest_restart for the case that the
         * main digest doesn't match.
         */
        if ((interval_ms == 0) && (data->rc == pcmk__digest_restart)) {
            pe_rsc_info(rsc, "Parameters containing extra ones to %ums-interval"
                             " %s action for %s on %s "
                             "changed: hash was %s vs. now %s (restart:%s) %s",
                        interval_ms, task, rsc->id, pe__node_name(node),
                        pcmk__s(digest_all, "missing"), data->digest_all_calc,
                        op_version,
                        crm_element_value(xml_op, XML_ATTR_TRANSITION_MAGIC));

        } else {
            pe_rsc_info(rsc, "Parameters to %ums-interval %s action for %s on %s "
                             "changed: hash was %s vs. now %s (%s:%s) %s",
                        interval_ms, task, rsc->id, pe__node_name(node),
                        pcmk__s(digest_all, "missing"), data->digest_all_calc,
                        (interval_ms > 0)? "reschedule" : "reload",
                        op_version,
                        crm_element_value(xml_op, XML_ATTR_TRANSITION_MAGIC));
            data->rc = pcmk__digest_mismatch;
        }

    } else {
        data->rc = pcmk__digest_match;
    }
    return data;
}

/*!
 * \internal
 * \brief Create an unfencing summary for use in special node attribute
 *
 * Create a string combining a fence device's resource ID, agent type, and
 * parameter digest (whether for all parameters or just non-private parameters).
 * This can be stored in a special node attribute, allowing us to detect changes
 * in either the agent type or parameters, to know whether unfencing must be
 * redone or can be safely skipped when the device's history is cleaned.
 *
 * \param[in] rsc_id        Fence device resource ID
 * \param[in] agent_type    Fence device agent
 * \param[in] param_digest  Fence device parameter digest
 *
 * \return Newly allocated string with unfencing digest
 * \note The caller is responsible for freeing the result.
 */
static inline char *
create_unfencing_summary(const char *rsc_id, const char *agent_type,
                         const char *param_digest)
{
    return crm_strdup_printf("%s:%s:%s", rsc_id, agent_type, param_digest);
}

/*!
 * \internal
 * \brief Check whether a node can skip unfencing
 *
 * Check whether a fence device's current definition matches a node's
 * stored summary of when it was last unfenced by the device.
 *
 * \param[in] rsc_id        Fence device's resource ID
 * \param[in] agent         Fence device's agent type
 * \param[in] digest_calc   Fence device's current parameter digest
 * \param[in] node_summary  Value of node's special unfencing node attribute
 *                          (a comma-separated list of unfencing summaries for
 *                          all devices that have unfenced this node)
 *
 * \return TRUE if digest matches, FALSE otherwise
 */
static bool
unfencing_digest_matches(const char *rsc_id, const char *agent,
                         const char *digest_calc, const char *node_summary)
{
    bool matches = FALSE;

    if (rsc_id && agent && digest_calc && node_summary) {
        char *search_secure = create_unfencing_summary(rsc_id, agent,
                                                       digest_calc);

        /* The digest was calculated including the device ID and agent,
         * so there is no risk of collision using strstr().
         */
        matches = (strstr(node_summary, search_secure) != NULL);
        crm_trace("Calculated unfencing digest '%s' %sfound in '%s'",
                  search_secure, matches? "" : "not ", node_summary);
        free(search_secure);
    }
    return matches;
}

/* Magic string to use as action name for digest cache entries used for
 * unfencing checks. This is not a real action name (i.e. "on"), so
 * pcmk__check_action_config() won't confuse these entries with real actions.
 */
#define STONITH_DIGEST_TASK "stonith-on"

/*!
 * \internal
 * \brief Calculate fence device digests and digest comparison result
 *
 * \param[in,out] rsc        Fence device resource
 * \param[in]     agent      Fence device's agent type
 * \param[in,out] node       Node with digest cache to use
 * \param[in,out] scheduler  Scheduler data
 *
 * \return Node's digest cache entry
 */
op_digest_cache_t *
pe__compare_fencing_digest(pcmk_resource_t *rsc, const char *agent,
                           pcmk_node_t *node, pcmk_scheduler_t *scheduler)
{
    const char *node_summary = NULL;

    // Calculate device's current parameter digests
    op_digest_cache_t *data = rsc_action_digest(rsc, STONITH_DIGEST_TASK, 0U,
                                                node, NULL, TRUE, scheduler);

    // Check whether node has special unfencing summary node attribute
    node_summary = pe_node_attribute_raw(node, CRM_ATTR_DIGESTS_ALL);
    if (node_summary == NULL) {
        data->rc = pcmk__digest_unknown;
        return data;
    }

    // Check whether full parameter digest matches
    if (unfencing_digest_matches(rsc->id, agent, data->digest_all_calc,
                                 node_summary)) {
        data->rc = pcmk__digest_match;
        return data;
    }

    // Check whether secure parameter digest matches
    node_summary = pe_node_attribute_raw(node, CRM_ATTR_DIGESTS_SECURE);
    if (unfencing_digest_matches(rsc->id, agent, data->digest_secure_calc,
                                 node_summary)) {
        data->rc = pcmk__digest_match;
        if (!pcmk__is_daemon && scheduler->priv != NULL) {
            pcmk__output_t *out = scheduler->priv;
            out->info(out, "Only 'private' parameters to %s "
                      "for unfencing %s changed", rsc->id,
                      pe__node_name(node));
        }
        return data;
    }

    // Parameters don't match
    data->rc = pcmk__digest_mismatch;
    if (pcmk_is_set(scheduler->flags, pcmk_sched_sanitized)
        && (data->digest_secure_calc != NULL)) {

        if (scheduler->priv != NULL) {
            pcmk__output_t *out = scheduler->priv;
            char *digest = create_unfencing_summary(rsc->id, agent,
                                                    data->digest_secure_calc);

            out->info(out, "Parameters to %s for unfencing "
                      "%s changed, try '%s'", rsc->id,
                      pe__node_name(node), digest);
            free(digest);
        } else if (!pcmk__is_daemon) {
            char *digest = create_unfencing_summary(rsc->id, agent,
                                                    data->digest_secure_calc);

            printf("Parameters to %s for unfencing %s changed, try '%s'\n",
                   rsc->id, pe__node_name(node), digest);
            free(digest);
        }
    }
    return data;
}