Skip to content

Commit c848a80

Browse files
shobsitswast
andauthored
feat!: change default ingress setting for remote_function to internal-only (#1544)
* feat!: change default ingress setting for `remote_function` to internal-only * fix ingress setting test * use "all" ingress setting for some remote function tests --------- Co-authored-by: Tim Sweña (Swast) <[email protected]>
1 parent 9eb9089 commit c848a80

File tree

5 files changed

+43
-50
lines changed

5 files changed

+43
-50
lines changed

bigframes/functions/_function_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def create_cloud_function(
365365
is_row_processor=False,
366366
vpc_connector=None,
367367
memory_mib=1024,
368-
ingress_settings="all",
368+
ingress_settings="internal-only",
369369
):
370370
"""Create a cloud function from the given user defined function."""
371371

bigframes/functions/_function_session.py

+9-21
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ def remote_function(
260260
cloud_function_max_instances: Optional[int] = None,
261261
cloud_function_vpc_connector: Optional[str] = None,
262262
cloud_function_memory_mib: Optional[int] = 1024,
263-
cloud_function_ingress_settings: Optional[
264-
Literal["all", "internal-only", "internal-and-gclb"]
265-
] = None,
263+
cloud_function_ingress_settings: Literal[
264+
"all", "internal-only", "internal-and-gclb"
265+
] = "internal-only",
266266
):
267267
"""Decorator to turn a user defined function into a BigQuery remote function.
268268
@@ -449,8 +449,9 @@ def remote_function(
449449
https://cloud.google.com/functions/docs/configuring/memory.
450450
cloud_function_ingress_settings (str, Optional):
451451
Ingress settings controls dictating what traffic can reach the
452-
function. By default `all` will be used. It must be one of:
453-
`all`, `internal-only`, `internal-and-gclb`. See for more details
452+
function. Options are: `all`, `internal-only`, or `internal-and-gclb`.
453+
If no setting is provided, `internal-only` will be used by default.
454+
See for more details
454455
https://cloud.google.com/functions/docs/networking/network-settings#ingress_settings.
455456
"""
456457
# Some defaults may be used from the session if not provided otherwise.
@@ -507,24 +508,11 @@ def remote_function(
507508
)
508509

509510
if cloud_function_ingress_settings is None:
510-
cloud_function_ingress_settings = "all"
511-
msg = bfe.format_message(
512-
"The `cloud_function_ingress_settings` are set to 'all' by default, "
513-
"which will change to 'internal-only' for enhanced security in future version 2.0 onwards. "
514-
"However, you will be able to explicitly pass cloud_function_ingress_settings='all' if you need. "
515-
"See https://cloud.google.com/functions/docs/networking/network-settings#ingress_settings for details."
516-
)
517-
warnings.warn(msg, category=FutureWarning, stacklevel=2)
518-
519-
if cloud_function_ingress_settings is None:
520-
cloud_function_ingress_settings = "all"
511+
cloud_function_ingress_settings = "internal-only"
521512
msg = bfe.format_message(
522-
"The `cloud_function_ingress_settings` are set to 'all' by default, "
523-
"which will change to 'internal-only' for enhanced security in future version 2.0 onwards. "
524-
"However, you will be able to explicitly pass cloud_function_ingress_settings='all' if you need. "
525-
"See https://cloud.google.com/functions/docs/networking/network-settings#ingress_settings for details."
513+
"The `cloud_function_ingress_settings` is being set to 'internal-only' by default."
526514
)
527-
warnings.warn(msg, category=FutureWarning, stacklevel=2)
515+
warnings.warn(msg, category=UserWarning, stacklevel=2)
528516

529517
bq_connection_manager = session.bqconnectionmanager
530518

bigframes/pandas/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ def remote_function(
8181
cloud_function_max_instances: Optional[int] = None,
8282
cloud_function_vpc_connector: Optional[str] = None,
8383
cloud_function_memory_mib: Optional[int] = 1024,
84-
cloud_function_ingress_settings: Optional[
85-
Literal["all", "internal-only", "internal-and-gclb"]
86-
] = None,
84+
cloud_function_ingress_settings: Literal[
85+
"all", "internal-only", "internal-and-gclb"
86+
] = "internal-only",
8787
):
8888
return global_session.with_default_session(
8989
bigframes.session.Session.remote_function,

bigframes/session/__init__.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1218,9 +1218,9 @@ def remote_function(
12181218
cloud_function_max_instances: Optional[int] = None,
12191219
cloud_function_vpc_connector: Optional[str] = None,
12201220
cloud_function_memory_mib: Optional[int] = 1024,
1221-
cloud_function_ingress_settings: Optional[
1222-
Literal["all", "internal-only", "internal-and-gclb"]
1223-
] = None,
1221+
cloud_function_ingress_settings: Literal[
1222+
"all", "internal-only", "internal-and-gclb"
1223+
] = "internal-only",
12241224
):
12251225
"""Decorator to turn a user defined function into a BigQuery remote function. Check out
12261226
the code samples at: https://cloud.google.com/bigquery/docs/remote-functions#bigquery-dataframes.
@@ -1393,8 +1393,8 @@ def remote_function(
13931393
cloud_function_ingress_settings (str, Optional):
13941394
Ingress settings controls dictating what traffic can reach the
13951395
function. Options are: `all`, `internal-only`, or `internal-and-gclb`.
1396-
If no setting is provided, `all` will be used by default and a warning
1397-
will be issued. See for more details
1396+
If no setting is provided, `internal-only` will be used by default.
1397+
See for more details
13981398
https://cloud.google.com/functions/docs/networking/network-settings#ingress_settings.
13991399
Returns:
14001400
collections.abc.Callable:

tests/system/large/functions/test_remote_function.py

+25-20
Original file line numberDiff line numberDiff line change
@@ -1336,17 +1336,27 @@ def test_remote_function_via_session_custom_sa(scalars_dfs):
13361336

13371337
try:
13381338

1339+
# TODO(shobs): Figure out why the default ingress setting
1340+
# (internal-only) does not work here
13391341
@rf_session.remote_function(
13401342
input_types=[int],
13411343
output_type=int,
13421344
reuse=False,
13431345
cloud_function_service_account=gcf_service_account,
1346+
cloud_function_ingress_settings="all",
13441347
)
13451348
def square_num(x):
13461349
if x is None:
13471350
return x
13481351
return x * x
13491352

1353+
# assert that the GCF is created with the intended SA
1354+
gcf = rf_session.cloudfunctionsclient.get_function(
1355+
name=square_num.bigframes_cloud_function
1356+
)
1357+
assert gcf.service_config.service_account_email == gcf_service_account
1358+
1359+
# assert that the function works as expected on data
13501360
scalars_df, scalars_pandas_df = scalars_dfs
13511361

13521362
bf_int64_col = scalars_df["int64_col"]
@@ -1358,12 +1368,6 @@ def square_num(x):
13581368
pd_result = pd_int64_col.to_frame().assign(result=pd_result_col)
13591369

13601370
assert_pandas_df_equal(bf_result, pd_result, check_dtype=False)
1361-
1362-
# Assert that the GCF is created with the intended SA
1363-
gcf = rf_session.cloudfunctionsclient.get_function(
1364-
name=square_num.bigframes_cloud_function
1365-
)
1366-
assert gcf.service_config.service_account_email == gcf_service_account
13671371
finally:
13681372
# clean up the gcp assets created for the remote function
13691373
cleanup_function_assets(
@@ -1476,14 +1480,24 @@ def square_num(x):
14761480
return x
14771481
return x * x
14781482

1483+
# TODO(shobs): See if the test vpc can be configured to make this flow
1484+
# work with the default ingress setting (internal-only)
14791485
square_num_remote = rf_session.remote_function(
14801486
input_types=[int],
14811487
output_type=int,
14821488
reuse=False,
14831489
cloud_function_service_account="default",
14841490
cloud_function_vpc_connector=gcf_vpc_connector,
1491+
cloud_function_ingress_settings="all",
14851492
)(square_num)
14861493

1494+
# assert that the GCF is created with the intended vpc connector
1495+
gcf = rf_session.cloudfunctionsclient.get_function(
1496+
name=square_num_remote.bigframes_cloud_function
1497+
)
1498+
assert gcf.service_config.vpc_connector == gcf_vpc_connector
1499+
1500+
# assert that the function works as expected on data
14871501
scalars_df, scalars_pandas_df = scalars_dfs
14881502

14891503
bf_int64_col = scalars_df["int64_col"]
@@ -1495,12 +1509,6 @@ def square_num(x):
14951509
pd_result = pd_int64_col.to_frame().assign(result=pd_result_col)
14961510

14971511
assert_pandas_df_equal(bf_result, pd_result, check_dtype=False)
1498-
1499-
# Assert that the GCF is created with the intended vpc connector
1500-
gcf = rf_session.cloudfunctionsclient.get_function(
1501-
name=square_num_remote.bigframes_cloud_function
1502-
)
1503-
assert gcf.service_config.vpc_connector == gcf_vpc_connector
15041512
finally:
15051513
# clean up the gcp assets created for the remote function
15061514
cleanup_function_assets(
@@ -2439,13 +2447,13 @@ def generate_stats(row: pandas.Series) -> list[int]:
24392447
[
24402448
pytest.param(
24412449
{},
2442-
functions_v2.ServiceConfig.IngressSettings.ALLOW_ALL,
2443-
True,
2450+
functions_v2.ServiceConfig.IngressSettings.ALLOW_INTERNAL_ONLY,
2451+
False,
24442452
id="no-set",
24452453
),
24462454
pytest.param(
24472455
{"cloud_function_ingress_settings": None},
2448-
functions_v2.ServiceConfig.IngressSettings.ALLOW_ALL,
2456+
functions_v2.ServiceConfig.IngressSettings.ALLOW_INTERNAL_ONLY,
24492457
True,
24502458
id="set-none",
24512459
),
@@ -2493,11 +2501,8 @@ def square(x: int) -> int:
24932501
default_ingress_setting_warnings = [
24942502
warn
24952503
for warn in record
2496-
if isinstance(warn.message, FutureWarning)
2497-
and "`cloud_function_ingress_settings` are set to 'all' by default"
2498-
in warn.message.args[0]
2499-
and "will change to 'internal-only' for enhanced security in future"
2500-
in warn.message.args[0]
2504+
if isinstance(warn.message, UserWarning)
2505+
and "The `cloud_function_ingress_settings` is being set to 'internal-only' by default."
25012506
]
25022507
assert len(default_ingress_setting_warnings) == (
25032508
1 if expect_default_ingress_setting_warning else 0

0 commit comments

Comments
 (0)