Skip to content

Adjusted key_setup() to fix my issue #110 #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 11 additions & 4 deletions easyauth/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,18 @@ def key_setup(self):
# check if keys exist in KEY_PATH else create
assert "KEY_NAME" in os.environ, f"missing KEY_NAME env variable"
key_path = os.getenv("KEY_PATH", "")

if not os.path.exists(key_path):
Copy link
Owner

Choose a reason for hiding this comment

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

I like the idea of adding a check here for the path, but raising an exception instead of creating the path.

The reason behind this is that with the ephemeral workers like docker / kubernetes, we would have a different private key each a new instance started instead of forcing the developer to ensure that a persistent volume is used to store the private key. For additional context, when a private key changes any existing and unexpired tokens will not work with a newly refreshed private key.

os.makedirs(key_path)

if key_path:
key_path = f"{key_path}/{os.environ['KEY_NAME']}.key"
key_path = f"{key_path}/{os.environ['KEY_NAME']}"
Copy link
Owner

Choose a reason for hiding this comment

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

Removing the .key postfix is probably a good idea in the long run, but is a breaking change for anyone defining the KEY_NAME without .key.

else:
key_path = f"{os.environ['KEY_NAME']}"

if not os.path.exists(key_path):
with open(key_path, "w") as file:
Copy link
Owner

Choose a reason for hiding this comment

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

Once the directory exists, there is is no need to make an empty file.

file.write("")
try:
with open(key_path + ".key", "r") as k:
self._privkey = jwk.JWK.from_json(k.readline())
Expand Down Expand Up @@ -429,9 +436,9 @@ async def send_email(

message = MessageSchema(
subject=f"{subject}",
recipients=recipients
if isinstance(recipients, list)
else [recipients], # List of recipients, as many as you can pass
recipients=(
recipients if isinstance(recipients, list) else [recipients]
), # List of recipients, as many as you can pass
body=body,
subtype="html",
)
Expand Down
Loading