Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ConfigurationModuleManager.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.integrationtesting;
20 
22 import java.lang.reflect.Method;
23 import java.lang.reflect.ParameterizedType;
24 import java.lang.reflect.Type;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.logging.Level;
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
33 import org.apache.commons.collections.CollectionUtils;
34 import org.openide.util.Pair;
39 
45 
47  private static final Logger logger = Logger.getLogger(ConfigurationModuleManager.class.getName());
48 
50  private static final Set<String> DEFAULT_EXCLUDED_MODULES = Stream.of(
51  "Plaso",
53  ).collect(Collectors.toSet());
55 
61  void revertConfigurationModules(List<ConfigurationModule<?>> configModules) {
62  List<ConfigurationModule<?>> reversed = new ArrayList<>(configModules);
63  Collections.reverse(reversed);
64  for (ConfigurationModule<?> configModule : reversed) {
65  try {
66  configModule.revert();
67  } catch (Exception ex) {
68  // firewall exception handler to ensure reverting a configuration module doesn't cause an error.
69  logger.log(Level.SEVERE, "An error occurred while reverting configuration module: " + configModule.getClass().getCanonicalName(), ex);
70  }
71  }
72  }
73 
81  static String getProfileName(String caseName) {
82  return String.format("integrationTestProfile-%s", caseName);
83  }
84 
93  private IngestJobSettings getDefaultIngestConfig(String caseName) {
94  return new IngestJobSettings(
95  getProfileName(caseName),
97  ingestModuleFactories.getFactories().stream()
98  .filter((f) -> !DEFAULT_EXCLUDED_MODULES.contains(f.getModuleDisplayName()))
99  .map(f -> new IngestModuleTemplate(f, f.getDefaultIngestJobSettings()))
100  .collect(Collectors.toList())
101  );
102  }
103 
114  Pair<IngestJobSettings, List<ConfigurationModule<?>>> runConfigurationModules(String caseName, List<ParameterizedResourceConfig> configModules) {
115  // if no config modules, return default ingest settings
116  if (CollectionUtils.isEmpty(configModules)) {
117  return Pair.of(getDefaultIngestConfig(caseName), Collections.emptyList());
118  }
119 
120  // create a base ingest job settings object with no templates.
121  IngestJobSettings curConfig = new IngestJobSettings(
122  getProfileName(caseName),
124  Collections.emptyList());
125 
126  List<ConfigurationModule<?>> configurationModuleCache = new ArrayList<>();
127 
128  // run through the configuration for each configuration module
129  for (ParameterizedResourceConfig configModule : configModules) {
130  Pair<IngestJobSettings, ConfigurationModule<?>> ingestResult = runConfigurationModule(curConfig, configModule);
131  // if there are results, update to the new ingest job settings and cache the config module.
132  if (ingestResult != null) {
133  curConfig = ingestResult.first() == null ? curConfig : ingestResult.first();
134  if (ingestResult.second() != null) {
135  configurationModuleCache.add(ingestResult.second());
136  }
137  }
138  }
139  return Pair.of(curConfig, configurationModuleCache);
140  }
141 
153  private Pair<IngestJobSettings, ConfigurationModule<?>> runConfigurationModule(IngestJobSettings curConfig, ParameterizedResourceConfig configModule) {
154  // acquire class described by resource
155  Class<?> clazz = null;
156  try {
157  clazz = Class.forName(configModule.getResource());
158  } catch (ClassNotFoundException ex) {
159  logger.log(Level.WARNING, "Unable to find module: " + configModule.getResource(), ex);
160  return null;
161  }
162 
163  // assure that the class is a configuration module.
164  if (clazz == null || !ConfigurationModule.class.isAssignableFrom(clazz)) {
165  logger.log(Level.WARNING, String.format("%s does not seem to be an instance of a configuration module.", configModule.getResource()));
166  return null;
167  }
168 
169  // determine generic parameter type
170  Type configurationModuleType = Stream.of(clazz.getGenericInterfaces())
171  .filter(type -> type instanceof ParameterizedType && ((ParameterizedType) type).getRawType().equals(ConfigurationModule.class))
172  .map(type -> ((ParameterizedType) type).getActualTypeArguments()[0])
173  .findFirst()
174  .orElse(null);
175 
176  if (configurationModuleType == null) {
177  logger.log(Level.SEVERE, String.format("Could not determine generic type of config module: %s", configModule.getResource()));
178  return null;
179  }
180 
181  // instantiate the object from the class and run the configure method.
182  ConfigurationModule<?> configModuleObj = null;
183  Object result = null;
184  try {
185  configModuleObj = (ConfigurationModule<?>) clazz.newInstance();
186  Method m = clazz.getMethod("configure", IngestJobSettings.class, (Class<?>) configurationModuleType);
187  result = m.invoke(configModuleObj, curConfig, configDeserializer.convertToObj(configModule.getParameters(), configurationModuleType));
188  } catch (Exception ex) {
189  // firewall exception handler.
190  logger.log(Level.SEVERE, String.format("There was an error calling configure method on Configuration Module %s", configModule.getResource()), ex);
191  return null;
192  }
193 
194  // return results or an error if no results returned.
195  if (result instanceof IngestJobSettings) {
196  return Pair.of((IngestJobSettings) result, configModuleObj);
197  } else {
198  logger.log(Level.SEVERE, String.format("Could not retrieve IngestJobSettings or null was returned from %s", configModule.getResource()));
199  return null;
200  }
201  }
202 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
Pair< IngestJobSettings, ConfigurationModule<?> > runConfigurationModule(IngestJobSettings curConfig, ParameterizedResourceConfig configModule)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.