NIFI-13812 [MiNiFi][C2] Add component type into Agent Manifest Hash calculation

Signed-off-by: Ferenc Kis <briansolo1985@gmail.com>

This closes #9320.
This commit is contained in:
Ferenc Erdei 2024-09-27 16:14:47 +02:00 committed by Ferenc Kis
parent 333c2688a6
commit 74091e1540
No known key found for this signature in database
GPG Key ID: 5E1CCAC15A5958F2
2 changed files with 33 additions and 4 deletions

View File

@ -23,13 +23,21 @@ import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.nifi.c2.protocol.api.SupportedOperation;
import org.apache.nifi.c2.protocol.component.api.Bundle;
import org.apache.nifi.c2.protocol.component.api.ComponentManifest;
import org.apache.nifi.c2.protocol.component.api.DefinedType;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
public class ManifestHashProvider {
private String currentBundles = null;
@ -39,12 +47,13 @@ public class ManifestHashProvider {
public String calculateManifestHash(List<Bundle> loadedBundles, Set<SupportedOperation> supportedOperations) {
String bundleString = loadedBundles.stream()
.map(bundle -> bundle.getGroup() + bundle.getArtifact() + bundle.getVersion())
.sorted()
.collect(Collectors.joining(","));
.map(this::getComponentCoordinates)
.flatMap(Collection::stream)
.sorted()
.collect(Collectors.joining(","));
int hashCode = Objects.hash(bundleString, supportedOperations);
if (hashCode != currentHashCode
|| !(Objects.equals(bundleString, currentBundles) && Objects.equals(supportedOperations, currentSupportedOperations))) {
|| !(Objects.equals(bundleString, currentBundles) && Objects.equals(supportedOperations, currentSupportedOperations))) {
byte[] bytes;
try {
bytes = MessageDigest.getInstance("SHA-512").digest(getBytes(supportedOperations, bundleString));
@ -78,4 +87,23 @@ public class ManifestHashProvider {
}
return builder.toString();
}
private List<String> getComponentCoordinates(Bundle bundle) {
ComponentManifest componentManifest = bundle.getComponentManifest();
List<String> coordinates = componentManifest == null
? emptyList()
: Stream.of(componentManifest.getProcessors(),
componentManifest.getApis(),
componentManifest.getControllerServices(),
componentManifest.getReportingTasks())
.filter(Objects::nonNull)
.flatMap(List::stream)
.map(DefinedType::getType)
.map(type -> bundle.getGroup() + bundle.getArtifact() + bundle.getVersion() + type).toList();
return coordinates.isEmpty()
? singletonList(bundle.getGroup() + bundle.getArtifact() + bundle.getVersion())
: coordinates;
}
}

View File

@ -78,6 +78,7 @@
<logger name="org.apache.nifi.processors" level="WARN"/>
<logger name="org.apache.nifi.processors.standard.LogAttribute" level="INFO"/>
<logger name="org.apache.nifi.controller.repository.StandardProcessSession" level="WARN" />
<logger name="org.apache.nifi.manifest.StandardRuntimeManifestService" level="ERROR" />
<!-- Logger for managing logging statements for jetty -->
<logger name="org.eclipse.jetty" level="INFO"/>