generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Wrap tableaccess delete in a transaction Changes are to ensure that if deleting the table access fails, the users original permissions are restored, and the database object remains. Otherwise the state of the DB and LF could get out of sync. * Default to local settings when using manage.py * Allow revoking to silently fail if the permission does not exist This change allows the revoke_permission method to silently fail if the permission does not exist. This is useful when revoking permissions that may have already been revoked, and avoids getting in a state where the user cannot be granted permissions again. * Update user usernames When creating users, use the UPN, preferred username or email address. This will only occur on initial login. For existing users, a data migration has been addeded to update the usernames to their email address, with the domain lowercase. This resolves a bug where permissions were not granted to the correct quicksight user ARN, as it was using the email value and the users quicksight username used the User Principal Name (UPN). * Bump chart version
- Loading branch information
1 parent
648c1fd
commit 20d0b4c
Showing
11 changed files
with
192 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Generated by Django 5.1 on 2024-09-20 16:23 | ||
|
||
from django.db import migrations | ||
|
||
|
||
def update_usernames(apps, schema_editor): | ||
User = apps.get_model("users", "User") | ||
for user in User.objects.all(): | ||
email, domain = user.email.split("@") | ||
domain = domain.lower() | ||
user.username = f"{email}@{domain}" | ||
user.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("users", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(code=update_usernames, reverse_code=migrations.RunPython.noop), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from unittest.mock import MagicMock, patch | ||
|
||
import botocore | ||
import botocore.exceptions | ||
import pytest | ||
|
||
from ap.aws.lakeformation import LakeFormationService | ||
|
||
|
||
class TestRevoke: | ||
def test_revoke_database_no_error(self): | ||
""" | ||
Test that if the user does not have access to revoke, no error is raised | ||
""" | ||
lf = LakeFormationService() | ||
with patch.object(lf, "get_client") as mock_get_client: | ||
mock_client = MagicMock() | ||
mock_client.revoke_permissions.side_effect = botocore.exceptions.ClientError( | ||
{ | ||
"Error": { | ||
"Code": "InvalidInputException", | ||
"Message": "Grantee has no permissions and no grantable permissions on resource", # noqa | ||
} | ||
}, | ||
"revoke_permissions", | ||
) | ||
mock_get_client.return_value = mock_client | ||
assert ( | ||
lf.revoke_database_permissions(database="db_without_access", principal="user") | ||
is None | ||
) | ||
|
||
def test_revoke_database_raises_error(self): | ||
lf = LakeFormationService() | ||
with patch.object(lf, "get_client") as mock_get_client: | ||
mock_client = MagicMock() | ||
mock_client.revoke_permissions.side_effect = botocore.exceptions.ClientError( | ||
{ | ||
"Error": { | ||
"Code": "SomeOtherError", | ||
"Message": "Some other error message", | ||
} | ||
}, | ||
"revoke_permissions", | ||
) | ||
mock_get_client.return_value = mock_client | ||
# revoking should raises exception | ||
with pytest.raises(botocore.exceptions.ClientError): | ||
lf.revoke_database_permissions(database="db_without_access", principal="user") | ||
|
||
def test_revoke_table_no_error(self): | ||
""" | ||
Test that if the user does not have access to revoke, no error is raised | ||
""" | ||
lf = LakeFormationService() | ||
with patch.object(lf, "get_client") as mock_get_client: | ||
mock_client = MagicMock() | ||
mock_client.revoke_permissions.side_effect = botocore.exceptions.ClientError( | ||
{ | ||
"Error": { | ||
"Code": "InvalidInputException", | ||
"Message": "Grantee has no permissions and no grantable permissions on resource", # noqa | ||
} | ||
}, | ||
"revoke_permissions", | ||
) | ||
mock_get_client.return_value = mock_client | ||
assert ( | ||
lf.revoke_table_permissions( | ||
database="db_without_access", table="table_without_access", principal="user" | ||
) | ||
is None | ||
) | ||
|
||
def test_revoke_table_raises_error(self): | ||
lf = LakeFormationService() | ||
with patch.object(lf, "get_client") as mock_get_client: | ||
mock_client = MagicMock() | ||
mock_client.revoke_permissions.side_effect = botocore.exceptions.ClientError( | ||
{ | ||
"Error": { | ||
"Code": "SomeOtherError", | ||
"Message": "Some other error message", | ||
} | ||
}, | ||
"revoke_permissions", | ||
) | ||
mock_get_client.return_value = mock_client | ||
# revoking should raises exception | ||
with pytest.raises(botocore.exceptions.ClientError): | ||
lf.revoke_table_permissions( | ||
database="db_without_access", table="table_without_access", principal="user" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters