Skip to content

Improved API key handling #3094

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

Merged
merged 29 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1ac583f
Allow users to reset their API key
tillprochaska May 25, 2023
a2a94ae
Only return API key once after it has been generated
tillprochaska Apr 11, 2024
399e85f
Add new UI to reset and display API key
tillprochaska Apr 15, 2024
5d96021
Refactor existing settings screen
tillprochaska Apr 15, 2024
b13425c
Ensure that toasts are always visible, even when scrolling
tillprochaska Apr 16, 2024
8065afa
Do not display password setting when password auth is disabled
tillprochaska Apr 16, 2024
688bb96
Use session tokens for authentication in API tests
tillprochaska Apr 30, 2024
b3eac7f
Do not generate API tokens for new roles
tillprochaska May 2, 2024
d7420be
Handle users without an API key properly in the settings UI
tillprochaska May 1, 2024
0622c84
Update wording to clarify that API keys are secrets
tillprochaska May 1, 2024
6f03791
Rename "reset_api_key" to "generate_api_key"
tillprochaska May 1, 2024
be5f029
Send email notification when API key is (re-)generated
tillprochaska May 2, 2024
f30757f
Extract logic to regenerate API keys into separate module
tillprochaska May 2, 2024
e1ed9b6
Let API keys expire after 90 days
tillprochaska May 6, 2024
a506ebe
Extract `generate_api_key` method from role model
tillprochaska May 6, 2024
6cc615b
Send notification when API keys are about to expire/have expired
tillprochaska May 6, 2024
6d2d89b
Display API key expiration date in UI
tillprochaska May 6, 2024
87ce636
Add CLI command to reset API key expiration of non-expiring API keys
tillprochaska May 6, 2024
5e52e2d
Replace use of deprecated `utcnow` method
tillprochaska May 8, 2024
c091223
Remove unnecessary keys from API JSON response
tillprochaska May 8, 2024
9a42b22
Add note to remind us to remove/update logic handling legacy API keys
tillprochaska May 8, 2024
b9ad42e
Send API key expiration notifications on a daily basis
tillprochaska May 8, 2024
c0ad86d
Fix Alembic migration order
tillprochaska Oct 22, 2024
1e74da4
Reauthenticate user in test to update session cache
tillprochaska Oct 23, 2024
f95572a
Update wording of API key notification emails based on feedback
tillprochaska Oct 23, 2024
de6eeff
Use strict equality check
tillprochaska Oct 23, 2024
9b9efa9
Clarify that API keys expire when generating a new key
tillprochaska Oct 23, 2024
50b689e
Display different UI messages in case the API key has expired
tillprochaska Nov 4, 2024
987b907
Fix Alembic migration order
tillprochaska Nov 4, 2024
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
113 changes: 113 additions & 0 deletions aleph/logic/api_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import datetime

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

from aleph.core import db
from aleph.model import Role
from aleph.model.common import make_token
from aleph.logic.mail import email_role
from aleph.logic.roles import update_role
from aleph.logic.util import ui_url

# Number of days after which API keys expire
API_KEY_EXPIRATION_DAYS = 90

# 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"
params = {"role": role, "event": event}
plain = render_template("email/api_key_generated.txt", **params)
html = render_template("email/api_key_generated.html", **params)
subject = f"API key {event}"
email_role(role, subject, html=html, plain=plain)

now = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0)
role.api_key = make_token()
role.api_key_expires_at = now + datetime.timedelta(days=API_KEY_EXPIRATION_DAYS)
role.api_key_expiration_notification_sent = None

db.session.add(role)
db.session.commit()
update_role(role)

return role.api_key


def send_api_key_expiration_notifications():
_send_api_key_expiration_notification(
days=7,
subject="Your API key will expire in 7 days",
plain_template="email/api_key_expires_soon.txt",
html_template="email/api_key_expires_soon.html",
)

_send_api_key_expiration_notification(
days=0,
subject="Your API key has expired",
plain_template="email/api_key_expired.txt",
html_template="email/api_key_expired.html",
)


def _send_api_key_expiration_notification(
days,
subject,
plain_template,
html_template,
):
now = datetime.date.today()
threshold = now + datetime.timedelta(days=days)

query = Role.all_users()
query = query.yield_per(1000)
query = query.where(
and_(
and_(
Role.api_key != None, # noqa: E711
func.date(Role.api_key_expires_at) <= threshold,
),
or_(
Role.api_key_expiration_notification_sent == None, # noqa: E711
Role.api_key_expiration_notification_sent > days,
),
)
)

for role in query:
expires_at = role.api_key_expires_at
params = {
"role": role,
"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})
db.session.commit()


def reset_api_key_expiration():
now = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0)
expires_at = now + datetime.timedelta(days=API_KEY_EXPIRATION_DAYS)

query = Role.all_users()
query = query.yield_per(500)
query = query.where(
and_(
Role.api_key != None, # noqa: E711
Role.api_key_expires_at == None, # noqa: E711
)
)

query.update({Role.api_key_expires_at: expires_at})
db.session.commit()
7 changes: 7 additions & 0 deletions aleph/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from aleph.queues import get_status, cancel_queue
from aleph.queues import get_active_dataset_status
from aleph.index.admin import delete_index
from aleph.logic.api_keys import reset_api_key_expiration as _reset_api_key_expiration
from aleph.index.entities import iter_proxies
from aleph.index.util import AlephOperationalException
from aleph.logic.collections import create_collection, update_collection
Expand Down Expand Up @@ -566,3 +567,9 @@ def evilshit():
delete_index()
destroy_db()
upgrade()


@cli.command()
def reset_api_key_expiration():
"""Reset the expiration date of all legacy, non-expiring API keys."""
_reset_api_key_expiration()
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""add primary key constraint to role_membership table

Revision ID: 131674bde902
Revises: c52a1f469ac7
Revises: 8adf50aadcb0
Create Date: 2024-07-17 14:37:25.269913

"""

# revision identifiers, used by Alembic.
revision = "131674bde902"
down_revision = "c52a1f469ac7"
down_revision = "8adf50aadcb0"

from alembic import op
import sqlalchemy as sa
Expand Down
30 changes: 30 additions & 0 deletions aleph/migrate/versions/d46fc882ec6b_api_key_expiration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""API key expiration

Revision ID: d46fc882ec6b
Revises: 131674bde902
Create Date: 2024-05-02 11:43:50.993948

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "d46fc882ec6b"
down_revision = "131674bde902"


def upgrade():
op.add_column("role", sa.Column("api_key_expires_at", sa.DateTime()))
op.add_column(
"role", sa.Column("api_key_expiration_notification_sent", sa.Integer())
)
op.create_index(
index_name="ix_role_api_key_expires_at",
table_name="role",
columns=["api_key_expires_at"],
)


def downgrade():
op.drop_column("role", "api_key_expires_at")
op.drop_column("role", "api_key_expiration_notification_sent")
27 changes: 21 additions & 6 deletions aleph/model/role.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import logging
from datetime import datetime
from datetime import datetime, timezone
from normality import stringify
from sqlalchemy import or_, not_, func
from itsdangerous import URLSafeTimedSerializer
from werkzeug.security import generate_password_hash, check_password_hash

from aleph.core import db
from aleph.settings import SETTINGS
from aleph.model.common import SoftDeleteModel, IdModel, make_token, query_like
from aleph.model.common import SoftDeleteModel, IdModel, query_like
from aleph.util import anonymize_email

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -52,6 +52,8 @@ class Role(db.Model, IdModel, SoftDeleteModel):
email = db.Column(db.Unicode, nullable=True)
type = db.Column(db.Enum(*TYPES, name="role_type"), nullable=False)
api_key = db.Column(db.Unicode, nullable=True)
api_key_expires_at = db.Column(db.DateTime, nullable=True)
api_key_expiration_notification_sent = db.Column(db.Integer, nullable=True)
is_admin = db.Column(db.Boolean, nullable=False, default=False)
is_muted = db.Column(db.Boolean, nullable=False, default=False)
is_tester = db.Column(db.Boolean, nullable=False, default=False)
Expand All @@ -68,6 +70,10 @@ class Role(db.Model, IdModel, SoftDeleteModel):
def has_password(self):
return self.password_digest is not None

@property
def has_api_key(self):
return self.api_key is not None

@property
def is_public(self):
return self.id in self.public_roles()
Expand Down Expand Up @@ -160,11 +166,12 @@ def to_dict(self):
"label": self.label,
"email": self.email,
"locale": self.locale,
"api_key": self.api_key,
"is_admin": self.is_admin,
"is_muted": self.is_muted,
"is_tester": self.is_tester,
"has_password": self.has_password,
"has_api_key": self.has_api_key,
"api_key_expires_at": self.api_key_expires_at,
# 'notified_at': self.notified_at
}
)
Expand Down Expand Up @@ -192,6 +199,17 @@ def by_api_key(cls, api_key):
return None
q = cls.all()
q = q.filter_by(api_key=api_key)
utcnow = datetime.now(timezone.utc)

# TODO: Exclude API keys without expiration date after deadline
# See https://github.com/alephdata/aleph/issues/3729
q = q.filter(
or_(
cls.api_key_expires_at == None, # noqa: E711
utcnow < cls.api_key_expires_at,
)
)

q = q.filter(cls.type == cls.USER)
q = q.filter(cls.is_blocked == False) # noqa
return q.first()
Expand All @@ -211,9 +229,6 @@ def load_or_create(cls, foreign_id, type_, name, email=None, is_admin=False):
role.is_blocked = False
role.notified_at = datetime.utcnow()

if role.api_key is None:
role.api_key = make_token()

if email is not None:
role.email = email

Expand Down
15 changes: 15 additions & 0 deletions aleph/templates/email/api_key_expired.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "email/layout.html" %}

{% block content -%}
<p>
{% trans expires_at=(expires_at | datetimeformat) -%}
Your Aleph API key has expired on {{expires_at}} UTC.
{%- endtrans %}
</p>

<p>
{% trans settings_url=settings_url -%}
If you do not use the Aleph API, or only use the API to access public data you can ignore this email. If you use the Aleph API to access data that is not publicly accessible then you’ll need to <a href="{{settings_url}}">regenerate your API key</a> to maintain access.
{%- endtrans %}
</p>
{%- endblock %}
11 changes: 11 additions & 0 deletions aleph/templates/email/api_key_expired.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "email/layout.txt" %}

{% block content -%}
{% trans expires_at=(expires_at | datetimeformat) -%}
Your Aleph API key has expired on {{expires_at}} UTC.
{%- endtrans %}

{% trans settings_url=settings_url -%}
If you do not use the Aleph API, or only use the API to access public data you can ignore this email. If you use the Aleph API to access data that is not publicly accessible then you’ll need to regenerate your API key ({{settings_url}}) to maintain access.
{%- endtrans %}
{%- endblock %}
15 changes: 15 additions & 0 deletions aleph/templates/email/api_key_expires_soon.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "email/layout.html" %}

{% block content -%}
<p>
{% trans expires_at=(expires_at | datetimeformat) -%}
Your Aleph API key will expire in 7 days, on {{expires_at}} UTC.
{%- endtrans %}
</p>

<p>
{% trans settings_url=settings_url -%}
If you do not use the Aleph API, or only use the API to access public data you can ignore this email. If you use the Aleph API to access data that is not publicly accessible then you’ll need to <a href="{{settings_url}}">regenerate your API key</a> to maintain access.
{%- endtrans %}
</p>
{%- endblock %}
11 changes: 11 additions & 0 deletions aleph/templates/email/api_key_expires_soon.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "email/layout.txt" %}

{% block content -%}
{% trans expires_at=(expires_at | datetimeformat) -%}
Your Aleph API key will expire in 7 days, on {{expires_at}} UTC.
{%- endtrans %}

{% trans -%}
If you do not use the Aleph API, or only use the API to access public data you can ignore this email. If you use the Aleph API to access data that is not publicly accessible then you’ll need to regenerate your API key ({{settings_url}}) to maintain access.
{%- endtrans %}
{%- endblock %}
13 changes: 13 additions & 0 deletions aleph/templates/email/api_key_generated.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "email/layout.html" %}

{% block content -%}
{% if event == "regenerated" -%}
{% trans -%}
Your Aleph API key has been regenerated. If that wasn’t you, please contact an administrator.
{%- endtrans %}
{% else -%}
{% trans -%}
An Aleph API key has been generated for your account. If that wasn’t you, please contact an administrator.
{%- endtrans %}
{%- endif %}
{%- endblock %}
13 changes: 13 additions & 0 deletions aleph/templates/email/api_key_generated.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "email/layout.txt" %}

{% block content -%}
{% if event == "regenerated" -%}
{% trans -%}
Your Aleph API key has been regenerated. If that wasn’t you, please contact an administrator.
{%- endtrans %}
{% else -%}
{% trans -%}
An Aleph API key has been generated for your account. If that wasn’t you, please contact an administrator.
{%- endtrans %}
{%- endif %}
{%- endblock %}
Loading
Loading