Skip to content

Commit

Permalink
Merge branch 'feature/sql-playground-sharing' into 1481-sql-runner-su…
Browse files Browse the repository at this point in the history
…pport-for-playground-sharing
  • Loading branch information
Zitrone44 authored Nov 22, 2023
2 parents a2b285f + 4249443 commit c7c5dc5
Show file tree
Hide file tree
Showing 86 changed files with 2,088 additions and 410 deletions.
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,24 @@ services:
start_period: 10s
retries: 3

psql-sharing:
image: postgres:14
restart: unless-stopped
environment:
POSTGRES_PASSWORD: R!7pWqY@K5zE3Xt&g9L1MfD
PGDATA: /var/lib/postgresql/data/pgdata
volumes:
- ./data/psql-sharing:/var/lib/postgresql/data
networks:
fbs:
healthcheck:
test: pg_isready
timeout: 10s
interval: 10s
start_period: 10s
retries: 3


mongodb:
image: mongo:5.0.6
restart: unless-stopped
Expand Down Expand Up @@ -170,6 +188,7 @@ services:
depends_on:
- mysql-checker
- psql-checker
- psql-sharing
ports:
- "127.0.0.1:8081:8081"
environment:
Expand All @@ -187,6 +206,9 @@ services:
SQL_PLAYGROUND_PSQL_SERVER_USERNAME: postgres
HMAC_SECRET: uigbduhegafudegufqu8o3q4tgru4ieubfiel
RUNNER_SQL_CHECKER_IMAGE: feedbacksystem_sql-checker
PSQL_SHARING_SERVER_URL: jdbc:postgresql://psql-sharing:5432/?allowMultiQueries=true
PSQL_SHARING_SERVER_PASSWORD: R!7pWqY@K5zE3Xt&g9L1MfD
PSQL_SHARING_SERVER_USERNAME: postgres
volumes:
- /tmp/feebi:/dockertemp # A temp dir where docker image stores task submissions temporarily
- /var/run/docker.sock:/var/run/docker.sock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ class PlaygroundController(
return databaseRepository.save(db)
}

@PostMapping("/{dbId}/dump")
@ResponseBody
fun createSharePlayground(@CurrentToken currentToken: LegacyToken, @PathVariable("dbId") dbId: Int): String {
val currentActiveDb = databaseRepository.findByOwner_IdAndIdAndDeleted(currentToken.id, dbId, false) ?: throw NotFoundException()
return sqlPlaygroundCheckerService.createSharePlayground(currentActiveDb)
}

@PostMapping("/{dbId}/reset")
@ResponseBody
@ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import de.thm.ii.fbs.services.v2.persistence.SqlPlaygroundQueryRepository
import de.thm.ii.fbs.utils.v2.exceptions.NotFoundException
import org.springframework.data.repository.findByIdOrNull
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*

Check warning on line 14 in modules/fbs-core/api/src/main/kotlin/de/thm/ii/fbs/controller/v2/RunnerApiController.kt

View workflow job for this annotation

GitHub Actions / fbs-core.api / Lint

Wildcard import (cannot be auto-corrected)

@RestController
class RunnerApiController(
Expand All @@ -31,6 +28,7 @@ class RunnerApiController(
queryRepository.save(query)
}


Check warning on line 31 in modules/fbs-core/api/src/main/kotlin/de/thm/ii/fbs/controller/v2/RunnerApiController.kt

View workflow job for this annotation

GitHub Actions / fbs-core.api / Lint

Needless blank line(s)
private fun updateAllEntity(query: SqlPlaygroundQuery, result: SqlPlaygroundRunnerResult) {
// Do not update the entity if the query had an error, as the checker will not return db information if there was an error
if (result.error) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package de.thm.ii.fbs.model.v2.checker
import com.fasterxml.jackson.annotation.JsonValue

enum class RunnerType(@JsonValue val label: String) {
SQL_PLAYGROUND("sql-playground")
SQL_PLAYGROUND("sql-playground"),
SHARE_PLAYGROUND("share")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.thm.ii.fbs.model.v2.checker

Check warning on line 1 in modules/fbs-core/api/src/main/kotlin/de/thm/ii/fbs/model/v2/checker/SharePlaygroundArguments.kt

View workflow job for this annotation

GitHub Actions / fbs-core.api / Lint

File must end with a newline (\n)

import com.fasterxml.jackson.annotation.JsonProperty

data class SharePlaygroundArguments(
@JsonProperty("user")
val user: RunnerUser,
@JsonProperty("database")
val database: RunnerDatabase,
@JsonProperty("runner")
val runner: Runner = Runner(RunnerType.SHARE_PLAYGROUND)
) : RunnerArguments()
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.thm.ii.fbs.model.v2.security

Check warning on line 1 in modules/fbs-core/api/src/main/kotlin/de/thm/ii/fbs/model/v2/security/SharePlaygroundToken.kt

View workflow job for this annotation

GitHub Actions / fbs-core.api / Lint

File must end with a newline (\n)
import java.time.LocalDateTime
import javax.persistence.Entity
import javax.persistence.Id

@Entity
class SharePlaygroundToken(
@Id
val token: String,
val userId: Int,
val dbId: Int,
val expiryTime: LocalDateTime,
val uri: String
)
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package de.thm.ii.fbs.services.v2.checker

import de.thm.ii.fbs.model.v2.checker.RunnerDatabase
import de.thm.ii.fbs.model.v2.checker.RunnerUser
import de.thm.ii.fbs.model.v2.checker.SqlPlaygroundRunnerArguments
import de.thm.ii.fbs.model.v2.checker.SqlPlaygroundRunnerDeleteArguments
import de.thm.ii.fbs.model.v2.checker.*

Check warning on line 3 in modules/fbs-core/api/src/main/kotlin/de/thm/ii/fbs/services/v2/checker/SqlPlaygroundCheckerService.kt

View workflow job for this annotation

GitHub Actions / fbs-core.api / Lint

Wildcard import (cannot be auto-corrected)
import de.thm.ii.fbs.model.v2.playground.SqlPlaygroundDatabase
import de.thm.ii.fbs.model.v2.playground.SqlPlaygroundQuery
import de.thm.ii.fbs.model.v2.security.SharePlaygroundToken
import de.thm.ii.fbs.services.v2.persistence.SharePlaygroundTokenRepository
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import java.time.LocalDateTime
import java.util.*

@Service
class SqlPlaygroundCheckerService(
@Value("\${services.masterRunner.insecure}")
insecure: Boolean,
@Value("\${services.masterRunner.url}")
private val masterRunnerURL: String
private val masterRunnerURL: String,
private val sharePlaygroundTokenRepository: SharePlaygroundTokenRepository,

Check warning on line 19 in modules/fbs-core/api/src/main/kotlin/de/thm/ii/fbs/services/v2/checker/SqlPlaygroundCheckerService.kt

View workflow job for this annotation

GitHub Actions / fbs-core.api / Lint

Unnecessary trailing comma before ")"
) : RemoteCheckerV2Service(insecure, masterRunnerURL) {

fun submit(query: SqlPlaygroundQuery) {
Expand All @@ -31,6 +33,18 @@ class SqlPlaygroundCheckerService(
)
}

fun createSharePlayground(db: SqlPlaygroundDatabase): String {
val token = UUID.randomUUID().toString()
val expiryTime = LocalDateTime.now()
val uri = this.sendToRunner(
SharePlaygroundArguments(
RunnerUser(db.owner.id!!, db.owner.username),
RunnerDatabase(db.id!!, db.name)
))

Check warning on line 43 in modules/fbs-core/api/src/main/kotlin/de/thm/ii/fbs/services/v2/checker/SqlPlaygroundCheckerService.kt

View workflow job for this annotation

GitHub Actions / fbs-core.api / Lint

Missing newline before ")"

Check warning on line 43 in modules/fbs-core/api/src/main/kotlin/de/thm/ii/fbs/services/v2/checker/SqlPlaygroundCheckerService.kt

View workflow job for this annotation

GitHub Actions / fbs-core.api / Lint

Missing newline before ")"
sharePlaygroundTokenRepository.save(SharePlaygroundToken(token, db.owner.id!!, db.id!!, expiryTime, uri.toString()))
return uri.toString()
}

fun deleteDatabase(database: SqlPlaygroundDatabase, userId: Int, username: String) {
this.sendToRunner(
SqlPlaygroundRunnerDeleteArguments(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package de.thm.ii.fbs.services.v2.persistence

Check warning on line 1 in modules/fbs-core/api/src/main/kotlin/de/thm/ii/fbs/services/v2/persistence/SharePlaygroundTokenRepository.kt

View workflow job for this annotation

GitHub Actions / fbs-core.api / Lint

File must end with a newline (\n)

import de.thm.ii.fbs.model.v2.security.SharePlaygroundToken
import org.springframework.data.jpa.repository.JpaRepository
import java.time.LocalDateTime

interface SharePlaygroundTokenRepository : JpaRepository<SharePlaygroundToken, String> {
fun findAllByExpiryTimeBefore(now: LocalDateTime?): List<SharePlaygroundToken>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import de.thm.ii.fbs.model.v2.security.SharePlaygroundToken
import de.thm.ii.fbs.services.v2.persistence.SharePlaygroundTokenRepository
import de.thm.ii.fbs.services.v2.persistence.SqlPlaygroundDatabaseRepository
import de.thm.ii.fbs.utils.v2.exceptions.NotFoundException
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
import java.time.LocalDateTime

@Service
class SharePlaygroundCleanupService(
private val sharePlaygroundTokenRepository: SharePlaygroundTokenRepository,
private val databaseRepository: SqlPlaygroundDatabaseRepository
) {
@Scheduled(fixedDelayString = "PT24H") // Runs every 24h
fun cleanupExpiredDumps() {
val now = LocalDateTime.now()
val expiredTokens: List<SharePlaygroundToken> = sharePlaygroundTokenRepository.findAllByExpiryTimeBefore(now)
expiredTokens.forEach { token ->
val db = databaseRepository.findByOwner_IdAndIdAndDeleted(token.userId, token.dbId, false)
?: throw NotFoundException()
//sqlPlaygroundCheckerService.deleteDatabase(db, db.id!!, db.name) WIP

Check warning on line 21 in modules/fbs-core/api/src/main/kotlin/de/thm/ii/fbs/services/v2/sharePlaygroundCleanup/SharePlaygroundCleanupService.kt

View workflow job for this annotation

GitHub Actions / fbs-core.api / Lint

Missing space after //
sharePlaygroundTokenRepository.delete(token)
}
}
}
88 changes: 84 additions & 4 deletions modules/fbs-core/web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c7c5dc5

Please sign in to comment.