Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22__all__ = ("DB_AUTH_ENVVAR", "DB_AUTH_PATH", "ConnectionStringFactory") 

23 

24from sqlalchemy.engine import url 

25from ._dbAuth import DbAuth, DbAuthNotFoundError 

26 

27DB_AUTH_ENVVAR = "LSST_DB_AUTH" 

28"""Default name of the environmental variable that will be used to locate DB 

29credentials configuration file. """ 

30 

31DB_AUTH_PATH = "~/.lsst/db-auth.yaml" 

32"""Default path at which it is expected that DB credentials are found.""" 

33 

34 

35class ConnectionStringFactory: 

36 """Factory for `sqlalchemy.engine.url.URL` instances. 

37 

38 The factory constructs a connection string URL object by parsing the 

39 connection string, the 'db' key in the registry configuration. 

40 Username, password, host, port or database can be specified as keys in the 

41 config explicitly. If username or password are missing a matching DB is 

42 found in the credentials file pointed to by `DB_AUTH_ENVVAR` or 

43 `DB_AUTH_PATH` values. 

44 """ 

45 

46 keys = ('username', 'password', 'host', 'port', 'database') 

47 

48 @classmethod 

49 def fromConfig(cls, registryConfig): 

50 """Parses the `db`, and, if they exist, username, password, host, port 

51 and database keys from the given config. 

52 

53 If no username and password are found in the connection string, or in 

54 the config, they are retrieved from a file at `DB_AUTH_PATH` or 

55 `DB_AUTH_ENVVAR`. Sqlite dialect does not require a password. 

56 

57 The `db` key value of the given config specifies the default connection 

58 string, such that if no additional connection string parameters are 

59 provided or retrieved, the `db` key is returned unmodified. 

60 

61 Parameters 

62 ---------- 

63 config : `ButlerConfig`, `RegistryConfig`, `Config` or `str` 

64 Registry configuration 

65 

66 Returns 

67 ------- 

68 connectionString : `sqlalchemy.engine.url.URL` 

69 URL object representing the connection string. 

70 

71 Raises 

72 ------ 

73 DbAuthPermissionsError 

74 If the credentials file has incorrect permissions. 

75 DbAuthError 

76 A problem occured when retrieving DB authentication. 

77 

78 Notes 

79 ----- 

80 Matching requires dialect, host and database keys. If dialect is not 

81 specified in the db string and host and database keys are not found in 

82 the db string, or as explicit keys in the config, the matcher returns 

83 an unchanged connection string. 

84 Insufficiently specified connection strings are interpreted as an 

85 indication that a 3rd party authentication mechanism, such as Oracle 

86 Wallet, is being used and therefore are left unmodified. 

87 """ 

88 # this import can not live on the top because of circular import issue 

89 from lsst.daf.butler.registry import RegistryConfig 

90 regConf = RegistryConfig(registryConfig) 

91 conStr = url.make_url(regConf['db']) 

92 

93 for key in cls.keys: 

94 if getattr(conStr, key) is None: 

95 setattr(conStr, key, regConf.get(key)) 

96 

97 # Allow 3rd party authentication mechanisms by assuming connection 

98 # string is correct when we can not recognize (dialect, host, database) 

99 # matching keys. 

100 if any((conStr.drivername is None, 

101 conStr.host is None, 

102 conStr.database is None)): 

103 return conStr 

104 

105 # Ignore when credentials are not set up, or when no matches are found 

106 try: 

107 dbAuth = DbAuth(DB_AUTH_PATH, DB_AUTH_ENVVAR) 

108 auth = dbAuth.getAuth(conStr.drivername, conStr.username, conStr.host, 

109 conStr.port, conStr.database) 

110 except DbAuthNotFoundError: 

111 # credentials file doesn't exist or no matches were found 

112 pass 

113 else: 

114 # only assign auth when *no* errors were raised 

115 conStr.username = auth[0] 

116 conStr.password = auth[1] 

117 

118 return conStr