|
| 1 | +/** |
| 2 | + * Copyright (c) 2026, Samsung Electronics Co., Ltd. All rights reserved. |
| 3 | + * |
| 4 | + * Use of this source code is governed by a MIT license that can be |
| 5 | + * found in the LICENSE file. |
| 6 | + */ |
| 7 | +package com.lpvs.config; |
| 8 | + |
| 9 | +import com.lpvs.controller.GitHubController; |
| 10 | +import com.lpvs.controller.HealthController; |
| 11 | +import com.lpvs.repository.LPVSQueueRepository; |
| 12 | +import com.lpvs.service.LPVSGitHubConnectionService; |
| 13 | +import com.lpvs.service.LPVSGitHubService; |
| 14 | +import com.lpvs.service.LPVSQueueService; |
| 15 | +import com.lpvs.util.LPVSExitHandler; |
| 16 | +import org.apache.commons.codec.binary.Hex; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.kohsuke.github.GHPullRequest; |
| 19 | +import org.kohsuke.github.GHRepository; |
| 20 | +import org.kohsuke.github.GitHub; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 23 | +import org.springframework.http.MediaType; |
| 24 | +import org.springframework.test.context.ContextConfiguration; |
| 25 | +import org.springframework.test.context.TestPropertySource; |
| 26 | +import org.springframework.test.context.bean.override.mockito.MockitoBean; |
| 27 | +import org.springframework.test.web.servlet.MockMvc; |
| 28 | + |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import javax.crypto.Mac; |
| 31 | +import javax.crypto.spec.SecretKeySpec; |
| 32 | + |
| 33 | +import static org.mockito.Mockito.mock; |
| 34 | +import static org.mockito.Mockito.never; |
| 35 | +import static org.mockito.Mockito.verify; |
| 36 | +import static org.mockito.Mockito.when; |
| 37 | +import static org.mockito.ArgumentMatchers.any; |
| 38 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 39 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 40 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; |
| 41 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 42 | + |
| 43 | +@WebMvcTest |
| 44 | +@ContextConfiguration( |
| 45 | + classes = {SecurityConfig.class, GitHubController.class, HealthController.class}) |
| 46 | +@TestPropertySource( |
| 47 | + properties = {"github.secret=" + SecurityConfigAccessTest.SECRET, "lpvs.api.key=test-key"}) |
| 48 | +class SecurityConfigAccessTest { |
| 49 | + |
| 50 | + static final String SECRET = "test-webhook-secret"; |
| 51 | + |
| 52 | + @Autowired private MockMvc mockMvc; |
| 53 | + |
| 54 | + @MockitoBean private LPVSQueueService queueService; |
| 55 | + @MockitoBean private LPVSGitHubService gitHubService; |
| 56 | + @MockitoBean private LPVSGitHubConnectionService gitHubConnectionService; |
| 57 | + @MockitoBean private LPVSQueueRepository queueRepository; |
| 58 | + @MockitoBean private LPVSExitHandler exitHandler; |
| 59 | + |
| 60 | + private static String sign(String payload) throws Exception { |
| 61 | + Mac mac = Mac.getInstance("HmacSHA256"); |
| 62 | + mac.init(new SecretKeySpec(SECRET.getBytes(StandardCharsets.UTF_8), "HmacSHA256")); |
| 63 | + return "sha256=" |
| 64 | + + Hex.encodeHexString(mac.doFinal(payload.getBytes(StandardCharsets.UTF_8))); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void healthIsPublic() throws Exception { |
| 69 | + mockMvc.perform(get("/health")).andExpect(status().isOk()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void webhookIsPublic() throws Exception { |
| 74 | + String payload = "{\"zen\": \"ping\"}"; |
| 75 | + mockMvc.perform( |
| 76 | + post("/webhooks") |
| 77 | + .contentType(MediaType.APPLICATION_JSON) |
| 78 | + .header("X-Hub-Signature-256", sign(payload)) |
| 79 | + .content(payload)) |
| 80 | + .andExpect(status().isOk()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void rootPostIsForwardedToWebhook() throws Exception { |
| 85 | + mockMvc.perform(post("/")).andExpect(forwardedUrl("/webhooks")); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void singleScanWithoutApiKeyIsForbidden() throws Exception { |
| 90 | + mockMvc.perform(post("/scan/org/repo/1")).andExpect(status().isForbidden()); |
| 91 | + verify(gitHubConnectionService, never()).connectToGitHubApi(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void singleScanWithApiKeyIsAllowed() throws Exception { |
| 96 | + GitHub gitHub = mock(GitHub.class); |
| 97 | + GHRepository repository = mock(GHRepository.class); |
| 98 | + when(gitHubConnectionService.connectToGitHubApi()).thenReturn(gitHub); |
| 99 | + when(gitHub.getRepository("org/repo")).thenReturn(repository); |
| 100 | + when(repository.getPullRequest(1)).thenReturn(mock(GHPullRequest.class)); |
| 101 | + |
| 102 | + mockMvc.perform(post("/scan/org/repo/1").header("X-LPVS-Api-Key", "test-key")) |
| 103 | + .andExpect(status().isOk()); |
| 104 | + verify(queueService).addFirst(any()); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void unknownEndpointIsDenied() throws Exception { |
| 109 | + mockMvc.perform(get("/unknown")).andExpect(status().isForbidden()); |
| 110 | + mockMvc.perform(post("/unknown")).andExpect(status().isForbidden()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void wrongMethodIsDenied() throws Exception { |
| 115 | + mockMvc.perform(post("/health")).andExpect(status().isForbidden()); |
| 116 | + mockMvc.perform(get("/scan/org/repo/1").header("X-LPVS-Api-Key", "test-key")) |
| 117 | + .andExpect(status().isForbidden()); |
| 118 | + } |
| 119 | +} |
0 commit comments