Created
April 7, 2025 19:12
-
-
Save rustyvz/af9f93ea5a43de69b50d8ad4813f7797 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Google Secrets Manager Setup Guide | |
| **Foreword to define** | |
| # 1. Enable the Secret Manager API | |
| ## 1.1. Web Console | |
| ### 1.1.1. Access the Google Cloud Console | |
| 1. Open your web browser and navigate to [https://console.cloud.google.com/](https://console.cloud.google.com/) | |
| 2. Sign in with your Google account that has access to your Google Cloud project | |
| 3. Make sure you've selected the correct project in the dropdown at the top of the page (next to "Google Cloud") | |
| ### 1.1.2. Navigate to the API Library | |
| There are two ways to reach the API Library: | |
| **Method A - Via Navigation Menu:** | |
| 1. Click on the hamburger menu (≡) in the top-left corner | |
| 2. Scroll down to "APIs & Services" | |
| 3. Select "Library" from the submenu | |
| **Method B - Via Search Bar:** | |
| 1. Click on the search bar at the top of the console | |
| 2. Type "API Library" and select it from the results | |
| ### 1.1.3: Find the Secret Manager API | |
| 1. In the API Library, you'll see a search bar | |
| 2. Type "Secret Manager" in the search field | |
| 3. The "Secret Manager API" should appear in the results | |
| 4. Click on it to open the API details page | |
| ### 1.1.4. Enable the API | |
| 1. On the Secret Manager API details page, you'll see an "Enable" button (or "Manage" if it's already enabled) | |
| 2. Click the "Enable" button | |
| 3. Wait for a moment while Google enables the API for your project | |
| 4. You should see a confirmation message and the button will change to "Manage" | |
| ### 1.1.5. Verify the API is Enabled | |
| To confirm the API is successfully enabled: | |
| 1. In the left navigation panel, go to "APIs & Services" > "Dashboard" | |
| 2. Look for "Secret Manager API" in the list of enabled APIs | |
| 3. If you see it listed, the API is successfully enabled | |
| ### ❓ Why This Step Matters | |
| Enabling the Secret Manager API is necessary because: | |
| - Google Cloud requires explicit API activation to prevent unexpected billing charges | |
| - Until the API is enabled, you cannot create or access any secrets | |
| - This step registers your project to use the Secret Manager service | |
| - After enabling, your application code and IAM permissions will be able to interact with the service | |
| ## 1.2. gcloud CLI | |
| ### 1.2.1 Enable API | |
| ```bash | |
| gcloud services enable secretmanager.googleapis.com | |
| ``` | |
| ### 1.2.2. Check it's enabled | |
| ```bash | |
| gcloud services list --enabled --filter="name:secretmanager.googleapis.com" | |
| ``` | |
| You should then see, as an output: | |
| ``` | |
| NAME: secretmanager.googleapis.com | |
| TITLE: Secret Manager API | |
| ``` | |
| # 2. Create the Required Secrets | |
| ## 2.1. Web Console | |
| This process combines visual confirmation with secure storage of your sensitive data. | |
| ### 2.1.1. Accessing Secret Manager | |
| 1. Navigate to [https://console.cloud.google.com/](https://console.cloud.google.com/) | |
| 2. Ensure you're in the correct project using the dropdown at the top | |
| 3. Open the navigation menu (hamburger icon ≡ in the top-left) | |
| 4. Scroll down to "Security" section | |
| 5. Click on "Secret Manager" | |
| ### 2.1.2. Creating the `db-user` Secret | |
| 1. On the Secret Manager page, click the "CREATE SECRET" button at the top | |
| 2. In the "Create secret" form, fill in the following fields: | |
| - **Name**: Enter `db-user` | |
| - **Replication policy**: Select "Automatic" (this means Google will handle replication across regions for high availability) | |
| 3. For the secret value, you have two options: | |
| - **Option 1**: Click the "Enter secret value" tab, and type your database username in the text box | |
| - **Option 2**: Click the "Upload file" tab if your username is stored in a file | |
| 4. Under "Regions," you'll see which regions your secret will be stored in (with automatic replication) | |
| 5. Under "Secret versions to keep," leave the default (typically "Keep all versions") unless you have specific retention requirements | |
| 6. Under "Access," you can set up initial access controls, but we'll handle permissions separately, so you can leave this section as is for now | |
| 7. Click the "Create secret" button at the bottom of the page | |
| ### 2.1.3. Verification | |
| 1. After creation, you'll be redirected to the details page for your new secret | |
| 2. Here you can see: | |
| - The secret name: `db-user` | |
| - The versions available (should be Version 1) | |
| - The replication status | |
| - Creation time | |
| - Access control settings | |
| ### ❓ What Just Happened? | |
| You've now created a secure container called `db-user` that holds your database username. This secret: | |
| - Is encrypted at rest in Google's infrastructure | |
| - Has a version number (starting with 1) | |
| - Can be accessed only by those with proper permissions | |
| - Will be used by your application code instead of hardcoding the username | |
| ### 🧠 Understanding Versions | |
| Each time you update a secret, a new version is created rather than overwriting the existing value. | |
| This is important because: | |
| - It allows you to roll back if needed | |
| - It supports credential rotation without breaking applications | |
| - Your code can request a specific version or always use the latest | |
| ### 👇 Next Steps | |
| You would repeat this process for each of the other required secrets in your code: | |
| - `db-password` | |
| - `db-host` | |
| - `db-name` | |
| - `ssl-ca` | |
| - `ssl-cert` | |
| - `ssl-key` | |
| ## 2.2. gcloud CLI | |
| ### 2.2.1. Creating the Secret Container | |
| ```bash | |
| gcloud secrets create db-user --replication-policy="automatic" | |
| ``` | |
| This command creates an empty container for your secret. Let's dissect it: | |
| - `gcloud`: This is Google Cloud's command-line interface tool | |
| - `secrets`: You're working with the Secret Manager service | |
| - `create`: You're creating a new secret container | |
| - `db-user`: This is the name you're giving to your secret | |
| - `--replication-policy="automatic"`: This tells Google to automatically replicate your secret across multiple regions for high availability | |
| **What's really happening**: Imagine you're creating a secure safe deposit box in a bank. | |
| At this stage, you've only created the empty box and labeled it "db-user" - you haven't put anything inside it yet. | |
| The replication policy means the bank will automatically create backup copies of this box in different locations. | |
| ### 2.2.2. Adding the Actual Secret Value | |
| ```bash | |
| echo -n "your-database-username" | gcloud secrets versions add db-user --data-file=- | |
| ``` | |
| This command puts the actual secret value into the container. Breaking it down: | |
| 1. `echo -n "your-database-username"`: | |
| - `echo` outputs text to the command line | |
| - The `-n` flag removes the trailing newline character (important for secrets) | |
| - `"your-database-username"` is the actual value you're storing | |
| 2. The pipe symbol `|` connects the output of the first command to the input of the next command | |
| 3. `gcloud secrets versions add db-user --data-file=-`: | |
| - `gcloud secrets versions add` adds a new version to an existing secret | |
| - `db-user` specifies which secret container to add to | |
| - `--data-file=-` tells the command to read the secret value from standard input (the `-` symbol) | |
| **What's really happening**: Now you're putting your valuable item (the username) into that safe deposit box. | |
| The "versions add" part is significant because Secret Manager keeps a history of values. | |
| Rather than replacing what's in the box, it's like adding a new compartment to the box with a new version of the item. | |
| ### ❓ Why Use This Two-Step Process? | |
| This approach separates: | |
| 1. **Container creation** (first command): Sets up the structure, naming, and replication policy | |
| 2. **Value storage** (second command): Puts the actual sensitive data into the container | |
| The pipe (`|`) approach is particularly useful because: | |
| - It prevents the secret from being stored in your command history | |
| - It avoids saving the secret value in a temporary file | |
| - It minimizes exposure of the sensitive value | |
| ### ☝️ The `echo -n` Flag Explained | |
| The `-n` flag on the echo command is crucial: | |
| - Without it, echo would add a newline character at the end of your secret | |
| - This newline would become part of your secret value | |
| - When your application retrieves the secret, this unexpected newline could cause authentication problems | |
| ### Versioning Concept | |
| Each time you add a value to a secret, it creates a new version rather than replacing the existing one: | |
| - Version 1 is created with your first value | |
| - If you update it, version 2 is created | |
| - Your code can request "the latest version" or a specific version number | |
| - This enables credential rotation without breaking applications | |
| This approach to secrets management significantly improves security by keeping sensitive values out of your code, command history, and deployment artifacts. | |
| # 3. Authentication Setup for Google Secret Manager in Web Console | |
| Authentication is how Google Cloud verifies your identity before allowing access to Secret Manager. | |
| Let's explore how to set this up using the Web Console. | |
| ## 3.1. Understanding Authentication for Secret Manager | |
| When your application accesses Secret Manager, it needs to prove its identity to Google Cloud. | |
| This typically happens in one of two ways: | |
| 1. **For local development**: Using your personal Google account | |
| 2. **For production**: Using a service account (a special type of Google account for applications) | |
| Let's walk through setting up both approaches in the Web Console. | |
| ## 3.2. Local Development Authentication | |
| For **testing and development**, you'll typically use your own Google account: | |
| 1. **Sign in to Google Cloud Console**: | |
| - Go to [https://console.cloud.google.com/](https://console.cloud.google.com/) | |
| - Sign in with your Google account | |
| 2. **Install Google Cloud SDK locally**: | |
| - Download and install from [cloud.google.com/sdk](https://cloud.google.com/sdk) | |
| - This provides the necessary local authentication tools | |
| 3. **Run local authentication**: | |
| - Open a terminal or command prompt | |
| - Run `gcloud auth application-default login` | |
| - This will open a browser window for you to authenticate | |
| - After authenticating, your credentials will be stored locally | |
| This approach works well for development but isn't suitable for production applications. | |
| ## 3.3. Production Authentication via Service Account | |
| For **production**, you need to create a service account: | |
| 1. **Navigate to Service Accounts**: | |
| - In Google Cloud Console, open the navigation menu (≡) | |
| - Go to "IAM & Admin" > "Service Accounts" | |
| 2. **Create a Service Account**: | |
| - Click "CREATE SERVICE ACCOUNT" at the top | |
| - Fill in the details: | |
| - **Name**: `db-service-account` (descriptive name) | |
| - **Description**: "Service account for database access" | |
| - Click "CREATE AND CONTINUE" | |
| 3. **Grant Permissions**: | |
| - In the "Grant this service account access to project" section | |
| - Click the "Role" dropdown and search for "Secret Manager" | |
| - Select "Secret Manager Secret Accessor" role | |
| - Click "CONTINUE" | |
| 4. **Complete Creation**: | |
| - You can skip user access for now | |
| - Click "DONE" | |
| 5. **Create a Key for the Service Account**: | |
| - Find your new service account in the list | |
| - Click the three dots (⋮) at the end of its row | |
| - Select "Manage keys" | |
| - Click "ADD KEY" > "Create new key" | |
| - Choose "JSON" format | |
| - Click "CREATE" | |
| - The key file will automatically download to your computer | |
| 6. **Secure Your Key File**: | |
| - This JSON file contains sensitive credentials | |
| - Store it securely and never commit it to version control | |
| - Treat it like a password | |
| ## 3.4. Using the Service Account Key in Your Application | |
| To use this key: | |
| 1. **Set Environment Variable**: | |
| - Store the path to your key file in the `GOOGLE_APPLICATION_CREDENTIALS` environment variable: | |
| ``` | |
| export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your-key-file.json" | |
| ``` | |
| 2. **In Google Cloud Environments** (like App Engine or Cloud Run): | |
| - You often don't need to explicitly set credentials | |
| - The environment automatically uses the service account assigned to the service | |
| ## 3.5. Verifying Authentication | |
| To verify your setup: | |
| 1. In the Google Cloud Console, go to "Secret Manager" | |
| 2. Click on one of your secrets (like `db-user`) | |
| 3. Try to view the secret value | |
| 4. If you can see it, your permissions are set up correctly | |
| This authentication setup ensures that only authorized users and applications can access your secrets, maintaining your security posture while enabling your applications to access the credentials they need. | |
| ## 🧠 How Service Accounts Connect to Python Code | |
| The connection between a service account and your Python code is a fundamental concept that isn't immediately obvious. | |
| Think of a service account as an identity card for your application. | |
| Let me explain how this identity card gets connected to your Python code: | |
| ### ⛓️ The Authentication Chain | |
| 1. **Service Account**: This is an identity in Google Cloud (like a digital user) | |
| 2. **Key File**: The JSON key file you downloaded is essentially the "password" for this identity | |
| 3. **Environment Variable**: Your system tells Python where to find this "password" | |
| 4. **Python Libraries**: Google's libraries automatically use this information | |
| ### ❓ How It Works in Practice | |
| When your Python code runs: | |
| 1. **Your code** imports the Google Cloud libraries: | |
| ```python | |
| from google.cloud import secretmanager | |
| ``` | |
| 2. **The libraries** automatically look for credentials in this order: | |
| - Environment variables (`GOOGLE_APPLICATION_CREDENTIALS`) | |
| - Default locations on your computer | |
| - Metadata server (if running on Google Cloud) | |
| 3. **The authentication process** happens invisibly: | |
| ```python | |
| # This line automatically uses your credentials | |
| client = secretmanager.SecretManagerServiceClient() | |
| ``` | |
| 4. **Google's servers** verify the credentials and grant access based on the service account's permissions | |
| ### A Concrete Example | |
| Let's walk through a complete example: | |
| 1. You create a service account named "db-service-account" | |
| 2. You give this account permission to access secrets | |
| 3. You download a key file named "service-key.json" | |
| 4. You tell your system where this file is: | |
| ```bash | |
| export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-key.json" | |
| ``` | |
| 5. You run your Python script which contains: | |
| ```python | |
| from google.cloud import secretmanager | |
| client = secretmanager.SecretManagerServiceClient() | |
| # Your code continues... | |
| ``` | |
| Behind the scenes: | |
| - The library reads the environment variable to find the key file | |
| - It loads the credentials from that file | |
| - It uses these credentials to authenticate to Google | |
| - Google recognizes your application as "db-service-account" | |
| - Access is granted based on the permissions you set up | |
| ### Visual Analogy | |
| Imagine your application is trying to enter a secured building: | |
| - The service account is your identity card | |
| - The key file is the physical card itself with your photo and ID number | |
| - The environment variable tells your application where you keep your ID card | |
| - The Google libraries are like your hands, automatically reaching for the ID card when needed | |
| - Google's servers are the security guards, checking your ID and permissions | |
| This way, your application can "prove its identity" to Google Cloud without hardcoding any credentials in your Python files. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment