Commit b60d83bf authored by Thomas Jürges's avatar Thomas Jürges

ROHD-2105: Use Python's configparser to extract credentials

parent 6076a841
......@@ -25,7 +25,8 @@
__version__ = "1.5"
import datetime
from os.path import expanduser
import configparser
from os.path import expanduser, exists
# Python2/3 dependent stuff
......@@ -38,32 +39,51 @@ else:
import xmlrpclib
string_types = basestring
def parse_config_file(file):
""" Parse an ini file that shall contain user credentials
for the LOFAR stager API.
"""
if exists(file) is False:
# File not found, nothing to do.
raise Exception
with open(file, 'r') as stream:
print("%s - stager_access: Parsing user credentials found in file \"%s\"." % (datetime.datetime.now(), file))
config = configparser.ConfigParser()
# This may seem odd but the Python configparser cannot parse ini
# files that just contain "A = B" lines without a section name
# like "[Foo Bar]". So I just add a section name just before the
# config file is read.
config.read_string("[LOFAR stager credentials]\n" + stream.read())
return config["LOFAR stager credentials"]
#---
# Determine credentials and create proxy
user = None
passw = None
try:
f = expanduser("~/.awe/Environment.cfg")
with open(f,'r') as file:
print("%s - stager_access: Parsing user credentials from \"%s\"" % (datetime.datetime.now(), f))
for line in file:
if line.startswith("database_user"):
user = line.split(':')[1].strip()
if line.startswith("database_password"):
passw = line.split(':')[1].strip()
except IOError:
f = expanduser("~/.stagingrc")
with open(f,'r') as file:
print("%s - stager_access: Parsing user credentials from \"%s\"" % (datetime.datetime.now(), f))
for line in file:
if line.startswith("user"):
user = line.split('=')[1].strip()
if line.startswith("password"):
passw = line.split('=', 1)[1].strip()
print("%s - stager_access: Creating proxy" % (datetime.datetime.now()))
proxy = xmlrpclib.ServerProxy("https://"+user+':'+passw+"@webportal.astron.nl/service-public/xmlrpc")
LtaStager = proxy.LtaStager
# Iterate over possible files that could contain user credentials.
for f in ["~/.awe/Environment.cfg", "~/.stagingrc"]:
file = expanduser(f)
credentials = []
try:
credentials = parse_config_file(file)
except Exception:
print("%s - stager_access: Could not find user credential file \"%s\"." % (datetime.datetime.now(), file))
continue
user = credentials["user"]
passw = credentials["password"]
break
if user is not None and passw is not None:
print("%s - stager_access: Creating proxy" % (datetime.datetime.now()))
proxy = xmlrpclib.ServerProxy("https://"+user+':'+passw+"@webportal.astron.nl/service-public/xmlrpc")
LtaStager = proxy.LtaStager
else:
print("%s - stager_access: Could not find a file with user credentials." % (datetime.datetime.now()))
# ---
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment