Skip to content

Commit 8540237

Browse files
authored
Add SQL Server to Cloud SQL Cloud Run quickstart. (GoogleCloudPlatform#7379)
* Add SQL Server to Cloud SQL Cloud Run quickstart. * Lint.
1 parent 966b217 commit 8540237

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

cloud-sql/sql-server/sqlalchemy/Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616
# https://hub.docker.com/_/python
1717
FROM python:3.10-buster
1818

19+
RUN apt-get update
20+
RUN apt install unixodbc-dev -y
21+
22+
# Add SQL Server ODBC Driver 17 for Ubuntu 18.04
23+
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
24+
RUN curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
25+
RUN apt-get update
26+
RUN ACCEPT_EULA=Y apt-get install -y --allow-unauthenticated msodbcsql17
27+
RUN ACCEPT_EULA=Y apt-get install -y --allow-unauthenticated mssql-tools
28+
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
29+
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
30+
1931
# Copy application dependency manifests to the container image.
2032
# Copying this separately prevents re-running pip install on every code change.
2133
COPY requirements.txt ./
@@ -29,6 +41,12 @@ ENV APP_HOME /app
2941
WORKDIR $APP_HOME
3042
COPY . ./
3143

44+
# Copy any certificates if present.
45+
COPY ./certs /app/certs
46+
47+
# Use server certificate for encrypted connection.
48+
COPY ./certs/server-ca.pem /usr/local/share/ca-certificates/server-ca.crt
49+
RUN update-ca-certificates
3250

3351
# Run the web service on container startup. Here we use the gunicorn
3452
# webserver, with one worker process and 8 threads.

cloud-sql/sql-server/sqlalchemy/certs/.gitkeep

Whitespace-only changes.

cloud-sql/sql-server/sqlalchemy/main.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,53 @@ def init_connection_engine():
6666
# [END cloud_sql_server_sqlalchemy_lifetime]
6767
}
6868

69+
if os.environ.get("DB_ROOT_CERT"):
70+
return init_tcp_sslcerts_connection_engine(db_config)
6971
return init_tcp_connection_engine(db_config)
7072

7173

74+
def init_tcp_sslcerts_connection_engine(db_config):
75+
# [START cloud_sql_postgres_sqlalchemy_create_tcp_sslcerts]
76+
# Remember - storing secrets in plaintext is potentially unsafe. Consider using
77+
# something like https://cloud.google.com/secret-manager/docs/overview to help keep
78+
# secrets secret.
79+
db_user = os.environ["DB_USER"]
80+
db_pass = os.environ["DB_PASS"]
81+
db_name = os.environ["DB_NAME"]
82+
db_host = os.environ["DB_HOST"]
83+
84+
# Extract port from db_host if present,
85+
# otherwise use DB_PORT environment variable.
86+
host_args = db_host.split(":")
87+
if len(host_args) == 1:
88+
db_hostname = host_args[0]
89+
db_port = int(os.environ["DB_PORT"])
90+
elif len(host_args) == 2:
91+
db_hostname, db_port = host_args[0], int(host_args[1])
92+
93+
pool = sqlalchemy.create_engine(
94+
# Equivalent URL:
95+
# mssql+pyodbc://<db_user>:<db_pass>@/<host>:<port>/<db_name>?driver=ODBC+Driver+17+for+SQL+Server
96+
sqlalchemy.engine.url.URL.create(
97+
"mssql+pyodbc",
98+
username=db_user,
99+
password=db_pass,
100+
database=db_name,
101+
host=db_hostname,
102+
port=db_port,
103+
query={
104+
"driver": "ODBC Driver 17 for SQL Server",
105+
"Encrypt": "yes",
106+
"Trusted_Connection": "no"
107+
},
108+
),
109+
**db_config
110+
)
111+
# [END cloud_sql_postgres_sqlalchemy_create_tcp_sslcerts]
112+
pool.dialect.description_encoding = None
113+
return pool
114+
115+
72116
def init_tcp_connection_engine(db_config):
73117
# [START cloud_sql_server_sqlalchemy_create_tcp]
74118
# [START cloud_sql_sqlserver_sqlalchemy_create_tcp]

cloud-sql/sql-server/sqlalchemy/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ Flask==2.0.2
22
SQLAlchemy==1.4.27
33
python-tds==1.11.0
44
sqlalchemy-pytds==0.3.4
5+
pyodbc==4.0.32
56
pyopenssl==21.0.0
67

7-

0 commit comments

Comments
 (0)