Skip to content
Merged
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
8 changes: 0 additions & 8 deletions checker/src/main/java/dev/cel/checker/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,6 @@ private static CelStandardDeclarations newStandardDeclarations(CelOptions celOpt
return false;
}
break;
case TIMESTAMP:
// TODO: Remove this flag guard once the feature has been
// auto-enabled.
if (!celOptions.enableTimestampEpoch()
&& overload.equals(Conversions.INT64_TO_TIMESTAMP)) {
return false;
}
break;
default:
// Heterogeneous numeric comparisons require UnsignedLong at runtime so uint
// values do not share Long with int and dispatch to signed int64 overloads.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import dev.cel.checker.CelStandardDeclarations.StandardFunction;
import dev.cel.checker.CelStandardDeclarations.StandardFunction.Overload.Arithmetic;
import dev.cel.checker.CelStandardDeclarations.StandardFunction.Overload.Comparison;
import dev.cel.checker.CelStandardDeclarations.StandardFunction.Overload.Conversions;
import dev.cel.checker.CelStandardDeclarations.StandardIdentifier;
import dev.cel.common.CelOptions;
import dev.cel.common.CelValidationException;
Expand Down Expand Up @@ -374,10 +375,14 @@ public void unsignedLongsDisabled_int64Identity_throws() {
}

@Test
public void timestampEpochDisabled_int64Identity_throws() {
public void standardEnvironment_excludeInt64ToTimestamp_throws() {
CelCompiler celCompiler =
CelCompilerFactory.standardCelCompilerBuilder()
.setOptions(CelOptions.current().enableTimestampEpoch(false).build())
.setStandardDeclarations(
CelStandardDeclarations.newBuilder()
.filterFunctions(
(function, overload) -> !overload.equals(Conversions.INT64_TO_TIMESTAMP))
.build())
.build();

CelValidationException e =
Expand Down
34 changes: 20 additions & 14 deletions common/src/main/java/dev/cel/common/CelOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ public enum ProtoUnsetFieldOptions {

public abstract boolean enableHomogeneousLiterals();

public abstract boolean enableTimestampEpoch();
/**
* @deprecated Timestamp from Unix epoch seconds ({@code timestamp(int)}) is always enabled.
*/
@Deprecated
public boolean enableTimestampEpoch() {
return true;
}

/**
* @deprecated Cross-type numeric comparisons are always enabled.
Expand Down Expand Up @@ -154,7 +160,6 @@ public static Builder newBuilder() {
// Type-Checker options
.enableCompileTimeOverloadResolution(false)
.enableHomogeneousLiterals(false)
.enableTimestampEpoch(false)
.enableNamespacedDeclarations(true)
.enableJsonFieldNames(false)
// Evaluation options
Expand Down Expand Up @@ -188,7 +193,6 @@ public static Builder current() {
.enableUnsignedComparisonAndArithmeticIsUnsigned(true)
.enableUnsignedLongs(true)
.enableRegexPartialMatch(true)
.enableTimestampEpoch(true)
.errorOnDuplicateMapKeys(true)
.evaluateCanonicalTypesToNativeValues(true)
.errorOnIntWrap(true)
Expand Down Expand Up @@ -309,20 +313,22 @@ public abstract static class Builder {
public abstract Builder enableHomogeneousLiterals(boolean value);

/**
* Enable the {@code int64_to_timestamp} overload which creates a timestamp from Unix epoch
* seconds.
*
* <p>This option is enabled by default in {@link CelOptions#current}.
*
* <p>TODO: Remove this feature once it has been auto-enabled.
*
* @deprecated This option is now enabled by default. If you are passing {@code true}, simply
* remove this method call. If you are passing {@code false} to disable this feature, subset
* the environment instead using {@code dev.cel.checker.CelStandardDeclarations} and {@code
* @deprecated Timestamp from Unix epoch seconds ({@code timestamp(int)}) is always enabled
* ({@code false} throws {@link IllegalArgumentException}). Remove calls passing {@code
* true}, or subset using {@code dev.cel.checker.CelStandardDeclarations} and {@code
* dev.cel.runtime.CelStandardFunctions}.
*/
@Deprecated
public abstract Builder enableTimestampEpoch(boolean value);
@CanIgnoreReturnValue
public Builder enableTimestampEpoch(boolean value) {
if (!value) {
throw new IllegalArgumentException(
"Misconfigured CelOptions: enableTimestampEpoch cannot be disabled."
+ " Subset the environment instead using CelStandardDeclarations and"
+ " CelStandardFunctions.");
}
return this;
}

/**
* @deprecated Cross-type numeric comparisons are always enabled ({@code false} throws {@link
Expand Down
21 changes: 21 additions & 0 deletions common/src/test/java/dev/cel/common/CelOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,25 @@ public void enableHeterogeneousNumericComparisons_false_throws() {
.hasMessageThat()
.contains("enableHeterogeneousNumericComparisons cannot be disabled");
}

@Test
@SuppressWarnings("deprecation") // Testing deprecated compatibility methods
public void enableTimestampEpoch_true_isNoOp() {
CelOptions.Builder builder = CelOptions.newBuilder();

assertThat(builder.enableTimestampEpoch(true)).isSameInstanceAs(builder);
assertThat(CelOptions.DEFAULT.enableTimestampEpoch()).isTrue();
assertThat(CelOptions.LEGACY.enableTimestampEpoch()).isTrue();
}

@Test
@SuppressWarnings("deprecation") // Testing deprecated compatibility methods
public void enableTimestampEpoch_false_throws() {
CelOptions.Builder builder = CelOptions.DEFAULT.toBuilder();

IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> builder.enableTimestampEpoch(false));

assertThat(e).hasMessageThat().contains("enableTimestampEpoch cannot be disabled");
}
}
6 changes: 0 additions & 6 deletions runtime/src/main/java/dev/cel/runtime/CelRuntimeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,6 @@ private static void assertAllowedCelOptions(CelOptions celOptions) {
throw new IllegalArgumentException(
prefix + "unwrapWellKnownTypesOnFunctionDispatch cannot be disabled.");
}
if (!celOptions.enableTimestampEpoch()) {
throw new IllegalArgumentException(
prefix
+ "enableTimestampEpoch cannot be disabled. Subset the environment instead using"
+ " setStandardFunctions method.");
}
if (!celOptions.evaluateCanonicalTypesToNativeValues()) {
throw new IllegalArgumentException(
prefix + "evaluateCanonicalTypesToNativeValues cannot be disabled.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import dev.cel.common.values.CelValueConverter;
import dev.cel.common.values.CelValueProvider;
import dev.cel.runtime.standard.IntFunction.IntOverload;
import dev.cel.runtime.standard.TimestampFunction.TimestampOverload;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Optional;
Expand Down Expand Up @@ -401,13 +400,6 @@ private ImmutableSet<CelFunctionBinding> newStandardFunctionBindings(
return options.enableUnsignedLongs();
}
break;
case TIMESTAMP:
// TODO: Remove this flag guard once the feature has been
// auto-enabled.
if (standardOverload.equals(TimestampOverload.INT64_TO_TIMESTAMP)) {
return options.enableTimestampEpoch();
}
break;
default:
if (!options.enableUnsignedLongs()) {
return !CelStandardFunctions.isHeterogeneousComparison(
Expand Down
1 change: 1 addition & 0 deletions runtime/src/test/java/dev/cel/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ java_library(
"//runtime/standard:not_strictly_false",
"//runtime/standard:standard_overload",
"//runtime/standard:subtract",
"//runtime/standard:timestamp",
"//runtime/standard:type",
"//testing:cel_runtime_flavor",
"//testing/protos:message_with_enum_cel_java_proto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import dev.cel.runtime.standard.AddOperator.AddOverload;
import dev.cel.runtime.standard.CelStandardOverload;
import dev.cel.runtime.standard.SubtractOperator.SubtractOverload;
import dev.cel.runtime.standard.TimestampFunction.TimestampOverload;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand Down Expand Up @@ -222,14 +223,19 @@ public void unsignedLongsDisabled_int64Identity_throws() {
}

@Test
public void timestampEpochDisabled_int64Identity_throws() {
public void standardEnvironment_excludeInt64ToTimestamp_throws() throws Exception {
CelCompiler celCompiler =
CelCompilerFactory.standardCelCompilerBuilder()
.setOptions(CelOptions.current().build())
.build();
CelRuntime celRuntime =
CelRuntimeFactory.legacyCelRuntimeBuilder()
.setOptions(CelOptions.current().enableTimestampEpoch(false).build())
.setStandardEnvironmentEnabled(false)
.setStandardFunctions(
CelStandardFunctions.newBuilder()
.filterFunctions((function, overload) ->
!overload.equals(TimestampOverload.INT64_TO_TIMESTAMP))
.build())
.build();

CelEvaluationException e =
Expand Down
Loading