Skip to content

Commit

Permalink
fix(masking): disable 'enabled' functionality and directly return u…
Browse files Browse the repository at this point in the history
…sing map rather than rely on side effects
  • Loading branch information
jamfor352 committed Nov 3, 2024
1 parent 743114d commit 24f5760
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
21 changes: 7 additions & 14 deletions src/main/java/org/akhq/utils/JsonMaskByDefaultMasker.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,41 @@
import io.micronaut.context.annotation.Requires;
import jakarta.inject.Singleton;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.akhq.configs.DataMasking;
import org.akhq.configs.JsonMaskingFilter;
import org.akhq.models.Record;

import java.util.List;
import java.util.Map;

@Slf4j
@Singleton
@Requires(property = "akhq.security.data-masking.mode", value = "json_mask_by_default")
public class JsonMaskByDefaultMasker implements Masker {

private final List<JsonMaskingFilter> jsonMaskingFilters;
private final String jsonMaskReplacement;
private final boolean enabled;

public JsonMaskByDefaultMasker(DataMasking dataMasking) {
this.jsonMaskingFilters = dataMasking.getJsonFilters();
this.jsonMaskReplacement = dataMasking.getJsonMaskReplacement();
if(this.jsonMaskingFilters.isEmpty()) {
this.enabled = false;
} else {
this.enabled = true;
}
}

public Record maskRecord(Record record) {
if(!enabled) {
return record;
}
try {
if(record.isTombstone()) {
log.debug("Record at topic {}, partition {}, offset {} is a tombstone, so not masking.", record.getTopic(), record.getPartition(), record.getOffset());
return record;
} else if(record.isJson()) {
jsonMaskingFilters
return jsonMaskingFilters
.stream()
.filter(jsonMaskingFilter -> record.getTopic().getName().equalsIgnoreCase(jsonMaskingFilter.getTopic()))
.findFirst()
.ifPresentOrElse(
filter -> applyMasking(record, filter.getKeys()),
() -> applyMasking(record, List.of())
);
.map(filter -> applyMasking(record, filter.getKeys()))
.orElseGet(() -> applyMasking(record, List.of()));
} else {
log.debug("Record at topic {}, partition {}, offset {} is not JSON, so not masking.", record.getTopic(), record.getPartition(), record.getOffset());
return record;
}
} catch (Exception e) {
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/org/akhq/utils/JsonShowByDefaultMasker.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,23 @@ public class JsonShowByDefaultMasker implements Masker {

private final List<JsonMaskingFilter> jsonMaskingFilters;
private final String jsonMaskReplacement;
private final boolean enabled;

public JsonShowByDefaultMasker(DataMasking dataMasking) {
this.jsonMaskingFilters = dataMasking.getJsonFilters();
this.jsonMaskReplacement = dataMasking.getJsonMaskReplacement();
if(this.jsonMaskingFilters.isEmpty()) {
this.enabled = false;
} else {
this.enabled = true;
}
}

public Record maskRecord(Record record) {
if(!enabled) {
return record;
}
try {
if(record.isTombstone()) {
return record;
} else if(record.isJson()) {
jsonMaskingFilters
return jsonMaskingFilters
.stream()
.filter(jsonMaskingFilter -> record.getTopic().getName().equalsIgnoreCase(jsonMaskingFilter.getTopic()))
.findFirst()
.ifPresent(filter -> applyMasking(record, filter.getKeys()));
.map(filter -> applyMasking(record, filter.getKeys()))
.orElse(record);
} else {
return record;
}
Expand Down

0 comments on commit 24f5760

Please sign in to comment.