Skip to content

Commit cb47a2f

Browse files
committed
Send API key expiration notifications on a daily basis
1 parent c702ab4 commit cb47a2f

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

aleph/logic/api_keys.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22

3+
import structlog
34
from flask import render_template
45
from sqlalchemy import and_, or_, func
56

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

20+
log = structlog.get_logger(__name__)
21+
1922

2023
def generate_user_api_key(role):
2124
event = "regenerated" if role.has_api_key else "generated"
@@ -78,13 +81,15 @@ def _send_api_key_expiration_notification(
7881
)
7982

8083
for role in query:
84+
expires_at = role.api_key_expires_at
8185
params = {
8286
"role": role,
83-
"expires_at": role.api_key_expires_at,
87+
"expires_at": expires_at,
8488
"settings_url": ui_url("settings"),
8589
}
8690
plain = render_template(plain_template, **params)
8791
html = render_template(html_template, **params)
92+
log.info(f"Sending API key expiration notification: {role} at {expires_at}")
8893
email_role(role, subject, html=html, plain=plain)
8994

9095
query.update({Role.api_key_expiration_notification_sent: days})

aleph/worker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
OP_PRUNE_ENTITY,
2222
)
2323
from aleph.logic.alerts import check_alerts
24+
from aleph.logic.api_keys import send_api_key_expiration_notifications
2425
from aleph.logic.collections import reingest_collection, reindex_collection
2526
from aleph.logic.collections import compute_collections, refresh_collection
2627
from aleph.logic.notifications import generate_digest, delete_old_notifications
@@ -91,6 +92,7 @@ def periodic(self):
9192
update_roles()
9293
check_alerts()
9394
generate_digest()
95+
send_api_key_expiration_notifications()
9496
delete_expired_exports()
9597
delete_old_notifications()
9698

0 commit comments

Comments
 (0)