sumanth-pasupuleti commented on a change in pull request #1043: URL: https://github.com/apache/cassandra/pull/1043#discussion_r649706756
########## File path: src/java/org/apache/cassandra/cql3/PasswordObfuscator.java ########## @@ -0,0 +1,48 @@ +/* + * 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 java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Obfuscates passwords in a given string + */ +public class PasswordObfuscator +{ + public static final String OBFUSCATION_TOKEN = "*******"; + private static final String PASSWORD_TOKEN = "password"; + + private static final int PATTERN_FLAGS = Pattern.CASE_INSENSITIVE | Pattern.DOTALL; + + private static final Pattern PASSWORD_PATTERN = Pattern.compile(".*password\\s*=?\\s*'(?<password>.*)'.*", PATTERN_FLAGS); Review comment: Replacing regex with a simpler approach (initial version of the patch) of obfuscating everything after password keyword, similar to what postgres audit does ########## 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: We do, thanks for catching this. fixed. ########## 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: fixed ########## 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: This is now resolved with the simpler logic of obfuscating everything after the first password occurrence ########## 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: fixed. ########## 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'")); Review comment: resolving since we moved to a simpler logic from using regex -- 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]

