示例#1
0
def custom_mp_config(
        mp_path: Union[str, Path]) -> Generator[Dict[str, Any], None, None]:
    """
    Context manager to temporarily set MSTICPYCONFIG path.

    Parameters
    ----------
    mp_path : Union[str, Path]
        Path to msticpy config yaml

    Yields
    ------
    Dict[str, Any]
        Custom settings.

    Raises
    ------
    FileNotFoundError
        If mp_path does not exist.

    """
    current_path = os.environ.get(pkg_config._CONFIG_ENV_VAR)
    if not Path(mp_path).is_file():
        raise FileNotFoundError(
            f"Setting MSTICPYCONFIG to non-existent file {mp_path}")
    try:
        os.environ[pkg_config._CONFIG_ENV_VAR] = str(mp_path)
        pkg_config.refresh_config()
        yield pkg_config.settings
    finally:
        if not current_path:
            del os.environ[pkg_config._CONFIG_ENV_VAR]
        else:
            os.environ[pkg_config._CONFIG_ENV_VAR] = current_path
        pkg_config.refresh_config()
示例#2
0
 def test_validate_config(self):
     """Test config validation function."""
     test_config1 = Path(_TEST_DATA).joinpath(pkg_config._CONFIG_FILE)
     with custom_mp_config(test_config1):
         results = pkg_config.validate_config()
         self.assertGreater(len(results[0]), 1)
         os.environ["VTAUTHKEY"] = "myXfId"
         os.environ["XFORCE_ID"] = "myXfId"
         os.environ["XFORCE_KEY"] = "myXfId"
         os.environ["MAXMIND_AUTH"] = "myXfId"
         pkg_config.refresh_config()
         results = pkg_config.validate_config()
         self.assertEqual(results, ([], []))
示例#3
0
def _read_config_settings(conf_file):
    sys_config = conf_file = os.environ["MSTICPYCONFIG"]
    if not conf_file:
        conf_file = sys_config
    if not conf_file:
        raise ValueError("Configuration file not found.")

    with open(conf_file, "r") as conf_hdl:
        cur_settings = yaml.safe_load(conf_hdl)

    # temporarily set env var to point to conf_file
    os.environ["MSTICPYCONFIG"] = conf_file
    config.refresh_config()
    kvlt_settings = KeyVaultSettings()
    os.environ["MSTICPYCONFIG"] = sys_config
    return cur_settings, kvlt_settings
示例#4
0
def custom_mp_config(
    mp_path: Union[str, Path],
    path_check: bool = True,
) -> Generator[Dict[str, Any], None, None]:
    """
    Context manager to temporarily set MSTICPYCONFIG path.

    Parameters
    ----------
    mp_path : Union[str, Path]
        Path to msticpy config yaml
    check_path : bool
        If False, skip check for existing file
    Yields
    ------
    Dict[str, Any]
        Custom settings.

    Raises
    ------
    FileNotFoundError
        If mp_path does not exist.

    """
    current_path = os.environ.get(pkg_config._CONFIG_ENV_VAR)
    if path_check and not Path(mp_path).is_file():
        raise FileNotFoundError(
            f"Setting MSTICPYCONFIG to non-existent file {mp_path}")
    try:
        # We need to lock the settings since these are global
        # Otherwise the tests interfere with each other.
        _lock_file_path = "./.mp_settings.lock"
        with FileLock(_lock_file_path):
            os.environ[pkg_config._CONFIG_ENV_VAR] = str(mp_path)
            pkg_config.refresh_config()
            yield pkg_config.settings
    finally:
        if not current_path:
            del os.environ[pkg_config._CONFIG_ENV_VAR]
        else:
            os.environ[pkg_config._CONFIG_ENV_VAR] = current_path
        pkg_config.refresh_config()
示例#5
0
def _set_mpconfig_var():
    """Set MSTICPYCONFIG to file in user directory if no other found."""
    mp_path_val = os.environ.get(MP_ENV_VAR)
    if (
            # If a valid MSTICPYCONFIG value is found - return
        (mp_path_val and Path(mp_path_val).is_file())
            # Or if there is a msticpconfig in the current folder.
            or Path(".").joinpath(MP_FILE).is_file()):
        return
    # Otherwise check the user's root folder
    user_dir = get_aml_user_folder()
    mp_path = Path(user_dir).joinpath(MP_FILE)
    if mp_path.is_file():
        # If there's a file there, set the env variable to that.
        os.environ[MP_ENV_VAR] = str(mp_path)
        # Since we have already imported msticpy to check the version
        # it will have already configured settings so we need to refresh.
        from msticpy.common.pkg_config import refresh_config

        refresh_config()
        _disp_html(
            f"<br>No {MP_FILE} found. Will use {MP_FILE} in user folder {user_dir}<br>"
        )