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

Commit

Permalink
Merge pull request #784 from Netflix/issue_783_sanitize_account_ident…
Browse files Browse the repository at this point in the history
…ifier

Sanitizing account identifiers.
  • Loading branch information
Patrick Kelley committed Aug 7, 2017
2 parents c79ed93 + 394d984 commit eb328ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
23 changes: 19 additions & 4 deletions security_monkey/account_manager.py
Expand Up @@ -70,6 +70,15 @@ class AccountManager(object):
identifier_label = None
identifier_tool_tip = None

def sanitize_account_identifier(self, identifier):
"""Each account type can determine how to sanitize the account identifier.
By default, will strip any whitespace.
Returns:
identifier stripped of whitespace
"""
return identifier.strip()

def update(self, account_id, account_type, name, active, third_party, notes, identifier, custom_fields=None):
"""
Updates an existing account in the database.
Expand Down Expand Up @@ -103,6 +112,7 @@ def update(self, account_id, account_type, name, active, third_party, notes, ide
account.notes = notes
account.active = active
account.third_party = third_party
account.identifier = self.sanitize_account_identifier(identifier)
self._update_custom_fields(account, custom_fields)

db.session.add(account)
Expand All @@ -118,7 +128,9 @@ def create(self, account_type, name, active, third_party, notes, identifier,
Creates an account in the database.
"""
account_type_result = _get_or_create_account_type(account_type)
account = Account.query.filter(Account.name == name, Account.account_type_id == account_type_result.id).first()
account = Account.query.filter(
Account.name == name,
Account.account_type_id == account_type_result.id).first()

# Make sure the account doesn't already exist:
if account:
Expand All @@ -128,7 +140,9 @@ def create(self, account_type, name, active, third_party, notes, identifier,

account = Account()
account = self._populate_account(account, account_type_result.id, name,
active, third_party, notes, identifier, custom_fields)
active, third_party, notes,
self.sanitize_account_identifier(identifier),
custom_fields)

db.session.add(account)
db.session.commit()
Expand All @@ -137,7 +151,8 @@ def create(self, account_type, name, active, third_party, notes, identifier,
return account

def lookup_account_by_identifier(self, identifier):
query = Account.query.filter(Account.identifier == identifier)
query = Account.query.filter(
Account.identifier == self.sanitize_account_identifier(identifier))

if query.count():
return query.first()
Expand All @@ -158,7 +173,7 @@ def _populate_account(self, account, account_type_id, name, active, third_party,
May be overridden to store additional data
"""
account.name = name
account.identifier = identifier
account.identifier = self.sanitize_account_identifier(identifier)
account.notes = notes
account.active = active
account.third_party = third_party
Expand Down
8 changes: 8 additions & 0 deletions security_monkey/account_managers/aws_account.py
Expand Up @@ -50,3 +50,11 @@ class AWSAccountManager(AccountManager):

def __init__(self):
super(AWSAccountManager, self).__init__()

def sanitize_account_identifier(self, identifier):
"""AWS identifer sanitization will strip and remove any hyphens.
Returns:
stripped identifier with hyphens removed
"""
return identifier.replace('-', '').strip()

0 comments on commit eb328ad

Please sign in to comment.