5 netrcFileName =
'.netrc'
6 defAuthPathEnvVar =
'HOME'
7 authPathEnvVar =
'COND_AUTH_PATH'
9 dbkey_filename =
'db.key'
10 dbkey_folder = os.path.join(
'.cms_cond',dbkey_filename)
12 reader_role =
'reader'
13 writer_role =
'writer'
17 return '%s@%s' %(role,service)
20 authFile = netrcFileName
21 if not authPath
is None:
22 authFile = os.path.join( authPath, authFile )
23 creds = netrc.netrc( authFile ).authenticators(machine)
28 if authPathEnvVar
in os.environ:
29 authPath = os.environ[authPathEnvVar]
31 if defAuthPathEnvVar
in os.environ:
32 authPath = os.environ[defAuthPathEnvVar]
39 if authPathEnvVar
in os.environ:
40 authPath = os.environ[authPathEnvVar]
42 if defAuthPathEnvVar
in os.environ:
43 authPath = os.environ[defAuthPathEnvVar]
46 dbkey_path = os.path.join(authPath,dbkey_folder)
47 if not os.path.exists(dbkey_path):
48 authFile = os.path.join(authPath,
'.netrc')
49 if not os.path.exists(authFile):
50 raise Exception(
"Can't get db credentials, since neither db key nor Netrc file have been found.")
51 machine =
'%s@%s.%s' %(role,schema.lower(),service)
52 logging.debug(
'Looking up db credentials %s in file %s ' %(machine,authFile) )
54 params = netrc.netrc( authFile ).authenticators(machine)
56 msg =
'The required credentials have not been found in the .netrc file.'
60 import libCondDBPyBind11Interface
as credential_db
61 roles_map = { reader_role: credential_db.reader_role, writer_role: credential_db.writer_role, admin_role: credential_db.admin_role }
62 connection_string =
'oracle://%s/%s'%(service.lower(),schema.upper())
63 logging.debug(
'Looking up db credentials for %s in credential store' %connection_string )
64 (dbuser,username,password) = credential_db.get_credentials_from_db(connection_string,roles_map[role],authPath)
65 if username==
'' or password==
'':
66 raise Exception(
'No credentials found to connect on %s with the required access role.'%connection_string)
67 return (username,dbuser,password)