Skip to content

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

Merged

Conversation

SaketramDurbha
Copy link
Contributor

  • 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

@SaketramDurbha SaketramDurbha requested a review from a team as a code owner June 3, 2020 21:56
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Jun 3, 2020
@kurtisvg kurtisvg assigned kurtisvg and unassigned busunkim96 Jun 3, 2020
@kurtisvg kurtisvg requested a review from shubha-rajan June 3, 2020 22:00
Comment on lines 102 to 104
db_user = os.environ.get("DB_USER")
db_pass = os.environ.get("DB_PASS")
db_name = os.environ.get("DB_NAME")
Copy link
Contributor

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:

Suggested change
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"]

Copy link
Contributor Author

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(
Copy link
Contributor

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"

Comment on lines 73 to 75
# Extract host and port from db_host socket address
db_hostname = db_host_parts[0]
db_port = int(db_host_parts[1])
Copy link
Contributor

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?

Suggested change
# 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])

@shubha-rajan
Copy link
Contributor

Approved! @SaketramDurbha once you address Kurtis's comments, let me know and I'll merge it

Copy link
Contributor

@kurtisvg kurtisvg left a 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?

@shubha-rajan
Copy link
Contributor

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 DB_HOST to decide between connection methods there

@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 SaketramDurbha changed the title refactor Cloud SQL PostgreSQL & MySQL samples refactor Cloud SQL samples Jun 5, 2020
@shubha-rajan shubha-rajan added the kokoro:run Add this label to force Kokoro to re-run the tests. label Jun 5, 2020
@kurtisvg kurtisvg added kokoro:force-run Add this label to force Kokoro to re-run the tests. and removed kokoro:run Add this label to force Kokoro to re-run the tests. labels Jun 5, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 5, 2020
@shubha-rajan
Copy link
Contributor

@SaketramDurbha looks like the lint check is failing and it has to do with an unused variable.

@googlebot
Copy link

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@googlebot I fixed it... If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added cla: no This human has *not* signed the Contributor License Agreement. and removed cla: yes This human has signed the Contributor License Agreement. labels Jun 5, 2020
Saketram Durbha added 6 commits June 5, 2020 15:28
* 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
@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added cla: yes This human has signed the Contributor License Agreement. and removed cla: no This human has *not* signed the Contributor License Agreement. labels Jun 5, 2020
@shubha-rajan shubha-rajan added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 5, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 5, 2020
@shubha-rajan shubha-rajan merged commit 86080b8 into GoogleCloudPlatform:master Jun 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants