-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now the ssl_context connection parameter can have one of four values: None - The default, meaning it'll try and connect over SSL but fall back to a plain socket if not. True - Will try and connect over SSL and fail if not. False - It'll not try to connect over SSL. SSLContext object - It'll use this object to connect over SSL.
- Loading branch information
Showing
6 changed files
with
164 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,22 +1,23 @@ | ||
import ssl | ||
|
||
import pytest | ||
|
||
from pg8000 import DatabaseError, connect | ||
|
||
# This requires a line in pg_hba.conf that requires scram-sha-256 for the | ||
# database scram-sha-256 | ||
# database pg8000_scram-sha-256 | ||
|
||
DB = "pg8000_scram_sha_256" | ||
|
||
|
||
@pytest.fixture | ||
def setup(con): | ||
try: | ||
con.run(f"CREATE DATABASE {DB}") | ||
except DatabaseError: | ||
pass | ||
|
||
def test_scram_sha_256_plus(db_kwargs): | ||
context = ssl.create_default_context() | ||
context.check_hostname = False | ||
context.verify_mode = ssl.CERT_NONE | ||
|
||
db_kwargs["ssl_context"] = context | ||
db_kwargs["database"] = "pg8000_scram_sha_256" | ||
def test_scram_sha_256_plus(setup, db_kwargs): | ||
db_kwargs["database"] = DB | ||
|
||
# Should only raise an exception saying db doesn't exist | ||
with pytest.raises(DatabaseError, match="3D000"): | ||
with connect(**db_kwargs) as con: | ||
con.close() | ||
with connect(**db_kwargs): | ||
pass |
This file contains 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 |
---|---|---|
@@ -1,14 +1,45 @@ | ||
import pytest | ||
|
||
from pg8000.native import Connection, DatabaseError | ||
from pg8000.native import Connection, DatabaseError, InterfaceError | ||
|
||
# This requires a line in pg_hba.conf that requires scram-sha-256 for the | ||
# database scram_sha_256 | ||
|
||
DB = "pg8000_scram_sha_256" | ||
|
||
def test_scram_sha_256(db_kwargs): | ||
db_kwargs["database"] = "pg8000_scram_sha_256" | ||
|
||
# Should only raise an exception saying db doesn't exist | ||
with pytest.raises(DatabaseError, match="3D000"): | ||
Connection(**db_kwargs) | ||
@pytest.fixture | ||
def setup(con): | ||
try: | ||
con.run(f"CREATE DATABASE {DB}") | ||
except DatabaseError: | ||
pass | ||
con.run("ALTER SYSTEM SET ssl = off") | ||
con.run("SELECT pg_reload_conf()") | ||
yield | ||
con.run("ALTER SYSTEM SET ssl = on") | ||
con.run("SELECT pg_reload_conf()") | ||
|
||
|
||
def test_scram_sha_256(setup, db_kwargs): | ||
db_kwargs["database"] = DB | ||
|
||
with Connection(**db_kwargs): | ||
pass | ||
|
||
|
||
def test_scram_sha_256_ssl_False(setup, db_kwargs): | ||
db_kwargs["database"] = DB | ||
db_kwargs["ssl_context"] = False | ||
|
||
with Connection(**db_kwargs): | ||
pass | ||
|
||
|
||
def test_scram_sha_256_ssl_True(setup, db_kwargs): | ||
db_kwargs["database"] = DB | ||
db_kwargs["ssl_context"] = True | ||
|
||
with pytest.raises(InterfaceError, match="Server refuses SSL"): | ||
with Connection(**db_kwargs): | ||
pass |
This file contains 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 |
---|---|---|
@@ -1,21 +1,53 @@ | ||
import ssl | ||
from ssl import CERT_NONE, SSLSocket, create_default_context | ||
|
||
import pytest | ||
|
||
from pg8000.native import Connection, DatabaseError | ||
|
||
# This requires a line in pg_hba.conf that requires scram-sha-256 for the | ||
# database scram_sha_256 | ||
# database pg8000_scram_sha_256 | ||
|
||
DB = "pg8000_scram_sha_256" | ||
|
||
def test_scram_sha_256_plus(db_kwargs): | ||
context = ssl.create_default_context() | ||
|
||
@pytest.fixture | ||
def setup(con): | ||
try: | ||
con.run(f"CREATE DATABASE {DB}") | ||
except DatabaseError: | ||
pass | ||
|
||
|
||
def test_scram_sha_256_plus(setup, db_kwargs): | ||
db_kwargs["database"] = DB | ||
|
||
with Connection(**db_kwargs) as con: | ||
assert isinstance(con._usock, SSLSocket) | ||
|
||
|
||
def test_scram_sha_256_plus_ssl_True(setup, db_kwargs): | ||
db_kwargs["ssl_context"] = True | ||
db_kwargs["database"] = DB | ||
|
||
with Connection(**db_kwargs) as con: | ||
assert isinstance(con._usock, SSLSocket) | ||
|
||
|
||
def test_scram_sha_256_plus_ssl_custom(setup, db_kwargs): | ||
context = create_default_context() | ||
context.check_hostname = False | ||
context.verify_mode = ssl.CERT_NONE | ||
context.verify_mode = CERT_NONE | ||
|
||
db_kwargs["ssl_context"] = context | ||
db_kwargs["database"] = "pg8000_scram_sha_256" | ||
db_kwargs["database"] = DB | ||
|
||
with Connection(**db_kwargs) as con: | ||
assert isinstance(con._usock, SSLSocket) | ||
|
||
|
||
def test_scram_sha_256_plus_ssl_False(setup, db_kwargs): | ||
db_kwargs["ssl_context"] = False | ||
db_kwargs["database"] = DB | ||
|
||
# Should only raise an exception saying db doesn't exist | ||
with pytest.raises(DatabaseError, match="3D000"): | ||
Connection(**db_kwargs) | ||
with Connection(**db_kwargs) as con: | ||
assert not isinstance(con._usock, SSLSocket) |