-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java 11 recipe for default keystore change (#667)
* first commit * add .yml and updated description * small update * post review comments * small updates * add no change tests and additional precondition for `java.security.KeyStore#getDefaultType()` --------- Co-authored-by: anuram <ranuradh@us.ibm.com> Co-authored-by: Merlin Bögershausen <merlin.boegershausen@rwth-aachen.de>
- Loading branch information
1 parent
3bdb201
commit 23ddb3b
Showing
3 changed files
with
205 additions
and
1 deletion.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/main/java/org/openrewrite/java/migrate/ChangeDefaultKeyStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://docs.moderne.io/licensing/moderne-source-available-license | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.migrate; | ||
|
||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaVisitor; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.search.UsesJavaVersion; | ||
import org.openrewrite.java.search.UsesMethod; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaType; | ||
import org.openrewrite.java.tree.Space; | ||
import org.openrewrite.marker.Markers; | ||
|
||
import static org.openrewrite.Tree.randomId; | ||
|
||
|
||
public class ChangeDefaultKeyStore extends Recipe { | ||
private static final MethodMatcher KEYSTORE_METHOD_REF = new MethodMatcher("java.security.KeyStore getDefaultType()", true); | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Return String `jks` when `KeyStore.getDefaultType()` is called"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "In Java 11 the default keystore was updated from JKS to PKCS12." + "As a result, applications relying on KeyStore.getDefaultType() may encounter issues after migrating, unless their JKS keystore has been converted to PKCS12." + "This recipe returns default key store of `jks` when `KeyStore.getDefaultType()` method is called to use the pre Java 11 default keystore."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check( | ||
Preconditions.and( | ||
new UsesJavaVersion<>(11), | ||
new UsesMethod<>("java.security.KeyStore getDefaultType()")), | ||
new JavaVisitor<ExecutionContext>() { | ||
@Override | ||
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
if (KEYSTORE_METHOD_REF.matches(method)) { | ||
return new J.Literal(randomId(), Space.EMPTY, Markers.EMPTY, "\"jks\"", "\"jks\"", null, JavaType.Primitive.String); | ||
} | ||
return super.visitMethodInvocation(method, ctx); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
src/test/java/org/openrewrite/java/migrate/ChangeDefaultKeyStoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://docs.moderne.io/licensing/moderne-source-available-license | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.migrate; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
import static org.openrewrite.java.Assertions.javaVersion; | ||
|
||
class ChangeDefaultKeyStoreTest implements RewriteTest { | ||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new ChangeDefaultKeyStore()) | ||
.allSources(src -> src.markers(javaVersion(11))); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void keyStore() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.security.Key; | ||
import java.security.KeyStore; | ||
class Foo { | ||
void bar() { | ||
try{ | ||
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
char[] password = "your_keystore_password".toCharArray(); | ||
FileInputStream keystoreFile = new FileInputStream("path_to_your_keystore_file.jks"); | ||
keystore.load(keystoreFile, password); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
""", | ||
""" | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.security.Key; | ||
import java.security.KeyStore; | ||
class Foo { | ||
void bar() { | ||
try{ | ||
KeyStore keystore = KeyStore.getInstance("jks"); | ||
char[] password = "your_keystore_password".toCharArray(); | ||
FileInputStream keystoreFile = new FileInputStream("path_to_your_keystore_file.jks"); | ||
keystore.load(keystoreFile, password); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void keepString() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.security.Key; | ||
import java.security.KeyStore; | ||
class Foo { | ||
void bar() { | ||
try{ | ||
KeyStore keystore = KeyStore.getInstance("jks"); | ||
char[] password = "your_keystore_password".toCharArray(); | ||
FileInputStream keystoreFile = new FileInputStream("path_to_your_keystore_file.jks"); | ||
keystore.load(keystoreFile, password); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
""") | ||
); | ||
} | ||
|
||
@Test | ||
void keepStringForJava8() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.security.Key; | ||
import java.security.KeyStore; | ||
class Foo { | ||
void bar() { | ||
try{ | ||
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
char[] password = "your_keystore_password".toCharArray(); | ||
FileInputStream keystoreFile = new FileInputStream("path_to_your_keystore_file.jks"); | ||
keystore.load(keystoreFile, password); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
""", | ||
spec -> spec.markers(javaVersion(8))) | ||
); | ||
} | ||
} |