-
Notifications
You must be signed in to change notification settings - Fork 82
feature(GH-1613)- New policy management types #1762
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
base: main
Are you sure you want to change the base?
Changes from all commits
c075700
00cc56a
c20bccf
6c015d6
81c4460
1c99bb4
f00319b
baf658b
7eb1526
2c4d312
37c0ad9
40f6c22
b54e985
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
"""The central package of the application to work with the environment""" | ||
|
||
from dataall.core.environment import api, cdk, tasks | ||
from dataall.core.environment import api, db, cdk, tasks |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import environment_enums, environment_models, environment_repositories |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from dataall.base.api import GraphQLEnumMapper | ||
|
||
|
||
class PolicyManagementOptions(GraphQLEnumMapper): | ||
FULLY_MANAGED = 'Fully-Managed' | ||
PARTIALLY_MANAGED = 'Partially-Managed' | ||
EXTERNALLY_MANAGED = 'Externally-Managed' |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
from dataall.base.aws.sts import SessionHelper | ||
from dataall.base.context import get_context | ||
from dataall.base.db.exceptions import AWSResourceNotFound | ||
from dataall.core.environment.db.environment_enums import PolicyManagementOptions | ||
from dataall.core.organizations.db.organization_repositories import OrganizationRepository | ||
from dataall.core.permissions.services.environment_permissions import ( | ||
ENABLE_ENVIRONMENT_SUBSCRIPTIONS, | ||
|
@@ -46,6 +47,7 @@ | |
from dataall.core.permissions.services.tenant_permissions import MANAGE_ENVIRONMENTS | ||
from dataall.core.stacks.db.stack_repositories import StackRepository | ||
from dataall.core.vpc.db.vpc_repositories import VpcRepository | ||
from dataall.modules.shares_base.services.shares_enums import PrincipalType | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
@@ -397,15 +399,21 @@ def invite_group(uri, data=None) -> (Environment, EnvironmentGroup): | |
env_group_iam_role_arn = f'arn:aws:iam::{environment.AwsAccountId}:role/{env_group_iam_role_name}' | ||
env_role_imported = False | ||
|
||
# If environment role is imported, then data.all should attach the policies at import time | ||
# If environment role is created in environment stack, then data.all should attach the policies in the env stack | ||
# If environment role is imported, then data.all should attach the policies at import time ( Fully Managed ) | ||
# If environment role is created in environment stack, then data.all should attach the policies in the env stack ( Partially Managed - Here policy will be created but won't be attached ) | ||
policy_management: str = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Earlier, when the dataallManaged = True, then share policies were getting attached via the API calls. But when dataallManaged = False, then the share policies were not attached via API calls instead are attached via CF templates. Mapping this logic to the new policy management types and keeping the functionality same |
||
PolicyManagementOptions.FULLY_MANAGED.value | ||
if env_role_imported is True | ||
else PolicyManagementOptions.PARTIALLY_MANAGED.value | ||
) | ||
PolicyManager( | ||
role_name=env_group_iam_role_name, | ||
environmentUri=environment.environmentUri, | ||
session=session, | ||
account=environment.AwsAccountId, | ||
region=environment.region, | ||
environmentUri=environment.environmentUri, | ||
resource_prefix=environment.resourcePrefix, | ||
).create_all_policies(managed=env_role_imported) | ||
role_name=env_group_iam_role_name, | ||
).create_all_policies(policy_management=policy_management) | ||
|
||
athena_workgroup = NamingConventionService( | ||
target_uri=environment.environmentUri, | ||
|
@@ -470,11 +478,12 @@ def remove_group(uri, group): | |
group_membership = EnvironmentService.find_environment_group(session, group, environment.environmentUri) | ||
|
||
PolicyManager( | ||
role_name=group_membership.environmentIAMRoleName, | ||
environmentUri=environment.environmentUri, | ||
session=session, | ||
account=environment.AwsAccountId, | ||
region=environment.region, | ||
environmentUri=environment.environmentUri, | ||
resource_prefix=environment.resourcePrefix, | ||
role_name=group_membership.environmentIAMRoleName, | ||
).delete_all_policies() | ||
|
||
if group_membership: | ||
|
@@ -590,16 +599,17 @@ def add_consumption_role(uri, data=None) -> (Environment, EnvironmentGroup): | |
groupUri=group, | ||
IAMRoleArn=IAMRoleArn, | ||
IAMRoleName=IAMRoleArn.split('/')[-1], | ||
dataallManaged=data.get('dataallManaged', True), | ||
dataallManaged=data.get('dataallManaged'), | ||
) | ||
|
||
PolicyManager( | ||
role_name=consumption_role.IAMRoleName, | ||
environmentUri=environment.environmentUri, | ||
session=session, | ||
account=environment.AwsAccountId, | ||
region=environment.region, | ||
environmentUri=environment.environmentUri, | ||
resource_prefix=environment.resourcePrefix, | ||
).create_all_policies(managed=consumption_role.dataallManaged) | ||
role_name=consumption_role.IAMRoleName, | ||
).create_all_policies(policy_management=consumption_role.dataallManaged) | ||
|
||
session.add(consumption_role) | ||
session.commit() | ||
|
@@ -630,11 +640,12 @@ def remove_consumption_role(uri, env_uri): | |
|
||
if consumption_role: | ||
PolicyManager( | ||
role_name=consumption_role.IAMRoleName, | ||
environmentUri=environment.environmentUri, | ||
session=session, | ||
account=environment.AwsAccountId, | ||
region=environment.region, | ||
environmentUri=environment.environmentUri, | ||
resource_prefix=environment.resourcePrefix, | ||
role_name=consumption_role.IAMRoleName, | ||
).delete_all_policies() | ||
|
||
ResourcePolicyService.delete_resource_policy( | ||
|
@@ -668,6 +679,26 @@ def update_consumption_role(uri, env_uri, input): | |
for key, value in input.items(): | ||
setattr(consumption_role, key, value) | ||
session.commit() | ||
|
||
# If the input consumption role is not Fully-Managed then attach the share policy if it exists | ||
if consumption_role.dataallManaged == PolicyManagementOptions.FULLY_MANAGED.value: | ||
environment: Environment = EnvironmentService.get_environment_by_uri(session, env_uri) | ||
share_policy_manager = PolicyManager( | ||
session=session, | ||
account=environment.AwsAccountId, | ||
region=environment.region, | ||
environmentUri=environment.environmentUri, | ||
resource_prefix=environment.resourcePrefix, | ||
role_name=consumption_role.IAMRoleName, | ||
) | ||
for policy_manager in [ | ||
Policy | ||
for Policy in share_policy_manager.initializedPolicies | ||
if Policy.policy_type == 'SharePolicy' | ||
]: | ||
managed_policy_list = policy_manager.get_policies_unattached_to_role() | ||
policy_manager.attach_policies(managed_policy_list) | ||
|
||
return consumption_role | ||
|
||
@staticmethod | ||
|
@@ -808,6 +839,15 @@ def paginated_all_environment_consumption_roles(uri, data=None) -> dict: | |
def get_consumption_role(session, uri) -> Query: | ||
return EnvironmentRepository.get_consumption_role(session, uri) | ||
|
||
@staticmethod | ||
def get_role_policy_management_type(principal_type: str, principal_id: str): | ||
with get_context().db_engine.scoped_session() as session: | ||
if principal_type == PrincipalType.ConsumptionRole.value: | ||
consumption_role: ConsumptionRole = EnvironmentService.get_consumption_role(session, uri=principal_id) | ||
return consumption_role.dataallManaged | ||
|
||
return PolicyManagementOptions.FULLY_MANAGED.value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the role is not a consumption role, then all other roles( i.e. env groups ,etc ) are treated as Fully Managed |
||
|
||
@staticmethod | ||
@ResourcePolicyService.has_resource_permission(environment_permissions.LIST_ENVIRONMENT_NETWORKS) | ||
def paginated_environment_networks(uri, data=None) -> dict: | ||
|
@@ -893,11 +933,12 @@ def delete_environment(uri): | |
StackStatus.DELETE_COMPLETE.value, | ||
]: | ||
PolicyManager( | ||
role_name=environment.EnvironmentDefaultIAMRoleName, | ||
environmentUri=environment.environmentUri, | ||
session=session, | ||
account=environment.AwsAccountId, | ||
region=environment.region, | ||
environmentUri=environment.environmentUri, | ||
resource_prefix=environment.resourcePrefix, | ||
role_name=environment.EnvironmentDefaultIAMRoleName, | ||
).delete_all_policies() | ||
|
||
KeyValueTagRepository.delete_key_value_tags(session, environment.environmentUri, 'environment') | ||
|
@@ -1111,16 +1152,12 @@ def get_template_from_resource_bucket(uri, template_name): | |
@ResourcePolicyService.has_resource_permission(environment_permissions.GET_ENVIRONMENT) | ||
def resolve_consumption_role_policies(uri, IAMRoleName): | ||
environment = EnvironmentService.find_environment_by_uri(uri=uri) | ||
return PolicyManager( | ||
role_name=IAMRoleName, | ||
environmentUri=uri, | ||
account=environment.AwsAccountId, | ||
region=environment.region, | ||
resource_prefix=environment.resourcePrefix, | ||
).get_all_policies() | ||
|
||
@staticmethod | ||
@ResourcePolicyService.has_resource_permission(environment_permissions.GET_ENVIRONMENT) | ||
def get_consumption_role_by_name(uri, IAMRoleName): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. get_consumption_role_by_name , remving this static method as it was not used any where |
||
with get_context().db_engine.scoped_session() as session: | ||
return EnvironmentRepository.get_environment_consumption_role_by_name(session, uri, IAMRoleName) | ||
return PolicyManager( | ||
session=session, | ||
account=environment.AwsAccountId, | ||
region=environment.region, | ||
environmentUri=uri, | ||
resource_prefix=environment.resourcePrefix, | ||
role_name=IAMRoleName, | ||
).get_all_policies() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removing print statement