Skip to content

Commit 5a352da

Browse files
committed
...
1 parent 64e31da commit 5a352da

File tree

3 files changed

+60
-14
lines changed

3 files changed

+60
-14
lines changed

README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,24 +301,41 @@ created with a random display name and assigned to the workspace with the defaul
301301
Use the `account_groups` argument to assign the service principal to account groups, which have the required
302302
permissions to perform a specific action.
303303

304+
Example:
305+
306+
```python
307+
def test_run_as_lower_privilege_user(make_run_as, ws):
308+
run_as = make_run_as(account_groups=['account.group.name'])
309+
through_query = next(run_as.sql_fetch_all("SELECT CURRENT_USER() AS my_name"))
310+
me = ws.current_user.me()
311+
assert me.user_name != through_query.my_name
312+
```
313+
304314
Returned object has the following properties:
305315
* `ws`: Workspace client that is authenticated as the ephemeral service principal.
306316
* `sql_backend`: SQL backend that is authenticated as the ephemeral service principal.
307317
* `sql_exec`: Function to execute a SQL statement on behalf of the ephemeral service principal.
308318
* `sql_fetch_all`: Function to fetch all rows from a SQL statement on behalf of the ephemeral service principal.
309319
* `display_name`: Display name of the ephemeral service principal.
310320
* `application_id`: Application ID of the ephemeral service principal.
311-
* ... other fixtures are not currently available through the returned object yet, as it's quite complex to
312-
implement, but there's a possibility to add generic support for them in the future.
321+
* if you want to have other fixtures available in the context of the ephemeral service principal, you can override
322+
the [`ws` fixture](#ws-fixture) on the file level, which would make all workspace fixtures provided by this
323+
plugin to run as lower privilege ephemeral service principal. You cannot combine it with the account-admin-level
324+
principal you're using to create the ephemeral principal.
313325

314326
Example:
315327

316328
```python
317-
def test_run_as_lower_privilege_user(make_run_as, ws):
318-
run_as = make_run_as(account_groups=['account.group.name'])
319-
through_query = next(run_as.sql_fetch_all("SELECT CURRENT_USER() AS my_name"))
320-
me = ws.current_user.me()
321-
assert me.user_name != through_query.my_name
329+
from pytest import fixture
330+
331+
@fixture
332+
def ws(make_run_as):
333+
run_as = make_run_as(account_groups=['account.group.used.for.all.tests.in.this.file'])
334+
return run_as.ws
335+
336+
def test_creating_notebook_on_behalf_of_ephemeral_principal(make_notebook):
337+
notebook = make_notebook()
338+
assert notebook.exists()
322339
```
323340

324341
See also [`acc`](#acc-fixture), [`ws`](#ws-fixture), [`make_random`](#make_random-fixture), [`env_or_skip`](#env_or_skip-fixture), [`log_account_link`](#log_account_link-fixture).

src/databricks/labs/pytester/fixtures/iam.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,24 +250,41 @@ def make_run_as(acc: AccountClient, ws: WorkspaceClient, make_random, env_or_ski
250250
Use the `account_groups` argument to assign the service principal to account groups, which have the required
251251
permissions to perform a specific action.
252252
253+
Example:
254+
255+
```python
256+
def test_run_as_lower_privilege_user(make_run_as, ws):
257+
run_as = make_run_as(account_groups=['account.group.name'])
258+
through_query = next(run_as.sql_fetch_all("SELECT CURRENT_USER() AS my_name"))
259+
me = ws.current_user.me()
260+
assert me.user_name != through_query.my_name
261+
```
262+
253263
Returned object has the following properties:
254264
* `ws`: Workspace client that is authenticated as the ephemeral service principal.
255265
* `sql_backend`: SQL backend that is authenticated as the ephemeral service principal.
256266
* `sql_exec`: Function to execute a SQL statement on behalf of the ephemeral service principal.
257267
* `sql_fetch_all`: Function to fetch all rows from a SQL statement on behalf of the ephemeral service principal.
258268
* `display_name`: Display name of the ephemeral service principal.
259269
* `application_id`: Application ID of the ephemeral service principal.
260-
* ... other fixtures are not currently available through the returned object yet, as it's quite complex to
261-
implement, but there's a possibility to add generic support for them in the future.
270+
* if you want to have other fixtures available in the context of the ephemeral service principal, you can override
271+
the [`ws` fixture](#ws-fixture) on the file level, which would make all workspace fixtures provided by this
272+
plugin to run as lower privilege ephemeral service principal. You cannot combine it with the account-admin-level
273+
principal you're using to create the ephemeral principal.
262274
263275
Example:
264276
265277
```python
266-
def test_run_as_lower_privilege_user(make_run_as, ws):
267-
run_as = make_run_as(account_groups=['account.group.name'])
268-
through_query = next(run_as.sql_fetch_all("SELECT CURRENT_USER() AS my_name"))
269-
me = ws.current_user.me()
270-
assert me.user_name != through_query.my_name
278+
from pytest import fixture
279+
280+
@fixture
281+
def ws(make_run_as):
282+
run_as = make_run_as(account_groups=['account.group.used.for.all.tests.in.this.file'])
283+
return run_as.ws
284+
285+
def test_creating_notebook_on_behalf_of_ephemeral_principal(make_notebook):
286+
notebook = make_notebook()
287+
assert notebook.exists()
271288
```
272289
"""
273290

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from pytest import fixture
2+
3+
4+
@fixture
5+
def ws(make_run_as):
6+
run_as = make_run_as(account_groups=['role.labs.lsql.write'])
7+
return run_as.ws
8+
9+
10+
def test_creating_notebook_on_behalf_of_ephemeral_principal(make_notebook):
11+
notebook = make_notebook()
12+
assert notebook.exists()

0 commit comments

Comments
 (0)