Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #180 from strongdm/feat/add-email-subaddress
Browse files Browse the repository at this point in the history
Add email subaddressing
  • Loading branch information
camposer authored Dec 7, 2021
2 parents 6edace7 + 343544f commit c4603d1
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/CONFIGURE_ACCESSBOT.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The following variables can be changed at runtime via slack -by an SDM_ADMIN- us
* **SDM_ENABLE_RESOURCES_FUZZY_MATCHING**. Tag to be used for enabling fuzzy matching for resources when a perfect match is not found. Default = true
* **SDM_RESOURCE_GRANT_TIMEOUT_TAG**. Tag to be used for registering the time (in minutes) that a specific resource will be made available for the user.
* **SDM_EMAIL_SLACK_FIELD**. Tag to be used for specifying a SDM email. For further information, please refer to [CONFIGURE_ALTERNATIVE_EMAILS.md](./CONFIGURE_ALTERNATIVE_EMAILS.md).
* **SDM_EMAIL_SUBADDRESS**. Flag to be used for specifying a subaddress for the SDM email (e.g. "[email protected]" becomes "[email protected]" when SDM_EMAIL_SUBADDRESS equals to "sub"). Default = None

See image below for more information:

Expand Down
26 changes: 26 additions & 0 deletions e2e/slack/test_accessbot_slack_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,32 @@ def test_override_email(self, mocked_testbot):
assert "Granting" in granting_message
assert self.override_email in granting_message

class Test_email_subaddress:
account_name_with_subaddress = '[email protected]'
email_subaddress = 'stable'

@pytest.fixture
def mocked_testbot(self, testbot):
config = create_config()
config['SENDER_EMAIL_OVERRIDE'] = None
config['SENDER_NICK_OVERRIDE'] = None
config['EMAIL_SUBADDRESS'] = self.email_subaddress
return inject_config(testbot, config, admins=[f'@{account_name}'])

def test_email_subaddress(self, mocked_testbot):
mocked_testbot._bot.callback_message = MagicMock(side_effect=callback_message_fn(
mocked_testbot._bot,
from_email=account_name,
from_nick=account_name
))
push_access_request(mocked_testbot)
mocked_testbot.push_message(f"yes {access_request_id}")
assert "valid request" in mocked_testbot.pop_message()
assert "access request" in mocked_testbot.pop_message()
granting_message = mocked_testbot.pop_message()
assert "Granting" in granting_message
assert self.account_name_with_subaddress in granting_message

class Test_custom_resource_grant_timeout:
timeout = 1

Expand Down
6 changes: 4 additions & 2 deletions e2e/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def create_config():
'USER_ROLES_TAG': None,
'ENABLE_RESOURCES_FUZZY_MATCHING': True,
'RESOURCE_GRANT_TIMEOUT_TAG': None,
'EMAIL_SLACK_FIELD': None
'EMAIL_SLACK_FIELD': None,
'EMAIL_SUBADDRESS': None
}

class DummyAccount:
Expand Down Expand Up @@ -80,13 +81,14 @@ def sm(msg):
bot.outgoing_message_queue.put(bot.md.convert(msg.body))
return sm

def callback_message_fn(bot, from_email=admin_default_email, approver_is_admin=False):
def callback_message_fn(bot, from_email=admin_default_email, approver_is_admin=False, from_nick=None):
def callback_message(msg):
frm = msg.frm
if approver_is_admin and "yes" in msg.body:
frm._email = admin_default_email
else:
frm._email = from_email
frm._nick = from_nick
msg = Message(
body=msg.body,
frm=frm,
Expand Down
6 changes: 5 additions & 1 deletion plugins/sdm/accessbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ def get_sender_email(self, sender):
override = self.config['SENDER_EMAIL_OVERRIDE']
if override:
return override
return self._platform.get_sender_email(sender)
sender_email = self._platform.get_sender_email(sender)
sdm_email_subaddress = self.config['EMAIL_SUBADDRESS']
if sdm_email_subaddress:
return sender_email.replace('@', f'+{sdm_email_subaddress}@')
return sender_email

def get_user_nick(self, user):
return self._platform.get_user_nick(user)
Expand Down
3 changes: 2 additions & 1 deletion plugins/sdm/config_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
'USER_ROLES_TAG': os.getenv("SDM_USER_ROLES_TAG"),
'RESOURCE_GRANT_TIMEOUT_TAG': os.getenv("SDM_RESOURCE_GRANT_TIMEOUT_TAG"),
'ENABLE_RESOURCES_FUZZY_MATCHING': str(os.getenv("SDM_ENABLE_RESOURCES_FUZZY_MATCHING", 'true')).lower() == 'true',
'EMAIL_SLACK_FIELD': str(os.getenv("SDM_EMAIL_SLACK_FIELD"))
'EMAIL_SLACK_FIELD': str(os.getenv("SDM_EMAIL_SLACK_FIELD")),
'EMAIL_SUBADDRESS': str(os.getenv("SDM_EMAIL_SUBADDRESS"))
}

def get():
Expand Down

0 comments on commit c4603d1

Please sign in to comment.