Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ recipeDependencies {
testParserClasspath("com.github.spotbugs:spotbugs-annotations:4.9.3")
testParserClasspath("com.sun.faces:jsf-api:2.1.29-11")
testParserClasspath("com.sun.istack:istack-commons-runtime:4.1.2")
testParserClasspath("com.twilio.sdk:twilio:7.55.3")
testParserClasspath("commons-fileupload:commons-fileupload:1.5")
testParserClasspath("commons-logging:commons-logging:1.3.2")
testParserClasspath("io.micrometer:micrometer-core:1.15.4")
Expand Down Expand Up @@ -51,6 +52,7 @@ recipeDependencies {
testParserClasspath("javax.xml.bind:jaxb-api:2.3.1")
testParserClasspath("org.apache.commons:commons-fileupload2-jakarta:2.0.0-M1")
testParserClasspath("org.apache.commons:commons-fileupload2-jakarta-servlet5:2.0.0-M5")
testParserClasspath("org.apache.httpcomponents:httpclient:4.5.14")
testParserClasspath("org.apache.johnzon:johnzon-core:1.2.18")
testParserClasspath("org.apache.logging.log4j:log4j-api:2.23.1")
testParserClasspath("org.apache.ws.security:wss4j:1.6.19")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright 2026 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.twilio;

import lombok.Getter;
import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.SourceFile;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JContainer;
import org.openrewrite.java.tree.JavaType;

public class TwilioJwtSecretToByteArray extends Recipe {
private static final MethodMatcher ACCESS_TOKEN_BUILDER =
new MethodMatcher("com.twilio.jwt.accesstoken.AccessToken$Builder <constructor>(String, String, String)");
private static final MethodMatcher CLIENT_CAPABILITY_BUILDER =
new MethodMatcher("com.twilio.jwt.client.ClientCapability$Builder <constructor>(String, String)");
private static final MethodMatcher TASK_ROUTER_CAPABILITY_BUILDER =
new MethodMatcher("com.twilio.jwt.taskrouter.TaskRouterCapability$Builder <constructor>(String, String, String, String)");

private static final JavaType BYTE_ARRAY = new JavaType.Array(null, JavaType.Primitive.Byte, null);

@Getter
final String displayName = "Pass Twilio JWT secrets as `byte[]`";

@Getter
final String description = "Twilio Java SDK 10.8.0 changed the secret of `AccessToken.Builder`, " +
"`ClientCapability.Builder` and `TaskRouterCapability.Builder` from `String` to `byte[]`. " +
"The SDK used to call `String#getBytes()` itself, so this calls it at the construction site, " +
"with `StandardCharsets.UTF_8` rather than the platform default. " +
"Twilio secrets are ASCII, so the bytes and the signatures made with them stay the same.";

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
Preconditions.or(
new UsesMethod<>(ACCESS_TOKEN_BUILDER),
new UsesMethod<>(CLIENT_CAPABILITY_BUILDER),
new UsesMethod<>(TASK_ROUTER_CAPABILITY_BUILDER)),
new JavaIsoVisitor<ExecutionContext>() {
@Override
public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
// The template is Java; Kotlin has no `String#getBytes(Charset)`
return sourceFile instanceof J.CompilationUnit;
}

@Override
public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
J.NewClass n = super.visitNewClass(newClass, ctx);
int index = secretIndex(n);
if (index < 0) {
return n;
}
Expression secret = n.getArguments().get(index);
if (J.Literal.isLiteralValue(secret, null) || secret.getType() instanceof JavaType.Array) {
return n;
}

String receiver = isPrimary(secret) ? "#{any(String)}" : "(#{any(String)})";
Expression bytes = JavaTemplate.builder(receiver + ".getBytes(StandardCharsets.UTF_8)")
.imports("java.nio.charset.StandardCharsets")
.build()
.apply(new Cursor(getCursor(), secret), secret.getCoordinates().replace(), secret);
maybeAddImport("java.nio.charset.StandardCharsets");

// Through the padding, as the whitespace before a closing parenthesis on its own line is kept there
JContainer<Expression> arguments = n.getPadding().getArguments();
n = n.getPadding().withArguments(arguments.getPadding().withElements(ListUtils.map(
arguments.getPadding().getElements(), (i, arg) -> i == index ? arg.withElement(bytes) : arg)));
JavaType.Method constructor = n.getConstructorType();
if (constructor != null) {
n = n.withConstructorType(constructor.withParameterTypes(ListUtils.map(
constructor.getParameterTypes(), (i, type) -> i == index ? BYTE_ARRAY : type)));
}
return n;
}

private int secretIndex(J.NewClass newClass) {
if (ACCESS_TOKEN_BUILDER.matches(newClass)) {
return 2;
}
if (CLIENT_CAPABILITY_BUILDER.matches(newClass) || TASK_ROUTER_CAPABILITY_BUILDER.matches(newClass)) {
return 1;
}
return -1;
}

private boolean isPrimary(Expression expression) {
return expression instanceof J.Identifier ||
expression instanceof J.FieldAccess ||
expression instanceof J.Literal ||
expression instanceof J.MethodInvocation ||
expression instanceof J.ArrayAccess ||
expression instanceof J.NewClass ||
expression instanceof J.Parentheses;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.
*/
@NullMarked
@NonNullFields
package org.openrewrite.java.migrate.twilio;

import org.jspecify.annotations.NullMarked;
import org.openrewrite.internal.lang.NonNullFields;
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/rewrite/category.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,8 @@ type: specs.openrewrite.org/v1beta/category
name: DataNucleus
packageName: org.openrewrite.java.migrate.datanucleus
description: Recipes for migrating [DataNucleus](https://www.datanucleus.org/) JDO/JPA persistence applications.
---
type: specs.openrewrite.org/v1beta/category
name: Twilio
packageName: org.openrewrite.java.migrate.twilio
description: Recipes for migrating applications that use the [Twilio Java SDK](https://github.com/twilio/twilio-java).
Loading