Skip to content
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

Fix directory permissions for multi-directory globs. Follow-up #50559 #52839

Merged

Conversation

zvonand
Copy link
Contributor

@zvonand zvonand commented Jul 31, 2023

Follow-up for #50559.

Add setting ignore_access_denied_multidirectory_globs to avoid permission denied in case there are inaccessible directories/files.

Explanation

Having the following structure in user_files:

user_files/
├── data1
│   ├── f1.csv
├── data2
│   ├── f2.csv
└── test_root

Where data1, data2 are accessible by CH, but no rights to read test_root.
CH would throw:
Code: 1001. DB::Exception: std::__1::__fs::filesystem::filesystem_error: filesystem error: in directory_iterator::directory_iterator(...): Permission denied
for a query like
SELECT *, _path, _file FROM file('{data1/f1,data2/f2}.csv', CSV).

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Add setting ignore_access_denied_multidirectory_globs.

@zvonand zvonand marked this pull request as ready for review July 31, 2023 22:08
@SmitaRKulkarni SmitaRKulkarni added the can be tested Allows running workflows for external contributors label Aug 1, 2023
@robot-clickhouse robot-clickhouse added the pr-not-for-changelog This PR should not be mentioned in the changelog label Aug 1, 2023
@SmitaRKulkarni SmitaRKulkarni self-assigned this Aug 1, 2023
@robot-clickhouse
Copy link
Member

robot-clickhouse commented Aug 1, 2023

This is an automated comment for commit 9e89055 with description of existing statuses. It's updated for the latest CI running

✅ Click here to open a full report in a separate page

Successful checks
Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help✅ success
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR✅ success
ClickHouse build checkBuilds ClickHouse in various configurations for use in further steps. You have to fix the builds that fail. Build logs often has enough information to fix the error, but you might have to reproduce the failure locally. The cmake options can be found in the build log, grepping for cmake. Use these options and follow the general build process✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker image for serversThe check to build and optionally push the mentioned image to docker hub✅ success
Docs CheckBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integrational tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests✅ success
Mergeable CheckChecks if all other necessary checks are successful✅ success
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests✅ success
Push to DockerhubThe check for building and pushing the CI related docker images to docker hub✅ success
SQLTestThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
SQLancerFuzzing tests that detect logical bugs with SQLancer tool✅ success
SqllogicRun clickhouse on the sqllogic test set against sqlite and checks that all statements are passed✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style CheckRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts✅ success

@zvonand zvonand changed the title One-line fix: add skip_permission_denied for multi-directory globs, follow up #50559 Add skip_permission_denied for multi-directory globs. Follow-up #50559 Aug 1, 2023
@zvonand

This comment was marked as outdated.

@zvonand

This comment was marked as outdated.

@@ -121,7 +121,7 @@ void listFilesWithFoldedRegexpMatchingImpl(const std::string & path_for_ls,
return;

const fs::directory_iterator end;
for (fs::directory_iterator it(path_for_ls); it != end; ++it)
for (fs::directory_iterator it(path_for_ls, fs::directory_options::skip_permission_denied); it != end; ++it)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zvonand : In the example, SELECT *, _path, _file FROM file('{data1/f1,data2/f2}.csv', CSV) if user doesn't have rights to data2 looks like we would just skip it without throwing any error.

Copy link
Contributor Author

@zvonand zvonand Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you're right, didn't think of this.
I'll take a look again.

Will think of a nice way to check if the checked path matches the provided glob. If not -- ignore error, otherwise throw it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out it's not an easy thing to implement, and I'm not sure it is worth it.
AFAIK there is no easy and good way to match string to be a prefix of regex. This would require introducing a rather complex FSM into the code. It may also be very slow, as it would be a lot of creating and modifying strings.

The question is how critical this silent ignore is?
Globs already work like this, even w/o my previous PR: for a query like SELECT *, _path, _file FROM file('{data1,data2}/{f1,f2}.csv', CSV) there will be no error thrown in case one of specified files or directories don't exist. If at least one matching file exists, it will keep silence.

Copy link
Contributor Author

@zvonand zvonand Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SmitaRKulkarni

IMO we could append the docs with something like a glob with '/' in curly braces will ignore directories that cannot be accessed.

I don't think that making the code twice as unreadable and three times as complex is a good idea for such a small issue.
Also, it doesn't affect old workflow, it is only about {foo/bar,x/y/z} globs.

I think it is fine to set some limits for a newly introduced feature :)

@zvonand zvonand marked this pull request as draft August 3, 2023 10:15
@zvonand zvonand changed the title Add skip_permission_denied for multi-directory globs. Follow-up #50559 Fix directory permissions for multi-directory globs. Follow-up #50559 Aug 3, 2023
@zvonand zvonand marked this pull request as ready for review August 10, 2023 10:30
@alexey-milovidov
Copy link
Member

Throwing an exception is better than silently ignoring the files.
You can introduce a new behavior under a setting, but it should be disabled by default.

@zvonand
Copy link
Contributor Author

zvonand commented Aug 10, 2023

Throwing an exception is better than silently ignoring the files.

In general, sure it is. But this limits the use cases, as there are often inaccessible directories where you don't need to go :).

Also, I added a docs update to this PR. It does not affect behavior prior to #50559. So I don't think it is a big deal to modify a thing that no one has used yet.

However, a setting may be a good idea. But there are already tons of them, so I'm afraid it would be difficult to find :)

@robot-ch-test-poll1 robot-ch-test-poll1 added pr-improvement Pull request with some product improvements and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels Aug 11, 2023
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-status-✅ PR with only success statuses label Sep 14, 2023
@robot-ch-test-poll2 robot-ch-test-poll2 added pr-status-⏳ PR with some pending statuses pr-status-❌ PR with some error/faliure statuses and removed pr-status-✅ PR with only success statuses pr-status-⏳ PR with some pending statuses labels Sep 14, 2023
@robot-clickhouse robot-clickhouse added pr-status-⏳ PR with some pending statuses and removed pr-status-❌ PR with some error/faliure statuses labels Sep 15, 2023
@robot-ch-test-poll robot-ch-test-poll added pr-status-✅ PR with only success statuses and removed pr-status-⏳ PR with some pending statuses labels Sep 15, 2023
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-status-❌ PR with some error/faliure statuses and removed pr-status-✅ PR with only success statuses labels Sep 15, 2023
@robot-clickhouse robot-clickhouse added pr-status-⏳ PR with some pending statuses and removed pr-status-❌ PR with some error/faliure statuses labels Sep 15, 2023
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added pr-status-✅ PR with only success statuses and removed pr-status-⏳ PR with some pending statuses labels Sep 15, 2023
@alexey-milovidov
Copy link
Member

In the example from the description:

Where data1, data2 are accessible by CH, but no rights to read test_root.
SELECT *, _path, _file FROM file('{data1/f1,data2/f2}.csv', CSV).

Why this query will even attempt to access test_root?

@zvonand
Copy link
Contributor Author

zvonand commented Sep 16, 2023

Why this query will even attempt to access test_root?

See this comment

tldr: because there is no way to match a regex prefix against a string (kind of "reverse" matching -- match a prefix of a regex against a given string)
Trying to do this means reinventing regex (constructing a fsm that parses regex manually, and that will definitely be big, non-optimal and error-prone for sure)

the only working approach is to just go recursively inside all directories that are there (until some estimated depth limit is hit) and match the path against a full regex

@alexey-milovidov
Copy link
Member

alexey-milovidov commented Sep 16, 2023

Ok. So, we need to traverse the whole directory tree to match by paths (rather than by separate components). That's understandable.

@alexey-milovidov
Copy link
Member

But it is still a problem - we are doing it the wrong way.

{x,y} - this is a generator, not a matcher.
It should iteratively generate x and y instead of constructing a regexp and matching by regexp.

@alexey-milovidov
Copy link
Member

We will have to rewrite all of this in the future and drop the setting you've introduced :(

@robot-clickhouse-ci-1 robot-clickhouse-ci-1 merged commit a878d10 into ClickHouse:master Sep 16, 2023
283 checks passed
@zvonand

This comment was marked as outdated.

@zvonand
Copy link
Contributor Author

zvonand commented Sep 20, 2023

We will have to rewrite all of this in the future and drop the setting you've introduced :(

I will do it.

@hodgesrm
Copy link
Contributor

Hi @zvonand,

What is the proposed fix?
Seems as if you could parse the string, conclude there are no globs, and generate the list. This would eliminate the need to search (performance hit) and eliminate permission issues.

@zvonand
Copy link
Contributor Author

zvonand commented Sep 20, 2023

Seems as if you could parse the string, conclude there are no globs, and generate the list. This would eliminate the need to search (performance hit) and eliminate permission issues.

Yes, exactly like this

@zvonand zvonand deleted the zvonand-globs-small-fix branch September 20, 2023 16:39
@zvonand
Copy link
Contributor Author

zvonand commented Sep 20, 2023

@alexey-milovidov could you please put a can be tested to #54863 (follow-up for these two PRs) ?
Let me finish this and forget about it 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can be tested Allows running workflows for external contributors pr-improvement Pull request with some product improvements pr-status-✅ PR with only success statuses
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants