top of page

Deploying Python Flask app with API keys

May 13, 2024

3 min read

0

4

0

Exposing API keys when deploying on the cloud can lead to various security risks and consequences:

  1. Unauthorized Access to sensitive data, systems, or functionalities.

  2. Exposed API keys can be used maliciously to extract or modify sensitive data, leading to data breaches.

  3. Exposed API keys can be used to incur financial charges or perform actions that result in financial loss.

  4. Security incidents resulting from exposed API keys can damage an organization’s reputation and erode trust with customers or users.

  5. Organizations may face legal and compliance issues, violations of contractual obligations.


Google cloud services offers a service ‘Google Cloud Secret Manager’ to manage secrets in your applications. This offers several advantages:

  1. Security: Secret Manager provides a secure and centralized way to store sensitive information such as API keys, database credentials, and other secrets.

  2. Storing secrets separately from your application’s source code reduces the risk of accidental exposure.

  3. Secret Manager provides a user-friendly interface for managing secrets, including versioning and rotation.

  4. Secret Manager seamlessly integrates with other Google Cloud services, allows you to securely access secrets from your applications running on these platforms without having to manage credentials or configuration files manually.

Overall, using Secret Manager helps enhance the security posture of your applications by reducing the risk of unauthorized access to sensitive information and simplifying the management of secrets throughout their lifecycle.

To integrate Google Cloud Secret Manager with a Python application running on Google Cloud Run, you can follow these steps:

  1. Create a new Google project


    New Project — Google Cloud console. The project name/ ID is the globally unique identifier for your project that cannot be changed

  2. Start console https://console.cloud.google.com/

  3. Activate Cloud Shell

  4. Create directory for your source code.

  5. Click on Open Editor

  6. Drag and drop your source code from the parent directory, along with requirements.txt

  7. In console.google.com, Dashboard, type in search “Secret manager”

  8. Enable the API for your project

  9. Create secret keys for all your APIs and enter the values or upload json files.

  10. Grant Permissions: Ensure that the service account used by your Cloud Run service has the appropriate permissions to access the secret. You can do this by granting the roles/secretmanager.secretAccessor role to the service account.

  11. Go to the IAM page in the Google Cloud console.

  12. Go to IAM

  13. Click the Project selector list at the top of the page.

  14. In the Select from dialog that appears, select the organization for which you want to enable Secret Manager.

  15. On the IAM page, next to your username, click Edit.

  16. In the Edit permissions panel that appears, add the necessary roles.

  17. Click Add another role. Select a role to add, such as Secret Manager Secret Accessor.

  18. To add more roles, repeat the previous step. Click Save.




Access Secrets in your Python Code:

  1. Add google-cloud-secret-manager to requirements.txt

  2. In your python code: Add imports

    from google.cloud import secretmanager

  3. Sample code using the keys

def get_secret(secret_name):

# Create the Secret Manager client

client = secretmanager.SecretManagerServiceClient()

# Build the secret name

secret_path = f”projects/{your-project-id}/secrets/{your-secret-id}/versions/latest”

# Access the secret

response = client.access_secret_version(name=secret_path)

# Return the secret value

return response.payload.data.decode(“UTF-8”)

secret_value = get_secret(“your-secret-id”)

-Replace “your-project-id” and “your-secret-id” with your actual project ID and secret ID.

-Deploy Your Cloud Run Service: Deploy your Python application to Google Cloud Run making sure to grant the Cloud Run service account the necessary permissions to access Secret Manager.

May 13, 2024

3 min read

0

4

0

Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page