Skip to content

Commit 08d3313

Browse files
Feature/cid 4110 fix full scan jwt expiration (#134)
* cid-4110: Fix fetch cache and expiration * cid-4110: Modify reconnect check * cid-4110: Modify installation token expiration
1 parent 3e100a6 commit 08d3313

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

src/main/kotlin/net/leanix/githubagent/handler/BrokerStompSessionHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class BrokerStompSessionHandler(
5252

5353
override fun handleTransportError(session: StompSession, exception: Throwable) {
5454
logger.error("Connection error: ${exception.message}")
55-
if (isConnected) {
55+
if (isConnected && session.sessionId == webSocketService.stompSession?.sessionId) {
5656
isConnected = false
5757
logger.error("Session closed. This could be due to a network error or the server closing the connection.")
5858
logger.info("Reconnecting...")

src/main/kotlin/net/leanix/githubagent/services/CachingService.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.github.benmanes.caffeine.cache.Expiry
66
import jakarta.annotation.PostConstruct
77
import net.leanix.githubagent.config.GitHubEnterpriseProperties
88
import org.springframework.stereotype.Service
9+
import java.util.concurrent.TimeUnit
910

1011
@Service
1112
class CachingService(
@@ -22,7 +23,7 @@ class CachingService(
2223
value: CacheValue,
2324
currentTime: Long
2425
): Long {
25-
return value.expiry?.times(1_000_000_000) ?: Long.MAX_VALUE
26+
return TimeUnit.SECONDS.toNanos(value.expiry ?: Long.MAX_VALUE)
2627
}
2728

2829
override fun expireAfterUpdate(
@@ -31,7 +32,7 @@ class CachingService(
3132
currentTime: Long,
3233
currentDuration: Long
3334
): Long {
34-
return value.expiry?.times(1_000_000_000) ?: Long.MAX_VALUE
35+
return TimeUnit.SECONDS.toNanos(value.expiry ?: Long.MAX_VALUE)
3536
}
3637

3738
override fun expireAfterRead(

src/main/kotlin/net/leanix/githubagent/services/FullScanService.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
package net.leanix.githubagent.services
22

33
import net.leanix.githubagent.exceptions.UnableToSendMessageException
4+
import org.springframework.scheduling.annotation.Async
45
import org.springframework.stereotype.Service
56

67
@Service
78
class FullScanService(
89
private val gitHubScanningService: GitHubScanningService,
9-
private val syncLogService: SyncLogService
10+
private val syncLogService: SyncLogService,
11+
private val gitHubAuthenticationService: GitHubAuthenticationService
1012
) {
1113

1214
companion object {
1315
var requireScan: Boolean = false
1416
}
1517

18+
@Async
1619
fun verifyAndStartScan() {
1720
if (requireScan) {
1821
runCatching {
1922
requireScan = false
23+
gitHubAuthenticationService.generateAndCacheJwtToken()
2024
syncLogService.sendFullScanStart(null)
2125
gitHubScanningService.scanGitHubResources()
2226
}.onSuccess {

src/main/kotlin/net/leanix/githubagent/services/GitHubAuthenticationService.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class GitHubAuthenticationService(
3232
) {
3333

3434
companion object {
35-
private const val JWT_EXPIRATION_DURATION = 600000L
35+
private const val JWT_EXPIRATION_DURATION_IN_SECONDS = 600L
36+
private const val INSTALLATION_JWT_EXPIRATION_DURATION_IN_SECONDS = 3600L
3637
private const val pemPrefix = "-----BEGIN RSA PRIVATE KEY-----"
3738
private const val pemSuffix = "-----END RSA PRIVATE KEY-----"
3839
private val logger = LoggerFactory.getLogger(GitHubAuthenticationService::class.java)
@@ -76,7 +77,7 @@ class GitHubAuthenticationService(
7677
return runCatching {
7778
Jwts.builder()
7879
.setIssuedAt(Date())
79-
.setExpiration(Date(System.currentTimeMillis() + JWT_EXPIRATION_DURATION))
80+
.setExpiration(Date(System.currentTimeMillis() + (JWT_EXPIRATION_DURATION_IN_SECONDS * 1000)))
8081
.setIssuer(cachingService.get("githubAppId").toString())
8182
.signWith(privateKey, SignatureAlgorithm.RS256)
8283
.compact()
@@ -89,7 +90,7 @@ class GitHubAuthenticationService(
8990
private fun verifyAndCacheJwtToken(jwt: String) {
9091
runCatching {
9192
gitHubEnterpriseService.verifyJwt(jwt)
92-
cachingService.set("jwtToken", jwt, JWT_EXPIRATION_DURATION)
93+
cachingService.set("jwtToken", jwt, JWT_EXPIRATION_DURATION_IN_SECONDS)
9394
logger.info("JWT token generated and cached successfully")
9495
}.onFailure {
9596
logger.error("Failed to verify and cache JWT token", it)
@@ -103,7 +104,11 @@ class GitHubAuthenticationService(
103104
) {
104105
installations.forEach { installation ->
105106
val installationToken = gitHubClient.createInstallationToken(installation.id, "Bearer $jwtToken").token
106-
cachingService.set("installationToken:${installation.id}", installationToken, 3600L)
107+
cachingService.set(
108+
"installationToken:${installation.id}",
109+
installationToken,
110+
INSTALLATION_JWT_EXPIRATION_DURATION_IN_SECONDS
111+
)
107112
}
108113
}
109114

0 commit comments

Comments
 (0)