Skip to content

Commit

Permalink
Add component property model and resource
Browse files Browse the repository at this point in the history
Supersedes DependencyTrack#2717

Co-authored-by: Robert Kesterson <rkesters@gmail.com>
Signed-off-by: nscuro <nscuro@protonmail.com>
  • Loading branch information
nscuro and rkesters committed Feb 26, 2024
1 parent ed98f57 commit ffcdab9
Show file tree
Hide file tree
Showing 8 changed files with 913 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/org/dependencytrack/model/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
@Persistent(name = "externalReferences"),
@Persistent(name = "parent"),
@Persistent(name = "children"),
@Persistent(name = "properties"),
@Persistent(name = "vulnerabilities"),
}),
@FetchGroup(name = "INTERNAL_IDENTIFICATION", members = {
Expand Down Expand Up @@ -326,6 +327,10 @@ public enum FetchGroup {
@Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering", value = "id ASC"))
private Collection<Component> children;

@Persistent(mappedBy = "component", defaultFetchGroup = "false")
@Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering", value = "groupName ASC, propertyName ASC"))
private List<ComponentProperty> properties;

@Persistent(table = "COMPONENTS_VULNERABILITIES")
@Join(column = "COMPONENT_ID")
@Element(column = "VULNERABILITY_ID")
Expand Down Expand Up @@ -708,6 +713,14 @@ public void setChildren(Collection<Component> children) {
this.children = children;
}

public List<ComponentProperty> getProperties() {
return properties;
}

public void setProperties(final List<ComponentProperty> properties) {
this.properties = properties;
}

public List<Vulnerability> getVulnerabilities() {
return vulnerabilities;
}
Expand Down
149 changes: 149 additions & 0 deletions src/main/java/org/dependencytrack/model/ComponentProperty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* This file is part of Dependency-Track.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* Copyright (c) Steve Springett. All Rights Reserved.
*/
package org.dependencytrack.model;

import alpine.model.IConfigProperty;
import alpine.server.json.TrimmedStringDeserializer;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import javax.jdo.annotations.Column;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Unique;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;

/**
* @since 4.11.0
*/
@PersistenceCapable(table = "COMPONENT_PROPERTY")
@Unique(name = "COMPONENT_PROPERTY_KEYS_IDX", members = {"component", "groupName", "propertyName"})
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ComponentProperty implements IConfigProperty, Serializable {

private static final long serialVersionUID = -7510889645969713080L;

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.NATIVE)
@JsonIgnore
private long id;

@Persistent
@Column(name = "COMPONENT_ID", allowsNull = "false")
private Component component;

@Persistent
@Column(name = "GROUPNAME", allowsNull = "false")
@NotBlank
@Size(min = 1, max = 255)
@JsonDeserialize(using = TrimmedStringDeserializer.class)
@Pattern(regexp = "\\P{Cc}+", message = "The groupName must not contain control characters")
private String groupName;

@Persistent
@Column(name = "PROPERTYNAME", allowsNull = "false")
@NotBlank
@Size(min = 1, max = 255)
@JsonDeserialize(using = TrimmedStringDeserializer.class)
@Pattern(regexp = "\\P{Cc}+", message = "The propertyName must not contain control characters")
private String propertyName;

@Persistent
@Column(name = "PROPERTYVALUE", length = 1024)
@Size(min = 0, max = 1024)
@JsonDeserialize(using = TrimmedStringDeserializer.class)
@Pattern(regexp = "\\P{Cc}+", message = "The propertyValue must not contain control characters")
private String propertyValue;

@Persistent
@Column(name = "PROPERTYTYPE", jdbcType = "VARCHAR", allowsNull = "false")
@NotNull
private PropertyType propertyType;

@Persistent
@Column(name = "DESCRIPTION")
@Size(max = 255)
@JsonDeserialize(using = TrimmedStringDeserializer.class)
@Pattern(regexp = "\\P{Cc}+", message = "The description must not contain control characters")
private String description;

public long getId() {
return id;
}

public void setId(final long id) {
this.id = id;
}

public Component getComponent() {
return component;
}

public void setComponent(final Component component) {
this.component = component;
}

public String getGroupName() {
return groupName;
}

public void setGroupName(final String groupName) {
this.groupName = groupName;
}

public String getPropertyName() {
return propertyName;
}

public void setPropertyName(final String propertyName) {
this.propertyName = propertyName;
}

public String getPropertyValue() {
return propertyValue;
}

public void setPropertyValue(final String propertyValue) {
this.propertyValue = propertyValue;
}

public PropertyType getPropertyType() {
return propertyType;
}

public void setPropertyType(final PropertyType propertyType) {
this.propertyType = propertyType;
}

public String getDescription() {
return description;
}

public void setDescription(final String description) {
this.description = description;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import alpine.common.logging.Logger;
import alpine.event.framework.Event;
import alpine.model.ApiKey;
import alpine.model.IConfigProperty;
import alpine.model.Team;
import alpine.model.UserPrincipal;
import alpine.persistence.PaginatedResult;
Expand All @@ -31,6 +32,7 @@
import org.dependencytrack.event.IndexEvent;
import org.dependencytrack.model.Component;
import org.dependencytrack.model.ComponentIdentity;
import org.dependencytrack.model.ComponentProperty;
import org.dependencytrack.model.ConfigPropertyConstants;
import org.dependencytrack.model.Project;
import org.dependencytrack.model.RepositoryMetaComponent;
Expand Down Expand Up @@ -830,4 +832,59 @@ private void getDirectDependenciesForPathDependencies(Map<String, Component> dep
}
dependencyGraph.putAll(addToDependencyGraph);
}

/**
* Returns a ComponentProperty with the specified groupName and propertyName.
*
* @param component the component the property belongs to
* @param groupName the group name of the config property
* @param propertyName the name of the property
* @return a ComponentProperty object
*/
@Override
public ComponentProperty getComponentProperty(final Component component, final String groupName, final String propertyName) {
final Query<ComponentProperty> query = this.pm.newQuery(ComponentProperty.class, "component == :component && groupName == :groupName && propertyName == :propertyName");
query.setRange(0, 1);
return singleResult(query.execute(component, groupName, propertyName));
}

/**
* Returns a List of ProjectProperty's for the specified project.
*
* @param component the project the property belongs to
* @return a List ProjectProperty objects
*/
@Override
@SuppressWarnings("unchecked")
public List<ComponentProperty> getComponentProperties(final Component component) {
final Query<ComponentProperty> query = this.pm.newQuery(ComponentProperty.class, "component == :component");
query.setOrdering("groupName asc, propertyName asc");
return (List<ComponentProperty>) query.execute(component);
}

/**
* Creates a key/value pair (ComponentProperty) for the specified Project.
*
* @param component the Component to create the property for
* @param groupName the group name of the property
* @param propertyName the name of the property
* @param propertyValue the value of the property
* @param propertyType the type of property
* @param description a description of the property
* @return the created ComponentProperty object
*/
@Override
public ComponentProperty createComponentProperty(final Component component, final String groupName, final String propertyName,
final String propertyValue, final IConfigProperty.PropertyType propertyType,
final String description) {
final ComponentProperty property = new ComponentProperty();
property.setComponent(component);
property.setGroupName(groupName);
property.setPropertyName(propertyName);
property.setPropertyValue(propertyValue);
property.setPropertyType(propertyType);
property.setDescription(description);
return persist(property);
}

}
17 changes: 17 additions & 0 deletions src/main/java/org/dependencytrack/persistence/QueryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import alpine.event.framework.Event;
import alpine.model.ApiKey;
import alpine.model.ConfigProperty;
import alpine.model.IConfigProperty;
import alpine.model.Team;
import alpine.model.UserPrincipal;
import alpine.notification.NotificationLevel;
Expand All @@ -44,6 +45,7 @@
import org.dependencytrack.model.Component;
import org.dependencytrack.model.ComponentAnalysisCache;
import org.dependencytrack.model.ComponentIdentity;
import org.dependencytrack.model.ComponentProperty;
import org.dependencytrack.model.ConfigPropertyConstants;
import org.dependencytrack.model.DependencyMetrics;
import org.dependencytrack.model.Finding;
Expand Down Expand Up @@ -557,6 +559,21 @@ public Map<String, Component> getDependencyGraphForComponents(Project project, L
return getComponentQueryManager().getDependencyGraphForComponents(project, components);
}

public List<ComponentProperty> getComponentProperties(final Component component) {
return getComponentQueryManager().getComponentProperties(component);
}

public ComponentProperty getComponentProperty(final Component component, final String groupName, final String propertyName) {
return getComponentQueryManager().getComponentProperty(component, groupName, propertyName);
}

public ComponentProperty createComponentProperty(final Component component, final String groupName, final String propertyName,
final String propertyValue, final IConfigProperty.PropertyType propertyType,
final String description) {
return getComponentQueryManager()
.createComponentProperty(component, groupName, propertyName, propertyValue, propertyType, description);
}

public PaginatedResult getLicenses() {
return getLicenseQueryManager().getLicenses();
}
Expand Down
Loading

0 comments on commit ffcdab9

Please sign in to comment.