Skip to content

Instantly share code, notes, and snippets.

@IAmJSD
Created December 24, 2018 14:51
Show Gist options
  • Save IAmJSD/9f8bdc01094a8568525689187efdc62a to your computer and use it in GitHub Desktop.
Save IAmJSD/9f8bdc01094a8568525689187efdc62a to your computer and use it in GitHub Desktop.
A Python secrets system using Google Firestore. Removes the need for configs that expose sensitive data or even environment variables.
from google.cloud import firestore
# Imports go here.
db = firestore.Client()
# Defines the Firestore client.
class SecretsManager:
"""Handles secrets from the Firestore DB."""
def __getitem__(self, key):
doc = db.collection("secrets").document(key).get()
if not doc.exists:
raise KeyError(key)
return doc.to_dict()['result']
def __setitem__(self, key, item):
db.collection("secrets").document(key).set({
"result": item
})
secrets = SecretsManager()
# The initialised secrets manager.
# ================== USAGE EXAMPLES ===================
# Getting from the DB: item_name = secrets['ITEM_NAME']
# Setting in the DB: secrets['ITEM_NAME'] = item_name
# =====================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment