2 * Copyright © 2020 Bell Canada
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.node.ObjectNode
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
24 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BluePrintMessageLibPropertyService
25 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BlueprintMessageProducerService
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
28 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
29 import org.onap.ccsdk.cds.controllerblueprints.core.common.ApplicationConstants
30 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
33 import org.onap.ccsdk.cds.controllerblueprints.core.utils.PropertyDefinitionUtils
34 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
35 import org.slf4j.LoggerFactory
36 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
37 import org.springframework.stereotype.Service
38 import javax.annotation.PostConstruct
41 * Audit service used to produce execution service input and output message
42 * sent into dedicated kafka topics.
44 @ConditionalOnProperty(
45 name = ["blueprintsprocessor.messageproducer.self-service-api.audit.kafkaEnable"],
49 class KafkaPublishAuditService(
50 private val bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService,
51 private val blueprintsProcessorCatalogService: BluePrintCatalogService
52 ) : PublishAuditService {
53 private var inputInstance: BlueprintMessageProducerService? = null
54 private var outputInstance: BlueprintMessageProducerService? = null
55 private val log = LoggerFactory.getLogger(KafkaPublishAuditService::class.toString())
58 const val INPUT_SELECTOR = "self-service-api.audit.request"
59 const val OUTPUT_SELECTOR = "self-service-api.audit.response"
64 log.info("Kakfa audit service is enabled")
68 * Publish execution input into a kafka topic.
69 * The correlation UUID is used to link the input to its output.
70 * Sensitive data within the request are hidden.
72 override suspend fun publish(executionServiceInput: ExecutionServiceInput) {
73 val secureExecutionServiceInput = hideSensitiveData(executionServiceInput)
74 this.inputInstance = this.getInputInstance(INPUT_SELECTOR)
75 this.inputInstance!!.sendMessage(secureExecutionServiceInput)
79 * Publish execution output into a kafka topic.
80 * The correlation UUID is used to link the output to its input.
81 * A correlation UUID is added to link the input to its output.
83 override fun publish(correlationUUID: String, executionServiceOutput: ExecutionServiceOutput) {
84 executionServiceOutput.correlationUUID = correlationUUID
85 this.outputInstance = this.getOutputInstance(OUTPUT_SELECTOR)
86 this.outputInstance!!.sendMessage(executionServiceOutput)
90 * Return the input kafka producer instance using a selector.
92 private fun getInputInstance(selector: String): BlueprintMessageProducerService = inputInstance ?: createInstance(selector)
95 * Return the output kafka producer instance using a selector.
97 private fun getOutputInstance(selector: String): BlueprintMessageProducerService = outputInstance ?: createInstance(selector)
100 * Create a kafka producer instance.
102 private fun createInstance(selector: String): BlueprintMessageProducerService {
104 "Setting up message producer($selector)..."
107 bluePrintMessageLibPropertyService
108 .blueprintMessageProducerService(selector)
109 } catch (e: Exception) {
110 throw BluePrintProcessorException("failed to create producer service ${e.message}")
115 * Hide sensitive data in the request.
116 * Sensitive data are declared in the resource resolution mapping using
117 * the property metadata "log-protect" set to true.
119 private suspend fun hideSensitiveData(
120 executionServiceInput: ExecutionServiceInput
121 ): ExecutionServiceInput {
123 var clonedExecutionServiceInput = ExecutionServiceInput().apply {
124 correlationUUID = executionServiceInput.correlationUUID
125 commonHeader = executionServiceInput.commonHeader
126 actionIdentifiers = executionServiceInput.actionIdentifiers
127 payload = executionServiceInput.payload.deepCopy()
128 stepData = executionServiceInput.stepData
131 val blueprintName = clonedExecutionServiceInput.actionIdentifiers.blueprintName
132 val workflowName = clonedExecutionServiceInput.actionIdentifiers.actionName
134 if (blueprintName == "default") return clonedExecutionServiceInput
136 if (clonedExecutionServiceInput.payload
137 .path("$workflowName-request").has("$workflowName-properties")) {
139 /** Retrieving sensitive input parameters */
140 val requestId = clonedExecutionServiceInput.commonHeader.requestId
141 val blueprintVersion = clonedExecutionServiceInput.actionIdentifiers.blueprintVersion
143 val basePath = blueprintsProcessorCatalogService.getFromDatabase(blueprintName, blueprintVersion)
145 val blueprintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(requestId, basePath.toString())
146 val blueprintContext = blueprintRuntimeService.bluePrintContext()
148 /** Looking for node templates defined as component-resource-resolution */
149 val nodeTemplates = blueprintContext.nodeTemplates()
150 nodeTemplates!!.forEach { nodeTemplate ->
151 val nodeTemplateName = nodeTemplate.key
152 val nodeTemplateType = blueprintContext.nodeTemplateByName(nodeTemplateName).type
153 if (nodeTemplateType == BluePrintConstants.NODE_TEMPLATE_TYPE_COMPONENT_RESOURCE_RESOLUTION) {
154 val interfaceName = blueprintContext.nodeTemplateFirstInterfaceName(nodeTemplateName)
155 val operationName = blueprintContext.nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName)
157 val propertyAssignments: MutableMap<String, JsonNode> =
158 blueprintContext.nodeTemplateInterfaceOperationInputs(nodeTemplateName, interfaceName, operationName)
161 val artifactPrefixNamesNode = propertyAssignments[ResourceResolutionConstants.INPUT_ARTIFACT_PREFIX_NAMES]
162 val artifactPrefixNames = JacksonUtils.getListFromJsonNode(artifactPrefixNamesNode!!, String::class.java)
164 /** Storing mapping entries with metadata log-protect set to true */
165 val sensitiveParameters: List<String> = artifactPrefixNames
166 .map { "$it-mapping" }
167 .map { blueprintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, it) }
168 .flatMap { JacksonUtils.getListFromJson(it, ResourceAssignment::class.java) }
169 .filter { PropertyDefinitionUtils.hasLogProtect(it.property) }
172 /** Hiding sensitive input parameters from the request */
173 var workflowProperties: ObjectNode = clonedExecutionServiceInput.payload
174 .path("$workflowName-request")
175 .path("$workflowName-properties") as ObjectNode
177 sensitiveParameters.forEach { sensitiveParameter ->
178 if (workflowProperties.has(sensitiveParameter)) {
179 workflowProperties.replace(sensitiveParameter, ApplicationConstants.LOG_REDACTED.asJsonPrimitive())
186 return clonedExecutionServiceInput