Skip to content

Commit

Permalink
move initial database setup to dedicated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
thestinger committed Sep 28, 2024
1 parent 59f6034 commit 9605d41
Showing 1 changed file with 48 additions and 41 deletions.
89 changes: 48 additions & 41 deletions src/main/java/app/attestation/server/AttestationServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 " +
Expand All @@ -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 " +
Expand All @@ -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,
Expand Down Expand Up @@ -400,7 +393,7 @@ INSERT INTO Devices (
deletionTime
FROM OldDevices""");

attestationConn.exec("""
conn.exec("""
INSERT INTO Attestations (
id,
fingerprint,
Expand Down Expand Up @@ -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));
Expand All @@ -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<Runnable>(1024),
Expand Down

0 comments on commit 9605d41

Please sign in to comment.