Skip to content

Commit 57a04f9

Browse files
committed
fix: ES connection using certificates
1 parent c52d9fd commit 57a04f9

File tree

1 file changed

+71
-66
lines changed

1 file changed

+71
-66
lines changed

src/DIRAC/ConfigurationSystem/Client/Utilities.py

Lines changed: 71 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -450,72 +450,7 @@ def getElasticDBParameters(fullname):
450450
cs_path = getDatabaseSection(fullname)
451451
parameters = {}
452452

453-
# Check mandatory parameters first: Password, User
454-
result = gConfig.getOption(cs_path + "/Password")
455-
if not result["OK"]:
456-
# No individual password found, try at the common place
457-
result = gConfig.getOption("/Systems/NoSQLDatabases/Password")
458-
if not result["OK"]:
459-
return S_ERROR("Failed to get the configuration parameter: Password.")
460-
dbPass = result["Value"]
461-
parameters["Password"] = dbPass
462-
463-
result = gConfig.getOption(cs_path + "/User")
464-
if not result["OK"]:
465-
# No individual user name found, try at the common place
466-
result = gConfig.getOption("/Systems/NoSQLDatabases/User")
467-
if not result["OK"]:
468-
return S_ERROR("Failed to get the configuration parameter: User.")
469-
dbUser = result["Value"]
470-
parameters["User"] = dbUser
471-
472-
# Check optional parameters: Host, Port, SSL, CRT, ca_certs, client_key, client_cert
473-
result = gConfig.getOption(cs_path + "/Host")
474-
if not result["OK"]:
475-
# No host name found, try at the common place
476-
result = gConfig.getOption("/Systems/NoSQLDatabases/Host")
477-
if not result["OK"]:
478-
gLogger.warn("Failed to get the configuration parameter: Host. Using localhost")
479-
dbHost = "localhost"
480-
else:
481-
dbHost = result["Value"]
482-
else:
483-
dbHost = result["Value"]
484-
# Check if the host is the local one and then set it to 'localhost' to use
485-
# a socket connection
486-
if dbHost != "localhost":
487-
localHostName = socket.getfqdn()
488-
if localHostName == dbHost:
489-
dbHost = "localhost"
490-
parameters["Host"] = dbHost
491-
492-
# Elasticsearch standard port
493-
result = gConfig.getOption(cs_path + "/Port")
494-
if not result["OK"]:
495-
# No individual port number found, try at the common place
496-
result = gConfig.getOption("/Systems/NoSQLDatabases/Port")
497-
if not result["OK"]:
498-
gLogger.debug("No configuration parameter set for Port, assuming URL points to right location")
499-
dbPort = None
500-
else:
501-
dbPort = int(result["Value"])
502-
else:
503-
dbPort = int(result["Value"])
504-
parameters["Port"] = dbPort
505-
506-
result = gConfig.getOption(cs_path + "/SSL")
507-
if not result["OK"]:
508-
# No SSL option found, try at the common place
509-
result = gConfig.getOption("/Systems/NoSQLDatabases/SSL")
510-
if not result["OK"]:
511-
gLogger.debug("Failed to get the configuration parameter: SSL. Assuming SSL is needed")
512-
ssl = True
513-
else:
514-
ssl = False if result["Value"].lower() in ("false", "no", "n") else True
515-
else:
516-
ssl = False if result["Value"].lower() in ("false", "no", "n") else True
517-
parameters["SSL"] = ssl
518-
453+
# Check if connection is through certificates and get certificate parameters
519454
# Elasticsearch use certs
520455
result = gConfig.getOption(cs_path + "/CRT")
521456
if not result["OK"]:
@@ -572,6 +507,76 @@ def getElasticDBParameters(fullname):
572507
client_cert = result["Value"]
573508
parameters["client_cert"] = client_cert
574509

510+
# If connection is not through certificates get: Password, User
511+
if parameters["CRT"]:
512+
parameters["Password"] = None
513+
parameters["User"] = None
514+
else:
515+
result = gConfig.getOption(cs_path + "/Password")
516+
if not result["OK"]:
517+
# No individual password found, try at the common place
518+
result = gConfig.getOption("/Systems/NoSQLDatabases/Password")
519+
if not result["OK"]:
520+
return S_ERROR("Failed to get the configuration parameter: Password.")
521+
dbPass = result["Value"]
522+
parameters["Password"] = dbPass
523+
524+
result = gConfig.getOption(cs_path + "/User")
525+
if not result["OK"]:
526+
# No individual user name found, try at the common place
527+
result = gConfig.getOption("/Systems/NoSQLDatabases/User")
528+
if not result["OK"]:
529+
return S_ERROR("Failed to get the configuration parameter: User.")
530+
dbUser = result["Value"]
531+
parameters["User"] = dbUser
532+
533+
# Check optional parameters: Host, Port, SSL
534+
result = gConfig.getOption(cs_path + "/Host")
535+
if not result["OK"]:
536+
# No host name found, try at the common place
537+
result = gConfig.getOption("/Systems/NoSQLDatabases/Host")
538+
if not result["OK"]:
539+
gLogger.warn("Failed to get the configuration parameter: Host. Using localhost")
540+
dbHost = "localhost"
541+
else:
542+
dbHost = result["Value"]
543+
else:
544+
dbHost = result["Value"]
545+
# Check if the host is the local one and then set it to 'localhost' to use
546+
# a socket connection
547+
if dbHost != "localhost":
548+
localHostName = socket.getfqdn()
549+
if localHostName == dbHost:
550+
dbHost = "localhost"
551+
parameters["Host"] = dbHost
552+
553+
# Elasticsearch standard port
554+
result = gConfig.getOption(cs_path + "/Port")
555+
if not result["OK"]:
556+
# No individual port number found, try at the common place
557+
result = gConfig.getOption("/Systems/NoSQLDatabases/Port")
558+
if not result["OK"]:
559+
gLogger.debug("No configuration parameter set for Port, assuming URL points to right location")
560+
dbPort = None
561+
else:
562+
dbPort = int(result["Value"])
563+
else:
564+
dbPort = int(result["Value"])
565+
parameters["Port"] = dbPort
566+
567+
result = gConfig.getOption(cs_path + "/SSL")
568+
if not result["OK"]:
569+
# No SSL option found, try at the common place
570+
result = gConfig.getOption("/Systems/NoSQLDatabases/SSL")
571+
if not result["OK"]:
572+
gLogger.debug("Failed to get the configuration parameter: SSL. Assuming SSL is needed")
573+
ssl = True
574+
else:
575+
ssl = False if result["Value"].lower() in ("false", "no", "n") else True
576+
else:
577+
ssl = False if result["Value"].lower() in ("false", "no", "n") else True
578+
parameters["SSL"] = ssl
579+
575580
return S_OK(parameters)
576581

577582

0 commit comments

Comments
 (0)