Alexmhack / python-with-secrets-manager

Testing a simple python application by making use of GCP Secrets Manager for the environment variables

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

python-with-secrets-manager

This is a very basic FastAPI application that makes use of Secrets Manager Providers like AWS & GCP.

You can take reference from the code for better understanding, below are listed a few points to be noted for using and managing secrets in Secrets Manager Provider.

  1. AWS provides us option to add Other type of secret which basically allows us to store Key/Value pairs as secrets, this is very helpful as we can store lots of key value pairs and make use of them as a dictionary in Python code. AWS - Other type of secret

  2. On the other hand, GCP does not allow this option and instead we have a text based input, but we can definitely enter JSON format there and store it and later on access this as a dictionary in Python code. GCP - Text type of secret only

  3. We are using lru_cache from itertools to cache our secrets once they have being retrieved in the Python code, which is a good way to fetch secrets very fast after initial loading but the secrets value won't change until the server has been restarted which is serving FastAPI app.

  4. AttrDict -> This is a custom written utility which helps to convert a Python dict to a class object which has access to all the dict keys using . notation. For e.g. if a secret is {"API_VERSION": "14"} then using AttrDict instance we can access it using attr_dict_instance.API_VERSION # output -> 14

  5. We can even use region wise secrets by defining the secrets in multiple regions of Secrets Manager Provider (AWS / GCP). This can be helpful when we have different servers in different regions and we want to match the Secrets with those regions and access accordingly.

AWS Setup

AWS - Image showing secret stored in AWS console

GCP Setup

GCP - Image showing secret stored in GCP console

About

Testing a simple python application by making use of GCP Secrets Manager for the environment variables


Languages

Language:Python 100.0%