From 9605d41ca800c0f3f4215046546693a21a6ca167 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sat, 28 Sep 2024 17:41:10 -0400 Subject: [PATCH] move initial database setup to dedicated methods --- .../attestation/server/AttestationServer.java | 89 ++++++++++--------- 1 file changed, 48 insertions(+), 41 deletions(-) diff --git a/src/main/java/app/attestation/server/AttestationServer.java b/src/main/java/app/attestation/server/AttestationServer.java index 554d1038..2d8feb75 100644 --- a/src/main/java/app/attestation/server/AttestationServer.java +++ b/src/main/java/app/attestation/server/AttestationServer.java @@ -276,28 +276,19 @@ private static int getUserVersion(final SQLiteConnection conn) throws SQLiteExce } } - public static void main(final String[] args) throws Exception { - Thread.currentThread().setName("Main"); - - Logger.getLogger("com.almworks.sqlite4java").setLevel(Level.OFF); - - Logger.getLogger("app.attestation").setUseParentHandlers(false); - final ConsoleHandler handler = new ConsoleHandler(); - handler.setFormatter(new JournaldFormatter()); - Logger.getLogger("app.attestation").addHandler(handler); - - final SQLiteConnection samplesConn = open(SAMPLES_DATABASE); + private static void setupSamplesDatabase() throws SQLiteException { + final SQLiteConnection conn = open(SAMPLES_DATABASE); try { - final SQLiteStatement selectCreated = samplesConn.prepare( + final SQLiteStatement selectCreated = conn.prepare( "SELECT 1 FROM sqlite_master WHERE type='table' AND name='Samples'"); if (!selectCreated.step()) { - samplesConn.exec("PRAGMA user_version = 1"); + conn.exec("PRAGMA user_version = 1"); } selectCreated.dispose(); - int userVersion = getUserVersion(samplesConn); + int userVersion = getUserVersion(conn); - createSamplesTable(samplesConn); + createSamplesTable(conn); if (userVersion < 1) { logger.log(ALERT, SAMPLES_DATABASE + " database schemas older than version 1 are no longer " + @@ -307,22 +298,24 @@ public static void main(final String[] args) throws Exception { logger.info("Finished database setup for " + SAMPLES_DATABASE); } finally { - samplesConn.dispose(); + conn.dispose(); } + } - final SQLiteConnection attestationConn = open(ATTESTATION_DATABASE); + private static void setupAttestationDatabase() throws DataFormatException, GeneralSecurityException, IOException, SQLiteException { + final SQLiteConnection conn = open(ATTESTATION_DATABASE); try { - final SQLiteStatement selectCreated = attestationConn.prepare( + final SQLiteStatement selectCreated = conn.prepare( "SELECT 1 FROM sqlite_master WHERE type='table' AND name='Configuration'"); if (!selectCreated.step()) { - attestationConn.exec("PRAGMA user_version = 13"); + conn.exec("PRAGMA user_version = 13"); } selectCreated.dispose(); - int userVersion = getUserVersion(attestationConn); + int userVersion = getUserVersion(conn); - createAttestationTables(attestationConn); - createAttestationIndices(attestationConn); + createAttestationTables(conn); + createAttestationIndices(conn); if (userVersion < 11) { logger.log(ALERT, ATTESTATION_DATABASE + " database schemas older than version 11 are no longer " + @@ -335,15 +328,15 @@ public static void main(final String[] args) throws Exception { // add pinnedAppVariant column to Devices table with default 0 value targetUserVersion = 12; if (userVersion < targetUserVersion) { - attestationConn.exec("PRAGMA foreign_keys = OFF"); - attestationConn.exec("BEGIN IMMEDIATE TRANSACTION"); + conn.exec("PRAGMA foreign_keys = OFF"); + conn.exec("BEGIN IMMEDIATE TRANSACTION"); - attestationConn.exec("ALTER TABLE Devices RENAME TO OldDevices"); - attestationConn.exec("ALTER TABLE Attestations RENAME TO OldAttestations"); + conn.exec("ALTER TABLE Devices RENAME TO OldDevices"); + conn.exec("ALTER TABLE Attestations RENAME TO OldAttestations"); - createAttestationTables(attestationConn); + createAttestationTables(conn); - attestationConn.exec(""" + conn.exec(""" INSERT INTO Devices ( fingerprint, pinnedCertificates, @@ -400,7 +393,7 @@ INSERT INTO Devices ( deletionTime FROM OldDevices"""); - attestationConn.exec(""" + conn.exec(""" INSERT INTO Attestations ( id, fingerprint, @@ -441,25 +434,25 @@ INSERT INTO Attestations ( systemUser FROM OldAttestations"""); - attestationConn.exec("DROP TABLE OldDevices"); - attestationConn.exec("DROP TABLE OldAttestations"); + conn.exec("DROP TABLE OldDevices"); + conn.exec("DROP TABLE OldAttestations"); - createAttestationIndices(attestationConn); - attestationConn.exec("PRAGMA user_version = " + targetUserVersion); - attestationConn.exec("COMMIT TRANSACTION"); + createAttestationIndices(conn); + conn.exec("PRAGMA user_version = " + targetUserVersion); + conn.exec("COMMIT TRANSACTION"); userVersion = targetUserVersion; - attestationConn.exec("PRAGMA foreign_keys = ON"); + conn.exec("PRAGMA foreign_keys = ON"); logger.info("Migrated to schema version: " + userVersion); } // update DEFLATE dictionary from 2 to 4 targetUserVersion = 13; if (userVersion < targetUserVersion) { - attestationConn.exec("BEGIN IMMEDIATE TRANSACTION"); + conn.exec("BEGIN IMMEDIATE TRANSACTION"); - final SQLiteStatement select = attestationConn.prepare( + final SQLiteStatement select = conn.prepare( "SELECT pinnedCertificates, fingerprint FROM Devices"); - final SQLiteStatement update = attestationConn.prepare( + final SQLiteStatement update = conn.prepare( "UPDATE Devices SET pinnedCertificates = ? where fingerprint = ?"); while (select.step()) { final Certificate[] chain = AttestationProtocol.decodeChain(AttestationProtocol.DEFLATE_DICTIONARY_2, select.columnBlob(0)); @@ -471,16 +464,30 @@ INSERT INTO Attestations ( select.dispose(); update.dispose(); - attestationConn.exec("PRAGMA user_version = " + targetUserVersion); - attestationConn.exec("COMMIT TRANSACTION"); + conn.exec("PRAGMA user_version = " + targetUserVersion); + conn.exec("COMMIT TRANSACTION"); userVersion = targetUserVersion; logger.info("Migrated to schema version: " + userVersion); } logger.info("Finished database setup for " + ATTESTATION_DATABASE); } finally { - attestationConn.dispose(); + conn.dispose(); } + } + + public static void main(final String[] args) throws Exception { + Thread.currentThread().setName("Main"); + + Logger.getLogger("com.almworks.sqlite4java").setLevel(Level.OFF); + + Logger.getLogger("app.attestation").setUseParentHandlers(false); + final ConsoleHandler handler = new ConsoleHandler(); + handler.setFormatter(new JournaldFormatter()); + Logger.getLogger("app.attestation").addHandler(handler); + + setupSamplesDatabase(); + setupAttestationDatabase(); final ThreadPoolExecutor executor = new ThreadPoolExecutor(32, 32, 0, TimeUnit.SECONDS, new LinkedBlockingQueue(1024),