-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat: Added TLS support for Redis Datasource #37106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
AnnaHariprasad5123
wants to merge
9
commits into
appsmithorg:release
from
AnnaHariprasad5123:Issue-#26723-feat-support-tls-for-redis-datasource
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
82d1955
Added TLS support for Redis Datasource
AnnaHariprasad5123 2abfee6
fix: some changes
AnnaHariprasad5123 148a41d
fix: add-try/catch-blocks
AnnaHariprasad5123 75f8f3c
fix: resolved comments
AnnaHariprasad5123 d43611d
fix: added catch block
AnnaHariprasad5123 faae249
added throw from catch blocks
AnnaHariprasad5123 8b87bbf
removed-unused-parameter
AnnaHariprasad5123 6da9385
added-condition-for-endpoint
AnnaHariprasad5123 0813212
updated redis datasource
AnnaHariprasad5123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
30 changes: 30 additions & 0 deletions
30
...rver/appsmith-interfaces/src/main/java/com/appsmith/external/models/TlsConfiguration.java
This file contains hidden or 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,30 @@ | ||
| package com.appsmith.external.models; | ||
|
|
||
| import com.appsmith.external.views.FromRequest; | ||
| import com.appsmith.external.views.Views; | ||
| import com.fasterxml.jackson.annotation.JsonView; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
| import lombok.ToString; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @ToString | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class TlsConfiguration { | ||
|
|
||
| @JsonView({Views.Public.class, FromRequest.class}) | ||
| Boolean tlsEnabled; | ||
|
|
||
| @JsonView({Views.Public.class, FromRequest.class}) | ||
| Boolean requiresClientAuth; | ||
|
|
||
| @JsonView({Views.Public.class, FromRequest.class}) | ||
| UploadedFile clientCertificateFile; | ||
|
|
||
| @JsonView({FromRequest.class}) | ||
| UploadedFile clientKeyFile; | ||
| } |
This file contains hidden or 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 hidden or 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
139 changes: 139 additions & 0 deletions
139
...server/appsmith-plugins/redisPlugin/src/main/java/com/external/utils/RedisTLSManager.java
This file contains hidden or 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,139 @@ | ||
| package com.external.utils; | ||
|
|
||
| import com.appsmith.external.models.DatasourceConfiguration; | ||
| import com.appsmith.external.models.TlsConfiguration; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import redis.clients.jedis.JedisPool; | ||
| import redis.clients.jedis.JedisPoolConfig; | ||
|
|
||
| import javax.net.ssl.KeyManagerFactory; | ||
| import javax.net.ssl.SSLContext; | ||
| import javax.net.ssl.SSLSocketFactory; | ||
| import javax.net.ssl.TrustManager; | ||
| import javax.net.ssl.TrustManagerFactory; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.security.KeyFactory; | ||
| import java.security.KeyStore; | ||
| import java.security.PrivateKey; | ||
| import java.security.SecureRandom; | ||
| import java.security.cert.CertificateException; | ||
| import java.security.cert.CertificateFactory; | ||
| import java.security.cert.X509Certificate; | ||
| import java.security.spec.PKCS8EncodedKeySpec; | ||
| import java.util.Base64; | ||
|
|
||
| @Slf4j | ||
| public class RedisTLSManager { | ||
|
|
||
| public static JedisPool createJedisPoolWithTLS( | ||
| JedisPoolConfig poolConfig, URI uri, int timeout, DatasourceConfiguration datasourceConfiguration) | ||
| throws Exception { | ||
NilanshBansal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| TlsConfiguration tlsConfiguration = datasourceConfiguration.getTlsConfiguration(); | ||
| if (tlsConfiguration == null) { | ||
| throw new IllegalArgumentException("TLS configuration is missing"); | ||
| } | ||
| Boolean requiresClientAuth = tlsConfiguration.getRequiresClientAuth(); | ||
| if (requiresClientAuth == null) { | ||
| throw new IllegalArgumentException("TLS configuration flags cannot be null"); | ||
| } | ||
| SSLContext sslContext = SSLContext.getInstance("TLS"); | ||
| KeyManagerFactory keyManagerFactory = null; | ||
| try { | ||
| // Handle client authentication if required, regardless of certificate verification | ||
| if (requiresClientAuth) { | ||
|
|
||
| CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); | ||
|
|
||
| X509Certificate clientCert = null; | ||
|
|
||
| byte[] clientCertBytes = | ||
| tlsConfiguration.getClientCertificateFile().getDecodedContent(); | ||
|
|
||
| try (ByteArrayInputStream certInputStream = new ByteArrayInputStream(clientCertBytes)) { | ||
| clientCert = (X509Certificate) certificateFactory.generateCertificate(certInputStream); | ||
|
|
||
| } catch (CertificateException e) { | ||
| log.error("Error occurred while parsing client certificate: " + e.getMessage()); | ||
| throw e; | ||
| } finally { | ||
| java.util.Arrays.fill(clientCertBytes, (byte) 0); | ||
| } | ||
|
|
||
| PrivateKey privateKey = null; | ||
|
|
||
| byte[] clientKeyBytes = tlsConfiguration.getClientKeyFile().getDecodedContent(); | ||
|
|
||
| try { | ||
| privateKey = loadPrivateKey(clientKeyBytes); | ||
| } catch (Exception e) { | ||
| log.error("Error occurred while parsing private key: " + e.getMessage()); | ||
| throw e; | ||
| } finally { | ||
NilanshBansal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| java.util.Arrays.fill(clientKeyBytes, (byte) 0); | ||
| } | ||
|
|
||
| // KeyStore for client authentication | ||
| KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
| keyStore.load(null, null); | ||
| keyStore.setKeyEntry("client-key", privateKey, null, new X509Certificate[] {clientCert}); | ||
|
|
||
| keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); | ||
| keyManagerFactory.init(keyStore, null); | ||
| } | ||
|
|
||
| // Use OS trust store for server certificate verification | ||
| log.debug("Using OS default trust store for server certificate verification."); | ||
| TrustManagerFactory trustManagerFactory = | ||
| TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
| trustManagerFactory.init((KeyStore) null); | ||
| TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); | ||
|
|
||
| // Initialize SSL context with appropriate managers | ||
| sslContext.init( | ||
| requiresClientAuth ? keyManagerFactory.getKeyManagers() : null, trustManagers, new SecureRandom()); | ||
| SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); | ||
|
|
||
| // Create and return JedisPool with TLS | ||
| JedisPool jedisPool = new JedisPool(poolConfig, uri, timeout, sslSocketFactory, null, null); | ||
| log.debug(Thread.currentThread().getName() + ": Created Jedis pool with TLS."); | ||
| return jedisPool; | ||
| } catch (CertificateException | IOException e) { | ||
| log.error("Error occurred during TLS setup (Certificate or I/O issue): {}", e.getMessage(), e); | ||
| throw e; | ||
| } catch (Exception e) { | ||
| log.error("Unexpected error occurred while creating Jedis pool with TLS: {}", e.getMessage(), e); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| private static PrivateKey loadPrivateKey(byte[] keyBytes) throws Exception { | ||
| byte[] decodedKey = null; | ||
| try { | ||
| String keyString = new String(keyBytes); | ||
|
|
||
| String keyType = "RSA"; | ||
| if (keyString.contains("BEGIN EC PRIVATE KEY")) { | ||
| keyType = "EC"; | ||
| } | ||
|
|
||
| String cleanKey = | ||
| keyString.replaceAll("-----(?:BEGIN|END)[^-]+-----", "").replaceAll("\\s", ""); | ||
|
|
||
| decodedKey = Base64.getDecoder().decode(cleanKey); | ||
|
|
||
| PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decodedKey); | ||
| KeyFactory kf = KeyFactory.getInstance(keyType); | ||
| return kf.generatePrivate(spec); | ||
| } catch (Exception e) { | ||
| log.error("Unexpected error while loading private key: {}", e.getMessage(), e); | ||
| throw e; | ||
| } finally { | ||
| if (decodedKey != null) { | ||
| java.util.Arrays.fill(decodedKey, (byte) 0); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.