Skip to content

Commit

Permalink
securedrop-admin: when a journalist key is present, the email is requ…
Browse files Browse the repository at this point in the history
…ired
  • Loading branch information
Loic Dachary committed Apr 3, 2018
1 parent a30499b commit 0713b76
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
22 changes: 22 additions & 0 deletions admin/securedrop_admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class FingerprintException(Exception):
pass


class JournalistAlertEmailException(Exception):
pass


class SiteConfig(object):

class ValidateNotEmpty(Validator):
Expand Down Expand Up @@ -341,6 +345,7 @@ def update_config(self):
self.config.update(self.user_prompt_config())
self.save()
self.validate_gpg_keys()
self.validate_journalist_alert_email()
return True

def user_prompt_config(self):
Expand Down Expand Up @@ -412,6 +417,23 @@ def validate_gpg_keys(self):
"the public key {}".format(public_key))
return True

def validate_journalist_alert_email(self):
if (self.config['journalist_alert_gpg_public_key'] == '' and
self.config['journalist_gpg_fpr'] == ''):
return True

class Document(object):
def __init__(self, text):
self.text = text

try:
SiteConfig.ValidateEmail().validate(Document(
self.config['journalist_alert_email']))
except ValidationError as e:
raise JournalistAlertEmailException(
"journalist alerts email: " + e.message)
return True

def exists(self):
return os.path.exists(self.args.site_config)

Expand Down
35 changes: 35 additions & 0 deletions admin/tests/test_securedrop-admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,41 @@ def test_validate_gpg_key(self, caplog):
site_config.validate_gpg_keys()
assert 'FAIL does not match' in e.value.message

def test_journalist_alert_email(self):
args = argparse.Namespace(site_config='INVALID',
ansible_path='tests/files',
app_path=dirname(__file__))
site_config = securedrop_admin.SiteConfig(args)
site_config.config = {
'journalist_alert_gpg_public_key':
'',

'journalist_gpg_fpr':
'',
}
assert site_config.validate_journalist_alert_email()
site_config.config = {
'journalist_alert_gpg_public_key':
'test_journalist_key.pub',

'journalist_gpg_fpr':
'65A1B5FF195B56353CC63DFFCC40EF1228271441',
}
site_config.config['journalist_alert_email'] = ''
with pytest.raises(
securedrop_admin.JournalistAlertEmailException) as e:
site_config.validate_journalist_alert_email()
assert 'not be empty' in e.value.message

site_config.config['journalist_alert_email'] = 'bademail'
with pytest.raises(
securedrop_admin.JournalistAlertEmailException) as e:
site_config.validate_journalist_alert_email()
assert 'Must contain a @' in e.value.message

site_config.config['journalist_alert_email'] = '[email protected]'
assert site_config.validate_journalist_alert_email()

@mock.patch('securedrop_admin.SiteConfig.validated_input',
side_effect=lambda p, d, v, t: d)
@mock.patch('securedrop_admin.SiteConfig.save')
Expand Down

0 comments on commit 0713b76

Please sign in to comment.