Skip to content

Commit 25d8743

Browse files
Always set ACL on new projects when using API key with team
Remove the ACCESS_MANAGEMENT_ACL_ENABLED check from updateNewProjectACL so that the API key's team is added to newly created projects regardless of whether the portfolio access control feature is enabled. This aligns the behaviour with createProject via the Project API, which applies accessTeams from the request body even when the ACL feature is disabled. Consistent ACL assignment ensures the uploading/creating team retains access when the feature is later enabled. Adds tests for BOM auto-create and Project API create with ACL disabled. Signed-off-by: Valentijn Scholten <valentijnscholten@gmail.com>
1 parent d0078ec commit 25d8743

3 files changed

Lines changed: 42 additions & 8 deletions

File tree

src/main/java/org/dependencytrack/persistence/ProjectQueryManager.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,13 +1032,13 @@ public void deleteProjectsByUUIDs(Collection<UUID> uuids) {
10321032
);
10331033
executeAndCloseWithArray(sqlQuery, queryParameter);
10341034

1035-
sqlQuery = pm.newQuery(JDOQuery.SQL_QUERY_LANGUAGE, """
1035+
sqlQuery = pm.newQuery(JDOQuery.SQL_QUERY_LANGUAGE, """
10361036
DELETE FROM "DEPENDENCYMETRICS" WHERE "PROJECT_ID" = ANY(?);
10371037
""".replaceAll(Pattern.quote("= ANY(?)"), inExpression)
10381038
);
10391039
executeAndCloseWithArray(sqlQuery, queryParameter);
10401040

1041-
sqlQuery = pm.newQuery(JDOQuery.SQL_QUERY_LANGUAGE, """
1041+
sqlQuery = pm.newQuery(JDOQuery.SQL_QUERY_LANGUAGE, """
10421042
DELETE FROM "FINDINGATTRIBUTION" WHERE "PROJECT_ID" = ANY(?);
10431043
""".replaceAll(Pattern.quote("= ANY(?)"), inExpression)
10441044
);
@@ -1060,13 +1060,13 @@ public void deleteProjectsByUUIDs(Collection<UUID> uuids) {
10601060
);
10611061
executeAndCloseWithArray(sqlQuery, queryParameter);
10621062

1063-
sqlQuery = pm.newQuery(JDOQuery.SQL_QUERY_LANGUAGE, """
1063+
sqlQuery = pm.newQuery(JDOQuery.SQL_QUERY_LANGUAGE, """
10641064
DELETE FROM "ANALYSIS" WHERE "PROJECT_ID" = ANY(?);
10651065
""".replace("= ANY(?)", inExpression)
10661066
);
10671067
executeAndCloseWithArray(sqlQuery, queryParameter);
10681068

1069-
sqlQuery = pm.newQuery(JDOQuery.SQL_QUERY_LANGUAGE, """
1069+
sqlQuery = pm.newQuery(JDOQuery.SQL_QUERY_LANGUAGE, """
10701070
DELETE FROM "COMPONENT_PROPERTY" WHERE "COMPONENT_ID" IN (
10711071
SELECT "ID" FROM "COMPONENT" WHERE "PROJECT_ID" = ANY(?)
10721072
);
@@ -1119,7 +1119,7 @@ WHERE PROJECT.ID IN (SELECT value FROM STRING_SPLIT(?, ','))
11191119
executeAndCloseWithArray(sqlQuery, queryParameter);
11201120
}
11211121

1122-
sqlQuery = pm.newQuery(JDOQuery.SQL_QUERY_LANGUAGE, """
1122+
sqlQuery = pm.newQuery(JDOQuery.SQL_QUERY_LANGUAGE, """
11231123
DELETE FROM "COMPONENT" WHERE "PROJECT_ID" = ANY(?);
11241124
""".replace("= ANY(?)", inExpression)
11251125
);
@@ -1318,7 +1318,7 @@ WHERE PROJECT.ID IN (SELECT value FROM STRING_SPLIT(?, ','))
13181318
executeAndCloseWithArray(sqlQuery, queryParameter);
13191319
}
13201320

1321-
sqlQuery = pm.newQuery(JDOQuery.SQL_QUERY_LANGUAGE, """
1321+
sqlQuery = pm.newQuery(JDOQuery.SQL_QUERY_LANGUAGE, """
13221322
DELETE FROM "PROJECT" WHERE "ID" = ANY(?);
13231323
""".replace("= ANY(?)", inExpression)
13241324
);
@@ -1564,7 +1564,7 @@ void preprocessACLs(final Query<?> query, final String inputFilter, final Map<St
15641564

15651565
/**
15661566
* Updates a Project ACL to add the principals Team to the AccessTeams
1567-
* This only happens if Portfolio Access Control is enabled and the @param principal is an ApyKey
1567+
* This only happens if @param principal is an ApyKey
15681568
* For a UserPrincipal we don't know which Team(s) to add to the ACL,
15691569
* See https://github.com/DependencyTrack/dependency-track/issues/1435
15701570
* @param project
@@ -1573,7 +1573,7 @@ void preprocessACLs(final Query<?> query, final String inputFilter, final Map<St
15731573
*/
15741574
@Override
15751575
public boolean updateNewProjectACL(Project project, Principal principal) {
1576-
if (isEnabled(ConfigPropertyConstants.ACCESS_MANAGEMENT_ACL_ENABLED) && principal instanceof ApiKey apiKey) {
1576+
if (principal instanceof ApiKey apiKey) {
15771577
final var apiTeam = apiKey.getTeams().stream().findFirst();
15781578
if (apiTeam.isPresent()) {
15791579
LOGGER.debug("adding Team to ACL of newly created project");

src/test/java/org/dependencytrack/resources/v1/BomResourceTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import alpine.common.util.UuidUtil;
2222
import alpine.model.IConfigProperty;
23+
import alpine.model.Team;
2324
import alpine.server.filters.ApiFilter;
2425
import alpine.server.filters.AuthenticationFilter;
2526
import com.fasterxml.jackson.core.StreamReadConstraints;
@@ -947,6 +948,23 @@ void uploadBomAutoCreateTest() throws Exception {
947948
Assertions.assertNotNull(project);
948949
}
949950

951+
@Test
952+
void uploadBomAutoCreateWithAclDisabledAddsApiKeyTeamTest() throws Exception {
953+
initializeWithPermissions(Permissions.BOM_UPLOAD, Permissions.PROJECT_CREATION_UPLOAD);
954+
// ACL is not enabled - updateNewProjectACL should still add the API key's team
955+
String bomString = Base64.getEncoder().encodeToString(resourceToByteArray("/unit/bom-1.xml"));
956+
BomSubmitRequest request = new BomSubmitRequest(null, "AclDisabled Example", "1.0", null, true, false, bomString);
957+
Response response = jersey.target(V1_BOM).request()
958+
.header(X_API_KEY, apiKey)
959+
.put(Entity.entity(request, MediaType.APPLICATION_JSON));
960+
Assertions.assertEquals(200, response.getStatus(), 0);
961+
Project project = qm.getProject("AclDisabled Example", "1.0");
962+
Assertions.assertNotNull(project);
963+
assertThat(project.getAccessTeams())
964+
.extracting(Team::getName)
965+
.containsOnly(team.getName());
966+
}
967+
950968
@Test
951969
void uploadBomAutoCreateWithTagsTest() throws Exception {
952970
initializeWithPermissions(Permissions.BOM_UPLOAD, Permissions.PROJECT_CREATION_UPLOAD);

src/test/java/org/dependencytrack/resources/v1/ProjectResourceTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,22 @@ void createProjectAsApiKeyWithAclEnabledAndWithExistentTeamTest() {
933933
assertThat(qm.getProject("acme-app", null)).satisfies(project ->
934934
assertThat(project.getAccessTeams()).extracting(Team::getName).containsOnly(team.getName()));
935935
}
936+
937+
@Test
938+
void createProjectWithAclDisabledAddsApiKeyTeamTest() {
939+
// ACL is not enabled - updateNewProjectACL should still add the API key's team
940+
Project project = new Project();
941+
project.setName("acme-app-acl-disabled");
942+
project.setVersion("1.0");
943+
Response response = jersey.target(V1_PROJECT)
944+
.request()
945+
.header(X_API_KEY, apiKey)
946+
.put(Entity.entity(project, MediaType.APPLICATION_JSON));
947+
assertThat(response.getStatus()).isEqualTo(201);
948+
assertThat(qm.getProject("acme-app-acl-disabled", "1.0")).satisfies(created ->
949+
assertThat(created.getAccessTeams()).extracting(Team::getName).containsOnly(team.getName()));
950+
}
951+
936952
@Test
937953
void createProjectAsLatestTest() {
938954
Project project = new Project();

0 commit comments

Comments
 (0)