Skip to content
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

chore(master): merge maint-0.9 #242

Merged
merged 5 commits into from
Jan 15, 2025
Merged
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: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.9.4"
".": "0.9.5"
}
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## [0.9.5](https://github.com/reanahub/reana-db/compare/0.9.4...0.9.5) (2024-11-26)


### Features

* **cli:** add new `migrate-secret-key` command ([#240](https://github.com/reanahub/reana-db/issues/240)) ([efcbe72](https://github.com/reanahub/reana-db/commit/efcbe724a2797edf94a531a2fd49ae0dc25d29f7))


### Continuous integration

* **actions:** pin setuptools 70 ([#239](https://github.com/reanahub/reana-db/issues/239)) ([3202759](https://github.com/reanahub/reana-db/commit/320275969c64513f695ce59a145088f6222aa594))
* **python:** test more Python versions ([#239](https://github.com/reanahub/reana-db/issues/239)) ([e0cba7f](https://github.com/reanahub/reana-db/commit/e0cba7faa97cbf2919c4008ec884ea46ec817cd5))

## [0.9.4](https://github.com/reanahub/reana-db/compare/0.9.3...0.9.4) (2024-03-01)


Expand Down
20 changes: 20 additions & 0 deletions reana_db/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from reana_db.database import init_db
from reana_db.models import Resource, ResourceType
from reana_db.utils import (
change_key_encrypted_columns,
update_users_cpu_quota,
update_users_disk_quota,
update_workflows_cpu_quota,
Expand All @@ -43,6 +44,25 @@
click.secho("Database initialised.", fg="green")


@cli.command()
@click.option(

Check warning on line 48 in reana_db/cli.py

View check run for this annotation

Codecov / codecov/patch

reana_db/cli.py#L47-L48

Added lines #L47 - L48 were not covered by tests
"--old-key",
required=True,
help="Previous key used to encrypt database columns.",
)
def migrate_secret_key(old_key):

Check warning on line 53 in reana_db/cli.py

View check run for this annotation

Codecov / codecov/patch

reana_db/cli.py#L53

Added line #L53 was not covered by tests
"""Change the secret key used to encrypt database columns."""
click.echo("Migrating secret key...")

Check warning on line 55 in reana_db/cli.py

View check run for this annotation

Codecov / codecov/patch

reana_db/cli.py#L55

Added line #L55 was not covered by tests

try:
change_key_encrypted_columns(old_key)
except Exception:
logging.exception("Failed to migrate secret key")
sys.exit(1)

Check warning on line 61 in reana_db/cli.py

View check run for this annotation

Codecov / codecov/patch

reana_db/cli.py#L57-L61

Added lines #L57 - L61 were not covered by tests

click.echo("Successfully migrated secret key")

Check warning on line 63 in reana_db/cli.py

View check run for this annotation

Codecov / codecov/patch

reana_db/cli.py#L63

Added line #L63 was not covered by tests


@cli.group("alembic")
@click.pass_context
def alembic_group(ctx):
Expand Down
14 changes: 12 additions & 2 deletions reana_db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
)
from reana_commons.errors import REANAValidationError
from reana_commons.utils import get_disk_usage

import reana_db.config
from reana_db.config import (
DB_SECRET_KEY,
DEFAULT_QUOTA_LIMITS,
DEFAULT_QUOTA_RESOURCES,
WORKFLOW_TERMINATION_QUOTA_UPDATE_POLICY,
Expand Down Expand Up @@ -86,6 +87,15 @@ def generate_uuid():
return str(uuid.uuid4())


def _secret_key():
"""Secret key used to encrypt databse columns.

Do not use `DB_SECRET_KEY` directly, as that does not let us change the key
at runtime, which is needed when migrating between different keys.
"""
return reana_db.config.DB_SECRET_KEY


class QuotaBase:
"""Quota base functionality."""

Expand Down Expand Up @@ -333,7 +343,7 @@ class UserToken(Base, Timestamp):

id_ = Column(UUIDType, primary_key=True, default=generate_uuid)
token = Column(
EncryptedType(String(length=255), DB_SECRET_KEY, AesEngine, "pkcs5"),
EncryptedType(String(length=255), _secret_key, AesEngine, "pkcs5"),
unique=True,
)
status = Column(Enum(UserTokenStatus))
Expand Down
30 changes: 30 additions & 0 deletions reana_db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,3 +670,33 @@
for workflow in workflows:
store_workflow_disk_quota(workflow)
timer.count_event()


def change_key_encrypted_columns(old_key):

Check warning on line 675 in reana_db/utils.py

View check run for this annotation

Codecov / codecov/patch

reana_db/utils.py#L675

Added line #L675 was not covered by tests
"""Re-encrypt database columns with new secret key.

REANA should be already deployed with the new secret key in `REANA_SECRET_KEY`.
The old key is needed to decrypt the database and is passed as parameter.
"""
from reana_db.database import Session
from reana_db.models import UserToken
from reana_db import config

Check warning on line 683 in reana_db/utils.py

View check run for this annotation

Codecov / codecov/patch

reana_db/utils.py#L681-L683

Added lines #L681 - L683 were not covered by tests

new_key = config.DB_SECRET_KEY

Check warning on line 685 in reana_db/utils.py

View check run for this annotation

Codecov / codecov/patch

reana_db/utils.py#L685

Added line #L685 was not covered by tests

# set old key to be able to decrypt columns in database
config.DB_SECRET_KEY = old_key

Check warning on line 688 in reana_db/utils.py

View check run for this annotation

Codecov / codecov/patch

reana_db/utils.py#L688

Added line #L688 was not covered by tests

# read the columns from the database
user_tokens = Session.query(UserToken.id_, UserToken.token).all()
Session.expunge_all()

Check warning on line 692 in reana_db/utils.py

View check run for this annotation

Codecov / codecov/patch

reana_db/utils.py#L691-L692

Added lines #L691 - L692 were not covered by tests

# revert to new key
config.DB_SECRET_KEY = new_key

Check warning on line 695 in reana_db/utils.py

View check run for this annotation

Codecov / codecov/patch

reana_db/utils.py#L695

Added line #L695 was not covered by tests

# write columns to the database to encrypt them with new key
for user_token in user_tokens:
UserToken.query.filter_by(id_=user_token.id_).update(

Check warning on line 699 in reana_db/utils.py

View check run for this annotation

Codecov / codecov/patch

reana_db/utils.py#L698-L699

Added lines #L698 - L699 were not covered by tests
{"token": user_token.token}
)
Session.commit()

Check warning on line 702 in reana_db/utils.py

View check run for this annotation

Codecov / codecov/patch

reana_db/utils.py#L702

Added line #L702 was not covered by tests
Loading