Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
CentralRepoPostgresSettingsUtil.java
Go to the documentation of this file.
1 /*
2  *
3  * Autopsy Forensic Browser
4  *
5  * Copyright 2012-2020 Basis Technology Corp.
6  *
7  * Copyright 2012 42six Solutions.
8  * Contact: aebadirad <at> 42six <dot> com
9  * Project Contact/Architect: carrier <at> sleuthkit <dot> org
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.sleuthkit.autopsy.centralrepository.datamodel;
24 
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.logging.Level;
34 import org.sleuthkit.datamodel.CaseDbConnectionInfo;
35 
40  private final static Logger LOGGER = Logger.getLogger(CentralRepoPostgresSettingsUtil.class.getName());
41 
42  private static final String PASSWORD_KEY = "db.postgresql.password";
43  private static final String BULK_THRESHOLD_KEY = "db.postgresql.bulkThreshold";
44  private static final String PORT_KEY = "db.postgresql.port";
45  private static final String USER_KEY = "db.postgresql.user";
46  private static final String DBNAME_KEY = "db.postgresql.dbName";
47  private static final String HOST_KEY = "db.postgresql.host";
48 
49  private static final String MODULE_KEY = "CentralRepository";
50 
52 
57  public static synchronized CentralRepoPostgresSettingsUtil getInstance() {
58  if (instance == null)
59  instance = new CentralRepoPostgresSettingsUtil();
60 
61  return instance;
62  }
63 
65 
74  private void setValOrLog(ValueSetter setter, String value) {
75  // ignore null values as they indicate a setting that is not set yet
76  if (value == null || value.isEmpty())
77  return;
78 
79  try {
80  setter.set(value);
81  }
82  catch (CentralRepoException | NumberFormatException e) {
83  LOGGER.log(Level.WARNING, "There was an error in converting central repo postgres settings", e);
84  }
85  }
86 
90  private interface ValueSetter {
91  void set(String value) throws CentralRepoException, NumberFormatException;
92  }
93 
102 
103  CaseDbConnectionInfo muConn;
104  try {
106  } catch (UserPreferencesException ex) {
107  LOGGER.log(Level.SEVERE, "Failed to import settings from multi-user settings.", ex);
108  return settings;
109  }
110 
111  setValOrLog((v) -> settings.setHost(v), muConn.getHost());
112  setValOrLog((v) -> settings.setUserName(v), muConn.getUserName());
113  setValOrLog((v) -> settings.setPassword(v), muConn.getPassword());
114 
115  setValOrLog((v) -> settings.setPort(Integer.parseInt(v)), muConn.getPort());
116 
117  return settings;
118  }
119 
120 
129  Map<String, String> keyVals = ModuleSettings.getConfigSettings(MODULE_KEY);
130 
131 
132  setValOrLog((v) -> settings.setHost(v), keyVals.get(HOST_KEY));
133  setValOrLog((v) -> settings.setDbName(v), keyVals.get(DBNAME_KEY));
134  setValOrLog((v) -> settings.setUserName(v), keyVals.get(USER_KEY));
135 
136  setValOrLog((v) -> settings.setPort(Integer.parseInt(v)), keyVals.get(PORT_KEY));
137  setValOrLog((v) -> settings.setBulkThreshold(Integer.parseInt(v)), keyVals.get((BULK_THRESHOLD_KEY)));
138 
139  String passwordHex = keyVals.get(PASSWORD_KEY);
140  if (passwordHex != null) {
141  String password;
142  try {
143  password = TextConverter.convertHexTextToText(passwordHex);
144  } catch (TextConverterException ex) {
145  LOGGER.log(Level.WARNING, "Failed to convert password from hex text to text.", ex);
146  password = null;
147  }
148 
149  final String finalPassword = password;
150  setValOrLog((v) -> settings.setPassword(v), finalPassword);
151  }
152 
153  return settings;
154  }
155 
161  Map<String, String> map = new HashMap<String, String>();
162  map.put(HOST_KEY, settings.getHost());
163  map.put(PORT_KEY, Integer.toString(settings.getPort()));
164  map.put(DBNAME_KEY, settings.getDbName());
165  map.put(BULK_THRESHOLD_KEY, Integer.toString(settings.getBulkThreshold()));
166  map.put(USER_KEY, settings.getUserName());
167  try {
168  map.put(PASSWORD_KEY, TextConverter.convertTextToHexText(settings.getPassword())); // NON-NLS
169  } catch (TextConverterException ex) {
170  LOGGER.log(Level.SEVERE, "Failed to convert password from text to hex text.", ex);
171  }
172 
173  ModuleSettings.setConfigSettings(MODULE_KEY, map);
174  }
175 
183  return saved.equals(settings);
184  }
185 }
static CaseDbConnectionInfo getDatabaseConnectionInfo()
static synchronized void setConfigSettings(String moduleName, Map< String, String > settings)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static String convertTextToHexText(String property)
static synchronized Map< String, String > getConfigSettings(String moduleName)
static String convertHexTextToText(String property)

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