Skip to content

Commit ca4feb6

Browse files
committed
Change to send slack invites only in production
1 parent 14821ba commit ca4feb6

4 files changed

Lines changed: 39 additions & 8 deletions

File tree

pybot/endpoints/api/slack_api.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
from slack import ROOT_URL
55
from slack.exceptions import SlackAPIError
66

7-
from pybot.endpoints.api.utils import _slack_info_from_email, handle_slack_invite_error
7+
from pybot.endpoints.api.utils import (
8+
_slack_info_from_email,
9+
handle_slack_invite_error,
10+
production_only,
11+
)
812
from pybot.plugins import APIPlugin
913
from pybot.plugins.api.request import SlackApiRequest
1014

@@ -32,13 +36,15 @@ async def verify(request: SlackApiRequest, app: SirBot) -> dict:
3236
return {"exists": False}
3337

3438

39+
@production_only
3540
async def invite(request: SlackApiRequest, app: SirBot):
3641
"""
3742
Pulls an email out of the querystring and sends it an invite
3843
to the slack team
3944
4045
:return: The request response from slack
4146
"""
47+
4248
admin_slack = app.plugins["admin_slack"].api
4349
slack = app.plugins["slack"].api
4450
body = await request.json()

pybot/endpoints/api/utils.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
import logging
2+
from typing import Optional
3+
4+
from slack import ROOT_URL
15
from slack.exceptions import SlackAPIError
2-
from slack.methods import Methods
36
from slack.io.abc import SlackAPI
4-
from slack import ROOT_URL
5-
from typing import Optional
7+
from slack.methods import Methods
68

7-
from pybot.endpoints.slack.utils import MODERATOR_CHANNEL
9+
from pybot.endpoints.slack.utils import MODERATOR_CHANNEL, PYBOT_ENV
810
from pybot.endpoints.slack.utils.action_messages import (
911
TICKET_OPTIONS,
1012
not_claimed_attachment,
1113
)
14+
from pybot.plugins.api.request import SlackApiRequest
15+
16+
logger = logging.getLogger(__name__)
1217

1318

1419
async def _slack_info_from_email(
@@ -77,3 +82,24 @@ async def handle_slack_invite_error(email, error, slack):
7782
}
7883

7984
return await slack.query(Methods.CHAT_POST_MESSAGE, response)
85+
86+
87+
def production_only(func):
88+
"""
89+
Decorator for functions that shouldn't be called unless in
90+
production environment.
91+
92+
Used to avoid doing things like sending slack workspace invites to
93+
staging environments.
94+
"""
95+
96+
def not_prod(request: SlackApiRequest, app):
97+
logger.info(
98+
f"Received request on staging to {request.request.raw_path}. Returning 200"
99+
)
100+
return {"ok": True, "details": "Development environment, returning 200"}
101+
102+
if PYBOT_ENV != "PRODUCTION":
103+
return not_prod
104+
105+
return func

pybot/endpoints/slack/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
PYBACK_TOKEN = os.environ.get("PYBACK_TOKEN") or "token"
1616
PORT = os.environ.get("SIRBOT_PORT", 5000)
1717
HOST = os.environ.get("SIRBOT_ADDR", "0.0.0.0")
18+
PYBOT_ENV = os.environ.get("PYBOT_ENV", "dev")
1819
BOT_URL = "https://github.com/OperationCode/operationcode-pybot"
1920

2021
slack_configs = {

pybot/plugins/api/endpoints.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ async def slack_api(request):
1515
try:
1616
slack_request = SlackApiRequest.from_request(request)
1717
except FailedVerification:
18-
logger.info(
19-
f"Failed verification to API route {request.url} from {request.remote}"
20-
)
18+
logger.info(f"Failed verification to API route {request.url}.")
2119
return Response(status=401)
2220

2321
futures = list(_dispatch(api_plugin.routers["slack"], slack_request, request.app))

0 commit comments

Comments
 (0)