Skip to content
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

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

Closed
keithchong opened this issue Feb 19, 2016 · 12 comments
Closed

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

keithchong opened this issue Feb 19, 2016 · 12 comments

Comments

@keithchong
Copy link

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.

@fehguy
Copy link
Contributor

fehguy commented Feb 19, 2016

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

@keithchong
Copy link
Author

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);

@wing328 wing328 added this to the v2.2.0 milestone May 1, 2016
@wing328 wing328 modified the milestones: v2.2.0, v2.3.0 Jul 7, 2016
@wing328 wing328 modified the milestones: v2.2.1, v2.2.2 Aug 8, 2016
@abderrazak-bouadma
Copy link

abderrazak-bouadma commented Jan 2, 2017

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 fare to be a solution.

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

@abderrazak-bouadma
Copy link

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);
        }

    }

@wing328
Copy link
Contributor

wing328 commented Jan 4, 2017

@jpuzzler thanks for sharing your solution.

@wing328 wing328 closed this as completed Feb 15, 2017
@chenrui333
Copy link

chenrui333 commented Jan 23, 2018

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

@wing328
Copy link
Contributor

wing328 commented Jan 24, 2018

@chenrui333
Copy link

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

@chenrui333
Copy link

chenrui333 commented Jan 24, 2018

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

@wing328
Copy link
Contributor

wing328 commented Jan 25, 2018

@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.

@chenrui333
Copy link

Sure!

@hyankov
Copy link

hyankov commented Aug 27, 2020

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants