Skip to content

Commit

Permalink
1.2.14 (#115)
Browse files Browse the repository at this point in the history
* 1.2.14

* 1.2.14
  • Loading branch information
acharneski authored Oct 28, 2024
1 parent 5d8c5fb commit 6b4d105
Show file tree
Hide file tree
Showing 38 changed files with 1,101 additions and 295 deletions.
2 changes: 1 addition & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ val hsqldb_version = "2.7.2"

dependencies {

implementation(group = "com.simiacryptus", name = "jo-penai", version = "1.1.10")
implementation(group = "com.simiacryptus", name = "jo-penai", version = "1.1.11")
implementation(group = "org.hsqldb", name = "hsqldb", version = hsqldb_version)

implementation("org.apache.commons:commons-text:1.11.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ open class AwsPlatform(
override val shareBase: String = System.getProperty("share_base", "https://share.simiacrypt.us"),
private val region: Region? = Region.US_EAST_1
) : CloudPlatformInterface {
private val log = LoggerFactory.getLogger(AwsPlatform::class.java)

protected open val kmsClient: KmsClient by lazy {
log.debug("Initializing KMS client for region: {}", Region.US_EAST_1)
KmsClient.builder().region(Region.US_EAST_1)
//.credentialsProvider(ProfileCredentialsProvider.create("data"))
.build()
}

protected open val s3Client: S3Client by lazy {
log.debug("Initializing S3 client for region: {}", region)
S3Client.builder()
.region(region)
.build()
Expand All @@ -35,13 +39,15 @@ open class AwsPlatform(
contentType: String,
bytes: ByteArray
): String {
log.info("Uploading {} bytes to S3 path: {}", bytes.size, path)
s3Client.putObject(
PutObjectRequest.builder()
.bucket(bucket).key(path.replace("/{2,}".toRegex(), "/").removePrefix("/"))
.contentType(contentType)
.build(),
RequestBody.fromBytes(bytes)
)
log.debug("Upload completed successfully")
return "$shareBase/$path"
}

Expand All @@ -50,38 +56,50 @@ open class AwsPlatform(
contentType: String,
request: String
): String {
log.info("Uploading string content to S3 path: {}", path)
s3Client.putObject(
PutObjectRequest.builder()
.bucket(bucket).key(path.replace("/{2,}".toRegex(), "/").removePrefix("/"))
.contentType(contentType)
.build(),
RequestBody.fromString(request)
)
log.debug("Upload completed successfully")
return "$shareBase/$path"
}


override fun encrypt(fileBytes: ByteArray, keyId: String): String? =
Base64.getEncoder().encodeToString(
override fun encrypt(fileBytes: ByteArray, keyId: String): String? {
log.info("Encrypting {} bytes using KMS key: {}", fileBytes.size, keyId)
val encryptedData = Base64.getEncoder().encodeToString(
kmsClient.encrypt(
EncryptRequest.builder()
.keyId(keyId)
.plaintext(SdkBytes.fromByteArray(fileBytes))
.build()
).ciphertextBlob().asByteArray()
)
log.debug("Encryption completed successfully")
return encryptedData
}

override fun decrypt(encryptedData: ByteArray): String = String(
kmsClient.decrypt(
DecryptRequest.builder()
.ciphertextBlob(SdkBytes.fromByteArray(Base64.getDecoder().decode(encryptedData)))
.build()
).plaintext().asByteArray(), StandardCharsets.UTF_8
)
override fun decrypt(encryptedData: ByteArray): String {
log.info("Decrypting {} bytes of data", encryptedData.size)
val decryptedData = String(
kmsClient.decrypt(
DecryptRequest.builder()
.ciphertextBlob(SdkBytes.fromByteArray(Base64.getDecoder().decode(encryptedData)))
.build()
).plaintext().asByteArray(), StandardCharsets.UTF_8
)
log.debug("Decryption completed successfully")
return decryptedData
}

companion object {
val log = LoggerFactory.getLogger(AwsPlatform::class.java)
fun get() = try {
log.info("Initializing AwsPlatform")
AwsPlatform()
} catch (e: Throwable) {
log.warn("Error initializing AWS platform", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,71 @@ open class AuthorizationManager : AuthorizationInterface {
user: User?,
operationType: AuthorizationInterface.OperationType,
) = try {
log.debug("Checking authorization for user: {}, operation: {}, application: {}", user, operationType, applicationClass)
if (isUserAuthorized("/permissions/${operationType.name.lowercase(Locale.getDefault())}.txt", user)) {
log.debug("User {} authorized for {} globally", user, operationType)
log.info("User {} authorized for {} globally", user, operationType)
true
} else if (null != applicationClass) {
val packagePath = applicationClass.`package`.name.replace('.', '/')
val opName = operationType.name.lowercase(Locale.getDefault())
log.debug("Checking application-specific authorization at path: /permissions/{}/{}.txt", packagePath, opName)
if (isUserAuthorized("/permissions/$packagePath/$opName.txt", user)) {
log.debug("User {} authorized for {} on {}", user, operationType, applicationClass)
log.info("User {} authorized for {} on {}", user, operationType, applicationClass)
true
} else {
log.debug("User {} not authorized for {} on {}", user, operationType, applicationClass)
log.warn("User {} not authorized for {} on {}", user, operationType, applicationClass)
false
}
} else {
log.debug("User {} not authorized for {} globally", user, operationType)
log.warn("User {} not authorized for {} globally", user, operationType)
false
}
} catch (e: Exception) {
log.error("Error checking authorization", e)
false
}

private fun isUserAuthorized(permissionPath: String, user: User?) =
javaClass.getResourceAsStream(permissionPath)?.use { stream ->
private fun isUserAuthorized(permissionPath: String, user: User?): Boolean {
log.debug("Checking user authorization at path: {}", permissionPath)
return javaClass.getResourceAsStream(permissionPath)?.use { stream ->
val lines = stream.bufferedReader().readLines()
log.trace("Permission file contents: {}", lines)
lines.any { line ->
matches(user, line)
}
} ?: false
} ?: run {
log.warn("Permission file not found: {}", permissionPath)
false
}
}

open fun matches(user: User?, line: String) = when {
line.equals(user?.email, ignoreCase = true) -> true // Exact match
line.startsWith("@") && user?.email?.endsWith(line.substring(1)) == true -> true // Domain match
line == "." && user != null -> true // Any user
line == "*" -> true // Any user including anonymous
else -> false
open fun matches(user: User?, line: String): Boolean {
log.trace("Matching user {} against line: {}", user, line)
return when {
line.equals(user?.email, ignoreCase = true) -> {
log.debug("Exact match found for user: {}", user)
true
}
line.startsWith("@") && user?.email?.endsWith(line.substring(1)) == true -> {
log.debug("Domain match found for user: {}", user)
true
}
line == "." && user != null -> {
log.debug("Any authenticated user match for: {}", user)
true
}
line == "*" -> {
log.debug("Any user (including anonymous) match")
true
}
else -> {
log.trace("No match found for user: {} and line: {}", user, line)
false
}
}
}

companion object {
private val log = org.slf4j.LoggerFactory.getLogger(AuthorizationManager::class.java)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,33 @@ open class UserSettingsManager : UserSettingsInterface {
private val userConfigDirectory by lazy { dataStorageRoot.resolve("users").apply { mkdirs() } }

override fun getUserSettings(user: User): UserSettings {
log.debug("Retrieving user settings for user: {}", user)
return userSettings.getOrPut(user) {
val file = File(userConfigDirectory, "$user.json")
if (file.exists()) {
try {
log.info("Loading user settings for $user from $file")
log.info("Loading existing user settings for user: {} from file: {}", user, file)
return@getOrPut JsonUtil.fromJson(file.readText(), UserSettings::class.java)
} catch (e: Throwable) {
log.warn("Error loading user settings for $user from $file", e)
log.error("Failed to load user settings for user: {} from file: {}. Creating new settings.", user, file, e)
}
}
log.info("Creating new user settings for $user at $file", RuntimeException())
log.info("User settings file not found for user: {}. Creating new settings at: {}", user, file)
return@getOrPut UserSettings()
}
}

override fun updateUserSettings(user: User, settings: UserSettings) {
log.debug("Updating user settings for user: {}", user)
userSettings[user] = settings
val file = File(userConfigDirectory, "$user.json")
file.parentFile.mkdirs()
file.writeText(JsonUtil.toJson(settings))
try {
file.writeText(JsonUtil.toJson(settings))
log.info("Successfully updated user settings for user: {} at file: {}", user, file)
} catch (e: Exception) {
log.error("Failed to write user settings for user: {} to file: {}", user, file, e)
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import java.sql.Timestamp
import java.util.*

class HSQLMetadataStorage(private val dbFile: File) : MetadataStorageInterface {
private val log = LoggerFactory.getLogger(HSQLMetadataStorage::class.java)
private val log = LoggerFactory.getLogger(javaClass)

private val connection: Connection by lazy {
log.info("Initializing HSQLMetadataStorage with database file: ${dbFile.absolutePath}")
Class.forName("org.hsqldb.jdbc.JDBCDriver")
val connection = DriverManager.getConnection("jdbc:hsqldb:file:${dbFile.absolutePath}/metadata;shutdown=true", "SA", "")
log.debug("Database connection established: $connection")
log.info("Database connection established successfully")
createSchema(connection)
connection
}

private fun createSchema(connection: Connection) {
log.info("Creating database schema if not exists")
log.debug("Attempting to create database schema if not exists")
connection.createStatement().executeUpdate(
"""
CREATE TABLE IF NOT EXISTS metadata (
Expand All @@ -36,6 +36,7 @@ class HSQLMetadataStorage(private val dbFile: File) : MetadataStorageInterface {
)
"""
)
log.info("Database schema creation completed")
}

override fun getSessionName(user: User?, session: Session): String {
Expand All @@ -47,9 +48,10 @@ class HSQLMetadataStorage(private val dbFile: File) : MetadataStorageInterface {
statement.setString(2, user?.email ?: "")
val resultSet = statement.executeQuery()
return if (resultSet.next()) {
resultSet.getString("value")
val name = resultSet.getString("value")
log.debug("Retrieved session name: $name for session: ${session.sessionId}")
name
} else {
log.debug("Session ${session.sessionId} has no name")
session.sessionId
}
}
Expand All @@ -70,6 +72,7 @@ class HSQLMetadataStorage(private val dbFile: File) : MetadataStorageInterface {
statement.setString(4, name)
statement.setTimestamp(5, Timestamp(System.currentTimeMillis()))
statement.executeUpdate()
log.info("Session name set successfully for session: ${session.sessionId}")
}

override fun getMessageIds(user: User?, session: Session): List<String> {
Expand All @@ -81,8 +84,11 @@ class HSQLMetadataStorage(private val dbFile: File) : MetadataStorageInterface {
statement.setString(2, user?.email ?: "")
val resultSet = statement.executeQuery()
return if (resultSet.next()) {
resultSet.getString("value").split(",")
val ids = resultSet.getString("value").split(",")
log.debug("Retrieved ${ids.size} message IDs for session: ${session.sessionId}")
ids
} else {
log.debug("No message IDs found for session: ${session.sessionId}")
emptyList()
}
}
Expand All @@ -103,6 +109,7 @@ class HSQLMetadataStorage(private val dbFile: File) : MetadataStorageInterface {
statement.setString(4, ids.joinToString(","))
statement.setTimestamp(5, Timestamp(System.currentTimeMillis()))
statement.executeUpdate()
log.info("Set ${ids.size} message IDs for session: ${session.sessionId}")
}

override fun getSessionTime(user: User?, session: Session): Date? {
Expand All @@ -114,14 +121,16 @@ class HSQLMetadataStorage(private val dbFile: File) : MetadataStorageInterface {
statement.setString(2, user?.email ?: "")
val resultSet = statement.executeQuery()
return if (resultSet.next()) {
val time = resultSet.getString("value")
try {
Date(resultSet.getString("value").toLong())
Date(time.toLong()).also {
log.debug("Retrieved session time: $it for session: ${session.sessionId}")
}
} catch (e: NumberFormatException) {
log.warn("Invalid session time value, falling back to timestamp")
log.warn("Invalid session time value: $time, falling back to timestamp for session: ${session.sessionId}")
resultSet.getTimestamp("timestamp")
}
} else {
log.debug("No session time found, returning current time")
Date()
}
}
Expand All @@ -142,6 +151,7 @@ class HSQLMetadataStorage(private val dbFile: File) : MetadataStorageInterface {
statement.setString(4, time.time.toString())
statement.setTimestamp(5, Timestamp(time.time))
statement.executeUpdate()
log.info("Session time set to $time for session: ${session.sessionId}")
}

override fun listSessions(path: String): List<String> {
Expand All @@ -155,7 +165,7 @@ class HSQLMetadataStorage(private val dbFile: File) : MetadataStorageInterface {
while (resultSet.next()) {
sessions.add(resultSet.getString("session_id"))
}
log.debug("Found ${sessions.size} sessions for path: $path")
log.info("Found ${sessions.size} sessions for path: $path")
return sessions
}

Expand All @@ -167,9 +177,8 @@ class HSQLMetadataStorage(private val dbFile: File) : MetadataStorageInterface {
statement.setString(1, session.sessionId)
statement.setString(2, user?.email ?: "")
statement.executeUpdate()
log.info("Deleted session: ${session.sessionId} for user: ${user?.email ?: "anonymous"}")
}

companion object {
private val log = LoggerFactory.getLogger(HSQLMetadataStorage::class.java)
}

}
Loading

0 comments on commit 6b4d105

Please sign in to comment.