Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-44755

Ignoring SSL errors on HttpsURLConnection causing null pointer exception

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • workflow-cps-plugin
    • None
    • Jenkins 2.59
      workflow-cps-global-lib-2.8

      We are hitting with the null pointer exception, when tried running the follow script to make a http request which has a code to ignore ssl errors.

      We tried running this code in as a normal groovy program it works, but not as a pipeline code. Also tried with @NonCPS annotation but it didn't work.

      Tried few other examples without disableCertificates(), those were working. But only when we have this code of ignoreSSL errors is causing this error, which is conn.getOutputStream() is returning nothing.

      node('LINUX') {
          stage("IgnoreSSLTesting") {
              def xml = """<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>"""
              echo sendPOST(xml).toString()
          }
      }
      //@NonCPS
      def sendPOST(body) throws IOException {
        URL url = new URL('https://northamerica.abc.net:8443/');
        def HttpsURLConnection con = url.openConnection()
        con.setHostnameVerifier(NoopHostnameVerifier.INSTANCE)
        con.setSSLSocketFactory(disableCertificates())
        con.setDoOutput(true);
        con.setRequestMethod("POST")
        con.setConnectTimeout(1000);
        con.setReadTimeout(1000);
        con.setUseCaches (false);
        con.setDefaultUseCaches (false);
        con.setRequestProperty ( "Content-Type", "text/xml" )
        println con.toString()
        OutputStreamWriter writer = new OutputStreamWriter( con.getOutputStream() )
        writer.write(body)
        writer.flush()
        con.connect()
      
        InputStreamReader reader = new InputStreamReader( con.getInputStream() )
        StringBuilder buf = new StringBuilder();
        char[] cbuf = new char[ 2048 ];
        int num;
        while ( -1 != (num=reader.read( cbuf ))) {
          buf.append( cbuf, 0, num );
        }
        String result = buf.toString();
        return result
      }
      
      //@NonCPS
      def disableCertificates() {
        TrustManager trustAllCert =   new X509ExtendedTrustManager() {
          public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
          }
          public void checkClientTrusted(X509Certificate[] certs, String authType) {  }
          public void checkClientTrusted(X509Certificate[] certs, String authType, SSLEngine engine ) {  }
          public void checkClientTrusted(X509Certificate[] certs, String authType, Socket engine ) {  }
          public void checkServerTrusted(X509Certificate[] certs, String authType) {  }
          public void checkServerTrusted(X509Certificate[] certs, String authType, Socket engine ) {  }
          public void checkServerTrusted(X509Certificate[] certs, String authType, SSLEngine engine ) {  }
        }
      
        TrustManager[] trustAllCerts = new TrustManager[1];
        trustAllCerts[0] = trustAllCert;
        try {
          SSLContext sc = SSLContext.getInstance("SSL");
          sc.init(new KeyManager[0], trustAllCerts, new java.security.SecureRandom());
          return sc.getSocketFactory()
        } catch (Exception e) { }
      }
      

      Exception being thrown:

      java.lang.NullPointerException
      	at java.io.Writer.<init>(Writer.java:88)
      	at java.io.OutputStreamWriter.<init>(OutputStreamWriter.java:109)
      	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      	at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
      	at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
      	at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.constructorCall(DefaultInvoker.java:23)
      	at WorkflowScript.sendPOST(WorkflowScript:84)
      	at WorkflowScript.run(WorkflowScript:67)
      	at ___cps.transform___(Native Method)
      	at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:96)
      	at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
      	at sun.reflect.GeneratedMethodAccessor322.invoke(Unknown Source)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
      	at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
      	at com.cloudbees.groovy.cps.Next.step(Next.java:83)
      	at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:173)
      	at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:162)
      	at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)
      	at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)
      	at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:162)
      	at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:330)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:82)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:242)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:230)
      	at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
      	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      	at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
      	at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
      	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      	at java.lang.Thread.run(Thread.java:748)
      Finished: FAILURE
      

      Not sure if this is the right plugin to log this issue to. As this is shared library logged this here. Please suggest if this is not the right place. Thanks for your help.

            Unassigned Unassigned
            nrayapati Naresh Rayapati
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: