diff --git a/src/main/java/io/kamax/mxisd/Mxisd.java b/src/main/java/io/kamax/mxisd/Mxisd.java index b7d8856..6d3524b 100644 --- a/src/main/java/io/kamax/mxisd/Mxisd.java +++ b/src/main/java/io/kamax/mxisd/Mxisd.java @@ -27,6 +27,8 @@ import io.kamax.mxisd.auth.AuthProviders; import io.kamax.mxisd.backend.IdentityStoreSupplier; import io.kamax.mxisd.backend.sql.synapse.Synapse; import io.kamax.mxisd.config.MxisdConfig; +import io.kamax.mxisd.config.PostgresqlStorageConfig; +import io.kamax.mxisd.config.StorageConfig; import io.kamax.mxisd.crypto.CryptoFactory; import io.kamax.mxisd.crypto.KeyManager; import io.kamax.mxisd.crypto.SignatureManager; @@ -109,7 +111,20 @@ public class Mxisd { IdentityServerUtils.setHttpClient(httpClient); srvFetcher = new RemoteIdentityServerFetcher(httpClient); - store = new OrmLiteSqlStorage(cfg); + StorageConfig.BackendEnum storageBackend = cfg.getStorage().getBackend(); + StorageConfig.Provider storageProvider = cfg.getStorage().getProvider(); + switch (storageBackend) { + case sqlite: + store = new OrmLiteSqlStorage(storageBackend, storageProvider.getSqlite().getDatabase()); + break; + case postgresql: + PostgresqlStorageConfig postgresql = storageProvider.getPostgresql(); + store = new OrmLiteSqlStorage(storageBackend, postgresql.getDatabase(), postgresql.getUsername(), postgresql.getPassword()); + break; + default: + throw new IllegalStateException("Storage provider hasn't been configured"); + } + keyMgr = CryptoFactory.getKeyManager(cfg.getKey()); signMgr = CryptoFactory.getSignatureManager(cfg, keyMgr); clientDns = new ClientDnsOverwrite(cfg.getDns().getOverwrite()); diff --git a/src/main/java/io/kamax/mxisd/config/PostgresqlStorageConfig.java b/src/main/java/io/kamax/mxisd/config/PostgresqlStorageConfig.java new file mode 100644 index 0000000..eae78c3 --- /dev/null +++ b/src/main/java/io/kamax/mxisd/config/PostgresqlStorageConfig.java @@ -0,0 +1,54 @@ +/* + * mxisd - Matrix Identity Server Daemon + * Copyright (C) 2017 Kamax Sarl + * + * https://www.kamax.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package io.kamax.mxisd.config; + +public class PostgresqlStorageConfig { + + private String database; + + private String username; + + private String password; + + public String getDatabase() { + return database; + } + + public void setDatabase(String database) { + this.database = database; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/src/main/java/io/kamax/mxisd/config/StorageConfig.java b/src/main/java/io/kamax/mxisd/config/StorageConfig.java index 7c28f22..01f1ae7 100644 --- a/src/main/java/io/kamax/mxisd/config/StorageConfig.java +++ b/src/main/java/io/kamax/mxisd/config/StorageConfig.java @@ -21,14 +21,21 @@ package io.kamax.mxisd.config; import io.kamax.mxisd.exception.ConfigurationException; -import org.apache.commons.lang.StringUtils; public class StorageConfig { + public enum BackendEnum { + sqlite, + + postgresql + } + public static class Provider { private SQLiteStorageConfig sqlite = new SQLiteStorageConfig(); + private PostgresqlStorageConfig postgresql = new PostgresqlStorageConfig(); + public SQLiteStorageConfig getSqlite() { return sqlite; } @@ -37,16 +44,23 @@ public class StorageConfig { this.sqlite = sqlite; } + public PostgresqlStorageConfig getPostgresql() { + return postgresql; + } + + public void setPostgresql(PostgresqlStorageConfig postgresql) { + this.postgresql = postgresql; + } } - private String backend = "sqlite"; + private BackendEnum backend = BackendEnum.sqlite; // or postgresql private Provider provider = new Provider(); - public String getBackend() { + public BackendEnum getBackend() { return backend; } - public void setBackend(String backend) { + public void setBackend(BackendEnum backend) { this.backend = backend; } @@ -59,7 +73,7 @@ public class StorageConfig { } public void build() { - if (StringUtils.isBlank(getBackend())) { + if (getBackend() == null) { throw new ConfigurationException("storage.backend"); } } diff --git a/src/main/java/io/kamax/mxisd/storage/ormlite/OrmLiteSqlStorage.java b/src/main/java/io/kamax/mxisd/storage/ormlite/OrmLiteSqlStorage.java index 24457fa..32c7b36 100644 --- a/src/main/java/io/kamax/mxisd/storage/ormlite/OrmLiteSqlStorage.java +++ b/src/main/java/io/kamax/mxisd/storage/ormlite/OrmLiteSqlStorage.java @@ -28,8 +28,8 @@ import com.j256.ormlite.stmt.QueryBuilder; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils; import io.kamax.matrix.ThreePid; -import io.kamax.mxisd.config.MxisdConfig; import io.kamax.mxisd.config.PolicyConfig; +import io.kamax.mxisd.config.StorageConfig; import io.kamax.mxisd.exception.ConfigurationException; import io.kamax.mxisd.exception.InternalServerError; import io.kamax.mxisd.exception.InvalidCredentialsException; @@ -92,21 +92,21 @@ public class OrmLiteSqlStorage implements IStorage { private Dao hashDao; private Dao changelogDao; - public OrmLiteSqlStorage(MxisdConfig cfg) { - this(cfg.getStorage().getBackend(), cfg.getStorage().getProvider().getSqlite().getDatabase()); + public OrmLiteSqlStorage(StorageConfig.BackendEnum backend, String path) { + this(backend, path, null, null); } - public OrmLiteSqlStorage(String backend, String path) { - if (StringUtils.isBlank(backend)) { + public OrmLiteSqlStorage(StorageConfig.BackendEnum backend, String database, String username, String password) { + if (backend == null) { throw new ConfigurationException("storage.backend"); } - if (StringUtils.isBlank(path)) { + if (StringUtils.isBlank(database)) { throw new ConfigurationException("Storage destination cannot be empty"); } withCatcher(() -> { - ConnectionSource connPool = new JdbcConnectionSource("jdbc:" + backend + ":" + path); + ConnectionSource connPool = new JdbcConnectionSource("jdbc:" + backend + ":" + database, username, password); changelogDao = createDaoAndTable(connPool, ChangelogDao.class); invDao = createDaoAndTable(connPool, ThreePidInviteIO.class); expInvDao = createDaoAndTable(connPool, HistoricalThreePidInviteIO.class); diff --git a/src/test/java/io/kamax/mxisd/test/storage/OrmLiteSqlStorageTest.java b/src/test/java/io/kamax/mxisd/test/storage/OrmLiteSqlStorageTest.java index 3526820..82a5b27 100644 --- a/src/test/java/io/kamax/mxisd/test/storage/OrmLiteSqlStorageTest.java +++ b/src/test/java/io/kamax/mxisd/test/storage/OrmLiteSqlStorageTest.java @@ -20,6 +20,7 @@ package io.kamax.mxisd.test.storage; +import io.kamax.mxisd.config.StorageConfig; import io.kamax.mxisd.storage.ormlite.OrmLiteSqlStorage; import org.junit.Test; @@ -29,14 +30,14 @@ public class OrmLiteSqlStorageTest { @Test public void insertAsTxnDuplicate() { - OrmLiteSqlStorage store = new OrmLiteSqlStorage("sqlite", ":memory:"); + OrmLiteSqlStorage store = new OrmLiteSqlStorage(StorageConfig.BackendEnum.sqlite, ":memory:"); store.insertTransactionResult("mxisd", "1", Instant.now(), "{}"); store.insertTransactionResult("mxisd", "2", Instant.now(), "{}"); } @Test(expected = RuntimeException.class) public void insertAsTxnSame() { - OrmLiteSqlStorage store = new OrmLiteSqlStorage("sqlite", ":memory:"); + OrmLiteSqlStorage store = new OrmLiteSqlStorage(StorageConfig.BackendEnum.sqlite, ":memory:"); store.insertTransactionResult("mxisd", "1", Instant.now(), "{}"); store.insertTransactionResult("mxisd", "1", Instant.now(), "{}"); }