You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/08/27 02:42:47 UTC

[GitHub] [dubbo] guohao commented on a change in pull request #8585: Add Header Filter for Triple Protocol

guohao commented on a change in pull request #8585:
URL: https://github.com/apache/dubbo/pull/8585#discussion_r697112689



##########
File path: dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/TokenHeaderFilter.java
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.dubbo.rpc.filter;
+
+import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.common.utils.ConfigUtils;
+import org.apache.dubbo.rpc.HeaderFilter;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.RpcContext;
+import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.RpcInvocation;
+
+import java.util.Map;
+
+import static org.apache.dubbo.rpc.Constants.TOKEN_KEY;
+
+@Activate
+public class TokenHeaderFilter implements HeaderFilter {
+    @Override
+    public RpcInvocation invoke(Invoker<?> invoker, RpcInvocation invocation) throws RpcException {
+        String token = invoker.getUrl().getParameter(TOKEN_KEY);
+        if (ConfigUtils.isNotEmpty(token)) {
+            Class<?> serviceType = invoker.getInterface();
+            Map<String, Object> attachments = invocation.getObjectAttachments();
+            String remoteToken = (attachments == null ? null : (String) attachments.get(TOKEN_KEY));
+            if (!token.equals(remoteToken)) {
+                throw new RpcException("Forbid invoke remote service " + serviceType + " method " + invocation.getMethodName() +

Review comment:
       Should an explicit exception be throwed to set `grpc-status` as `Unauthorized`?

##########
File path: dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/ServerStream.java
##########
@@ -83,27 +84,34 @@ public void onMetadata(Metadata metadata, boolean endStream, OperationHandler ha
                 subscribe((StreamObserver<Object>) result.getValue());
             } catch (Throwable t) {
                 transportError(GrpcStatus.fromCode(GrpcStatus.Code.INTERNAL)
-                        .withDescription("Failed to create server's observer"));
+                    .withDescription("Failed to create server's observer"));
             }
         }
 
         @Override
         public void onData(byte[] in, boolean endStream, OperationHandler handler) {
             try {
-                final Object[] arguments = deserializeRequest(in);
-                if (arguments != null) {
-                    if (getMethodDescriptor().getRpcType() == MethodDescriptor.RpcType.SERVER_STREAM) {
-                        final RpcInvocation inv = buildInvocation(getHeaders());
+                if (getMethodDescriptor().getRpcType() == MethodDescriptor.RpcType.SERVER_STREAM) {
+                    RpcInvocation inv = buildInvocation(getHeaders());
+                    for (HeaderFilter headerFilter : getHeaderFilters()) {
+                        inv = headerFilter.invoke(getInvoker(), inv);
+                    }
+                    final Object[] arguments = deserializeRequest(in);
+                    if (arguments != null) {
                         inv.setArguments(new Object[]{arguments[0], asStreamObserver()});
                         getInvoker().invoke(inv);
-                    } else {
+                    }
+                } else {
+                    final Object[] arguments = deserializeRequest(in);

Review comment:
       Bi-stream will create inocation at `onMetadata` method, filters should also be added there




-- 
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: notifications-unsubscribe@dubbo.apache.org

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



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org