Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port : Enhance badge API to require authorization #941

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/org/dependencytrack/auth/Permissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public enum Permissions {
POLICY_MANAGEMENT_UPDATE("Allows the modification of a policy"),
POLICY_MANAGEMENT_DELETE("Allows the deletion of a policy"),
TAG_MANAGEMENT("Allows the modification and deletion of tags"),
TAG_MANAGEMENT_DELETE("Allows the deletion of a tag");
TAG_MANAGEMENT_DELETE("Allows the deletion of a tag"),
VIEW_BADGES("Provides the ability to view badges");

private final String description;

Expand Down Expand Up @@ -112,6 +113,7 @@ public static class Constants {
public static final String POLICY_MANAGEMENT_DELETE = "POLICY_MANAGEMENT_DELETE";
public static final String TAG_MANAGEMENT = "TAG_MANAGEMENT";
public static final String TAG_MANAGEMENT_DELETE = "TAG_MANAGEMENT_DELETE";
public static final String VIEW_BADGES = "VIEW_BADGES";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public enum ConfigPropertyConstants {
INTERNAL_CLUSTER_ID("internal", "cluster.id", UUID.randomUUID().toString(), PropertyType.STRING, "Unique identifier of the cluster", ConfigPropertyAccessMode.READ_ONLY),
INTERNAL_DEFAULT_OBJECTS_VERSION("internal", "default.objects.version", null, PropertyType.STRING, "Version of the default objects in the database", ConfigPropertyAccessMode.READ_ONLY),
GENERAL_BASE_URL("general", "base.url", null, PropertyType.URL, "URL used to construct links back to Dependency-Track from external systems", ConfigPropertyAccessMode.READ_WRITE),
GENERAL_BADGE_ENABLED("general", "badge.enabled", "false", PropertyType.BOOLEAN, "Flag to enable/disable SVG badge support from metrics", ConfigPropertyAccessMode.READ_WRITE),
GENERAL_BADGE_ENABLED("general", "badge.enabled", "false", PropertyType.BOOLEAN, "Flag to enable/disable unauthenticated access to SVG badge from metrics", ConfigPropertyAccessMode.READ_WRITE),
EMAIL_SMTP_ENABLED("email", "smtp.enabled", "false", PropertyType.BOOLEAN, "Flag to enable/disable SMTP", ConfigPropertyAccessMode.READ_WRITE),
EMAIL_SMTP_FROM_ADDR("email", "smtp.from.address", null, PropertyType.STRING, "The from email address to use to send output SMTP mail", ConfigPropertyAccessMode.READ_WRITE),
EMAIL_SMTP_SERVER_HOSTNAME("email", "smtp.server.hostname", null, PropertyType.STRING, "The hostname or IP address of the SMTP mail server", ConfigPropertyAccessMode.READ_WRITE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,21 @@ private void loadDefaultPersonas() {
final Team managers = qm.createTeam("Portfolio Managers", false);
LOGGER.debug("Creating team: Automation");
final Team automation = qm.createTeam("Automation", true);
LOGGER.debug("Creating team: Badge Viewers");
final Team badges = qm.createTeam("Badge Viewers", true);

final List<Permission> fullList = qm.getPermissions();

LOGGER.debug("Assigning default permissions to teams");
sysadmins.setPermissions(fullList);
managers.setPermissions(getPortfolioManagersPermissions(fullList));
automation.setPermissions(getAutomationPermissions(fullList));
badges.setPermissions(getBadgesPermissions(fullList));

qm.persist(sysadmins);
qm.persist(managers);
qm.persist(automation);
qm.persist(badges);

LOGGER.debug("Adding admin user to System Administrators");
qm.addUserToTeam(admin, sysadmins);
Expand Down Expand Up @@ -279,6 +283,16 @@ private List<Permission> getAutomationPermissions(final List<Permission> fullLis
return permissions;
}

private List<Permission> getBadgesPermissions(final List<Permission> fullList) {
final List<Permission> permissions = new ArrayList<>();
for (final Permission permission : fullList) {
if (permission.getName().equals(Permissions.Constants.VIEW_BADGES)) {
permissions.add(permission);
}
}
return permissions;
}

/**
* Loads the default repositories
*/
Expand Down
222 changes: 177 additions & 45 deletions src/main/java/org/dependencytrack/resources/v1/BadgeResource.java

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/main/resources/openapi-configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ openAPI:
BearerAuth:
type: http
scheme: Bearer
ApiKeyQueryAuth:
name: apiKey
type: apiKey
in: query
prettyPrint: true
resourcePackages:
- alpine.server.resources
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/dependencytrack/ResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public abstract class ResourceTest {
protected final String SIZE = "size";
protected final String TOTAL_COUNT_HEADER = "X-Total-Count";
protected final String X_API_KEY = "X-Api-Key";
protected final String API_KEY = "apiKey";
protected final String V1_TAG = "/v1/tag";

// Hashing is expensive. Do it once and re-use across tests as much as possible.
Expand Down
51 changes: 27 additions & 24 deletions src/test/java/org/dependencytrack/auth/PermissionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,50 @@
import org.junit.Assert;
import org.junit.Test;

import static org.dependencytrack.auth.Permissions.Constants.ACCESS_MANAGEMENT;
import static org.dependencytrack.auth.Permissions.Constants.ACCESS_MANAGEMENT_CREATE;
import static org.dependencytrack.auth.Permissions.Constants.ACCESS_MANAGEMENT_DELETE;
import static org.dependencytrack.auth.Permissions.Constants.ACCESS_MANAGEMENT_READ;
import static org.dependencytrack.auth.Permissions.Constants.ACCESS_MANAGEMENT_UPDATE;
import static org.dependencytrack.auth.Permissions.Constants.BOM_UPLOAD;
import static org.dependencytrack.auth.Permissions.Constants.VIEW_PORTFOLIO;
import static org.dependencytrack.auth.Permissions.Constants.POLICY_MANAGEMENT;
import static org.dependencytrack.auth.Permissions.Constants.POLICY_MANAGEMENT_CREATE;
import static org.dependencytrack.auth.Permissions.Constants.POLICY_MANAGEMENT_DELETE;
import static org.dependencytrack.auth.Permissions.Constants.POLICY_MANAGEMENT_READ;
import static org.dependencytrack.auth.Permissions.Constants.POLICY_MANAGEMENT_UPDATE;
import static org.dependencytrack.auth.Permissions.Constants.POLICY_VIOLATION_ANALYSIS;
import static org.dependencytrack.auth.Permissions.Constants.PORTFOLIO_MANAGEMENT;
import static org.dependencytrack.auth.Permissions.Constants.PORTFOLIO_MANAGEMENT_CREATE;
import static org.dependencytrack.auth.Permissions.Constants.PORTFOLIO_MANAGEMENT_DELETE;
import static org.dependencytrack.auth.Permissions.Constants.PORTFOLIO_MANAGEMENT_READ;
import static org.dependencytrack.auth.Permissions.Constants.PORTFOLIO_MANAGEMENT_UPDATE;
import static org.dependencytrack.auth.Permissions.Constants.PORTFOLIO_MANAGEMENT_DELETE;
import static org.dependencytrack.auth.Permissions.Constants.PROJECT_CREATION_UPLOAD;
import static org.dependencytrack.auth.Permissions.Constants.SYSTEM_CONFIGURATION;
import static org.dependencytrack.auth.Permissions.Constants.SYSTEM_CONFIGURATION_CREATE;
import static org.dependencytrack.auth.Permissions.Constants.SYSTEM_CONFIGURATION_DELETE;
import static org.dependencytrack.auth.Permissions.Constants.SYSTEM_CONFIGURATION_READ;
import static org.dependencytrack.auth.Permissions.Constants.SYSTEM_CONFIGURATION_UPDATE;
import static org.dependencytrack.auth.Permissions.Constants.TAG_MANAGEMENT;
import static org.dependencytrack.auth.Permissions.Constants.TAG_MANAGEMENT_DELETE;
import static org.dependencytrack.auth.Permissions.Constants.VIEW_POLICY_VIOLATION;
import static org.dependencytrack.auth.Permissions.Constants.VIEW_PORTFOLIO;
import static org.dependencytrack.auth.Permissions.Constants.VIEW_VULNERABILITY;
import static org.dependencytrack.auth.Permissions.Constants.VULNERABILITY_ANALYSIS;
import static org.dependencytrack.auth.Permissions.Constants.VULNERABILITY_ANALYSIS_CREATE;
import static org.dependencytrack.auth.Permissions.Constants.VULNERABILITY_ANALYSIS_READ;
import static org.dependencytrack.auth.Permissions.Constants.VULNERABILITY_ANALYSIS_UPDATE;
import static org.dependencytrack.auth.Permissions.Constants.VIEW_POLICY_VIOLATION;
import static org.dependencytrack.auth.Permissions.Constants.VULNERABILITY_MANAGEMENT;
import static org.dependencytrack.auth.Permissions.Constants.VULNERABILITY_MANAGEMENT_CREATE;
import static org.dependencytrack.auth.Permissions.Constants.VULNERABILITY_MANAGEMENT_DELETE;
import static org.dependencytrack.auth.Permissions.Constants.VULNERABILITY_MANAGEMENT_READ;
import static org.dependencytrack.auth.Permissions.Constants.VULNERABILITY_MANAGEMENT_UPDATE;
import static org.dependencytrack.auth.Permissions.Constants.VULNERABILITY_MANAGEMENT_DELETE;
import static org.dependencytrack.auth.Permissions.Constants.POLICY_VIOLATION_ANALYSIS;
import static org.dependencytrack.auth.Permissions.Constants.ACCESS_MANAGEMENT;
import static org.dependencytrack.auth.Permissions.Constants.ACCESS_MANAGEMENT_CREATE;
import static org.dependencytrack.auth.Permissions.Constants.ACCESS_MANAGEMENT_READ;
import static org.dependencytrack.auth.Permissions.Constants.ACCESS_MANAGEMENT_UPDATE;
import static org.dependencytrack.auth.Permissions.Constants.ACCESS_MANAGEMENT_DELETE;
import static org.dependencytrack.auth.Permissions.Constants.SYSTEM_CONFIGURATION;
import static org.dependencytrack.auth.Permissions.Constants.SYSTEM_CONFIGURATION_CREATE;
import static org.dependencytrack.auth.Permissions.Constants.SYSTEM_CONFIGURATION_READ;
import static org.dependencytrack.auth.Permissions.Constants.SYSTEM_CONFIGURATION_UPDATE;
import static org.dependencytrack.auth.Permissions.Constants.SYSTEM_CONFIGURATION_DELETE;
import static org.dependencytrack.auth.Permissions.Constants.PROJECT_CREATION_UPLOAD;
import static org.dependencytrack.auth.Permissions.Constants.POLICY_MANAGEMENT;
import static org.dependencytrack.auth.Permissions.Constants.POLICY_MANAGEMENT_CREATE;
import static org.dependencytrack.auth.Permissions.Constants.POLICY_MANAGEMENT_READ;
import static org.dependencytrack.auth.Permissions.Constants.POLICY_MANAGEMENT_UPDATE;
import static org.dependencytrack.auth.Permissions.Constants.POLICY_MANAGEMENT_DELETE;
import static org.dependencytrack.auth.Permissions.Constants.TAG_MANAGEMENT;
import static org.dependencytrack.auth.Permissions.Constants.TAG_MANAGEMENT_DELETE;
import static org.dependencytrack.auth.Permissions.Constants.VIEW_BADGES;

public class PermissionsTest {

@Test
public void testPermissionEnums() {
Assert.assertEquals(37, Permissions.values().length);
Assert.assertEquals(38, Permissions.values().length);
Assert.assertEquals("BOM_UPLOAD", Permissions.BOM_UPLOAD.name());
Assert.assertEquals("VIEW_PORTFOLIO", Permissions.VIEW_PORTFOLIO.name());
Assert.assertEquals("PORTFOLIO_MANAGEMENT", Permissions.PORTFOLIO_MANAGEMENT.name());
Expand Down Expand Up @@ -101,6 +102,7 @@ public void testPermissionEnums() {
Assert.assertEquals("POLICY_MANAGEMENT_DELETE", Permissions.POLICY_MANAGEMENT_DELETE.name());
Assert.assertEquals("TAG_MANAGEMENT", Permissions.TAG_MANAGEMENT.name());
Assert.assertEquals("TAG_MANAGEMENT_DELETE", Permissions.TAG_MANAGEMENT_DELETE.name());
Assert.assertEquals("VIEW_BADGES", Permissions.VIEW_BADGES.name());
}

@Test
Expand Down Expand Up @@ -142,5 +144,6 @@ public void testPermissionConstants() {
Assert.assertEquals("POLICY_MANAGEMENT_DELETE", POLICY_MANAGEMENT_DELETE);
Assert.assertEquals("TAG_MANAGEMENT", TAG_MANAGEMENT);
Assert.assertEquals("TAG_MANAGEMENT_DELETE", TAG_MANAGEMENT_DELETE);
Assert.assertEquals("VIEW_BADGES", VIEW_BADGES);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void testLoadDefaultPersonas() throws Exception {
Method method = generator.getClass().getDeclaredMethod("loadDefaultPersonas");
method.setAccessible(true);
method.invoke(generator);
Assert.assertEquals(3, qm.getTeams().size());
Assert.assertEquals(4, qm.getTeams().size());
}

@Test
Expand Down
Loading
Loading