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

1960 deserialize config #2043

Merged
merged 67 commits into from
Jun 27, 2022
Merged

Conversation

mssalvatore
Copy link
Collaborator

@mssalvatore mssalvatore commented Jun 24, 2022

What does this PR do?

Use the AgentConfiguration object in the agent.

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?
  • Have you checked that you haven't introduced any duplicate code?

Testing Checklist

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

    Tested by running unit tests
    Tested by running ETE tests

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

VakarisZ and others added 30 commits June 23, 2022 14:47
Creating AgentConfiguration object from dictionary makes sense because it doesn't couple the configuration to any specific serialization methods. Also, the json sent from the island doesn't match the config structure because it stores config in a dict under "config" key.
"supported_os" was removed from the schema in d079d74
The old config scheme stored timeouts as milliseconds, whereas the new
one uses seconds. Seconds are more convenient because most python
methods expecting timeouts are expecting floating-point seconds.
The variable name "os" conflicts with the name of Python's `os` library.
monkey/infection_monkey/master/automated_master.py Outdated Show resolved Hide resolved
Comment on lines +41 to +42
# TODO: This has been replaced by common.configuration.InvalidConfigurationError. Use that error
# instead and remove this one.
Copy link
Contributor

Choose a reason for hiding this comment

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

Not in this PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, it's out of scope. Other components are still using the old ConfigService and other components that still use this error. The TODO is here because vulture can't tell which one is being used, so once everything is switched over to the new error, we don't want to forget to remove this one.

Comment on lines +2 to +12
from .agent_sub_configurations import (
CustomPBAConfiguration,
PluginConfiguration,
ScanTargetConfiguration,
ICMPScanConfiguration,
TCPScanConfiguration,
NetworkScanConfiguration,
ExploitationOptionsConfiguration,
ExploiterConfiguration,
ExploitationConfiguration,
PropagationConfiguration,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we have both PluginConfiguration and ExploiterConfiguration dataclasses which have the same member variables (link to file with definitions)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good question! ExploiterConfiguration had supported_os in it, until that was removed by #2038. When supported_os was removed, everything that used ExploiterConfiguration should have been changed to PluginConfiguration. I'll add a task to #1960.

# makes it impossible to construct an invalid object
try:
AgentConfigurationSchema().dump(self)
except Exception as err:
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we expecting anything but MarshmallowError?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That I'm aware, it can raise MarshmallowError, TypeError, or ValueError. It may raise others as well, I'm not sure.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A better question is, "Is there any case where AgentConfigurationSchema().dump() could raise an exception for any reason other than an invalid exception?" To my knowledge, there's not.

@@ -204,5 +204,4 @@


def build_default_agent_configuration() -> AgentConfiguration:
schema = AgentConfigurationSchema()
return schema.loads(DEFAULT_AGENT_CONFIGURATION_JSON)
return AgentConfiguration.from_json(DEFAULT_AGENT_CONFIGURATION_JSON)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably specify the defaults in the marshmallow schema. We'll do it with validation, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, #2004.

Move line 60 to f formatting from the old %s style
self, target_config: ScanTargetConfiguration
) -> List[NetworkAddress]:
ranges_to_scan = target_config.subnets
inaccessible_subnets = target_config.inaccessible_subnets
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the purpose of these variables? I'd rather we just use keyword arguments in compile_scan_target_list call and pass them directly

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It keeps _scan_network() a little shorter and easier to read. That was probably more important when we were using a dict. I could go either way. Feel free to fix it.

AgentConfiguration.from_json(json.dumps(invalid_dict))


def test_to_json():
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it's worth considering merging the serialization tests. Not only because from_json and to_json will be used in tandem, but also to avoid the maintenance of default configuration in json format

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added DEFAULT_AGENT_CONFIGURATION and removed DEFAULT_AGENT_CONFIGURATION_JSON

Copy link
Contributor

@VakarisZ VakarisZ left a comment

Choose a reason for hiding this comment

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

Approved, but left some comments

It's easier to maintain object than a JSON string for the default
configuration.
@mssalvatore mssalvatore merged commit 44a6197 into 1960-configuration-object Jun 27, 2022
@mssalvatore mssalvatore deleted the 1960-deserialize-config branch June 27, 2022 12:35
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.

4 participants