Parse the pgpass file and return the matching password. :return: Password string, if found, ``None`` otherwise.
(
*, passfile: typing.Optional[pathlib.Path],
hosts: typing.List[str],
ports: typing.List[int],
database: str,
user: str)
| 141 | |
| 142 | |
| 143 | def _read_password_from_pgpass( |
| 144 | *, passfile: typing.Optional[pathlib.Path], |
| 145 | hosts: typing.List[str], |
| 146 | ports: typing.List[int], |
| 147 | database: str, |
| 148 | user: str): |
| 149 | """Parse the pgpass file and return the matching password. |
| 150 | |
| 151 | :return: |
| 152 | Password string, if found, ``None`` otherwise. |
| 153 | """ |
| 154 | |
| 155 | passtab = _read_password_file(passfile) |
| 156 | if not passtab: |
| 157 | return None |
| 158 | |
| 159 | for host, port in zip(hosts, ports): |
| 160 | if host.startswith('/'): |
| 161 | # Unix sockets get normalized into 'localhost' |
| 162 | host = 'localhost' |
| 163 | |
| 164 | for phost, pport, pdatabase, puser, ppassword in passtab: |
| 165 | if phost != '*' and phost != host: |
| 166 | continue |
| 167 | if pport != '*' and pport != str(port): |
| 168 | continue |
| 169 | if pdatabase != '*' and pdatabase != database: |
| 170 | continue |
| 171 | if puser != '*' and puser != user: |
| 172 | continue |
| 173 | |
| 174 | # Found a match. |
| 175 | return ppassword |
| 176 | |
| 177 | return None |
| 178 | |
| 179 | |
| 180 | def _validate_port_spec(hosts, port): |
no test coverage detected