Postbox: refactor smtp tools
This commit is contained in:
71
scripts/smtp-convert-secret-key-to-password.py
Normal file
71
scripts/smtp-convert-secret-key-to-password.py
Normal file
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
SMTP Password Generator for Yandex Cloud Postbox
|
||||
|
||||
Этот скрипт конвертирует Secret Access Key в SMTP пароль для использования
|
||||
с Yandex Cloud Postbox сервисом. Скрипт реализует алгоритм AWS4 подписи
|
||||
для генерации корректного SMTP пароля.
|
||||
|
||||
Yandex Cloud Postbox использует AWS-совместимый API, и для SMTP аутентификации
|
||||
требуется специальный пароль, который генерируется из Secret Access Key
|
||||
с использованием определенного алгоритма подписи.
|
||||
|
||||
Примеры использования:
|
||||
|
||||
python3 smtp-convert-secret-key-to-password.py "YourSecretAccessKey123"
|
||||
|
||||
Требования:
|
||||
- Python 3.x
|
||||
- Стандартные библиотеки: hmac, hashlib, base64, argparse, sys
|
||||
|
||||
Автор: Yandex.Cloud Team
|
||||
Ссылка: https://yandex.cloud/ru/docs/postbox/operations/send-email
|
||||
"""
|
||||
|
||||
import hmac
|
||||
import hashlib
|
||||
import base64
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
|
||||
# These values are required to calculate the signature. Do not change them.
|
||||
DATE = "20230926"
|
||||
SERVICE = "postbox"
|
||||
MESSAGE = "SendRawEmail"
|
||||
REGION = "ru-central1"
|
||||
TERMINAL = "aws4_request"
|
||||
VERSION = 0x04
|
||||
|
||||
|
||||
def sign(key, msg):
|
||||
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
|
||||
|
||||
|
||||
def calculate_key(secret_access_key):
|
||||
signature = sign(("AWS4" + secret_access_key).encode("utf-8"), DATE)
|
||||
signature = sign(signature, REGION)
|
||||
signature = sign(signature, SERVICE)
|
||||
signature = sign(signature, TERMINAL)
|
||||
signature = sign(signature, MESSAGE)
|
||||
signature_and_version = bytes([VERSION]) + signature
|
||||
smtp_password = base64.b64encode(signature_and_version)
|
||||
return smtp_password.decode("utf-8")
|
||||
|
||||
|
||||
def main():
|
||||
if sys.version_info[0] < 3:
|
||||
raise Exception("Must be using Python 3")
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Convert a Secret Access Key to an SMTP password."
|
||||
)
|
||||
parser.add_argument("secret", help="The Secret Access Key to convert.")
|
||||
args = parser.parse_args()
|
||||
|
||||
print(calculate_key(args.secret))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user