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

Remove Scopes and Add Viewer Role #2821

Merged
merged 5 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The types of changes are:
* Serialise `bson.ObjectId` types in SAR data packages [#2785](https://github.com/ethyca/fides/pull/2785)
* The ability to assign users as system managers for a specific system [#2714](https://github.com/ethyca/fides/pull/2714)
* New endpoints to add and remove users as system managers [#2726](https://github.com/ethyca/fides/pull/2726)

* Add an automated migration to give users a `viewer` role [#2821](https://github.com/ethyca/fides/pull/2821)

### Changed

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""automigrate viewer role

Revision ID: 50180bbbb959
Revises: 68a518a3c050
Create Date: 2023-03-14 14:26:32.910570

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
from sqlalchemy import text

revision = "50180bbbb959"
down_revision = "68a518a3c050"
branch_labels = None
depends_on = None


def upgrade():
op.drop_constraint(
pattisdr marked this conversation as resolved.
Show resolved Hide resolved
"messagingconfig_service_type_key", "messagingconfig", type_="unique"
)
op.create_index(
op.f("ix_messagingconfig_service_type"),
"messagingconfig",
["service_type"],
unique=True,
)

"""Automatic data migration: Set user scopes to empty, and set all user roles to viewer"""
bind = op.get_bind()
bind.execute(
text(
"""
UPDATE fidesuserpermissions
SET scopes = '{}',
roles = '{viewer}';
"""
)
)

"""Automatic data migration: Similarly Update all client scopes attached to users"""
bind.execute(
text(
"""
UPDATE client
SET scopes = '{}',
roles = '{viewer}'
FROM fidesuserpermissions
WHERE fidesuserpermissions.user_id = client.user_id;
"""
)
)


def downgrade():
op.drop_index(op.f("ix_messagingconfig_service_type"), table_name="messagingconfig")
op.create_unique_constraint(
"messagingconfig_service_type_key", "messagingconfig", ["service_type"]
)
1 change: 0 additions & 1 deletion src/fides/api/ops/tasks/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import pandas as pd
from boto3 import Session
from botocore.exceptions import ClientError, ParamValidationError

from bson import ObjectId
from loguru import logger

Expand Down
2 changes: 2 additions & 0 deletions src/fides/lib/oauth/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
CLI_OBJECTS_READ,
CLIENT_READ,
CONFIG_READ,
CONFIG_UPDATE,
CONNECTION_READ,
CONNECTION_TYPE_READ,
CONSENT_READ,
Expand Down Expand Up @@ -114,6 +115,7 @@ class RoleRegistryEnum(Enum):
MESSAGING_CREATE_OR_UPDATE,
MESSAGING_DELETE,
PRIVACY_REQUEST_NOTIFICATIONS_CREATE_OR_UPDATE,
CONFIG_UPDATE,
pattisdr marked this conversation as resolved.
Show resolved Hide resolved
]

ROLES_TO_SCOPES_MAPPING: Dict[str, List] = {
Expand Down
22 changes: 22 additions & 0 deletions tests/ops/api/v1/endpoints/test_config_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from fides.api.ops.api.v1 import urn_registry as urls
from fides.api.ops.models.application_config import ApplicationConfig
from fides.api.ops.schemas.storage.storage import StorageType
from fides.lib.oauth.roles import CONTRIBUTOR, OWNER, VIEWER


class TestPatchApplicationConfig:
Expand Down Expand Up @@ -46,6 +47,27 @@ def test_patch_application_config_wrong_scope(
response = api_client.patch(url, headers=auth_header, json=payload)
assert 403 == response.status_code

def test_patch_application_config_viewer_role(
self, api_client: TestClient, payload, url, generate_role_header
):
auth_header = generate_role_header(roles=[VIEWER])
response = api_client.patch(url, headers=auth_header, json=payload)
assert 403 == response.status_code

def test_patch_application_config_contributor_role(
self, api_client: TestClient, payload, url, generate_role_header
):
auth_header = generate_role_header(roles=[CONTRIBUTOR])
response = api_client.patch(url, headers=auth_header, json=payload)
assert 403 == response.status_code

def test_patch_application_config_admin_role(
self, api_client: TestClient, payload, url, generate_role_header
):
auth_header = generate_role_header(roles=[OWNER])
response = api_client.patch(url, headers=auth_header, json=payload)
assert 200 == response.status_code

def test_patch_application_config_with_invalid_key(
self,
api_client: TestClient,
Expand Down
2 changes: 1 addition & 1 deletion tests/ops/service/storage_uploader_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from unittest.mock import Mock
from zipfile import ZipFile

from bson import ObjectId
import pandas as pd
import pytest
from bson import ObjectId
from sqlalchemy.orm import Session

from fides.api.ops.common_exceptions import StorageUploadError
Expand Down