Created
December 24, 2018 14:51
-
-
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.
This file contains 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
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