Skip to content

Commit 5f5fc51

Browse files
authored
throw error if engagement type participant roles don't match the allowed values in the config (#187)
1 parent b358e22 commit 5f5fc51

File tree

6 files changed

+71
-2
lines changed

6 files changed

+71
-2
lines changed

src/main/java/com/redhat/labs/lodestar/resource/ConfigResource.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,27 @@ public Response invalidateRbacCache() {
5757
@Path("artifact/options")
5858
@SecurityRequirement(name = "jwt")
5959
@APIResponses(value = { @APIResponse(responseCode = "401", description = "Missing or Invalid JWT"),
60-
@APIResponse(responseCode = "200", description = "Artifact ptions success.") })
61-
@Operation(summary = "Returns a map of key value pairs or artifact option.")
60+
@APIResponse(responseCode = "200", description = "Artifact options success.") })
61+
@Operation(summary = "Returns a map of key value pairs of artifact options.")
6262
public Map<String, String> getArtifactOptions() {
6363
return configService.getArtifactOptions();
6464
}
6565

66+
@GET
67+
@Path("participant/options")
68+
@SecurityRequirement(name = "jwt")
69+
@APIResponses(value = { @APIResponse(responseCode = "401", description = "Missing or Invalid JWT"),
70+
@APIResponse(responseCode = "200", description = "Participant options success.") })
71+
@Operation(summary = "Returns a map of key value pairs of participant options. " +
72+
"If the engagement type is not found it will return the default values (Residency)")
73+
public Map<String, String> getParticipantOptions(@QueryParam("engagementType") String type) {
74+
if(type == null) {
75+
return configService.getParticipantOptions();
76+
}
77+
78+
return configService.getParticipantOptions(type);
79+
}
80+
6681
@GET
6782
@Path("engagement/options")
6883
@SecurityRequirement(name = "jwt")

src/main/java/com/redhat/labs/lodestar/rest/client/ConfigApiClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public interface ConfigApiClient {
3333
@Path("artifact/options")
3434
Map<String, String> getArtifactOptions();
3535

36+
@GET
37+
@Path("participant/options")
38+
Map<String, String> getParticipantOptions(@QueryParam("engagementType") String type);
39+
40+
@GET
41+
@Path("participant/options")
42+
Map<String, String> getParticipantOptions();
43+
3644
@GET
3745
@Path("engagement/options")
3846
Map<String, String> getEngagementOptions();

src/main/java/com/redhat/labs/lodestar/service/ConfigService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ public Response getRuntimeConfig(Optional<String> type) {
5454
return configApiClient.getRuntimeConfig(type.isPresent() ? type.get() : null);
5555
}
5656

57+
@CacheResult(cacheName = "participant-options")
58+
public Map<String, String> getParticipantOptions(String type) {
59+
LOGGER.debug("cache miss for participant options ({})", type);
60+
return configApiClient.getParticipantOptions(type);
61+
}
62+
@CacheResult(cacheName = "participant-options-base")
63+
public Map<String, String> getParticipantOptions() {
64+
LOGGER.debug("cache miss for participant options (base)");
65+
return configApiClient.getParticipantOptions();
66+
}
67+
5768
@CacheResult(cacheName = "artifact-options")
5869
public Map<String, String> getArtifactOptions() {
5970
LOGGER.debug("cache miss for artifact options");

src/main/java/com/redhat/labs/lodestar/service/EngagementService.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ public Engagement create(Engagement engagement) {
146146

147147
/**
148148
* Updates the {@link Engagement} resource in the data store
149+
* This supports the way v1 frontend saves the entire engagement. In the future the FE should use direct
150+
* updates to each component
149151
*
150152
* @param engagement
151153
* @return
@@ -194,6 +196,20 @@ public Engagement update(Engagement engagement) {
194196
diff = javers.compareCollections(new HashSet<>(participants), engagement.getEngagementUsers(), EngagementUser.class);
195197
if(diff.hasChanges()) {
196198
LOGGER.debug("Participants changed {}", diff);
199+
200+
//Validate participant options
201+
Set<String> allowed = configService.getParticipantOptions(engagement.getType()).keySet();
202+
String errors = "";
203+
for(EngagementUser p : engagement.getEngagementUsers()) {
204+
if(allowed.contains(p.getRole())) {
205+
errors += String.format("Participant %s has invalid role %s. ", p.getEmail(), p.getRole());
206+
LOGGER.error("Participant {} has invalid role {} for engagement type {} - {}", p.getEmail(), p.getRole(), engagement.getType(), engagement.getUuid());
207+
}
208+
}
209+
210+
if(!errors.isEmpty()) {
211+
throw new WebApplicationException(Response.status(400).entity(Map.of("lodestarMessage", errors.trim())).build());
212+
}
197213
participants = participantService.updateParticipantsAndReload(engagementUuid, author, authorEmail,
198214
engagement.getEngagementUsers());
199215
LOGGER.debug("Updated {}", participants);

src/test/java/com/redhat/labs/lodestar/resource/ConfigResourceTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,19 @@ void testEngagementOptions() throws Exception {
137137
.body("$", Matchers.hasKey("training"));
138138
}
139139

140+
@Test
141+
void testParticipantOptions() throws Exception {
142+
String participants = ResourceLoader.load("config-participant-options.json");
143+
Map<String, String> pariticipantsMap = om.readValue(participants, Map.class);
144+
Mockito.when(configApiClient.getParticipantOptions()).thenReturn(pariticipantsMap);
145+
given().when().auth().oauth2(VALID_TOKEN).get("/participant/options").then().statusCode(200)
146+
.body("$", Matchers.hasKey("arole"))
147+
.body("$", Matchers.hasKey("brole"));
148+
149+
Mockito.when(configApiClient.getParticipantOptions("DO")).thenReturn(pariticipantsMap);
150+
given().queryParam("engagementType", "DO").when().auth().oauth2(VALID_TOKEN).get("/participant/options").then().statusCode(200)
151+
.body("$", Matchers.hasKey("arole"))
152+
.body("$", Matchers.hasKey("brole"));
153+
}
154+
140155
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"arole": "Rolly",
3+
"brole": "Polly"
4+
}

0 commit comments

Comments
 (0)