Django + APP ENGINE (GAE) - No local .env or GOOGLE_CLOUD_PROJECT detected. No secrets found

558 Views Asked by At

i want to deploy in App Engine a Django app. I created and configurate a SECRET MANAGER in GAE and when i want to get that secret from my SETTINGS.PY, it display the error 'No local .env or GOOGLE_CLOUD_PROJECT detected. No secrets found'.

If i create the .env locally it works, but i want to get the secret info from the GAE.

SETTING.PY

env_file = os.path.join(BASE_DIR, ".env")

if os.path.isfile(env_file):
   # Use a local secret file, if provided

   env.read_env(env_file)
# ...
elif os.environ.get("GOOGLE_CLOUD_PROJECT", None):
   # Pull secrets from Secret Manager
   project_id = os.environ.get("GOOGLE_CLOUD_PROJECT")

   client = secretmanager.SecretManagerServiceClient()
   settings_name = os.environ.get("SETTINGS_NAME", "secret-django-phi")
   name = f"projects/{project_id}/secrets/{settings_name}/versions/latest"
   payload = client.access_secret_version(name=name).payload.data.decode("UTF-8")

   env.read_env(io.StringIO(payload))
else:
   raise Exception("No local .env or GOOGLE_CLOUD_PROJECT detected. No secrets found.")

REQUIREMENTS.txt

google-cloud-secret-manager==1.0.0
django-environ==0.4.5

SECRET MANAGER that i upload on GAE like an .env file

db_ip=x
db_name=x
db_user=x
db_pass=x
SECRET_KEY=*a lot of characters*

1

There are 1 best solutions below

0
On

I was getting this same error, while running locally. Turned out I had my .env in the parent directory, but the example code from Google assumes it's in the current directory. I just changed:

env_file = os.path.join(BASE_DIR, ".env")

to

env_file = os.path.join(BASE_DIR, "../.env")

and that fixed my problem.