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

[GT-157] Drop messages from banned dns, and add tests for messages saving to the correct queue #238

Open
wants to merge 13 commits into
base: dev
Choose a base branch
from
Open
31 changes: 31 additions & 0 deletions ssm/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ def run_receiver(protocol, brokers, project, token, cp, log, dn_file):
dns = get_dns(dn_file, log)
ssm.set_dns(dns)

log.info('Fetching banned DNs.')
banned_dns = get_banned_dns(log)
ssm.set_banned_dns(banned_dns)

except Exception as e:
log.fatal('Failed to initialise SSM: %s', e)
log.info(LOG_BREAK)
Expand Down Expand Up @@ -379,3 +383,30 @@ def get_dns(dn_file, log):

log.debug('%s DNs found.', len(dns))
return dns


def get_banned_dns(log):
"""Retrieve the list of banned dns"""
banned_dns_list = []
tofu-rocketry marked this conversation as resolved.
Show resolved Hide resolved
try:
banned_dns_path = cp.get('auth', 'banned-dns')
tofu-rocketry marked this conversation as resolved.
Show resolved Hide resolved
banned_dns_file = os.path.normpath(os.path.expandvars(banned_dns_path))
except ConfigParser.NoOptionError:
banned_dns_file = None
f = None
try:
f = open(banned_dns_file, 'r')
tofu-rocketry marked this conversation as resolved.
Show resolved Hide resolved
lines = f.readlines()
for line in lines:
if line.isspace() or line.strip().startswith('#'):
continue
elif line.strip().startswith('/'):
banned_dns_list.append(line.strip())
else:
log.warning('DN in banned dns list is not in correct format: %s', line)
finally:
Copy link
Member

Choose a reason for hiding this comment

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

Now we have a context manager for the file, we can get rid of the try...finally wrapper (and the `f=None') as the file will close as soon as the handler is exited (whether normally or by exception).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

if f is not None:
f.close()

return banned_dns_list

14 changes: 14 additions & 0 deletions ssm/ssm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def __init__(self, hosts_and_ports, qpath, cert, key, dest=None, listen=None,
self._dest = dest

self._valid_dns = []
self._banned_dns = []
self._pidfile = pidfile

# Used to differentiate between STOMP and AMS methods
Expand Down Expand Up @@ -189,6 +190,10 @@ def set_dns(self, dn_list):
"""Set the list of DNs which are allowed to sign incoming messages."""
self._valid_dns = dn_list

def set_banned_dns(self, banned_dn_list):
"""Set the list of banned dns, so their messages can be dropped."""
self._banned_dns = banned_dn_list

##########################################################################
# Methods called by stomppy
##########################################################################
Expand Down Expand Up @@ -283,8 +288,10 @@ def _handle_msg(self, text):
Namely:
- decrypt if necessary
- verify signature
- drop the message if the sender is a banned dn
- Return plain-text message, signer's DN and an error/None.
"""

if text is None or text == '':
warning = 'Empty text passed to _handle_msg.'
log.warning(warning)
Expand All @@ -307,6 +314,13 @@ def _handle_msg(self, text):
log.error(error)
return None, None, error

# If the message has been sent from a banned dn,
# don't send the message to the reject queue.
# Instead, just drop the message.
if signer in self._banned_dns:
log.info("Message deleted as was sent from banned dn: %s", signer)
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
return

if signer not in self._valid_dns:
warning = 'Signer not in valid DNs list: %s' % signer
log.warning(warning)
Expand Down