Skip to content

Commit

Permalink
refactor(headers): Update spinnaker headers (spinnaker#2861)
Browse files Browse the repository at this point in the history
Update the headers to match spinnaker/kork#270
  • Loading branch information
marchello2000 authored May 1, 2019
1 parent 56a768c commit f449970
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 39 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ allprojects {
group = "com.netflix.spinnaker.orca"

ext {
spinnakerDependenciesVersion = '1.42.0'
spinnakerDependenciesVersion = '1.43.1'
if (project.hasProperty('spinnakerDependenciesVersion')) {
spinnakerDependenciesVersion = project.property('spinnakerDependenciesVersion')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import com.netflix.spinnaker.orca.listeners.Persister;
import com.netflix.spinnaker.orca.pipeline.model.Execution;
import com.netflix.spinnaker.orca.pipeline.persistence.ExecutionRepository;
import com.netflix.spinnaker.security.AuthenticatedRequest;
import org.slf4j.MDC;
import org.springframework.context.ApplicationListener;
import static com.netflix.spinnaker.security.AuthenticatedRequest.SPINNAKER_EXECUTION_ID;

/**
* Adapts events emitted by the nu-orca queue to an old-style listener.
Expand All @@ -43,14 +43,14 @@ public ExecutionListenerAdapter(ExecutionListener delegate, ExecutionRepository

@Override public void onApplicationEvent(ExecutionEvent event) {
try {
MDC.put(SPINNAKER_EXECUTION_ID, event.getExecutionId());
MDC.put(AuthenticatedRequest.Header.EXECUTION_ID.getHeader(), event.getExecutionId());
if (event instanceof ExecutionStarted) {
onExecutionStarted((ExecutionStarted) event);
} else if (event instanceof ExecutionComplete) {
onExecutionComplete((ExecutionComplete) event);
}
} finally {
MDC.remove(SPINNAKER_EXECUTION_ID);
MDC.remove(AuthenticatedRequest.Header.EXECUTION_ID.getHeader());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.netflix.spinnaker.orca.pipeline.model.Trigger;
import com.netflix.spinnaker.orca.pipeline.persistence.ExecutionNotFoundException;
import com.netflix.spinnaker.orca.pipeline.persistence.ExecutionRepository;
import com.netflix.spinnaker.security.AuthenticatedRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class ExecutionSpec extends Specification {
def "should build AuthenticationDetails containing authenticated details"() {
given:
MDC.clear()
MDC.put(AuthenticatedRequest.SPINNAKER_USER, "SpinnakerUser")
MDC.put(AuthenticatedRequest.SPINNAKER_ACCOUNTS, "Account1,Account2")
MDC.put(AuthenticatedRequest.Header.USER.header, "SpinnakerUser")
MDC.put(AuthenticatedRequest.Header.ACCOUNTS.header, "Account1,Account2")

when:
def authenticationDetails = Execution.AuthenticationDetails.build().get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ class EchoNotifyingExecutionListener implements ExecutionListener {
if (execution.type == PIPELINE) {
addApplicationNotifications(execution)
}
echoService.recordEvent(
details: [
source : "orca",
type : "orca:${execution.type}:starting".toString(),
application: execution.application,
],
content: buildContent(execution)
)
AuthenticatedRequest.allowAnonymous({
echoService.recordEvent(
details: [
source : "orca",
type : "orca:${execution.type}:starting".toString(),
application: execution.application,
],
content: buildContent(execution)
)
})
}
} catch (Exception e) {
log.error("Failed to send pipeline start event: ${execution?.id}", e)
Expand All @@ -81,14 +83,16 @@ class EchoNotifyingExecutionListener implements ExecutionListener {
if (execution.type == PIPELINE) {
addApplicationNotifications(execution)
}
echoService.recordEvent(
details: [
source : "orca",
type : "orca:${execution.type}:${wasSuccessful ? "complete" : "failed"}".toString(),
application: execution.application,
],
content: buildContent(execution)
)
AuthenticatedRequest.allowAnonymous({
echoService.recordEvent(
details: [
source : "orca",
type : "orca:${execution.type}:${wasSuccessful ? "complete" : "failed"}".toString(),
application: execution.application,
],
content: buildContent(execution)
)
})
}
} catch (Exception e) {
log.error("Failed to send pipeline end event: ${execution?.id}", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ import com.netflix.spinnaker.orca.pipeline.model.Stage
import com.netflix.spinnaker.orca.pipeline.model.Task
import com.netflix.spinnaker.orca.pipeline.persistence.ExecutionRepository
import com.netflix.spinnaker.orca.pipeline.util.ContextParameterProcessor
import com.netflix.spinnaker.security.AuthenticatedRequest
import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import org.slf4j.MDC
import org.springframework.beans.factory.annotation.Autowired
import static com.netflix.spinnaker.orca.ExecutionStatus.*
import static com.netflix.spinnaker.orca.pipeline.model.Execution.ExecutionType.ORCHESTRATION
import static com.netflix.spinnaker.security.AuthenticatedRequest.SPINNAKER_EXECUTION_ID
import static com.netflix.spinnaker.security.AuthenticatedRequest.SPINNAKER_USER

/**
* Converts execution events to Echo events.
Expand Down Expand Up @@ -127,12 +126,14 @@ class EchoNotifyingStageListener implements StageListener {
}

try {
MDC.put(SPINNAKER_EXECUTION_ID, stage.execution.id);
MDC.put(SPINNAKER_USER, stage.execution?.authentication?.user ?: "anonymous")
echoService.recordEvent(event)
MDC.put(AuthenticatedRequest.Header.EXECUTION_ID.header, stage.execution.id)
MDC.put(AuthenticatedRequest.Header.USER.header, stage.execution?.authentication?.user ?: "anonymous")
AuthenticatedRequest.allowAnonymous({
echoService.recordEvent(event)
})
} finally {
MDC.remove(SPINNAKER_EXECUTION_ID)
MDC.remove(SPINNAKER_USER)
MDC.remove(AuthenticatedRequest.Header.EXECUTION_ID.header)
MDC.remove(AuthenticatedRequest.Header.USER.header)
}
} catch (Exception e) {
log.error("Failed to send ${type} event ${phase} ${stage.execution.id} ${maybeTask.map { Task task -> task.name }}", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ class EchoNotifyingExecutionListenerSpec extends Specification {
pipeline.notifications == [slackPipes]

1 * front50Service.getApplicationNotifications("myapp") >> {
assert MDC.get(AuthenticatedRequest.SPINNAKER_USER) == "[email protected]"
assert MDC.get(AuthenticatedRequest.SPINNAKER_ACCOUNTS) == "someAccount,anotherAccount"
assert MDC.get(AuthenticatedRequest.Header.USER.header) == "[email protected]"
assert MDC.get(AuthenticatedRequest.Header.ACCOUNTS.header) == "someAccount,anotherAccount"
return notifications
}
1 * echoService.recordEvent(_)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

package com.netflix.spinnaker.orca.q.audit

import com.netflix.spinnaker.orca.q.ApplicationAware
import com.netflix.spinnaker.orca.q.ExecutionLevel
import com.netflix.spinnaker.orca.q.StageLevel
import com.netflix.spinnaker.orca.q.TaskLevel
import com.netflix.spinnaker.q.Message
import com.netflix.spinnaker.q.MessageHandler
import com.netflix.spinnaker.security.AuthenticatedRequest.SPINNAKER_EXECUTION_ID
import com.netflix.spinnaker.security.AuthenticatedRequest
import org.slf4j.MDC
import org.springframework.beans.factory.config.BeanPostProcessor
import org.springframework.stereotype.Component
Expand All @@ -45,19 +46,30 @@ class ExecutionTrackingMessageHandlerPostProcessor : BeanPostProcessor {
try {
when(message) {
is TaskLevel -> {
MDC.put(SPINNAKER_EXECUTION_ID, "${message.executionId}:${message.stageId}:${message.taskId}")
MDC.put(
AuthenticatedRequest.Header.EXECUTION_ID.header,
"${message.executionId}:${message.stageId}:${message.taskId}")
}
is StageLevel -> {
MDC.put(SPINNAKER_EXECUTION_ID, "${message.executionId}:${message.stageId}")
MDC.put(
AuthenticatedRequest.Header.EXECUTION_ID.header,
"${message.executionId}:${message.stageId}")
}
is ExecutionLevel -> {
MDC.put(SPINNAKER_EXECUTION_ID, message.executionId)
MDC.put(
AuthenticatedRequest.Header.EXECUTION_ID.header,
message.executionId)
}
}

if (message is ApplicationAware) {
MDC.put(AuthenticatedRequest.Header.APPLICATION.header, message.application)
}

delegate.invoke(message)
} finally {
MDC.remove(SPINNAKER_EXECUTION_ID)
MDC.remove(AuthenticatedRequest.Header.EXECUTION_ID.header)
MDC.remove(AuthenticatedRequest.Header.APPLICATION.header)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ class RunTaskHandler(

private fun Stage.withLoggingContext(taskModel: com.netflix.spinnaker.orca.pipeline.model.Task, block: () -> Unit) {
try {
MDC.put("application", this.execution.application)
MDC.put("stageType", type)
MDC.put("taskType", taskModel.implementingClass)

Expand All @@ -318,7 +317,6 @@ class RunTaskHandler(
MDC.remove("stageType")
MDC.remove("taskType")
MDC.remove("taskStartTime")
MDC.remove("application")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class OperationsControllerSpec extends Specification {
buildService.getBuild(buildNumber, master, job) >> buildInfo

if (queryUser) {
MDC.put(AuthenticatedRequest.SPINNAKER_USER, queryUser)
MDC.put(AuthenticatedRequest.Header.USER.header, queryUser)
}
when:
controller.orchestrate(requestedPipeline, Mock(HttpServletResponse))
Expand Down

0 comments on commit f449970

Please sign in to comment.