Skip to content

Commit

Permalink
NIFI-13812 [MiNiFi][C2] Add component type into Agent Manifest Hash c…
Browse files Browse the repository at this point in the history
…alculation

Signed-off-by: Ferenc Kis <[email protected]>

This closes apache#9320.
  • Loading branch information
ferencerdei authored and briansolo1985 committed Oct 1, 2024
1 parent 333c268 commit 74091e1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@
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;
Expand All @@ -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));
Expand Down Expand Up @@ -78,4 +87,23 @@ private String bytesToHex(byte[] in) {
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand Down

0 comments on commit 74091e1

Please sign in to comment.