Skip to content

Commit

Permalink
Get rid of wheels
Browse files Browse the repository at this point in the history
  • Loading branch information
sk-keeper committed Jan 2, 2025
1 parent 43e9c9e commit 3a7245f
Show file tree
Hide file tree
Showing 34 changed files with 8,522 additions and 15 deletions.
5 changes: 4 additions & 1 deletion keepercommander/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
# Contact: ops@keepersecurity.com
#

__version__ = '17.0.0'
import keepercommander.keeper_dag as keeper_dag
import keepercommander.discovery_common as discovery_common

__version__ = '17.0.2'
3 changes: 3 additions & 0 deletions keepercommander/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
import re
import shlex
import sys

from pathlib import Path

from . import __version__
from . import cli
from .params import KeeperParams
from .config_storage import loader



def get_params_from_config(config_filename=None, launched_with_shortcut=False): # type: (Optional[str], bool) -> KeeperParams
if os.getenv("KEEPER_COMMANDER_DEBUG"):
logging.getLogger().setLevel(logging.DEBUG)
Expand Down
5 changes: 5 additions & 0 deletions keepercommander/discovery_common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Discovery Common

Python code that is used by the Gateway/KDNRM and Commander.

This is common code to interact with the DAG.
Empty file.
1 change: 1 addition & 0 deletions keepercommander/discovery_common/__version__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '1.0.26'
33 changes: 33 additions & 0 deletions keepercommander/discovery_common/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This should the relationship between Keeper Vault record
RECORD_LINK_GRAPH_ID = 0

# The rules
DIS_RULES_GRAPH_ID = 10

# The discovery job history
DIS_JOBS_GRAPH_ID = 11

# Discovery infrastructure
DIS_INFRA_GRAPH_ID = 12

# The user-to-services graph
USER_SERVICE_GRAPH_ID = 13

PAM_DIRECTORY = "pamDirectory"
PAM_DATABASE = "pamDatabase"
PAM_MACHINE = "pamMachine"
PAM_USER = "pamUser"

LOCAL_USER = "local"

# The record types to process.
# The order defined the order the user will be presented the new discovery objects.
# The sort defined how the discovery objects for a record type are sorted and presented.
# Cloud-based users are presented first, then directories second.
# We want to prompt about users that may appear on machines before processing the machine.
VERTICES_SORT_MAP = {
PAM_USER: {"order": 1, "sort": "sort_infra_name", "item": "DiscoveryUser", "key": "user"},
PAM_DIRECTORY: {"order": 1, "sort": "sort_infra_name", "item": "DiscoveryDirectory", "key": "host_port"},
PAM_MACHINE: {"order": 2, "sort": "sort_infra_host", "item": "DiscoveryMachine", "key": "host"},
PAM_DATABASE: {"order": 3, "sort": "sort_infra_host", "item": "DiscoveryDatabase", "key": "host_port"},
}
121 changes: 121 additions & 0 deletions keepercommander/discovery_common/dag_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from __future__ import annotations
from .constants import VERTICES_SORT_MAP
from .types import DiscoveryObject
import logging
import functools
import re
from typing import List, Optional, Union, TYPE_CHECKING

Logger = Union[logging.RootLogger, logging.Logger]
if TYPE_CHECKING:
from keeper_dag.vertex import DAGVertex


def sort_infra_name(vertices: List[DAGVertex]) -> List[DAGVertex]:
"""
Sort the vertices by name in ascending order.
"""

def _sort(t1: DAGVertex, t2: DAGVertex):
t1_name = t1.content_as_dict.get("name")
t2_name = t2.content_as_dict.get("name")
if t1_name < t2_name:
return -1
elif t1_name > t2_name:
return 1
else:
return 0

return sorted(vertices, key=functools.cmp_to_key(_sort))


def sort_infra_host(vertices: List[DAGVertex]) -> List[DAGVertex]:
"""
Sort the vertices by host name.
Host name should appear first in ascending order.
IP should appear second in ascending order.
"""

def _is_ip(host: str) -> bool:
if re.match(r'^\d+\.\d+\.\d+\.\d+', host) is not None:
return True
return False

def _make_ip_number(ip: str) -> int:
ip_port = ip.split(":")
parts = ip_port[0].split(".")
value = ""
for part in parts:
value += part.zfill(3)
return int(value)

def _sort(t1: DAGVertex, t2: DAGVertex):
t1_name = t1.content_as_dict.get("name")
t2_name = t2.content_as_dict.get("name")

# Both names are ip addresses
if _is_ip(t1_name) and _is_ip(t2_name):
t1_num = _make_ip_number(t1_name)
t2_num = _make_ip_number(t2_name)

if t1_num < t2_num:
return -1
elif t1_num > t2_num:
return 1
else:
return 0

# T1 is an IP, T2 is a host name
elif _is_ip(t1_name) and not _is_ip(t2_name):
return 1
# T2 is not an IP and T2 is an IP
elif not _is_ip(t1_name) and _is_ip(t2_name):
return -1
# T1 and T2 are host name
else:
if t1_name < t2_name:
return -1
elif t1_name > t2_name:
return 1
else:
return 0

return sorted(vertices, key=functools.cmp_to_key(_sort))


def sort_infra_vertices(current_vertex: DAGVertex, logger: Optional[Logger] = None) -> dict:

if logger is None:
logger = logging.getLogger()

# Make a map, record type to list of vertices (of that record type)
record_type_to_vertices_map = {k: [] for k, v in VERTICES_SORT_MAP.items()}

# Collate the vertices into a record type lookup.
vertices = current_vertex.has_vertices()
logger.debug(f" found {len(vertices)} vertices")
for vertex in vertices:
if vertex.active is True:
content = DiscoveryObject.get_discovery_object(vertex)
logger.debug(f" * {content.description}")
for vertex in vertices:
if vertex.active is False:
logger.debug(" vertex is not active")
continue
# We can't load into a pydantic object since Pydantic has a problem with Union type.
# We only want the record type, so it is too much work to try to get into an object.
content_dict = vertex.content_as_dict
record_type = content_dict.get("record_type")
if record_type in record_type_to_vertices_map:
record_type_to_vertices_map[record_type].append(vertex)

# Sort the vertices for each record type.
for k, v in VERTICES_SORT_MAP.items():
if v["sort"] == "sort_infra_name":
record_type_to_vertices_map[k] = sort_infra_name(record_type_to_vertices_map[k])
elif v["sort"] == "sort_infra_host":
record_type_to_vertices_map[k] = sort_infra_host(record_type_to_vertices_map[k])

return record_type_to_vertices_map
Loading

0 comments on commit 3a7245f

Please sign in to comment.