Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ def read_files(files):
'dotenvx'
],
install_requires=[
'eciespy>=0.4.3',
'python-dotenv>=1.0.1',
],
)
50 changes: 48 additions & 2 deletions src/dotenvx/main.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
def load_dotenvx():
raise NotImplementedError("go to [github.com/dotenvx/dotenvx] and follow python directions there")
from typing import Optional, IO
import dotenv

import ecies
import base64
import os

def decrypt_value(value: str, private_key: str) -> str:
private_key = ecies.PrivateKey.from_hex(curve='secp256k1', sk_hex=private_key)
base64_ciphertext = value.lstrip('encrypted:')
ciphertext = base64.b64decode(base64_ciphertext)
decrypted_value = ecies.decrypt(private_key.to_hex(), ciphertext)
return decrypted_value.decode()

def load_dotenvx(
dotenv_path: Optional[str] = None,
stream : Optional[IO[str]] = None,
verbose : bool = False,
override : bool = False,
interpolate: bool = True,
encoding : Optional[str] = "utf-8",
) -> bool:

return_value = False # Set to True if at least one variable is set, otherwise False

env_values = dotenv.dotenv_values(
dotenv_path = dotenv_path,
stream = stream,
verbose = verbose,
interpolate = interpolate,
encoding = encoding)

env_keys_values = dotenv.dotenv_values('.env.keys')
dotenv_private_key = env_keys_values['DOTENV_PRIVATE_KEY']

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since .env.keys should be .gitignored, can we assume os.environ['DOTENV_PRIVATE_KEY'] is already set correctly and use it?


# Decrypt encrypted values
for key, value in env_values.items():
if value.startswith('encrypted:'):
env_values[key] = decrypt_value(value, dotenv_private_key)

# Set environment variables
for key, value in env_values.items():
if key in os.environ and not override:
continue
os.environ[key] = value
return_value = True

return return_value