Skip to content

Commit

Permalink
refactor: enforce that all GitHandler operations are managed via lock
Browse files Browse the repository at this point in the history
remove components from Spring Context so that they cannot be
accidentally injected directly and bypass the operation context
  • Loading branch information
JohannesRudolph committed Jun 21, 2024
1 parent e0df96d commit 6963037
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,13 @@ package io.meshcloud.dockerosb.persistence
import io.meshcloud.dockerosb.config.MeshServiceDefinition
import mu.KotlinLogging
import org.springframework.cloud.servicebroker.model.catalog.Catalog
import org.springframework.cloud.servicebroker.model.catalog.ServiceDefinition
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

private val log = KotlinLogging.logger { }

@Configuration
class CatalogRepository(
private val yamlHandler: YamlHandler,
private val gitHandler: GitHandler
) {

@Bean
fun catalog(): Catalog {
return getCatalog()
}

fun getCatalog(): Catalog {
val catalogYml = gitHandler.fileInRepo("catalog.yml")

Expand All @@ -35,11 +25,11 @@ class CatalogRepository(
throw IllegalArgumentException("ServiceDefinitionId cannot contain any characters other than a-z, A-Z, 0-9 and - in your catalog!")
else
return Catalog.builder()
.serviceDefinitions(catalog.services)
.build()
.serviceDefinitions(catalog.services)
.build()
}

class YamlCatalog(
val services: List<MeshServiceDefinition>
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import java.io.File
private val log = KotlinLogging.logger {}

/**
* Note: consumers should use this only via a [GitOperationContext]
* Note: consumers should use this only via a [GitOperationContext] to manage concurrent access to the git repo
*/
@Service
class GitHandlerService(
open class GitHandlerService(
private val gitConfig: GitConfig
) : GitHandler {

Expand Down Expand Up @@ -246,7 +245,7 @@ class GitHandlerService(
.call()
}

protected fun push() {
protected open fun push() {
val pushCommand = git.push()

gitConfig.username?.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.meshcloud.dockerosb.persistence

import io.meshcloud.dockerosb.config.GitConfig
import org.springframework.stereotype.Service
import java.util.concurrent.locks.ReentrantLock

Expand All @@ -14,9 +15,10 @@ import java.util.concurrent.locks.ReentrantLock
*/
@Service
class GitOperationContextFactory(
private val gitHandler: GitHandler,
gitConfig: GitConfig,
private val yamlHandler: YamlHandler
) {
private val gitHandler = GitHandlerService(gitConfig)

// we have exactly one git operation that may be active at any time
private val lock = ReentrantLock(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package io.meshcloud.dockerosb.persistence
import io.meshcloud.dockerosb.model.ServiceInstance
import io.meshcloud.dockerosb.model.Status
import org.springframework.cloud.servicebroker.model.instance.OperationState
import org.springframework.jmx.support.MetricType
import org.springframework.stereotype.Component
import java.io.File
import java.time.Instant

@Component

class ServiceInstanceRepository(private val yamlHandler: YamlHandler, private val gitHandler: GitHandler) {

fun createServiceInstance(serviceInstance: ServiceInstance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ class ServiceBrokerFixture(catalogPath: String) : Closeable {

val yamlHandler = YamlHandler()

/**
* an unsafe git handler
*/
val gitHandler = GitHandlerService(gitConfig)

val contextFactory = GitOperationContextFactory(gitHandler, yamlHandler)
val contextFactory = GitOperationContextFactory(gitConfig, yamlHandler)

val builder = TestDataBuilder(catalogPath, yamlHandler)

Expand Down

0 comments on commit 6963037

Please sign in to comment.