Jackie-Jiang commented on a change in pull request #6552:
URL: https://github.com/apache/incubator-pinot/pull/6552#discussion_r573145470



##########
File path: 
pinot-broker/src/main/java/org/apache/pinot/broker/broker/BasicAuthAccessControlFactory.java
##########
@@ -0,0 +1,159 @@
+/**
+ * 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.pinot.broker.broker;
+
+import com.google.common.base.Preconditions;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.broker.api.AccessControl;
+import org.apache.pinot.broker.api.HttpRequesterIdentity;
+import org.apache.pinot.broker.api.RequesterIdentity;
+import org.apache.pinot.common.request.BrokerRequest;
+import org.apache.pinot.spi.env.PinotConfiguration;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+
+/**
+ * Basic Authentication based on http headers. Configured via the 
"pinot.broker.access.control" family of properties.
+ *
+ * <pre>
+ *     Example:
+ *     pinot.broker.access.control.principals=admin123,user456
+ *     pinot.broker.access.control.principals.admin123.password=verysecret
+ *     pinot.broker.access.control.principals.user456.password=kindasecret
+ *     
pinot.broker.access.control.principals.user456.tables=stuff,lessImportantStuff
+ * </pre>
+ */
+public class BasicAuthAccessControlFactory extends AccessControlFactory {
+  private static final String PRINCIPALS = "principals";
+  private static final String PASSWORD = "password";
+  private static final String TABLES = "tables";
+  private static final String TABLES_ALL = "*";
+
+  private static final String HEADER_AUTHORIZATION = "authorization";
+
+  private AccessControl _accessControl;
+
+  public BasicAuthAccessControlFactory() {
+    // left blank
+  }
+
+  public void init(PinotConfiguration configuration) {
+    String principalNames = configuration.getProperty(PRINCIPALS);
+    Preconditions.checkArgument(StringUtils.isNotBlank(principalNames), "must 
provide principals");
+
+    List<BasicAuthPrincipal> principals = 
Arrays.stream(principalNames.split(",")).map(rawName -> {
+      String name = rawName.trim();
+      Preconditions.checkArgument(StringUtils.isNotBlank(name), "%s is not a 
valid name", name);
+
+      String password = configuration.getProperty(String.format("%s.%s.%s", 
PRINCIPALS, name, PASSWORD));
+      Preconditions.checkArgument(StringUtils.isNotBlank(password), "must 
provide a password for %s", name);
+
+      Set<String> tables = new HashSet<>();
+      String tableNames = configuration.getProperty(String.format("%s.%s.%s", 
PRINCIPALS, name, TABLES));
+      if (StringUtils.isNotBlank(tableNames) && 
!TABLES_ALL.equals(tableNames)) {
+        tables.addAll(Arrays.asList(tableNames.split(",")));
+      }
+
+      return new BasicAuthPrincipal(name, toToken(name, password), tables);
+    }).collect(Collectors.toList());
+
+    _accessControl = new BasicAuthAccessControl(principals);
+  }
+
+  public AccessControl create() {
+    return _accessControl;
+  }
+
+  /**
+   * Access Control using header-based basic http authentication
+   */
+  private static class BasicAuthAccessControl implements AccessControl {
+    private final Map<String, BasicAuthPrincipal> _principals;
+
+    public BasicAuthAccessControl(Collection<BasicAuthPrincipal> principals) {
+      this._principals = 
principals.stream().collect(Collectors.toMap(BasicAuthPrincipal::getToken, p -> 
p));
+    }
+
+    @Override
+    public boolean hasAccess(RequesterIdentity requesterIdentity, 
BrokerRequest brokerRequest) {
+      Preconditions.checkArgument(requesterIdentity instanceof 
HttpRequesterIdentity, "HttpRequesterIdentity required");
+      HttpRequesterIdentity identity = (HttpRequesterIdentity) 
requesterIdentity;
+
+      Collection<String> tokens = 
identity.getHttpHeaders().get(HEADER_AUTHORIZATION);
+      Optional<BasicAuthPrincipal> principalOpt = 
tokens.stream().map(BasicAuthAccessControlFactory::normalizeToken)
+              .map(_principals::get).filter(Objects::nonNull).findFirst();
+
+      if (!principalOpt.isPresent()) {
+        // no matching token? reject
+        return false;
+      }
+
+      BasicAuthPrincipal principal = principalOpt.get();
+      if (principal.getTables().isEmpty()
+              || !brokerRequest.isSetQuerySource()
+              || !brokerRequest.getQuerySource().isSetTableName()) {
+        // no table restrictions? accept
+        return true;
+      }
+
+      return 
principal.getTables().contains(brokerRequest.getQuerySource().getTableName());
+    }
+  }
+
+  /**
+   * Container object for basic auth principal
+   */
+  private static class BasicAuthPrincipal {
+    private final String _name;
+    private final String _token;
+    private final Set<String> _tables;
+
+    public BasicAuthPrincipal(String name, String token, Set<String> tables) {
+      this._name = name;
+      this._token = token;
+      this._tables = tables;
+    }
+
+    public String getName() {
+      return _name;
+    }
+
+    public Set<String> getTables() {
+      return _tables;
+    }
+
+    public String getToken() {
+      return _token;
+    }
+  }
+
+  private static String toToken(String name, String password) {
+    String identifier = String.format("%s:%s", name, password);
+    return normalizeToken(String.format("Basic %s", 
Base64.getEncoder().encodeToString(identifier.getBytes())));
+  }
+
+  private static String normalizeToken(String token) {
+    if (token == null) {

Review comment:
       If it returns `null`, will `.map(_principals::get)` on line 103 throw 
NPE?
   If we allow `null` here, suggest annotate token with `@Nullable`

##########
File path: 
pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/PinotQueryResource.java
##########
@@ -29,18 +29,22 @@
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
+import java.util.HashMap;

Review comment:
       Some imports are unused?

##########
File path: 
pinot-broker/src/main/java/org/apache/pinot/broker/broker/BasicAuthAccessControlFactory.java
##########
@@ -0,0 +1,168 @@
+/**
+ * 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.pinot.broker.broker;
+
+import com.google.common.base.Preconditions;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.broker.api.AccessControl;
+import org.apache.pinot.broker.api.HttpRequesterIdentity;
+import org.apache.pinot.broker.api.RequesterIdentity;
+import org.apache.pinot.common.request.BrokerRequest;
+import org.apache.pinot.spi.env.PinotConfiguration;
+
+import java.nio.charset.StandardCharsets;

Review comment:
       Can you follow the instructions here to set up the IDE and auto-reformat 
the code: 
https://docs.pinot.apache.org/developers/developers-and-contributors/code-setup




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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to