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

30 changes: 28 additions & 2 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
- send an error message if the message wasn't sent from a valid 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,10 +314,23 @@ def _handle_msg(self, text):
log.error(error)
return None, None, error

if signer not in self._valid_dns:
# If the message has been sent from a banned DN,
# set a specific error message that can be
# checked for later.
if signer in self._banned_dns:
warning = 'Signer is in the banned DNs list'
log.warning(warning)
return None, signer, warning

# Else, if the signer is not in valid DNs list,
# but also not a banned dn,
# set a specific error message
elif signer not in self._valid_dns:
warning = 'Signer not in valid DNs list: %s' % signer
log.warning(warning)
return None, signer, warning

# Else, the message has been sent from a valid DN
else:
log.info('Valid signer: %s', signer)

Expand All @@ -320,9 +340,15 @@ def _save_msg_to_queue(self, body, empaid):
"""Extract message contents and add to the accept or reject queue."""
extracted_msg, signer, err_msg = self._handle_msg(body)
try:
# If the warning states the message was sent from a banned DN,
# don't send the message to the reject queue.
# Instead, drop the message (don't send it to any queue)
if err_msg == "Signer is in the banned DNs list":
log.info("Message dropped as was sent from a banned dn: %s", signer)
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved

# If the message is empty or the error message is not empty
# then reject the message.
if extracted_msg is None or err_msg is not None:
elif extracted_msg is None or err_msg is not None:
if signer is None: # crypto failed
signer = 'Not available.'
elif extracted_msg is not None:
Expand Down