Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions keepercommander/commands/discoveryrotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,8 @@ class PAMGatewayListCommand(Command):
help='Verbose output')
parser.add_argument('--format', dest='format', action='store', choices=['table', 'json'], default='table',
help='Output format (table, json)')
parser.add_argument('--online', '-o', required=False, default=False, dest='online_only', action='store_true',
help='Show only online gateways')

def get_parser(self):
return PAMGatewayListCommand.parser
Expand All @@ -1408,6 +1410,7 @@ def execute(self, params, **kwargs):
is_force = kwargs.get('is_force')
is_verbose = kwargs.get('is_verbose')
format_type = kwargs.get('format', 'table')
online_only = kwargs.get('online_only', False)

is_router_down = False
krouter_url = router_helper.get_router_url(params)
Expand Down Expand Up @@ -1478,13 +1481,26 @@ def execute(self, params, **kwargs):
connected_controllers_dict[controller.controllerUid] = []
connected_controllers_dict[controller.controllerUid].append(controller)

gateway_counts = {'online': 0, 'offline': 0, 'total': len(enterprise_controllers_all)}

# Process each gateway and handle multiple instances
for c in enterprise_controllers_all:
gateway_uid_bytes = c.controllerUid
gateway_uid_str = utils.base64_url_encode(c.controllerUid)

connected_instances = connected_controllers_dict.get(gateway_uid_bytes, [])

is_online = not is_router_down and len(connected_instances) > 0
if is_router_down:
pass
elif is_online:
gateway_counts['online'] += 1
else:
gateway_counts['offline'] += 1

if online_only and not is_online:
continue

ksm_app_uid_str = utils.base64_url_encode(c.applicationUid)
ksm_app = KSMCommand.get_app_record(params, ksm_app_uid_str)

Expand Down Expand Up @@ -1714,6 +1730,9 @@ def execute(self, params, **kwargs):
else:
result = {"gateways": gateways_data}

if online_only:
result["gateway_counts"] = gateway_counts

return json.dumps(result, indent=2)
else:
# Separate rows into groups: each parent with its instances
Expand Down Expand Up @@ -1746,6 +1765,14 @@ def execute(self, params, **kwargs):
dump_report_data(table, headers, fmt='table', filename="",
row_number=False, column_width=None)

if online_only:
print(
f"\n{bcolors.BOLD}Gateways: "
f"{bcolors.OKGREEN}Online: {gateway_counts['online']}{bcolors.ENDC}, "
f"{bcolors.FAIL}Offline: {gateway_counts['offline']}{bcolors.ENDC}, "
f"Total: {gateway_counts['total']}{bcolors.ENDC}\n"
)


class PAMConfigurationListCommand(Command):
parser = argparse.ArgumentParser(prog='pam config list')
Expand Down
62 changes: 62 additions & 0 deletions unit-tests/pam/test_pam_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,13 @@ def test_parser(self):
self.assertTrue(args.is_verbose)
self.assertTrue(args.is_force)

def test_parser_online(self):
args = self.parser.parse_args(['--online'])
self.assertTrue(args.online_only)

args = self.parser.parse_args(['-o'])
self.assertTrue(args.online_only)

@patch('keepercommander.commands.discoveryrotation.router_get_connected_gateways')
@patch('keepercommander.commands.discoveryrotation.router_helper.get_router_url')
@patch('keepercommander.commands.discoveryrotation.gateway_helper.get_all_gateways')
Expand Down Expand Up @@ -488,6 +495,61 @@ def test_execute_router_down(self, mock_dump_report_data, mock_get_all_gateways,
self.assertTrue(mock_get_all_gateways.called)
self.assertTrue(mock_get_router_url.called)

@patch('keepercommander.commands.discoveryrotation.print')
@patch('keepercommander.commands.discoveryrotation.router_get_connected_gateways')
@patch('keepercommander.commands.discoveryrotation.router_helper.get_router_url')
@patch('keepercommander.commands.discoveryrotation.gateway_helper.get_all_gateways')
@patch('keepercommander.commands.discoveryrotation.KSMCommand.get_app_record')
@patch('keepercommander.commands.discoveryrotation.dump_report_data')
def test_execute_online_only(self, mock_dump_report_data, mock_get_app_record, mock_get_all_gateways,
mock_get_router_url, mock_router_get_connected_gateways, mock_print):
mock_params = create_mock_params()

online_uid = utils.base64_url_decode('controller_uid')
offline_uid = utils.base64_url_decode('offline_uid')

mock_router_get_connected_gateways.return_value.controllers = [
MagicMock(controllerUid=online_uid, version='1.0.0')
]

mock_get_all_gateways.return_value = [
MagicMock(
applicationUid=utils.base64_url_decode('app_uid'),
controllerUid=online_uid,
controllerName='Online Gateway',
deviceName='Device 1',
deviceToken='Token 1',
created=int(datetime.now().timestamp() * 1000),
lastModified=int(datetime.now().timestamp() * 1000),
nodeId='Node 1'
),
MagicMock(
applicationUid=utils.base64_url_decode('app_uid2'),
controllerUid=offline_uid,
controllerName='Offline Gateway',
deviceName='Device 2',
deviceToken='Token 2',
created=int(datetime.now().timestamp() * 1000),
lastModified=int(datetime.now().timestamp() * 1000),
nodeId='Node 2'
)
]

mock_get_app_record.return_value = {
'data_unencrypted': json.dumps({'title': 'App Title'})
}

kwargs = {'is_force': True, 'online_only': True}
self.command.execute(mock_params, **kwargs)

self.assertTrue(mock_dump_report_data.called)
table = mock_dump_report_data.call_args[0][0]
self.assertEqual(len(table), 1)
self.assertIn('Online Gateway', table[0][1])

totals_printed = any('Online: 1' in str(call.args[0]) for call in mock_print.call_args_list)
self.assertTrue(totals_printed)

@patch('keepercommander.commands.discoveryrotation.router_get_connected_gateways')
@patch('keepercommander.commands.discoveryrotation.router_helper.get_router_url')
@patch('keepercommander.commands.discoveryrotation.gateway_helper.get_all_gateways')
Expand Down
Loading