Skip to content

Commit

Permalink
chore: use socket for pg8000 (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon authored Nov 17, 2023
1 parent 2ec5a2d commit e844465
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions google/cloud/alloydb/connector/pg8000.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import socket
import ssl
from typing import Any, TYPE_CHECKING

Expand Down Expand Up @@ -45,16 +45,19 @@ def connect(
raise ImportError(
'Unable to import module "pg8000." Please install and try again.'
)
# Create socket and wrap with context.
sock = ctx.wrap_socket(
socket.create_connection((ip_address, SERVER_PROXY_PORT)),
server_hostname=ip_address,
)

user = kwargs.pop("user")
db = kwargs.pop("db")
passwd = kwargs.pop("password")
setattr(ctx, "request_ssl", False)
return pg8000.dbapi.connect(
user,
database=db,
password=passwd,
host=ip_address,
port=SERVER_PROXY_PORT,
ssl_context=ctx,
sock=sock,
**kwargs,
)
2 changes: 1 addition & 1 deletion tests/unit/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_connect(credentials: FakeCredentials) -> None:
with Connector(credentials) as connector:
connector._client = client
# patch db connection creation
with patch("pg8000.dbapi.connect") as mock_connect:
with patch("google.cloud.alloydb.connector.pg8000.connect") as mock_connect:
mock_connect.return_value = True
connection = connector.connect(
"projects/test-project/locations/test-region/clusters/test-cluster/instances/test-instance",
Expand Down

0 comments on commit e844465

Please sign in to comment.