From de9a39a180ed3efd6b1de200ffdc3b0c594ac8dd Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Sat, 1 May 2021 00:30:14 +0300
Subject: [PATCH] Add IMAP check.

---
 functions.tf |  1 +
 src/imap.py  | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
 create mode 100644 src/imap.py

diff --git a/functions.tf b/functions.tf
index 0c8990c..dd3c36c 100644
--- a/functions.tf
+++ b/functions.tf
@@ -15,6 +15,7 @@ locals {
     "vouch",
     "ssh",
     "smtp",
+    "imap",
   ]
   function_names = [for name in local.functions : "${local.function_name_prefix}-${replace(name, "_", "")}"]
 }
diff --git a/src/imap.py b/src/imap.py
new file mode 100644
index 0000000..f967ed7
--- /dev/null
+++ b/src/imap.py
@@ -0,0 +1,29 @@
+from imaplib import IMAP4_SSL
+from utils import publish
+
+
+def check_imap():
+    """Check the IMAP port."""
+    try:
+        imap = IMAP4_SSL("imap.shore.co.il", 993)
+        if "AUTH=PLAIN" not in imap.capabilities:
+            return [False, "AUTH not in IMAP capabilities."]
+        if imap.noop()[0] != "OK":
+            return [False, "NOOP failed in IMAP connection."]
+        imap.logout()
+    except Exception as e:  # pylint: disable=broad-except,invalid-name
+        print(str(e))
+        return [False, "IMAP failure."]
+    return [True, "IAMP is OK."]
+
+
+def handler(event, context):  # pylint: disable=unused-argument
+    """Lambda event handler."""
+    success, message = check_imap()
+    print(message)
+    if not success:
+        publish(message)
+
+
+if __name__ == "__main__":
+    handler("event", "context")
-- 
GitLab