Thinktecture Logo

Securing Colab Notebooks – Protecting Your OpenAI API Keys

Marco Frodl
Author: Marco Frodl • Published: 04.09.2023 • Category: AI

Update 01/2024: Colab notebooks now offer a better way to securely manage and use your secrets. Read all about it in the article Secrets in Google Colab – The New Way to Protect API Keys.


Hey there, fellow AI enthusiasts! Today, I have a very important topic to address – handling security concerns in Google Colab notebooks, specifically when dealing with secret keys like OpenAI API keys.

Colab notebooks – a great place to bootstrap ideas

Google Colab notebooks are fantastic tools, right? They let us mix notes, instructions, and code effortlessly, something which I particularly love in my frequent experiments with large language models, embeddings, and experimenting with the LangChain framework. They allow me to conveniently share my work with my followers and provide an accessible, interactive platform for replicating and extending ideas.
The catch, though, is that many of these shared notebooks require API keys – like an OpenAI API key. Now, sharing these notebooks with API keys in plaintext is a no-go. We cannot insist enough on this issue as it risks accidental sharing outside of our organization.

Safeguarding our secret keys with Google Secret Manager

Fortunately, we can overcome this challenge by using Google Cloud Platform’s (GCP) Secret Manager. Here’s why it’s pretty solid:

  • It provides robust rights management features.
  • You can refresh your passwords through Secret versions.
  • It’s included in the Google Cloud Free Tier, offering resources that are freely available up to certain limits.
  • These usage limits apply both during and after the free trial period.

Here are the monthly free usage limits:

  • Active secret versions: 6 versions
  • Access operations: 10,000 operations
  • Rotation Notifications: 3 notifications

For a deeper dive, check out GCP’s pricing details

Getting Started with GCP’s Secret Manager

Before you go ballistic on your keyboard setting things up, here are a few prerequisites:

  1. Google Cloud Platform Account: Create one using the same Google account that you’ll use for your Colab notebooks. You can navigate the process here.
  2. Setup a GCP Project: It’s absolutely free to set up one.
  3. Enable Google Secret Manager: This link should guide you through this process. No need to enable the Google Cloud CLI.
  4. Creating a Secret: Once you’ve enabled the Secret Manager, go ahead and create a Secret for your OpenAI API key. You might name it something like ‘your-openai-api-key-secret-name’.
  5. GCP Project ID: Make sure you keep the project id of your GCP project handy, as you’ll need it in the forthcoming script as GCP_PROJECT_ID.

Now you’re all set to integrate the Secret Manager in your Colab notebook. But how? Below is an essential Python3 script you need to use in your notebook:
 

# Install and import GCP Secret Manager
!pip -q install --user google-cloud-secret-manager
from google.cloud import secretmanager

# let's authenticate with our Google account
from google.colab import auth
auth.authenticate_user()

# access secret function
def access_secret_version(secret_id, version_id="latest"):
    # reference to your GCP project where your Secret Manager lives
    GCP_PROJECT_ID = '123456789'

    # Create the Secret Manager client.
    client = secretmanager.SecretManagerServiceClient()

    # Build the resource name of the secret version.
    name = f"projects/{GCP_PROJECT_ID}/secrets/{secret_id}/versions/{version_id}"

    # Access the secret version.
    response = client.access_secret_version(name=name)

    # Return the decoded payload.
    return response.payload.data.decode('UTF-8')

    # usage: access_secret_version('your-openai-api-key-secret-name')
Code language: SQL (Structured Query Language) (sql)

Now you can Use the function access_secret_version('your-openai-api-key-secret-name') in your notebook wherever you need you secret key.

How does the script work?

After importing the needed library for GCP’s Secret Manager we need the right role to access our secrets. In Google Cloud Platform this would be the role ‘Secret Manager Secret Accessor’. However, it is not possible to associate this role with a Colab notebook. But our Google account has the admin role for the Google Cloud project and this includes the ‘Secret Manager Secret Accessor’ role.

The line auth.authenticate_user() connects our Google account to the Colab notebook and includes it in the request for a secret to the Google Cloud Platform. After attaching a new compute instance to your Colab notebook, the line auth.authenticate_user() will prompt you once to allow running the notebook using your Google account.

Fixing possible issues with the script

Why did I call the script the “essential version”? Well – it didn’t work in my notebooks.

In my case, this script throws an error while running it: ImportError: cannot import name 'secretmanager' from 'google.cloud' (unknown location)

Pinning the version for google-cloud-secret-manager to version 2.16.2 and using a more specific import statement for the secret manager can fix the problem.

Here’s the modified script:

# Install and import GCP Secret Manager
!pip -q install google-cloud-secret-manager==2.16.2
import google.cloud.secretmanager as secretmanager

# let's authenticate with our Google account
from google.colab import auth
auth.authenticate_user()

# access secret function
def access_secret_version(secret_id, version_id="latest"):
    # reference to your GCP project where your Secret Manager lives
    GCP_PROJECT_ID = '123456789'

    # Create the Secret Manager client.
    client = secretmanager.SecretManagerServiceClient()

    # Build the resource name of the secret version.
    name = f"projects/{GCP_PROJECT_ID}/secrets/{secret_id}/versions/{version_id}"

    # Access the secret version.
    response = client.access_secret_version(name=name)

    # Return the decoded payload.
    return response.payload.data.decode('UTF-8')

    # usage: access_secret_version('your-openai-api-key-secret-name')
Code language: Python (python)

Voila! Now, we can share our beloved notebooks without worrying about accidentally sharing our private API keys. It’s essential to maintain security while fostering a collaborative mindset.

Aktuelle Research-Insights unserer Experten für Sie

Lesen Sie, was unsere Experten bei ihrem Research bewegt und melden Sie sich zu unserem kostenlosen Thinktecture Labs-Newsletter an.

Labs-Newsletter Anmeldung
Marco Frodl

Marco Frodl

As a Principal Consultant for Generative AI at Thinktecture AG, I specialize in OpenAI's Generative Pre-trained Transformers (GPTs) and community-driven Large Language Models (LLMs) such as Llama2, Mistral, Falcon, DeepSeek Coder, and RNN-based models such as RWKV. My current research focuses on integrating Generative AI and LLMs into business applications, using frameworks such as LangChain, LlamaIndex or Canopy to create impactful and efficient workflows. With over 10 years of experience as a CTO in the financial services sector, I bring a holistic approach to technology adoption, always considering the various stakeholders involved in any product or project. This perspective is critical in the field of Generative AI, where I am passionate about guiding organizations in developing innovative solutions and adopting cutting-edge technologies. For collaborations or inquiries, please feel free to email me at marco.frodl@thinktecture.com or connect with me on X @marcofrodl.

More about me →