Skip to content

Commit

Permalink
unify creation of attestation tables/indices
Browse files Browse the repository at this point in the history
  • Loading branch information
thestinger committed Nov 28, 2021
1 parent f2a3330 commit 39737e9
Showing 1 changed file with 28 additions and 43 deletions.
71 changes: 28 additions & 43 deletions src/main/java/app/attestation/server/AttestationServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ static void open(final SQLiteConnection conn, final boolean readOnly) throws SQL
conn.exec("PRAGMA journal_mode = WAL");
}

private static void createAccountsTable(final SQLiteConnection conn) throws SQLiteException {
private static void createAttestationTablesAndIndices(final SQLiteConnection conn) throws SQLiteException {
conn.exec(
"CREATE TABLE IF NOT EXISTS Configuration (\n" +
"key TEXT PRIMARY KEY NOT NULL,\n" +
"value NOT NULL\n" +
")");

conn.exec(
"CREATE TABLE IF NOT EXISTS Accounts (\n" +
"userId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
Expand All @@ -132,14 +138,29 @@ private static void createAccountsTable(final SQLiteConnection conn) throws SQLi
"verifyInterval INTEGER NOT NULL,\n" +
"alertDelay INTEGER NOT NULL\n" +
")");
}

private static void createAccountsIndices(final SQLiteConnection conn) throws SQLiteException {
conn.exec("CREATE INDEX IF NOT EXISTS Accounts_loginTime " +
"ON Accounts (loginTime)");
}

private static void createDevicesTable(final SQLiteConnection conn) throws SQLiteException {
conn.exec(
"CREATE TABLE IF NOT EXISTS EmailAddresses (\n" +
"userId INTEGER NOT NULL REFERENCES Accounts (userId) ON DELETE CASCADE,\n" +
"address TEXT NOT NULL,\n" +
"PRIMARY KEY (userId, address)\n" +
")");

conn.exec(
"CREATE TABLE IF NOT EXISTS Sessions (\n" +
"sessionId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
"userId INTEGER NOT NULL REFERENCES Accounts (userId) ON DELETE CASCADE,\n" +
"cookieToken BLOB NOT NULL,\n" +
"requestToken BLOB NOT NULL,\n" +
"expiryTime INTEGER NOT NULL\n" +
")");
conn.exec("CREATE INDEX IF NOT EXISTS Sessions_expiryTime " +
"ON Sessions (expiryTime)");
conn.exec("CREATE INDEX IF NOT EXISTS Sessions_userId " +
"ON Sessions (userId)");

conn.exec(
"CREATE TABLE IF NOT EXISTS Devices (\n" +
"fingerprint BLOB NOT NULL PRIMARY KEY,\n" +
Expand Down Expand Up @@ -171,9 +192,6 @@ private static void createDevicesTable(final SQLiteConnection conn) throws SQLit
"userId INTEGER NOT NULL REFERENCES Accounts (userId) ON DELETE CASCADE,\n" +
"deletionTime INTEGER\n" +
")");
}

private static void createDevicesIndices(final SQLiteConnection conn) throws SQLiteException {
conn.exec("CREATE INDEX IF NOT EXISTS Devices_userId_verifiedTimeFirst " +
"ON Devices (userId, verifiedTimeFirst)");
conn.exec("CREATE INDEX IF NOT EXISTS Devices_userId_verifiedTimeLast_deletionTimeNull " +
Expand All @@ -182,9 +200,7 @@ private static void createDevicesIndices(final SQLiteConnection conn) throws SQL
"ON Devices (deletionTime) WHERE deletionTime IS NOT NULL");
conn.exec("CREATE INDEX IF NOT EXISTS Devices_verifiedTimeLast_deletionTimeNull " +
"ON Devices (verifiedTimeLast) WHERE deletionTime IS NULL");
}

private static void createAttestationsTable(final SQLiteConnection conn) throws SQLiteException {
conn.exec(
"CREATE TABLE IF NOT EXISTS Attestations (\n" +
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
Expand All @@ -194,9 +210,6 @@ private static void createAttestationsTable(final SQLiteConnection conn) throws
"teeEnforced TEXT NOT NULL,\n" +
"osEnforced TEXT NOT NULL\n" +
")");
}

private static void createAttestationsIndices(final SQLiteConnection conn) throws SQLiteException {
conn.exec("CREATE INDEX IF NOT EXISTS Attestations_fingerprint_id " +
"ON Attestations (fingerprint, id)");
}
Expand Down Expand Up @@ -230,35 +243,7 @@ public static void main(final String[] args) throws Exception {
getUserVersion.dispose();
logger.info("Existing schema version: " + userVersion);

attestationConn.exec(
"CREATE TABLE IF NOT EXISTS Configuration (\n" +
"key TEXT PRIMARY KEY NOT NULL,\n" +
"value NOT NULL\n" +
")");
createAccountsTable(attestationConn);
createAccountsIndices(attestationConn);
attestationConn.exec(
"CREATE TABLE IF NOT EXISTS EmailAddresses (\n" +
"userId INTEGER NOT NULL REFERENCES Accounts (userId) ON DELETE CASCADE,\n" +
"address TEXT NOT NULL,\n" +
"PRIMARY KEY (userId, address)\n" +
")");
attestationConn.exec(
"CREATE TABLE IF NOT EXISTS Sessions (\n" +
"sessionId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
"userId INTEGER NOT NULL REFERENCES Accounts (userId) ON DELETE CASCADE,\n" +
"cookieToken BLOB NOT NULL,\n" +
"requestToken BLOB NOT NULL,\n" +
"expiryTime INTEGER NOT NULL\n" +
")");
attestationConn.exec("CREATE INDEX IF NOT EXISTS Sessions_expiryTime " +
"ON Sessions (expiryTime)");
attestationConn.exec("CREATE INDEX IF NOT EXISTS Sessions_userId " +
"ON Sessions (userId)");
createDevicesTable(attestationConn);
createDevicesIndices(attestationConn);
createAttestationsTable(attestationConn);
createAttestationsIndices(attestationConn);
createAttestationTablesAndIndices(attestationConn);

attestationConn.exec("INSERT OR IGNORE INTO Configuration " +
"(key, value) VALUES ('backups', 0)");
Expand Down

0 comments on commit 39737e9

Please sign in to comment.