summaryrefslogtreecommitdiffstats
path: root/events/src/utils.py
blob: 7d9b7b51d955e1dd86ba35852d3a60f48e9b1006 (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
# -*- coding: utf-8 -*-
#
#  Copyright (c) 2016 Red Hat, Inc. <http://www.redhat.com>
#  This file is part of GlusterFS.
#
#  This file is licensed to you under your choice of the GNU Lesser
#  General Public License, version 3 or any later version (LGPLv3 or
#  later), or the GNU General Public License, version 2 (GPLv2), in all
#  cases as published by the Free Software Foundation.
#

import json
import os
import logging
import fcntl
from errno import EBADF
from threading import Thread
import multiprocessing
from Queue import Queue
from datetime import datetime, timedelta
import base64
import hmac
from hashlib import sha256
from calendar import timegm

from eventsapiconf import (LOG_FILE,
                           WEBHOOKS_FILE,
                           DEFAULT_CONFIG_FILE,
                           CUSTOM_CONFIG_FILE,
                           UUID_FILE,
                           CERTS_DIR)
import eventtypes


# Webhooks list
_webhooks = {}
_webhooks_file_mtime = 0
# Default Log Level
_log_level = "INFO"
# Config Object
_config = {}

# Init Logger instance
logger = logging.getLogger(__name__)
NodeID = None
webhooks_pool = None


def boolify(value):
    value = str(value)
    if value.lower() in ["1", "on", "true", "yes"]:
        return True
    else:
        return False


def log_event(data):
    # Log all published events unless it is disabled
    if not _config.get("disable-events-log", False):
        logger.info(repr(data))


def get_node_uuid():
    val = None
    with open(UUID_FILE) as f:
        for line in f:
            if line.startswith("UUID="):
                val = line.strip().split("=")[-1]
                break
    return val


def get_config(key, default_value=None):
    if not _config:
        load_config()
    return _config.get(key, default_value)


def get_event_type_name(idx):
    """
    Returns Event Type text from the index. For example, VOLUME_CREATE
    """
    return eventtypes.all_events[idx].replace("EVENT_", "")


def setup_logger():
    """
    Logging initialization, Log level by default will be INFO, once config
    file is read, respective log_level will be set.
    """
    global logger
    logger.setLevel(logging.INFO)

    # create the logging file handler
    fh = logging.FileHandler(LOG_FILE)

    formatter = logging.Formatter("[%(asctime)s] %(levelname)s "
                                  "[%(module)s - %(lineno)s:%(funcName)s] "
                                  "- %(message)s")

    fh.setFormatter(formatter)

    # add handler to logger object
    logger.addHandler(fh)


def load_config():
    """
    Load/Reload the config from REST Config files. This function will
    be triggered during init and when SIGUSR2.
    """
    global _config
    _config = {}
    if os.path.exists(DEFAULT_CONFIG_FILE):
        _config = json.load(open(DEFAULT_CONFIG_FILE))
    if os.path.exists(CUSTOM_CONFIG_FILE):
        _config.update(json.load(open(CUSTOM_CONFIG_FILE)))


def load_log_level():
    """
    Reads log_level from Config file and sets accordingly. This function will
    be triggered during init and when SIGUSR2.
    """
    global logger, _log_level
    new_log_level = _config.get("log-level", "INFO")
    if _log_level != new_log_level:
        logger.setLevel(getattr(logging, new_log_level.upper()))
        _log_level = new_log_level.upper()


def load_webhooks():
    """
    Load/Reload the webhooks list. This function will
    be triggered during init and when SIGUSR2.
    """
    global _webhooks, _webhooks_file_mtime
    _webhooks = {}
    if os.path.exists(WEBHOOKS_FILE):
        _webhooks = json.load(open(WEBHOOKS_FILE))
        st = os.lstat(WEBHOOKS_FILE)
        _webhooks_file_mtime = st.st_mtime


def load_all():
    """
    Wrapper function to call all load/reload functions. This function will
    be triggered during init and when SIGUSR2.
    """
    load_config()
    load_webhooks()
    load_log_level()


def publish(ts, event_key, data):
    global NodeID
    if NodeID is None:
        NodeID = get_node_uuid()

    autoload_webhooks()

    message = {
        "nodeid": NodeID,
        "ts": int(ts),
        "event": get_event_type_name(event_key),
        "message": data
    }

    log_event(message)

    if _webhooks:
        plugin_webhook(message)
    else:
        # TODO: Default action?
        pass


def autoload_webhooks():
    global _webhooks_file_mtime
    try:
        st = os.lstat(WEBHOOKS_FILE)
    except OSError:
        st = None

    if st is not None:
        # If Stat is available and mtime is not matching with
        # previously recorded mtime, reload the webhooks file
        if st.st_mtime != _webhooks_file_mtime:
            load_webhooks()


def base64_urlencode(inp):
    return base64.urlsafe_b64encode(inp).replace("=", "").strip()


def get_jwt_token(secret, event_type, event_ts, jwt_expiry_time_seconds=60):
    exp = datetime.utcnow() + timedelta(seconds=jwt_expiry_time_seconds)
    payload = {
        "exp": timegm(exp.utctimetuple()),
        "iss": "gluster",
        "sub": event_type,
        "iat": event_ts
    }
    header = '{"alg":"HS256","typ":"JWT"}'
    payload = json.dumps(payload, separators=(',', ':'), sort_keys=True)
    msg = base64_urlencode(header) + "." + base64_urlencode(payload)
    return "%s.%s" % (
        msg,
        base64_urlencode(hmac.HMAC(str(secret), msg, sha256).digest())
    )


def save_https_cert(domain, port, cert_path):
    import ssl

    # Cert file already available for this URL
    if os.path.exists(cert_path):
        return

    cert_data = ssl.get_server_certificate((domain, port))
    with open(cert_path, "w") as f:
        f.write(cert_data)


def publish_to_webhook(url, token, secret, message_queue):
    # Import requests here since not used in any other place
    import requests

    http_headers = {"Content-Type": "application/json"}
    urldata = requests.utils.urlparse(url)
    parts = urldata.netloc.split(":")
    domain = parts[0]
    # Default https port if not specified
    port = 443
    if len(parts) == 2:
        port = int(parts[1])

    cert_path = os.path.join(CERTS_DIR, url.replace("/", "_").strip())

    while True:
        hashval = ""
        event_type, event_ts, message_json = message_queue.get()
        if token != "" and token is not None:
            hashval = token

        if secret != "" and secret is not None:
            hashval = get_jwt_token(secret, event_type, event_ts)

        if hashval:
            http_headers["Authorization"] = "Bearer " + hashval

        verify = True
        while True:
            try:
                resp = requests.post(url, headers=http_headers,
                                     data=message_json,
                                     verify=verify)
                # Successful webhook push
                message_queue.task_done()
                if resp.status_code != 200:
                    logger.warn("Event push failed to URL: {url}, "
                                "Event: {event}, "
                                "Status Code: {status_code}".format(
                                    url=url,
                                    event=message_json,
                                    status_code=resp.status_code))
                break
            except requests.exceptions.SSLError as e:
                # If verify is equal to cert path, but still failed with
                # SSLError, Looks like some issue with custom downloaded
                # certificate, Try with verify = false
                if verify == cert_path:
                    logger.warn("Event push failed with certificate, "
                                "ignoring verification url={0} "
                                "Error={1}".format(url, e))
                    verify = False
                    continue

                # If verify is instance of bool and True, then custom cert
                # is required, download the cert and retry
                try:
                    save_https_cert(domain, port, cert_path)
                    verify = cert_path
                except Exception as ex:
                    verify = False
                    logger.warn("Unable to get Server certificate, "
                                "ignoring verification url={0} "
                                "Error={1}".format(url, ex))

                # Done with collecting cert, continue
                continue
            except Exception as e:
                logger.warn("Event push failed to URL: {url}, "
                            "Event: {event}, "
                            "Status: {error}".format(
                                url=url,
                                event=message_json,
                                error=e))
                message_queue.task_done()
                break


def plugin_webhook(message):
    message_json = json.dumps(message, sort_keys=True)
    logger.debug("EVENT: {0}".format(message_json))
    webhooks_pool.send(message["event"], message["ts"], message_json)


class LockedOpen(object):

    def __init__(self, filename, *args, **kwargs):
        self.filename = filename
        self.open_args = args
        self.open_kwargs = kwargs
        self.fileobj = None

    def __enter__(self):
        """
        If two processes compete to update a file, The first process
        gets the lock and the second process is blocked in the fcntl.flock()
        call. When first process replaces the file and releases the lock,
        the already open file descriptor in the second process now points
        to a  "ghost" file(not reachable by any path name) with old contents.
        To avoid that conflict, check the fd already opened is same or
        not. Open new one if not same
        """
        f = open(self.filename, *self.open_args, **self.open_kwargs)
        while True:
            fcntl.flock(f, fcntl.LOCK_EX)
            fnew = open(self.filename, *self.open_args, **self.open_kwargs)
            if os.path.sameopenfile(f.fileno(), fnew.fileno()):
                fnew.close()
                break
            else:
                f.close()
                f = fnew
        self.fileobj = f
        return f

    def __exit__(self, _exc_type, _exc_value, _traceback):
        self.fileobj.close()


class PidFileLockFailed(Exception):
    pass


class PidFile(object):
    def __init__(self, filename):
        self.filename = filename
        self.pid = os.getpid()
        self.fh = None

    def cleanup(self, remove_file=True):
        try:
            if self.fh is not None:
                self.fh.close()
        except IOError as exc:
            if exc.errno != EBADF:
                raise
        finally:
            if os.path.isfile(self.filename) and remove_file:
                os.remove(self.filename)

    def __enter__(self):
        self.fh = open(self.filename, 'a+')
        try:
            fcntl.flock(self.fh.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
        except IOError as exc:
            self.cleanup(remove_file=False)
            raise PidFileLockFailed(exc)

        self.fh.seek(0)
        self.fh.truncate()
        self.fh.write("%d\n" % self.pid)
        self.fh.flush()
        self.fh.seek(0)
        return self

    def __exit__(self, _exc_type, _exc_value, _traceback):
        self.cleanup()


def webhook_monitor(proc_queue, webhooks):
    queues = {}
    for url, data in webhooks.items():
        if isinstance(data, str) or isinstance(data, unicode):
            token = data
            secret = None
        else:
            token = data["token"]
            secret = data["secret"]

        queues[url] = Queue()
        t = Thread(target=publish_to_webhook, args=(url, token, secret,
                                                    queues[url]))
        t.start()

    # Get the message sent to Process queue and distribute to all thread queues
    while True:
        message = proc_queue.get()
        for _, q in queues.items():
            q.put(message)


class WebhookThreadPool(object):
    def start(self):
        # Seperate process to emit messages to webhooks
        # which maintains one thread per webhook. Seperate
        # process is required since on reload we need to stop
        # and start the thread pool. In Python Threads can't be stopped
        # so terminate the process and start again. Note: In transit
        # events will be missed during reload
        self.queue = multiprocessing.Queue()
        self.proc = multiprocessing.Process(target=webhook_monitor,
                                            args=(self.queue, _webhooks))
        self.proc.start()

    def restart(self):
        # In transit messages are skipped, since webhooks monitor process
        # is terminated.
        self.proc.terminate()
        self.start()

    def send(self, event_type, event_ts, message):
        self.queue.put((event_type, event_ts, message))


def init_webhook_pool():
    global webhooks_pool
    webhooks_pool = WebhookThreadPool()
    webhooks_pool.start()


def restart_webhook_pool():
    global webhooks_pool
    if webhooks_pool is not None:
        webhooks_pool.restart()