Skip to content

Commit

Permalink
Send API key expiration notifications on a daily basis
Browse files Browse the repository at this point in the history
  • Loading branch information
tillprochaska committed Jul 30, 2024
1 parent c702ab4 commit cb47a2f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion aleph/logic/api_keys.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime

import structlog
from flask import render_template
from sqlalchemy import and_, or_, func

Expand All @@ -16,6 +17,8 @@
# Number of days before an API key expires
API_KEY_EXPIRES_SOON_DAYS = 7

log = structlog.get_logger(__name__)


def generate_user_api_key(role):
event = "regenerated" if role.has_api_key else "generated"
Expand Down Expand Up @@ -78,13 +81,15 @@ def _send_api_key_expiration_notification(
)

for role in query:
expires_at = role.api_key_expires_at
params = {
"role": role,
"expires_at": role.api_key_expires_at,
"expires_at": expires_at,
"settings_url": ui_url("settings"),
}
plain = render_template(plain_template, **params)
html = render_template(html_template, **params)
log.info(f"Sending API key expiration notification: {role} at {expires_at}")
email_role(role, subject, html=html, plain=plain)

query.update({Role.api_key_expiration_notification_sent: days})
Expand Down
2 changes: 2 additions & 0 deletions aleph/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
OP_PRUNE_ENTITY,
)
from aleph.logic.alerts import check_alerts
from aleph.logic.api_keys import send_api_key_expiration_notifications
from aleph.logic.collections import reingest_collection, reindex_collection
from aleph.logic.collections import compute_collections, refresh_collection
from aleph.logic.notifications import generate_digest, delete_old_notifications
Expand Down Expand Up @@ -91,6 +92,7 @@ def periodic(self):
update_roles()
check_alerts()
generate_digest()
send_api_key_expiration_notifications()
delete_expired_exports()
delete_old_notifications()

Expand Down

0 comments on commit cb47a2f

Please sign in to comment.