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

Add waf v2 logs #23397

Open
wants to merge 4 commits into
base: epic/23361-migrate-aws-waf-v2
Choose a base branch
from

Conversation

javiersanchz
Copy link
Member

Related issue
#22572

Description

This PR closes #22572. It modified WAF to implement WAF v2 logs and updated related unit tests.

Test
(unittest-env) wazuh@javier:~/Git/wazuh$ PYTHONPATH=/home/wazuh/Git/wazuh/api:/home/wazuh/Git/wazuh/framework python3 -m pytest wodles/aws/tests/test_waf.py
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.10.12, pytest-7.3.1, pluggy-1.4.0
rootdir: /home/wazuh/Git/wazuh/wodles/aws/tests
configfile: pytest.ini
plugins: anyio-4.3.0, aiohttp-1.0.4, trio-0.8.0, html-2.1.1, metadata-3.1.0, asyncio-0.18.1, tavern-1.23.5
asyncio: mode=auto
collected 15 items                                                                                                                                                                                                

wodles/aws/tests/test_waf.py ...............                                                                                                                                                                [100%]

=============================================================================================== 15 passed in 0.27s ================================================================================================
(unittest-env) wazuh@javier:~/Git/wazuh$ PYTHONPATH=/home/wazuh/Git/wazuh/api:/home/wazuh/Git/wazuh/framework python3 -m pytest wodles/aws/tests
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.10.12, pytest-7.3.1, pluggy-1.4.0
rootdir: /home/wazuh/Git/wazuh/wodles/aws/tests
configfile: pytest.ini
plugins: anyio-4.3.0, aiohttp-1.0.4, trio-0.8.0, html-2.1.1, metadata-3.1.0, asyncio-0.18.1, tavern-1.23.5
asyncio: mode=auto
collected 598 items                                                                                                                                                                                               

wodles/aws/tests/test_aws_bucket.py ....................................................................................................................................................................... [ 27%]
....................................                                                                                                                                                                        [ 33%]
wodles/aws/tests/test_aws_s3.py ....................                                                                                                                                                        [ 37%]
wodles/aws/tests/test_aws_service.py ....                                                                                                                                                                   [ 37%]
wodles/aws/tests/test_cloudtrail.py ..                                                                                                                                                                      [ 38%]
wodles/aws/tests/test_cloudwatchlogs.py .....................................................                                                                                                               [ 47%]
wodles/aws/tests/test_config.py ..............................................................................                                                                                              [ 60%]
wodles/aws/tests/test_guardduty.py .................                                                                                                                                                        [ 63%]
wodles/aws/tests/test_inspector.py ......                                                                                                                                                                   [ 64%]
wodles/aws/tests/test_load_balancers.py ............                                                                                                                                                        [ 66%]
wodles/aws/tests/test_s3_log_handler.py ................                                                                                                                                                    [ 68%]
wodles/aws/tests/test_server_access.py .................................                                                                                                                                    [ 74%]
wodles/aws/tests/test_sqs_message_processor.py ........                                                                                                                                                     [ 75%]
wodles/aws/tests/test_sqs_queue.py .......                                                                                                                                                                  [ 76%]
wodles/aws/tests/test_tools.py ..................................                                                                                                                                           [ 82%]
wodles/aws/tests/test_umbrella.py ......                                                                                                                                                                    [ 83%]
wodles/aws/tests/test_vpcflow.py .....................                                                                                                                                                      [ 86%]
wodles/aws/tests/test_waf.py ...............                                                                                                                                                                [ 89%]
wodles/aws/tests/test_wazuh_integration.py ...............................................................                                                                                                  [100%]

=============================================================================================== 598 passed in 2.84s ===============================================================================================

@javiersanchz javiersanchz self-assigned this May 14, 2024
@javiersanchz javiersanchz linked an issue May 14, 2024 that may be closed by this pull request
4 tasks
wodles/aws/buckets_s3/waf.py Outdated Show resolved Hide resolved
wodles/aws/buckets_s3/waf.py Outdated Show resolved Hide resolved
wodles/aws/buckets_s3/waf.py Outdated Show resolved Hide resolved
wodles/aws/tests/test_waf.py Show resolved Hide resolved
Copy link
Member

@GGP1 GGP1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@Selutario Selutario left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍 just some questions/requests.

wodles/aws/buckets_s3/waf.py Show resolved Hide resolved
wodles/aws/buckets_s3/waf.py Outdated Show resolved Hide resolved
Comment on lines 41 to 42
def check_waf_type(self):
try:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a docstring here to describe this method since it could be a bit confusing. What does it mean when it returns True? Is it native or using Kinesis? and what when it returns False? It can be inferred, but it is better to explain it.


def check_waf_type(self):
try:
return 'CommonPrefixes' in self.client.list_objects_v2(Bucket=self.bucket, Prefix=f'{self.prefix}AWSLogs', Delimiter='/', MaxKeys=1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too long line

Suggested change
return 'CommonPrefixes' in self.client.list_objects_v2(Bucket=self.bucket, Prefix=f'{self.prefix}AWSLogs', Delimiter='/', MaxKeys=1)
return 'CommonPrefixes' in self.client.list_objects_v2(Bucket=self.bucket, Prefix=f'{self.prefix}AWSLogs',
Delimiter='/', MaxKeys=1)


def get_full_prefix(self, account_id, account_region, acl_name=None):
if self.type == WAF_NATIVE:
path = self.client.list_objects_v2(Bucket=self.bucket)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an imported module named path, it is usually better to choose a different name the variable so it doesn't shadow the outer scope

Suggested change
path = self.client.list_objects_v2(Bucket=self.bucket)
bucket_path = self.client.list_objects_v2(Bucket=self.bucket)

Comment on lines 94 to 98
if 'Contents' in path:
for obj in path['Contents']:
log_key = obj['Key']
parts = log_key.split("/")
acl_name = parts[parts.index("WAFLogs") + 2]
Copy link
Member

@Selutario Selutario May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to better understand what this is for. Would it work if only the first object in path['Contents'] is used? As it is now, it still iterates all items inside path['Contents'], but only uses the last one. In this part:

acl_name = parts[parts.index("WAFLogs") + 2]

only the parts.index("WAFLogs") of the last object found in the loop will be used. Additionally, if Contents is empty, the loop will not iterate and parts will be undefined.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update

As we talked about in the huddle, with the new change, access the last Key of Contents to obtain the acl_name

Comment on lines 109 to 115
def iter_regions_and_accounts(self, account_id, regions):
if self.type == WAF_NATIVE:
AWSBucket.iter_regions_and_accounts(self, account_id, regions)
else:
print(WAF_DEPRECATED_MESSAGE.format(release="5.0", url=WAF_URL))
self.check_prefix = True
AWSCustomBucket.iter_regions_and_accounts(self, account_id, regions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def iter_regions_and_accounts(self, account_id, regions):
if self.type == WAF_NATIVE:
AWSBucket.iter_regions_and_accounts(self, account_id, regions)
else:
print(WAF_DEPRECATED_MESSAGE.format(release="5.0", url=WAF_URL))
self.check_prefix = True
AWSCustomBucket.iter_regions_and_accounts(self, account_id, regions)
def iter_regions_and_accounts(self, account_id, regions):
if self.type == WAF_NATIVE:
AWSBucket.iter_regions_and_accounts(self, account_id, regions)
else:
print(WAF_DEPRECATED_MESSAGE.format(release="5.0", url=WAF_URL))
self.check_prefix = True
AWSCustomBucket.iter_regions_and_accounts(self, account_id, regions)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Modify AWS module to process logs from WAF v2
3 participants