Skip to content

Commit 69850e5

Browse files
committed
Changing Quicksight permissons to receive usernames istead of principals. #434
1 parent a70e5aa commit 69850e5

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

awswrangler/quicksight/_create.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import boto3
88

99
from awswrangler import _utils, exceptions, sts
10-
from awswrangler.quicksight._get_list import get_data_source_arn, get_dataset_id
10+
from awswrangler.quicksight._get_list import get_data_source_arn, get_dataset_id, list_users
1111
from awswrangler.quicksight._utils import extract_athena_query_columns, extract_athena_table_columns
1212

1313
_logger: logging.Logger = logging.getLogger(__name__)
@@ -52,13 +52,13 @@
5252
}
5353

5454

55-
def _generate_principal(user_name: str, account_id: str, region: str) -> str:
56-
user_name = user_name if "/" in user_name else f"default/{user_name}"
57-
return f"arn:aws:quicksight:{region}:{account_id}:user/{user_name}"
55+
def _usernames_to_arns(user_names: List[str], all_users: List[Dict[str, Any]]) -> List[str]:
56+
return [cast(str, u["Arn"]) for u in all_users if u.get("UserName") in user_names]
5857

5958

6059
def _generate_permissions(
6160
resource: str,
61+
namespace: str,
6262
account_id: str,
6363
boto3_session: boto3.Session,
6464
allowed_to_use: Optional[List[str]] = None,
@@ -68,26 +68,31 @@ def _generate_permissions(
6868
if (allowed_to_use is None) and (allowed_to_manage is None):
6969
return permissions
7070

71-
# Forcing same principal not be in both lists at the same time.
71+
# Forcing same user not be in both lists at the same time.
7272
if (allowed_to_use is not None) and (allowed_to_manage is not None):
7373
allowed_to_use = list(set(allowed_to_use) - set(allowed_to_manage))
7474

75-
region: str = _utils.get_region_from_session(boto3_session=boto3_session)
75+
all_users: List[Dict[str, Any]] = list_users(
76+
namespace=namespace, account_id=account_id, boto3_session=boto3_session
77+
)
78+
7679
if allowed_to_use is not None:
80+
allowed_arns: List[str] = _usernames_to_arns(user_names=allowed_to_use, all_users=all_users)
7781
permissions += [
7882
{
79-
"Principal": _generate_principal(user_name=user_name, account_id=account_id, region=region),
83+
"Principal": arn,
8084
"Actions": _ALLOWED_ACTIONS[resource]["allowed_to_use"],
8185
}
82-
for user_name in allowed_to_use
86+
for arn in allowed_arns
8387
]
8488
if allowed_to_manage is not None:
89+
allowed_arns = _usernames_to_arns(user_names=allowed_to_manage, all_users=all_users)
8590
permissions += [
8691
{
87-
"Principal": _generate_principal(user_name=user_name, account_id=account_id, region=region),
92+
"Principal": arn,
8893
"Actions": _ALLOWED_ACTIONS[resource]["allowed_to_manage"],
8994
}
90-
for user_name in allowed_to_manage
95+
for arn in allowed_arns
9196
]
9297
return permissions
9398

@@ -113,6 +118,7 @@ def create_athena_data_source(
113118
tags: Optional[Dict[str, str]] = None,
114119
account_id: Optional[str] = None,
115120
boto3_session: Optional[boto3.Session] = None,
121+
namespace: str = "default",
116122
) -> None:
117123
"""Create a QuickSight data source pointing to an Athena/Workgroup.
118124
@@ -140,6 +146,8 @@ def create_athena_data_source(
140146
If None, the account ID will be inferred from your boto3 session.
141147
boto3_session : boto3.Session(), optional
142148
Boto3 Session. The default boto3 session will be used if boto3_session receive None.
149+
namespace : str
150+
The namespace. Currently, you should set this to default.
143151
144152
Returns
145153
-------
@@ -172,6 +180,7 @@ def create_athena_data_source(
172180
boto3_session=session,
173181
allowed_to_use=allowed_to_use,
174182
allowed_to_manage=allowed_to_manage,
183+
namespace=namespace,
175184
)
176185
if permissions:
177186
args["Permissions"] = permissions
@@ -198,13 +207,14 @@ def create_athena_dataset(
198207
tags: Optional[Dict[str, str]] = None,
199208
account_id: Optional[str] = None,
200209
boto3_session: Optional[boto3.Session] = None,
210+
namespace: str = "default",
201211
) -> str:
202212
"""Create a QuickSight dataset.
203213
204214
Note
205215
----
206216
You will not be able to see the the dataset in the console
207-
if you not pass your user to one of the ``allowed_*`` arguments.
217+
if you not pass your username to one of the ``allowed_*`` arguments.
208218
209219
Note
210220
----
@@ -237,10 +247,10 @@ def create_athena_dataset(
237247
Key/Value collection to put on the Cluster.
238248
e.g. {"foo": "boo", "bar": "xoo"})
239249
allowed_to_use : optional
240-
List of principals that will be allowed to see and use the data source.
250+
List of usernames that will be allowed to see and use the data source.
241251
e.g. ["john", "Mary"]
242252
allowed_to_manage : optional
243-
List of principals that will be allowed to see, use, update and delete the data source.
253+
List of usernames that will be allowed to see, use, update and delete the data source.
244254
e.g. ["Mary"]
245255
logical_table_alias : str
246256
A display name for the logical table.
@@ -253,6 +263,8 @@ def create_athena_dataset(
253263
If None, the account ID will be inferred from your boto3 session.
254264
boto3_session : boto3.Session(), optional
255265
Boto3 Session. The default boto3 session will be used if boto3_session receive None.
266+
namespace : str
267+
The namespace. Currently, you should set this to default.
256268
257269
Returns
258270
-------
@@ -333,6 +345,7 @@ def create_athena_dataset(
333345
boto3_session=session,
334346
allowed_to_use=allowed_to_use,
335347
allowed_to_manage=allowed_to_manage,
348+
namespace=namespace,
336349
)
337350
if permissions:
338351
args["Permissions"] = permissions

awswrangler/quicksight/_get_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def list_users(
350350
Parameters
351351
----------
352352
namespace : str
353-
The namespace. Currently, you should set this to default .
353+
The namespace. Currently, you should set this to default.
354354
account_id : str, optional
355355
If None, the account ID will be inferred from your boto3 session.
356356
boto3_session : boto3.Session(), optional

0 commit comments

Comments
 (0)