ekaterinadimitrova2 commented on a change in pull request #1043:
URL: https://github.com/apache/cassandra/pull/1043#discussion_r649332307
##########
File path: src/java/org/apache/cassandra/cql3/QueryEvents.java
##########
@@ -205,7 +219,7 @@ public void
notifyPrepareSuccess(Supplier<QueryHandler.Prepared> preparedProvide
try
{
for (Listener listener : listeners)
- listener.prepareSuccess(prepared.statement, query,
state, queryTime, response);
+ listener.prepareSuccess(prepared.statement,
possiblyObfuscateQuery(prepared.statement, query), state, queryTime, response);
Review comment:
Don't we need again before the loop `final String
possiblyObfuscatedQuery = listeners.size() > 0 ?
possiblyObfuscateQuery(statement, query) : query;`?
##########
File path: test/unit/org/apache/cassandra/audit/AuditLoggerAuthTest.java
##########
@@ -38,6 +38,8 @@
import org.apache.cassandra.config.OverrideConfigurationLoader;
import org.apache.cassandra.config.ParameterizedClass;
import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.cql3.PasswordObfuscator;
+import org.apache.cassandra.cql3.QueryEvents;
Review comment:
nit: unused import
##########
File path: test/unit/org/apache/cassandra/cql3/PasswordObfuscatorTest.java
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.cassandra.cql3;
+
+import org.junit.Ignore;
+import org.junit.Test;
+
+import static java.lang.String.format;
+import static org.junit.Assert.assertEquals;
+
+public class PasswordObfuscatorTest
+{
+ private static final PasswordObfuscator obfuscator = new
PasswordObfuscator();
+
+ @Test
+ public void testCreatRoleWithLoginPriorToPassword()
+ {
+ assertEquals(format("CREATE ROLE role1 WITH LOGIN = true AND PASSWORD
= '%s'", PasswordObfuscator.OBFUSCATION_TOKEN),
+ obfuscator.obfuscate("CREATE ROLE role1 WITH LOGIN = true
AND PASSWORD = '123'"));
+ }
+
+ @Test
+ public void testCreatRoleWithLoginAfterPassword()
+ {
+ assertEquals(format("CREATE ROLE role1 WITH password = '%s' AND LOGIN
= true", PasswordObfuscator.OBFUSCATION_TOKEN),
+ obfuscator.obfuscate("CREATE ROLE role1 WITH password =
'123' AND LOGIN = true"));
+ }
+
+ @Test
+ public void testCreateRoleWithoutPassword()
+ {
+ assertEquals("CREATE ROLE role1", obfuscator.obfuscate("CREATE ROLE
role1"));
+ }
+
+ @Test
+ public void testAlterRoleWithPassword()
+ {
+ assertEquals(format("ALTER ROLE role1 with PASSWORD = '%s'",
PasswordObfuscator.OBFUSCATION_TOKEN),
+ obfuscator.obfuscate("ALTER ROLE role1 with PASSWORD =
'123'"));
+ }
+
+ @Test
+ public void testAlterRoleWithPasswordNoSpace()
+ {
+ assertEquals(format("ALTER ROLE role1 with PASSWORD='%s'",
PasswordObfuscator.OBFUSCATION_TOKEN),
+ obfuscator.obfuscate("ALTER ROLE role1 with
PASSWORD='123'"));
+ }
+
+ @Test
+ public void testAlterRoleWithPasswordNoImmediateSpace()
+ {
+ assertEquals(format("ALTER ROLE role1 with PASSWORD= '%s'",
PasswordObfuscator.OBFUSCATION_TOKEN),
+ obfuscator.obfuscate("ALTER ROLE role1 with PASSWORD=
'123'"));
+ }
+
+ @Test
+ public void testAlterRoleWithoutPassword()
+ {
+ assertEquals("ALTER ROLE role1", obfuscator.obfuscate("ALTER ROLE
role1"));
+ }
+
+ @Test
+ public void testCreateUserWithPassword()
+ {
+ assertEquals(format("CREATE USER user1 with PASSWORD '%s'",
PasswordObfuscator.OBFUSCATION_TOKEN),
+ obfuscator.obfuscate("CREATE USER user1 with PASSWORD
'123'"));
+ }
+
+ @Test
+ public void testCreateUserWithoutPassword()
+ {
+ assertEquals("CREATE USER user1", obfuscator.obfuscate("CREATE USER
user1"));
+ }
+
+ @Test
+ public void testAlterUserWithPassword()
+ {
+ assertEquals(format("ALTER USER user1 with PASSWORD '%s'",
PasswordObfuscator.OBFUSCATION_TOKEN),
+ obfuscator.obfuscate("ALTER USER user1 with PASSWORD
'123'"));
+ }
+
+ @Test
+ public void testAlterUserWithPasswordMixedCase()
+ {
+ assertEquals(format("ALTER USER user1 with paSSwoRd '%s'",
PasswordObfuscator.OBFUSCATION_TOKEN),
+ obfuscator.obfuscate("ALTER USER user1 with paSSwoRd
'123'"));
+ }
+
+ @Test
+ public void testAlterUserWithPasswordWithNewLine()
+ {
+ assertEquals(format("ALTER USER user1 with PASSWORD\n'%s'",
PasswordObfuscator.OBFUSCATION_TOKEN),
+ obfuscator.obfuscate("ALTER USER user1 with
PASSWORD\n'123'"));
+ }
+
+ @Test
+ public void testPasswordWithNewLinesObfuscation()
+ {
+ assertEquals(String.format("CREATE USER user1 with PASSWORD '%s'",
PasswordObfuscator.OBFUSCATION_TOKEN),
+ obfuscator.obfuscate("CREATE USER user1 with PASSWORD
'a\nb'"));
+ }
+
+ @Test
+ public void testEmptyPasswordObfuscation()
+ {
+ assertEquals(String.format("CREATE USER user1 with PASSWORD '%s'",
PasswordObfuscator.OBFUSCATION_TOKEN),
+ obfuscator.obfuscate("CREATE USER user1 with PASSWORD
''"));
+ }
+
+ @Test
+ public void testPasswordWithSpaces()
+ {
+ assertEquals(String.format("CREATE USER user1 with PASSWORD '%s'",
PasswordObfuscator.OBFUSCATION_TOKEN),
+ obfuscator.obfuscate("CREATE USER user1 with PASSWORD 'p
a ss wor d'"));
+ }
+
+ @Test
+ public void testSimpleBatch()
+ {
+ assertEquals(format("BEGIN BATCH \n" +
+ " CREATE ROLE alice1 WITH PASSWORD = '%s' and
LOGIN = true; \n" +
+ "APPLY BATCH;",
+ PasswordObfuscator.OBFUSCATION_TOKEN),
+ obfuscator.obfuscate("BEGIN BATCH \n" +
+ " CREATE ROLE alice1 WITH
PASSWORD = 'alice123' and LOGIN = true; \n" +
+ "APPLY BATCH;"));
+ }
+
+ @Test
+ @Ignore
Review comment:
Also, small observation, If we add more` " CREATE ROLE alice1 WITH
PASSWORD = '%s' and LOGIN = true; \n" +`, it obfuscates only the last
occurrence... I need to check the code to see why.
##########
File path: src/java/org/apache/cassandra/cql3/QueryEvents.java
##########
@@ -235,6 +250,12 @@ public void notifyPrepareFailure(@Nullable CQLStatement
statement, String query,
}
}
+ private String possiblyObfuscateQuery(CQLStatement statement, String query)
+ {
+ // statement might be null as side-effect of failed parsing,
originates from QueryMessage#execute
Review comment:
```suggestion
// Statement might be null as side-effect of failed parsing,
originates from QueryMessage#execute
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]