Skip to content

Commit

Permalink
refactor user subject
Browse files Browse the repository at this point in the history
  • Loading branch information
sahibamittal committed Jun 18, 2024
1 parent 36e4e70 commit c26b1cb
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import org.dependencytrack.proto.notification.v1.ProjectVulnAnalysisCompleteSubject;
import org.dependencytrack.proto.notification.v1.ProjectVulnAnalysisStatus;
import org.dependencytrack.proto.notification.v1.Scope;
import org.dependencytrack.proto.notification.v1.UserPrincipalSubject;
import org.dependencytrack.proto.notification.v1.UserSubject;
import org.dependencytrack.proto.notification.v1.VexConsumedOrProcessedSubject;
import org.dependencytrack.proto.notification.v1.Vulnerability;
import org.dependencytrack.proto.notification.v1.VulnerabilityAnalysis;
Expand Down Expand Up @@ -74,7 +74,7 @@
PolicyViolationSubject.class,
Project.class,
ProjectVulnAnalysisCompleteSubject.class,
UserPrincipalSubject.class,
UserSubject.class,
ProjectVulnAnalysisStatus.class,
Scope.class,
Timestamp.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.dependencytrack.proto.notification.v1.PolicyViolationAnalysisDecisionChangeSubject;
import org.dependencytrack.proto.notification.v1.PolicyViolationSubject;
import org.dependencytrack.proto.notification.v1.ProjectVulnAnalysisCompleteSubject;
import org.dependencytrack.proto.notification.v1.UserPrincipalSubject;
import org.dependencytrack.proto.notification.v1.UserSubject;
import org.dependencytrack.proto.notification.v1.VexConsumedOrProcessedSubject;
import org.dependencytrack.proto.notification.v1.VulnerabilityAnalysisDecisionChangeSubject;

Expand All @@ -45,6 +45,7 @@ public class PublishContext {
private static final String SUBJECT_PROJECT = "project";
private static final String SUBJECT_VULNERABILITY = "vulnerability";
private static final String SUBJECT_VULNERABILITIES = "vulnerabilities";
private static final String SUBJECT_USER = "user";

private final String kafkaTopic;
private final int kafkaTopicPartition;
Expand Down Expand Up @@ -130,9 +131,9 @@ public static PublishContext fromRecord(final ConsumerRecord<String, Notificatio
} else if (notification.getSubject().is(VexConsumedOrProcessedSubject.class)) {
final VexConsumedOrProcessedSubject subject = notification.getSubject().unpack(VexConsumedOrProcessedSubject.class);
notificationSubjects.put(SUBJECT_PROJECT, Project.convert(subject.getProject()));
} else if (notification.getSubject().is(UserPrincipalSubject.class)) {
final UserPrincipalSubject subject = notification.getSubject().unpack(UserPrincipalSubject.class);
notificationSubjects.put("User", subject);
} else if (notification.getSubject().is(UserSubject.class)) {
final UserSubject subject = notification.getSubject().unpack(UserSubject.class);
notificationSubjects.put(SUBJECT_USER, User.convert(subject));
}

return new PublishContext(consumerRecord.topic(), consumerRecord.partition(), consumerRecord.offset(),
Expand Down Expand Up @@ -221,6 +222,17 @@ private static Vulnerability convert(final org.dependencytrack.proto.notificatio

}

public record User(String username, String email) {

private static User convert(final UserSubject userSubject) {
if (userSubject == null) {
return null;
}
return new User(userSubject.getUsername(), userSubject.getEmail());
}

}

public String kafkaTopic() {
return kafkaTopic;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.dependencytrack.proto.notification.v1.PolicyViolationAnalysisDecisionChangeSubject;
import org.dependencytrack.proto.notification.v1.PolicyViolationSubject;
import org.dependencytrack.proto.notification.v1.ProjectVulnAnalysisCompleteSubject;
import org.dependencytrack.proto.notification.v1.UserPrincipalSubject;
import org.dependencytrack.proto.notification.v1.UserSubject;
import org.dependencytrack.proto.notification.v1.VexConsumedOrProcessedSubject;
import org.dependencytrack.proto.notification.v1.VulnerabilityAnalysisDecisionChangeSubject;
import org.eclipse.microprofile.config.ConfigProvider;
Expand Down Expand Up @@ -132,8 +132,8 @@ default String prepareTemplate(final Notification notification, final PebbleTemp
}
}
else if (notification.getScope() == SCOPE_SYSTEM) {
if (notification.getSubject().is(UserPrincipalSubject.class)) {
final var subject = notification.getSubject().unpack(UserPrincipalSubject.class);
if (notification.getSubject().is(UserSubject.class)) {

Check warning on line 135 in notification-publisher/src/main/java/org/dependencytrack/notification/publisher/Publisher.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

notification-publisher/src/main/java/org/dependencytrack/notification/publisher/Publisher.java#L135

These nested if statements could be combined
final var subject = notification.getSubject().unpack(UserSubject.class);
context.put("subject", subject);
context.put("subjectJson", JsonFormat.printer().print(subject));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import org.dependencytrack.proto.notification.v1.PolicyViolationAnalysisDecisionChangeSubject;
import org.dependencytrack.proto.notification.v1.PolicyViolationSubject;
import org.dependencytrack.proto.notification.v1.Project;
import org.dependencytrack.proto.notification.v1.UserPrincipalSubject;
import org.dependencytrack.proto.notification.v1.UserSubject;
import org.dependencytrack.proto.notification.v1.VexConsumedOrProcessedSubject;
import org.dependencytrack.proto.notification.v1.Vulnerability;
import org.dependencytrack.proto.notification.v1.VulnerabilityAnalysisDecisionChangeSubject;
Expand Down Expand Up @@ -852,7 +852,7 @@ void testResolveRulesUserCreatedNotification() throws Exception {
.setScope(SCOPE_SYSTEM)
.setGroup(GROUP_USER_CREATED)
.setLevel(LEVEL_INFORMATIONAL)
.setSubject(Any.pack(UserPrincipalSubject.newBuilder()
.setSubject(Any.pack(UserSubject.newBuilder()
.setUsername("username")
.setEmail("email.com")
.build()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.dependencytrack.proto.notification.v1.PolicyViolationSubject;
import org.dependencytrack.proto.notification.v1.Project;
import org.dependencytrack.proto.notification.v1.ProjectVulnAnalysisCompleteSubject;
import org.dependencytrack.proto.notification.v1.UserPrincipalSubject;
import org.dependencytrack.proto.notification.v1.UserSubject;
import org.dependencytrack.proto.notification.v1.VexConsumedOrProcessedSubject;
import org.dependencytrack.proto.notification.v1.VulnerabilityAnalysisDecisionChangeSubject;
import org.junit.jupiter.api.Nested;
Expand Down Expand Up @@ -516,13 +516,13 @@ void testWithVexProcessedSubject() throws Exception {
}

@Test
void testWithUserPrincipalSubject() throws Exception {
void testWithUserSubject() throws Exception {
final var notification = Notification.newBuilder()
.setGroup(GROUP_USER_CREATED)
.setLevel(LEVEL_INFORMATIONAL)
.setScope(SCOPE_SYSTEM)
.setTimestamp(Timestamps.fromSeconds(666))
.setSubject(Any.pack(UserPrincipalSubject.newBuilder()
.setSubject(Any.pack(UserSubject.newBuilder()
.setUsername("username")
.setEmail("email.com")
.build()))
Expand All @@ -537,10 +537,11 @@ void testWithUserPrincipalSubject() throws Exception {
assertThat(ctx.notificationScope()).isEqualTo("SCOPE_SYSTEM");
assertThat(ctx.notificationTimestamp()).isEqualTo("1970-01-01T00:11:06.000Z");
assertThat(ctx.notificationSubjects())
.hasEntrySatisfying("User", userObj -> {
final var user = (UserPrincipalSubject) userObj;
assertThat(user.getUsername()).isEqualTo("username");
assertThat(user.getEmail()).isEqualTo("email.com");
.hasEntrySatisfying("user", userObj -> {
assertThat(userObj).isInstanceOf(PublishContext.User.class);
final var user = (PublishContext.User) userObj;
assertThat(user.username()).isEqualTo("username");
assertThat(user.email()).isEqualTo("email.com");
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ message ProjectVulnAnalysisCompleteSubject {
string token = 4;
}

message UserPrincipalSubject {
message UserSubject {
string username = 1;
string email = 2;
optional string email = 2;
}

enum ProjectVulnAnalysisStatus {
Expand Down

0 comments on commit c26b1cb

Please sign in to comment.