paul-rogers commented on a change in pull request #1977: DRILL-7573: Support htpasswd based authentication URL: https://github.com/apache/drill/pull/1977#discussion_r377823342
########## 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) { Review comment: Nit: best to use a name that flows in an if-statement. Maybe `isPasswordValid`. So: ``` if (isPasswordValid(...)) { // Welcome! } else { // You're outta here! } ``` ---------------------------------------------------------------- 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] With regards, Apache Git Services
