From 4787c5f083d9f39ad7bee9b2c09509e73338f9b5 Mon Sep 17 00:00:00 2001
From: Manuela Kuhn <manuela.kuhn@desy.de>
Date: Wed, 27 Apr 2016 14:09:37 +0200
Subject: [PATCH] Enable multple host in whitelist of DataReceiver

---
 APIs/dataTransferAPI.py      | 21 ++++++++++++++++++---
 conf/dataReceiver.conf       |  3 +--
 src/receiver/DataReceiver.py |  5 ++++-
 test/zmq_test/auth_server.py |  7 ++++---
 4 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/APIs/dataTransferAPI.py b/APIs/dataTransferAPI.py
index 737649b4..a89ca71c 100644
--- a/APIs/dataTransferAPI.py
+++ b/APIs/dataTransferAPI.py
@@ -264,9 +264,24 @@ class dataTransfer():
 
         # Receive data only from whitelisted nodes
         if whitelist:
-            self.auth = ThreadAuthenticator(self.context)
-            self.auth.start()
-            self.auth.allow(whitelist)
+            if type(whitelist) == list:
+                self.auth = ThreadAuthenticator(self.context)
+                self.auth.start()
+                for host in whitelist:
+                    try:
+                        if host == "localhost":
+                            ip = [socket.gethostbyname(host)]
+                        else:
+                            hostname, tmp, ip = socket.gethostbyaddr(host)
+
+                        self.log.debug("Allowing host " + host + " (" + str(ip[0]) + ")")
+                        self.auth.allow(ip[0])
+                    except:
+                        self.log.error("Error was: ", exc_info=True)
+                        raise AuthenticationFailed("Could not get IP of host " + host)
+            else:
+                raise FormatError("Whitelist has to be a list of IPs")
+
 
         socketIdToConnect = self.streamStarted or self.queryNextStarted
 
diff --git a/conf/dataReceiver.conf b/conf/dataReceiver.conf
index fa94d291..01f9d04d 100644
--- a/conf/dataReceiver.conf
+++ b/conf/dataReceiver.conf
@@ -17,8 +17,7 @@ logfileSize       = 104857600 ; #100 MB
 #
 # List of hosts allowed to receive data from
 
-whitelist         = "131.169.185.121"
-#whitelist         = "localhost" "zitpcx19282.desy.de" "zitpcx22614" "lsdma-lab04"
+whitelist         = ["localhost", "zitpcx19282.desy.de", "zitpcx22614", "lsdma-lab04"]
 
 # Where incoming data will be stored to
 targetDir         = /space/projects/zeromq-data-transfer/data/zmq_target
diff --git a/src/receiver/DataReceiver.py b/src/receiver/DataReceiver.py
index bb9386d1..75a78d9b 100644
--- a/src/receiver/DataReceiver.py
+++ b/src/receiver/DataReceiver.py
@@ -6,6 +6,7 @@ import argparse
 import logging
 import os
 import ConfigParser
+import json
 
 
 BASE_PATH   = os.path.dirname ( os.path.dirname ( os.path.dirname ( os.path.realpath ( __file__ ) )))
@@ -37,7 +38,7 @@ def argumentParsing():
     logfileName    = config.get('asection', 'logfileName')
     logfileSize    = config.get('asection', 'logfileSize')
 
-    whitelist      = config.get('asection', 'whitelist')
+    whitelist      = json.loads(config.get('asection', 'whitelist'))
 
     targetDir      = config.get('asection', 'targetDir')
 
@@ -115,6 +116,8 @@ class DataReceiver:
 
         self.whitelist    = arguments.whitelist
 
+        self.log.info("Configured whitelist: " + str(self.whitelist))
+
         self.targetDir    = os.path.normpath(arguments.targetDir)
         self.dataIp       = arguments.dataStreamIp
         self.dataPort     = arguments.dataStreamPort
diff --git a/test/zmq_test/auth_server.py b/test/zmq_test/auth_server.py
index f2fe5815..d156c8a2 100644
--- a/test/zmq_test/auth_server.py
+++ b/test/zmq_test/auth_server.py
@@ -13,10 +13,11 @@ socket.zap_domain = b'global'
 socket.bind("tcp://" + ip + ":%s" % port)
 
 auth = ThreadAuthenticator(context)
-#whitelist = "131.169.185.121"
-whitelist = "131.169.185.34"
 auth.start()
-auth.allow(whitelist)
+
+whitelist = ["131.169.185.34", "131.169.185.121"]
+for host in whitelist:
+    auth.allow(host)
 
 
 while True:
-- 
GitLab