forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refresh Certs every X minutes (apache#3568)
- Loading branch information
Jai Asher
authored
Feb 18, 2019
1 parent
384fca3
commit a0f7661
Showing
14 changed files
with
404 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
pulsar-common/src/main/java/org/apache/pulsar/common/util/ClientSslContextRefresher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* 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.pulsar.common.util; | ||
|
||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
import java.security.cert.X509Certificate; | ||
|
||
import org.apache.pulsar.client.api.AuthenticationDataProvider; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.netty.handler.ssl.SslContext; | ||
|
||
public class ClientSslContextRefresher { | ||
private volatile SslContext sslContext; | ||
private boolean tlsAllowInsecureConnection; | ||
private String tlsTrustCertsFilePath; | ||
private AuthenticationDataProvider authData; | ||
|
||
public ClientSslContextRefresher(boolean allowInsecure, String trustCertsFilePath, | ||
AuthenticationDataProvider authData) throws IOException, GeneralSecurityException { | ||
this.tlsAllowInsecureConnection = allowInsecure; | ||
this.tlsTrustCertsFilePath = trustCertsFilePath; | ||
this.authData = authData; | ||
|
||
if (authData != null && authData.hasDataForTls()) { | ||
this.sslContext = SecurityUtility.createNettySslContextForClient(this.tlsAllowInsecureConnection, | ||
this.tlsTrustCertsFilePath, (X509Certificate[]) authData.getTlsCertificates(), | ||
authData.getTlsPrivateKey()); | ||
} else { | ||
this.sslContext = SecurityUtility.createNettySslContextForClient(this.tlsAllowInsecureConnection, | ||
this.tlsTrustCertsFilePath); | ||
} | ||
} | ||
|
||
public SslContext get() { | ||
if (authData != null && authData.hasDataForTls()) { | ||
try { | ||
this.sslContext = SecurityUtility.createNettySslContextForClient(this.tlsAllowInsecureConnection, | ||
this.tlsTrustCertsFilePath, (X509Certificate[]) authData.getTlsCertificates(), | ||
authData.getTlsPrivateKey()); | ||
} catch (GeneralSecurityException | IOException e) { | ||
LOG.error("Exception occured while trying to refresh sslContext: ", e); | ||
} | ||
|
||
} | ||
return sslContext; | ||
} | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ClientSslContextRefresher.class); | ||
} |
65 changes: 65 additions & 0 deletions
65
pulsar-common/src/main/java/org/apache/pulsar/common/util/FileModifiedTimeUpdater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* 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.pulsar.common.util; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.attribute.FileTime; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import lombok.Getter; | ||
|
||
public class FileModifiedTimeUpdater { | ||
@Getter | ||
String fileName; | ||
@Getter | ||
FileTime lastModifiedTime; | ||
|
||
public FileModifiedTimeUpdater(String fileName) { | ||
this.fileName = fileName; | ||
this.lastModifiedTime = updateLastModifiedTime(); | ||
} | ||
|
||
private FileTime updateLastModifiedTime() { | ||
if (fileName != null) { | ||
Path p = Paths.get(fileName); | ||
try { | ||
return Files.getLastModifiedTime(p); | ||
} catch (IOException e) { | ||
LOG.error("Unable to fetch lastModified time for file {}: ", fileName, e); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public boolean checkAndRefresh() { | ||
FileTime newLastModifiedTime = updateLastModifiedTime(); | ||
if (newLastModifiedTime != null && !newLastModifiedTime.equals(lastModifiedTime)) { | ||
this.lastModifiedTime = newLastModifiedTime; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(FileModifiedTimeUpdater.class); | ||
} |
87 changes: 87 additions & 0 deletions
87
pulsar-common/src/main/java/org/apache/pulsar/common/util/ServerSslContextRefresher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* 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.pulsar.common.util; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.net.ssl.SSLException; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.netty.handler.ssl.SslContext; | ||
|
||
public class ServerSslContextRefresher { | ||
private final boolean tlsAllowInsecureConnection; | ||
private final FileModifiedTimeUpdater tlsTrustCertsFilePath, tlsCertificateFilePath, tlsKeyFilePath; | ||
private final Set<String> tlsCiphers; | ||
private final Set<String> tlsProtocols; | ||
private final boolean tlsRequireTrustedClientCertOnConnect; | ||
private final long delayInMins; | ||
private long nextRefreshTimeInMins; | ||
private volatile SslContext sslContext; | ||
|
||
public ServerSslContextRefresher(boolean allowInsecure, String trustCertsFilePath, String certificateFilePath, | ||
String keyFilePath, Set<String> ciphers, Set<String> protocols, boolean requireTrustedClientCertOnConnect, | ||
long delayInMins) throws SSLException, FileNotFoundException, GeneralSecurityException, IOException { | ||
this.tlsAllowInsecureConnection = allowInsecure; | ||
this.tlsTrustCertsFilePath = new FileModifiedTimeUpdater(trustCertsFilePath); | ||
this.tlsCertificateFilePath = new FileModifiedTimeUpdater(certificateFilePath); | ||
this.tlsKeyFilePath = new FileModifiedTimeUpdater(keyFilePath); | ||
this.tlsCiphers = ciphers; | ||
this.tlsProtocols = protocols; | ||
this.tlsRequireTrustedClientCertOnConnect = requireTrustedClientCertOnConnect; | ||
this.delayInMins = delayInMins; | ||
this.nextRefreshTimeInMins = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) + delayInMins; | ||
|
||
buildSSLContext(); | ||
|
||
if (LOG.isDebugEnabled()) { | ||
LOG.debug("Certs will be refreshed every {} minutes", delayInMins); | ||
} | ||
} | ||
|
||
public void buildSSLContext() throws SSLException, FileNotFoundException, GeneralSecurityException, IOException { | ||
this.sslContext = SecurityUtility.createNettySslContextForServer(tlsAllowInsecureConnection, | ||
tlsTrustCertsFilePath.getFileName(), tlsCertificateFilePath.getFileName(), tlsKeyFilePath.getFileName(), | ||
tlsCiphers, tlsProtocols, tlsRequireTrustedClientCertOnConnect); | ||
} | ||
|
||
public synchronized SslContext get() { | ||
long nowInSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()); | ||
if (nextRefreshTimeInMins > nowInSeconds) { | ||
nextRefreshTimeInMins = nowInSeconds + delayInMins; | ||
if (tlsTrustCertsFilePath.checkAndRefresh() || tlsCertificateFilePath.checkAndRefresh() | ||
|| tlsKeyFilePath.checkAndRefresh()) { | ||
try { | ||
buildSSLContext(); | ||
} catch (GeneralSecurityException | IOException e) { | ||
LOG.error("Execption while trying to refresh ssl Context: ", e); | ||
} | ||
} | ||
} | ||
return this.sslContext; | ||
} | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ServerSslContextRefresher.class); | ||
} |
Oops, something went wrong.