[
https://issues.apache.org/jira/browse/TINKERPOP-3028?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17852590#comment-17852590
]
ASF GitHub Bot commented on TINKERPOP-3028:
-------------------------------------------
vkagamlyk commented on code in PR #2640:
URL: https://github.com/apache/tinkerpop/pull/2640#discussion_r1628482186
##########
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/language/translator/AnonymizedTranslatorVisitor.java:
##########
@@ -0,0 +1,181 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tinkerpop.gremlin.language.translator;
+
+import org.antlr.v4.runtime.ParserRuleContext;
+import org.apache.tinkerpop.gremlin.language.grammar.GremlinParser;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * A translator that anonymizes Gremlin so that arguments that might contain
sensitive information are removed.
+ */
+public class AnonymizedTranslatorVisitor extends TranslateVisitor {
+
+ private final Map<String, Map<Object, String>> simpleNameToObjectCache =
new HashMap<>();
+
+ public AnonymizedTranslatorVisitor() {
+ this("g");
+ }
+
+ public AnonymizedTranslatorVisitor(final String graphTraversalSourceName) {
+ super(graphTraversalSourceName);
+ }
+
+ /**
+ * Anonymizes the given context by replacing the text with a lower case
version of the class name and a number
+ * that is incremented for each unique value.
+ *
+ * @param ctx the context to anonymize
+ * @param clazz the class of the context
+ * @return null
+ */
+ protected Void anonymize(final ParserRuleContext ctx, final Class<?>
clazz) {
+ final String text = ctx.getText();
+ final String type = clazz.getSimpleName();
+ Map<Object, String> objectToAnonymizedString =
simpleNameToObjectCache.get(type);
+ if (objectToAnonymizedString != null){
+ // this object type has been handled at least once before
+ final String innerValue = objectToAnonymizedString.get(text);
+ if (innerValue != null){
+ sb.append(innerValue);
+ } else {
+ final String anonymizedValue = type.toLowerCase() +
objectToAnonymizedString.size();
+ objectToAnonymizedString.put(text, anonymizedValue);
+ sb.append(anonymizedValue);
+ }
+ } else {
+ objectToAnonymizedString = new HashMap<>();
+ simpleNameToObjectCache.put(type, objectToAnonymizedString);
+ final String anonymizedValue = type.toLowerCase() +
objectToAnonymizedString.size();
+ objectToAnonymizedString.put(text, anonymizedValue);
+ sb.append(anonymizedValue);
+ }
+ return null;
+ }
+
+ @Override
+ public Void visitGenericLiteralCollection(final
GremlinParser.GenericLiteralCollectionContext ctx) {
+ return anonymize(ctx, List.class);
+ }
+
+ @Override
+ public Void visitGenericLiteralMap(final
GremlinParser.GenericLiteralMapContext ctx) {
+ return anonymize(ctx, Map.class);
+ }
+
+ @Override
+ public Void visitGenericLiteralMapNullableArgument(final
GremlinParser.GenericLiteralMapNullableArgumentContext ctx) {
+ return anonymize(ctx, Map.class);
+ }
+
+ @Override
+ public Void visitStringLiteral(final GremlinParser.StringLiteralContext
ctx) {
+ return anonymize(ctx, String.class);
+ }
+
+ @Override
+ public Void visitStringNullableLiteral(final
GremlinParser.StringNullableLiteralContext ctx) {
+ return anonymize(ctx, String.class);
+ }
+
+ @Override
+ public Void visitIntegerLiteral(final GremlinParser.IntegerLiteralContext
ctx) {
+ final String integerLiteral = ctx.getText().toLowerCase();
+
+ // check suffix
+ final int lastCharIndex = integerLiteral.length() - 1;
+ final char lastCharacter = integerLiteral.charAt(lastCharIndex);
+ switch (lastCharacter) {
+ case 'b':
+ anonymize(ctx, Byte.class);
+ break;
+ case 's':
+ anonymize(ctx, Short.class);
+ break;
+ case 'i':
+ anonymize(ctx, Integer.class);
+ break;
+ case 'l':
+ anonymize(ctx, Long.class);
+ break;
+ case 'n':
+ anonymize(ctx, BigInteger.class);
+ break;
+ default:
+ anonymize(ctx, Number.class);
+ break;
+ }
+ return null;
+ }
+
+ @Override
+ public Void visitFloatLiteral(final GremlinParser.FloatLiteralContext ctx)
{
+ final String floatLiteral = ctx.getText().toLowerCase();
+
+ // check suffix
+ final int lastCharIndex = floatLiteral.length() - 1;
+ final char lastCharacter = floatLiteral.charAt(lastCharIndex);
+ switch (lastCharacter) {
Review Comment:
do we need special handling for special values like NaN and Infinity?
> Convert translators to make use of the grammar
> ----------------------------------------------
>
> Key: TINKERPOP-3028
> URL: https://issues.apache.org/jira/browse/TINKERPOP-3028
> Project: TinkerPop
> Issue Type: Improvement
> Components: translator
> Affects Versions: 3.6.6
> Reporter: Stephen Mallette
> Assignee: Stephen Mallette
> Priority: Major
>
> {{Translator}} infrastructure uses bytecode and reflection which has a number
> of technical imperfections and exemptions for it to work properly. Switching
> to the grammar for translation simplifies the code, removes friction and
> complexity when generating GLV tests, and should perform better. The focus
> for this issue is the Java series of translators which are relied on quite
> heavily.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)