kezhenxu94 commented on a change in pull request #5376: URL: https://github.com/apache/skywalking/pull/5376#discussion_r480009375
########## File path: oap-server/server-library/library-server/src/main/java/org/apache/skywalking/oap/server/library/server/grpc/ssl/DynamicSslContext.java ########## @@ -0,0 +1,134 @@ +/* + * 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.skywalking.oap.server.library.server.grpc.ssl; + +import io.grpc.netty.GrpcSslContexts; +import io.netty.buffer.ByteBufAllocator; +import io.netty.handler.ssl.ApplicationProtocolNegotiator; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslContextBuilder; +import io.netty.handler.ssl.SslProvider; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Paths; +import java.security.GeneralSecurityException; +import java.util.List; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLException; +import javax.net.ssl.SSLSessionContext; +import org.apache.skywalking.oap.server.library.util.MultipleFilesChangeMonitor; + +/** + * Load SslContext dynamically. + */ +public class DynamicSslContext extends SslContext { + private final MultipleFilesChangeMonitor monitor; + private volatile SslContext ctx; + + public static DynamicSslContext forServer(final String privateKeyFile, final String certChainFile) { + return new DynamicSslContext(privateKeyFile, certChainFile); + } + + public static DynamicSslContext forClient(final String caFile) { + return new DynamicSslContext(caFile); + } + + private DynamicSslContext(final String privateKeyFile, final String certChainFile) { + updateContext(privateKeyFile, certChainFile); + monitor = new MultipleFilesChangeMonitor( + 10, + readableContents -> updateContext(privateKeyFile, certChainFile), + certChainFile, + privateKeyFile); + } + + private DynamicSslContext(final String caFile) { + updateContext(caFile); + monitor = new MultipleFilesChangeMonitor( + 10, + readableContents -> updateContext(caFile), + caFile); + } + + private void updateContext(String caFile) { + try { + GrpcSslContexts.forClient().trustManager(Paths.get(caFile).toFile()).build(); Review comment: The returned value is just ignored? If that's intentionally, do we need a null check in the following uses of `ctx`? ---------------------------------------------------------------- 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]
