From 870e58ee93f6bbb69f82d3f9f3dbd5c360ec1c47 Mon Sep 17 00:00:00 2001 From: Michael Sekamanya <86433807+mawandm@users.noreply.github.com> Date: Thu, 13 Jun 2024 23:58:06 -0700 Subject: [PATCH 1/3] fix(api): add TASKS to role action resource type (#121) This PR 1. Adds TASKS to role action type. 2. Adds APPS to the role action type Closes #120 --- ...3d6d802ca102_remove_model_role_entities.py | 57 ++++++++++--------- ..._add_tasks_to_role_action_resource_type.py | 35 ++++++++++++ 2 files changed, 64 insertions(+), 28 deletions(-) create mode 100644 nesis/api/alembic/versions/50bebf15de30_add_tasks_to_role_action_resource_type.py diff --git a/nesis/api/alembic/versions/3d6d802ca102_remove_model_role_entities.py b/nesis/api/alembic/versions/3d6d802ca102_remove_model_role_entities.py index eb12300..359f4dc 100644 --- a/nesis/api/alembic/versions/3d6d802ca102_remove_model_role_entities.py +++ b/nesis/api/alembic/versions/3d6d802ca102_remove_model_role_entities.py @@ -22,42 +22,15 @@ def upgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### op.drop_index("idx_model_name", table_name="model") - op.drop_table("model") op.drop_table("model_rule") op.drop_constraint("fk_rule_model", "prediction", type_="foreignkey") + op.drop_table("model") # ### end Alembic commands ### def downgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### - op.create_table( - "model_rule", - sa.Column("id", sa.BIGINT(), autoincrement=True, nullable=False), - sa.Column("name", sa.VARCHAR(length=255), autoincrement=False, nullable=True), - sa.Column("model", sa.BIGINT(), autoincrement=False, nullable=False), - sa.Column( - "description", sa.VARCHAR(length=255), autoincrement=False, nullable=True - ), - sa.Column( - "created_by", sa.VARCHAR(length=255), autoincrement=False, nullable=True - ), - sa.Column( - "create_date", postgresql.TIMESTAMP(), autoincrement=False, nullable=False - ), - sa.Column( - "value", - postgresql.JSONB(astext_type=sa.Text()), - autoincrement=False, - nullable=False, - ), - sa.Column("enabled", sa.BOOLEAN(), autoincrement=False, nullable=False), - sa.ForeignKeyConstraint( - ["model"], ["model.id"], name="fk_model_rule_model", ondelete="CASCADE" - ), - sa.PrimaryKeyConstraint("id", name="model_rule_pkey"), - sa.UniqueConstraint("name", name="model_rule_name_key"), - ) op.create_table( "model", sa.Column("id", sa.BIGINT(), autoincrement=True, nullable=False), @@ -87,6 +60,34 @@ def downgrade() -> None: sa.PrimaryKeyConstraint("id", name="model_pkey"), sa.UniqueConstraint("module", "name", name="uq_model_module_name"), ) + + op.create_table( + "model_rule", + sa.Column("id", sa.BIGINT(), autoincrement=True, nullable=False), + sa.Column("name", sa.VARCHAR(length=255), autoincrement=False, nullable=True), + sa.Column("model", sa.BIGINT(), autoincrement=False, nullable=False), + sa.Column( + "description", sa.VARCHAR(length=255), autoincrement=False, nullable=True + ), + sa.Column( + "created_by", sa.VARCHAR(length=255), autoincrement=False, nullable=True + ), + sa.Column( + "create_date", postgresql.TIMESTAMP(), autoincrement=False, nullable=False + ), + sa.Column( + "value", + postgresql.JSONB(astext_type=sa.Text()), + autoincrement=False, + nullable=False, + ), + sa.Column("enabled", sa.BOOLEAN(), autoincrement=False, nullable=False), + sa.ForeignKeyConstraint( + ["model"], ["model.id"], name="fk_model_rule_model", ondelete="CASCADE" + ), + sa.PrimaryKeyConstraint("id", name="model_rule_pkey"), + sa.UniqueConstraint("name", name="model_rule_name_key"), + ) op.create_index("idx_model_name", "model", ["module", "name"], unique=False) op.create_foreign_key( "fk_rule_model", "prediction", "model", ["model"], ["id"], ondelete="CASCADE" diff --git a/nesis/api/alembic/versions/50bebf15de30_add_tasks_to_role_action_resource_type.py b/nesis/api/alembic/versions/50bebf15de30_add_tasks_to_role_action_resource_type.py new file mode 100644 index 0000000..1448dae --- /dev/null +++ b/nesis/api/alembic/versions/50bebf15de30_add_tasks_to_role_action_resource_type.py @@ -0,0 +1,35 @@ +"""add tasks to role_action_resource_type + +Revision ID: 50bebf15de30 +Revises: 3d6d802ca102 +Create Date: 2024-06-13 23:41:54.517558 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "50bebf15de30" +down_revision: Union[str, None] = "3d6d802ca102" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.execute("ALTER TYPE role_action_resource_type ADD VALUE 'APPS';") + op.execute("ALTER TYPE role_action_resource_type ADD VALUE 'TASKS';") + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + """ + We keep the APPS and TASKS types as might be used by the application + """ + pass + # ### end Alembic commands ### From 26eed901e22d9538eebfd1cf1658b556b2fb84e4 Mon Sep 17 00:00:00 2001 From: Michael Sekamanya <86433807+mawandm@users.noreply.github.com> Date: Fri, 14 Jun 2024 00:05:54 -0700 Subject: [PATCH 2/3] fix(api): add parent_id to tasks unique constraint (#122) Closes #118 --- ...add_parent_id_to_task_unique_constraint.py | 35 +++++++++++++++++++ nesis/api/core/models/entities.py | 4 ++- nesis/api/core/services/task_service.py | 2 +- 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 nesis/api/alembic/versions/cbad6afbe13d_add_parent_id_to_task_unique_constraint.py diff --git a/nesis/api/alembic/versions/cbad6afbe13d_add_parent_id_to_task_unique_constraint.py b/nesis/api/alembic/versions/cbad6afbe13d_add_parent_id_to_task_unique_constraint.py new file mode 100644 index 0000000..24ce66b --- /dev/null +++ b/nesis/api/alembic/versions/cbad6afbe13d_add_parent_id_to_task_unique_constraint.py @@ -0,0 +1,35 @@ +"""add parent_id to task unique constraint + +Revision ID: cbad6afbe13d +Revises: 50bebf15de30 +Create Date: 2024-06-13 23:59:45.211996 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "cbad6afbe13d" +down_revision: Union[str, None] = "50bebf15de30" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint("uq_task_type_schedule", "task", type_="unique") + op.create_unique_constraint( + "uq_task_parent_id_type_schedule", "task", ["parent_id", "type", "schedule"] + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint("uq_task_parent_id_type_schedule", "task", type_="unique") + op.create_unique_constraint("uq_task_type_schedule", "task", ["type", "schedule"]) + # ### end Alembic commands ### diff --git a/nesis/api/core/models/entities.py b/nesis/api/core/models/entities.py index 5b02adc..2bd5a12 100644 --- a/nesis/api/core/models/entities.py +++ b/nesis/api/core/models/entities.py @@ -394,7 +394,9 @@ class Task(Base): update_date = Column(DateTime, default=dt.datetime.utcnow, nullable=False) __table_args__ = ( - UniqueConstraint("type", "schedule", name="uq_task_type_schedule"), + UniqueConstraint( + "parent_id", "type", "schedule", name="uq_task_parent_id_type_schedule" + ), Index("idx_task_type", "type"), Index("idx_task_parent", "parent_id"), ) diff --git a/nesis/api/core/services/task_service.py b/nesis/api/core/services/task_service.py index 82dfa7a..b74f490 100644 --- a/nesis/api/core/services/task_service.py +++ b/nesis/api/core/services/task_service.py @@ -205,7 +205,7 @@ def create(self, **kwargs): error_str = str(exc).lower() if ("unique constraint" in error_str) and ( - "uq_task_type_schedule" in error_str + "uq_task_parent_id_type_schedule" in error_str ): # valid failure raise ConflictException("Task already scheduled on this type") From 8417fb809b830430716c16413680ed6da4d539ab Mon Sep 17 00:00:00 2001 From: Michael Sekamanya <86433807+mawandm@users.noreply.github.com> Date: Mon, 17 Jun 2024 11:21:30 -0700 Subject: [PATCH 3/3] fix(frontend): run ms oauth logout only when enabled (#123) This PR fixes a bug that run the MS oauth logic even when MS oauth was not enabled. Fixes #119 --- nesis/frontend/client/src/SessionContext.js | 22 +++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/nesis/frontend/client/src/SessionContext.js b/nesis/frontend/client/src/SessionContext.js index 2425ae1..929da17 100644 --- a/nesis/frontend/client/src/SessionContext.js +++ b/nesis/frontend/client/src/SessionContext.js @@ -77,16 +77,18 @@ export function useSignOut(client, config) { async function logoutMicrosoft(config) { try { - const msalInstance = new PublicClientApplication({ - auth: { - clientId: config?.auth?.OAUTH_AZURE_CLIENT_ID, - authority: config?.auth?.OAUTH_AZURE_AUTHORITY, - redirectUri: config?.auth?.OAUTH_AZURE_REDIRECTURI, - postLogoutRedirectUri: config?.auth?.OAUTH_AZURE_REDIRECTURI, - }, - }); - await msalInstance.initialize(); - await msalInstance.logoutRedirect(); + if (config?.auth?.NESIS_OAUTH_AZURE_ENABLED) { + const msalInstance = new PublicClientApplication({ + auth: { + clientId: config?.auth?.OAUTH_AZURE_CLIENT_ID, + authority: config?.auth?.OAUTH_AZURE_AUTHORITY, + redirectUri: config?.auth?.OAUTH_AZURE_REDIRECTURI, + postLogoutRedirectUri: config?.auth?.OAUTH_AZURE_REDIRECTURI, + }, + }); + await msalInstance.initialize(); + await msalInstance.logoutRedirect(); + } } catch (e) { /* ignored */ }