Integrate Google Cloud Secret Manager with Django


To integrate Google Cloud Secret Manager with Django, follow these steps:

1. Set Up Google Cloud Secret Manager

  1. Enable Secret Manager API:

  2. Create a Secret:

    • In the Secret Manager section, click on Create Secret.
    • Give it a name (e.g., my_database_password) and input the secret value (e.g., an API key, database password, etc.).
    • Click Create.
  3. Set Permissions:

    • Make sure your service account (used by your Django app) has the Secret Manager Secret Accessor role, which allows it to access the secrets.

2. Install Google Cloud SDK and Required Libraries

You need to install the Google Cloud SDK and libraries for Python.

pip install google-cloud-secret-manager

3. Configure Authentication

Ensure that your Django application has access to Google Cloud. You can authenticate using a service account key.

  1. Create a service account:

    • Go to the IAM & Admin section.
    • Create a service account with the Secret Manager Secret Accessor role.
    • Download the private key in JSON format.
  2. Set up authentication in your Django app:

    • Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of your downloaded service account key JSON file.
    export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-file.json"

4. Access Secrets in Django

You can now access secrets from Google Cloud Secret Manager in your Django app.

  1. Create a function to retrieve secrets:
from google.cloud import secretmanager
import json

def get_secret(secret_name):
    # Initialize the Secret Manager client
    client = secretmanager.SecretManagerServiceClient()
    
    # Construct the resource name of the secret
    project_id = "your-gcp-project-id"
    secret_version_name = f"projects/{project_id}/secrets/{secret_name}/versions/latest"
    
    # Access the secret version
    response = client.access_secret_version(name=secret_version_name)
    
    # Extract the secret payload
    secret_payload = response.payload.data.decode("UTF-8")
    
    return secret_payload
  1. Integrate the function into your Django settings:

In settings.py, you can use the get_secret function to retrieve sensitive information such as API keys, database credentials, etc.

import os
from myapp.utils import get_secret

# Get the database password from Secret Manager
DB_PASSWORD = get_secret('my_database_password')

# Now use this in your DATABASES configuration
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'myuser',
        'PASSWORD': DB_PASSWORD,
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

5. Use Secret Data in Your Django App

Now you can retrieve and use secrets from Google Cloud Secret Manager anywhere in your Django app.

6. Deploy to Google Cloud (Optional)

If you're deploying your Django app to Google Cloud (e.g., on Google App Engine or Google Kubernetes Engine), ensure that the environment variables and permissions are properly set for your app to access the Google Cloud Secret Manager.


This process helps you securely manage secrets, keeping sensitive data out of your codebase and environment variables.

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete