Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fe7463a

Browse files
arwas11tswast
andauthoredMar 1, 2025
fix: warn if default cloud_function_service_account is used in remote_function (#1424)
* feat: warn if default cloud_function_service_account is used * fix docstring style * Update warning to check default as an input for cloud functions service account * update warning wording and test * Apply suggestions from code review * speed up warning test --------- Co-authored-by: Tim Sweña (Swast) <swast@google.com>
1 parent 32cddfe commit fe7463a

File tree

4 files changed

+100
-41
lines changed

4 files changed

+100
-41
lines changed
 

‎bigframes/functions/_function_session.py

+29
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
)
4747

4848
from bigframes import clients
49+
from bigframes import version as bigframes_version
4950

5051
if TYPE_CHECKING:
5152
from bigframes.session import Session
@@ -129,6 +130,14 @@ def remote_function(
129130
.. deprecated:: 0.0.1
130131
This is an internal method. Please use :func:`bigframes.pandas.remote_function` instead.
131132
133+
.. warning::
134+
To use remote functions with Bigframes 2.0 and onwards, please (preferred)
135+
set an explicit user-managed ``cloud_function_service_account`` or (discouraged)
136+
set ``cloud_function_service_account`` to use the Compute Engine service account
137+
by setting it to `"default"`.
138+
139+
See, https://cloud.google.com/functions/docs/securing/function-identity.
140+
132141
.. note::
133142
Please make sure following is setup before using this API:
134143
@@ -314,6 +323,26 @@ def remote_function(
314323

315324
session = cast(bigframes.session.Session, session or bpd.get_global_session())
316325

326+
# raise a UserWarning if user does not explicitly set cloud_function_service_account to a
327+
# user-managed cloud_function_service_account of to default
328+
msg = (
329+
"You have not explicitly set a user-managed `cloud_function_service_account`. "
330+
"Using the default Compute Engine service account. "
331+
"To use Bigframes 2.0, please explicitly set `cloud_function_service_account` "
332+
'either to a user-managed service account (preferred) or to `"default"` '
333+
"to use the Compute Engine service account (discouraged). "
334+
"See, https://cloud.google.com/functions/docs/securing/function-identity."
335+
)
336+
337+
if (
338+
bigframes_version.__version__.startswith("1.")
339+
and cloud_function_service_account is None
340+
):
341+
warnings.warn(msg, stacklevel=2, category=FutureWarning)
342+
343+
if cloud_function_service_account == "default":
344+
cloud_function_service_account = None
345+
317346
# A BigQuery client is required to perform BQ operations
318347
if not bigquery_client:
319348
bigquery_client = session.bqclient

‎bigframes/session/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,14 @@ def remote_function(
12111211
supports dataframe with column types ``Int64``/``Float64``/``boolean``/
12121212
``string``/``binary[pyarrow]``.
12131213
1214+
.. warning::
1215+
To use remote functions with Bigframes 2.0 and onwards, please (preferred)
1216+
set an explicit user-managed ``cloud_function_service_account`` or (discouraged)
1217+
set ``cloud_function_service_account`` to use the Compute Engine service account
1218+
by setting it to `"default"`.
1219+
1220+
See, https://cloud.google.com/functions/docs/securing/function-identity.
1221+
12141222
.. note::
12151223
Please make sure following is setup before using this API:
12161224

‎notebooks/remote_functions/remote_function_usecases.ipynb

+53-41
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
{
2525
"cell_type": "code",
26-
"execution_count": null,
26+
"execution_count": 1,
2727
"metadata": {},
2828
"outputs": [],
2929
"source": [
@@ -44,7 +44,7 @@
4444
},
4545
{
4646
"cell_type": "code",
47-
"execution_count": 21,
47+
"execution_count": 2,
4848
"metadata": {
4949
"id": "Y6QAttCqqMM0"
5050
},
@@ -55,7 +55,7 @@
5555
},
5656
{
5757
"cell_type": "code",
58-
"execution_count": 22,
58+
"execution_count": 3,
5959
"metadata": {
6060
"colab": {
6161
"base_uri": "https://localhost:8080/",
@@ -66,17 +66,21 @@
6666
},
6767
"outputs": [
6868
{
69-
"name": "stderr",
70-
"output_type": "stream",
71-
"text": [
72-
"/usr/local/google/home/shobs/code/bigframes1/venv/lib/python3.10/site-packages/IPython/core/interactiveshell.py:3550: UserWarning: Reading cached table from 2024-07-24 08:01:12.491984+00:00 to avoid incompatibilies with previous reads of this table. To read the latest version, set `use_cache=False` or close the current session with Session.close() or bigframes.pandas.close_session().\n",
73-
" exec(code_obj, self.user_global_ns, self.user_ns)\n"
74-
]
69+
"data": {
70+
"text/html": [
71+
"Query job 1f6094e9-1942-477c-9ce3-87a614d71294 is DONE. 0 Bytes processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:1f6094e9-1942-477c-9ce3-87a614d71294&page=queryresults\">Open Job</a>"
72+
],
73+
"text/plain": [
74+
"<IPython.core.display.HTML object>"
75+
]
76+
},
77+
"metadata": {},
78+
"output_type": "display_data"
7579
},
7680
{
7781
"data": {
7882
"text/html": [
79-
"Query job 9d155f10-e37a-4d20-b2ff-02868ecb58f4 is DONE. 582.8 kB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:9d155f10-e37a-4d20-b2ff-02868ecb58f4&page=queryresults\">Open Job</a>"
83+
"Query job ba19f29c-33d3-4f12-9605-ddeafb74918e is DONE. 582.8 kB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:ba19f29c-33d3-4f12-9605-ddeafb74918e&page=queryresults\">Open Job</a>"
8084
],
8185
"text/plain": [
8286
"<IPython.core.display.HTML object>"
@@ -88,7 +92,7 @@
8892
{
8993
"data": {
9094
"text/html": [
91-
"Query job 5a524e70-12dc-4116-b416-04570bbf754e is DONE. 82.0 kB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:5a524e70-12dc-4116-b416-04570bbf754e&page=queryresults\">Open Job</a>"
95+
"Query job dd1ff8be-700a-4ce5-91a0-31413f70cfad is DONE. 82.0 kB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:dd1ff8be-700a-4ce5-91a0-31413f70cfad&page=queryresults\">Open Job</a>"
9296
],
9397
"text/plain": [
9498
"<IPython.core.display.HTML object>"
@@ -125,49 +129,49 @@
125129
" </thead>\n",
126130
" <tbody>\n",
127131
" <tr>\n",
128-
" <th>36</th>\n",
129-
" <td>Reds</td>\n",
130-
" <td>Cubs</td>\n",
131-
" <td>159</td>\n",
132+
" <th>88</th>\n",
133+
" <td>Royals</td>\n",
134+
" <td>Athletics</td>\n",
135+
" <td>176</td>\n",
132136
" </tr>\n",
133137
" <tr>\n",
134-
" <th>358</th>\n",
138+
" <th>106</th>\n",
135139
" <td>Dodgers</td>\n",
136-
" <td>Diamondbacks</td>\n",
137-
" <td>223</td>\n",
140+
" <td>Giants</td>\n",
141+
" <td>216</td>\n",
138142
" </tr>\n",
139143
" <tr>\n",
140-
" <th>416</th>\n",
141-
" <td>Yankees</td>\n",
142-
" <td>White Sox</td>\n",
143-
" <td>216</td>\n",
144+
" <th>166</th>\n",
145+
" <td>Phillies</td>\n",
146+
" <td>Royals</td>\n",
147+
" <td>162</td>\n",
144148
" </tr>\n",
145149
" <tr>\n",
146-
" <th>523</th>\n",
147-
" <td>Rays</td>\n",
148-
" <td>Athletics</td>\n",
149-
" <td>187</td>\n",
150+
" <th>247</th>\n",
151+
" <td>Rangers</td>\n",
152+
" <td>Royals</td>\n",
153+
" <td>161</td>\n",
150154
" </tr>\n",
151155
" <tr>\n",
152-
" <th>594</th>\n",
153-
" <td>Pirates</td>\n",
154-
" <td>Brewers</td>\n",
155-
" <td>169</td>\n",
156+
" <th>374</th>\n",
157+
" <td>Athletics</td>\n",
158+
" <td>Astros</td>\n",
159+
" <td>161</td>\n",
156160
" </tr>\n",
157161
" </tbody>\n",
158162
"</table>\n",
159163
"</div>"
160164
],
161165
"text/plain": [
162-
" homeTeamName awayTeamName duration_minutes\n",
163-
"36 Reds Cubs 159\n",
164-
"358 Dodgers Diamondbacks 223\n",
165-
"416 Yankees White Sox 216\n",
166-
"523 Rays Athletics 187\n",
167-
"594 Pirates Brewers 169"
166+
" homeTeamName awayTeamName duration_minutes\n",
167+
"88 Royals Athletics 176\n",
168+
"106 Dodgers Giants 216\n",
169+
"166 Phillies Royals 162\n",
170+
"247 Rangers Royals 161\n",
171+
"374 Athletics Astros 161"
168172
]
169173
},
170-
"execution_count": 22,
174+
"execution_count": 3,
171175
"metadata": {},
172176
"output_type": "execute_result"
173177
}
@@ -216,7 +220,7 @@
216220
},
217221
{
218222
"cell_type": "code",
219-
"execution_count": 23,
223+
"execution_count": 4,
220224
"metadata": {
221225
"colab": {
222226
"base_uri": "https://localhost:8080/",
@@ -226,10 +230,18 @@
226230
"outputId": "19351206-116e-4da2-8ff0-f288b7745b27"
227231
},
228232
"outputs": [
233+
{
234+
"name": "stderr",
235+
"output_type": "stream",
236+
"text": [
237+
"/usr/local/google/home/arwas/src1/python-bigquery-dataframes/bigframes/functions/_function_session.py:335: UserWarning: You have not explicitly set a user-managed cloud_function_service_account. Using the default compute service account, {cloud_function_service_account}. To use Bigframes 2.0, please set an explicit user-managed cloud_function_service_account or set cloud_function_service_account explicitly to `default`.See, https://cloud.google.com/functions/docs/securing/function-identity.\n",
238+
" warnings.warn(msg, category=UserWarning)\n"
239+
]
240+
},
229241
{
230242
"data": {
231243
"text/html": [
232-
"Query job ec8d958d-93ef-45ae-8150-6ccfa8feb89a is DONE. 0 Bytes processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:ec8d958d-93ef-45ae-8150-6ccfa8feb89a&page=queryresults\">Open Job</a>"
244+
"Query job 7c021760-59c4-4f3a-846c-9693a4d16eef is DONE. 0 Bytes processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:7c021760-59c4-4f3a-846c-9693a4d16eef&page=queryresults\">Open Job</a>"
233245
],
234246
"text/plain": [
235247
"<IPython.core.display.HTML object>"
@@ -242,7 +254,7 @@
242254
"name": "stdout",
243255
"output_type": "stream",
244256
"text": [
245-
"Created cloud function 'projects/bigframes-dev/locations/us-central1/functions/bigframes-session54c8b0-e22dbecc9ec0374bda36bc23df3775b0-g8zp' and BQ remote function 'bigframes-dev._1b6c31ff1bcd5d2f6d86833cf8268317f1b12d57.bigframes_session54c8b0_e22dbecc9ec0374bda36bc23df3775b0_g8zp'.\n"
257+
"Created cloud function 'projects/bigframes-dev/locations/us-central1/functions/bigframes-sessionca6012-ca541a90249f8b62951f38b7aba6a711-49to' and BQ remote function 'bigframes-dev._ed1e4d0f7d41174ba506d34d15dccf040d13f69e.bigframes_sessionca6012_ca541a90249f8b62951f38b7aba6a711_49to'.\n"
246258
]
247259
}
248260
],
@@ -1430,7 +1442,7 @@
14301442
"name": "python",
14311443
"nbconvert_exporter": "python",
14321444
"pygments_lexer": "ipython3",
1433-
"version": "3.10.12"
1445+
"version": "3.9.19"
14341446
}
14351447
},
14361448
"nbformat": 4,

‎tests/system/large/functions/test_remote_function.py

+10
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,16 @@ def square_num(x):
13591359
)
13601360

13611361

1362+
def test_remote_function_warns_default_cloud_function_service_account():
1363+
project = "bigframes-dev-perf"
1364+
rf_session = bigframes.Session(context=bigframes.BigQueryOptions(project=project))
1365+
1366+
with pytest.warns(FutureWarning, match="You have not explicitly set a"):
1367+
rf_session.remote_function(
1368+
cloud_function_service_account=None, # Explicitly omit service account.
1369+
)
1370+
1371+
13621372
@pytest.mark.flaky(retries=2, delay=120)
13631373
def test_remote_function_with_gcf_cmek():
13641374
# TODO(shobs): Automate the following set-up during testing in the test project.

0 commit comments

Comments
 (0)
Failed to load comments.