e7a346
From c55511be71a6181788067fa018b5f0deaca10e61 Mon Sep 17 00:00:00 2001
e7a346
From: Aravinda VK <avishwan@redhat.com>
e7a346
Date: Thu, 28 Dec 2017 14:04:50 +0530
e7a346
Subject: [PATCH 115/128] eventsapi: JWT signing without external dependency
e7a346
e7a346
Added support for JWT signing without using python-jwt since it is not
e7a346
available in all the distributions.
e7a346
e7a346
>upstream mainline patch : https://review.gluster.org/19102
e7a346
e7a346
BUG: 1466129
e7a346
Change-Id: I95699055442fbf9da15249f5defe8a8b287010f1
e7a346
Signed-off-by: Aravinda VK <avishwan@redhat.com>
e7a346
Reviewed-on: https://code.engineering.redhat.com/gerrit/126619
e7a346
Tested-by: RHGS Build Bot <nigelb@redhat.com>
e7a346
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
e7a346
---
e7a346
 events/src/utils.py | 20 +++++++++++++++++---
e7a346
 glusterfs.spec.in   |  4 ++--
e7a346
 2 files changed, 19 insertions(+), 5 deletions(-)
e7a346
e7a346
diff --git a/events/src/utils.py b/events/src/utils.py
e7a346
index 5130720..f24d64d 100644
e7a346
--- a/events/src/utils.py
e7a346
+++ b/events/src/utils.py
e7a346
@@ -18,6 +18,10 @@ from threading import Thread
e7a346
 import multiprocessing
e7a346
 from Queue import Queue
e7a346
 from datetime import datetime, timedelta
e7a346
+import base64
e7a346
+import hmac
e7a346
+from hashlib import sha256
e7a346
+from calendar import timegm
e7a346
 
e7a346
 from eventsapiconf import (LOG_FILE,
e7a346
                            WEBHOOKS_FILE,
e7a346
@@ -184,15 +188,25 @@ def autoload_webhooks():
e7a346
             load_webhooks()
e7a346
 
e7a346
 
e7a346
+def base64_urlencode(inp):
e7a346
+    return base64.urlsafe_b64encode(inp).replace("=", "").strip()
e7a346
+
e7a346
+
e7a346
 def get_jwt_token(secret, event_type, event_ts, jwt_expiry_time_seconds=60):
e7a346
-    import jwt
e7a346
+    exp = datetime.utcnow() + timedelta(seconds=jwt_expiry_time_seconds)
e7a346
     payload = {
e7a346
-        "exp": datetime.utcnow() + timedelta(seconds=jwt_expiry_time_seconds),
e7a346
+        "exp": timegm(exp.utctimetuple()),
e7a346
         "iss": "gluster",
e7a346
         "sub": event_type,
e7a346
         "iat": event_ts
e7a346
     }
e7a346
-    return jwt.encode(payload, secret, algorithm='HS256')
e7a346
+    header = '{"alg":"HS256","typ":"JWT"}'
e7a346
+    payload = json.dumps(payload, separators=(',', ':'), sort_keys=True)
e7a346
+    msg = base64_urlencode(header) + "." + base64_urlencode(payload)
e7a346
+    return "%s.%s" % (
e7a346
+        msg,
e7a346
+        base64_urlencode(hmac.HMAC(secret, msg, sha256).digest())
e7a346
+    )
e7a346
 
e7a346
 
e7a346
 def publish_to_webhook(url, token, secret, message_queue):
e7a346
diff --git a/glusterfs.spec.in b/glusterfs.spec.in
e7a346
index 29329fa..56a62a9 100644
e7a346
--- a/glusterfs.spec.in
e7a346
+++ b/glusterfs.spec.in
e7a346
@@ -671,9 +671,9 @@ Requires:         %{name}-server%{?_isa} = %{version}-%{release}
e7a346
 Requires:         python2 python-prettytable
e7a346
 Requires:         python2-gluster = %{version}-%{release}
e7a346
 %if ( 0%{?rhel} )
e7a346
-Requires:         python-requests python-jwt
e7a346
+Requires:         python-requests
e7a346
 %else
e7a346
-Requires:         python2-requests python2-jwt
e7a346
+Requires:         python2-requests
e7a346
 %endif
e7a346
 %if ( 0%{?rhel} && 0%{?rhel} < 7 )
e7a346
 Requires:         python-argparse
e7a346
-- 
e7a346
1.8.3.1
e7a346