Skip to content

Commit

Permalink
Java 11 recipe for default keystore change (#667)
Browse files Browse the repository at this point in the history
* 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
3 people authored Feb 4, 2025
1 parent 3bdb201 commit 23ddb3b
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 1 deletion.
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);
}
});
}
}
3 changes: 2 additions & 1 deletion src/main/resources/META-INF/rewrite/java-version-11.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ recipeList:
- org.openrewrite.java.migrate.ReplaceLocalizedStreamMethods
- org.openrewrite.java.migrate.ArrayStoreExceptionToTypeNotPresentException
- org.openrewrite.java.migrate.IllegalArgumentExceptionToAlreadyConnectedException

- org.openrewrite.java.migrate.ChangeDefaultKeyStore
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.UpgradeBuildToJava11
Expand Down Expand Up @@ -292,3 +292,4 @@ recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: java.nio.file.Path get(..)
newMethodName: of

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

0 comments on commit 23ddb3b

Please sign in to comment.