Skip to content

Commit

Permalink
Merge pull request #22 from leanix/feature/CID-2885/send-GH-app-name-…
Browse files Browse the repository at this point in the history
…to-backend

CID-2885: Send GitHub App name to backend
  • Loading branch information
mohamedlajmileanix authored Aug 27, 2024
2 parents 1f3ebe6 + dcc21c2 commit afcd120
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package net.leanix.githubagent.runners

import net.leanix.githubagent.services.CachingService
import net.leanix.githubagent.services.GitHubAuthenticationService
import net.leanix.githubagent.services.GitHubEnterpriseService
import net.leanix.githubagent.services.GitHubScanningService
import net.leanix.githubagent.services.WebSocketService
import net.leanix.githubagent.shared.AGENT_METADATA_TOPIC
import org.springframework.boot.ApplicationArguments
import org.springframework.boot.ApplicationRunner
import org.springframework.context.annotation.Profile
Expand All @@ -13,12 +16,19 @@ import org.springframework.stereotype.Component
class PostStartupRunner(
private val githubAuthenticationService: GitHubAuthenticationService,
private val webSocketService: WebSocketService,
private val gitHubScanningService: GitHubScanningService
private val gitHubScanningService: GitHubScanningService,
private val gitHubEnterpriseService: GitHubEnterpriseService,
private val cachingService: CachingService
) : ApplicationRunner {

override fun run(args: ApplicationArguments?) {
webSocketService.initSession()
githubAuthenticationService.generateAndCacheJwtToken()
val jwt = cachingService.get("jwt") as String
webSocketService.sendMessage(
AGENT_METADATA_TOPIC,
gitHubEnterpriseService.getGitHubApp(jwt).name
)
gitHubScanningService.scanGitHubResources()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GitHubEnterpriseService(private val githubClient: GitHubClient) {

fun verifyJwt(jwt: String) {
runCatching {
val githubApp = githubClient.getApp("Bearer $jwt")
val githubApp = getGitHubApp(jwt)
validateGithubAppResponse(githubApp)
logger.info("Authenticated as GitHub App: '${githubApp.name}'")
}.onFailure {
Expand Down Expand Up @@ -48,4 +48,6 @@ class GitHubEnterpriseService(private val githubClient: GitHubClient) {
throw GitHubAppInsufficientPermissionsException(message)
}
}

fun getGitHubApp(jwt: String) = githubClient.getApp("Bearer $jwt")
}
1 change: 1 addition & 0 deletions src/main/kotlin/net/leanix/githubagent/shared/Constants.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.leanix.githubagent.shared

const val TOPIC_PREFIX = "/app/ghe/"
const val AGENT_METADATA_TOPIC = "agent"

enum class ManifestFileName(val fileName: String) {
YAML("leanix.yaml"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package net.leanix.githubagent.runners

import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import net.leanix.githubagent.dto.GitHubAppResponse
import net.leanix.githubagent.services.CachingService
import net.leanix.githubagent.services.GitHubAuthenticationService
import net.leanix.githubagent.services.GitHubEnterpriseService
import net.leanix.githubagent.services.GitHubScanningService
import net.leanix.githubagent.services.WebSocketService
import net.leanix.githubagent.shared.AGENT_METADATA_TOPIC
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

class PostStartupRunnerTest {

private lateinit var githubAuthenticationService: GitHubAuthenticationService
private lateinit var webSocketService: WebSocketService
private lateinit var gitHubScanningService: GitHubScanningService
private lateinit var gitHubEnterpriseService: GitHubEnterpriseService
private lateinit var cachingService: CachingService
private lateinit var postStartupRunner: PostStartupRunner

@BeforeEach
fun setUp() {
githubAuthenticationService = mockk()
webSocketService = mockk()
gitHubScanningService = mockk()
gitHubEnterpriseService = mockk()
cachingService = mockk()

postStartupRunner = PostStartupRunner(
githubAuthenticationService,
webSocketService,
gitHubScanningService,
gitHubEnterpriseService,
cachingService
)

every { webSocketService.initSession() } returns Unit
every { webSocketService.sendMessage(any(), any()) } returns Unit
every { githubAuthenticationService.generateAndCacheJwtToken() } returns Unit
every { cachingService.get("jwt") } returns "jwt"
every { gitHubScanningService.scanGitHubResources() } returns Unit
}

@Test
fun `should send GitHub App name`() {
val gitHubAppName = "appName"
every { gitHubEnterpriseService.getGitHubApp("jwt") } returns
GitHubAppResponse(
gitHubAppName, mapOf(), listOf()
)

postStartupRunner.run(mockk())

verify { webSocketService.sendMessage(AGENT_METADATA_TOPIC, gitHubAppName) }
}
}

0 comments on commit afcd120

Please sign in to comment.