-
Notifications
You must be signed in to change notification settings - Fork 6.6k
refactor Cloud SQL samples #3952
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
refactor Cloud SQL samples #3952
Conversation
SaketramDurbha
commented
Jun 3, 2020
- Rename environment variable DB_SOCKET_PATH to DB_SOCKET_DIR for better description of its contents
- Move region tags and refactor code so as to give users a better sample experience
6ddf64b
to
783505e
Compare
783505e
to
055a202
Compare
cloud-sql/mysql/sqlalchemy/main.py
Outdated
db_user = os.environ.get("DB_USER") | ||
db_pass = os.environ.get("DB_PASS") | ||
db_name = os.environ.get("DB_NAME") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make these required, since the application will fail without them:
db_user = os.environ.get("DB_USER") | |
db_pass = os.environ.get("DB_PASS") | |
db_name = os.environ.get("DB_NAME") | |
db_user = os.environ["DB_USER"] | |
db_pass = os.environ["DB_PASS"] | |
db_name = os.environ["DB_NAME"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I'm making all the env vars except the "DB_SOCKET_DIR" required. Including "DB_HOST" for TCP connections and "CLOUD_SQL_CONNECTION_NAME" for Unix connections.
db_socket_dir = os.environ.get("DB_SOCKET_DIR", "/cloudsql") | ||
cloud_sql_connection_name = os.environ.get("CLOUD_SQL_CONNECTION_NAME") | ||
|
||
pool = sqlalchemy.create_engine( | ||
# Equivalent URL: | ||
# mysql+pymysql://<db_user>:<db_pass>@/<db_name>?unix_socket=<socket_path>/<cloud_sql_instance_name> | ||
sqlalchemy.engine.url.URL( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add init examples next to these values?
username=db_user, # e.g. "my-database-user"
cloud-sql/mysql/sqlalchemy/main.py
Outdated
# Extract host and port from db_host socket address | ||
db_hostname = db_host_parts[0] | ||
db_port = int(db_host_parts[1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we condense this a bit?
# Extract host and port from db_host socket address | |
db_hostname = db_host_parts[0] | |
db_port = int(db_host_parts[1]) | |
# Extract host and port from db_host socket address | |
host_args = db_host.split(":") | |
db_hostname, db_port = host_args[0], int(host_args[1]) |
Approved! @SaketramDurbha once you address Kurtis's comments, let me know and I'll merge it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additional thought - do we need to update sqlserver as well?
sqlserver has a hard-coded string for the host. We might want to change it to an env var for consistency's sake but I think the hard coded version might be cleaner. We're not using the presence of |
cloud-sql/mysql/sqlalchemy/README.md
Outdated
@@ -52,6 +41,8 @@ export DB_USER='<DB_USER_NAME>' | |||
export DB_PASS='<DB_PASSWORD>' | |||
export DB_NAME='<DB_NAME>' | |||
``` | |||
Note: Saving credentials in environment variables is convenient, but not secure - consider a more | |||
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe. | |
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to help keep secrets safe. |
cloud-sql/mysql/sqlalchemy/README.md
Outdated
@@ -67,6 +58,8 @@ $env:DB_USER="<DB_USER_NAME>" | |||
$env:DB_PASS="<DB_PASSWORD>" | |||
$env:DB_NAME="<DB_NAME>" | |||
``` | |||
Note: Saving credentials in environment variables is convenient, but not secure - consider a more | |||
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe. | |
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to help keep secrets safe. |
cloud-sql/mysql/sqlalchemy/README.md
Outdated
@@ -98,10 +91,12 @@ export DB_USER='<DB_USER_NAME>' | |||
export DB_PASS='<DB_PASSWORD>' | |||
export DB_NAME='<DB_NAME>' | |||
``` | |||
Note: Saving credentials in environment variables is convenient, but not secure - consider a more | |||
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe. | |
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to help keep secrets safe. |
@@ -51,6 +40,8 @@ export DB_USER='<DB_USER_NAME>' | |||
export DB_PASS='<DB_PASSWORD>' | |||
export DB_NAME='<DB_NAME>' | |||
``` | |||
Note: Saving credentials in environment variables is convenient, but not secure - consider a more | |||
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe. | |
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to help keep secrets safe. |
@@ -66,6 +57,8 @@ $env:DB_USER="<DB_USER_NAME>" | |||
$env:DB_PASS="<DB_PASSWORD>" | |||
$env:DB_NAME="<DB_NAME>" | |||
``` | |||
Note: Saving credentials in environment variables is convenient, but not secure - consider a more | |||
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe. | |
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to help keep secrets safe. |
@@ -96,10 +89,12 @@ export DB_USER='<DB_USER_NAME>' | |||
export DB_PASS='<DB_PASSWORD>' | |||
export DB_NAME='<DB_NAME>' | |||
``` | |||
Note: Saving credentials in environment variables is convenient, but not secure - consider a more | |||
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe. | |
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to help keep secrets safe. |
$env:DB_HOST="127.0.0.1:1433" | ||
``` | ||
Note: Saving credentials in environment variables is convenient, but not secure - consider a more | ||
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep secrets safe. | |
secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to help keep secrets safe. |
@SaketramDurbha looks like the lint check is failing and it has to do with an unused variable. |
We found a Contributor License Agreement for you (the sender of this pull request), but were unable to determine that you authored the commits in this PR. Maybe you used a different email address in the git commits than was used to sign the CLA? If someone else authored these commits, then please add them to this pull request and have them confirm that they're okay with them being contributed to Google. If there are co-authors, make sure they're formatted properly. In order to pass this check, please resolve this problem and then comment ℹ️ Googlers: Go here for more info. |
* Rename environment variable DB_SOCKET_PATH to DB_SOCKET_DIR for better description of its contents * Move region tags and refactor code so as to give users a better sample experience
… MySQL samples * make necessary environment variables such as "DB_USER" required * add comments to show examples for some input arguments * rewrite small piece of code to be more condensed and concise
* use environment varialbe for proxy connection hostname over hardcoding it * make all environment variables required * include instructions in README for running locally on Windows * make code structure consistent with PostgreSQL and MySQL samples
…for consistency across Cloud SQL samples
6a2d27e
to
734c833
Compare
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |