-
Notifications
You must be signed in to change notification settings - Fork 10
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
ENG-50887: Mask value using a masking config #227
Merged
Merged
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
46b1921
create masking config
siddhant2001 5e424ae
WIP: time range filter
siddhant2001 4f178ae
working
siddhant2001 a2a381c
fix app.conf
siddhant2001 e167411
comments
siddhant2001 1167c62
remove comment
siddhant2001 3201839
remove state
siddhant2001 22a7c05
stateless changes
siddhant2001 e033b5b
spotless
siddhant2001 0421c5d
comments
siddhant2001 345b5c5
comments
siddhant2001 4559457
tests
siddhant2001 e89416f
logical col idx
siddhant2001 858fbbc
Merge branch 'main' into mask-unbofuscated-cookies
siddhant2001 19e4834
application.conf comments
siddhant2001 a055b31
remove stale comments
siddhant2001 398ac00
Merge branch 'mask-unbofuscated-cookies' of github.com:hypertrace/que…
siddhant2001 fdfcfcb
comments
siddhant2001 8fd154f
changes
siddhant2001 041e520
change config
siddhant2001 916076e
cleanup tests
siddhant2001 7b5e7ca
change
siddhant2001 dc54e02
refactor
siddhant2001 612f3bd
remove extra line
siddhant2001 02d2d72
refactor
siddhant2001 4ebe89c
refactor
siddhant2001 5375d85
list to set
siddhant2001 d38e08c
vuln fix
siddhant2001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
...vice-impl/src/main/java/org/hypertrace/core/query/service/HandlerScopedMaskingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package org.hypertrace.core.query.service; | ||
|
||
import com.typesafe.config.Config; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import lombok.Value; | ||
import lombok.experimental.NonFinal; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class HandlerScopedMaskingConfig { | ||
private static final String TENANT_SCOPED_MASKS_CONFIG_KEY = "tenantScopedMaskingCriteria"; | ||
private final Map<String, List<MaskValuesForTimeRange>> tenantToMaskValuesMap; | ||
private HashMap<String, String> maskedValue = new HashMap<>(); | ||
|
||
public HandlerScopedMaskingConfig(Config config) { | ||
if (config.hasPath(TENANT_SCOPED_MASKS_CONFIG_KEY)) { | ||
this.tenantToMaskValuesMap = | ||
config.getConfigList(TENANT_SCOPED_MASKS_CONFIG_KEY).stream() | ||
.map(maskConfig -> new TenantMasks(maskConfig)) | ||
.collect( | ||
Collectors.toMap( | ||
tenantFilters -> tenantFilters.tenantId, | ||
tenantFilters -> tenantFilters.maskValues)); | ||
} else { | ||
this.tenantToMaskValuesMap = Collections.emptyMap(); | ||
} | ||
} | ||
|
||
public void parseColumns(ExecutionContext executionContext) { | ||
String tenantId = executionContext.getTenantId(); | ||
maskedValue.clear(); | ||
if (!tenantToMaskValuesMap.containsKey(tenantId)) { | ||
return; | ||
} | ||
|
||
Optional<QueryTimeRange> queryTimeRange = executionContext.getQueryTimeRange(); | ||
Instant queryStartTime, queryEndTime; | ||
if (queryTimeRange.isPresent()) { | ||
queryStartTime = queryTimeRange.get().getStartTime(); | ||
queryEndTime = queryTimeRange.get().getEndTime(); | ||
} else { | ||
queryEndTime = Instant.MAX; | ||
queryStartTime = Instant.MIN; | ||
} | ||
for (MaskValuesForTimeRange timeRangeAndMasks : tenantToMaskValuesMap.get(tenantId)) { | ||
boolean timeRangeOverlap = | ||
isTimeRangeOverlap(timeRangeAndMasks, queryStartTime, queryEndTime); | ||
|
||
if (timeRangeOverlap) { | ||
Map<String, String> attributeToMaskedValue = | ||
timeRangeAndMasks.maskValues.attributeToMaskedValue; | ||
for (String attribute : attributeToMaskedValue.keySet()) { | ||
maskedValue.put(attribute, attributeToMaskedValue.get(attribute)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static boolean isTimeRangeOverlap( | ||
MaskValuesForTimeRange timeRangeAndMasks, Instant queryStartTime, Instant queryEndTime) { | ||
boolean timeRangeOverlap = true; | ||
|
||
if (timeRangeAndMasks.getStartTimeMillis().isPresent()) { | ||
Instant startTimeInstant = Instant.ofEpochMilli(timeRangeAndMasks.getStartTimeMillis().get()); | ||
if (startTimeInstant.isBefore(queryStartTime) || startTimeInstant.isAfter(queryEndTime)) { | ||
timeRangeOverlap = false; | ||
} | ||
} | ||
|
||
if (timeRangeAndMasks.getEndTimeMillis().isPresent()) { | ||
Instant endTimeInstant = Instant.ofEpochMilli(timeRangeAndMasks.getEndTimeMillis().get()); | ||
if (endTimeInstant.isBefore(queryStartTime) || endTimeInstant.isAfter(queryEndTime)) { | ||
timeRangeOverlap = false; | ||
} | ||
} | ||
return timeRangeOverlap; | ||
} | ||
|
||
public boolean shouldMask(String attributeName) { | ||
return this.maskedValue.containsKey(attributeName); | ||
} | ||
|
||
public String getMaskedValue(String attributeName) { | ||
return this.maskedValue.get(attributeName); | ||
} | ||
|
||
@Value | ||
@NonFinal | ||
private class TenantMasks { | ||
private static final String TENANT_ID_CONFIG_KEY = "tenantId"; | ||
private static final String TIME_RANGE_AND_MASK_VALUES_CONFIG_KEY = "timeRangeAndMaskValues"; | ||
String tenantId; | ||
List<MaskValuesForTimeRange> maskValues; | ||
|
||
private TenantMasks(Config config) { | ||
this.tenantId = config.getString(TENANT_ID_CONFIG_KEY); | ||
this.maskValues = | ||
config.getConfigList(TIME_RANGE_AND_MASK_VALUES_CONFIG_KEY).stream() | ||
.map(MaskValuesForTimeRange::new) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. filter out the empty maskings |
||
.collect(Collectors.toList()); | ||
} | ||
} | ||
|
||
@Value | ||
private class MaskValues { | ||
Map<String, String> attributeToMaskedValue; | ||
|
||
MaskValues(Map<String, String> columnToMaskedValue) { | ||
this.attributeToMaskedValue = columnToMaskedValue; | ||
} | ||
} | ||
|
||
@Value | ||
@NonFinal | ||
class MaskValuesForTimeRange { | ||
private static final String START_TIME_CONFIG_PATH = "startTimeMillis"; | ||
private static final String END_TIME_CONFIG_PATH = "endTimeMillis"; | ||
private static final String MASK_VALUE_CONFIG_PATH = "maskValues"; | ||
private static final String ATTRIBUTE_ID_CONFIG_PATH = "attributeId"; | ||
private static final String MASKED_VALUE_CONFIG_PATH = "maskedValue"; | ||
Optional<Long> startTimeMillis; | ||
Optional<Long> endTimeMillis; | ||
MaskValues maskValues; | ||
|
||
private MaskValuesForTimeRange(Config config) { | ||
if (config.hasPath(START_TIME_CONFIG_PATH) && config.hasPath(END_TIME_CONFIG_PATH)) { | ||
this.startTimeMillis = Optional.of(config.getLong(START_TIME_CONFIG_PATH)); | ||
this.endTimeMillis = Optional.of(config.getLong(END_TIME_CONFIG_PATH)); | ||
} else { | ||
startTimeMillis = Optional.empty(); | ||
endTimeMillis = Optional.empty(); | ||
} | ||
if (config.hasPath(MASK_VALUE_CONFIG_PATH)) { | ||
List<Config> maskedValuesList = | ||
new ArrayList<>(config.getConfigList(MASK_VALUE_CONFIG_PATH)); | ||
HashMap<String, String> maskedValuesMap = new HashMap<>(); | ||
maskedValuesList.forEach( | ||
maskedValue -> { | ||
maskedValuesMap.put( | ||
maskedValue.getString(ATTRIBUTE_ID_CONFIG_PATH), | ||
maskedValue.getString(MASKED_VALUE_CONFIG_PATH)); | ||
}); | ||
|
||
maskValues = new MaskValues(maskedValuesMap); | ||
} else { | ||
maskValues = new MaskValues(new HashMap<>()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
import org.apache.pinot.client.ResultSetGroup; | ||
import org.hypertrace.core.query.service.ExecutionContext; | ||
import org.hypertrace.core.query.service.HandlerScopedFiltersConfig; | ||
import org.hypertrace.core.query.service.HandlerScopedMaskingConfig; | ||
import org.hypertrace.core.query.service.QueryCost; | ||
import org.hypertrace.core.query.service.RequestHandler; | ||
import org.hypertrace.core.query.service.api.Expression; | ||
|
@@ -67,6 +68,7 @@ public class PinotBasedRequestHandler implements RequestHandler { | |
private QueryRequestToPinotSQLConverter request2PinotSqlConverter; | ||
private final PinotMapConverter pinotMapConverter; | ||
private HandlerScopedFiltersConfig handlerScopedFiltersConfig; | ||
private HandlerScopedMaskingConfig handlerScopedMaskingConfig; | ||
// The implementations of ResultSet are package private and hence there's no way to determine the | ||
// shape of the results | ||
// other than to do string comparison on the simple class names. In order to be able to unit test | ||
|
@@ -143,6 +145,7 @@ private void processConfig(Config config) { | |
|
||
this.handlerScopedFiltersConfig = | ||
new HandlerScopedFiltersConfig(config, this.startTimeAttributeName); | ||
this.handlerScopedMaskingConfig = new HandlerScopedMaskingConfig(config); | ||
LOG.info( | ||
"Using {}ms as the threshold for logging slow queries of handler: {}", | ||
slowQueryThreshold, | ||
|
@@ -424,7 +427,7 @@ public Observable<Row> handleRequest( | |
LOG.debug("Query results: [ {} ]", resultSetGroup.toString()); | ||
} | ||
// need to merge data especially for Pinot. That's why we need to track the map columns | ||
return this.convert(resultSetGroup, executionContext.getSelectedColumns()) | ||
return this.convert(resultSetGroup, executionContext) | ||
.doOnComplete( | ||
() -> { | ||
long requestTimeMs = stopwatch.stop().elapsed(TimeUnit.MILLISECONDS); | ||
|
@@ -493,10 +496,13 @@ private Filter rewriteLeafFilter( | |
return queryFilter; | ||
} | ||
|
||
Observable<Row> convert(ResultSetGroup resultSetGroup, LinkedHashSet<String> selectedAttributes) { | ||
Observable<Row> convert(ResultSetGroup resultSetGroup, ExecutionContext executionContext) { | ||
String tenantId = executionContext.getTenantId(); | ||
LinkedHashSet<String> selectedAttributes = executionContext.getSelectedColumns(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you move this also inside |
||
List<Row.Builder> rowBuilderList = new ArrayList<>(); | ||
if (resultSetGroup.getResultSetCount() > 0) { | ||
ResultSet resultSet = resultSetGroup.getResultSet(0); | ||
handlerScopedMaskingConfig.parseColumns(executionContext); | ||
// Pinot has different Response format for selection and aggregation/group by query. | ||
if (resultSetTypePredicateProvider.isSelectionResultSetType(resultSet)) { | ||
// map merging is only supported in the selection. Filtering and Group by has its own | ||
|
@@ -508,6 +514,7 @@ Observable<Row> convert(ResultSetGroup resultSetGroup, LinkedHashSet<String> sel | |
handleAggregationAndGroupBy(resultSetGroup, rowBuilderList); | ||
} | ||
} | ||
|
||
return Observable.fromIterable(rowBuilderList) | ||
.map(Builder::build) | ||
.doOnNext(row -> LOG.debug("collect a row: {}", row)); | ||
|
@@ -536,7 +543,11 @@ private void handleSelection( | |
for (String logicalName : selectedAttributes) { | ||
// colVal will never be null. But getDataRow can throw a runtime exception if it failed | ||
// to retrieve data | ||
String colVal = resultAnalyzer.getDataFromRow(rowId, logicalName); | ||
String colVal = | ||
!handlerScopedMaskingConfig.shouldMask(logicalName) | ||
? resultAnalyzer.getDataFromRow(rowId, logicalName) | ||
: handlerScopedMaskingConfig.getMaskedValue(logicalName); | ||
|
||
builder.addColumn(Value.newBuilder().setString(colVal).build()); | ||
} | ||
} | ||
|
@@ -678,4 +689,8 @@ private boolean isInvalidExpression(Expression expression) { | |
&& viewDefinition.getColumnType(expression.getAttributeExpression().getAttributeId()) | ||
!= ValueType.STRING_MAP; | ||
} | ||
|
||
HandlerScopedMaskingConfig getHandlerScopedMaskingConfig() { | ||
return handlerScopedMaskingConfig; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default should be false, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following conditionals check for no overlap, i.e. they set the timeRangeOverlap to false. This statement is correct.