Skip to content
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

Feature: adding optional sslmode parameter to postgres connection #2154

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
### Features
- Add a "docs" field to models, with a "show" subfield ([#1671](https://github.com/fishtown-analytics/dbt/issues/1671), [#2107](https://github.com/fishtown-analytics/dbt/pull/2107))
- Add a dbt-{dbt_version} user agent field to the bigquery connector ([#2121](https://github.com/fishtown-analytics/dbt/issues/2121), [#2146](https://github.com/fishtown-analytics/dbt/pull/2146))

- Adding optional "sslmode" parameter for postgres ([#2152](https://github.com/fishtown-analytics/dbt/issues/2152))
0xdholleran marked this conversation as resolved.
Show resolved Hide resolved

### Fixes
- Fix issue where dbt did not give an error in the presence of duplicate doc names ([#2054](https://github.com/fishtown-analytics/dbt/issues/2054), [#2080](https://github.com/fishtown-analytics/dbt/pull/2080))
- Include vars provided to the cli method when running the actual method ([#2092](https://github.com/fishtown-analytics/dbt/issues/2092), [#2104](https://github.com/fishtown-analytics/dbt/pull/2104))
Expand Down
6 changes: 5 additions & 1 deletion plugins/postgres/dbt/adapters/postgres/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class PostgresCredentials(Credentials):
password: str # on postgres the password is mandatory
search_path: Optional[str] = None
keepalives_idle: int = 0 # 0 means to use the default value
sslmode: Optional[str] = None

_ALIASES = {
'dbname': 'database',
Expand All @@ -33,7 +34,7 @@ def type(self):

def _connection_keys(self):
return ('host', 'port', 'user', 'database', 'schema', 'search_path',
'keepalives_idle')
'keepalives_idle', 'sslmode')


class PostgresConnectionManager(SQLConnectionManager):
Expand Down Expand Up @@ -89,6 +90,9 @@ def open(cls, connection):
kwargs['options'] = '-c search_path={}'.format(
search_path.replace(' ', '\\ '))

if credentials.sslmode:
kwargs['sslmode'] = credentials.sslmode
0xdholleran marked this conversation as resolved.
Show resolved Hide resolved

try:
handle = psycopg2.connect(
dbname=credentials.database,
Expand Down
17 changes: 17 additions & 0 deletions test/unit/test_postgres_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def test_changed_keepalive(self, psycopg2):
connect_timeout=10,
keepalives_idle=256)


@mock.patch('dbt.adapters.postgres.connections.psycopg2')
def test_role(self, psycopg2):
self.config.credentials = self.config.credentials.replace(role='somerole')
Expand All @@ -159,6 +160,22 @@ def test_search_path(self, psycopg2):
connect_timeout=10,
options="-c search_path=test")

@mock.patch('dbt.adapters.postgres.connections.psycopg2')
def test_sslmode(self, psycopg2):
self.config.credentials = self.config.credentials.replace(sslmode="require")
connection = self.adapter.acquire_connection('dummy')

psycopg2.connect.assert_not_called()
connection.handle
psycopg2.connect.assert_called_once_with(
dbname='postgres',
user='root',
host='thishostshouldnotexist',
password='password',
port=5432,
connect_timeout=10,
sslmode="require")

@mock.patch('dbt.adapters.postgres.connections.psycopg2')
def test_schema_with_space(self, psycopg2):
self.config.credentials = self.config.credentials.replace(search_path="test test")
Expand Down