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

Create data directory on Island initialisation #1170

Merged
merged 39 commits into from
May 26, 2021

Conversation

shreyamalviya
Copy link
Contributor

@shreyamalviya shreyamalviya commented May 14, 2021

Fixes #1147
(Still need to figure out Windows permissions!)

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 without --server-config and with (passing it valid path/valid file (with "data_dir" field and without), valid path/invalid file, invalid path)

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

@ghost
Copy link

ghost commented May 14, 2021

DeepCode's analysis on #baee74 found:

  • ⚠️ 1 warning 👇
  • ✔️ 1 issue was fixed.

Top issues

Description Example fixes
Use os.makedirs instead of os.mkdir because the given path may require creating the parent directories. Occurrences: 🔧 Example fixes

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

@codecov
Copy link

codecov bot commented May 14, 2021

Codecov Report

Merging #1170 (baee74b) into develop (d8c1e2b) will decrease coverage by 0.14%.
The diff coverage is 20.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #1170      +/-   ##
===========================================
- Coverage    28.94%   28.80%   -0.15%     
===========================================
  Files          419      424       +5     
  Lines        12797    12863      +66     
===========================================
+ Hits          3704     3705       +1     
- Misses        9093     9158      +65     
Impacted Files Coverage Δ
monkey/monkey_island.py 0.00% <0.00%> (ø)
monkey/monkey_island/cc/arg_parser.py 0.00% <0.00%> (ø)
...monkey_island/cc/environment/data_dir_generator.py 0.00% <0.00%> (ø)
...monkey_island/cc/environment/environment_config.py 100.00% <ø> (ø)
...key_island/cc/environment/environment_singleton.py 44.82% <ø> (-26.15%) ⬇️
...key_island/cc/environment/server_config_handler.py 0.00% <0.00%> (ø)
...onkey_island/cc/environment/windows_permissions.py 0.00% <0.00%> (ø)
monkey/monkey_island/cc/main.py 0.00% <0.00%> (ø)
monkey/monkey_island/setup/config_setup.py 0.00% <0.00%> (ø)
monkey/monkey_island/setup/gevent_setup.py 0.00% <0.00%> (ø)
... and 13 more

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 d8c1e2b...baee74b. Read the comment docs.

@shreyamalviya shreyamalviya changed the title Data dir on island init Create data directory on Island initialisation May 14, 2021
monkey/monkey_island.py Outdated Show resolved Hide resolved
@shreyamalviya shreyamalviya marked this pull request as ready for review May 19, 2021 13:10
def create_default_config_file(path):
def create_default_server_config_file() -> str:
if not os.path.isfile(DEFAULT_SERVER_CONFIG_PATH):
create_data_dir(DEFAULT_DATA_DIR, False)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm sorry, but I still don't understand what's wrong with using os.makedirs here. The explanation of $HOME might not be resolved doesn't make sense. It's the responsibility of DEFAULT_DATA_DIR to point to a resolvable path. And if DEFAULT_DATA_DIR can't be resolved, how will not making parent directories help us?

Copy link
Contributor Author

@shreyamalviya shreyamalviya May 24, 2021

Choose a reason for hiding this comment

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

For example, on Linux, if the $HOME env variable doesn't exist, the value of DEFAULT_DATA_DIR will remain "$HOME/.monkey_island" (we're using os.path.expandvars() to resolve the path but in case nothing is resolved, it will simply return the same string, it won't throw any kind of error). Using os.makedirs will create a directory named "$HOME" and the ".monkey_island" directory under that. Creating a directory named "$HOME" is not the expected behaviour. If the $HOME variable can't be resolved, we expect the user to specify the path for the data directory.

Copy link
Contributor

Choose a reason for hiding this comment

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

If the $HOME variable can't be resolved, we expect the user to specify the path for the data directory.

How is this expressed in the code and where? Users and developers are expected to get os.mkdir error and know that os.mkdir failed because it doesn't create parent folders, so it means that the parent folder of data_dir is not present?
What prevents the user from making the same mistake via cmd arguments?
Wouldn't it be better to

if not os.path.isdir(data_dir_parent):
    logger.error(f"Can't create a data directory because path {data_dir_parent} doesn't exist. Specify a reachable directory with {data_dir_flag}")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See baee74b7


config = config_loader.load_server_config_from_file(server_config_path)

create_data_dir(config["data_dir"], True)
Copy link
Contributor

Choose a reason for hiding this comment

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

This line only makes sense if island_args.server_config, because otherwise you create the dir in server_config_generator.create_default_server_config_file()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in #1184

VakarisZ and others added 15 commits May 24, 2021 11:05
…e file, with server config write/read operations
…f server_config setup by cmd_arguments and setup of server_config using defaults
… though server path is not specified. Island thinks that server config path was specified.
Comment on lines 27 to 39
add_default_values_to_config(config)

return config


def add_default_values_to_config(config):
config["data_dir"] = os.path.abspath(
os.path.expanduser(os.path.expandvars(config.get("data_dir", DEFAULT_DATA_DIR)))
)

config.setdefault("log_level", DEFAULT_LOG_LEVEL)

return config
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are we deliberately removing this or is this part of the rebase mess?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This commit also removes monkey/tests/unit_tests/monkey_island/cc/environment/test_server_config_handler.py as part of the rebase. Depending on this comment, we need to re-add/modify this file too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Deliberately. Config is now built in island_config_options.py.

@VakarisZ VakarisZ merged commit a211c0f into develop May 26, 2021
@VakarisZ VakarisZ deleted the data-dir-on-island-init branch May 26, 2021 11:37
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.

Create data_dir on Monkey Island initialization
2 participants