Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
IngestProfiles.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2019 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.ingest;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.nio.file.Paths;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.logging.Level;
28 import org.apache.commons.io.FileUtils;
29 import org.apache.commons.io.FilenameUtils;
34 
38 public final class IngestProfiles {
39 
40  private static final String PROFILE_NAME_KEY = "Profile_Name";
41  private static final String PROFILE_DESC_KEY = "Profile_Description";
42  private static final String PROFILE_FILTER_KEY = "Profile_Filter";
44  private static final Logger logger = Logger.getLogger(IngestProfiles.class.getName());
45 
50  static String getIngestProfilePrefix() {
51  return SETTINGS_FILE_PREFIX;
52  }
53 
61  static String getExecutionContext(String profileName) {
62  return SETTINGS_FILE_PREFIX + profileName;
63  }
64 
72  private static String getSanitizedProfile(String executionContext) {
73  return (executionContext != null && executionContext.startsWith(getIngestProfilePrefix()))
74  ? executionContext.substring(getIngestProfilePrefix().length())
75  : executionContext;
76  }
77 
86  private static File getRootSettingsFile(String profileName) {
87  return Paths.get(
89  IngestJobSettings.getModuleSettingsResource(
90  getExecutionContext(getSanitizedProfile(profileName))) + ".properties"
91  ).toFile();
92  }
93 
102  private static File getSettingsDirectory(String profileName) {
103  return IngestJobSettings.getSavedModuleSettingsFolder(getExecutionContext(getSanitizedProfile(profileName))).toFile();
104  }
105 
111  public synchronized static List<IngestProfile> getIngestProfiles() {
112  File dir = new File(IngestJobSettings.getBaseSettingsPath());
113  // find all settings files for ingest profiles (starts with ingest profiles prefix)
114  File[] directoryListing = dir.listFiles((file) -> file.getName() != null && file.getName().startsWith(getIngestProfilePrefix()) && file.isFile());
115  List<IngestProfile> profileList = new ArrayList<>();
116  if (directoryListing != null) {
117  for (File child : directoryListing) {
118  String resourceName = FilenameUtils.removeExtension(child.getName());
119  String profileName = getSanitizedProfile(resourceName);
120  String moduleSettingsResource = IngestJobSettings.getModuleSettingsResource(resourceName);
121  String desc = ModuleSettings.getConfigSetting(moduleSettingsResource, PROFILE_DESC_KEY);
122  String fileIngestFilter = ModuleSettings.getConfigSetting(moduleSettingsResource, PROFILE_FILTER_KEY);
123  profileList.add(new IngestProfile(profileName, desc, fileIngestFilter));
124  }
125  }
126  return profileList;
127  }
128 
132  synchronized static void setProfiles(List<IngestProfile> profiles) {
133  for (IngestProfile profile : profiles) {
134  IngestProfile.saveProfile(profile);
135  }
136  }
137 
143  public static final class IngestProfile {
144 
145  private final String name;
146  private final String description;
147  private final String fileIngestFilter;
148 
156  IngestProfile(String name, String desc, String selectedFilter) {
157  this.name = name;
158  this.description = desc;
159  this.fileIngestFilter = selectedFilter;
160  }
161 
167  @Override
168  public String toString() {
169  return getName();
170  }
171 
177  String getName() {
178  return name;
179  }
180 
186  public String getDescription() {
187  return description;
188  }
189 
195  public String getFileIngestFilter() {
196  return fileIngestFilter;
197  }
198 
204  synchronized static void deleteProfile(IngestProfile selectedProfile) {
205  deleteProfile(selectedProfile.getName());
206  }
207 
213  synchronized static void deleteProfile(String profileName) {
214  try {
215  File rootSettingsFile = getRootSettingsFile(profileName);
216  File settingsDirectory = getSettingsDirectory(profileName);
217  Files.deleteIfExists(rootSettingsFile.toPath());
218  FileUtils.deleteDirectory(settingsDirectory);
219  } catch (IOException ex) {
220  logger.log(Level.WARNING, "Error deleting directory for profile " + profileName, ex);
221  }
222  }
223 
230  synchronized static void renameProfile(String oldName, String newName) {
231  if (!oldName.equals(newName)) { //if renameProfile was called with the new name being the same as the old name, it is complete already
232  File oldRootSettings = getRootSettingsFile(oldName);
233  File newRootSettings = getRootSettingsFile(newName);
234  oldRootSettings.renameTo(newRootSettings);
235 
236  File oldSettingsFolder = getSettingsDirectory(oldName);
237  File newSettingsFolder = getSettingsDirectory(newName);
238  oldSettingsFolder.renameTo(newSettingsFolder);
239  }
240  }
241 
247  synchronized static void saveProfile(IngestProfile profile) {
248  String context = IngestJobSettings.getModuleSettingsResource(getExecutionContext(profile.getName()));
249  ModuleSettings.setConfigSetting(context, PROFILE_NAME_KEY, profile.getName());
250  ModuleSettings.setConfigSetting(context, PROFILE_DESC_KEY, profile.getDescription());
251  ModuleSettings.setConfigSetting(context, PROFILE_FILTER_KEY, profile.getFileIngestFilter());
252  }
253  }
254 }
static synchronized String getConfigSetting(String moduleName, String settingName)
static String getSanitizedProfile(String executionContext)
static File getRootSettingsFile(String profileName)
static synchronized List< IngestProfile > getIngestProfiles()
static File getSettingsDirectory(String profileName)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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