From 4698681e7cc304d97e42f3471c6f82119e307ddb Mon Sep 17 00:00:00 2001 From: zhyyu Date: Fri, 18 Jun 2021 16:09:55 +0800 Subject: [PATCH] Support authentication for log report channel. (#7131) --- CHANGES.md | 1 + .../log/GRPCLogReportServiceClient.java | 36 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 0273b9658..b656961c2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,7 @@ Release Notes. #### Java Agent * Supports modifying span attributes in async mode. * Agent supports the collection of JVM arguments and jar dependency information. +* [Temporary] Support authentication for log report channel. This feature and grpc channel is going to be removed after Satellite 0.2.0 release. #### OAP-Backend * Disable Spring sleuth meter analyzer by default. diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logging-common/src/main/java/org/apache/skywalking/apm/toolkit/logging/common/log/GRPCLogReportServiceClient.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logging-common/src/main/java/org/apache/skywalking/apm/toolkit/logging/common/log/GRPCLogReportServiceClient.java index 8f374063a..94259852d 100644 --- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-logging-common/src/main/java/org/apache/skywalking/apm/toolkit/logging/common/log/GRPCLogReportServiceClient.java +++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-logging-common/src/main/java/org/apache/skywalking/apm/toolkit/logging/common/log/GRPCLogReportServiceClient.java @@ -18,8 +18,16 @@ package org.apache.skywalking.apm.toolkit.logging.common.log; +import io.grpc.CallOptions; +import io.grpc.Channel; +import io.grpc.ClientCall; +import io.grpc.ClientInterceptor; +import io.grpc.ClientInterceptors; +import io.grpc.ForwardingClientCall; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; +import io.grpc.Metadata; +import io.grpc.MethodDescriptor; import io.grpc.StatusRuntimeException; import io.grpc.stub.StreamObserver; import java.util.List; @@ -38,6 +46,7 @@ import org.apache.skywalking.apm.commons.datacarrier.buffer.BufferStrategy; import org.apache.skywalking.apm.network.common.v3.Commands; import org.apache.skywalking.apm.network.logging.v3.LogData; import org.apache.skywalking.apm.network.logging.v3.LogReportServiceGrpc; +import org.apache.skywalking.apm.util.StringUtil; /** * Report log to server by grpc @@ -55,6 +64,8 @@ public class GRPCLogReportServiceClient extends LogReportServiceClient { private AtomicBoolean disconnected = new AtomicBoolean(false); + private static final Metadata.Key AUTH_HEAD_HEADER_NAME = Metadata.Key.of("Authentication", Metadata.ASCII_STRING_MARSHALLER); + @Override public void boot() throws Throwable { carrier = new DataCarrier<>("gRPC-log", "gRPC-log", @@ -70,7 +81,9 @@ public class GRPCLogReportServiceClient extends LogReportServiceClient { ) .usePlaintext() .build(); - asyncStub = LogReportServiceGrpc.newStub(channel) + + Channel decoratedChannel = decorateLogChannelWithAuthentication(channel); + asyncStub = LogReportServiceGrpc.newStub(decoratedChannel) .withMaxOutboundMessageSize( ToolkitConfig.Plugin.Toolkit.Log.GRPC.Reporter.MAX_MESSAGE_SIZE); } @@ -144,4 +157,25 @@ public class GRPCLogReportServiceClient extends LogReportServiceClient { waitStatus.wait4Finish(); } } + + private Channel decorateLogChannelWithAuthentication(Channel channel) { + if (StringUtil.isEmpty(Config.Agent.AUTHENTICATION)) { + return channel; + } + + return ClientInterceptors.intercept(channel, new ClientInterceptor() { + @Override + public ClientCall interceptCall(MethodDescriptor method, + CallOptions options, Channel channel) { + return new ForwardingClientCall.SimpleForwardingClientCall(channel.newCall(method, options)) { + @Override + public void start(Listener responseListener, Metadata headers) { + headers.put(AUTH_HEAD_HEADER_NAME, Config.Agent.AUTHENTICATION); + super.start(responseListener, headers); + } + }; + } + }); + } + }