Autopsy  4.21.0
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;
35 import org.sleuthkit.datamodel.CaseDbConnectionInfo;
36 
41  private final static Logger LOGGER = Logger.getLogger(CentralRepoPostgresSettingsUtil.class.getName());
42 
43  private static final String PASSWORD_KEY = "db.postgresql.password";
44  private static final String BULK_THRESHOLD_KEY = "db.postgresql.bulkThreshold";
45  private static final String PORT_KEY = "db.postgresql.port";
46  private static final String USER_KEY = "db.postgresql.user";
47  private static final String DBNAME_KEY = "db.postgresql.dbName";
48  private static final String HOST_KEY = "db.postgresql.host";
49 
51 
53 
58  public static synchronized CentralRepoPostgresSettingsUtil getInstance() {
59  if (instance == null)
60  instance = new CentralRepoPostgresSettingsUtil();
61 
62  return instance;
63  }
64 
66 
75  private void setValOrLog(ValueSetter setter, String value) {
76  // ignore null values as they indicate a setting that is not set yet
77  if (value == null || value.isEmpty())
78  return;
79 
80  try {
81  setter.set(value);
82  }
83  catch (CentralRepoException | NumberFormatException e) {
84  LOGGER.log(Level.WARNING, "There was an error in converting central repo postgres settings", e);
85  }
86  }
87 
91  private interface ValueSetter {
92  void set(String value) throws CentralRepoException, NumberFormatException;
93  }
94 
103 
104  CaseDbConnectionInfo muConn;
105  try {
107  } catch (UserPreferencesException ex) {
108  LOGGER.log(Level.SEVERE, "Failed to import settings from multi-user settings.", ex);
109  return settings;
110  }
111 
112  setValOrLog((v) -> settings.setHost(v), muConn.getHost());
113  setValOrLog((v) -> settings.setUserName(v), muConn.getUserName());
114  setValOrLog((v) -> settings.setPassword(v), muConn.getPassword());
115 
116  setValOrLog((v) -> settings.setPort(Integer.parseInt(v)), muConn.getPort());
117 
118  return settings;
119  }
120 
121 
130  Map<String, String> keyVals = ModuleSettings.getConfigSettings(MODULE_KEY);
131 
132 
133  setValOrLog((v) -> settings.setHost(v), keyVals.get(HOST_KEY));
134  setValOrLog((v) -> settings.setDbName(v), keyVals.get(DBNAME_KEY));
135  setValOrLog((v) -> settings.setUserName(v), keyVals.get(USER_KEY));
136 
137  setValOrLog((v) -> settings.setPort(Integer.parseInt(v)), keyVals.get(PORT_KEY));
138  setValOrLog((v) -> settings.setBulkThreshold(Integer.parseInt(v)), keyVals.get((BULK_THRESHOLD_KEY)));
139 
140  String passwordHex = keyVals.get(PASSWORD_KEY);
141  if (passwordHex != null) {
142  String password;
143  try {
144  password = TextConverter.convertHexTextToText(passwordHex);
145  } catch (TextConverterException ex) {
146  LOGGER.log(Level.WARNING, "Failed to convert password from hex text to text.", ex);
147  password = null;
148  }
149 
150  final String finalPassword = password;
151  setValOrLog((v) -> settings.setPassword(v), finalPassword);
152  }
153 
154  return settings;
155  }
156 
162  Map<String, String> map = new HashMap<String, String>();
163  map.put(HOST_KEY, settings.getHost());
164  map.put(PORT_KEY, Integer.toString(settings.getPort()));
165  map.put(DBNAME_KEY, settings.getDbName());
166  map.put(BULK_THRESHOLD_KEY, Integer.toString(settings.getBulkThreshold()));
167  map.put(USER_KEY, settings.getUserName());
168  try {
169  map.put(PASSWORD_KEY, TextConverter.convertTextToHexText(settings.getPassword())); // NON-NLS
170  } catch (TextConverterException ex) {
171  LOGGER.log(Level.SEVERE, "Failed to convert password from text to hex text.", ex);
172  }
173 
174  ModuleSettings.setConfigSettings(MODULE_KEY, map);
175  }
176 
184  return saved.equals(settings);
185  }
186 }
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-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.