-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Dataset integration tests - S3 Share requests (#1389)
### Feature or Bugfix - Tests ### Detail - module `share_base` - bugfix `delete_env` requires `env_object` not `envUri` - TEMPORARY: hardcoded dataset_uri --> I wait for dataset module ### Relates - #1376 ### Security Please answer the questions below briefly where applicable, or write `N/A`. Based on [OWASP 10](https://owasp.org/Top10/en/). - Does this PR introduce or modify any input fields or queries - this includes fetching data from storage outside the application (e.g. a database, an S3 bucket)? - Is the input sanitized? - What precautions are you taking before deserializing the data you consume? - Is injection prevented by parametrizing queries? - Have you ensured no `eval` or similar functions are used? - Does this PR introduce any functionality or component that requires authorization? - How have you ensured it respects the existing AuthN/AuthZ mechanisms? - Are you logging failed auth attempts? - Are you using or adding any cryptographic features? - Do you use a standard proven implementations? - Are the used keys controlled by the customer? Where are they stored? - Are you introducing any new policies/roles/users? - Have you used the least-privilege principle? How? By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. --------- Co-authored-by: dlpzx <dlpzx@amazon.com> Co-authored-by: Noah Paige <noahpaig@amazon.com> Co-authored-by: Sofia Sazonova <sazonova@amazon.co.uk>
- Loading branch information
1 parent
075b43c
commit 2005863
Showing
24 changed files
with
1,232 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
my_array=("$(aws s3api list-buckets --query 'Buckets[?contains(Name, `session`) == `true`].[Name]' --output text)") | ||
array=("${(@f)my_array}") | ||
for YOUR_BUCKET in "${array[@]}" | ||
do | ||
|
||
aws s3api delete-objects --bucket ${YOUR_BUCKET} \ | ||
--delete "$(aws s3api list-object-versions --bucket ${YOUR_BUCKET} --query='{Objects: Versions[].{Key:Key,VersionId:VersionId}}')" | ||
|
||
aws s3api delete-objects --bucket ${YOUR_BUCKET} \ | ||
--delete "$(aws s3api list-object-versions --bucket ${YOUR_BUCKET} --query='{Objects: DeleteMarkers[].{Key:Key,VersionId:VersionId}}')" | ||
|
||
aws s3api delete-bucket --bucket ${YOUR_BUCKET} | ||
done |
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,37 @@ | ||
import time | ||
from tests_new.integration_tests.utils import poller | ||
|
||
|
||
class AthenaClient: | ||
def __init__(self, session, region): | ||
self._client = session.client('athena', region_name=region) | ||
self._region = region | ||
|
||
def _run_query(self, query, workgroup='primary', output_location=None): | ||
if output_location: | ||
result = self._client.start_query_execution( | ||
QueryString=query, ResultConfiguration={'OutputLocation': output_location} | ||
) | ||
else: | ||
result = self._client.start_query_execution(QueryString=query, WorkGroup=workgroup) | ||
return result['QueryExecutionId'] | ||
|
||
@poller(check_success=lambda state: state not in ['QUEUED', 'RUNNING'], timeout=600, sleep_time=5) | ||
def _wait_for_query(self, query_id): | ||
result = self._client.get_query_execution(QueryExecutionId=query_id) | ||
return result['QueryExecution']['Status']['State'] | ||
|
||
def execute_query(self, query, workgroup='primary', output_location=None): | ||
q_id = self._run_query(query, workgroup, output_location) | ||
return self._wait_for_query(q_id) | ||
|
||
def list_work_groups(self): | ||
result = self._client.list_work_groups() | ||
return [x['Name'] for x in result['WorkGroups']] | ||
|
||
def get_env_work_group(self, env_name): | ||
workgroups = self.list_work_groups() | ||
for workgroup in workgroups: | ||
if env_name in workgroup: | ||
return workgroup | ||
return workgroups[0] if workgroups else None |
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,96 @@ | ||
import json | ||
import logging | ||
import os | ||
|
||
import boto3 | ||
|
||
from dataall.base.aws.parameter_store import ParameterStoreManager | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class IAMClient: | ||
def __init__(self, session=boto3.Session(), region=os.environ.get('AWS_REGION', 'us-east-1')): | ||
self._client = session.client('iam', region_name=region) | ||
self._resource = session.resource('iam', region_name=region) | ||
self._region = region | ||
|
||
def get_role(self, role_name): | ||
try: | ||
role = self._client.get_role(RoleName=role_name) | ||
return role | ||
except Exception as e: | ||
log.info(f'Error occurred: {e}') | ||
return None | ||
|
||
@staticmethod | ||
def get_tooling_account_id(): | ||
session = boto3.Session() | ||
param_client = session.client('ssm', os.environ.get('AWS_REGION', 'us-east-1')) | ||
parameter_path = f"/dataall/{os.environ.get('ENVNAME', 'dev')}/toolingAccount" | ||
toolingAccount = param_client.get_parameter(Name=parameter_path)['Parameter']['Value'] | ||
return toolingAccount | ||
|
||
def create_role(self, account_id, role_name, test_role_name): | ||
policy_doc = { | ||
'Version': '2012-10-17', | ||
'Statement': [ | ||
{ | ||
'Effect': 'Allow', | ||
'Principal': { | ||
'AWS': [ | ||
f'arn:aws:iam::{account_id}:root', | ||
f'arn:aws:iam::{IAMClient.get_tooling_account_id()}:root', | ||
f'arn:aws:sts::{account_id}:assumed-role/{test_role_name}/{test_role_name}', | ||
] | ||
}, | ||
'Action': 'sts:AssumeRole', | ||
'Condition': {}, | ||
} | ||
], | ||
} | ||
try: | ||
role = self._client.create_role( | ||
RoleName=role_name, | ||
AssumeRolePolicyDocument=json.dumps(policy_doc), | ||
Description='Role for Lambda function', | ||
) | ||
return role | ||
except Exception as e: | ||
log.error(e) | ||
raise e | ||
|
||
def create_role_if_not_exists(self, account_id, role_name, test_role_name): | ||
role = self.get_role(role_name) | ||
if role is None: | ||
role = self.create_role(account_id, role_name, test_role_name) | ||
return role | ||
|
||
def get_consumption_role(self, account_id, role_name, test_role_name): | ||
role = self.get_role(role_name) | ||
if role is None: | ||
role = self.create_role(account_id, role_name, test_role_name) | ||
self.put_consumption_role_policy(role_name) | ||
return role | ||
|
||
def put_consumption_role_policy(self, role_name): | ||
self._client.put_role_policy( | ||
RoleName=role_name, | ||
PolicyName='ConsumptionPolicy', | ||
PolicyDocument="""{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Sid": "VisualEditor0", | ||
"Effect": "Allow", | ||
"Action": [ | ||
"s3:*", | ||
"athena:*", | ||
"glue:*", | ||
"lakeformation:GetDataAccess" | ||
], | ||
"Resource": "*" | ||
} | ||
] | ||
}""", | ||
) |
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,19 @@ | ||
import os | ||
|
||
import boto3 | ||
|
||
|
||
class StsClient: | ||
def __init__(self, session=boto3.Session(), region=os.environ.get('AWS_REGION', 'us-east-1')): | ||
self._client = session.client('sts', region_name=region) | ||
self._region = region | ||
|
||
def get_role_session(self, role_arn): | ||
assumed_role_object = self._client.assume_role(RoleArn=role_arn, RoleSessionName='AssumeRole') | ||
credentials = assumed_role_object['Credentials'] | ||
|
||
return boto3.Session( | ||
aws_access_key_id=credentials['AccessKeyId'], | ||
aws_secret_access_key=credentials['SecretAccessKey'], | ||
aws_session_token=credentials['SessionToken'], | ||
) |
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
Oops, something went wrong.