LEFT | RIGHT |
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 """The artifact definitions CLI arguments helper.""" | 2 """The artifact definitions CLI arguments helper.""" |
3 | 3 |
4 import os | 4 import os |
5 | 5 |
6 from artifacts import errors as artifacts_errors | 6 from artifacts import errors as artifacts_errors |
7 from artifacts import reader as artifacts_reader | 7 from artifacts import reader as artifacts_reader |
8 from artifacts import registry as artifacts_registry | 8 from artifacts import registry as artifacts_registry |
9 | 9 |
10 from plaso.cli import tools | 10 from plaso.cli import tools |
11 from plaso.cli.helpers import interface | 11 from plaso.cli.helpers import interface |
12 from plaso.cli.helpers import manager | 12 from plaso.cli.helpers import manager |
13 from plaso.lib import errors | 13 from plaso.lib import errors |
| 14 from plaso.preprocessors import manager as preprocessors_manager |
14 | 15 |
15 | 16 |
16 class ArtifactDefinitionsArgumentsHelper(interface.ArgumentsHelper): | 17 class ArtifactDefinitionsArgumentsHelper(interface.ArgumentsHelper): |
17 """Artifact definition CLI arguments helper.""" | 18 """Artifact definition CLI arguments helper.""" |
18 | 19 |
19 NAME = u'artifact_definitions' | 20 NAME = u'artifact_definitions' |
20 DESCRIPTION = u'Artifact definition command line arguments.' | 21 DESCRIPTION = u'Artifact definition command line arguments.' |
21 | 22 |
22 @classmethod | 23 @classmethod |
23 def AddArguments(cls, argument_group): | 24 def AddArguments(cls, argument_group): |
(...skipping 19 matching lines...) Expand all Loading... |
43 def ParseOptions(cls, options, configuration_object): | 44 def ParseOptions(cls, options, configuration_object): |
44 """Parses and validates options. | 45 """Parses and validates options. |
45 | 46 |
46 Args: | 47 Args: |
47 options (argparse.Namespace): parser options. | 48 options (argparse.Namespace): parser options. |
48 configuration_object (CLITool): object to be configured by the argument | 49 configuration_object (CLITool): object to be configured by the argument |
49 helper. | 50 helper. |
50 | 51 |
51 Raises: | 52 Raises: |
52 BadConfigObject: when the configuration object is of the wrong type. | 53 BadConfigObject: when the configuration object is of the wrong type. |
| 54 BadConfigOption: if the required artifact definitions are not defined. |
53 """ | 55 """ |
54 if not isinstance(configuration_object, tools.CLITool): | 56 if not isinstance(configuration_object, tools.CLITool): |
55 raise errors.BadConfigObject( | 57 raise errors.BadConfigObject( |
56 u'Configuration object is not an instance of CLITool') | 58 u'Configuration object is not an instance of CLITool') |
57 | |
58 registry = None | |
59 | 59 |
60 path = getattr(options, u'artifact_definitions_path', None) | 60 path = getattr(options, u'artifact_definitions_path', None) |
61 | 61 |
62 data_location = getattr(configuration_object, u'_data_location', None) | 62 data_location = getattr(configuration_object, u'_data_location', None) |
63 if (not path or not os.path.exists(path)) and data_location: | 63 if (not path or not os.path.exists(path)) and data_location: |
64 path = os.path.dirname(data_location) | 64 path = os.path.dirname(data_location) |
65 path = os.path.join(path, u'artifacts') | 65 path = os.path.join(path, u'artifacts') |
66 | 66 |
67 if path and os.path.exists(path): | 67 if not path or not os.path.exists(path): |
68 registry = artifacts_registry.ArtifactDefinitionsRegistry() | 68 raise errors.BadConfigOption( |
69 reader = artifacts_reader.YamlArtifactsReader() | 69 u'Unable to determine path to artifact definitions.') |
70 | 70 |
71 try: | 71 registry = artifacts_registry.ArtifactDefinitionsRegistry() |
72 registry.ReadFromDirectory(reader, path) | 72 reader = artifacts_reader.YamlArtifactsReader() |
73 | 73 |
74 except (KeyError, artifacts_errors.FormatError) as exception: | 74 try: |
75 raise errors.BadConfigObject(( | 75 registry.ReadFromDirectory(reader, path) |
76 u'Unable to read artifact definitions from: {0:s} with error: ' | 76 |
77 u'{1!s}').format(path, exception)) | 77 except (KeyError, artifacts_errors.FormatError) as exception: |
| 78 raise errors.BadConfigOption(( |
| 79 u'Unable to read artifact definitions from: {0:s} with error: ' |
| 80 u'{1!s}').format(path, exception)) |
| 81 |
| 82 for name in preprocessors_manager.PreprocessPluginsManager.GetNames(): |
| 83 if not registry.GetDefinitionByName(name): |
| 84 raise errors.BadConfigOption( |
| 85 u'Missing required artifact definition: {0:s}'.format(name)) |
78 | 86 |
79 setattr(configuration_object, u'_artifacts_registry', registry) | 87 setattr(configuration_object, u'_artifacts_registry', registry) |
80 | 88 |
81 | 89 |
82 manager.ArgumentHelperManager.RegisterHelper(ArtifactDefinitionsArgumentsHelper) | 90 manager.ArgumentHelperManager.RegisterHelper(ArtifactDefinitionsArgumentsHelper) |
LEFT | RIGHT |