Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove separate config for island logger #1151

Merged
merged 20 commits into from
May 11, 2021
Merged

Conversation

shreyamalviya
Copy link
Contributor

Partial fix for #1146

PR Checklist

  • Have you added an explanation of what your changes do and why you'd like to include them?
  • Is the TravisCI build passing?
  • Was the CHANGELOG.md updated to reflect the changes?
  • Was the documentation framework updated to reflect the changes?

Testing Checklist

  • Added relevant unit tests?
  • Have you successfully tested your changes locally? Elaborate:

    Ran monkey_island.py with --server-config and without

  • If applicable, add screenshots or log transcripts of the feature working

@codecov
Copy link

codecov bot commented May 10, 2021

Codecov Report

Merging #1151 (b13839d) into develop (aa959c3) will increase coverage by 0.04%.
The diff coverage is 70.73%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #1151      +/-   ##
===========================================
+ Coverage    28.62%   28.67%   +0.04%     
===========================================
  Files          411      412       +1     
  Lines        12742    12755      +13     
===========================================
+ Hits          3648     3658      +10     
- Misses        9094     9097       +3     
Impacted Files Coverage Δ
monkey/monkey_island.py 0.00% <0.00%> (ø)
monkey/monkey_island/cc/arg_parser.py 0.00% <0.00%> (ø)
monkey/monkey_island/cc/server_utils/consts.py 100.00% <ø> (ø)
...key/monkey_island/cc/server_utils/island_logger.py 100.00% <100.00%> (+9.09%) ⬆️
monkey/monkey_island/config_loader.py 100.00% <100.00%> (ø)
monkey/tests/conftest.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update aa959c3...b13839d. Read the comment docs.

@shreyamalviya shreyamalviya requested review from mssalvatore and VakarisZ and removed request for mssalvatore May 10, 2021 11:19
Comment on lines 49 to 50
if not os.path.exists(data_dir_path):
os.makedirs(data_dir_path, mode=0o700, exist_ok=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be the responsibility of the logger. The logger can assume the directory it's been configured to write to exists.

from typing import Dict

from monkey_island.cc.server_utils.consts import DEFAULT_LOGGER_CONFIG_PATH

__author__ = "Maor.Rayzin"
Copy link
Collaborator

@mssalvatore mssalvatore May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this __author__ field.

Comment on lines 21 to 23
server_config_path = os.path.expanduser(island_args.server_config)
if not Path(server_config_path).is_file():
server_config_generator.create_default_config_file(server_config_path)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user has a typo in their command-line argument, this will use a config other than the one they specified and pretend like everything is OK. Monkey Island will run with the wrong config and the user will waste time trying to figure out why the island isn't behaving as expected.

Just read the file. Trap any OSErrors and print a useful message for the user.

DEFAULT_LOGGER_CONFIG_PATH = os.path.join(
MONKEY_ISLAND_ABS_PATH, "cc", "island_logger_default_config.json"
)
DEFAULT_LOG_LEVEL = "NOTSET"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not DEFAULT or INFO? Also, if this is only used in monkey_island.py, it can probably be moved there.

Comment on lines 25 to 33
with open(server_config_path, "r") as f:
config_content = f.read()
data = json.loads(config_content)
data_dir = os.path.abspath(
os.path.expanduser(
os.path.expandvars(data["data_dir"] if "data_dir" in data else DEFAULT_DATA_DIR)
)
)
log_level = data["log_level"] if "log_level" in data else DEFAULT_LOG_LEVEL
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can probably be a separate function called load_server_config() or something similar.

Make sure to write tests for load_server_config() that test all of the possible behaviors.

def test_expanduser_filename(mock_home_env, tmpdir):
DATA_DIR = tmpdir
INFO_LOG = os.path.join(DATA_DIR, "monkey_island.log")
LOG_LEVEL = "DEBUG"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is testing the call to logger.info(), let's make this log level INFO. Or we could leave this as DEBUG and call logger.debug() instead.

We should also add another test where we set the log level to INFO, call logger.debug(), and verify that the file does not contain the line.

@@ -17,11 +12,13 @@ def mock_home_env(monkeypatch, tmpdir):
monkeypatch.setenv("HOME", str(tmpdir))
Copy link
Collaborator

@mssalvatore mssalvatore May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this fixture is needed anymore since we're passing data_dir directly to the logger configuration function..

@ghost
Copy link

ghost commented May 11, 2021

DeepCode's analysis on #b13839 found:

  • ℹ️ 1 minor issue. 👇

Top issues

Description Example fixes
Replace exit with sys.exit. Occurrences: 🔧 Example fixes

👉 View analysis in DeepCode’s Dashboard | Configure the bot

Because `monkey_island.py` has the same name as the `monkey_island`
module, pytest can't import monkey_island.py and run any tests against
its code.
As the number of configuration items will increase in the future, return
the config dict instead of individual config properties.
@mssalvatore mssalvatore merged commit 0b21dac into develop May 11, 2021
@mssalvatore mssalvatore deleted the untangle-logger-config branch May 11, 2021 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants