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

feat: add the /secret command for encrypted-message #382

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions app/commands/locales/secret.en-US.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
en-US:
locale_button: "Français"
locale_value: "en-US"
locale_short: "en"
submit: "Submit"
close: "Close"
title: "Share a new secret"
label: "Secret"
placeholder: "Enter your secret here"
ttl: "Time to live"
days: "days"
day: "day"
hours: "hours"
hour: "hour"
minutes: "minutes"
minute: "minute"
error: "There was an error creating your secret"
link_available: "Your secret is available at the following link:"
18 changes: 18 additions & 0 deletions app/commands/locales/secret.fr-FR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fr-FR:
locale_button: "English"
locale_value: "fr-FR"
locale_short: "fr"
submit: "Soumettre"
close: "Fermer"
title: "Partager secret"
label: "Secret"
placeholder: "Entrez votre secret ici"
ttl: "Durée de vie"
days: "jours"
day: "jour"
hours: "heures"
hour: "heure"
minutes: "minutes"
minute: "minute"
error: "Il y avait une erreur en créant votre secret"
link_available: "Votre secret est disponible au lien suivant:"
163 changes: 163 additions & 0 deletions app/commands/secret.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import i18n
import requests
import time
from commands.utils import get_user_locale

i18n.load_path.append("./commands/locales/")

i18n.set("locale", "en-US")
i18n.set("fallback", "en-US")


def secret_command(client, ack, command, body):
ack()
if "user" in body:
user_id = body["user"]["id"]
else:
user_id = body["user_id"]
locale = get_user_locale(user_id, client)
i18n.set("locale", locale)
view = generate_secret_command_modal_view(command, user_id, locale)
client.views_open(trigger_id=body["trigger_id"], view=view)


def secret_view_handler(ack, client, view, logger):
ack()
locale = view["blocks"][0]["elements"][0]["value"]
i18n.set("locale", locale)
secret = view["state"]["values"]["secret_input"]["secret_submission"]["value"]
ttl = view["state"]["values"]["product"]["secret_ttl"]["selected_option"]["value"]

# Encrypted message API
endpoint = "https://encrypted-message.cdssandbox.xyz/encrypt"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super, super minor thing which we don't need to change, but the URL https://encrypted-message.cdssandbox.xyz might be better to live in a variable or secret since you are anyways using it in multiple places throughout your code. Again super minor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed - I hardcoded it for now but should be made into an env in the future

json = {"body": secret, "ttl": int(ttl) + int(time.time())}
response = requests.post(
endpoint, json=json, timeout=10, headers={"Content-Type": "application/json"}
)

try:
id = response.json()["id"]
url = f"https://encrypted-message.cdssandbox.xyz/{i18n.t('secret.locale_short')}/view/{id}"
client.chat_postEphemeral(
channel=view["private_metadata"],
user=view["private_metadata"],
text=f"{i18n.t('secret.link_available')} {url}",
)
except Exception as e:
logger.error(e)
client.chat_postEphemeral(
channel=view["private_metadata"],
user=view["private_metadata"],
text=i18n.t("secret.error"),
)
return


def handle_change_locale_button(ack, client, body):
ack()
if "user" in body:
user_id = body["user"]["id"]
else:
user_id = body["user_id"]
locale = body["actions"][0]["value"]
if locale == "en-US":
locale = "fr-FR"
else:
locale = "en-US"
i18n.set("locale", locale)
command = {
"text": body["view"]["state"]["values"]["secret_input"]["secret_submission"][
"value"
]
}
if command["text"] is None:
command["text"] = ""
view = generate_secret_command_modal_view(command, user_id, locale)
client.views_update(view_id=body["view"]["id"], view=view)


def generate_secret_command_modal_view(command, user_id, locale="en-US"):
ttl_options = [
{"name": "7 " + i18n.t("secret.days"), "value": "604800"},
{"name": "3 " + i18n.t("secret.days"), "value": "259200"},
{"name": "1 " + i18n.t("secret.day"), "value": "86400"},
{"name": "12 " + i18n.t("secret.hours"), "value": "43200"},
{"name": "4 " + i18n.t("secret.hours"), "value": "14400"},
{"name": "1 " + i18n.t("secret.hour"), "value": "3600"},
{"name": "30 " + i18n.t("secret.minutes"), "value": "1800"},
{"name": "5 " + i18n.t("secret.minutes"), "value": "300"},
]

options = [
{
"text": {"type": "plain_text", "text": i["name"]},
"value": i["value"],
}
for i in ttl_options
]
return {
"type": "modal",
"private_metadata": user_id,
"callback_id": "secret_view",
"title": {
"type": "plain_text",
"text": i18n.t("secret.title"),
},
"submit": {
"type": "plain_text",
"text": i18n.t("secret.submit"),
},
"blocks": [
{
"type": "actions",
"block_id": "locale",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": i18n.t("secret.locale_button"),
"emoji": True,
},
"value": locale,
"action_id": "secret_change_locale",
}
],
},
{
"type": "input",
"block_id": "secret_input",
"label": {
"type": "plain_text",
"text": i18n.t("secret.label"),
},
"element": {
"type": "plain_text_input",
"action_id": "secret_submission",
"initial_value": command["text"],
"placeholder": {
"type": "plain_text",
"text": i18n.t("secret.placeholder"),
},
},
},
{
"block_id": "product",
"type": "input",
"element": {
"type": "static_select",
"placeholder": {
"type": "plain_text",
"text": i18n.t("secret.ttl"),
},
"options": options,
"action_id": "secret_ttl",
},
"label": {
"type": "plain_text",
"text": i18n.t("secret.ttl"),
"emoji": True,
},
},
],
}
7 changes: 6 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from slack_bolt.adapter.socket_mode import SocketModeHandler
from slack_bolt import App
from dotenv import load_dotenv
from commands import atip, aws, incident, sre, role
from commands import atip, aws, incident, secret, sre, role
from commands.helpers import incident_helper, webhook_helper
from server import bot_middleware, server

Expand Down Expand Up @@ -60,6 +60,11 @@ def main(bot):
bot.action("archive_channel")(incident_helper.archive_channel_action)
bot.view("view_save_incident_roles")(incident_helper.save_incident_roles)

# Register Secret command
bot.command(f"/{PREFIX}secret")(secret.secret_command)
bot.action("secret_change_locale")(secret.handle_change_locale_button)
bot.view("secret_view")(secret.secret_view_handler)

# Register SRE events
bot.command(f"/{PREFIX}sre")(sre.sre_command)

Expand Down
Loading
Loading