generated from leanix/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CID-2732: improve implementation, refactor, optimise dependencies and…
… fix Snyk vulnerabilities
- Loading branch information
1 parent
6a786c0
commit 53894be
Showing
9 changed files
with
282 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/net/leanix/githubagent/services/GitHubEnterpriseService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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 org.slf4j.LoggerFactory | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class GitHubEnterpriseService(private val githubClient: GithubClient) { | ||
|
||
companion object { | ||
val expectedPermissions = listOf("administration", "contents", "metadata") | ||
val expectedEvents = listOf("label", "public", "repository") | ||
} | ||
private val logger = LoggerFactory.getLogger(GitHubEnterpriseService::class.java) | ||
|
||
fun verifyJwt(jwt: String) { | ||
runCatching { | ||
val githubApp = githubClient.getApp("Bearer $jwt") | ||
validateGithubAppResponse(githubApp) | ||
logger.info("Authenticated as GitHub App: '${githubApp.name}'") | ||
}.onFailure { | ||
when (it) { | ||
is GithubAppInsufficientPermissionsException -> throw it | ||
else -> throw UnableToConnectToGithubEnterpriseException("Failed to verify JWT token") | ||
} | ||
} | ||
} | ||
|
||
fun validateGithubAppResponse(response: GithubAppResponse) { | ||
val missingPermissions = expectedPermissions.filterNot { response.permissions.containsKey(it) } | ||
val missingEvents = expectedEvents.filterNot { response.events.contains(it) } | ||
|
||
if (missingPermissions.isNotEmpty() || missingEvents.isNotEmpty()) { | ||
var message = "GitHub App is missing the following " | ||
if (missingPermissions.isNotEmpty()) { | ||
message = message.plus("permissions: $missingPermissions") | ||
} | ||
if (missingEvents.isNotEmpty()) { | ||
if (missingPermissions.isNotEmpty()) { | ||
message = message.plus(", and the following") | ||
} | ||
message = message.plus("events: $missingEvents") | ||
} | ||
throw GithubAppInsufficientPermissionsException(message) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/test/kotlin/net/leanix/githubagent/services/GitHubEnterpriseServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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.services.GitHubEnterpriseService | ||
import org.junit.jupiter.api.Assertions.assertThrows | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
|
||
class GitHubEnterpriseServiceTest { | ||
|
||
private val githubClient = mockk<GithubClient>() | ||
private val service = GitHubEnterpriseService(githubClient) | ||
|
||
@Test | ||
fun `verifyJwt with valid jwt should not throw exception`() { | ||
val jwt = "validJwt" | ||
val githubApp = GithubAppResponse( | ||
name = "validApp", | ||
permissions = mapOf("administration" to "read", "contents" to "read", "metadata" to "read"), | ||
events = listOf("label", "public", "repository") | ||
) | ||
every { githubClient.getApp(any()) } returns githubApp | ||
|
||
assertDoesNotThrow { service.verifyJwt(jwt) } | ||
} | ||
|
||
@Test | ||
fun `verifyJwt with invalid jwt should throw exception`() { | ||
val jwt = "invalidJwt" | ||
every { githubClient.getApp(any()) } throws Exception() | ||
|
||
assertThrows(UnableToConnectToGithubEnterpriseException::class.java) { service.verifyJwt(jwt) } | ||
} | ||
|
||
@Test | ||
fun `validateGithubAppResponse with correct permissions should not throw exception`() { | ||
val response = GithubAppResponse( | ||
name = "validApp", | ||
permissions = mapOf("administration" to "read", "contents" to "read", "metadata" to "read"), | ||
events = listOf("label", "public", "repository") | ||
) | ||
|
||
assertDoesNotThrow { service.validateGithubAppResponse(response) } | ||
} | ||
|
||
@Test | ||
fun `validateGithubAppResponse with missing permissions should throw exception`() { | ||
val response = GithubAppResponse( | ||
name = "validApp", | ||
permissions = mapOf("administration" to "read", "contents" to "read"), | ||
events = listOf("label", "public", "repository") | ||
) | ||
|
||
assertThrows( | ||
GithubAppInsufficientPermissionsException::class.java | ||
) { service.validateGithubAppResponse(response) } | ||
} | ||
|
||
@Test | ||
fun `validateGithubAppResponse with missing events should throw exception`() { | ||
val response = GithubAppResponse( | ||
name = "validApp", | ||
permissions = mapOf("administration" to "read", "contents" to "read", "metadata" to "read"), | ||
events = listOf("label", "public") | ||
) | ||
|
||
assertThrows( | ||
GithubAppInsufficientPermissionsException::class.java | ||
) { service.validateGithubAppResponse(response) } | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/kotlin/net/leanix/githubagent/services/GithubAuthenticationServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import net.leanix.githubagent.config.GithubEnterpriseProperties | ||
import net.leanix.githubagent.services.CachingService | ||
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 | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
import org.springframework.core.io.ClassPathResource | ||
import org.springframework.core.io.ResourceLoader | ||
|
||
class GithubAuthenticationServiceTest { | ||
|
||
private val cachingService = mockk<CachingService>() | ||
private val githubEnterpriseProperties = mockk<GithubEnterpriseProperties>() | ||
private val resourceLoader = mockk<ResourceLoader>() | ||
private val gitHubEnterpriseService = mockk<GitHubEnterpriseService>() | ||
private val githubAuthenticationService = GithubAuthenticationService( | ||
cachingService, | ||
githubEnterpriseProperties, | ||
resourceLoader, | ||
gitHubEnterpriseService | ||
) | ||
|
||
@Test | ||
fun `generateJwtToken with valid data should not throw exception`() { | ||
every { cachingService.get(any()) } returns "dummy-value" | ||
every { cachingService.set(any(), any()) } returns Unit | ||
every { githubEnterpriseProperties.pemFile } returns "valid-private-key.pem" | ||
every { resourceLoader.getResource(any()) } returns ClassPathResource("valid-private-key.pem") | ||
every { gitHubEnterpriseService.verifyJwt(any()) } returns Unit | ||
|
||
assertDoesNotThrow { githubAuthenticationService.generateJwtToken() } | ||
assertNotNull(cachingService.get("jwtToken")) | ||
} | ||
|
||
@Test | ||
fun `generateJwtToken with invalid data should throw exception`() { | ||
every { cachingService.get(any()) } returns "dummy-value" | ||
every { githubEnterpriseProperties.pemFile } returns "invalid-private-key.pem" | ||
every { resourceLoader.getResource(any()) } returns ClassPathResource("invalid-private-key.pem") | ||
|
||
assertThrows(IllegalArgumentException::class.java) { githubAuthenticationService.generateJwtToken() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
MIIEpAIBAAKCAQEAwqjwjl1IJ2Mo4TMtNmAoZl+lnRP88D2ocMrj1QgKYzHTnsAW | ||
UudOX909Mxbjj9ZKpuDpggL3/X+h/pCaD7yhS1OgYo1pl8TmWwmDq8ok0VJYlfxi | ||
3oH76kexyQZ+SYT7YqZ7Xy67Q/kcjDyVK708vnKdhEaGFCVdIxbUfzsIynq6xbKx | ||
PETEMlW1dBHQSrwIYgGTAwKTrvqmpNxZw7yIFA6qASddQmpbm0ycoMXYVrz+Nac6 | ||
RrR93YVY3Jc+0c13bSenCqlRtMEHLfmGTuKboKiQWRgS16CEgfg2b29310OtLC6T | ||
PSk9Dtv1knrVjpnWVaMq3w28ky3I1aeoKZCkPQIDAQABAoIBAQCWEJ0ac0k7rBMI | ||
wWY6hBjBCz1mgdE995qSEadgRImVfQUSXi0Xjl/6QVl7uEqISYBVdBAv/U/m6m0d | ||
DabnONjzdC2xrCjaKp4XUpdiaTzG7f+C6QXjWTu2mbyyJ8JVtSIDJCr57tHJDhN2 | ||
/QFWrdVVUJCkN6YHg+JwOZpp1z3osSldnRCYUJ7NcPfNYCj/n0Gq5fQ3MUmk17ch | ||
O5+XOxa8GBFj9hCqqFB97qnYSkRDTv0YoLdlIdnnVQeKYYMFCdKa++vgHX/7Pu8B | ||
KFr34Fm1BFjkoIYjOtYbeUf2lWG+dzwEwLUu5DUcYS+YyUBCogUDLtROHScPSSFU | ||
5hHin6S1AoGBAPHde46hvPmBNR6DGkds1twavbvEynlKiKdpgWn+ycBaLOPXO/hb | ||
xdjAohZNIYwE72ggYWnMhHy1OnhytUMopMsT/xbDu+v5iwF+/9x9C7gdBj8drEzx | ||
4E86O+lQ7ROh1PoAPwTqFUY0rEmsJRvfTY8oUp9LuiPWuO5Mc1tGIjJXAoGBAM4J | ||
OYVKqc5Rzt4pSWzy3wzxekE1XVN7SRdcdYyjqOiYRLmc1jSx5nuTotluSd/trtZw | ||
5Sf65e9YkO2zx5Ou4/TWdnGurWP8BgBAT2bDCDKjetiJTHSB68Hcz0zfH99C9h+E | ||
8vn8Lpn57fFG+TOiADBPAYNEEkBxBJyGn4d+r8mLAoGBAISRIhT2f46+DDByKWg2 | ||
trmjipUtctDyUl54TK+dMFXW1z32je891f5M70qL8jQ9zD7laJ9FsuRrrOWx8boi | ||
v9hzWGDQ3eKkP1WNl43xmAfNGMxlZjgyZwDl6UqjyZ32GLcChYgbCZgWbMxgp2JU | ||
jb1Gm6qmJhtYqLosexnvIfU3AoGAR5znNFAmQ0MmDwv0rHyiUIJiRuYAgTK5zffi | ||
F7cOz4GVaZp8zaYEAXHoSYDPBpk7iueEjufjIdT70tMJDGjebMxaMNtRAw6nG1E/ | ||
B+3EHK271iWqwFgkFKbmGsb28gf5Oi1gsskXfYdkT9emaG7nd+MOGI0BdwqRWsJk | ||
EplTCk8CgYATcdreHFdXBCbRLszoiPPpvNTi0lBUdor+PzVrewAdByOY9dajBbap | ||
2Fbuu2fkhBPEP8BL+3fJmbXsVVxOf9Nzy/IusekfuC5ZGnc41aCtaC6hplaXs131 | ||
UvAdbhohImJi8D/p6uXPvrwrApBvoDpEu3Sq36VMCPeSv3YmTngLXw== | ||
-----END RSA PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIIEpAIBAAKCAQEAwqjwjl1IJ2Mo4TMtNmAoZl+lnRP88D2ocMrj1QgKYzHTnsAW | ||
UudOX909Mxbjj9ZKpuDpggL3/X+h/pCaD7yhS1OgYo1pl8TmWwmDq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qL8jQ9zD7laJ9FsuRrrOWx8boi | ||
v9hzWGDQ3eKkP1WNl43xmAfNGMxlZjgyZwDl6UqjyZ32GLcChYgbCZgWbMxgp2JU | ||
jb1Gm6qmJhtYqLosexnvIfU3AoGAR5znNFAmQ0MmDwv0rHyiUIJiRuYAgTK5zffi | ||
F7cOz4GVaZp8zaYEAXHoSYDPBpk7iueEjufjIdT70tMJDGjebMxaMNtRAw6nG1E/ | ||
B+3EHK271iWqwFgkFKbmGsb28gf5Oi1gsskXfYdkT9emaG7nd+MOGI0BdwqRWsJk | ||
EplTCk8CgYATcdreHFdXBCbRLszoiPPpvNTi0lBUdor+PzVrewAdByOY9dajBbap | ||
2Fbuu2fkhBPEP8BL+3fJmbXsVVxOf9Nzy/IusekfuC5ZGnc41aCtaC6hplaXs131 | ||
UvAdbhohImJi8D/p6uXPvrwrApBvoDpEu3Sq36VMCPeSv3YmTngLXw== | ||
-----END RSA PRIVATE KEY----- |