[ 
https://issues.apache.org/jira/browse/DRILL-7573?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17034759#comment-17034759
 ] 

ASF GitHub Bot commented on DRILL-7573:
---------------------------------------

dobesv commented on pull request #1977: DRILL-7573: Support htpasswd based 
authentication
URL: https://github.com/apache/drill/pull/1977#discussion_r377847858
 
 

 ##########
 File path: 
exec/java-exec/src/main/java/org/apache/drill/exec/rpc/user/security/HtpasswdFileUserAuthenticator.java
 ##########
 @@ -0,0 +1,147 @@
+/*
+ * 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.drill.exec.rpc.user.security;
+
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.commons.codec.digest.Md5Crypt;
+import org.apache.commons.io.Charsets;
+import org.apache.drill.common.config.DrillConfig;
+import org.apache.drill.exec.ExecConstants;
+import org.apache.drill.exec.exception.DrillbitStartupException;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.text.MessageFormat;
+import java.util.Base64;
+import java.util.HashMap;
+import java.util.Scanner;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Implementation of UserAuthenticator that reads passwords from an htpasswd
+ * formatted file.
+ *
+ * Currently supports MD5, SHA-1, and plaintext passwords.
+ *
+ * Use the htpasswd command line tool to create and modify htpasswd files.
+ *
+ * By default this loads the passwords from the path 
<code>/opt/drill/conf/htpasswd</code>.  You
+ * can change the path by setting 
<code>drill.exec.security.user.auth.htpasswd.path</code> in
+ * <code>drill-override.conf</code>.
+ *
+ * This is intended for situations where the list of users is relatively 
static, and you are running
+ * drill in a container so using pam is not convenient.
+ */
+@UserAuthenticatorTemplate(type = "htpasswd")
+public class HtpasswdFileUserAuthenticator implements UserAuthenticator {
+  private static final org.slf4j.Logger logger = 
org.slf4j.LoggerFactory.getLogger(Pam4jUserAuthenticator.class);
+  public static final String DEFAULT_HTPASSWD_AUTHENTICATOR_PATH = 
"/opt/drill/conf/htpasswd";
+
+  String path = DEFAULT_HTPASSWD_AUTHENTICATOR_PATH;
+  long lastModified;
+  HashMap<String, String> userToPassword;
+
+  @Override
+  public void setup(DrillConfig drillConfig) throws DrillbitStartupException {
+    if (drillConfig.hasPath(ExecConstants.HTPASSWD_AUTHENTICATOR_PATH)) {
+      path = drillConfig.getString(ExecConstants.HTPASSWD_AUTHENTICATOR_PATH);
+    }
+  }
+
+
+  /**
+   * Check password against hash read from the file
+   *
+   * @param password User provided password
+   * @param hash     Hash stored in the htpasswd file
+   * @return true if the password matched the hash
+   */
+  public static boolean checkPassword(String password, String hash) {
+    if (hash.startsWith("$apr1$")) {
+      return hash.equals(Md5Crypt.apr1Crypt(password, hash));
+    } else if (hash.startsWith("$1$")) {
+      return hash.equals(Md5Crypt.md5Crypt(password.getBytes(Charsets.UTF_8), 
hash));
+    } else if (hash.startsWith("{SHA}")) {
+      return 
hash.substring(5).equals(Base64.getEncoder().encodeToString(DigestUtils.sha1(password)));
+    } else if (hash.startsWith("$2y$")) {
+      // bcrypt not supported currently
+      return false;
+    } else {
+      return hash.equals(password);
+    }
+  }
+
+  /**
+   * Validate the given username and password against the password file
+   *
+   * @param username Username provided
+   * @param password Password provided
+   * @throws UserAuthenticationException If the username and password could 
not be validated
+   */
+  @Override
+  public void authenticate(String username, String password) throws 
UserAuthenticationException {
+    read();
+    String hash = this.userToPassword.get(username);
+    logger.error("Testing " + hash + " against password " + password);
+    boolean credentialsAccepted = (hash != null && 
this.checkPassword(password, hash));
+    if (!credentialsAccepted) {
+      throw new UserAuthenticationException(String.format("htpasswd auth 
failed for user '%s'",
+        username));
+    }
+  }
+
+  /**
+   * Read the password file into the map, if the file has changed since we 
last read it
+   */
+  protected synchronized void read() {
+    File file = new File(path);
+    if (file.exists() && (userToPassword == null || file.lastModified() != 
lastModified)) {
 
 Review comment:
   This logging is already present in the `else` clause below.
   
 
----------------------------------------------------------------
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:
us...@infra.apache.org


> Support text file for user authentication instead of using PAM
> --------------------------------------------------------------
>
>                 Key: DRILL-7573
>                 URL: https://issues.apache.org/jira/browse/DRILL-7573
>             Project: Apache Drill
>          Issue Type: Improvement
>          Components:  Server, Web Server
>    Affects Versions: 1.17.0
>            Reporter: Dobes Vandermeer
>            Priority: Major
>
> Currently plain login using PAM as its user database.  However, in a 
> containerized or server environment the passwd file is generally kept static, 
> so some other mechanism for managing users is preferred.  Also, pam does not 
> by default come with an easy to way to check passwords other than via the 
> passwd/shadow files.
>  It would be great if there was another authentication method included in 
> drill that was easier to use in a containerized environment.
> Reading the usernames and password from a specific file would probably be the 
> simplest mechanism.
>  
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to