Repository navigation
feat: add webhook v1 disable flag and default UI to v2-only creation #4065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package io.terrakube.api.rs.hooks.webhook; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import org.apache.hc.core5.http.HttpStatus; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
|
|
||
| import com.yahoo.elide.annotation.LifeCycleHookBinding.Operation; | ||
| import com.yahoo.elide.annotation.LifeCycleHookBinding.TransactionPhase; | ||
| import com.yahoo.elide.core.lifecycle.LifeCycleHook; | ||
| import com.yahoo.elide.core.security.ChangeSpec; | ||
| import com.yahoo.elide.core.security.RequestScope; | ||
|
|
||
| import io.terrakube.api.plugin.vcs.WebhookProperties; | ||
| import io.terrakube.api.rs.vcs.VcsType; | ||
| import io.terrakube.api.rs.webhook.Webhook; | ||
|
|
||
| /** | ||
| * Rejects attempts to revert an already-migrated v2 webhook back to v1 ({@code migratedV2} | ||
| * transitioning from {@code true} to {@code false}) when {@code io.terrakube.webhook.v1.disabled} | ||
| * is enabled, while allowing unrelated updates (such as trigger edits) on untouched v1 webhooks. | ||
| */ | ||
| public class WebhookV1RevertGuardHook implements LifeCycleHook<Webhook> { | ||
|
|
||
| @Autowired | ||
|
Check warning on line 25 in api/src/main/java/io/terrakube/api/rs/hooks/webhook/WebhookV1RevertGuardHook.java
|
||
| WebhookProperties webhookProperties; | ||
|
|
||
| @Override | ||
| public void execute(Operation operation, TransactionPhase phase, Webhook elideEntity, RequestScope requestScope, | ||
| Optional<ChangeSpec> changes) { | ||
| if (operation != Operation.UPDATE || phase != TransactionPhase.PRECOMMIT) { | ||
| return; | ||
| } | ||
| if (webhookProperties == null || !webhookProperties.getV1().isDisabled()) { | ||
| return; | ||
| } | ||
| boolean revertingToV1 = changes | ||
| .filter(change -> "migratedV2".equals(change.getFieldName())) | ||
| .map(change -> Boolean.TRUE.equals(change.getOriginal()) && Boolean.FALSE.equals(change.getModified())) | ||
| .orElse(false); | ||
| if (revertingToV1 && isSharedWebhookProvider(elideEntity)) { | ||
| throw new WebhookManagementException(HttpStatus.SC_BAD_REQUEST, | ||
| "Webhook v1 is disabled; switching to webhook v1 is not allowed"); | ||
| } | ||
| } | ||
|
|
||
| private boolean isSharedWebhookProvider(Webhook webhook) { | ||
| VcsType vcsType = webhook.getWorkspace() != null && webhook.getWorkspace().getVcs() != null | ||
| ? webhook.getWorkspace().getVcs().getVcsType() | ||
| : null; | ||
| return vcsType == VcsType.GITHUB | ||
| || vcsType == VcsType.GITLAB | ||
| || vcsType == VcsType.AZURE_SP_MI | ||
| || vcsType == VcsType.AZURE_DEVOPS | ||
| || vcsType == VcsType.BITBUCKET; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -174,6 +174,10 @@ io.terrakube.webhook.dispatch.http.connectionRequestTimeoutSeconds=${WebhookDisp | |
| # whose configured branch matches the PR's target (base) branch. Trigger branch patterns | ||
| # continue to match against the PR source (head) branch. Defaults to false for backward compatibility. | ||
| io.terrakube.webhook.pr-base-branch-filter=${WebhookPrBaseBranchFilter:false} | ||
| # When true, /webhook/v1/{webhookId} returns 404 and new webhooks for supported VCS providers | ||
| # are created as shared v2 webhooks. Existing v1 workspaces must be migrated first (automated on | ||
| # startup once the full migration stack is merged). | ||
| io.terrakube.webhook.v1.disabled=${WebhookV1Disabled:false} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turning this on makes every existing v1 hook return 404 while the remote hooks stay registered, and nothing in this PR migrates them (that comes in #4066). Could the comment say that existing workspaces have to be migrated first? Also
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the |
||
|
|
||
| ############## | ||
| #EXECUTOR URL# | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package io.terrakube.api; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.test.context.TestPropertySource; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| import static io.restassured.RestAssured.given; | ||
|
|
||
| @TestPropertySource(properties = "io.terrakube.webhook.v1.disabled=true") | ||
| class WebhookV1DisabledIntegrationTest extends ServerApplicationTests { | ||
|
|
||
| @Test | ||
| void webhookV1Returns404WhenDisabledForValidUuid() { | ||
| given() | ||
| .contentType("application/json") | ||
| .body("{}") | ||
| .when() | ||
| .post("/webhook/v1/" + UUID.randomUUID()) | ||
| .then() | ||
| .statusCode(HttpStatus.NOT_FOUND.value()); | ||
| } | ||
|
|
||
| @Test | ||
| void webhookV1Returns404WhenDisabledForInvalidUuid() { | ||
| given() | ||
| .contentType("application/json") | ||
| .body("{}") | ||
| .when() | ||
| .post("/webhook/v1/not-a-uuid") | ||
| .then() | ||
| .statusCode(HttpStatus.NOT_FOUND.value()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package io.terrakube.api.plugin.vcs.controller; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import io.terrakube.api.plugin.vcs.RepoWebhookDispatchService; | ||
| import io.terrakube.api.plugin.vcs.RepoWebhookService; | ||
| import io.terrakube.api.plugin.vcs.WebhookProperties; | ||
| import io.terrakube.api.plugin.vcs.WebhookService; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.UUID; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.verifyNoInteractions; | ||
|
|
||
| class WebHookControllerTest { | ||
|
|
||
| private WebhookService webhookService; | ||
| private WebhookProperties webhookProperties; | ||
| private WebHookController controller; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| webhookService = mock(WebhookService.class); | ||
| webhookProperties = new WebhookProperties(); | ||
| controller = new WebHookController(); | ||
| controller.webhookService = webhookService; | ||
| controller.repoWebhookService = mock(RepoWebhookService.class); | ||
| controller.repoWebhookDispatchService = mock(RepoWebhookDispatchService.class); | ||
| controller.objectMapper = new ObjectMapper(); | ||
| controller.webhookProperties = webhookProperties; | ||
| } | ||
|
|
||
| @Test | ||
| void processWebhookReturns404WhenV1DisabledForValidAndInvalidIds() { | ||
| webhookProperties.getV1().setDisabled(true); | ||
|
|
||
| ResponseEntity<String> validResponse = controller.processWebhook( | ||
| UUID.randomUUID().toString(), Map.of("ref", "refs/heads/main"), Map.of()); | ||
| ResponseEntity<String> invalidResponse = controller.processWebhook( | ||
| "not-a-uuid", Map.of(), Map.of()); | ||
|
|
||
| assertEquals(HttpStatus.NOT_FOUND, validResponse.getStatusCode()); | ||
| assertEquals(HttpStatus.NOT_FOUND, invalidResponse.getStatusCode()); | ||
| verifyNoInteractions(webhookService); | ||
| } | ||
|
|
||
| @Test | ||
| void processWebhookProcessesRequestWhenV1Enabled() { | ||
| webhookProperties.getV1().setDisabled(false); | ||
| String webhookId = UUID.randomUUID().toString(); | ||
|
|
||
| ResponseEntity<String> invalidResponse = controller.processWebhook("not-a-uuid", Map.of(), Map.of()); | ||
| ResponseEntity<String> validResponse = controller.processWebhook( | ||
| webhookId, Map.of("key", "value"), Map.of("X-Test", "1")); | ||
|
|
||
| assertEquals(HttpStatus.UNAUTHORIZED, invalidResponse.getStatusCode()); | ||
| assertEquals(HttpStatus.OK, validResponse.getStatusCode()); | ||
| verify(webhookService).processWebhook(eq(webhookId), any(), eq(Map.of("X-Test", "1"))); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
@RequestBodyis still deserialized before this check, so a malformed body gets 400 instead of 404 when v1 is disabled. Not a problem functionally, just noting the endpoint still parses bodies.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Acknowledged — keeping
@RequestBodyas-is since it does not affect valid webhook callers.