소스 검색

log the failed encrypted content to debug

Son NK 5 년 전
부모
커밋
9603683c18
1개의 변경된 파일8개의 추가작업 그리고 0개의 파일을 삭제
  1. 8 0
      app/pgp_utils.py

+ 8 - 0
app/pgp_utils.py

@@ -3,6 +3,8 @@ from io import BytesIO
 import gnupg
 import gnupg
 
 
 from app.config import GNUPGHOME
 from app.config import GNUPGHOME
+from app.log import LOG
+from app.utils import random_string
 
 
 gpg = gnupg.GPG(gnupghome=GNUPGHOME)
 gpg = gnupg.GPG(gnupghome=GNUPGHOME)
 gpg.encoding = "utf-8"
 gpg.encoding = "utf-8"
@@ -24,6 +26,12 @@ def load_public_key(public_key: str) -> str:
 def encrypt_file(data: BytesIO, fingerprint: str) -> str:
 def encrypt_file(data: BytesIO, fingerprint: str) -> str:
     r = gpg.encrypt_file(data, fingerprint, always_trust=True)
     r = gpg.encrypt_file(data, fingerprint, always_trust=True)
     if not r.ok:
     if not r.ok:
+        # save the content for debugging
+        random_file_name = random_string(20) + ".eml"
+        full_path = f"/tmp/{random_file_name}"
+        with open(full_path, "wb") as f:
+            f.write(data.getbuffer())
+        LOG.error("Log to %s", full_path)
         raise PGPException("Cannot encrypt")
         raise PGPException("Cannot encrypt")
 
 
     return str(r)
     return str(r)