-
Notifications
You must be signed in to change notification settings - Fork 446
[tabcmd] fix: CSVImport bugs (AUTH column, case-insensitive validation, username case) #1811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
948e382
fix: CSVImport AUTH column, case-insensitive validation, username pre…
jacalata 0b61917
Merge origin/development into jac/csv-import-fixes-and-find-by-name
jacalata dfd5541
fix: raise on unknown AUTH in CSV import and route auth through prope…
jacalata 2515664
revert: move find_by_name to its own PR
jacalata 95c75da
docs: changelog entry for CSVImport username case-preservation
jacalata 117cbac
refactor: CSVImport MAX/auth cleanup from adversarial review
jacalata b088d3d
fix: don't enforce Auth enum on server-parsed UserItems
jacalata 5042949
Merge branch 'development' into jac/csv-import-fixes-and-find-by-name
jacalata 4b9eb47
Use ValueError on 'too many columns'; drop stale enum-guard test
jacalata 9618c26
fix: raise ValueError (not AttributeError) on invalid CSV column value
jacalata 91dd6e3
chore: address fresh-eyes review nits
jacalata File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -308,7 +308,10 @@ def _set_values( | |
| if email: | ||
| self.email = email | ||
| if auth_setting: | ||
| self._auth_setting = auth_setting | ||
| # Go through the @property_is_enum(Auth) setter rather than writing | ||
| # to _auth_setting directly, so CSV-parsed users can't carry an | ||
| # invalid auth_setting that only fails later at the API call. | ||
| self.auth_setting = auth_setting | ||
| if domain_name: | ||
| self._domain_name = domain_name | ||
| if locale: | ||
|
|
@@ -432,36 +435,59 @@ class ColumnType(IntEnum): | |
| EMAIL = 6 | ||
| AUTH = 7 | ||
|
|
||
| MAX = 7 | ||
| # Total number of columns supported by the import format. Held outside | ||
| # the ColumnType enum so it can't be mistaken for a real column index. | ||
| COLUMN_COUNT = 8 | ||
|
|
||
| # Lowercase -> canonical form mapping for the AUTH column. Class-level | ||
| # so the dict isn't rebuilt on every call to create_user_from_line / | ||
| # _validate_import_line_or_throw. The set of accepted values is derived | ||
| # from this map (see _valid_attributes[AUTH]) so there's a single | ||
| # source of truth. | ||
| _AUTH_CANONICAL: dict[str, str] = { | ||
| "saml": "SAML", | ||
| "openid": "OpenID", | ||
| "serverdefault": "ServerDefault", | ||
| "tableauidwithmfa": "TableauIDWithMFA", | ||
| } | ||
|
|
||
| # Read a csv line and create a user item populated by the given attributes | ||
| @staticmethod | ||
| def create_user_from_line(line: str): | ||
| if line is None or line is False or line == "\n" or line == "": | ||
| return None | ||
| line = line.strip().lower() | ||
| values: list[str] = list(map(str.strip, line.split(","))) | ||
| user = UserItem(values[UserItem.CSVImport.ColumnType.USERNAME]) | ||
| values: list[str] = list(map(str.strip, line.strip().split(","))) | ||
| if len(values) > UserItem.CSVImport.COLUMN_COUNT: | ||
| raise ValueError("Too many attributes for user import") | ||
| username = values[UserItem.CSVImport.ColumnType.USERNAME] | ||
| user = UserItem(username) | ||
| if len(values) > 1: | ||
| if len(values) > UserItem.CSVImport.ColumnType.MAX: | ||
| raise ValueError("Too many attributes for user import") | ||
| while len(values) <= UserItem.CSVImport.ColumnType.MAX: | ||
| while len(values) < UserItem.CSVImport.COLUMN_COUNT: | ||
| values.append("") | ||
| site_role = UserItem.CSVImport._evaluate_site_role( | ||
| values[UserItem.CSVImport.ColumnType.LICENSE], | ||
| values[UserItem.CSVImport.ColumnType.ADMIN], | ||
| values[UserItem.CSVImport.ColumnType.PUBLISHER], | ||
| ) | ||
|
|
||
| raw_auth = values[UserItem.CSVImport.ColumnType.AUTH] | ||
| if raw_auth: | ||
| auth = UserItem.CSVImport._AUTH_CANONICAL.get(raw_auth.lower()) | ||
| if auth is None: | ||
| raise ValueError( | ||
| f"Unknown auth setting: {raw_auth!r}. " | ||
| f"Valid values: {sorted(UserItem.CSVImport._AUTH_CANONICAL.values())}" | ||
| ) | ||
| else: | ||
| auth = None | ||
| user._set_values( | ||
| None, | ||
| values[UserItem.CSVImport.ColumnType.USERNAME], | ||
| username, | ||
| site_role, | ||
| None, | ||
| None, | ||
| values[UserItem.CSVImport.ColumnType.DISPLAY_NAME], | ||
| values[UserItem.CSVImport.ColumnType.EMAIL], | ||
| values[UserItem.CSVImport.ColumnType.AUTH], | ||
| auth, | ||
| None, | ||
| None, | ||
| None, | ||
|
|
@@ -493,6 +519,8 @@ def validate_file_for_import(csv_file: io.TextIOWrapper, logger) -> tuple[int, l | |
| # Iterate through each field and validate the given value against hardcoded constraints | ||
| @staticmethod | ||
| def _validate_import_line_or_throw(incoming, logger) -> None: | ||
| # AUTH column's valid set is derived from _AUTH_CANONICAL so there's | ||
| # one source of truth for the accepted values. | ||
| _valid_attributes: list[list[str]] = [ | ||
| [], | ||
| [], | ||
|
|
@@ -501,20 +529,26 @@ def _validate_import_line_or_throw(incoming, logger) -> None: | |
| ["system", "site", "none", "no"], # admin | ||
| ["yes", "true", "1", "no", "false", "0"], # publisher | ||
| [], | ||
| [UserItem.Auth.SAML, UserItem.Auth.OpenID, UserItem.Auth.ServerDefault], # auth | ||
| list(UserItem.CSVImport._AUTH_CANONICAL.values()), # auth — normalized before comparison | ||
| ] | ||
|
|
||
| line = list(map(str.strip, incoming.split(","))) | ||
| if len(line) > UserItem.CSVImport.ColumnType.MAX: | ||
| if len(line) > UserItem.CSVImport.COLUMN_COUNT: | ||
| raise AttributeError("Too many attributes in line") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, fixed in 9618c26. Aligning on |
||
| username = line[UserItem.CSVImport.ColumnType.USERNAME.value] | ||
| logger.debug(f"> details - {username}") | ||
| UserItem.validate_username_or_throw(username) | ||
| for i in range(1, len(line)): | ||
| logger.debug(f"column {UserItem.CSVImport.ColumnType(i).name}: {line[i]}") | ||
| UserItem.CSVImport._validate_attribute_value( | ||
| line[i], _valid_attributes[i], UserItem.CSVImport.ColumnType(i) | ||
| ) | ||
| value = line[i] | ||
| valid = _valid_attributes[i] | ||
| # normalize case for fields with a restricted value set | ||
| if valid: | ||
| if i == UserItem.CSVImport.ColumnType.AUTH: | ||
| value = UserItem.CSVImport._AUTH_CANONICAL.get(value.lower(), value) | ||
| else: | ||
| value = value.lower() | ||
| logger.debug(f"column {UserItem.CSVImport.ColumnType(i).name}: {value}") | ||
| UserItem.CSVImport._validate_attribute_value(value, valid, UserItem.CSVImport.ColumnType(i)) | ||
|
|
||
| # Given a restricted set of possible values, confirm the item is in that set | ||
| @staticmethod | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.