swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

Can't generate a java client from a secure (SSL) swagger definition

Open keithchong opened this issue 10 years ago • 13 comments

See related, #102 I am using v2.1.5.

I tried to configure -Dheader and -a but can't seem to get this to work.

I always get the message:

Server returned HTTP response code: 401 for URL

when I try to generate a java client.

keithchong avatar Feb 19 '16 14:02 keithchong

Please share the entire command that you're using to generate the client.

fehguy avatar Feb 19 '16 16:02 fehguy

I tried various combinations, the difference is just tweaking the options -a and -Dhead:

java -jar C:\swagger\swagger-codegen-cli.jar generate -l java -a "Authorization: Basic {xor}KD4sPjsyNjE=" -t C:\mytemplates\jaxrsClient" -i https://abc.com/wp/swagger.json -o C:\out

java -jar C:\swagger\swagger-codegen-cli.jar generate -l java -a "Authorization: Basic {xor}KD4sPjsyNjE=" -t C:\mytemplates\jaxrsClient" -i https://abc.com/wp/swagger.json -o C:\out -Dhead=Authorization: Basic {xor}KD4sPjsyNjE="

I set -a because I think that is how RemoteURL and AuthParser works:

conn.setRequestProperty(item.getKeyName(), item.getValue());

ie. conn.setRequestProperty("Authorization", "Basic " + encodedValue);

keithchong avatar Feb 19 '16 20:02 keithchong

It's a an SSL issue SSLHandshakeException with a detailMessage 'java.security.cert.CertificateException: No name matching localhost found`.

which means that there's a hostname verifier that returns false and the invokation fails.

there's a work around by implementing a hostname verifier that returns true for localhost but it's far from an ideal solution.

the solution we adopted is to consider that the /swagger.json uri isn't https.

abderraza8k avatar Jan 02 '17 16:01 abderraza8k

here's what I used to disable hostname verifier

   private static void disableSSLVerification() {

        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }

        }};

        SSLContext sc;
        try {
            sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HostnameVerifier allHostsValid = (hostname, session) -> true;
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
        } catch (KeyManagementException | NoSuchAlgorithmException e) {
            logger.error("[SSL] Error by passing hostname verifier", e);
        }

    }

abderraza8k avatar Jan 02 '17 17:01 abderraza8k

@jpuzzler thanks for sharing your solution.

wing328 avatar Jan 04 '17 15:01 wing328

So we have to manually change the source code to disable the hostname verifier? Is there any flag that we can use?

chenrui333 avatar Jan 23 '18 15:01 chenrui333

@chenrui333 would the following help?

https://github.com/swagger-api/swagger-codegen/wiki/FAQ#is-there-a-way-to-disable-certificate-verification

wing328 avatar Jan 24 '18 04:01 wing328

@wing328 Yeah, that works, thanks for the link ref!

chenrui333 avatar Jan 24 '18 12:01 chenrui333

@wing328 is there anyway that we can stuff in the X-Xsrf-Header header or other HTTP headers?

chenrui333 avatar Jan 24 '18 12:01 chenrui333

@wing328 is there anyway that we can stuff in the X-Xsrf-Header header or other HTTP headers?

Please open a new issue so that the community can help you out.

wing328 avatar Jan 25 '18 14:01 wing328

Sure!

chenrui333 avatar Jan 25 '18 14:01 chenrui333

@chenrui333 would the following help?

https://github.com/swagger-api/swagger-codegen/wiki/FAQ#is-there-a-way-to-disable-certificate-verification

This doesn't work (anymore?)

java -jar swagger-codegen-cli.jar generate -Dio.swagger.parser.util.RemoteUrl.trustAll=true -i https://localhost:5001/swagger/v1/swagger.json -l csharp -o Temp -c config.json
15:33:27.169 [main] DEBUG io.swagger.codegen.v3.cli.SwaggerCodegen - there are not options for command 'langs'
15:33:27.171 [main] DEBUG io.swagger.codegen.v3.cli.SwaggerCodegen - there are not options for command 'version'
15:33:27.635 [Thread-1] ERROR io.swagger.v3.parser.util.RemoteUrl - unable to read
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Edit: This worked: java -jar swagger-codegen-cli.jar generate -Dio.swagger.v3.parser.util.RemoteUrl.trustAll=true -i https://localhost:5001/swagger/v1/swagger.json -l csharp -o Temp -c config.json

The important difference here being the v3

hyankov avatar Aug 27 '20 20:08 hyankov

In case anyone else runs into this issue, it'll be useful to know that the above command works, but contains a typo. The correct command will be:

java -jar swagger-codegen-cli.jar generate -D io.swagger.v3.parser.util.RemoteUrl.trustAll=true -i <swagger json file URL> -l csharp -o Temp -c config.json

I was getting a Java runtime error in my terminal saying that everything following the -Dio is an invalid argument, and with some further digging found this thread, which uses the same command except placed in a .xml file and without the D at the beginning. I think -D is intended to be a flag, and everything else after that is the actual setting.

Happy coding

AO-sbah avatar Mar 03 '25 15:03 AO-sbah