Skip to content

Commit 3745daa

Browse files
author
root
committed
Added a 5-minute timeout for TOTP input during authentication
1 parent 7af969a commit 3745daa

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

vertica_python/vertica/connection.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
import uuid
4646
import warnings
4747
import re
48+
import time
49+
import signal
50+
import select
51+
import sys
4852
from collections import deque
4953
from struct import unpack
5054

@@ -990,25 +994,34 @@ def send_startup(totp_value=None):
990994
# ✅ If TOTP not provided initially, allow 3 retry attempts
991995
if not totp:
992996
max_attempts = 3
997+
timeout_seconds = 300 # 5 minutes timeout
993998
for attempt in range(max_attempts):
994999
try:
995-
totp_input = input(f"🔐 Enter TOTP (attempt {attempt+1}/{max_attempts}): ").strip()
996-
if not totp_input:
997-
print("⚠️ TOTP cannot be empty.")
998-
continue
999-
totp = totp_input
1000-
self.close_socket()
1001-
self.socket = self.establish_socket_connection(self.address_list)
1002-
self._logger.info(f"🚀 Retrying with TOTP: '{totp}'")
1003-
# ✅ Re-init required attributes
1004-
self.backend_pid = 0
1005-
self.backend_key = 0
1006-
self.transaction_status = None
1007-
self.session_id = None
1008-
self._logger.debug("✅ Startup message sent with TOTP.")
1009-
# Send new startup message with updated TOTP
1010-
send_startup(totp_value=totp)
1011-
break
1000+
print(f"🔐 Enter TOTP (attempt {attempt+1}/{max_attempts}): ", end="", flush=True)
1001+
ready, _, _ = select.select([sys.stdin], [], [], timeout_seconds)
1002+
if ready:
1003+
totp_input = sys.stdin.readline().strip()
1004+
if not totp_input:
1005+
print("⚠️ TOTP cannot be empty.")
1006+
continue
1007+
totp = totp_input
1008+
self.close_socket()
1009+
self.socket = self.establish_socket_connection(self.address_list)
1010+
self._logger.info(f"🚀 Retrying with TOTP: '{totp}'")
1011+
# ✅ Re-init required attributes
1012+
self.backend_pid = 0
1013+
self.backend_key = 0
1014+
self.transaction_status = None
1015+
self.session_id = None
1016+
self._logger.debug("✅ Startup message sent with TOTP.")
1017+
# Send new startup message with updated TOTP
1018+
send_startup(totp_value=totp)
1019+
break
1020+
else:
1021+
print("⏰ Session timed out. No TOTP entered within time limit.")
1022+
self._logger.error("Session timeout: No TOTP entered within time limit.")
1023+
self.close_socket()
1024+
raise errors.ConnectionError("Session timeout: No TOTP entered within time limit.")
10121025
except (KeyboardInterrupt, EOFError):
10131026
raise errors.ConnectionError("TOTP input cancelled.")
10141027
else:

0 commit comments

Comments
 (0)