Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
remove dots from the start or end of a key (#1650)
Browse files Browse the repository at this point in the history
* remove dots from the start or end of a key

* updating the comment text

* re-envisioned code per @pwnbus' suggestion

* addressing review comments

* adds logic to check for null keys
  • Loading branch information
Phrozyn committed Jul 16, 2020
1 parent a8a1663 commit e219435
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
35 changes: 35 additions & 0 deletions mq/plugins/remove_dots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# Copyright (c) 2014 Mozilla Corporation


class message(object):

def __init__(self):
'''
takes an incoming message and checks for dots at the
start or end of the key and removes them
'''

self.registration = ['cloudtrail']
self.priority = 5

def onMessage(self, message, metadata):
def renameKeysToRemoveDots(message):
if isinstance(message, dict):
message_keys = list(message.keys())
for key in message_keys:
if len(key) > 0 and (key[0] == '.' or key[-1] == '.'):
new_key = key.replace(".", "")
if new_key != key:
message[new_key] = message.pop(key)
if isinstance(message.get(key), (dict, list)):
message[key] = renameKeysToRemoveDots(message[key])
elif isinstance(message, list):
for item in message:
item = renameKeysToRemoveDots(item)
return message

message = renameKeysToRemoveDots(message)
return (message, metadata)
103 changes: 103 additions & 0 deletions tests/mq/plugins/test_remove_dots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# Copyright (c) 2017 Mozilla Corporation

from mq.plugins.remove_dots import message


class TestRemoveDotsPlugin():
def setup(self):
self.plugin = message()

def test_first_dot_removal(self):
msg = {
'source': 'cloudtrail',
'details': {
'responseelements': {
'fileuploadurls': {
'.ds_store': 'astringvalue',
'js/app.js': 'astringvalue'
}
}
}
}
(retmessage, retmeta) = self.plugin.onMessage(msg, {})

expected_message = {
'source': 'cloudtrail',
'details': {
'responseelements': {
'fileuploadurls': {
'ds_store': 'astringvalue',
'js/app.js': 'astringvalue'
}
}
}
}
assert retmessage == expected_message
assert retmeta == {}

def test_last_dot_removal(self):
msg = {
'source': 'cloudtrail',
'details': {
'responseelements': {
'fileuploadurls': {
'ds_store.': 'astringvalue',
'js/app.js': 'astringvalue'
}
}
}
}
(retmessage, retmeta) = self.plugin.onMessage(msg, {})

expected_message = {
'source': 'cloudtrail',
'details': {
'responseelements': {
'fileuploadurls': {
'ds_store': 'astringvalue',
'js/app.js': 'astringvalue'
}
}
}
}
assert retmessage == expected_message
assert retmeta == {}

def test_list_dot_removal(self):
msg = {
'source': 'cloudtrail',
'details': {
'responseelements': {
'fileuploadurls': {
'ds_store.': 'astringvalue',
'js/app.js': 'astringvalue'
},
'.randomlist.': [
'weird_case',
'random_case',
],
}
}
}
(retmessage, retmeta) = self.plugin.onMessage(msg, {})

expected_message = {
'source': 'cloudtrail',
'details': {
'responseelements': {
'fileuploadurls': {
'ds_store': 'astringvalue',
'js/app.js': 'astringvalue'
},
'randomlist': [
'weird_case',
'random_case',
],
}
}
}
assert retmessage == expected_message
assert retmeta == {}

0 comments on commit e219435

Please sign in to comment.