Skip to content

Add support for admin api - #196

Draft
rohanshah18 wants to merge 5 commits into
mainfrom
rshah/admin-api
Draft

rohanshah18 wants to merge 5 commits into
mainfrom
rshah/admin-api

Conversation

@rohanshah18

Copy link
Copy Markdown
Contributor

Problem

Describe the purpose of this change. What problem is being solved and why?

Solution

Describe the approach you took. Link to any relevant bugs, issues, docs, or other resources.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Infrastructure change (CI configs, etc)
  • Non-code change (docs, etc)
  • None of the above: (explain here)

Test Plan

Describe specific steps for validating this change.

}

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, trustManagers, new SecureRandom());

Check failure

Code scanning / CodeQL

`TrustManager` that accepts all certificates

This uses [TrustManager](1), which is defined in [ApiClient$](2) and trusts any certificate.

Copilot Autofix

AI about 1 year ago

To fix this issue, the custom TrustManager that accepts all certificates must be removed. Instead, the code should always use proper certificate validation. If the intent is to allow self-signed certificates for certain situations, the recommended alternative is to allow users to specify a CA certificate file or certificate store for validation (as already implemented in the else branch). If an insecure mode is strictly required (e.g., for local development), add prominent warnings and/or throw a runtime exception if used outside test environments; otherwise, remove the insecure TrustManager path entirely. For now, the best fix is to remove the code placing a custom insecure TrustManager in the SSL context and make verifyingSsl == false and verifyingSsl == true behave the same: always use strict certificate validation through a proper trust manager factory.

Required changes:

  • Remove (lines 1477–1492) the custom insecure X509TrustManager and HostnameVerifier.
  • Always use TrustManagerFactory and OkHostnameVerifier, regardless of verifyingSsl.
  • Optionally, provide a warning or exception if a user attempts to disable SSL verification, to prevent accidental use.
Suggested changeset 1
src/main/java/org/openapitools/admin/client/ApiClient.java

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/main/java/org/openapitools/admin/client/ApiClient.java b/src/main/java/org/openapitools/admin/client/ApiClient.java
--- a/src/main/java/org/openapitools/admin/client/ApiClient.java
+++ b/src/main/java/org/openapitools/admin/client/ApiClient.java
@@ -1473,52 +1473,32 @@
         try {
             TrustManager[] trustManagers;
             HostnameVerifier hostnameVerifier;
+            // Always use a proper TrustManager. Insecure SSL verification is not allowed.
             if (!verifyingSsl) {
-                trustManagers = new TrustManager[]{
-                        new X509TrustManager() {
-                            @Override
-                            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
-                            }
+                throw new RuntimeException("Insecure SSL verification is disabled for security reasons. Please provide a CA certificate if you need to trust self-signed certificates.");
+            }
+            
+            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
 
-                            @Override
-                            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
-                            }
-
-                            @Override
-                            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
-                                return new java.security.cert.X509Certificate[]{};
-                            }
-                        }
-                };
-                hostnameVerifier = new HostnameVerifier() {
-                    @Override
-                    public boolean verify(String hostname, SSLSession session) {
-                        return true;
-                    }
-                };
+            if (sslCaCert == null) {
+                trustManagerFactory.init((KeyStore) null);
             } else {
-                TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
-
-                if (sslCaCert == null) {
-                    trustManagerFactory.init((KeyStore) null);
-                } else {
-                    char[] password = null; // Any password will work.
-                    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
-                    Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(sslCaCert);
-                    if (certificates.isEmpty()) {
-                        throw new IllegalArgumentException("expected non-empty set of trusted certificates");
-                    }
-                    KeyStore caKeyStore = newEmptyKeyStore(password);
-                    int index = 0;
-                    for (Certificate certificate : certificates) {
-                        String certificateAlias = "ca" + (index++);
-                        caKeyStore.setCertificateEntry(certificateAlias, certificate);
-                    }
-                    trustManagerFactory.init(caKeyStore);
+                char[] password = null; // Any password will work.
+                CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
+                Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(sslCaCert);
+                if (certificates.isEmpty()) {
+                    throw new IllegalArgumentException("expected non-empty set of trusted certificates");
                 }
-                trustManagers = trustManagerFactory.getTrustManagers();
-                hostnameVerifier = OkHostnameVerifier.INSTANCE;
+                KeyStore caKeyStore = newEmptyKeyStore(password);
+                int index = 0;
+                for (Certificate certificate : certificates) {
+                    String certificateAlias = "ca" + (index++);
+                    caKeyStore.setCertificateEntry(certificateAlias, certificate);
+                }
+                trustManagerFactory.init(caKeyStore);
             }
+            trustManagers = trustManagerFactory.getTrustManagers();
+            hostnameVerifier = OkHostnameVerifier.INSTANCE;
 
             SSLContext sslContext = SSLContext.getInstance("TLS");
             sslContext.init(keyManagers, trustManagers, new SecureRandom());
EOF
Copilot is powered by AI and may make mistakes. Always verify output.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants