Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
LogicalImagerConfigDeserializer.java
Go to the documentation of this file.
1 /*
2  * Autopsy
3  *
4  * Copyright 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.logicalimager.configuration;
20 
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonDeserializationContext;
23 import com.google.gson.JsonDeserializer;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParseException;
27 import java.lang.reflect.Type;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import org.openide.util.NbBundle;
33 
37 @NbBundle.Messages({
38  "LogicalImagerConfigDeserializer.missingRuleSetException=Missing rule-set",
39  "# {0} - key",
40  "LogicalImagerConfigDeserializer.unsupportedKeyException=Unsupported key: {0}",
41  "LogicalImagerConfigDeserializer.fullPathsException=A rule with full-paths cannot have other rule definitions",})
42 class LogicalImagerConfigDeserializer implements JsonDeserializer<LogicalImagerConfig> {
43 
44  @Override
45  public LogicalImagerConfig deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException {
46  String version = LogicalImagerConfig.getCurrentVersion();
47  boolean finalizeImageWriter = false;
48  boolean promptBeforeExit = true;
49  boolean createVHD = false;
50 
51  final JsonObject jsonObject = je.getAsJsonObject();
52  final JsonElement jsonVersion = jsonObject.get("version"); // NON-NLS
53  if (jsonVersion != null) {
54  version = jsonVersion.getAsString();
55  }
56 
57  final JsonElement jsonFinalizeImageWriter = jsonObject.get("finalize-image-writer"); // NON-NLS
58  if (jsonFinalizeImageWriter != null) {
59  finalizeImageWriter = jsonFinalizeImageWriter.getAsBoolean();
60  }
61 
62  final JsonElement jsonPromptBeforeExit = jsonObject.get("prompt-before-exit"); // NON-NLS
63  if (jsonPromptBeforeExit != null) {
64  promptBeforeExit = jsonPromptBeforeExit.getAsBoolean();
65  }
66 
67  final JsonElement jsonCreateVHD = jsonObject.get("create-VHD"); // NON-NLS
68  if (jsonCreateVHD != null) {
69  createVHD = jsonCreateVHD.getAsBoolean();
70  }
71 
72  JsonArray asJsonArray = jsonObject.get("rule-sets").getAsJsonArray(); // NON-NLS
73  if (asJsonArray == null) {
74  throw new JsonParseException(Bundle.LogicalImagerConfigDeserializer_missingRuleSetException());
75  }
76 
77  List<LogicalImagerRuleSet> ruleSets = new ArrayList<>();
78  for (JsonElement element : asJsonArray) {
79  String setName = null;
80  List<LogicalImagerRule> rules = null;
81  JsonObject asJsonObject = element.getAsJsonObject();
82  JsonElement setNameElement = asJsonObject.get("set-name");
83  setName = setNameElement.getAsString();
84  JsonElement rulesElement = asJsonObject.get("rules");
85  rules = parseRules(rulesElement.getAsJsonArray());
86  LogicalImagerRuleSet ruleSet = new LogicalImagerRuleSet(setName, rules);
87  ruleSets.add(ruleSet);
88  }
89  return new LogicalImagerConfig(version, finalizeImageWriter, promptBeforeExit, createVHD, ruleSets);
90  }
91 
92  private List<LogicalImagerRule> parseRules(JsonArray asJsonArray) {
93  List<LogicalImagerRule> rules = new ArrayList<>();
94 
95  for (JsonElement element : asJsonArray) {
96  String key1;
97  Boolean shouldSave = false;
98  Boolean shouldAlert = true;
99  String name = null;
100  String description = null;
101  List<String> extensions = null;
102  List<String> paths = null;
103  List<String> fullPaths = null;
104  List<String> filenames = null;
105  Long minFileSize = null;
106  Long maxFileSize = null;
107  Integer minDays = null;
108  Integer minDate = null;
109  Integer maxDate = null;
110 
111  Set<Map.Entry<String, JsonElement>> entrySet = element.getAsJsonObject().entrySet();
112 
113  for (Map.Entry<String, JsonElement> entry1 : entrySet) {
114  key1 = entry1.getKey();
115  switch (key1) {
116  case "shouldAlert": // NON-NLS
117  shouldAlert = entry1.getValue().getAsBoolean();
118  break;
119  case "shouldSave": // NON-NLS
120  shouldSave = entry1.getValue().getAsBoolean();
121  break;
122  case "name": // NON-NLS
123  name = entry1.getValue().getAsString();
124  break;
125  case "description": // NON-NLS
126  description = entry1.getValue().getAsString();
127  break;
128  case "extensions": // NON-NLS
129  JsonArray extensionsArray = entry1.getValue().getAsJsonArray();
130  extensions = new ArrayList<>();
131  for (JsonElement e : extensionsArray) {
132  extensions.add(e.getAsString());
133  }
134  break;
135  case "folder-names": // NON-NLS
136  JsonArray pathsArray = entry1.getValue().getAsJsonArray();
137  paths = new ArrayList<>();
138  for (JsonElement e : pathsArray) {
139  paths.add(e.getAsString());
140  }
141  break;
142  case "file-names": // NON-NLS
143  JsonArray filenamesArray = entry1.getValue().getAsJsonArray();
144  filenames = new ArrayList<>();
145  for (JsonElement e : filenamesArray) {
146  filenames.add(e.getAsString());
147  }
148  break;
149  case "full-paths": // NON-NLS
150  JsonArray fullPathsArray = entry1.getValue().getAsJsonArray();
151  fullPaths = new ArrayList<>();
152  for (JsonElement e : fullPathsArray) {
153  fullPaths.add(e.getAsString());
154  }
155  break;
156  case "size-range": // NON-NLS
157  JsonObject sizeRangeObject = entry1.getValue().getAsJsonObject();
158  Set<Map.Entry<String, JsonElement>> entrySet1 = sizeRangeObject.entrySet();
159  for (Map.Entry<String, JsonElement> entry2 : entrySet1) {
160  String sizeKey = entry2.getKey();
161  switch (sizeKey) {
162  case "min": // NON-NLS
163  minFileSize = entry2.getValue().getAsLong();
164  break;
165  case "max": // NON-NLS
166  maxFileSize = entry2.getValue().getAsLong();
167  break;
168  default:
169  throw new JsonParseException(Bundle.LogicalImagerConfigDeserializer_unsupportedKeyException(sizeKey));
170  }
171  }
172  break;
173  case "date-range": // NON-NLS
174  JsonObject dateRangeObject = entry1.getValue().getAsJsonObject();
175  Set<Map.Entry<String, JsonElement>> entrySet2 = dateRangeObject.entrySet();
176  for (Map.Entry<String, JsonElement> entry2 : entrySet2) {
177  String dateKey = entry2.getKey();
178  switch (dateKey) {
179  case "min": // NON-NLS
180  minDate = entry2.getValue().getAsInt();
181  break;
182  case "max": // NON-NLS
183  maxDate = entry2.getValue().getAsInt();
184  break;
185  case "min-days": // NON-NLS
186  minDays = entry2.getValue().getAsInt();
187  break;
188  default:
189  throw new JsonParseException(Bundle.LogicalImagerConfigDeserializer_unsupportedKeyException(dateKey));
190  }
191  }
192  break;
193  default:
194  throw new JsonParseException(Bundle.LogicalImagerConfigDeserializer_unsupportedKeyException(key1));
195  }
196  }
197 
198  // A rule with full-paths cannot have other rule definitions
199  if ((fullPaths != null && !fullPaths.isEmpty()) && ((extensions != null && !extensions.isEmpty())
200  || (paths != null && !paths.isEmpty())
201  || (filenames != null && !filenames.isEmpty()))) {
202  throw new JsonParseException(Bundle.LogicalImagerConfigDeserializer_fullPathsException());
203  }
204 
205  LogicalImagerRule rule = new LogicalImagerRule.Builder()
206  .getShouldAlert(shouldAlert)
207  .getShouldSave(shouldSave)
208  .getName(name)
209  .getDescription(description)
210  .getExtensions(extensions)
211  .getPaths(paths)
212  .getFullPaths(fullPaths)
213  .getFilenames(filenames)
214  .getMinFileSize(minFileSize)
215  .getMaxFileSize(maxFileSize)
216  .getMinDays(minDays)
217  .getMinDate(minDate)
218  .getMaxDate(maxDate)
219  .build();
220  rules.add(rule);
221  } // for
222 
223  return rules;
224  }
225 }

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.