-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add encoders extension library with base64 encoding/decoding capabili…
…ties PiperOrigin-RevId: 552931694
- Loading branch information
1 parent
df15385
commit fa6bfa1
Showing
6 changed files
with
285 additions
and
16 deletions.
There are no files selected for viewing
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
64 changes: 64 additions & 0 deletions
64
extensions/src/main/java/dev/cel/extensions/CelEncoderExtensions.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 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// 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 dev.cel.extensions; | ||
|
||
import com.google.errorprone.annotations.Immutable; | ||
import com.google.protobuf.ByteString; | ||
import dev.cel.checker.CelCheckerBuilder; | ||
import dev.cel.common.CelFunctionDecl; | ||
import dev.cel.common.CelOverloadDecl; | ||
import dev.cel.common.types.SimpleType; | ||
import dev.cel.compiler.CelCompilerLibrary; | ||
import dev.cel.runtime.CelRuntime; | ||
import dev.cel.runtime.CelRuntimeBuilder; | ||
import dev.cel.runtime.CelRuntimeLibrary; | ||
import java.util.Base64; | ||
import java.util.Base64.Decoder; | ||
import java.util.Base64.Encoder; | ||
|
||
/** Internal implementation of Encoder Extensions. */ | ||
@Immutable | ||
public class CelEncoderExtensions implements CelCompilerLibrary, CelRuntimeLibrary { | ||
private static final Encoder BASE64_ENCODER = Base64.getEncoder(); | ||
|
||
private static final Decoder BASE64_DECODER = Base64.getDecoder(); | ||
|
||
@Override | ||
public void setCheckerOptions(CelCheckerBuilder checkerBuilder) { | ||
checkerBuilder.addFunctionDeclarations( | ||
CelFunctionDecl.newFunctionDeclaration( | ||
"base64.decode", | ||
CelOverloadDecl.newGlobalOverload( | ||
"base64_decode_string", SimpleType.BYTES, SimpleType.STRING)), | ||
CelFunctionDecl.newFunctionDeclaration( | ||
"base64.encode", | ||
CelOverloadDecl.newGlobalOverload( | ||
"base64_encode_bytes", SimpleType.STRING, SimpleType.BYTES))); | ||
} | ||
|
||
@SuppressWarnings("Immutable") // Instances of java.util.Base64 are immutable | ||
@Override | ||
public void setRuntimeOptions(CelRuntimeBuilder runtimeBuilder) { | ||
runtimeBuilder.addFunctionBindings( | ||
CelRuntime.CelFunctionBinding.from( | ||
"base64_decode_string", | ||
String.class, | ||
str -> ByteString.copyFrom(BASE64_DECODER.decode(str))), | ||
CelRuntime.CelFunctionBinding.from( | ||
"base64_encode_bytes", | ||
ByteString.class, | ||
bytes -> BASE64_ENCODER.encodeToString(bytes.toByteArray()))); | ||
} | ||
} |
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
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
130 changes: 130 additions & 0 deletions
130
extensions/src/test/java/dev/cel/extensions/CelEncoderExtensionsTest.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,130 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// 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 dev.cel.extensions; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static java.nio.charset.StandardCharsets.ISO_8859_1; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.protobuf.ByteString; | ||
import com.google.testing.junit.testparameterinjector.TestParameterInjector; | ||
import dev.cel.common.CelAbstractSyntaxTree; | ||
import dev.cel.common.CelValidationException; | ||
import dev.cel.common.types.SimpleType; | ||
import dev.cel.compiler.CelCompiler; | ||
import dev.cel.compiler.CelCompilerFactory; | ||
import dev.cel.runtime.CelEvaluationException; | ||
import dev.cel.runtime.CelRuntime; | ||
import dev.cel.runtime.CelRuntimeFactory; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(TestParameterInjector.class) | ||
public class CelEncoderExtensionsTest { | ||
|
||
private static final CelCompiler CEL_COMPILER = | ||
CelCompilerFactory.standardCelCompilerBuilder() | ||
.addVar("stringVar", SimpleType.STRING) | ||
.addLibraries(CelExtensions.encoders()) | ||
.build(); | ||
private static final CelRuntime CEL_RUNTIME = | ||
CelRuntimeFactory.standardCelRuntimeBuilder().addLibraries(CelExtensions.encoders()).build(); | ||
|
||
@Test | ||
public void encode_success() throws Exception { | ||
String encodedBytes = | ||
(String) | ||
CEL_RUNTIME | ||
.createProgram(CEL_COMPILER.compile("base64.encode(b'hello')").getAst()) | ||
.eval(); | ||
|
||
assertThat(encodedBytes).isEqualTo("aGVsbG8="); | ||
} | ||
|
||
@Test | ||
public void decode_success() throws Exception { | ||
ByteString decodedBytes = | ||
(ByteString) | ||
CEL_RUNTIME | ||
.createProgram(CEL_COMPILER.compile("base64.decode('aGVsbG8=')").getAst()) | ||
.eval(); | ||
|
||
assertThat(decodedBytes.size()).isEqualTo(5); | ||
assertThat(decodedBytes.toString(ISO_8859_1)).isEqualTo("hello"); | ||
} | ||
|
||
@Test | ||
public void decode_withoutPadding_success() throws Exception { | ||
ByteString decodedBytes = | ||
(ByteString) | ||
CEL_RUNTIME | ||
// RFC2045 6.8, padding can be ignored. | ||
.createProgram(CEL_COMPILER.compile("base64.decode('aGVsbG8')").getAst()) | ||
.eval(); | ||
|
||
assertThat(decodedBytes.size()).isEqualTo(5); | ||
assertThat(decodedBytes.toString(ISO_8859_1)).isEqualTo("hello"); | ||
} | ||
|
||
@Test | ||
public void roundTrip_success() throws Exception { | ||
String encodedString = | ||
(String) | ||
CEL_RUNTIME | ||
.createProgram(CEL_COMPILER.compile("base64.encode(b'Hello World!')").getAst()) | ||
.eval(); | ||
ByteString decodedBytes = | ||
(ByteString) | ||
CEL_RUNTIME | ||
.createProgram(CEL_COMPILER.compile("base64.decode(stringVar)").getAst()) | ||
.eval(ImmutableMap.of("stringVar", encodedString)); | ||
|
||
assertThat(decodedBytes.toString(ISO_8859_1)).isEqualTo("Hello World!"); | ||
} | ||
|
||
@Test | ||
public void encode_invalidParam_throwsCompilationException() { | ||
CelValidationException e = | ||
assertThrows( | ||
CelValidationException.class, | ||
() -> CEL_COMPILER.compile("base64.encode('hello')").getAst()); | ||
|
||
assertThat(e).hasMessageThat().contains("found no matching overload for 'base64.encode'"); | ||
} | ||
|
||
@Test | ||
public void decode_invalidParam_throwsCompilationException() { | ||
CelValidationException e = | ||
assertThrows( | ||
CelValidationException.class, | ||
() -> CEL_COMPILER.compile("base64.decode(b'aGVsbG8=')").getAst()); | ||
|
||
assertThat(e).hasMessageThat().contains("found no matching overload for 'base64.decode'"); | ||
} | ||
|
||
@Test | ||
public void decode_malformedBase64Char_throwsEvaluationException() throws Exception { | ||
CelAbstractSyntaxTree ast = CEL_COMPILER.compile("base64.decode('z!')").getAst(); | ||
|
||
CelEvaluationException e = | ||
assertThrows(CelEvaluationException.class, () -> CEL_RUNTIME.createProgram(ast).eval()); | ||
|
||
assertThat(e) | ||
.hasMessageThat() | ||
.contains("Function 'base64_decode_string' failed with arg(s) 'z!'"); | ||
assertThat(e).hasCauseThat().hasMessageThat().contains("Illegal base64 character"); | ||
} | ||
} |
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