You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by GitBox <gi...@apache.org> on 2022/03/18 08:03:33 UTC

[GitHub] [cassandra] smiklosovic commented on a change in pull request #1478: CASSANDRA-17334 Pre hashed passwords in CQL for trunk

smiklosovic commented on a change in pull request #1478:
URL: https://github.com/apache/cassandra/pull/1478#discussion_r829767362



##########
File path: src/java/org/apache/cassandra/tools/HashPassword.java
##########
@@ -0,0 +1,195 @@
+/*
+ * 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.tools;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.GnuParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.OptionGroup;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.lang3.StringUtils;
+import org.mindrot.jbcrypt.BCrypt;
+
+public class HashPassword
+{
+    private static final String LOGROUNDS_OPTION = "logrounds";
+    private static final String HELP_OPTION = "help";
+    private static final String ENV_VAR = "environment-var";
+    private static final String PLAIN = "plain";
+    private static final String INPUT = "input";
+
+    private static final int LOGROUNDS_DEFAULT = 10;
+
+    public static void main(String[] args)
+    {
+        try
+        {
+            Options options = getOptions();
+            CommandLine cmd = parseCommandLine(args, options);
+
+            String password = null;
+            if (cmd.hasOption(ENV_VAR))
+            {
+                password = System.getenv(cmd.getOptionValue(ENV_VAR));
+                if (password == null)
+                {
+                    System.err.println(String.format("Environment variable '%s' is undefined.", cmd.getOptionValue(ENV_VAR)));
+                    System.exit(1);
+                }
+            }
+            else if (cmd.hasOption(PLAIN))
+            {
+                password = cmd.getOptionValue(PLAIN);
+            }
+            else if (cmd.hasOption(INPUT))
+            {
+                String input = cmd.getOptionValue(INPUT);
+                byte[] fileInput = null;
+                if ("-".equals(input))
+                {
+                    ByteArrayOutputStream os = new ByteArrayOutputStream();
+                    int rd;
+                    while ((rd = System.in.read()) != -1)
+                        os.write(rd);
+                    fileInput = os.toByteArray();
+                }
+                else
+                {
+                    try
+                    {
+                        Path file = Paths.get(input);
+                        fileInput = Files.readAllBytes(file);
+                    }
+                    catch (IOException e)
+                    {
+                        System.err.println(String.format("Failed to read from '%s': %s",
+                                                         input, e));
+                        System.exit(1);
+                    }
+                }
+                password = new String(fileInput, StandardCharsets.UTF_8);
+            }
+            else
+            {
+                System.err.println(String.format("One of the options --%s, --%s or --%s must be used.",
+                                                 ENV_VAR, PLAIN, INPUT));
+                printUsage(options);
+                System.exit(1);
+            }
+
+            if (password.chars().anyMatch(i -> i < 32))
+                System.err.println("WARNING: The provided plain text password contains non-printable characters (ASCII<32).");
+
+            if (password.length() < 4)

Review comment:
       Why do we even check the length and why it has to be 4 and not 3? Or any number for that matter. What is this "4" based on? Good faith / practice? I do not think doing this check in this PR is a right thing to do. It should be probably done in a separate ticket with more discussion what we want to actually achieve.




-- 
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.

To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org