Autopsy  4.19.3
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 
21 import java.lang.reflect.Method;
22 import java.lang.reflect.ParameterizedType;
23 import java.lang.reflect.Type;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.logging.Level;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
32 import org.apache.commons.collections.CollectionUtils;
33 import org.openide.util.Pair;
38 
44 
46  private static final Logger logger = Logger.getLogger(ConfigurationModuleManager.class.getName());
47 
49  private static final Set<String> DEFAULT_EXCLUDED_MODULES = Stream.of("Plaso").collect(Collectors.toSet());
51 
57  void revertConfigurationModules(List<ConfigurationModule<?>> configModules) {
58  List<ConfigurationModule<?>> reversed = new ArrayList<>(configModules);
59  Collections.reverse(reversed);
60  for (ConfigurationModule<?> configModule : reversed) {
61  try {
62  configModule.revert();
63  } catch (Exception ex) {
64  // firewall exception handler to ensure reverting a configuration module doesn't cause an error.
65  logger.log(Level.SEVERE, "An error occurred while reverting configuration module: " + configModule.getClass().getCanonicalName(), ex);
66  }
67  }
68  }
69 
77  static String getProfileName(String caseName) {
78  return String.format("integrationTestProfile-%s", caseName);
79  }
80 
89  private IngestJobSettings getDefaultIngestConfig(String caseName) {
90  return new IngestJobSettings(
91  getProfileName(caseName),
93  ingestModuleFactories.getFactories().stream()
94  .filter((f) -> !DEFAULT_EXCLUDED_MODULES.contains(f.getModuleDisplayName()))
95  .map(f -> new IngestModuleTemplate(f, f.getDefaultIngestJobSettings()))
96  .collect(Collectors.toList())
97  );
98  }
99 
110  Pair<IngestJobSettings, List<ConfigurationModule<?>>> runConfigurationModules(String caseName, List<ParameterizedResourceConfig> configModules) {
111  // if no config modules, return default ingest settings
112  if (CollectionUtils.isEmpty(configModules)) {
113  return Pair.of(getDefaultIngestConfig(caseName), Collections.emptyList());
114  }
115 
116  // create a base ingest job settings object with no templates.
117  IngestJobSettings curConfig = new IngestJobSettings(
118  getProfileName(caseName),
120  Collections.emptyList());
121 
122  List<ConfigurationModule<?>> configurationModuleCache = new ArrayList<>();
123 
124  // run through the configuration for each configuration module
125  for (ParameterizedResourceConfig configModule : configModules) {
126  Pair<IngestJobSettings, ConfigurationModule<?>> ingestResult = runConfigurationModule(curConfig, configModule);
127  // if there are results, update to the new ingest job settings and cache the config module.
128  if (ingestResult != null) {
129  curConfig = ingestResult.first() == null ? curConfig : ingestResult.first();
130  if (ingestResult.second() != null) {
131  configurationModuleCache.add(ingestResult.second());
132  }
133  }
134  }
135  return Pair.of(curConfig, configurationModuleCache);
136  }
137 
149  private Pair<IngestJobSettings, ConfigurationModule<?>> runConfigurationModule(IngestJobSettings curConfig, ParameterizedResourceConfig configModule) {
150  // acquire class described by resource
151  Class<?> clazz = null;
152  try {
153  clazz = Class.forName(configModule.getResource());
154  } catch (ClassNotFoundException ex) {
155  logger.log(Level.WARNING, "Unable to find module: " + configModule.getResource(), ex);
156  return null;
157  }
158 
159  // assure that the class is a configuration module.
160  if (clazz == null || !ConfigurationModule.class.isAssignableFrom(clazz)) {
161  logger.log(Level.WARNING, String.format("%s does not seem to be an instance of a configuration module.", configModule.getResource()));
162  return null;
163  }
164 
165  // determine generic parameter type
166  Type configurationModuleType = Stream.of(clazz.getGenericInterfaces())
167  .filter(type -> type instanceof ParameterizedType && ((ParameterizedType) type).getRawType().equals(ConfigurationModule.class))
168  .map(type -> ((ParameterizedType) type).getActualTypeArguments()[0])
169  .findFirst()
170  .orElse(null);
171 
172  if (configurationModuleType == null) {
173  logger.log(Level.SEVERE, String.format("Could not determine generic type of config module: %s", configModule.getResource()));
174  return null;
175  }
176 
177  // instantiate the object from the class and run the configure method.
178  ConfigurationModule<?> configModuleObj = null;
179  Object result = null;
180  try {
181  configModuleObj = (ConfigurationModule<?>) clazz.newInstance();
182  Method m = clazz.getMethod("configure", IngestJobSettings.class, (Class<?>) configurationModuleType);
183  result = m.invoke(configModuleObj, curConfig, configDeserializer.convertToObj(configModule.getParameters(), configurationModuleType));
184  } catch (Exception ex) {
185  // firewall exception handler.
186  logger.log(Level.SEVERE, String.format("There was an error calling configure method on Configuration Module %s", configModule.getResource()), ex);
187  return null;
188  }
189 
190  // return results or an error if no results returned.
191  if (result instanceof IngestJobSettings) {
192  return Pair.of((IngestJobSettings) result, configModuleObj);
193  } else {
194  logger.log(Level.SEVERE, String.format("Could not retrieve IngestJobSettings or null was returned from %s", configModule.getResource()));
195  return null;
196  }
197  }
198 }
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: Thu Sep 29 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.