summaryrefslogtreecommitdiffstats
path: root/registry/registry.c
blob: cd33e5ca09dc087889c2f7fae797541ef9e78e50 (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
// SPDX-License-Identifier: GPL-3.0-or-later

#include "daemon/common.h"
#include "registry_internals.h"

#define REGISTRY_STATUS_OK "ok"
#define REGISTRY_STATUS_REDIRECT "redirect"
#define REGISTRY_STATUS_FAILED "failed"
#define REGISTRY_STATUS_DISABLED "disabled"

bool registry_is_valid_url(const char *url) {
    return url && (*url == 'h' || *url == '*');
}

// ----------------------------------------------------------------------------
// REGISTRY concurrency locking

static inline void registry_lock(void) {
    netdata_mutex_lock(&registry.lock);
}

static inline void registry_unlock(void) {
    netdata_mutex_unlock(&registry.lock);
}

// ----------------------------------------------------------------------------
// COOKIES

static void registry_set_cookie(struct web_client *w, const char *guid) {
    char e_date[100];
    time_t et = now_realtime_sec() + registry.persons_expiration;
    struct tm e_tm_buf, *etm = gmtime_r(&et, &e_tm_buf);
    strftime(e_date, sizeof(e_date), "%a, %d %b %Y %H:%M:%S %Z", etm);

    buffer_sprintf(w->response.header, "Set-Cookie: " NETDATA_REGISTRY_COOKIE_NAME "=%s; Expires=%s\r\n", guid, e_date);
    buffer_sprintf(w->response.header, "Set-Cookie: " NETDATA_REGISTRY_COOKIE_NAME "=%s; SameSite=Strict; Expires=%s\r\n", guid, e_date);
    if(registry.enable_cookies_samesite_secure)
        buffer_sprintf(w->response.header, "Set-Cookie: " NETDATA_REGISTRY_COOKIE_NAME "=%s; Expires=%s; SameSite=None; Secure\r\n", guid, e_date);

    if(registry.registry_domain && *registry.registry_domain) {
        buffer_sprintf(w->response.header, "Set-Cookie: " NETDATA_REGISTRY_COOKIE_NAME "=%s; Expires=%s; Domain=%s\r\n", guid, e_date, registry.registry_domain);
        buffer_sprintf(w->response.header, "Set-Cookie: " NETDATA_REGISTRY_COOKIE_NAME "=%s; Expires=%s; Domain=%s; SameSite=Strict\r\n", guid, e_date, registry.registry_domain);
        if(registry.enable_cookies_samesite_secure)
            buffer_sprintf(w->response.header, "Set-Cookie: " NETDATA_REGISTRY_COOKIE_NAME "=%s; Expires=%s; Domain=%s; SameSite=None; Secure\r\n", guid, e_date, registry.registry_domain);
    }

    w->response.has_cookies = true;
}

static inline void registry_set_person_cookie(struct web_client *w, REGISTRY_PERSON *p) {
    registry_set_cookie(w, p->guid);
}


// ----------------------------------------------------------------------------
// JSON GENERATION

static inline void registry_json_header(RRDHOST *host, struct web_client *w, const char *action, const char *status) {
    buffer_flush(w->response.data);
    w->response.data->content_type = CT_APPLICATION_JSON;
    buffer_json_initialize(w->response.data, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_DEFAULT);
    buffer_json_member_add_string(w->response.data, "action", action);
    buffer_json_member_add_string(w->response.data, "status", status);
    buffer_json_member_add_string(w->response.data, "hostname", rrdhost_registry_hostname(host));
    buffer_json_member_add_string(w->response.data, "machine_guid", host->machine_guid);
}

static inline void registry_json_footer(struct web_client *w) {
    buffer_json_finalize(w->response.data);
}

static inline int registry_json_disabled(RRDHOST *host, struct web_client *w, const char *action) {
    registry_json_header(host, w, action, REGISTRY_STATUS_DISABLED);

    buffer_json_member_add_string(w->response.data, "registry", registry.registry_to_announce);

    registry_json_footer(w);
    return HTTP_RESP_OK;
}


// ----------------------------------------------------------------------------
// CALLBACKS FOR WALKING THROUGH REGISTRY OBJECTS

// structure used be the callbacks below
struct registry_json_walk_person_urls_callback {
    REGISTRY_PERSON *p;
    REGISTRY_MACHINE *m;
    struct web_client *w;
    int count;
};

static STRING *asterisks = NULL;

// callback for rendering PERSON_URLs
static int registry_json_person_url_callback(REGISTRY_PERSON_URL *pu, struct registry_json_walk_person_urls_callback *c) {
    if(unlikely(!asterisks))
        asterisks = string_strdupz("***");

    struct web_client *w = c->w;

    if (pu->url == asterisks) return 0;

    buffer_json_add_array_item_array(w->response.data);
    buffer_json_add_array_item_string(w->response.data, pu->machine->guid);
    buffer_json_add_array_item_string(w->response.data, string2str(pu->url));
    buffer_json_add_array_item_uint64(w->response.data, pu->last_t * (uint64_t) 1000);
    buffer_json_add_array_item_uint64(w->response.data, pu->usages);
    buffer_json_add_array_item_string(w->response.data, string2str(pu->machine_name));
    buffer_json_array_close(w->response.data);

    return 1;
}

// callback for rendering MACHINE_URLs
static int registry_json_machine_url_callback(REGISTRY_MACHINE_URL *mu, struct registry_json_walk_person_urls_callback *c, STRING *hostname) {
    if(unlikely(!asterisks))
        asterisks = string_strdupz("***");

    struct web_client *w = c->w;
    REGISTRY_MACHINE *m = c->m;

    if (mu->url == asterisks) return 0;

    buffer_json_add_array_item_array(w->response.data);
    buffer_json_add_array_item_string(w->response.data, m->guid);
    buffer_json_add_array_item_string(w->response.data, string2str(mu->url));
    buffer_json_add_array_item_uint64(w->response.data, mu->last_t * (uint64_t) 1000);
    buffer_json_add_array_item_uint64(w->response.data, mu->usages);
    buffer_json_add_array_item_string(w->response.data, string2str(hostname));
    buffer_json_array_close(w->response.data);

    return 1;
}

// ----------------------------------------------------------------------------

// structure used be the callbacks below
struct registry_person_url_callback_verify_machine_exists_data {
    REGISTRY_MACHINE *m;
    int count;
};

static inline int registry_person_url_callback_verify_machine_exists(REGISTRY_PERSON_URL *pu, struct registry_person_url_callback_verify_machine_exists_data *d) {
    REGISTRY_MACHINE *m = d->m;

    if(pu->machine == m)
        d->count++;

    return 0;
}

// ----------------------------------------------------------------------------
// dynamic update of the configuration
// The registry does not seem to be designed to support this and I cannot see any concurrency protection
// that could make this safe, so try to be as atomic as possible.

void registry_update_cloud_base_url() {
    registry.cloud_base_url = appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "cloud base url", DEFAULT_CLOUD_BASE_URL);
    setenv("NETDATA_REGISTRY_CLOUD_BASE_URL", registry.cloud_base_url, 1);
}

// ----------------------------------------------------------------------------
// public HELLO request

int registry_request_hello_json(RRDHOST *host, struct web_client *w, bool do_not_track) {
    registry_json_header(host, w, "hello", REGISTRY_STATUS_OK);

    if(host->node_id)
        buffer_json_member_add_uuid(w->response.data, "node_id", host->node_id);

    buffer_json_member_add_object(w->response.data, "agent");
    {
        buffer_json_member_add_string(w->response.data, "machine_guid", localhost->machine_guid);

        if(localhost->node_id)
            buffer_json_member_add_uuid(w->response.data, "node_id", localhost->node_id);

        char *claim_id = get_agent_claimid();
        if (claim_id) {
            buffer_json_member_add_string(w->response.data, "claim_id", claim_id);
            freez(claim_id);
        }

        buffer_json_member_add_boolean(w->response.data, "bearer_protection", netdata_is_protected_by_bearer);
    }
    buffer_json_object_close(w->response.data);

    CLOUD_STATUS status = cloud_status();
    buffer_json_member_add_string(w->response.data, "cloud_status", cloud_status_to_string(status));
    buffer_json_member_add_string(w->response.data, "cloud_base_url", registry.cloud_base_url);

    buffer_json_member_add_string(w->response.data, "registry", registry.registry_to_announce);
    buffer_json_member_add_boolean(w->response.data, "anonymous_statistics", do_not_track ? false : netdata_anonymous_statistics_enabled);
    buffer_json_member_add_boolean(w->response.data, "X-Netdata-Auth", true);

    buffer_json_member_add_array(w->response.data, "nodes");
    RRDHOST *h;
    dfe_start_read(rrdhost_root_index, h) {
        buffer_json_add_array_item_object(w->response.data);
        buffer_json_member_add_string(w->response.data, "machine_guid", h->machine_guid);

        if(h->node_id)
            buffer_json_member_add_uuid(w->response.data, "node_id", h->node_id);

        buffer_json_member_add_string(w->response.data, "hostname", rrdhost_registry_hostname(h));
        buffer_json_object_close(w->response.data);
    }
    dfe_done(h);
    buffer_json_array_close(w->response.data); // nodes

    registry_json_footer(w);
    return HTTP_RESP_OK;
}

// ----------------------------------------------------------------------------
// public ACCESS request

// the main method for registering an access
int registry_request_access_json(RRDHOST *host, struct web_client *w, char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
    if(unlikely(!registry.enabled))
        return registry_json_disabled(host, w, "access");

    if(!registry_is_valid_url(url)) {
        buffer_flush(w->response.data);
        buffer_strcat(w->response.data, "Invalid URL given in the request");
        return HTTP_RESP_BAD_REQUEST;
    }

    // ------------------------------------------------------------------------
    // verify the browser supports cookies or the bearer

    if(registry.verify_cookies_redirects > 0 && !person_guid[0]) {
        registry_lock();
        registry_request_access(REGISTRY_VERIFY_COOKIES_GUID, machine_guid, url, name, when);
        registry_unlock();

        buffer_flush(w->response.data);
        registry_set_cookie(w, REGISTRY_VERIFY_COOKIES_GUID);
        w->response.data->content_type = CT_APPLICATION_JSON;
        registry_json_header(host, w, "access", REGISTRY_STATUS_REDIRECT);
        buffer_json_member_add_string(w->response.data, "person_guid", REGISTRY_VERIFY_COOKIES_GUID);
        buffer_json_member_add_string(w->response.data, "registry", registry.registry_to_announce);
        registry_json_footer(w);
        return HTTP_RESP_OK;
    }

    if(unlikely(person_guid[0] && is_dummy_person(person_guid)))
        // it passed the check - they gave us a different person_guid
        // empty the dummy one, so that we will generate a new person_guid
        person_guid[0] = '\0';

    // ------------------------------------------------------------------------

    registry_lock();

    REGISTRY_PERSON *p = registry_request_access(person_guid, machine_guid, url, name, when);
    if(!p) {
        registry_json_header(host, w, "access", REGISTRY_STATUS_FAILED);
        registry_json_footer(w);
        registry_unlock();
        return HTTP_RESP_PRECOND_FAIL;
    }

    // set the cookie
    registry_set_person_cookie(w, p);

    // generate the response
    registry_json_header(host, w, "access", REGISTRY_STATUS_OK);
    buffer_json_member_add_string(w->response.data, "person_guid", p->guid);
    buffer_json_member_add_array(w->response.data, "urls");

    struct registry_json_walk_person_urls_callback c = { p, NULL, w, 0 };
    for(REGISTRY_PERSON_URL *pu = p->person_urls; pu ;pu = pu->next)
        registry_json_person_url_callback(pu, &c);
    buffer_json_array_close(w->response.data); // urls

    registry_json_footer(w);
    registry_unlock();
    return HTTP_RESP_OK;
}

// ----------------------------------------------------------------------------
// public DELETE request

// the main method for deleting a URL from a person
int registry_request_delete_json(RRDHOST *host, struct web_client *w, char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
    if(!registry.enabled)
        return registry_json_disabled(host, w, "delete");

    if(!registry_is_valid_url(url)) {
        buffer_flush(w->response.data);
        buffer_strcat(w->response.data, "Invalid URL given in the request");
        return HTTP_RESP_BAD_REQUEST;
    }

    registry_lock();

    REGISTRY_PERSON *p = registry_request_delete(person_guid, machine_guid, url, delete_url, when);
    if(!p) {
        registry_json_header(host, w, "delete", REGISTRY_STATUS_FAILED);
        registry_json_footer(w);
        registry_unlock();
        return HTTP_RESP_PRECOND_FAIL;
    }

    // generate the response
    registry_json_header(host, w, "delete", REGISTRY_STATUS_OK);
    registry_json_footer(w);
    registry_unlock();
    return HTTP_RESP_OK;
}

// ----------------------------------------------------------------------------
// public SEARCH request

// the main method for searching the URLs of a netdata
int registry_request_search_json(RRDHOST *host, struct web_client *w, char *person_guid, char *request_machine) {
    if(!registry.enabled)
        return registry_json_disabled(host, w, "search");

    if(!person_guid || !person_guid[0]) {
        registry_json_header(host, w, "search", REGISTRY_STATUS_FAILED);
        registry_json_footer(w);
        return HTTP_RESP_PRECOND_FAIL;
    }

    registry_lock();

    STRING *hostname = NULL;
    REGISTRY_MACHINE *m = registry_request_machine(person_guid, request_machine, &hostname);
    if(!m) {
        registry_json_header(host, w, "search", REGISTRY_STATUS_FAILED);
        registry_json_footer(w);
        registry_unlock();
        string_freez(hostname);
        return HTTP_RESP_NOT_FOUND;
    }

    registry_json_header(host, w, "search", REGISTRY_STATUS_OK);

    buffer_json_member_add_array(w->response.data, "urls");
    struct registry_json_walk_person_urls_callback c = { NULL, m, w, 0 };

    for(REGISTRY_MACHINE_URL *mu = m->machine_urls; mu ; mu = mu->next)
        registry_json_machine_url_callback(mu, &c, hostname);

    buffer_json_array_close(w->response.data);

    registry_json_footer(w);
    registry_unlock();
    string_freez(hostname);
    return HTTP_RESP_OK;
}

// ----------------------------------------------------------------------------
// SWITCH REQUEST

// the main method for switching user identity
int registry_request_switch_json(RRDHOST *host, struct web_client *w, char *person_guid, char *machine_guid, char *url __maybe_unused, char *new_person_guid, time_t when __maybe_unused) {
    if(!registry.enabled)
        return registry_json_disabled(host, w, "switch");

    if(!person_guid || !person_guid[0]) {
        buffer_flush(w->response.data);
        buffer_strcat(w->response.data, "Who are you? Person GUID is missing");
        return HTTP_RESP_PRECOND_FAIL;
    }

    if(!registry_is_valid_url(url)) {
        buffer_flush(w->response.data);
        buffer_strcat(w->response.data, "Invalid URL given in the request");
        return HTTP_RESP_BAD_REQUEST;
    }

    registry_lock();

    REGISTRY_PERSON *op = registry_person_find(person_guid);
    if(!op) {
        registry_json_header(host, w, "switch", REGISTRY_STATUS_FAILED);
        registry_json_footer(w);
        registry_unlock();
        return 430;
    }

    REGISTRY_PERSON *np = registry_person_find(new_person_guid);
    if(!np) {
        registry_json_header(host, w, "switch", REGISTRY_STATUS_FAILED);
        registry_json_footer(w);
        registry_unlock();
        return 431;
    }

    REGISTRY_MACHINE *m = registry_machine_find(machine_guid);
    if(!m) {
        registry_json_header(host, w, "switch", REGISTRY_STATUS_FAILED);
        registry_json_footer(w);
        registry_unlock();
        return 432;
    }

    struct registry_person_url_callback_verify_machine_exists_data data = { m, 0 };

    // verify the old person has access to this machine
    for(REGISTRY_PERSON_URL *pu = op->person_urls; pu ;pu = pu->next)
        registry_person_url_callback_verify_machine_exists(pu, &data);

    if(!data.count) {
        registry_json_header(host, w, "switch", REGISTRY_STATUS_FAILED);
        registry_json_footer(w);
        registry_unlock();
        return 433;
    }

    // verify the new person has access to this machine
    data.count = 0;
    for(REGISTRY_PERSON_URL *pu = np->person_urls; pu ;pu = pu->next)
        registry_person_url_callback_verify_machine_exists(pu, &data);

    if(!data.count) {
        registry_json_header(host, w, "switch", REGISTRY_STATUS_FAILED);
        registry_json_footer(w);
        registry_unlock();
        return 434;
    }

    // set the cookie of the new person
    // the user just switched identity
    registry_set_person_cookie(w, np);

    // generate the response
    registry_json_header(host, w, "switch", REGISTRY_STATUS_OK);
    buffer_json_member_add_string(w->response.data, "person_guid", np->guid);
    registry_json_footer(w);

    registry_unlock();
    return HTTP_RESP_OK;
}

// ----------------------------------------------------------------------------
// STATISTICS

void registry_statistics(void) {
    if(!registry.enabled) return;

    static RRDSET *sts = NULL, *stc = NULL, *stm = NULL;

    if(unlikely(!sts)) {
        sts = rrdset_create_localhost(
                "netdata"
                , "registry_sessions"
                , NULL
                , "registry"
                , NULL
                , "Netdata Registry Sessions"
                , "sessions"
                , "registry"
                , "stats"
                , 131000
                , localhost->rrd_update_every
                , RRDSET_TYPE_LINE
        );

        rrddim_add(sts, "sessions",  NULL,  1, 1, RRD_ALGORITHM_ABSOLUTE);
    }

    rrddim_set(sts, "sessions", (collected_number)registry.usages_count);
    rrdset_done(sts);

    // ------------------------------------------------------------------------

    if(unlikely(!stc)) {
        stc = rrdset_create_localhost(
                "netdata"
                , "registry_entries"
                , NULL
                , "registry"
                , NULL
                , "Netdata Registry Entries"
                , "entries"
                , "registry"
                , "stats"
                , 131100
                , localhost->rrd_update_every
                , RRDSET_TYPE_LINE
        );

        rrddim_add(stc, "persons",        NULL,  1, 1, RRD_ALGORITHM_ABSOLUTE);
        rrddim_add(stc, "machines",       NULL,  1, 1, RRD_ALGORITHM_ABSOLUTE);
        rrddim_add(stc, "persons_urls",   NULL,  1, 1, RRD_ALGORITHM_ABSOLUTE);
        rrddim_add(stc, "machines_urls",  NULL,  1, 1, RRD_ALGORITHM_ABSOLUTE);
    }

    rrddim_set(stc, "persons",       (collected_number)registry.persons_count);
    rrddim_set(stc, "machines",      (collected_number)registry.machines_count);
    rrddim_set(stc, "persons_urls",  (collected_number)registry.persons_urls_count);
    rrddim_set(stc, "machines_urls", (collected_number)registry.machines_urls_count);
    rrdset_done(stc);

    // ------------------------------------------------------------------------

    if(unlikely(!stm)) {
        stm = rrdset_create_localhost(
                "netdata"
                , "registry_mem"
                , NULL
                , "registry"
                , NULL
                , "Netdata Registry Memory"
                , "KiB"
                , "registry"
                , "stats"
                , 131300
                , localhost->rrd_update_every
                , RRDSET_TYPE_STACKED
        );

        rrddim_add(stm, "persons",        NULL,  1, 1024, RRD_ALGORITHM_ABSOLUTE);
        rrddim_add(stm, "machines",       NULL,  1, 1024, RRD_ALGORITHM_ABSOLUTE);
        rrddim_add(stm, "persons_urls",   NULL,  1, 1024, RRD_ALGORITHM_ABSOLUTE);
        rrddim_add(stm, "machines_urls",  NULL,  1, 1024, RRD_ALGORITHM_ABSOLUTE);
    }

    struct aral_statistics *p_aral_stats = aral_statistics(registry.persons_aral);
    rrddim_set(stm, "persons",       (collected_number)p_aral_stats->structures.allocated_bytes + (collected_number)p_aral_stats->malloc.allocated_bytes + (collected_number)p_aral_stats->mmap.allocated_bytes);

    struct aral_statistics *m_aral_stats = aral_statistics(registry.machines_aral);
    rrddim_set(stm, "machines",      (collected_number)m_aral_stats->structures.allocated_bytes + (collected_number)m_aral_stats->malloc.allocated_bytes + (collected_number)m_aral_stats->mmap.allocated_bytes);

    struct aral_statistics *pu_aral_stats = aral_statistics(registry.person_urls_aral);
    rrddim_set(stm, "persons_urls",  (collected_number)pu_aral_stats->structures.allocated_bytes + (collected_number)pu_aral_stats->malloc.allocated_bytes + (collected_number)pu_aral_stats->mmap.allocated_bytes);

    struct aral_statistics *mu_aral_stats = aral_statistics(registry.machine_urls_aral);
    rrddim_set(stm, "machines_urls", (collected_number)mu_aral_stats->structures.allocated_bytes + (collected_number)mu_aral_stats->malloc.allocated_bytes + (collected_number)mu_aral_stats->mmap.allocated_bytes);

    rrdset_done(stm);
}