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
17 changes: 17 additions & 0 deletions conformance/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ cc_library(
"//extensions:math_ext",
"//extensions:math_ext_decls",
"//extensions:math_ext_macros",
"//extensions:network_ext",
"//extensions:network_ext_decls",
"//extensions:proto_ext",
"//extensions:select_optimization",
"//extensions:set_membership_optimization",
Expand Down Expand Up @@ -153,6 +155,7 @@ _ALL_TESTS = [
"@cel_conformance//tests/simple:testdata/macros2.textproto",
"@cel_conformance//tests/simple:testdata/math_ext.textproto",
"@cel_conformance//tests/simple:testdata/namespace.textproto",
"@cel_conformance//tests/simple:testdata/network_ext.textproto",
"@cel_conformance//tests/simple:testdata/optionals.textproto",
"@cel_conformance//tests/simple:testdata/parse.textproto",
"@cel_conformance//tests/simple:testdata/plumbing.textproto",
Expand Down Expand Up @@ -214,6 +217,10 @@ _TESTS_TO_SKIP = [
# precision to preserve value. Not available on older compilers where we just use absl::Format.
# We should probably update the spec to allow different formats that parse to the same value.
"conversions/string/double_hard",

# SimpleTest specifies eval_error while disable_check is false, but this triggers a
# compile-time type-check error before evaluation.
"network_ext/ip_type/is_ip_cidr_compile_error",
]

_TESTS_TO_SKIP_PARSE_ONLY = [
Expand All @@ -235,6 +242,10 @@ _TESTS_TO_SKIP_MODERN_DASHBOARD = [
# TODO(issues/119): Strong typing support for enums, specified but not implemented.
"enums/strong_proto2",
"enums/strong_proto3",

# SimpleTest specifies eval_error while disable_check is false, but this triggers a
# compile-time type-check error before evaluation.
"network_ext/ip_type/is_ip_cidr_compile_error",
]

_TESTS_TO_SKIP_LEGACY = _TESTS_TO_SKIP + [
Expand All @@ -255,6 +266,9 @@ _TESTS_TO_SKIP_LEGACY = _TESTS_TO_SKIP + [

# lists_ext not implemented for legacy evaluator
"lists_ext",

# network_ext requires OpaqueValue which is unsupported in legacy evaluator.
"network_ext",
]

_TESTS_TO_SKIP_LEGACY_DASHBOARD = [
Expand All @@ -265,6 +279,9 @@ _TESTS_TO_SKIP_LEGACY_DASHBOARD = [

# Legacy value does not support optional_type.
"optionals/optionals",

# network_ext requires OpaqueValue which is unsupported in legacy evaluator.
"network_ext",
]

# Generates a bunch of `cc_test` whose names follow the pattern
Expand Down
7 changes: 7 additions & 0 deletions conformance/service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
#include "extensions/math_ext.h"
#include "extensions/math_ext_decls.h"
#include "extensions/math_ext_macros.h"
#include "extensions/network_ext.h"
#include "extensions/network_ext_decls.h"
#include "extensions/proto_ext.h"
#include "extensions/protobuf/enum_adapter.h"
#include "extensions/select_optimization.h"
Expand Down Expand Up @@ -200,6 +202,8 @@ absl::Status CheckImpl(google::protobuf::Arena* arena,
builder->AddLibrary(cel::extensions::ListsCheckerLibrary()));
CEL_RETURN_IF_ERROR(
builder->AddLibrary(cel::extensions::SetsCheckerLibrary()));
CEL_RETURN_IF_ERROR(
builder->AddLibrary(cel::extensions::NetworkCheckerLibrary()));
}

for (const auto& decl : request.type_env()) {
Expand Down Expand Up @@ -329,6 +333,8 @@ class LegacyConformanceServiceImpl : public ConformanceServiceInterface {
builder->GetRegistry(), options));
CEL_RETURN_IF_ERROR(cel::extensions::RegisterSetsFunctions(
builder->GetRegistry(), options));
CEL_RETURN_IF_ERROR(cel::extensions::RegisterNetworkFunctions(
builder->GetRegistry(), options));

return absl::WrapUnique(new LegacyConformanceServiceImpl(
std::move(builder), enable_variadic_logical_operators,
Expand Down Expand Up @@ -552,6 +558,7 @@ class ModernConformanceServiceImpl : public ConformanceServiceInterface {
builder.function_registry(), options));
CEL_RETURN_IF_ERROR(cel::extensions::RegisterSetsFunctions(
builder.function_registry(), options));
CEL_RETURN_IF_ERROR(cel::extensions::RegisterNetworkFunctions(builder));

return std::move(builder).Build();
}
Expand Down
4 changes: 4 additions & 0 deletions env/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ cc_library(
"//extensions:encoders",
"//extensions:lists_functions",
"//extensions:math_ext_decls",
"//extensions:network_ext_decls",
"//extensions:proto_ext",
"//extensions:regex_ext",
"//extensions:sets_functions",
Expand Down Expand Up @@ -154,6 +155,8 @@ cc_library(
"//extensions:lists_functions",
"//extensions:math_ext",
"//extensions:math_ext_decls",
"//extensions:network_ext",
"//extensions:network_ext_decls",
"//extensions:regex_ext",
"//extensions:sets_functions",
"//extensions:strings",
Expand Down Expand Up @@ -308,6 +311,7 @@ cc_test(
"//compiler",
"//extensions:lists_functions",
"//extensions:math_ext_decls",
"//extensions:network_ext_decls",
"//extensions:strings",
"//internal:testing",
"//internal:testing_descriptor_pool",
Expand Down
4 changes: 3 additions & 1 deletion env/env_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ absl::StatusOr<RuntimeBuilder> EnvRuntime::CreateRuntimeBuilder(
if (extension_config.name == "optional") {
optional_extension_config = &extension_config;
runtime_options.enable_qualified_type_identifiers = true;
break;
} else if (extension_config.name == "network" ||
extension_config.name == "cel.lib.ext.network") {
runtime_options.enable_qualified_type_identifiers = true;
}
}

Expand Down
16 changes: 16 additions & 0 deletions env/env_runtime_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ std::vector<TestCase> GetEnvRuntimeTestCases() {
"1 + 2 == 3",
.expected_to_fail = true,
},
TestCase{
.config_yaml = R"yaml(
extensions:
- name: "network"
)yaml",
.expr = "type(ip('192.168.0.1')) == net.IP && "
"cidr('10.0.0.0/8').containsIP('10.0.0.1')",
},
TestCase{
.config_yaml = R"yaml(
extensions:
- name: "cel.lib.ext.network"
version: 1
)yaml",
.expr = "cidr('192.168.0.1/24').masked() == cidr('192.168.0.0/24')",
},
};
}

Expand Down
7 changes: 7 additions & 0 deletions env/env_std_extensions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "extensions/encoders.h"
#include "extensions/lists_functions.h"
#include "extensions/math_ext_decls.h"
#include "extensions/network_ext_decls.h"
#include "extensions/proto_ext.h"
#include "extensions/regex_ext.h"
#include "extensions/sets_functions.h"
Expand Down Expand Up @@ -71,6 +72,12 @@ void RegisterStandardExtensions(Env& env) {
env.RegisterCompilerLibrary("cel.lib.ext.regex", "regex", 0, []() {
return extensions::RegexExtCompilerLibrary();
});
for (int version = 0; version <= extensions::kNetworkExtensionLatestVersion;
++version) {
env.RegisterCompilerLibrary(
"cel.lib.ext.network", "network", version,
[version]() { return extensions::NetworkCompilerLibrary(version); });
}
}

} // namespace cel
9 changes: 9 additions & 0 deletions env/env_std_extensions_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ INSTANTIATE_TEST_SUITE_P(
TestCase{
.extension = "regex",
.expr = "regex.replace('abc', '$', '_end')",
},
TestCase{
.extension = "cel.lib.ext.network",
.expr = "type(ip('192.168.0.1')) == net.IP && "
"cidr('10.0.0.0/8').containsIP('10.0.0.1')",
},
TestCase{
.extension = "network",
.expr = "cidr('192.168.0.1/24').masked() == cidr('192.168.0.0/24')",
}));

} // namespace
Expand Down
13 changes: 13 additions & 0 deletions env/runtime_std_extensions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "extensions/lists_functions.h"
#include "extensions/math_ext.h"
#include "extensions/math_ext_decls.h"
#include "extensions/network_ext.h"
#include "extensions/network_ext_decls.h"
#include "extensions/regex_ext.h"
#include "extensions/sets_functions.h"
#include "extensions/strings.h"
Expand Down Expand Up @@ -128,6 +130,17 @@ void RegisterStandardExtensions(EnvRuntime& env_runtime) {
return cel::extensions::RegisterRegexExtensionFunctions(
runtime_builder);
});

for (int version = 0; version <= extensions::kNetworkExtensionLatestVersion;
++version) {
registry.AddFunctionRegistration(
"cel.lib.ext.network", "network", version,
[version](RuntimeBuilder& runtime_builder,
const RuntimeOptions& runtime_options) -> absl::Status {
return cel::extensions::RegisterNetworkFunctions(runtime_builder,
version);
});
}
}

} // namespace cel
17 changes: 17 additions & 0 deletions env/runtime_std_extensions_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "env/env_std_extensions.h"
#include "extensions/lists_functions.h"
#include "extensions/math_ext_decls.h"
#include "extensions/network_ext_decls.h"
#include "extensions/strings.h"
#include "internal/testing.h"
#include "internal/testing_descriptor_pool.h"
Expand Down Expand Up @@ -219,6 +220,22 @@ std::vector<TestCase> GetRuntimeStdExtensionTestCases() {
.expr = "regex.replace('abc', '$', '_end') == 'abc_end'",
.requires_optional_extension = true,
},
TestCase{
.extension_name = "cel.lib.ext.network",
.extension_versions = {0, 1},
.latest_extension_version =
extensions::kNetworkExtensionLatestVersion,
.expr = "type(ip('192.168.0.1')) == net.IP && "
"ip.isCanonical('192.168.0.1') && "
"cidr('10.0.0.0/8').containsIP('10.0.0.1')",
},
TestCase{
.extension_name = "network",
.extension_versions = {0, 1},
.latest_extension_version =
extensions::kNetworkExtensionLatestVersion,
.expr = "cidr('192.168.0.1/24').masked() == cidr('192.168.0.0/24')",
},
};
}

Expand Down
125 changes: 125 additions & 0 deletions extensions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -961,3 +961,128 @@ cc_test(
"@com_google_protobuf//:protobuf",
],
)

cc_library(
name = "network_ext_decls",
srcs = ["network_ext_decls.cc"],
hdrs = ["network_ext_decls.h"],
deps = [
"//checker:type_checker_builder",
"//checker/internal:builtins_arena",
"//common:constant",
"//common:decl",
"//common:expr",
"//common:navigable_ast",
"//common:standard_definitions",
"//common:type",
"//compiler",
"//internal:network_addr",
"//internal:status_macros",
"//validator",
"@com_google_absl//absl/base:no_destructor",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/types:span",
],
)

cc_test(
name = "network_ext_decls_test",
srcs = ["network_ext_decls_test.cc"],
deps = [
":network_ext_decls",
"//checker:standard_library",
"//checker:validation_result",
"//common:ast",
"//common:decl",
"//common:source",
"//common:type",
"//compiler",
"//compiler:compiler_factory",
"//internal:status_macros",
"//internal:testing",
"//internal:testing_descriptor_pool",
"//validator",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings:string_view",
],
)

cc_library(
name = "network_ext",
srcs = ["network_ext.cc"],
hdrs = ["network_ext.h"],
deps = [
":network_ext_decls",
"//common:native_type",
"//common:standard_definitions",
"//common:type",
"//common:typeinfo",
"//common:value",
"//eval/public:cel_function_registry",
"//eval/public:cel_options",
"//internal:casts",
"//internal:network_addr",
"//internal:status_macros",
"//runtime",
"//runtime:function",
"//runtime:function_adapter",
"//runtime:function_registry",
"//runtime:runtime_builder",
"//runtime:runtime_options",
"//runtime:type_registry",
"//runtime/internal:errors",
"//runtime/internal:runtime_friend_access",
"//runtime/internal:runtime_impl",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings:string_view",
"@com_google_protobuf//:protobuf",
],
)

cc_test(
name = "network_ext_test",
srcs = ["network_ext_test.cc"],
deps = [
":network_ext",
"//checker:standard_library",
"//checker:validation_result",
"//common:ast",
"//common:decl",
"//common:kind",
"//common:type",
"//common:value",
"//common:value_testing",
"//compiler",
"//compiler:compiler_factory",
"//compiler:optional",
"//eval/public:activation",
"//eval/public:cel_expr_builder_factory",
"//eval/public:cel_expression",
"//eval/public:cel_function_registry",
"//eval/public:cel_options",
"//eval/public:cel_value",
"//extensions/protobuf:runtime_adapter",
"//internal:status_macros",
"//internal:testing",
"//internal:testing_descriptor_pool",
"//parser",
"//runtime",
"//runtime:activation",
"//runtime:constant_folding",
"//runtime:optional_types",
"//runtime:reference_resolver",
"//runtime:runtime_options",
"//runtime:standard_runtime_builder_factory",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings:cord_test_helpers",
"@com_google_absl//absl/strings:string_view",
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
"@com_google_protobuf//:protobuf",
],
)
Loading
Loading