Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
PostgresCentralRepoSettings.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2015-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.centralrepository.datamodel;
20 
21 import java.sql.Connection;
22 import java.sql.DriverManager;
23 import java.sql.PreparedStatement;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.sql.Statement;
27 import java.util.Properties;
28 import java.util.logging.Level;
30 
38 
39  private final static Logger LOGGER = Logger.getLogger(PostgresCentralRepoSettings.class.getName());
40  private final static String VALIDATION_QUERY = "SELECT version()"; // NON-NLS
41  private final static String JDBC_BASE_URI = "jdbc:postgresql://"; // NON-NLS
42  private final static String JDBC_DRIVER = "org.postgresql.Driver"; // NON-NLS
43 
44 
47 
52  else if (choice == CentralRepoDbChoice.POSTGRESQL_MULTIUSER)
54  else
55  throw new CentralRepoException("cannot load or save postgres settings for selection: " + choice);
56  }
57 
63  this.loader = loader;
64  loadSettings();
65  }
66 
71  this(getLoaderFromSaved());
72  }
73 
74 
75  @Override
76  public void loadSettings() {
77  this.connSettings = loader.loadSettings();
78  }
79 
80  @Override
81  public void saveSettings() {
82  loader.saveSettings(connSettings);
83  }
84 
85 
86  @Override
87  public String toString() {
88  return String.format("PostgresCentralRepoSettings: [db type: postgres, host: %s:%d, db name: %s, username: %s]",
90  }
91 
92 
96  String getValidationQuery() {
97  return VALIDATION_QUERY;
98  }
99 
103  String getDriver() {
104  return JDBC_DRIVER;
105  }
106 
110  String getJDBCBaseURI() {
111  return JDBC_BASE_URI;
112  }
113 
114 
123  String getConnectionURL(boolean usePostgresDb) {
124  StringBuilder url = new StringBuilder()
125  .append(getJDBCBaseURI())
126  .append(getHost())
127  .append(":") // NON-NLS
128  .append(getPort())
129  .append("/"); // NON-NLS
130  if (usePostgresDb) {
131  url.append("postgres"); // NON-NLS
132  } else {
133  url.append(getDbName());
134  }
135 
136  return url.toString();
137  }
138 
145  Connection getEphemeralConnection(boolean usePostgresDb) {
146  Connection conn;
147  try {
148  String url = getConnectionURL(usePostgresDb);
149  Properties props = new Properties();
150  props.setProperty("user", getUserName());
151  props.setProperty("password", getPassword());
152 
153  Class.forName(getDriver());
154  conn = DriverManager.getConnection(url, props);
155  } catch (ClassNotFoundException | SQLException ex) {
156  // TODO: Determine why a connection failure (ConnectionException) re-throws
157  // the SQLException and does not print this log message?
158  LOGGER.log(Level.SEVERE, "Failed to acquire ephemeral connection to postgresql.", ex); // NON-NLS
159  conn = null;
160  }
161  return conn;
162  }
163 
170  @Override
171  public boolean verifyConnection() {
172  Connection conn = getEphemeralConnection(true);
173  if (null == conn) {
174  return false;
175  }
176 
177  boolean result = CentralRepoDbUtil.executeValidationQuery(conn, VALIDATION_QUERY);
179  return result;
180  }
181 
187  @Override
188  public boolean verifyDatabaseExists() {
189  Connection conn = getEphemeralConnection(true);
190  if (null == conn) {
191  return false;
192  }
193 
194  String sql = "SELECT datname FROM pg_catalog.pg_database WHERE lower(datname) = lower(?) LIMIT 1"; // NON-NLS
195  PreparedStatement ps = null;
196  ResultSet rs = null;
197  try {
198  ps = conn.prepareStatement(sql);
199  ps.setString(1, getDbName());
200  rs = ps.executeQuery();
201  if (rs.next()) {
202  return true;
203  }
204  } catch (SQLException ex) {
205  LOGGER.log(Level.SEVERE, "Failed to execute database existance query.", ex); // NON-NLS
206  return false;
207  } finally {
211  }
212  return false;
213  }
214 
221  @Override
222  public boolean verifyDatabaseSchema() {
223  Connection conn = getEphemeralConnection(false);
224  if (null == conn) {
225  return false;
226  }
227 
228  boolean result = CentralRepoDbUtil.schemaVersionIsSet(conn);
229 
231  return result;
232  }
233 
234  @Override
235  public boolean createDatabase() {
236  Connection conn = getEphemeralConnection(true);
237  if (null == conn) {
238  return false;
239  }
240 
241  String sql = "CREATE DATABASE %s OWNER %s"; // NON-NLS
242  try {
243  Statement stmt;
244  stmt = conn.createStatement();
245  stmt.execute(String.format(sql, getDbName(), getUserName()));
246  } catch (SQLException ex) {
247  LOGGER.log(Level.SEVERE, "Failed to execute create database statement.", ex); // NON-NLS
248  return false;
249  } finally {
251  }
252  return true;
253 
254  }
255 
256  @Override
257  public boolean deleteDatabase() {
258  Connection conn = getEphemeralConnection(true);
259  if (null == conn) {
260  return false;
261  }
262 
263  String sql = "DROP DATABASE %s"; // NON-NLS
264  try {
265  Statement stmt;
266  stmt = conn.createStatement();
267  stmt.execute(String.format(sql, getDbName()));
268  } catch (SQLException ex) {
269  LOGGER.log(Level.SEVERE, "Failed to execute drop database statement.", ex); // NON-NLS
270  return false;
271  } finally {
273  }
274  return true;
275 
276  }
277 
278 
282  public String getHost() {
283  return connSettings.getHost();
284  }
285 
289  public void setHost(String host) throws CentralRepoException {
290  connSettings.setHost(host);
291  }
292 
296  public int getPort() {
297  return connSettings.getPort();
298  }
299 
303  public void setPort(int port) throws CentralRepoException {
304  connSettings.setPort(port);
305  }
306 
313  public String getDbName() {
314  return connSettings.getDbName() == null ? null : connSettings.getDbName().toLowerCase();
315  }
316 
320  public void setDbName(String dbName) throws CentralRepoException {
321  connSettings.setDbName(dbName);
322  }
323 
327  int getBulkThreshold() {
328  return connSettings.getBulkThreshold();
329  }
330 
334  public void setBulkThreshold(int bulkThreshold) throws CentralRepoException {
335  connSettings.setBulkThreshold(bulkThreshold);
336  }
337 
341  public String getUserName() {
342  return connSettings.getUserName();
343  }
344 
348  public void setUserName(String userName) throws CentralRepoException {
349  connSettings.setUserName(userName);
350  }
351 
355  public String getPassword() {
356  return connSettings.getPassword();
357  }
358 
362  public void setPassword(String password) throws CentralRepoException {
363  connSettings.setPassword(password);
364  }
365 
366  @Override
368  if (verifyConnection()) {
369  if (verifyDatabaseExists()) {
370  if (verifyDatabaseSchema()) {
372  } else {
374  }
375  } else {
377  }
378  } else {
380  }
381  }
382 }
static boolean executeValidationQuery(Connection conn, String validationQuery)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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