-
Notifications
You must be signed in to change notification settings - Fork 13
ENT-12739: Added detection of the user providing wrong policy set path in cfbs analyze #222
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 all commits
Commits
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |||||||||||||||||||||||||||||||
| from cfbs.utils import ( | ||||||||||||||||||||||||||||||||
| FetchError, | ||||||||||||||||||||||||||||||||
| cfbs_dir, | ||||||||||||||||||||||||||||||||
| deduplicate_list, | ||||||||||||||||||||||||||||||||
| fetch_url, | ||||||||||||||||||||||||||||||||
| file_sha256, | ||||||||||||||||||||||||||||||||
| get_json, | ||||||||||||||||||||||||||||||||
|
|
@@ -25,14 +26,20 @@ def path_components(path): | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| The first component is `""` for a path starting with a separator. On Windows, if `path` begins with n backslashes, the first n components will be `""`. | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| The last component is the filename, trailing separators do not affect the result.""" | ||||||||||||||||||||||||||||||||
| The last component is the name of the file or directory, trailing separators do not affect the result. | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| norm_path = os.path.normpath(path) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| dir_components = norm_path.split(os.sep) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return dir_components | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def name(path): | ||||||||||||||||||||||||||||||||
| """Returns the name of the path to file or directory.""" | ||||||||||||||||||||||||||||||||
| return path_components(path)[-1] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def is_path_component(path, component): | ||||||||||||||||||||||||||||||||
| """Returns whether `component` is a path component of `path`.""" | ||||||||||||||||||||||||||||||||
| p_components = path_components(path) | ||||||||||||||||||||||||||||||||
|
|
@@ -551,8 +558,51 @@ def analyze_policyset( | |||||||||||||||||||||||||||||||
| if reference_version is None: | ||||||||||||||||||||||||||||||||
| reference_version = versions_data.version_counter.most_common_version() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # if not a single file in the analyzed policyset has an MPF-known checksum, and a specific `reference_version` was not given, `reference_version` will still be `None` | ||||||||||||||||||||||||||||||||
| # if not a single file in the analyzed policyset has an MPF-known (checksum, filepath), and a specific `reference_version` was not given, `reference_version` will still be `None` | ||||||||||||||||||||||||||||||||
| if reference_version is None: | ||||||||||||||||||||||||||||||||
| # try to detect whether the user provided a wrong policy set path | ||||||||||||||||||||||||||||||||
| # gather all possible policy set paths, by detecting promises.cf or update.cf | ||||||||||||||||||||||||||||||||
| possible_policyset_relpaths = [] | ||||||||||||||||||||||||||||||||
| mpfnorm_path = mpf_normalized_path(path, is_parentpath, masterfiles_dir) | ||||||||||||||||||||||||||||||||
| for filepath in files_dict: | ||||||||||||||||||||||||||||||||
| file_name = name(filepath) | ||||||||||||||||||||||||||||||||
| if file_name in ("promises.cf", "update.cf"): | ||||||||||||||||||||||||||||||||
| relpath = os.path.relpath(filepath, mpfnorm_path) | ||||||||||||||||||||||||||||||||
| relpath = relpath.replace(os.sep, "/") | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if "/" not in relpath: | ||||||||||||||||||||||||||||||||
| # `"."`, not `path`, as the list collects relative paths | ||||||||||||||||||||||||||||||||
| possible_policyset_relpaths.append(".") | ||||||||||||||||||||||||||||||||
| if relpath.endswith("/update.cf"): | ||||||||||||||||||||||||||||||||
| possible_policyset_relpaths.append(relpath[:-10]) | ||||||||||||||||||||||||||||||||
| if relpath.endswith("/promises.cf"): | ||||||||||||||||||||||||||||||||
| possible_policyset_relpaths.append(relpath[:-12]) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # for drive root, the path's parent is the path itself, so only check the parent path if this is not the case | ||||||||||||||||||||||||||||||||
| if os.path.realpath(path) != os.path.realpath(os.path.join(path, "..")): | ||||||||||||||||||||||||||||||||
| if os.path.exists(os.path.join(path, "..", "update.cf")) or os.path.exists( | ||||||||||||||||||||||||||||||||
| os.path.join(path, "..", "promises.cf") | ||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||
| possible_policyset_relpaths.append("..") | ||||||||||||||||||||||||||||||||
|
Comment on lines
+581
to
+586
Member
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.
This part is a bit cryptic, and could be more readable, I think, something like this;
Suggested change
That might even make the comment unnecessary (which is a good thing). Also, importing the functions you need from |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| possible_policyset_relpaths = deduplicate_list(possible_policyset_relpaths) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # check whether the policy set contains update.cf or promises.cf directly in masterfiles | ||||||||||||||||||||||||||||||||
| if not ( | ||||||||||||||||||||||||||||||||
| (masterfiles_dir if is_parentpath else ".") in possible_policyset_relpaths | ||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||
| extra_error_text = "" | ||||||||||||||||||||||||||||||||
| if len(possible_policyset_relpaths) > 0: | ||||||||||||||||||||||||||||||||
| extra_error_text = ( | ||||||||||||||||||||||||||||||||
| "Did you mean to provide one of the following paths (or their parent paths), relative to the provided path, instead?:\n " | ||||||||||||||||||||||||||||||||
| + "\n ".join(possible_policyset_relpaths) | ||||||||||||||||||||||||||||||||
| + "\n" | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
| user_error( | ||||||||||||||||||||||||||||||||
| "There doesn't seem to be a valid policy set in the supplied path.\n Usage: cfbs analyze path/to/policy-set\n" | ||||||||||||||||||||||||||||||||
| + extra_error_text | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| reference_version_files = [] | ||||||||||||||||||||||||||||||||
| reference_version_checksums = {} | ||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is usually referred to as
basename. Maybe useos.path.basename()instead, or if you need some slightly different behavior, make a wrapper calledbasename()which does what you want.