CMS 3D CMS Logo

credentials.py
Go to the documentation of this file.
1 import netrc
2 import os
3 import logging
4 
5 netrcFileName = '.netrc'
6 defAuthPathEnvVar = 'HOME'
7 authPathEnvVar = 'COND_AUTH_PATH'
8 
9 dbkey_filename = 'db.key'
10 dbkey_folder = os.path.join('.cms_cond',dbkey_filename)
11 
12 reader_role = 'reader'
13 writer_role = 'writer'
14 admin_role = 'admin'
15 
16 def netrc_machine( service, role ):
17  return '%s@%s' %(role,service)
18 
19 def get_credentials_from_file( machine, authPath ):
20  authFile = netrcFileName
21  if not authPath is None:
22  authFile = os.path.join( authPath, authFile )
23  creds = netrc.netrc( authFile ).authenticators(machine)
24  return creds
25 
26 def get_credentials( machine, authPath=None ):
27  if authPath is None:
28  if authPathEnvVar in os.environ:
29  authPath = os.environ[authPathEnvVar]
30  else:
31  if defAuthPathEnvVar in os.environ:
32  authPath = os.environ[defAuthPathEnvVar]
33  else:
34  authPath = ''
35  return get_credentials_from_file( machine, authPath )
36 
37 def get_credentials_for_schema( service, schema, role, authPath=None ):
38  if authPath is None:
39  if authPathEnvVar in os.environ:
40  authPath = os.environ[authPathEnvVar]
41  else:
42  if defAuthPathEnvVar in os.environ:
43  authPath = os.environ[defAuthPathEnvVar]
44  else:
45  authPath = ''
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) )
53  import netrc
54  params = netrc.netrc( authFile ).authenticators(machine)
55  if params is None:
56  msg = 'The required credentials have not been found in the .netrc file.'
57  raise Exception(msg)
58  return params
59  else:
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)
68 
credentials.get_credentials_for_schema
def get_credentials_for_schema(service, schema, role, authPath=None)
Definition: credentials.py:37
credentials.get_credentials_from_file
def get_credentials_from_file(machine, authPath)
Definition: credentials.py:19
credentials.get_credentials
def get_credentials(machine, authPath=None)
Definition: credentials.py:26
credentials.netrc_machine
def netrc_machine(service, role)
Definition: credentials.py:16
Exception