Skip to content

Commit

Permalink
Fixing Mordor and azure_ml_tools tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ianhelle committed Jul 15, 2021
1 parent 1ec1a00 commit 8707868
Show file tree
Hide file tree
Showing 8 changed files with 1,589 additions and 3,450 deletions.
4,934 changes: 1,582 additions & 3,352 deletions NOTICE.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion msticpy/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""Version file."""
VERSION = "1.2.3"
VERSION = "1.2.4"
2 changes: 1 addition & 1 deletion msticpy/config/mp_config_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def __init__(
vbox = widgets.VBox(
[
self.txt_current_file,
widgets.HBox([self.btn_save, self.btn_validate]),
widgets.HBox([self.btn_save, self.cb_backup, self.btn_validate]),
self.mp_conf_file.viewer,
]
)
Expand Down
2 changes: 1 addition & 1 deletion msticpy/data/drivers/mordor_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def _to_datetime(date_val) -> datetime:
return datetime.min


DS_PREFIX = "https://raw.githubusercontent.com/OTRF/mordor/master/datasets/"
DS_PREFIX = "https://raw.githubusercontent.com/OTRF/Security-Datasets/master/datasets/"


# pylint: disable=not-an-iterable, no-member
Expand Down
63 changes: 2 additions & 61 deletions msticpy/nbtools/azure_ml_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
"""Checker functions for Azure ML notebooks."""
import json
import os
import re
import socket
import sys
import urllib
from pathlib import Path
from typing import Any, List, Mapping, Optional, Tuple, Union
from urllib import request

from IPython import get_ipython
from IPython.display import HTML, display
Expand Down Expand Up @@ -104,9 +102,6 @@ def check_versions(
min_py_ver = _get_pkg_version(min_py_ver).release
check_python_ver(min_py_ver=min_py_ver)

if is_in_aml() and not _check_nb_check_ver():
_disp_html("Check stopped because nb_check has been updated.")
return
_check_mp_install(min_mp_ver, mp_release, extras)
_check_kql_prereqs()
_set_kql_env_vars(extras)
Expand Down Expand Up @@ -381,60 +376,6 @@ def _check_kql_prereqs():
)
_disp_html(
"To prevent warnings when loading the Kqlmagic data provider,"
" Please run the following commands:<br>"
f"<pre>!sudo apt-get --yes install {' '.join(missing_lx_pkg)}</pre><br>"
"<pre>!pip uninstall -y enum34</pre><br>"
"<pre>!pip install PyGObject</pre>"
" Please run the following command:<br>"
"!conda install --yes -c conda-forge pygobject<br>"
)


# pylint: disable=broad-except
def _check_nb_check_ver():
"""Check the version of nb_check and optionally update."""
nb_check_path = "utils/nb_check.py"
gh_file = ""
curr_file = ""
try:
# Bandit warning - fixed https URL
with request.urlopen(NB_CHECK_URI) as gh_fh: # nosec
gh_file = gh_fh.read().decode("utf-8")
except Exception:
_disp_html(f"Warning could not check version of {NB_CHECK_URI}")
return True
nbc_path = get_aml_user_folder().joinpath(nb_check_path)
if nbc_path.is_file():
try:
curr_file = nbc_path.read_text()
except Exception:
_disp_html(f"Warning could not check version local {nb_check_path}")

if _get_file_ver(gh_file) == _get_file_ver(curr_file):
return True

_disp_html("Updating local {nb_check_path}...")
bk_up = get_aml_user_folder().joinpath(f"{nb_check_path}._save_")
if bk_up.is_file():
bk_up.unlink()
nbc_path.replace(bk_up)
try:
with open(nbc_path, "w") as repl_fh:
repl_fh.write(gh_file)
except Exception:
bk_up.replace(nbc_path)

_disp_html(
"<h4><font color='orange'>"
f"Important: The version of {nb_check_path} has been updated.<br>"
"Please re-run this cell to run the new version."
"</font></h4>"
)
return False


def _get_file_ver(file_text) -> Optional[str]:
if not file_text:
return None
f_match = re.search(r"__version__\s*=\s*\"([\d.]+)\"", file_text)
if f_match:
return f_match.groups()[0]
return None
2 changes: 1 addition & 1 deletion msticpy/nbtools/nbwidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class RegisteredWidget(ABC):
initialization parameters. If an instance of the same widget class is
created with the same parameters, its previous value will be repopulated
from the registry.
This is especially useful in notebooks where people accidently re-run
This is especially useful in notebooks where people accidentally re-run
the same cell after entering values.
"""

Expand Down
2 changes: 1 addition & 1 deletion tests/data/drivers/test_mordor_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_mordor_query_provider(qry_provider):
check.is_in("Mitre Techniques:", output.getvalue())

f_path = q_func("print")
check.is_in("https://raw.githubusercontent.com/OTRF/mordor", f_path)
check.is_in("https://raw.githubusercontent.com/OTRF/Security-Datasets", f_path)

d_frame = q_func()
check.is_instance(d_frame, pd.DataFrame)
Expand Down
32 changes: 0 additions & 32 deletions tests/nbtools/test_azure_ml_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,38 +197,6 @@ def test_check_versions_mpconfig(monkeypatch, aml_file_sys, test_case):
mp_backup.copy(mp_path)


# pylint: disable=protected-access
def test_check_versions_nbcheck(monkeypatch, aml_file_sys):
"""Test nb_check update."""
_, user_dir = aml_file_sys

# monkeypatch for various test cases
_os = _PyOs()
monkeypatch.setattr(aml, "os", _os)
monkeypatch.setattr(aml, "_get_vm_fqdn", lambda: "myhost")

# Set an env var to emulate AML
_os.environ["APPSETTING_WEBSITE_SITE_NAME"] = "AMLComputeInstance"

# Create an old version of nb_check
nb_check = user_dir.join("utils").join("nb_check.py")
nb_check_save = user_dir.join("utils").join("nb_check.save")
nb_check.copy(nb_check_save)
nb_check.write_text(
_NB_CHECK_TXT.replace('__version__ = "1.5.0"', '__version__ = "1.0.0"'),
encoding="utf-8",
)

with change_directory(str(user_dir)):
aml.check_versions(min_py_ver=_MIN_PY_VER, min_mp_ver=_MIN_MP_VER)

nb_check_txt = nb_check.read_text(encoding="utf-8")
file_ver = aml._get_file_ver(nb_check_txt)
check.not_equal(file_ver, "1.0.0")
env = "KQLMAGIC_EXTRAS_REQUIRE"
check.is_not_in(env, _os.environ)


def test_check_versions_nbuser_settings(monkeypatch, aml_file_sys):
"""Test nb_check update."""
_, user_dir = aml_file_sys
Expand Down

0 comments on commit 8707868

Please sign in to comment.