Skip to content

Commit

Permalink
Change Proto package from hyades to dependencytrack
Browse files Browse the repository at this point in the history
As this feature will be backported, we need to make sure policies will be compatible once folks start upgrading to Hyades.

Signed-off-by: nscuro <[email protected]>
  • Loading branch information
nscuro committed Sep 26, 2023
1 parent 94eb8fc commit d26815c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 36 deletions.
44 changes: 22 additions & 22 deletions src/main/java/org/dependencytrack/policy/cel/CelPolicyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
import org.dependencytrack.policy.cel.mapping.ProjectProjection;
import org.dependencytrack.policy.cel.mapping.ProjectPropertyProjection;
import org.dependencytrack.policy.cel.mapping.VulnerabilityProjection;
import org.dependencytrack.proto.policy.v1.Vulnerability;
import org.dependencytrack.util.NotificationUtil;
import org.dependencytrack.util.VulnerabilityUtil;
import org.hyades.proto.policy.v1.Vulnerability;
import org.projectnessie.cel.tools.ScriptCreateException;
import org.projectnessie.cel.tools.ScriptException;

Expand Down Expand Up @@ -145,18 +145,18 @@ public void evaluateProject(final UUID uuid) {
LOGGER.debug("Requirements for project %s and %d policy conditions: %s"
.formatted(uuid, conditionScriptPairs.size(), requirements));

final org.hyades.proto.policy.v1.Project protoProject;
final org.dependencytrack.proto.policy.v1.Project protoProject;
if (requirements.containsKey(TYPE_PROJECT)) {
protoProject = mapToProto(celQm.fetchProject(project.getId(), requirements.get(TYPE_PROJECT), requirements.get(TYPE_PROJECT_PROPERTY)));
} else {
protoProject = org.hyades.proto.policy.v1.Project.getDefaultInstance();
protoProject = org.dependencytrack.proto.policy.v1.Project.getDefaultInstance();
}

// Preload components for the entire project, to avoid excessive queries.
final List<ComponentProjection> components = celQm.fetchAllComponents(project.getId(), requirements.get(TYPE_COMPONENT));

// Preload licenses for the entire project, as chances are high that they will be used by multiple components.
final Map<Long, org.hyades.proto.policy.v1.License> licenseById;
final Map<Long, org.dependencytrack.proto.policy.v1.License> licenseById;
if (requirements.containsKey(TYPE_LICENSE) || (requirements.containsKey(TYPE_COMPONENT) && requirements.get(TYPE_COMPONENT).contains("resolved_license"))) {
licenseById = celQm.fetchAllLicenses(project.getId(), requirements.get(TYPE_LICENSE), requirements.get(TYPE_LICENSE_GROUP)).stream()
.collect(Collectors.toMap(
Expand All @@ -168,7 +168,7 @@ public void evaluateProject(final UUID uuid) {
}

// Preload vulnerabilities for the entire project, as chances are high that they will be used by multiple components.
final Map<Long, org.hyades.proto.policy.v1.Vulnerability> protoVulnById;
final Map<Long, org.dependencytrack.proto.policy.v1.Vulnerability> protoVulnById;
final Map<Long, List<Long>> vulnIdsByComponentId;
if (requirements.containsKey(TYPE_VULNERABILITY)) {
protoVulnById = celQm.fetchAllVulnerabilities(project.getId(), requirements.get(TYPE_VULNERABILITY)).stream()
Expand All @@ -190,8 +190,8 @@ public void evaluateProject(final UUID uuid) {
// Evaluate all policy conditions against all components.
final var conditionsViolated = new HashSetValuedHashMap<Long, PolicyCondition>();
for (final ComponentProjection component : components) {
final org.hyades.proto.policy.v1.Component protoComponent = mapToProto(component, licenseById);
final List<org.hyades.proto.policy.v1.Vulnerability> protoVulns =
final org.dependencytrack.proto.policy.v1.Component protoComponent = mapToProto(component, licenseById);
final List<org.dependencytrack.proto.policy.v1.Vulnerability> protoVulns =
vulnIdsByComponentId.getOrDefault(component.id, emptyList()).stream()
.map(protoVulnById::get)
.toList();
Expand Down Expand Up @@ -379,8 +379,8 @@ private static List<PolicyViolation> evaluatePolicyOperators(final Collection<Po
.toList();
}

private static org.hyades.proto.policy.v1.Project mapToProto(final ProjectProjection projection) {
final org.hyades.proto.policy.v1.Project.Builder builder = org.hyades.proto.policy.v1.Project.newBuilder()
private static org.dependencytrack.proto.policy.v1.Project mapToProto(final ProjectProjection projection) {
final org.dependencytrack.proto.policy.v1.Project.Builder builder = org.dependencytrack.proto.policy.v1.Project.newBuilder()
.setUuid(trimToEmpty(projection.uuid))
.setGroup(trimToEmpty(projection.group))
.setName(trimToEmpty(projection.name))
Expand All @@ -398,7 +398,7 @@ private static org.hyades.proto.policy.v1.Project mapToProto(final ProjectProjec
OBJECT_MAPPER.readValue(projection.propertiesJson, new TypeReference<>() {
});
for (final ProjectPropertyProjection property : properties) {
builder.addProperties(org.hyades.proto.policy.v1.Project.Property.newBuilder()
builder.addProperties(org.dependencytrack.proto.policy.v1.Project.Property.newBuilder()
.setGroup(trimToEmpty(property.group))
.setName(trimToEmpty(property.name))
.setValue(trimToEmpty(property.value))
Expand All @@ -425,10 +425,10 @@ private static org.hyades.proto.policy.v1.Project mapToProto(final ProjectProjec
return builder.build();
}

private static org.hyades.proto.policy.v1.Component mapToProto(final ComponentProjection projection,
final Map<Long, org.hyades.proto.policy.v1.License> protoLicenseById) {
final org.hyades.proto.policy.v1.Component.Builder componentBuilder =
org.hyades.proto.policy.v1.Component.newBuilder()
private static org.dependencytrack.proto.policy.v1.Component mapToProto(final ComponentProjection projection,
final Map<Long, org.dependencytrack.proto.policy.v1.License> protoLicenseById) {
final org.dependencytrack.proto.policy.v1.Component.Builder componentBuilder =
org.dependencytrack.proto.policy.v1.Component.newBuilder()
.setUuid(trimToEmpty(projection.uuid))
.setGroup(trimToEmpty(projection.group))
.setName(trimToEmpty(projection.name))
Expand All @@ -453,7 +453,7 @@ private static org.hyades.proto.policy.v1.Component mapToProto(final ComponentPr
.setBlake3(trimToEmpty(projection.blake3));

if (projection.resolvedLicenseId != null && projection.resolvedLicenseId > 0) {
final org.hyades.proto.policy.v1.License protoLicense = protoLicenseById.get(projection.resolvedLicenseId);
final org.dependencytrack.proto.policy.v1.License protoLicense = protoLicenseById.get(projection.resolvedLicenseId);
if (protoLicense != null) {
componentBuilder.setResolvedLicense(protoLicenseById.get(projection.resolvedLicenseId));
} else {
Expand All @@ -465,9 +465,9 @@ private static org.hyades.proto.policy.v1.Component mapToProto(final ComponentPr
return componentBuilder.build();
}

private static org.hyades.proto.policy.v1.License mapToProto(final LicenseProjection projection) {
final org.hyades.proto.policy.v1.License.Builder licenseBuilder =
org.hyades.proto.policy.v1.License.newBuilder()
private static org.dependencytrack.proto.policy.v1.License mapToProto(final LicenseProjection projection) {
final org.dependencytrack.proto.policy.v1.License.Builder licenseBuilder =
org.dependencytrack.proto.policy.v1.License.newBuilder()
.setUuid(trimToEmpty(projection.uuid))
.setId(trimToEmpty(projection.licenseId))
.setName(trimToEmpty(projection.name));
Expand All @@ -480,7 +480,7 @@ private static org.hyades.proto.policy.v1.License mapToProto(final LicenseProjec
try {
final ArrayNode groupsArray = OBJECT_MAPPER.readValue(projection.licenseGroupsJson, ArrayNode.class);
for (final JsonNode groupNode : groupsArray) {
licenseBuilder.addGroups(org.hyades.proto.policy.v1.License.Group.newBuilder()
licenseBuilder.addGroups(org.dependencytrack.proto.policy.v1.License.Group.newBuilder()
.setUuid(Optional.ofNullable(groupNode.get("uuid")).map(JsonNode::asText).orElse(""))
.setName(Optional.ofNullable(groupNode.get("name")).map(JsonNode::asText).orElse(""))
.build());
Expand All @@ -497,9 +497,9 @@ private static org.hyades.proto.policy.v1.License mapToProto(final LicenseProjec
private static final TypeReference<List<VulnerabilityAlias>> VULNERABILITY_ALIASES_TYPE_REF = new TypeReference<>() {
};

private static org.hyades.proto.policy.v1.Vulnerability mapToProto(final VulnerabilityProjection projection) {
final org.hyades.proto.policy.v1.Vulnerability.Builder builder =
org.hyades.proto.policy.v1.Vulnerability.newBuilder()
private static org.dependencytrack.proto.policy.v1.Vulnerability mapToProto(final VulnerabilityProjection projection) {
final org.dependencytrack.proto.policy.v1.Vulnerability.Builder builder =
org.dependencytrack.proto.policy.v1.Vulnerability.newBuilder()
.setUuid(trimToEmpty(projection.uuid))
.setId(trimToEmpty(projection.vulnId))
.setSource(trimToEmpty(projection.source))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import io.github.nscuro.versatile.VersException;
import org.apache.commons.lang3.tuple.Pair;
import org.dependencytrack.persistence.QueryManager;
import org.hyades.proto.policy.v1.Component;
import org.hyades.proto.policy.v1.License;
import org.hyades.proto.policy.v1.Project;
import org.hyades.proto.policy.v1.Vulnerability;
import org.dependencytrack.proto.policy.v1.Component;
import org.dependencytrack.proto.policy.v1.License;
import org.dependencytrack.proto.policy.v1.Project;
import org.dependencytrack.proto.policy.v1.Vulnerability;
import org.projectnessie.cel.EnvOption;
import org.projectnessie.cel.Library;
import org.projectnessie.cel.ProgramOption;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public String apply(final PolicyCondition policyCondition) {
}

final String fieldName = hash.getAlgorithm().toLowerCase().replaceAll("-", "_");
if (org.hyades.proto.policy.v1.Component.getDescriptor().findFieldByName(fieldName) == null) {
if (org.dependencytrack.proto.policy.v1.Component.getDescriptor().findFieldByName(fieldName) == null) {
LOGGER.warn("Component does not have a field named %s".formatted(fieldName));
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
syntax = "proto3";

package org.hyades.policy.v1;
package org.dependencytrack.policy.v1;

import "google/protobuf/timestamp.proto";

option java_multiple_files = true;
option java_package = "org.hyades.proto.policy.v1";
option java_package = "org.dependencytrack.proto.policy.v1";

message Component {

// UUID of the component.
string uuid = 1;

Expand Down Expand Up @@ -133,5 +132,4 @@ message Vulnerability {
string id = 1;
string source = 2;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.google.protobuf.Descriptors.Descriptor;
import org.dependencytrack.PersistenceCapableTest;
import org.hyades.proto.policy.v1.Component;
import org.hyades.proto.policy.v1.License;
import org.hyades.proto.policy.v1.Project;
import org.hyades.proto.policy.v1.Vulnerability;
import org.dependencytrack.proto.policy.v1.Component;
import org.dependencytrack.proto.policy.v1.License;
import org.dependencytrack.proto.policy.v1.Project;
import org.dependencytrack.proto.policy.v1.Vulnerability;
import org.junit.Test;

import javax.jdo.PersistenceManagerFactory;
Expand Down

0 comments on commit d26815c

Please sign in to comment.