Autopsy  4.14.0
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  url.append(getJDBCBaseURI());
126  url.append(getHost());
127  url.append("/"); // NON-NLS
128  if (usePostgresDb) {
129  url.append("postgres"); // NON-NLS
130  } else {
131  url.append(getDbName());
132  }
133 
134  return url.toString();
135  }
136 
143  Connection getEphemeralConnection(boolean usePostgresDb) {
144  Connection conn;
145  try {
146  String url = getConnectionURL(usePostgresDb);
147  Properties props = new Properties();
148  props.setProperty("user", getUserName());
149  props.setProperty("password", getPassword());
150 
151  Class.forName(getDriver());
152  conn = DriverManager.getConnection(url, props);
153  } catch (ClassNotFoundException | SQLException ex) {
154  // TODO: Determine why a connection failure (ConnectionException) re-throws
155  // the SQLException and does not print this log message?
156  LOGGER.log(Level.SEVERE, "Failed to acquire ephemeral connection to postgresql."); // NON-NLS
157  conn = null;
158  }
159  return conn;
160  }
161 
168  @Override
169  public boolean verifyConnection() {
170  Connection conn = getEphemeralConnection(true);
171  if (null == conn) {
172  return false;
173  }
174 
175  boolean result = CentralRepoDbUtil.executeValidationQuery(conn, VALIDATION_QUERY);
177  return result;
178  }
179 
185  @Override
186  public boolean verifyDatabaseExists() {
187  Connection conn = getEphemeralConnection(true);
188  if (null == conn) {
189  return false;
190  }
191 
192  String sql = "SELECT datname FROM pg_catalog.pg_database WHERE lower(datname) = lower(?) LIMIT 1"; // NON-NLS
193  PreparedStatement ps = null;
194  ResultSet rs = null;
195  try {
196  ps = conn.prepareStatement(sql);
197  ps.setString(1, getDbName());
198  rs = ps.executeQuery();
199  if (rs.next()) {
200  return true;
201  }
202  } catch (SQLException ex) {
203  LOGGER.log(Level.SEVERE, "Failed to execute database existance query.", ex); // NON-NLS
204  return false;
205  } finally {
209  }
210  return false;
211  }
212 
219  @Override
220  public boolean verifyDatabaseSchema() {
221  Connection conn = getEphemeralConnection(false);
222  if (null == conn) {
223  return false;
224  }
225 
226  boolean result = CentralRepoDbUtil.schemaVersionIsSet(conn);
227 
229  return result;
230  }
231 
232  @Override
233  public boolean createDatabase() {
234  Connection conn = getEphemeralConnection(true);
235  if (null == conn) {
236  return false;
237  }
238 
239  String sql = "CREATE DATABASE %s OWNER %s"; // NON-NLS
240  try {
241  Statement stmt;
242  stmt = conn.createStatement();
243  stmt.execute(String.format(sql, getDbName(), getUserName()));
244  } catch (SQLException ex) {
245  LOGGER.log(Level.SEVERE, "Failed to execute create database statement.", ex); // NON-NLS
246  return false;
247  } finally {
249  }
250  return true;
251 
252  }
253 
254  @Override
255  public boolean deleteDatabase() {
256  Connection conn = getEphemeralConnection(true);
257  if (null == conn) {
258  return false;
259  }
260 
261  String sql = "DROP DATABASE %s"; // NON-NLS
262  try {
263  Statement stmt;
264  stmt = conn.createStatement();
265  stmt.execute(String.format(sql, getDbName()));
266  } catch (SQLException ex) {
267  LOGGER.log(Level.SEVERE, "Failed to execute drop database statement.", ex); // NON-NLS
268  return false;
269  } finally {
271  }
272  return true;
273 
274  }
275 
276 
280  public String getHost() {
281  return connSettings.getHost();
282  }
283 
287  public void setHost(String host) throws CentralRepoException {
288  connSettings.setHost(host);
289  }
290 
294  public int getPort() {
295  return connSettings.getPort();
296  }
297 
301  public void setPort(int port) throws CentralRepoException {
302  connSettings.setPort(port);
303  }
304 
311  public String getDbName() {
312  return connSettings.getDbName() == null ? null : connSettings.getDbName().toLowerCase();
313  }
314 
318  public void setDbName(String dbName) throws CentralRepoException {
319  connSettings.setDbName(dbName);
320  }
321 
325  int getBulkThreshold() {
326  return connSettings.getBulkThreshold();
327  }
328 
332  public void setBulkThreshold(int bulkThreshold) throws CentralRepoException {
333  connSettings.setBulkThreshold(bulkThreshold);
334  }
335 
339  public String getUserName() {
340  return connSettings.getUserName();
341  }
342 
346  public void setUserName(String userName) throws CentralRepoException {
347  connSettings.setUserName(userName);
348  }
349 
353  public String getPassword() {
354  return connSettings.getPassword();
355  }
356 
360  public void setPassword(String password) throws CentralRepoException {
361  connSettings.setPassword(password);
362  }
363 
364  @Override
366  if (verifyConnection()) {
367  if (verifyDatabaseExists()) {
368  if (verifyDatabaseSchema()) {
370  } else {
372  }
373  } else {
375  }
376  } else {
378  }
379  }
380 }
static boolean executeValidationQuery(Connection conn, String validationQuery)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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.