Skip to content

Commit 66e4e2e

Browse files
authored
feat: support Active Directory auth for Windows (#131)
* feat: support Active Directory auth for Windows * rename integrated_security to active_directory_auth * Update README.md
1 parent 1bc2ee4 commit 66e4e2e

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,32 @@ connector.connect(
127127
enable_iam_auth=True,
128128
)
129129
```
130+
131+
### SQL Server Active Directory Authentication
132+
Active Directory authentication for SQL Server instances is currently only supported on Windows. First, make sure to follow [these steps](https://cloud.google.com/blog/topics/developers-practitioners/creating-sql-server-instance-integrated-active-directory-using-google-cloud-sql) to set up a Managed AD domain and join your Cloud SQL instance to the domain. [See here for more info on Cloud SQL Active Directory integration](https://cloud.google.com/sql/docs/sqlserver/ad).
133+
134+
Once you have followed the steps linked above, you can run the following code to return a connection object:
135+
```python
136+
connector.connect(
137+
"project:region:instance",
138+
"pytds",
139+
db="my_database",
140+
active_directory_auth=True,
141+
server_name="public.[instance].[location].[project].cloudsql.[domain]",
142+
)
143+
```
144+
Or, if using Private IP:
145+
```python
146+
connector.connect(
147+
"project:region:instance",
148+
"pytds",
149+
db="my_database",
150+
active_directory_auth=True,
151+
server_name="private.[instance].[location].[project].cloudsql.[domain]",
152+
ip_types=IPTypes.PRIVATE
153+
)
154+
```
155+
130156
### Setup for development
131157

132158
Tests can be run with `nox`. Change directory into the `cloud-sql-python-connector` and just run `nox` to run the tests.

google/cloud/sql/connector/instance_connection_manager.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from google.auth.credentials import Credentials
3030
import google.auth.transport.requests
3131
import OpenSSL
32+
import platform
3233
import ssl
3334
import socket
3435
from tempfile import TemporaryDirectory
@@ -108,6 +109,15 @@ def __init__(self, *args: Any) -> None:
108109
super(CloudSQLIPTypeError, self).__init__(self, *args)
109110

110111

112+
class PlatformNotSupportedError(Exception):
113+
"""
114+
Raised when a feature is not supported on the current platform.
115+
"""
116+
117+
def __init__(self, *args: Any) -> None:
118+
super(PlatformNotSupportedError, self).__init__(self, *args)
119+
120+
111121
class InstanceMetadata:
112122
ip_addrs: Dict[str, Any]
113123
context: ssl.SSLContext
@@ -564,15 +574,31 @@ def _connect_with_pytds(
564574
raise ImportError(
565575
'Unable to import module "pytds." Please install and try again.'
566576
)
567-
user = kwargs.pop("user")
568-
db = kwargs.pop("db")
569-
passwd = kwargs.pop("password")
577+
578+
db = kwargs.pop("db", None)
570579

571580
# Create socket and wrap with context.
572581
sock = ctx.wrap_socket(
573582
socket.create_connection((ip_address, SERVER_PROXY_PORT)),
574583
server_hostname=ip_address,
575584
)
585+
if kwargs.pop("active_directory_auth", False):
586+
if platform.system() == "Windows":
587+
# Ignore username and password if using active directory auth
588+
server_name = kwargs.pop("server_name")
589+
return pytds.connect(
590+
database=db,
591+
auth=pytds.login.SspiAuth(port=1433, server_name=server_name),
592+
sock=sock,
593+
**kwargs,
594+
)
595+
else:
596+
raise PlatformNotSupportedError(
597+
"Active Directory authentication is currently only supported on Windows."
598+
)
599+
600+
user = kwargs.pop("user")
601+
passwd = kwargs.pop("password")
576602
return pytds.connect(
577603
ip_address, database=db, user=user, password=passwd, sock=sock, **kwargs
578604
)

0 commit comments

Comments
 (0)