Autopsy  4.19.2
Graphical digital forensics platform for The Sleuth Kit and other tools.
ReportingConfigLoader.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2019-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.report.infrastructure;
20 
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.Arrays;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.Set;
35 import java.util.TreeSet;
36 import java.util.logging.Level;
37 import org.openide.util.io.NbObjectInputStream;
38 import org.openide.util.io.NbObjectOutputStream;
42 
48 final class ReportingConfigLoader {
49 
50  private static final Logger logger = Logger.getLogger(ReportingConfigLoader.class.getName());
51  private static final String REPORT_CONFIG_FOLDER = "ReportingConfigs"; //NON-NLS
52  private static final String REPORT_CONFIG_FOLDER_PATH = Paths.get(PlatformUtil.getUserConfigDirectory(), ReportingConfigLoader.REPORT_CONFIG_FOLDER).toAbsolutePath().toString();
53  private static final String REPORT_SETTINGS_FILE_EXTENSION = ".settings";
54  private static final String TABLE_REPORT_CONFIG_FILE = "TableReportSettings.settings";
55  private static final String FILE_REPORT_CONFIG_FILE = "FileReportSettings.settings";
56  private static final String GENERAL_REPORT_CONFIG_FILE = "GeneralReportSettings.settings";
57  private static final String MODULE_CONFIG_FILE = "ModuleConfigs.settings";
58 
59  // Collection of standard report modules that are no longer in Autopsy. We keep
60  // track to suppress any errors when searching for the module since it may still
61  // existing in the configuration file.
62  private static final List<String> DELETED_REPORT_MODULES = Arrays.asList("org.sleuthkit.autopsy.report.modules.stix.STIXReportModule");
63 
76  @SuppressWarnings("unchecked")
77  static synchronized ReportingConfig loadConfig(String configName) throws ReportConfigException {
78 
79  // construct the configuration directory path
80  Path reportDirPath = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, configName);
81  File reportDirectory = reportDirPath.toFile();
82 
83  // Return null if a reporting configuration for the given name does not exist.
84  if (!reportDirectory.exists()) {
85  throw new ReportConfigException("Unable to find report configuration folder for " + reportDirPath.toString() + ". Please configure in the application Options panel.");
86  }
87 
88  if (!reportDirectory.isDirectory() || !reportDirectory.canRead()) {
89  throw new ReportConfigException("Unable to read reporting configuration directory " + reportDirPath.toString());
90  }
91 
92  // read in the configuration
93  ReportingConfig config = new ReportingConfig(configName);
94 
95  // read table report settings
96  String filePath = reportDirPath.toString() + File.separator + TABLE_REPORT_CONFIG_FILE;
97  try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(filePath))) {
98  config.setTableReportSettings((TableReportSettings) in.readObject());
99  } catch (IOException | ClassNotFoundException ex) {
100  throw new ReportConfigException("Unable to read table report settings " + filePath, ex);
101  }
102 
103  // read file report settings
104  filePath = reportDirPath.toString() + File.separator + FILE_REPORT_CONFIG_FILE;
105  try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(filePath))) {
106  config.setFileReportSettings((FileReportSettings) in.readObject());
107  } catch (IOException | ClassNotFoundException ex) {
108  throw new ReportConfigException("Unable to read file report settings " + filePath, ex);
109  }
110 
111  filePath = reportDirPath.resolve(GENERAL_REPORT_CONFIG_FILE).toString();
112  try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(filePath))) {
113  config.setGeneralReportSettings((GeneralReportSettings) in.readObject());
114  } catch (IOException | ClassNotFoundException ex) {
115  throw new ReportConfigException("Unable to read general report settings " + filePath, ex);
116  }
117 
118  // read map of module configuration objects
119  Map<String, ReportModuleConfig> moduleConfigs = null;
120  filePath = reportDirPath.toString() + File.separator + MODULE_CONFIG_FILE;
121  try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(filePath))) {
122  moduleConfigs = (Map<String, ReportModuleConfig>) in.readObject();
123  } catch (IOException | ClassNotFoundException ex) {
124  throw new ReportConfigException("Unable to read module configurations map " + filePath, ex);
125  }
126 
127  if (moduleConfigs == null || moduleConfigs.isEmpty()) {
128  return config;
129  }
130 
131  // read each ReportModuleSettings object individually
132  for (Iterator<Entry<String, ReportModuleConfig>> iterator = moduleConfigs.entrySet().iterator(); iterator.hasNext();) {
133  ReportModuleConfig moduleConfig = iterator.next().getValue();
134  if (DELETED_REPORT_MODULES.contains(moduleConfig.getModuleClassName())) {
135  // Don't try to load settings for known deleted modules
136  continue;
137  }
138  filePath = reportDirPath.toString() + File.separator + moduleConfig.getModuleClassName() + REPORT_SETTINGS_FILE_EXTENSION;
139  try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(filePath))) {
140  moduleConfig.setModuleSettings((ReportModuleSettings) in.readObject());
141  } catch (IOException | ClassNotFoundException ex) {
142  /*
143  * NOTE: we do not want to re-throw the exception because we do
144  * not want a single error while reading in a (3rd party) report
145  * module to prevent us from reading the entire reporting
146  * configuration.
147  */
148  logger.log(Level.SEVERE, "Unable to read module settings " + filePath, ex);
149  iterator.remove();
150  }
151  }
152 
153  config.setModuleConfigs(moduleConfigs);
154 
155  return config;
156  }
157 
167  static synchronized void saveConfig(ReportingConfig reportConfig) throws ReportConfigException {
168 
169  if (reportConfig == null) {
170  throw new ReportConfigException("Reporting configuration is NULL");
171  }
172 
173  // construct the configuration directory path
174  Path pathToConfigDir = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, reportConfig.getName());
175 
176  // create configuration directory
177  try {
178  Files.createDirectories(pathToConfigDir); // does not throw if directory already exists
179  } catch (IOException | SecurityException ex) {
180  throw new ReportConfigException("Failed to create reporting configuration directory " + pathToConfigDir.toString(), ex);
181  }
182 
183  // save table report settings
184  String filePath = pathToConfigDir.toString() + File.separator + TABLE_REPORT_CONFIG_FILE;
185  try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(filePath))) {
186  out.writeObject(reportConfig.getTableReportSettings());
187  } catch (IOException ex) {
188  throw new ReportConfigException("Unable to save table report configuration " + filePath, ex);
189  }
190 
191  // save file report settings
192  filePath = pathToConfigDir.toString() + File.separator + FILE_REPORT_CONFIG_FILE;
193  try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(filePath))) {
194  out.writeObject(reportConfig.getFileReportSettings());
195  } catch (IOException ex) {
196  throw new ReportConfigException("Unable to save file report configuration " + filePath, ex);
197  }
198 
199  filePath = pathToConfigDir.resolve(GENERAL_REPORT_CONFIG_FILE).toString();
200  try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(filePath))) {
201  out.writeObject(reportConfig.getGeneralReportSettings());
202  } catch (IOException ex) {
203  throw new ReportConfigException("Unable to save general report configuration " + filePath, ex);
204  }
205 
206  // save map of module configuration objects
207  filePath = pathToConfigDir.toString() + File.separator + MODULE_CONFIG_FILE;
208  try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(filePath))) {
209  out.writeObject(reportConfig.getModuleConfigs());
210  } catch (IOException ex) {
211  throw new ReportConfigException("Unable to save module configurations map " + filePath, ex);
212  }
213 
214  // save each ReportModuleSettings object individually
215  /*
216  * NOTE: This is done to protect us from errors in reading/writing 3rd
217  * party report module settings. If we were to serialize the entire
218  * ReportingConfig object, then a single error while reading in a 3rd
219  * party report module would prevent us from reading the entire
220  * reporting configuration.
221  */
222  if (reportConfig.getModuleConfigs() == null) {
223  return;
224  }
225  for (ReportModuleConfig moduleConfig : reportConfig.getModuleConfigs().values()) {
226  ReportModuleSettings settings = moduleConfig.getModuleSettings();
227  filePath = pathToConfigDir.toString() + File.separator + moduleConfig.getModuleClassName() + REPORT_SETTINGS_FILE_EXTENSION;
228  try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(filePath))) {
229  out.writeObject(settings);
230  } catch (IOException ex) {
231  throw new ReportConfigException("Unable to save module settings " + filePath, ex);
232  }
233  }
234  }
235 
243  static synchronized Set<String> getListOfReportConfigs() {
244  File reportDirPath = new File(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH);
245  Set<String> reportNameList = new TreeSet<>();
246 
247  if (!reportDirPath.exists()) {
248  return reportNameList;
249  }
250 
251  for (File file : reportDirPath.listFiles()) {
252  reportNameList.add(file.getName());
253  }
254 
255  return reportNameList;
256  }
257 
267  static synchronized boolean configExists(String configName) {
268  // construct the configuration directory path
269  Path reportDirPath = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, configName);
270  File reportDirectory = reportDirPath.toFile();
271 
272  return reportDirectory.exists();
273  }
274 
275 }

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