Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with CACERTS IntelliJ + Gradle

I currently am running some REST calls behind a proxy, so I need to follow some strict processes in order for the calls to go through.

Previously I was building in Eclipse for a POC, but now that I know it works, I am trying to transfer it over to IntelliJ (Personal favorite IDEA) along with Gradle for the build automation.

I got the project to compile, export all the dependencies, etc... but when I run it IN IntelliJ I get a "Cert not found error". On a side note however, if I execute the compiled Jar file (from gradle) using "java-jar MyJar.jar", it runs perfectly and doesn't throw the cert error. The kicker here is, if I execute the Jar using JUST the gradle task outside of IntelliJ it works, but if I try to execute the task right after the build in IntelliJ it fails.

Works:

  • Executing the jar created from Gradle build task manually VIA CLI
  • Executing the gradle task below using "gradle runMain" VIA CLI

Doesn't work: - Executing the build task within IntelliJ and calling "runMain" at the end of the build task

My current theory, is that running it via java -jar and gradle runMain, causes the JVM to use the default cacerts "/jre_xxx/libs/security/cacerts" (where I already added the certificate) but when I execute the Jar within IntelliJ with Gradle, it uses a different location. I've also added the cert to "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.5\jre64\lib\security\cacerts" as well but I still recieved this eror while running it in IntelliJ.

task(runMain, dependsOn: 'classes', type: JavaExec) {
     main = 'com.xxx.xx.x.Utopia'
     classpath = sourceSets.main.runtimeClasspath
     args=[
             "-Djavax.net.ssl.trustStore=C:\\ProgramFiles\\Java\\jre1.8.0_121\\lib\\security\\cacerts"
     ]
 }

Running this VIA CLI seems to work but never with the Gradle build task within IntelliJ.

Any help would be greatly appreciated.

EDIT: The error that I get ONLY while running it within IntelliJ (PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target)

like image 379
97WaterPolo Avatar asked Jul 10 '18 18:07

97WaterPolo


People also ask

Where is cacerts in IntelliJ?

1\system\tasks\cacerts (both VIA IntelliJ and java keytool), as well as C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.

Why is my Gradle project not working in IntelliJ?

When you make any dependency changes in your gradle build file, you might receive errors while building your project from within the IDE – as a result of IntelliJ not resolving the dependency changes. To resolve this issue, navigate to the Gradle Tool window via View -> Tool Windows -> Gradle

How do I manage the storage for IntelliJ IDEA certificates?

IntelliJ IDEA provides its own storage for trusted certificates. Use this page to manage this storage. Select this option if you want non-trusted certificates (that is the certificates that are not added to the list) to be accepted automatically, without sending a request to the server.

How do I add a JDK certificate to a Gradle?

Add certificate manually to your jdk cert strore. download the certificate from browser. Find your jdk installation directory. You can try changing the path for the Gradle wrapper going to Eclipse > Preferences > Gradle. Choose Local Installation directory instead of Gradle Wrapper.

How to import some certifications into Android Studio JDK cacerts from Android Studio?

Import some certifications into Android Studio JDK cacerts from Android Studio’s cacerts. I used the following import command. $ keytool -importkeystore -v -srckeystore {src cacerts} -destkeystore {dest cacerts} 2. Add modified cacert path to gradle.properties-


2 Answers

After contacting JetBrains support with my issue, I was made aware of the problem. Logically I was under the assumption that the JRE would execute the JAR file, this is ONLY the case when running java -jar my.jar or executing Gradle from CLI. The way IntelliJ works is that it solely uses the JDK, so I had to modify the small JRE that was within the JDK. Once I did that and added it to the CACERTS found within my jdk.xxx/jre/lib/security/cacerts, I was able to resolve this issue.

https://youtrack.jetbrains.com/issue/IDEA-195428

like image 55
97WaterPolo Avatar answered Oct 22 '22 13:10

97WaterPolo


In case anyone comes across this issue as well. I had added my certs to the JDK store and ensured IntelliJ was using the JDK however it would still fail when trying to download JARs. Turned out I had to kill the gradle deamon running in the background as it was persisting between IntelliJ restarts. I'm on windows so ps java | kill worked in Powershell. pkill java will work in Linux.

like image 33
Chris Avatar answered Oct 22 '22 13:10

Chris