Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 464
Implement connection service file functionality#1223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
elprans
merged 11 commits into
MagicStack:master
from
AndrewJackson2020:connection_service_fileOct 11, 2025
+274
−9
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
31c2d43
Implement connection service file functionality
1f87efd
Update asyncpg/connect_utils.py
AndrewJackson2020 b1a723b
Cosolidate if statements
1c337cd
Fix formatting
8558a46
Move con service file parse below query
b6fb1e4
Add tests
93347b0
Fix format
c02781c
Update asyncpg/connect_utils.py
AndrewJackson2020 fc21240
fix ssl handling
c99ad22
fix format
ae843a2
Add servicefile as parameter
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,6 +7,7 @@ | ||
| from __future__ import annotations | ||
| import asyncio | ||
| import configparser | ||
| import collections | ||
| from collections.abc import Callable | ||
| import enum | ||
| @@ -87,6 +88,9 @@ class SSLNegotiation(compat.StrEnum): | ||
| PGPASSFILE = '.pgpass' | ||
| PG_SERVICEFILE = '.pg_service.conf' | ||
| def _read_password_file(passfile: pathlib.Path) \ | ||
| -> typing.List[typing.Tuple[str, ...]]: | ||
| @@ -269,6 +273,7 @@ def _dot_postgresql_path(filename) -> typing.Optional[pathlib.Path]: | ||
| def _parse_connect_dsn_and_args(*, dsn, host, port, user, | ||
| password, passfile, database, ssl, | ||
| service, servicefile, | ||
| direct_tls, server_settings, | ||
| target_session_attrs, krbsrvname, gsslib): | ||
| # `auth_hosts` is the version of host information for the purposes | ||
| @@ -281,6 +286,32 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user, | ||
| if dsn: | ||
| parsed = urllib.parse.urlparse(dsn) | ||
| query = None | ||
| if parsed.query: | ||
| query = urllib.parse.parse_qs(parsed.query, strict_parsing=True) | ||
| for key, val in query.items(): | ||
| if isinstance(val, list): | ||
| query[key] = val[-1] | ||
| if 'service' in query: | ||
| val = query.pop('service') | ||
| if not service and val: | ||
| service = val | ||
| connection_service_file = servicefile | ||
| if connection_service_file is None: | ||
| connection_service_file = os.getenv('PGSERVICEFILE') | ||
| if connection_service_file is None: | ||
| homedir = compat.get_pg_home_directory() | ||
| if homedir: | ||
| connection_service_file = homedir / PG_SERVICEFILE | ||
| else: | ||
| connection_service_file = None | ||
| else: | ||
| connection_service_file = pathlib.Path(connection_service_file) | ||
| if parsed.scheme not in {'postgresql', 'postgres'}: | ||
| raise exceptions.ClientConfigurationError( | ||
| 'invalid DSN: scheme is expected to be either ' | ||
| @@ -315,11 +346,7 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user, | ||
| if password is None and dsn_password: | ||
| password = urllib.parse.unquote(dsn_password) | ||
| if parsed.query: | ||
| query = urllib.parse.parse_qs(parsed.query, strict_parsing=True) | ||
| for key, val in query.items(): | ||
| if isinstance(val, list): | ||
| query[key] = val[-1] | ||
| if query: | ||
| if 'port' in query: | ||
| val = query.pop('port') | ||
| @@ -406,12 +433,124 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user, | ||
| if gsslib is None: | ||
| gsslib = val | ||
| if 'service' in query: | ||
| val = query.pop('service') | ||
AndrewJackson2020 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if service is None: | ||
| service = val | ||
| if query: | ||
| if server_settings is None: | ||
| server_settings = query | ||
| else: | ||
| server_settings = {**query, **server_settings} | ||
| if connection_service_file is not None and service is not None: | ||
| pg_service = configparser.ConfigParser() | ||
| pg_service.read(connection_service_file) | ||
| if service in pg_service.sections(): | ||
| service_params = pg_service[service] | ||
| if 'port' in service_params: | ||
| val = service_params.pop('port') | ||
| if not port and val: | ||
| port = [int(p) for p in val.split(',')] | ||
| if 'host' in service_params: | ||
| val = service_params.pop('host') | ||
| if not host and val: | ||
| host, port = _parse_hostlist(val, port) | ||
| if 'dbname' in service_params: | ||
| val = service_params.pop('dbname') | ||
| if database is None: | ||
| database = val | ||
| if 'database' in service_params: | ||
| val = service_params.pop('database') | ||
| if database is None: | ||
| database = val | ||
| if 'user' in service_params: | ||
| val = service_params.pop('user') | ||
| if user is None: | ||
| user = val | ||
| if 'password' in service_params: | ||
| val = service_params.pop('password') | ||
| if password is None: | ||
| password = val | ||
| if 'passfile' in service_params: | ||
| val = service_params.pop('passfile') | ||
| if passfile is None: | ||
| passfile = val | ||
| if 'sslmode' in service_params: | ||
| val = service_params.pop('sslmode') | ||
| if ssl is None: | ||
| ssl = val | ||
| if 'sslcert' in service_params: | ||
AndrewJackson2020 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| val = service_params.pop('sslcert') | ||
| if sslcert is None: | ||
| sslcert = val | ||
| if 'sslkey' in service_params: | ||
| val = service_params.pop('sslkey') | ||
| if sslkey is None: | ||
| sslkey = val | ||
| if 'sslrootcert' in service_params: | ||
| val = service_params.pop('sslrootcert') | ||
| if sslrootcert is None: | ||
| sslrootcert = val | ||
| if 'sslnegotiation' in service_params: | ||
| val = service_params.pop('sslnegotiation') | ||
| if sslnegotiation is None: | ||
| sslnegotiation = val | ||
| if 'sslcrl' in service_params: | ||
| val = service_params.pop('sslcrl') | ||
| if sslcrl is None: | ||
| sslcrl = val | ||
| if 'sslpassword' in service_params: | ||
| val = service_params.pop('sslpassword') | ||
| if sslpassword is None: | ||
| sslpassword = val | ||
| if 'ssl_min_protocol_version' in service_params: | ||
| val = service_params.pop( | ||
| 'ssl_min_protocol_version' | ||
| ) | ||
| if ssl_min_protocol_version is None: | ||
| ssl_min_protocol_version = val | ||
| if 'ssl_max_protocol_version' in service_params: | ||
| val = service_params.pop( | ||
| 'ssl_max_protocol_version' | ||
| ) | ||
| if ssl_max_protocol_version is None: | ||
| ssl_max_protocol_version = val | ||
| if 'target_session_attrs' in service_params: | ||
| dsn_target_session_attrs = service_params.pop( | ||
| 'target_session_attrs' | ||
| ) | ||
| if target_session_attrs is None: | ||
| target_session_attrs = dsn_target_session_attrs | ||
| if 'krbsrvname' in service_params: | ||
| val = service_params.pop('krbsrvname') | ||
| if krbsrvname is None: | ||
| krbsrvname = val | ||
| if 'gsslib' in service_params: | ||
| val = service_params.pop('gsslib') | ||
| if gsslib is None: | ||
| gsslib = val | ||
| if not service: | ||
| service = os.environ.get('PGSERVICE') | ||
| if not host: | ||
| hostspec = os.environ.get('PGHOST') | ||
| if hostspec: | ||
| @@ -724,7 +863,8 @@ def _parse_connect_arguments(*, dsn, host, port, user, password, passfile, | ||
| max_cached_statement_lifetime, | ||
| max_cacheable_statement_size, | ||
| ssl, direct_tls, server_settings, | ||
| target_session_attrs, krbsrvname, gsslib): | ||
| target_session_attrs, krbsrvname, gsslib, | ||
| service, servicefile): | ||
| local_vars = locals() | ||
| for var_name in {'max_cacheable_statement_size', | ||
| 'max_cached_statement_lifetime', | ||
| @@ -754,7 +894,8 @@ def _parse_connect_arguments(*, dsn, host, port, user, password, passfile, | ||
| direct_tls=direct_tls, database=database, | ||
| server_settings=server_settings, | ||
| target_session_attrs=target_session_attrs, | ||
| krbsrvname=krbsrvname, gsslib=gsslib) | ||
| krbsrvname=krbsrvname, gsslib=gsslib, | ||
| service=service, servicefile=servicefile) | ||
| config = _ClientConfiguration( | ||
| command_timeout=command_timeout, | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2074,6 +2074,8 @@ async def _do_execute( | ||
| async def connect(dsn=None, *, | ||
| host=None, port=None, | ||
| user=None, password=None, passfile=None, | ||
| service=None, | ||
AndrewJackson2020 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| servicefile=None, | ||
| database=None, | ||
| loop=None, | ||
| timeout=60, | ||
| @@ -2183,6 +2185,14 @@ async def connect(dsn=None, *, | ||
| (defaults to ``~/.pgpass``, or ``%APPDATA%\postgresql\pgpass.conf`` | ||
| on Windows). | ||
| :param service: | ||
| The name of the postgres connection service stored in the postgres | ||
| connection service file. | ||
| :param servicefile: | ||
| The location of the connnection service file used to store | ||
| connection parameters. | ||
| :param loop: | ||
| An asyncio event loop instance. If ``None``, the default | ||
| event loop will be used. | ||
| @@ -2395,6 +2405,9 @@ async def connect(dsn=None, *, | ||
| .. versionchanged:: 0.30.0 | ||
| Added the *krbsrvname* and *gsslib* parameters. | ||
| .. versionchanged:: 0.31.0 | ||
| Added the *servicefile* and *service* parameters. | ||
| .. _SSLContext: https://docs.python.org/3/library/ssl.html#ssl.SSLContext | ||
| .. _create_default_context: | ||
| https://docs.python.org/3/library/ssl.html#ssl.create_default_context | ||
| @@ -2428,6 +2441,8 @@ async def connect(dsn=None, *, | ||
| user=user, | ||
| password=password, | ||
| passfile=passfile, | ||
| service=service, | ||
| servicefile=servicefile, | ||
| ssl=ssl, | ||
| direct_tls=direct_tls, | ||
| database=database, | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.