Skip to content

Commit

Permalink
Thycotic import: send keep-alive periodically. KC-767
Browse files Browse the repository at this point in the history
  • Loading branch information
sk-keeper committed Apr 5, 2024
1 parent 4655557 commit dd853fa
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions keepercommander/importer/thycotic/thycotic.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from ..importer import (BaseImporter, BaseDownloadMembership, Record, SharedFolder, Folder, Attachment, Permission,
RecordField, BytesAttachment, Team, BaseDownloadRecordType, RecordType, RecordTypeField)
from ...params import KeeperParams
from ... import record_types, vault
from ... import record_types, vault, api


class ThycoticMixin:
Expand Down Expand Up @@ -214,6 +214,8 @@ def get_folders(auth, skip_permissions=False): # type: (ThycoticAuth, bool
class ThycoticImporter(BaseImporter, ThycoticMixin):
def __init__(self):
super().__init__()
self._last_keep_alive = int(datetime.datetime.utcnow().timestamp())
self._keep_alive_period = 30 * 60

def support_folder_filter(self):
return True
Expand All @@ -232,8 +234,17 @@ def pop_field_value(fields, key): # type: (Dict[str, Dict[str, str]], str) -> s
def request_totp():
return input('Enter TOTP code: '.rjust(25))

def _send_keep_alive_if_needed(self, params): # type: (KeeperParams) -> None
now = int(datetime.datetime.utcnow().timestamp())
if (now - self._last_keep_alive) > self._keep_alive_period:
self._last_keep_alive = now
try:
api.send_keepalive(params)
except:
pass

def do_import(self, filename, **kwargs):
# type: (BaseImporter, str, ...) -> Iterable[Union[Record, SharedFolder]]
# type: (ThycoticImporter, str, ...) -> Iterable[Union[Record, SharedFolder]]
loaded_record_types = {}
params = kwargs.get('params')
if isinstance(params, KeeperParams):
Expand Down Expand Up @@ -304,6 +315,7 @@ def do_import(self, filename, **kwargs):
pass
print(f'Loaded {len(totp_codes)} code(s)', flush=True)

self._send_keep_alive_if_needed()
folders = ThycoticImporter.get_folders(auth, skip_permissions=True)
filter_folder = kwargs.get('filter_folder')
if filter_folder:
Expand Down Expand Up @@ -334,6 +346,7 @@ def do_import(self, filename, **kwargs):
else:
secrets_ids = [x['id'] for x in auth.thycotic_search(f'/v1/secrets/lookup')]

self._send_keep_alive_if_needed()
print(f'Loading {len(secrets_ids)} Records ', flush=True, end='')
secrets = []
for secret_id in secrets_ids:
Expand All @@ -343,7 +356,10 @@ def do_import(self, filename, **kwargs):
secrets.append(secret)
if len(secrets) % 10 == 9:
print('.', flush=True, end='')
if len(secrets) % 100 == 99:
self._send_keep_alive_if_needed()

self._send_keep_alive_if_needed()
for secret in secrets:
record = Record()
record.title = secret.get('name', '')
Expand Down

0 comments on commit dd853fa

Please sign in to comment.