Skip to content
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

feat(echo): Support for a set of required roles in the Manual Judgment stage #3991

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions orca-echo/orca-echo.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
implementation("org.springframework.boot:spring-boot-autoconfigure")
implementation("javax.validation:validation-api")
implementation("com.netflix.spinnaker.fiat:fiat-core:$fiatVersion")
implementation("com.netflix.spinnaker.fiat:fiat-api:$fiatVersion")

testImplementation("com.squareup.retrofit:retrofit-mock")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.netflix.spinnaker.orca.echo.pipeline
import com.fasterxml.jackson.annotation.JsonAnyGetter
import com.fasterxml.jackson.annotation.JsonAnySetter
import com.fasterxml.jackson.annotation.JsonIgnore
import com.netflix.spinnaker.fiat.shared.FiatPermissionEvaluator
import com.netflix.spinnaker.orca.api.pipeline.models.ExecutionStatus
import com.netflix.spinnaker.orca.api.pipeline.OverridableTimeoutRetryableTask
import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution
Expand Down Expand Up @@ -72,33 +73,59 @@ class ManualJudgmentStage implements StageDefinitionBuilder, AuthenticatedStage
final long backoffPeriod = 15000
final long timeout = TimeUnit.DAYS.toMillis(3)

@Autowired(required = false)
EchoService echoService
final EchoService echoService
final FiatPermissionEvaluator fiatPermissionEvaluator

@Autowired
WaitForManualJudgmentTask(Optional<EchoService> echoService, Optional<FiatPermissionEvaluator> fiatPermissionEvaluator) {
this.echoService = echoService.orElse(null)
this.fiatPermissionEvaluator = fiatPermissionEvaluator.orElse(null)
}

@Override
TaskResult execute(StageExecution stage) {
StageData stageData = stage.mapTo(StageData)
String notificationState
ExecutionStatus executionStatus

switch (stageData.state) {
case StageData.State.CONTINUE:
notificationState = "manualJudgmentContinue"
executionStatus = ExecutionStatus.SUCCEEDED
break
case StageData.State.STOP:
notificationState = "manualJudgmentStop"
executionStatus = ExecutionStatus.TERMINAL
break
default:
notificationState = "manualJudgment"
executionStatus = ExecutionStatus.RUNNING
break
String notificationState = "manualJudgment"
ExecutionStatus executionStatus = ExecutionStatus.RUNNING

def updatedStageContext = [:]

def hasRequiredActionRoles = true
if (fiatPermissionEvaluator && stageData.requiredActionRoles && !stageData.judgmentStatus?.trim()?.isEmpty()) {
// AuthenticatedRequest.getSpinnakerUser() does not yet reflect the user that judged this stage until it has
// been marked CONTINUE (see ManualJudgmentStage.authenticatedUser()).
def judgingUser = stage.lastModified?.user
def userPermissionRoles = fiatPermissionEvaluator.getPermission(judgingUser).roles.collect { it.name }

def missingRequiredActionRoles = stageData.requiredActionRoles - userPermissionRoles.intersect(stageData.requiredActionRoles)
if (missingRequiredActionRoles) {
log.debug(
"Judging user '{}' is missing the following required action roles: {}",
judgingUser,
missingRequiredActionRoles
)
hasRequiredActionRoles = false
}
}

Map outputs = processNotifications(stage, stageData, notificationState)
if (!hasRequiredActionRoles) {
// one or more required action roles are missing, prepare stage context for re-judging.
updatedStageContext.judgementStatus = ""
} else {
switch (stageData.state) {
case StageData.State.CONTINUE:
notificationState = "manualJudgmentContinue"
executionStatus = ExecutionStatus.SUCCEEDED
break
case StageData.State.STOP:
notificationState = "manualJudgmentStop"
executionStatus = ExecutionStatus.TERMINAL
break
}
}

return TaskResult.builder(executionStatus).context(outputs).build()
updatedStageContext = updatedStageContext + processNotifications(stage, stageData, notificationState)
return TaskResult.builder(executionStatus).context(updatedStageContext).build()
}

Map processNotifications(StageExecution stage, StageData stageData, String notificationState) {
Expand Down Expand Up @@ -130,6 +157,8 @@ class ManualJudgmentStage implements StageDefinitionBuilder, AuthenticatedStage
List<Notification> notifications = []
boolean propagateAuthenticationContext

List<String> requiredActionRoles = []

State getState() {
switch (judgmentStatus?.toLowerCase()) {
case "continue":
Expand Down