-
Notifications
You must be signed in to change notification settings - Fork 344
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
[CDAP-21064] Validate workflow token on program status trigger. #15810
Open
ritwiksahani
wants to merge
1
commit into
develop
Choose a base branch
from
CDAP-21064
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
51 changes: 51 additions & 0 deletions
51
...src/main/java/io/cdap/cdap/internal/app/runtime/schedule/trigger/NotificationContext.java
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,51 @@ | ||
/* | ||
* Copyright © 2025 Cask Data, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package io.cdap.cdap.internal.app.runtime.schedule.trigger; | ||
|
||
import io.cdap.cdap.api.workflow.WorkflowToken; | ||
import io.cdap.cdap.internal.app.store.AppMetadataStore; | ||
import io.cdap.cdap.proto.Notification; | ||
import io.cdap.cdap.proto.ProgramType; | ||
import io.cdap.cdap.proto.id.ProgramId; | ||
import io.cdap.cdap.proto.id.ProgramRunId; | ||
import io.cdap.cdap.proto.id.WorkflowId; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class NotificationContext { | ||
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. Add javadocs |
||
|
||
private final List<Notification> notifications; | ||
private final AppMetadataStore appMetadataStore; | ||
|
||
public NotificationContext(List<Notification> notifications, AppMetadataStore appMetadataStore) { | ||
this.notifications = notifications; | ||
this.appMetadataStore = appMetadataStore; | ||
} | ||
|
||
public List<Notification> getNotifications() { | ||
return notifications; | ||
} | ||
|
||
public WorkflowToken getWorkflowToken(ProgramRunId programRunId) throws IOException { | ||
ProgramId programId = programRunId.getParent(); | ||
if (!programId.getType().equals(ProgramType.WORKFLOW)) { | ||
return null; | ||
} | ||
return appMetadataStore.getWorkflowToken(new WorkflowId(programId.getParent(), programId.getProgram()), | ||
programRunId.getRun()); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
import io.cdap.cdap.api.ProgramStatus; | ||
import io.cdap.cdap.api.app.ProgramType; | ||
import io.cdap.cdap.api.schedule.TriggerInfo; | ||
import io.cdap.cdap.api.workflow.WorkflowToken; | ||
import io.cdap.cdap.common.app.RunIds; | ||
import io.cdap.cdap.internal.app.runtime.ProgramOptionConstants; | ||
import io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule; | ||
|
@@ -33,21 +34,28 @@ | |
import io.cdap.cdap.proto.ProtoTrigger; | ||
import io.cdap.cdap.proto.id.ProgramId; | ||
import io.cdap.cdap.proto.id.ProgramRunId; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A Trigger that schedules a ProgramSchedule, when a certain status of a program has been | ||
* achieved. | ||
*/ | ||
public class ProgramStatusTrigger extends ProtoTrigger.ProgramStatusTrigger implements | ||
SatisfiableTrigger { | ||
private static final String RESOLVED_PLUGIN_PROPERTIES_MAP = "resolved.plugin.properties.map"; | ||
|
||
private static final Gson GSON = new Gson(); | ||
private static final Logger LOG = | ||
LoggerFactory.getLogger( | ||
io.cdap.cdap.internal.app.runtime.schedule.trigger.ProgramStatusTrigger.class); | ||
|
||
public ProgramStatusTrigger(ProgramId programId, Set<ProgramStatus> programStatuses) { | ||
super(programId, programStatuses); | ||
|
@@ -59,13 +67,26 @@ public ProgramStatusTrigger(ProgramId programId, ProgramStatus... programStatuse | |
} | ||
|
||
@Override | ||
public boolean isSatisfied(ProgramSchedule schedule, List<Notification> notifications) { | ||
return getTriggerSatisfiedResult(notifications, false, new Function<ProgramRunInfo, Boolean>() { | ||
@Override | ||
public Boolean apply(ProgramRunInfo input) { | ||
return true; | ||
} | ||
}); | ||
public boolean isSatisfied(ProgramSchedule schedule, NotificationContext notificationContext) { | ||
return getTriggerSatisfiedResult(notificationContext.getNotifications(), false, | ||
new Function<ProgramRunInfo, Boolean>() { | ||
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. make it a lambda |
||
@Override | ||
public Boolean apply(ProgramRunInfo runInfo) { | ||
try { | ||
WorkflowToken workflowToken = notificationContext.getWorkflowToken( | ||
runInfo.getProgramRunId()); | ||
if (workflowToken != null && workflowToken.get(RESOLVED_PLUGIN_PROPERTIES_MAP) != null) { | ||
// Return true only if workflow token has been recorded and resolved properties have | ||
// been added. | ||
return true; | ||
} | ||
|
||
} catch (IOException e) { | ||
LOG.error("Reading workflow token failed for runInfo {} with error:",runInfo,e); | ||
} | ||
return false; | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
|
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
125 changes: 125 additions & 0 deletions
125
...est/java/io/cdap/cdap/internal/app/runtime/schedule/trigger/ProgramStatusTriggerTest.java
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,125 @@ | ||
/* | ||
* Copyright © 2025 Cask Data, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package io.cdap.cdap.internal.app.runtime.schedule.trigger; | ||
|
||
|
||
import static org.mockito.Mockito.mock; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.gson.Gson; | ||
import io.cdap.cdap.api.ProgramStatus; | ||
import io.cdap.cdap.api.app.ProgramType; | ||
import io.cdap.cdap.internal.app.runtime.ProgramOptionConstants; | ||
import io.cdap.cdap.internal.app.runtime.workflow.BasicWorkflowToken; | ||
import io.cdap.cdap.internal.app.store.AppMetadataStore; | ||
import io.cdap.cdap.proto.Notification; | ||
import io.cdap.cdap.proto.id.ProgramRunId; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ProgramStatusTriggerTest { | ||
|
||
private static final Gson GSON = new Gson(); | ||
private ProgramStatusTrigger trigger; | ||
private ProgramRunId programRunId; | ||
@Mock | ||
private AppMetadataStore mockMetadataStore; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
trigger = new ProgramStatusTriggerBuilder(ProgramType.WORKFLOW.name(), "program", | ||
ProgramStatus.COMPLETED, ProgramStatus.FAILED).build("default", "application", "123"); | ||
programRunId = new ProgramRunId("default", "application", | ||
io.cdap.cdap.proto.ProgramType.WORKFLOW, "program", "testRun"); | ||
mockMetadataStore = mock(AppMetadataStore.class); | ||
} | ||
|
||
@Test | ||
public void testIsSatisfiedTrue() throws Exception { | ||
Map<String, String> properties = ImmutableMap.of(ProgramOptionConstants.PROGRAM_RUN_ID, | ||
GSON.toJson(programRunId), | ||
ProgramOptionConstants.PROGRAM_STATUS, ProgramStatus.COMPLETED.name()); | ||
Notification notification = new Notification(Notification.Type.PROGRAM_STATUS, properties); | ||
List<Notification> notificationList = ImmutableList.of(notification); | ||
BasicWorkflowToken validWorkflowToken = new BasicWorkflowToken(1); | ||
validWorkflowToken.setCurrentNode("node"); | ||
validWorkflowToken.put("resolved.plugin.properties.map", ""); | ||
|
||
Mockito.when(mockMetadataStore.getWorkflowToken(Mockito.any(), Mockito.any())) | ||
.thenReturn(validWorkflowToken); | ||
boolean result = trigger.isSatisfied(null, | ||
new NotificationContext(notificationList, mockMetadataStore)); | ||
|
||
Assert.assertTrue(result); | ||
} | ||
|
||
@Test | ||
public void testIsSatisfiedFalseNullWorkflowToken() throws Exception { | ||
Map<String, String> properties = ImmutableMap.of(ProgramOptionConstants.PROGRAM_RUN_ID, | ||
GSON.toJson(programRunId), | ||
ProgramOptionConstants.PROGRAM_STATUS, ProgramStatus.COMPLETED.name()); | ||
Notification notification = new Notification(Notification.Type.PROGRAM_STATUS, properties); | ||
List<Notification> notificationList = ImmutableList.of(notification); | ||
|
||
Mockito.when(mockMetadataStore.getWorkflowToken(Mockito.any(), Mockito.any())) | ||
.thenReturn(null); | ||
boolean result = trigger.isSatisfied(null, | ||
new NotificationContext(notificationList, mockMetadataStore)); | ||
|
||
Assert.assertFalse(result); | ||
} | ||
|
||
@Test | ||
public void testIsSatisfiedFalseEmptyWorkflowToken() throws Exception { | ||
Map<String, String> properties = ImmutableMap.of(ProgramOptionConstants.PROGRAM_RUN_ID, | ||
GSON.toJson(programRunId), | ||
ProgramOptionConstants.PROGRAM_STATUS, ProgramStatus.COMPLETED.name()); | ||
Notification notification = new Notification(Notification.Type.PROGRAM_STATUS, properties); | ||
List<Notification> notificationList = ImmutableList.of(notification); | ||
BasicWorkflowToken emptyWorkflowToken = new BasicWorkflowToken(0); | ||
|
||
Mockito.when(mockMetadataStore.getWorkflowToken(Mockito.any(), Mockito.any())) | ||
.thenReturn(emptyWorkflowToken); | ||
boolean result = trigger.isSatisfied(null, | ||
new NotificationContext(notificationList, mockMetadataStore)); | ||
|
||
Assert.assertFalse(result); | ||
} | ||
|
||
@Test | ||
public void testIsSatisfiedFalseNonMatchingStatus() throws Exception { | ||
Map<String, String> properties = ImmutableMap.of(ProgramOptionConstants.PROGRAM_RUN_ID, | ||
GSON.toJson(programRunId), | ||
ProgramOptionConstants.PROGRAM_STATUS, ProgramStatus.KILLED.name()); | ||
Notification notification = new Notification(Notification.Type.PROGRAM_STATUS, properties); | ||
List<Notification> notificationList = ImmutableList.of(notification); | ||
|
||
boolean result = trigger.isSatisfied(null, | ||
new NotificationContext(notificationList, mockMetadataStore)); | ||
|
||
Assert.assertFalse(result); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please add space after comma