From 30ef674b60e18ea1af460786f15ff3eea30e9e3f Mon Sep 17 00:00:00 2001 From: mohamedlajmileanix Date: Thu, 4 Jul 2024 13:24:51 +0200 Subject: [PATCH] CID-2732: Address PR comments --- ...plication.kt => GitHubAgentApplication.kt} | 6 ++--- .../{GithubClient.kt => GitHubClient.kt} | 6 ++--- .../config/AgentSetupValidation.kt | 6 ++--- ...rties.kt => GitHubEnterpriseProperties.kt} | 2 +- ...hubAppResponse.kt => GitHubAppResponse.kt} | 2 +- .../githubagent/exceptions/Exceptions.kt | 6 ++--- .../githubagent/runners/PostStartupRunner.kt | 4 ++-- .../githubagent/services/CachingService.kt | 4 ++-- ...vice.kt => GitHubAuthenticationService.kt} | 8 +++---- .../services/GitHubEnterpriseService.kt | 19 ++++++++------- ...ests.kt => GitHubAgentApplicationTests.kt} | 2 +- ....kt => GitHubAuthenticationServiceTest.kt} | 10 ++++---- .../services/GitHubEnterpriseServiceTest.kt | 24 +++++++++---------- 13 files changed, 50 insertions(+), 49 deletions(-) rename src/main/kotlin/net/leanix/githubagent/{GithubAgentApplication.kt => GitHubAgentApplication.kt} (79%) rename src/main/kotlin/net/leanix/githubagent/client/{GithubClient.kt => GitHubClient.kt} (82%) rename src/main/kotlin/net/leanix/githubagent/config/{GithubEnterpriseProperties.kt => GitHubEnterpriseProperties.kt} (86%) rename src/main/kotlin/net/leanix/githubagent/dto/{GithubAppResponse.kt => GitHubAppResponse.kt} (92%) rename src/main/kotlin/net/leanix/githubagent/services/{GithubAuthenticationService.kt => GitHubAuthenticationService.kt} (93%) rename src/test/kotlin/net/leanix/githubagent/{GithubAgentApplicationTests.kt => GitHubAgentApplicationTests.kt} (88%) rename src/test/kotlin/net/leanix/githubagent/services/{GithubAuthenticationServiceTest.kt => GitHubAuthenticationServiceTest.kt} (86%) diff --git a/src/main/kotlin/net/leanix/githubagent/GithubAgentApplication.kt b/src/main/kotlin/net/leanix/githubagent/GitHubAgentApplication.kt similarity index 79% rename from src/main/kotlin/net/leanix/githubagent/GithubAgentApplication.kt rename to src/main/kotlin/net/leanix/githubagent/GitHubAgentApplication.kt index 54ca4dc..ec3f9fd 100644 --- a/src/main/kotlin/net/leanix/githubagent/GithubAgentApplication.kt +++ b/src/main/kotlin/net/leanix/githubagent/GitHubAgentApplication.kt @@ -7,9 +7,9 @@ import org.springframework.cloud.openfeign.EnableFeignClients @SpringBootApplication @EnableFeignClients(value = ["net.leanix.githubagent.client"]) -@EnableConfigurationProperties(value = [net.leanix.githubagent.config.GithubEnterpriseProperties::class]) -class GithubAgentApplication +@EnableConfigurationProperties(value = [net.leanix.githubagent.config.GitHubEnterpriseProperties::class]) +class GitHubAgentApplication fun main() { - runApplication() + runApplication() } diff --git a/src/main/kotlin/net/leanix/githubagent/client/GithubClient.kt b/src/main/kotlin/net/leanix/githubagent/client/GitHubClient.kt similarity index 82% rename from src/main/kotlin/net/leanix/githubagent/client/GithubClient.kt rename to src/main/kotlin/net/leanix/githubagent/client/GitHubClient.kt index 437afcd..36da759 100644 --- a/src/main/kotlin/net/leanix/githubagent/client/GithubClient.kt +++ b/src/main/kotlin/net/leanix/githubagent/client/GitHubClient.kt @@ -1,16 +1,16 @@ package net.leanix.githubagent.client -import net.leanix.githubagent.dto.GithubAppResponse +import net.leanix.githubagent.dto.GitHubAppResponse import org.springframework.cloud.openfeign.FeignClient import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestHeader @FeignClient(name = "githubClient", url = "\${github-enterprise.baseUrl}") -interface GithubClient { +interface GitHubClient { @GetMapping("/api/v3/app") fun getApp( @RequestHeader("Authorization") jwt: String, @RequestHeader("Accept") accept: String = "application/vnd.github.v3+json" - ): GithubAppResponse + ): GitHubAppResponse } diff --git a/src/main/kotlin/net/leanix/githubagent/config/AgentSetupValidation.kt b/src/main/kotlin/net/leanix/githubagent/config/AgentSetupValidation.kt index f10e5c9..ef85fc6 100644 --- a/src/main/kotlin/net/leanix/githubagent/config/AgentSetupValidation.kt +++ b/src/main/kotlin/net/leanix/githubagent/config/AgentSetupValidation.kt @@ -1,12 +1,12 @@ package net.leanix.githubagent.config import jakarta.annotation.PostConstruct -import net.leanix.githubagent.exceptions.GithubEnterpriseConfigurationMissingException +import net.leanix.githubagent.exceptions.GitHubEnterpriseConfigurationMissingException import org.springframework.stereotype.Component @Component class AgentSetupValidation( - private val githubEnterpriseProperties: GithubEnterpriseProperties + private val githubEnterpriseProperties: GitHubEnterpriseProperties ) { @PostConstruct @@ -24,7 +24,7 @@ class AgentSetupValidation( } if (missingProperties.isNotEmpty()) { - throw GithubEnterpriseConfigurationMissingException(missingProperties.joinToString(", ")) + throw GitHubEnterpriseConfigurationMissingException(missingProperties.joinToString(", ")) } } } diff --git a/src/main/kotlin/net/leanix/githubagent/config/GithubEnterpriseProperties.kt b/src/main/kotlin/net/leanix/githubagent/config/GitHubEnterpriseProperties.kt similarity index 86% rename from src/main/kotlin/net/leanix/githubagent/config/GithubEnterpriseProperties.kt rename to src/main/kotlin/net/leanix/githubagent/config/GitHubEnterpriseProperties.kt index 6dae6e3..7791c9e 100644 --- a/src/main/kotlin/net/leanix/githubagent/config/GithubEnterpriseProperties.kt +++ b/src/main/kotlin/net/leanix/githubagent/config/GitHubEnterpriseProperties.kt @@ -3,7 +3,7 @@ package net.leanix.githubagent.config import org.springframework.boot.context.properties.ConfigurationProperties @ConfigurationProperties(prefix = "github-enterprise") -data class GithubEnterpriseProperties( +data class GitHubEnterpriseProperties( val baseUrl: String, val githubAppId: String, val pemFile: String, diff --git a/src/main/kotlin/net/leanix/githubagent/dto/GithubAppResponse.kt b/src/main/kotlin/net/leanix/githubagent/dto/GitHubAppResponse.kt similarity index 92% rename from src/main/kotlin/net/leanix/githubagent/dto/GithubAppResponse.kt rename to src/main/kotlin/net/leanix/githubagent/dto/GitHubAppResponse.kt index 9880aee..167859f 100644 --- a/src/main/kotlin/net/leanix/githubagent/dto/GithubAppResponse.kt +++ b/src/main/kotlin/net/leanix/githubagent/dto/GitHubAppResponse.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties import com.fasterxml.jackson.annotation.JsonProperty @JsonIgnoreProperties(ignoreUnknown = true) -data class GithubAppResponse( +data class GitHubAppResponse( @JsonProperty("name") val name: String, @JsonProperty("permissions") val permissions: Map, @JsonProperty("events") val events: List diff --git a/src/main/kotlin/net/leanix/githubagent/exceptions/Exceptions.kt b/src/main/kotlin/net/leanix/githubagent/exceptions/Exceptions.kt index 20bd154..a0b4691 100644 --- a/src/main/kotlin/net/leanix/githubagent/exceptions/Exceptions.kt +++ b/src/main/kotlin/net/leanix/githubagent/exceptions/Exceptions.kt @@ -1,8 +1,8 @@ package net.leanix.githubagent.exceptions -class GithubEnterpriseConfigurationMissingException(properties: String) : RuntimeException( +class GitHubEnterpriseConfigurationMissingException(properties: String) : RuntimeException( "Github Enterprise properties '$properties' are not set" ) -class GithubAppInsufficientPermissionsException(message: String) : RuntimeException(message) +class GitHubAppInsufficientPermissionsException(message: String) : RuntimeException(message) class FailedToCreateJWTException(message: String) : RuntimeException(message) -class UnableToConnectToGithubEnterpriseException(message: String) : RuntimeException(message) +class UnableToConnectToGitHubEnterpriseException(message: String) : RuntimeException(message) diff --git a/src/main/kotlin/net/leanix/githubagent/runners/PostStartupRunner.kt b/src/main/kotlin/net/leanix/githubagent/runners/PostStartupRunner.kt index 08bdce2..897b19b 100644 --- a/src/main/kotlin/net/leanix/githubagent/runners/PostStartupRunner.kt +++ b/src/main/kotlin/net/leanix/githubagent/runners/PostStartupRunner.kt @@ -1,6 +1,6 @@ package net.leanix.githubagent.runners -import net.leanix.githubagent.services.GithubAuthenticationService +import net.leanix.githubagent.services.GitHubAuthenticationService import org.springframework.boot.ApplicationArguments import org.springframework.boot.ApplicationRunner import org.springframework.context.annotation.Profile @@ -8,7 +8,7 @@ import org.springframework.stereotype.Component @Component @Profile("!test") -class PostStartupRunner(private val githubAuthenticationService: GithubAuthenticationService) : ApplicationRunner { +class PostStartupRunner(private val githubAuthenticationService: GitHubAuthenticationService) : ApplicationRunner { override fun run(args: ApplicationArguments?) { githubAuthenticationService.generateJwtToken() diff --git a/src/main/kotlin/net/leanix/githubagent/services/CachingService.kt b/src/main/kotlin/net/leanix/githubagent/services/CachingService.kt index 24f9c3b..26566e6 100644 --- a/src/main/kotlin/net/leanix/githubagent/services/CachingService.kt +++ b/src/main/kotlin/net/leanix/githubagent/services/CachingService.kt @@ -1,12 +1,12 @@ package net.leanix.githubagent.services import jakarta.annotation.PostConstruct -import net.leanix.githubagent.config.GithubEnterpriseProperties +import net.leanix.githubagent.config.GitHubEnterpriseProperties import org.springframework.stereotype.Service @Service class CachingService( - private val githubEnterpriseProperties: GithubEnterpriseProperties + private val githubEnterpriseProperties: GitHubEnterpriseProperties ) { private val cache = HashMap() diff --git a/src/main/kotlin/net/leanix/githubagent/services/GithubAuthenticationService.kt b/src/main/kotlin/net/leanix/githubagent/services/GitHubAuthenticationService.kt similarity index 93% rename from src/main/kotlin/net/leanix/githubagent/services/GithubAuthenticationService.kt rename to src/main/kotlin/net/leanix/githubagent/services/GitHubAuthenticationService.kt index 72a7f9d..c621bd8 100644 --- a/src/main/kotlin/net/leanix/githubagent/services/GithubAuthenticationService.kt +++ b/src/main/kotlin/net/leanix/githubagent/services/GitHubAuthenticationService.kt @@ -2,7 +2,7 @@ package net.leanix.githubagent.services import io.jsonwebtoken.Jwts import io.jsonwebtoken.SignatureAlgorithm -import net.leanix.githubagent.config.GithubEnterpriseProperties +import net.leanix.githubagent.config.GitHubEnterpriseProperties import net.leanix.githubagent.exceptions.FailedToCreateJWTException import org.bouncycastle.jce.provider.BouncyCastleProvider import org.slf4j.LoggerFactory @@ -20,9 +20,9 @@ import java.security.spec.PKCS8EncodedKeySpec import java.util.* @Service -class GithubAuthenticationService( +class GitHubAuthenticationService( private val cachingService: CachingService, - private val githubEnterpriseProperties: GithubEnterpriseProperties, + private val githubEnterpriseProperties: GitHubEnterpriseProperties, private val resourceLoader: ResourceLoader, private val gitHubEnterpriseService: GitHubEnterpriseService ) { @@ -31,7 +31,7 @@ class GithubAuthenticationService( private const val JWT_EXPIRATION_DURATION = 600000L private const val pemPrefix = "-----BEGIN RSA PRIVATE KEY-----" private const val pemSuffix = "-----END RSA PRIVATE KEY-----" - private val logger = LoggerFactory.getLogger(GithubAuthenticationService::class.java) + private val logger = LoggerFactory.getLogger(GitHubAuthenticationService::class.java) } fun generateJwtToken() { diff --git a/src/main/kotlin/net/leanix/githubagent/services/GitHubEnterpriseService.kt b/src/main/kotlin/net/leanix/githubagent/services/GitHubEnterpriseService.kt index 418acdf..d809db3 100644 --- a/src/main/kotlin/net/leanix/githubagent/services/GitHubEnterpriseService.kt +++ b/src/main/kotlin/net/leanix/githubagent/services/GitHubEnterpriseService.kt @@ -1,14 +1,14 @@ package net.leanix.githubagent.services -import net.leanix.githubagent.client.GithubClient -import net.leanix.githubagent.dto.GithubAppResponse -import net.leanix.githubagent.exceptions.GithubAppInsufficientPermissionsException -import net.leanix.githubagent.exceptions.UnableToConnectToGithubEnterpriseException +import net.leanix.githubagent.client.GitHubClient +import net.leanix.githubagent.dto.GitHubAppResponse +import net.leanix.githubagent.exceptions.GitHubAppInsufficientPermissionsException +import net.leanix.githubagent.exceptions.UnableToConnectToGitHubEnterpriseException import org.slf4j.LoggerFactory import org.springframework.stereotype.Service @Service -class GitHubEnterpriseService(private val githubClient: GithubClient) { +class GitHubEnterpriseService(private val githubClient: GitHubClient) { companion object { val expectedPermissions = listOf("administration", "contents", "metadata") @@ -22,14 +22,15 @@ class GitHubEnterpriseService(private val githubClient: GithubClient) { validateGithubAppResponse(githubApp) logger.info("Authenticated as GitHub App: '${githubApp.name}'") }.onFailure { + logger.error("Failed to verify JWT token", it) when (it) { - is GithubAppInsufficientPermissionsException -> throw it - else -> throw UnableToConnectToGithubEnterpriseException("Failed to verify JWT token") + is GitHubAppInsufficientPermissionsException -> throw it + else -> throw UnableToConnectToGitHubEnterpriseException("Failed to verify JWT token") } } } - fun validateGithubAppResponse(response: GithubAppResponse) { + fun validateGithubAppResponse(response: GitHubAppResponse) { val missingPermissions = expectedPermissions.filterNot { response.permissions.containsKey(it) } val missingEvents = expectedEvents.filterNot { response.events.contains(it) } @@ -44,7 +45,7 @@ class GitHubEnterpriseService(private val githubClient: GithubClient) { } message = message.plus("events: $missingEvents") } - throw GithubAppInsufficientPermissionsException(message) + throw GitHubAppInsufficientPermissionsException(message) } } } diff --git a/src/test/kotlin/net/leanix/githubagent/GithubAgentApplicationTests.kt b/src/test/kotlin/net/leanix/githubagent/GitHubAgentApplicationTests.kt similarity index 88% rename from src/test/kotlin/net/leanix/githubagent/GithubAgentApplicationTests.kt rename to src/test/kotlin/net/leanix/githubagent/GitHubAgentApplicationTests.kt index 07458a3..2405e95 100644 --- a/src/test/kotlin/net/leanix/githubagent/GithubAgentApplicationTests.kt +++ b/src/test/kotlin/net/leanix/githubagent/GitHubAgentApplicationTests.kt @@ -6,7 +6,7 @@ import org.springframework.test.context.ActiveProfiles @SpringBootTest @ActiveProfiles("test") -class GithubAgentApplicationTests { +class GitHubAgentApplicationTests { @Test fun contextLoads() { diff --git a/src/test/kotlin/net/leanix/githubagent/services/GithubAuthenticationServiceTest.kt b/src/test/kotlin/net/leanix/githubagent/services/GitHubAuthenticationServiceTest.kt similarity index 86% rename from src/test/kotlin/net/leanix/githubagent/services/GithubAuthenticationServiceTest.kt rename to src/test/kotlin/net/leanix/githubagent/services/GitHubAuthenticationServiceTest.kt index 80659b3..be6fbde 100644 --- a/src/test/kotlin/net/leanix/githubagent/services/GithubAuthenticationServiceTest.kt +++ b/src/test/kotlin/net/leanix/githubagent/services/GitHubAuthenticationServiceTest.kt @@ -1,9 +1,9 @@ import io.mockk.every import io.mockk.mockk -import net.leanix.githubagent.config.GithubEnterpriseProperties +import net.leanix.githubagent.config.GitHubEnterpriseProperties import net.leanix.githubagent.services.CachingService +import net.leanix.githubagent.services.GitHubAuthenticationService import net.leanix.githubagent.services.GitHubEnterpriseService -import net.leanix.githubagent.services.GithubAuthenticationService import org.junit.jupiter.api.Assertions.assertNotNull import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Test @@ -11,13 +11,13 @@ import org.junit.jupiter.api.assertDoesNotThrow import org.springframework.core.io.ClassPathResource import org.springframework.core.io.ResourceLoader -class GithubAuthenticationServiceTest { +class GitHubAuthenticationServiceTest { private val cachingService = mockk() - private val githubEnterpriseProperties = mockk() + private val githubEnterpriseProperties = mockk() private val resourceLoader = mockk() private val gitHubEnterpriseService = mockk() - private val githubAuthenticationService = GithubAuthenticationService( + private val githubAuthenticationService = GitHubAuthenticationService( cachingService, githubEnterpriseProperties, resourceLoader, diff --git a/src/test/kotlin/net/leanix/githubagent/services/GitHubEnterpriseServiceTest.kt b/src/test/kotlin/net/leanix/githubagent/services/GitHubEnterpriseServiceTest.kt index 92c88a7..7e4fdf5 100644 --- a/src/test/kotlin/net/leanix/githubagent/services/GitHubEnterpriseServiceTest.kt +++ b/src/test/kotlin/net/leanix/githubagent/services/GitHubEnterpriseServiceTest.kt @@ -1,9 +1,9 @@ import io.mockk.every import io.mockk.mockk -import net.leanix.githubagent.client.GithubClient -import net.leanix.githubagent.dto.GithubAppResponse -import net.leanix.githubagent.exceptions.GithubAppInsufficientPermissionsException -import net.leanix.githubagent.exceptions.UnableToConnectToGithubEnterpriseException +import net.leanix.githubagent.client.GitHubClient +import net.leanix.githubagent.dto.GitHubAppResponse +import net.leanix.githubagent.exceptions.GitHubAppInsufficientPermissionsException +import net.leanix.githubagent.exceptions.UnableToConnectToGitHubEnterpriseException import net.leanix.githubagent.services.GitHubEnterpriseService import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Test @@ -11,13 +11,13 @@ import org.junit.jupiter.api.assertDoesNotThrow class GitHubEnterpriseServiceTest { - private val githubClient = mockk() + private val githubClient = mockk() private val service = GitHubEnterpriseService(githubClient) @Test fun `verifyJwt with valid jwt should not throw exception`() { val jwt = "validJwt" - val githubApp = GithubAppResponse( + val githubApp = GitHubAppResponse( name = "validApp", permissions = mapOf("administration" to "read", "contents" to "read", "metadata" to "read"), events = listOf("label", "public", "repository") @@ -32,12 +32,12 @@ class GitHubEnterpriseServiceTest { val jwt = "invalidJwt" every { githubClient.getApp(any()) } throws Exception() - assertThrows(UnableToConnectToGithubEnterpriseException::class.java) { service.verifyJwt(jwt) } + assertThrows(UnableToConnectToGitHubEnterpriseException::class.java) { service.verifyJwt(jwt) } } @Test fun `validateGithubAppResponse with correct permissions should not throw exception`() { - val response = GithubAppResponse( + val response = GitHubAppResponse( name = "validApp", permissions = mapOf("administration" to "read", "contents" to "read", "metadata" to "read"), events = listOf("label", "public", "repository") @@ -48,27 +48,27 @@ class GitHubEnterpriseServiceTest { @Test fun `validateGithubAppResponse with missing permissions should throw exception`() { - val response = GithubAppResponse( + val response = GitHubAppResponse( name = "validApp", permissions = mapOf("administration" to "read", "contents" to "read"), events = listOf("label", "public", "repository") ) assertThrows( - GithubAppInsufficientPermissionsException::class.java + GitHubAppInsufficientPermissionsException::class.java ) { service.validateGithubAppResponse(response) } } @Test fun `validateGithubAppResponse with missing events should throw exception`() { - val response = GithubAppResponse( + val response = GitHubAppResponse( name = "validApp", permissions = mapOf("administration" to "read", "contents" to "read", "metadata" to "read"), events = listOf("label", "public") ) assertThrows( - GithubAppInsufficientPermissionsException::class.java + GitHubAppInsufficientPermissionsException::class.java ) { service.validateGithubAppResponse(response) } } }