|
| 1 | +/* |
| 2 | + * Copyright ${inceptionYear} - ${year} ${owner} |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package jetbrains.exodus.entitystore.youtrackdb |
| 18 | + |
| 19 | +import com.jetbrains.youtrack.db.api.DatabaseType |
| 20 | +import com.jetbrains.youtrack.db.api.config.GlobalConfiguration |
| 21 | +import com.jetbrains.youtrack.db.api.config.YouTrackDBConfig |
| 22 | +import com.jetbrains.youtrack.db.api.config.YouTrackDBConfigBuilder |
| 23 | +import java.util.* |
| 24 | +import kotlin.math.min |
| 25 | + |
| 26 | +class YTDBDatabaseParams private constructor( |
| 27 | + val databasePath: String, |
| 28 | + val databaseName: String, |
| 29 | + val databaseType: DatabaseType, |
| 30 | + val userName: String, |
| 31 | + val password: String, |
| 32 | + val encryptionKey: String?, |
| 33 | + val closeDatabaseInDbProvider: Boolean, |
| 34 | + val closeAfterDelayTimeout: Int, |
| 35 | + val configBuilder: YouTrackDBConfigBuilder.() -> Unit = {} |
| 36 | +) { |
| 37 | + |
| 38 | + companion object { |
| 39 | + |
| 40 | + fun builder(): Builder { |
| 41 | + return Builder() |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + val youTrackDBConfig: YouTrackDBConfig = YouTrackDBConfig.builder() |
| 46 | + .addGlobalConfigurationParameter(GlobalConfiguration.AUTO_CLOSE_AFTER_DELAY, true) |
| 47 | + .addGlobalConfigurationParameter(GlobalConfiguration.AUTO_CLOSE_DELAY, closeAfterDelayTimeout) |
| 48 | + .addGlobalConfigurationParameter(GlobalConfiguration.NON_TX_READS_WARNING_MODE, "SILENT") |
| 49 | + .apply { |
| 50 | + encryptionKey?.let { addGlobalConfigurationParameter(GlobalConfiguration.STORAGE_ENCRYPTION_KEY, it) } |
| 51 | + } |
| 52 | + .apply(configBuilder) |
| 53 | + .build() |
| 54 | + |
| 55 | + @Suppress("unused") |
| 56 | + class Builder internal constructor() { |
| 57 | + |
| 58 | + private var databasePath: String = "" |
| 59 | + private var databaseName: String = "" |
| 60 | + private var databaseType: DatabaseType = DatabaseType.MEMORY |
| 61 | + private var userName: String = "admin" |
| 62 | + private var password: String = "admin" |
| 63 | + private var closeAfterDelayTimeout: Int = 10 |
| 64 | + private var encryptionKey: String? = null |
| 65 | + private var closeDatabaseInDbProvider = true |
| 66 | + private var configBuilder: YouTrackDBConfigBuilder.() -> Unit = {} |
| 67 | + |
| 68 | + fun withDatabasePath(databaseUrl: String) = apply { |
| 69 | + this.databasePath = databaseUrl |
| 70 | + } |
| 71 | + |
| 72 | + fun withDatabaseName(databaseName: String) = apply { |
| 73 | + this.databaseName = databaseName |
| 74 | + } |
| 75 | + |
| 76 | + fun withDatabaseType(databaseType: DatabaseType) = apply { |
| 77 | + this.databaseType = databaseType |
| 78 | + } |
| 79 | + |
| 80 | + fun withUserName(userName: String) = apply { |
| 81 | + this.userName = userName |
| 82 | + } |
| 83 | + |
| 84 | + fun withPassword(password: String) = apply { |
| 85 | + this.password = password |
| 86 | + } |
| 87 | + |
| 88 | + fun withCloseDatabaseInDbProvider(closeDatabaseInDbProvider: Boolean) = apply { |
| 89 | + this.closeDatabaseInDbProvider = closeDatabaseInDbProvider |
| 90 | + } |
| 91 | + |
| 92 | + fun withCloseAfterDelayTimeout(closeAfterDelayTimeout: Int) = apply { |
| 93 | + this.closeAfterDelayTimeout = closeAfterDelayTimeout |
| 94 | + } |
| 95 | + |
| 96 | + fun withEncryptionKey(encryptionKey: ByteArray) = apply { |
| 97 | + this.encryptionKey = Base64.getEncoder().encodeToString(encryptionKey) |
| 98 | + } |
| 99 | + |
| 100 | + fun withEncryptionKey(encryptionKey: String) = apply { |
| 101 | + this.encryptionKey = encryptionKey |
| 102 | + } |
| 103 | + |
| 104 | + fun withHexEncryptionKey(key: String, iv: Long) = apply { |
| 105 | + require(encryptionKey == null) { "Cipher is already initialized" } |
| 106 | + // Truncate the key to 16 bytes (32 hex symbols = 16 bytes) according to the YouTrackDB requirements |
| 107 | + val truncatedHex = key.substring(0, min(32, key.length)) |
| 108 | + // 16 bytes hex + 8 bytes long iv = 24 bytes |
| 109 | + val bytes = HexFormat.of().parseHex(truncatedHex) + iv.toByteArray() |
| 110 | + withEncryptionKey(bytes) |
| 111 | + } |
| 112 | + |
| 113 | + fun withConfigBuilder(tweakConfig: YouTrackDBConfigBuilder.() -> Unit) = apply { |
| 114 | + this.configBuilder = tweakConfig |
| 115 | + } |
| 116 | + |
| 117 | + fun build(): YTDBDatabaseParams { |
| 118 | + return YTDBDatabaseParams( |
| 119 | + databasePath, |
| 120 | + databaseName, |
| 121 | + databaseType, |
| 122 | + userName, |
| 123 | + password, |
| 124 | + encryptionKey, |
| 125 | + closeDatabaseInDbProvider, |
| 126 | + closeAfterDelayTimeout, |
| 127 | + configBuilder |
| 128 | + ) |
| 129 | + } |
| 130 | + |
| 131 | + private fun Long.toByteArray(): ByteArray { |
| 132 | + return ByteArray(8) { i -> |
| 133 | + (this shr (i * 8) and 0xFF).toByte() |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | + |
0 commit comments