Skip to content

Commit c1ccb4f

Browse files
author
constanze
committed
build(pem): make the direct-query kill switch a bool_flag with local_defines
Review feedback on #2401. The BUILD comment advertised `--//src/vizier/services/agent/pem:direct_query=disabled`, but no such target existed — `bazel query` fails with "target 'direct_query' not declared" — so the only working off-switch was `--define`. Declare the bool_flag the comment promised and key the config_setting off it, so the documented invocation works and the setting stays scoped to the targets that read it instead of the global --define configuration key. Also switches cc_library from `defines` to `local_defines` so the macro stops propagating to every transitive dependent. Because that macro no longer reaches dependents, direct_query_server_test selects on the same config_setting itself — without that its `#ifdef PX_PEM_DIRECT_QUERY_DISABLED` assertions would silently compile the enabled branch in a disabled build. Verified with --config=x86_64_sysroot: default build compiles the feature in (34 passed / 3 skipped), --//…:direct_query=false compiles the macro into both the library and the test, and the resulting binary behaves identically to the previous --define path (38 tests, 7 pass, 28 fail — unchanged from the old mechanism; those failures are a separate issue). NOTE: --define=PX_PEM_DIRECT_QUERY=disabled no longer has any effect.
1 parent 7f63749 commit c1ccb4f

3 files changed

Lines changed: 43 additions & 15 deletions

File tree

src/vizier/services/agent/pem/BUILD.bazel

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,34 @@
1414
#
1515
# SPDX-License-Identifier: Apache-2.0
1616

17+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
1718
load("@io_bazel_rules_docker//cc:image.bzl", "cc_image")
1819
load("@io_bazel_rules_docker//container:container.bzl", "container_image")
1920
load("//bazel:pl_build_system.bzl", "pl_cc_binary", "pl_cc_library", "pl_cc_test")
2021

2122
package(default_visibility = ["//src/vizier:__subpackages__"])
2223

23-
# Compile-time kill switch for the direct-query endpoint.
24-
# Operators who do not want the feature available even as a disabled-by-default
25-
# option build with `bazel build … --//src/vizier/services/agent/pem:direct_query=disabled`
26-
# (or `--define=PX_PEM_DIRECT_QUERY=disabled`). This propagates
27-
# `-DPX_PEM_DIRECT_QUERY_DISABLED` into cc_library; the entire feature body
28-
# in direct_query_server.cc and pem_manager.cc is `#ifndef`'d out and
29-
# stub methods return UNIMPLEMENTED. See DIRECT_QUERY_SECURITY.md.
24+
# Compile-time kill switch for the direct-query endpoint. Defaults to True, so
25+
# the feature is compiled in and gated at runtime by --direct_query_enabled
26+
# (itself false by default). Operators who do not want the feature present in
27+
# the binary at all build with
28+
# `bazel build … --//src/vizier/services/agent/pem:direct_query=false`, which
29+
# puts `-DPX_PEM_DIRECT_QUERY_DISABLED` on this package's compilations only:
30+
# the feature body in direct_query_server.cc and pem_manager.cc is `#ifndef`'d
31+
# out and stub methods return UNIMPLEMENTED. See DIRECT_QUERY_SECURITY.md.
32+
#
33+
# A build setting rather than `--define`: `--define` lives in the global
34+
# configuration key, so it applies to every target in the build and shows up in
35+
# every dependency's configuration; this flag is scoped to the targets that
36+
# read it.
37+
bool_flag(
38+
name = "direct_query",
39+
build_setting_default = True,
40+
)
41+
3042
config_setting(
3143
name = "direct_query_disabled",
32-
define_values = {"PX_PEM_DIRECT_QUERY": "disabled"},
44+
flag_values = {":direct_query": "False"},
3345
)
3446

3547
pl_cc_library(
@@ -42,7 +54,11 @@ pl_cc_library(
4254
],
4355
),
4456
hdrs = glob(["*.h"]),
45-
defines = select({
57+
# local_defines, not defines: the macro only guards this package's sources,
58+
# so there is no reason to push it onto every transitive dependent's
59+
# compilation. Targets that need to compile against the disabled build
60+
# (direct_query_server_test below) select on the same config_setting.
61+
local_defines = select({
4662
":direct_query_disabled": ["PX_PEM_DIRECT_QUERY_DISABLED"],
4763
"//conditions:default": [],
4864
}),
@@ -88,6 +104,14 @@ pl_cc_test(
88104
pl_cc_test(
89105
name = "direct_query_server_test",
90106
srcs = ["direct_query_server_test.cc"],
107+
# The kill-switch assertions are `#ifdef PX_PEM_DIRECT_QUERY_DISABLED`, and
108+
# cc_library's local_defines deliberately do not reach here — so select on
109+
# the flag again, otherwise a `--//…:direct_query=false` run would silently
110+
# compile the enabled branch and skip the coverage entirely.
111+
defines = select({
112+
":direct_query_disabled": ["PX_PEM_DIRECT_QUERY_DISABLED"],
113+
"//conditions:default": [],
114+
}),
91115
deps = [
92116
":cc_library",
93117
"//src/api/proto/vizierpb:vizier_pl_cc_proto",

src/vizier/services/agent/pem/DIRECT_QUERY_SECURITY.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,15 @@ When `--direct_query_enabled=false`:
226226

227227
```bash
228228
bazel build //src/vizier/services/agent/pem:pem_image \
229-
--define=PX_PEM_DIRECT_QUERY=disabled
229+
--//src/vizier/services/agent/pem:direct_query=false
230230
```
231231

232-
When `PX_PEM_DIRECT_QUERY_DISABLED` is defined at compile time
233-
(propagated by the `:direct_query_disabled` `config_setting` in
234-
`src/vizier/services/agent/pem/BUILD.bazel`):
232+
The `:direct_query` `bool_flag` defaults to `True`, so ordinary builds
233+
compile the feature in and rely on the runtime flag above. Setting it to
234+
`false` matches the `:direct_query_disabled` `config_setting` in
235+
`src/vizier/services/agent/pem/BUILD.bazel`, which puts
236+
`PX_PEM_DIRECT_QUERY_DISABLED` on this package's compilations (and on the
237+
endpoint's test target). When that macro is defined:
235238

236239
- The **entire feature-bearing body** of `direct_query_server.cc` is
237240
excluded via `#ifndef`: no openssl HMAC, no rapidjson, no JWT

src/vizier/services/agent/pem/direct_query_server_test.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ TEST_F(DirectQueryServerExecTest, FailSoft_BrokerFailureToleratedByDirectQuery)
816816
// stand it up here; the visible contract is: the runtime flag's early return
817817
// short-circuits before line 1 of feature code runs.
818818
//
819-
// Compile-time toggle (PX_PEM_DIRECT_QUERY=disabled) is asserted in code
819+
// Compile-time toggle (--//src/vizier/services/agent/pem:direct_query=false) is asserted in code
820820
// below: when compiled with the macro, AuthenticateRequest and
821821
// DirectQueryServer::ExecuteScript both return
822822
// UNAUTHENTICATED/UNIMPLEMENTED unconditionally, independent of token or
@@ -852,7 +852,8 @@ TEST_F(DirectQueryServerTest, CompiledOut_NoToken_Unauthenticated) {
852852
TEST_F(DirectQueryServerTest, ToggleContract_DocumentBothLevels) {
853853
SUCCEED() << "Default build: runtime --direct_query_enabled gates port :50305 "
854854
"binding (pem_manager.cc:MaybeStartDirectQueryServer early-returns). "
855-
"Compile-time PX_PEM_DIRECT_QUERY=disabled additionally drops all "
855+
"Compile-time --//src/vizier/services/agent/pem:direct_query=false "
856+
"additionally drops all "
856857
"feature bytes from the binary (no JWT verifier, no Carnot driver, "
857858
"no openssl/rapidjson includes). See DIRECT_QUERY_SECURITY.md.";
858859
}

0 commit comments

Comments
 (0)