-
Notifications
You must be signed in to change notification settings - Fork 4.5k
chore: shadow PR for external contribution #37106 [DO NOT MERGE] #37222
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
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
82d1955
Added TLS support for Redis Datasource
AnnaHariprasad5123 2abfee6
fix: some changes
AnnaHariprasad5123 951e3f1
Merge branch 'Issue-#26723-feat-support-tls-for-redis-datasource' of …
NilanshBansal 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 22bc066
Merge branch 'Issue-#26723-feat-support-tls-for-redis-datasource' of …
NilanshBansal 8b87bbf
removed-unused-parameter
AnnaHariprasad5123 6c4ee8a
Merge branch 'Issue-#26723-feat-support-tls-for-redis-datasource' of …
NilanshBansal 6da9385
added-condition-for-endpoint
AnnaHariprasad5123 5eb3b2e
Merge branch 'Issue-#26723-feat-support-tls-for-redis-datasource' of …
NilanshBansal 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
36 changes: 36 additions & 0 deletions
36
...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,36 @@ | ||
| 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 verifyTlsCertificate; | ||
|
|
||
| @JsonView({Views.Public.class, FromRequest.class}) | ||
| UploadedFile caCertificateFile; | ||
NilanshBansal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @JsonView({Views.Public.class, FromRequest.class}) | ||
| Boolean requiresClientAuth; | ||
|
|
||
| @JsonView({Views.Public.class, FromRequest.class}) | ||
| UploadedFile clientCertificateFile; | ||
|
|
||
| @JsonView({Views.Public.class, 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
171 changes: 171 additions & 0 deletions
171
...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,171 @@ | ||
| 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 javax.net.ssl.X509TrustManager; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| 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 { | ||
|
|
||
| TlsConfiguration tlsConfiguration = datasourceConfiguration.getTlsConfiguration(); | ||
| if (tlsConfiguration == null) { | ||
| throw new IllegalArgumentException("TLS configuration is missing"); | ||
| } | ||
| Boolean verifyTlsCertificate = tlsConfiguration.getVerifyTlsCertificate(); | ||
| Boolean requiresClientAuth = tlsConfiguration.getRequiresClientAuth(); | ||
| if (verifyTlsCertificate == null || 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 { | ||
| 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); | ||
| } | ||
|
|
||
| // Handle server certificate verification | ||
| TrustManager[] trustManagers; | ||
| if (verifyTlsCertificate) { | ||
|
|
||
| String caCertContent = | ||
| new String(tlsConfiguration.getCaCertificateFile().getDecodedContent(), StandardCharsets.UTF_8); | ||
|
|
||
| CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); | ||
| X509Certificate caCert = (X509Certificate) | ||
| certificateFactory.generateCertificate(new ByteArrayInputStream(caCertContent.getBytes())); | ||
|
|
||
| KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
| trustStore.load(null, null); | ||
| trustStore.setCertificateEntry("ca-cert", caCert); | ||
|
|
||
| TrustManagerFactory trustManagerFactory = | ||
| TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
| trustManagerFactory.init(trustStore); | ||
| trustManagers = trustManagerFactory.getTrustManagers(); | ||
| } else { | ||
| trustManagers = new TrustManager[] { | ||
| new X509TrustManager() { | ||
| @Override | ||
| public X509Certificate[] getAcceptedIssuers() { | ||
| return new X509Certificate[0]; | ||
| } | ||
|
|
||
| @Override | ||
| public void checkClientTrusted(X509Certificate[] certs, String authType) {} | ||
|
|
||
| @Override | ||
| public void checkServerTrusted(X509Certificate[] certs, String authType) {} | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| // 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
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.