-
Notifications
You must be signed in to change notification settings - Fork 82
[Gh 884] IAM policy splitting for requestor IAM policies #1650
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
Merged
dlpzx
merged 35 commits into
data-dot-all:main
from
TejasRGitHub:gh-884-IAM-policy-splitting
Feb 3, 2025
Merged
Changes from 30 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
34ac3e2
Changes for splitting IAM role changes
TejasRGitHub 330dc51
Merge branch 'main' into gh-884-IAM-policy-splitting
44d1205
Sycing latest chanegs from aws dev for iam splitting
TejasRGitHub e131da7
Corrections to unit tests
TejasRGitHub 3087bc6
Service Quota file
TejasRGitHub db36fb0
Adding comments and other changes
TejasRGitHub 6af3c2e
Correcting unit tests and making env chanegs for SES emails to work
TejasRGitHub e072f25
Changes observed during testing
TejasRGitHub d83c805
Adding new file and changes for IAM policy utils
TejasRGitHub b51a428
Corrections
TejasRGitHub fe6c1fe
Adding converter file
TejasRGitHub 2070328
backend linting changes
TejasRGitHub 508f520
Unit test corrections
TejasRGitHub 9d8583d
Correction in share
TejasRGitHub d40faab
Adding comments
TejasRGitHub f78429c
Simplifying interface
TejasRGitHub 7303bba
Fixing few tests
TejasRGitHub 200313b
Linting
TejasRGitHub 0f1d2d8
Merge branch 'main' into gh-884-IAM-policy-splitting
21af992
Change after PR review
TejasRGitHub 73f98b1
Reverting changes made
TejasRGitHub e27b0aa
More changes
TejasRGitHub 8abc4d6
Minor changes
TejasRGitHub 3b4dce2
Removing managed policy from unused gql calls
TejasRGitHub 1f5ec6e
python linting
TejasRGitHub a048fe9
Corrections
TejasRGitHub f48e07f
Naming changes in exceptions
TejasRGitHub 99556b3
Refactoring and corrections
TejasRGitHub 61f616b
Fixing unit tests
TejasRGitHub e84e526
Linting after correcting tests
TejasRGitHub f5825ef
Removing parts not part of this PR
TejasRGitHub 50c906d
Corrections
TejasRGitHub 340d3a2
Merge branch 'main' into gh-884-IAM-policy-splitting
5a29bd1
Merge branch 'main' into gh-884-IAM-policy-splitting
5d283b3
Linting / formatting and fixing tests
TejasRGitHub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,58 @@ | ||
import logging | ||
from botocore.exceptions import ClientError | ||
|
||
from .sts import SessionHelper | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class ServiceQuota: | ||
def __init__(self, account_id, region): | ||
session = SessionHelper.remote_session(accountid=account_id, region=region) | ||
self.client = session.client('service-quotas') | ||
|
||
def list_services(self): | ||
try: | ||
log.info('Fetching services list with service codes in aws account') | ||
services_list = [] | ||
paginator = self.client.get_paginator('list_services') | ||
for page in paginator.paginate(): | ||
services_list.extend(page.get('Services')) | ||
return services_list | ||
except ClientError as e: | ||
if e.response['Error']['Code'] == 'AccessDenied': | ||
raise Exception(f'Data.all Environment Pivot Role does not have permissions to do list_services : {e}') | ||
log.error(f'Failed list services and service codes due to: {e}') | ||
return [] | ||
|
||
def list_service_quota(self, service_code): | ||
try: | ||
log.info('Fetching services quota code in aws account') | ||
service_quota_code_list = [] | ||
paginator = self.client.get_paginator('list_service_quotas') | ||
for page in paginator.paginate(ServiceCode=service_code): | ||
service_quota_code_list.extend(page.get('Quotas')) | ||
log.debug(f'Services quota list: {service_quota_code_list}') | ||
return service_quota_code_list | ||
except ClientError as e: | ||
if e.response['Error']['Code'] == 'AccessDenied': | ||
raise Exception( | ||
f'Data.all Environment Pivot Role does not have permissions to do list_service_quota : {e}' | ||
) | ||
log.error(f'Failed list quota codes to: {e}') | ||
return [] | ||
|
||
def get_service_quota_value(self, service_code, service_quota_code): | ||
try: | ||
log.info( | ||
f'Getting service quota for service code: {service_code} and service quota code: {service_quota_code}' | ||
) | ||
response = self.client.get_service_quota(ServiceCode=service_code, QuotaCode=service_quota_code) | ||
return response['Quota']['Value'] | ||
except ClientError as e: | ||
if e.response['Error']['Code'] == 'AccessDenied': | ||
raise Exception( | ||
f'Data.all Environment Pivot Role does not have permissions to do get_service_quota: {e}' | ||
) | ||
log.error(f'Failed list quota codes to: {e}') | ||
return None |
This file contains hidden or 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 hidden or 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,71 @@ | ||
from typing import Dict, Any, List | ||
from aws_cdk import aws_iam as iam | ||
|
||
from dataall.base.utils.iam_policy_utils import ( | ||
split_policy_statements_in_chunks, | ||
split_policy_with_resources_in_statements, | ||
split_policy_with_mutiple_value_condition_in_statements, | ||
) | ||
|
||
|
||
def convert_from_json_to_iam_policy_statement_with_conditions(iam_policy: Dict[Any, Any]): | ||
return iam.PolicyStatement( | ||
sid=iam_policy.get('Sid'), | ||
effect=iam.Effect.ALLOW if iam_policy.get('Effect').casefold() == 'Allow'.casefold() else iam.Effect.DENY, | ||
actions=_convert_to_array(str, iam_policy.get('Action')), | ||
resources=_convert_to_array(str, iam_policy.get('Resource')), | ||
conditions=iam_policy.get('Condition'), | ||
) | ||
|
||
|
||
def convert_from_json_to_iam_policy_statement(iam_policy: Dict[Any, Any]): | ||
return iam.PolicyStatement( | ||
sid=iam_policy.get('Sid'), | ||
effect=iam.Effect.ALLOW if iam_policy.get('Effect').casefold() == 'Allow'.casefold() else iam.Effect.DENY, | ||
actions=_convert_to_array(str, iam_policy.get('Action')), | ||
resources=_convert_to_array(str, iam_policy.get('Resource')), | ||
) | ||
|
||
|
||
def process_and_split_statements_in_chunks(statements: List[Dict]): | ||
statement_chunks_json: List[List[Dict]] = split_policy_statements_in_chunks(statements) | ||
statements_chunks: List[List[iam.PolicyStatement]] = [] | ||
for statement_js_chunk in statement_chunks_json: | ||
statements: List[iam.PolicyStatement] = [] | ||
for statement in statement_js_chunk: | ||
if statement.get('Condition', None): | ||
statements.append(convert_from_json_to_iam_policy_statement_with_conditions(statement)) | ||
else: | ||
statements.append(convert_from_json_to_iam_policy_statement(statement)) | ||
statements_chunks.append(statements) | ||
return statements_chunks | ||
|
||
|
||
dlpzx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def process_and_split_policy_with_resources_in_statements( | ||
base_sid: str, effect: str, actions: List[str], resources: List[str], condition_dict: Dict = None | ||
): | ||
if condition_dict is not None: | ||
print(f'Condition dictionary is: {condition_dict}') | ||
json_statements = split_policy_with_mutiple_value_condition_in_statements( | ||
base_sid=base_sid, effect=effect, actions=actions, resources=resources, condition_dict=condition_dict | ||
) | ||
else: | ||
json_statements = split_policy_with_resources_in_statements( | ||
base_sid=base_sid, effect=effect, actions=actions, resources=resources | ||
) | ||
iam_statements: [iam.PolicyStatement] = [] | ||
for json_statement in json_statements: | ||
if json_statement.get('Condition', None): | ||
iam_policy_statement = convert_from_json_to_iam_policy_statement_with_conditions(json_statement) | ||
else: | ||
iam_policy_statement = convert_from_json_to_iam_policy_statement(json_statement) | ||
iam_statements.append(iam_policy_statement) | ||
return iam_statements | ||
|
||
|
||
# If item is of item type i.e. single instance if present, then wrap in an array. | ||
# This is helpful at places where array is required even if one element is present | ||
def _convert_to_array(item_type, item): | ||
if isinstance(item, item_type): | ||
return [item] | ||
return item |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.