This is an automated email from the ASF dual-hosted git repository.

jackietien pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 1c2c42845e7 rest service adjustment permission authentication (#11312)
1c2c42845e7 is described below

commit 1c2c42845e77ca27ac9c10d54bee53193d520caf
Author: CloudWise-Lukemiao 
<[email protected]>
AuthorDate: Wed Oct 25 10:55:55 2023 +0800

    rest service adjustment permission authentication (#11312)
---
 .../protocol/rest/filter/AuthorizationFilter.java  | 33 ++++++++-
 .../rest/v1/impl/GrafanaApiServiceImpl.java        |  6 +-
 .../protocol/rest/v1/impl/RestApiServiceImpl.java  |  9 ++-
 .../rest/v2/impl/GrafanaApiServiceImpl.java        |  6 +-
 .../protocol/rest/v2/impl/RestApiServiceImpl.java  |  8 +--
 .../db/protocol/session/RestClientSession.java     | 79 ++++++++++++++++++++++
 .../thrift-datanode/src/main/thrift/client.thrift  |  1 +
 7 files changed, 125 insertions(+), 17 deletions(-)

diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/filter/AuthorizationFilter.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/filter/AuthorizationFilter.java
index 48eda6a6c28..96dbca17425 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/filter/AuthorizationFilter.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/filter/AuthorizationFilter.java
@@ -19,36 +19,46 @@ package org.apache.iotdb.db.protocol.rest.filter;
 
 import org.apache.iotdb.common.rpc.thrift.TSStatus;
 import org.apache.iotdb.commons.auth.AuthException;
+import org.apache.iotdb.commons.conf.IoTDBConstant;
 import org.apache.iotdb.db.auth.AuthorityChecker;
 import org.apache.iotdb.db.conf.rest.IoTDBRestServiceConfig;
 import org.apache.iotdb.db.conf.rest.IoTDBRestServiceDescriptor;
 import org.apache.iotdb.db.protocol.rest.model.ExecutionStatus;
+import org.apache.iotdb.db.protocol.session.RestClientSession;
+import org.apache.iotdb.db.protocol.session.SessionManager;
 import org.apache.iotdb.rpc.TSStatusCode;
 
 import javax.servlet.annotation.WebFilter;
 import javax.ws.rs.container.ContainerRequestContext;
 import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.container.ContainerResponseContext;
+import javax.ws.rs.container.ContainerResponseFilter;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.ext.Provider;
 
 import java.io.IOException;
+import java.time.ZoneId;
 import java.util.Base64;
+import java.util.UUID;
 
 @WebFilter("/*")
 @Provider
-public class AuthorizationFilter implements ContainerRequestFilter {
+public class AuthorizationFilter implements ContainerRequestFilter, 
ContainerResponseFilter {
 
   private final UserCache userCache = UserCache.getInstance();
   IoTDBRestServiceConfig config = 
IoTDBRestServiceDescriptor.getInstance().getConfig();
 
+  private static final SessionManager SESSION_MANAGER = 
SessionManager.getInstance();
+
   public AuthorizationFilter() throws AuthException {
     // do nothing
   }
 
   @Override
   public void filter(ContainerRequestContext containerRequestContext) throws 
IOException {
+
     if ("OPTIONS".equals(containerRequestContext.getMethod())
         || "ping".equals(containerRequestContext.getUriInfo().getPath())
         || (config.isEnableSwagger()
@@ -84,7 +94,17 @@ public class AuthorizationFilter implements 
ContainerRequestFilter {
         userCache.setUser(authorizationHeader, user);
       }
     }
-
+    String sessionid = UUID.randomUUID().toString();
+    if (SESSION_MANAGER.getCurrSession() == null) {
+      RestClientSession restClientSession = new RestClientSession(sessionid);
+      restClientSession.setUsername(user.getUsername());
+      SESSION_MANAGER.registerSession(restClientSession);
+      SESSION_MANAGER.supplySession(
+          SESSION_MANAGER.getCurrSession(),
+          user.getUsername(),
+          ZoneId.systemDefault().getId(),
+          IoTDBConstant.ClientVersion.V_1_0);
+    }
     BasicSecurityContext basicSecurityContext =
         new BasicSecurityContext(
             user, 
IoTDBRestServiceDescriptor.getInstance().getConfig().isEnableHttps());
@@ -130,4 +150,13 @@ public class AuthorizationFilter implements 
ContainerRequestFilter {
     }
     return user;
   }
+
+  @Override
+  public void filter(
+      ContainerRequestContext requestContext, ContainerResponseContext 
responseContext)
+      throws IOException {
+    if (SESSION_MANAGER.getSessionInfo(SESSION_MANAGER.getCurrSession()) != 
null) {
+      SESSION_MANAGER.removeCurrSession();
+    }
+  }
 }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/GrafanaApiServiceImpl.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/GrafanaApiServiceImpl.java
index 882dec8b86e..e3909a7873c 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/GrafanaApiServiceImpl.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/GrafanaApiServiceImpl.java
@@ -113,7 +113,7 @@ public class GrafanaApiServiceImpl extends 
GrafanaApiService {
           COORDINATOR.execute(
               statement,
               queryId,
-              null,
+              SESSION_MANAGER.getSessionInfo(SESSION_MANAGER.getCurrSession()),
               sql.getSql(),
               partitionFetcher,
               schemaFetcher,
@@ -180,7 +180,7 @@ public class GrafanaApiServiceImpl extends 
GrafanaApiService {
           COORDINATOR.execute(
               statement,
               queryId,
-              null,
+              SESSION_MANAGER.getSessionInfo(SESSION_MANAGER.getCurrSession()),
               sql,
               partitionFetcher,
               schemaFetcher,
@@ -241,7 +241,7 @@ public class GrafanaApiServiceImpl extends 
GrafanaApiService {
             COORDINATOR.execute(
                 statement,
                 queryId,
-                null,
+                
SESSION_MANAGER.getSessionInfo(SESSION_MANAGER.getCurrSession()),
                 sql,
                 partitionFetcher,
                 schemaFetcher,
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
index e9cd7262f48..7563a1c1d5e 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
@@ -102,7 +102,7 @@ public class RestApiServiceImpl extends RestApiService {
           COORDINATOR.execute(
               statement,
               queryId,
-              null,
+              SESSION_MANAGER.getSessionInfo(SESSION_MANAGER.getCurrSession()),
               sql.getSql(),
               partitionFetcher,
               schemaFetcher,
@@ -150,14 +150,13 @@ public class RestApiServiceImpl extends RestApiService {
       if (response != null) {
         return response;
       }
-
       queryId = SESSION_MANAGER.requestQueryId();
       // create and cache dataset
       ExecutionResult result =
           COORDINATOR.execute(
               statement,
               queryId,
-              null,
+              SESSION_MANAGER.getSessionInfo(SESSION_MANAGER.getCurrSession()),
               sql.getSql(),
               partitionFetcher,
               schemaFetcher,
@@ -206,7 +205,7 @@ public class RestApiServiceImpl extends RestApiService {
           COORDINATOR.execute(
               insertRowsStatement,
               SESSION_MANAGER.requestQueryId(),
-              null,
+              SESSION_MANAGER.getSessionInfo(SESSION_MANAGER.getCurrSession()),
               "",
               partitionFetcher,
               schemaFetcher,
@@ -262,7 +261,7 @@ public class RestApiServiceImpl extends RestApiService {
           COORDINATOR.execute(
               insertTabletStatement,
               queryId,
-              null,
+              SESSION_MANAGER.getSessionInfo(SESSION_MANAGER.getCurrSession()),
               "",
               partitionFetcher,
               schemaFetcher,
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/GrafanaApiServiceImpl.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/GrafanaApiServiceImpl.java
index a7eb24066e3..b093798b228 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/GrafanaApiServiceImpl.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/GrafanaApiServiceImpl.java
@@ -113,7 +113,7 @@ public class GrafanaApiServiceImpl extends 
GrafanaApiService {
           COORDINATOR.execute(
               statement,
               queryId,
-              null,
+              SESSION_MANAGER.getSessionInfo(SESSION_MANAGER.getCurrSession()),
               sql.getSql(),
               partitionFetcher,
               schemaFetcher,
@@ -180,7 +180,7 @@ public class GrafanaApiServiceImpl extends 
GrafanaApiService {
           COORDINATOR.execute(
               statement,
               queryId,
-              null,
+              SESSION_MANAGER.getSessionInfo(SESSION_MANAGER.getCurrSession()),
               sql,
               partitionFetcher,
               schemaFetcher,
@@ -241,7 +241,7 @@ public class GrafanaApiServiceImpl extends 
GrafanaApiService {
             COORDINATOR.execute(
                 statement,
                 queryId,
-                null,
+                
SESSION_MANAGER.getSessionInfo(SESSION_MANAGER.getCurrSession()),
                 sql,
                 partitionFetcher,
                 schemaFetcher,
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
index 643e3f91036..ff34db35324 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
@@ -102,7 +102,7 @@ public class RestApiServiceImpl extends RestApiService {
           COORDINATOR.execute(
               statement,
               queryId,
-              null,
+              SESSION_MANAGER.getSessionInfo(SESSION_MANAGER.getCurrSession()),
               sql.getSql(),
               partitionFetcher,
               schemaFetcher,
@@ -157,7 +157,7 @@ public class RestApiServiceImpl extends RestApiService {
           COORDINATOR.execute(
               statement,
               queryId,
-              null,
+              SESSION_MANAGER.getSessionInfo(SESSION_MANAGER.getCurrSession()),
               sql.getSql(),
               partitionFetcher,
               schemaFetcher,
@@ -206,7 +206,7 @@ public class RestApiServiceImpl extends RestApiService {
           COORDINATOR.execute(
               insertRowsStatement,
               SESSION_MANAGER.requestQueryId(),
-              null,
+              SESSION_MANAGER.getSessionInfo(SESSION_MANAGER.getCurrSession()),
               "",
               partitionFetcher,
               schemaFetcher,
@@ -262,7 +262,7 @@ public class RestApiServiceImpl extends RestApiService {
           COORDINATOR.execute(
               insertTabletStatement,
               SESSION_MANAGER.requestQueryId(),
-              null,
+              SESSION_MANAGER.getSessionInfo(SESSION_MANAGER.getCurrSession()),
               "",
               partitionFetcher,
               schemaFetcher,
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/session/RestClientSession.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/session/RestClientSession.java
new file mode 100644
index 00000000000..30ca7509d60
--- /dev/null
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/session/RestClientSession.java
@@ -0,0 +1,79 @@
+/*
+ * 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.iotdb.db.protocol.session;
+
+import org.apache.iotdb.service.rpc.thrift.TSConnectionType;
+
+import java.util.Collections;
+import java.util.Set;
+
+public class RestClientSession extends IClientSession {
+
+  private final String clientID;
+
+  public RestClientSession(String clientID) {
+    this.clientID = clientID;
+  }
+
+  @Override
+  public String getClientAddress() {
+    return clientID;
+  }
+
+  @Override
+  int getClientPort() {
+    return 0;
+  }
+
+  @Override
+  TSConnectionType getConnectionType() {
+    return TSConnectionType.REST_BASED;
+  }
+
+  @Override
+  String getConnectionId() {
+    return clientID;
+  }
+
+  @Override
+  public Iterable<Long> getStatementIds() {
+    return Collections.emptySet();
+  }
+
+  @Override
+  public void addStatementId(long statementId) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public Set<Long> removeStatementId(long statementId) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public void addQueryId(Long statementId, long queryId) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public void removeQueryId(Long statementId, Long queryId) {
+    throw new UnsupportedOperationException();
+  }
+}
diff --git a/iotdb-protocol/thrift-datanode/src/main/thrift/client.thrift 
b/iotdb-protocol/thrift-datanode/src/main/thrift/client.thrift
index bcfd8853a96..2b2f5585726 100644
--- a/iotdb-protocol/thrift-datanode/src/main/thrift/client.thrift
+++ b/iotdb-protocol/thrift-datanode/src/main/thrift/client.thrift
@@ -501,6 +501,7 @@ enum TSConnectionType {
   THRIFT_BASED
   MQTT_BASED
   INTERNAL
+  REST_BASED
 }
 
 struct TSConnectionInfo {

Reply via email to