Skip to content

Add definers for views - #54901

Merged
nikitamikhaylov merged 60 commits into
masterfrom
pufit/views-sql-security
Feb 22, 2024
Merged

nikitamikhaylov merged 60 commits into
masterfrom
pufit/views-sql-security

Conversation

@pufit

@pufit pufit commented Sep 21, 2023 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • New Feature

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

Added new syntax which allows to specify definer user in View/Materialized View. This allows to execute selects/inserts from views without explicit grants for underlying tables.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Closes #19228

TODO:

  • Documentation
  • More complex tests
  • GRANT DEFINER ON anotheruser TO user

Information about CI checks: https://clickhouse.com/docs/en/development/continuous-integration/

@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-feature Pull request with new product feature label Sep 21, 2023
@robot-ch-test-poll4

robot-ch-test-poll4 commented Sep 21, 2023 •

Copy link
Copy Markdown
Contributor

This is an automated comment for commit a2a4657 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
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ 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 keeper imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docker server imageThe 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
Mergeable CheckChecks if all other necessary checks are successful✅ 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
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
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
Check nameDescriptionStatus
A SyncThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS❌ failure
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⏳ pending
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests❌ failure
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors❌ failure

@vitlibar vitlibar self-assigned this Sep 21, 2023
@pufit
pufit force-pushed the pufit/views-sql-security branch from 243aa99 to bee2786 Compare October 11, 2023 19:08
@pufit
pufit marked this pull request as ready for review October 31, 2023 18:48
Comment thread src/Access/Common/AccessFlags.cpp Resolved
Comment thread src/Storages/StorageInMemoryMetadata.h Resolved
Comment thread src/Interpreters/Context.h Outdated
String getUserName() const;

/// WARNING: all requests with this context won't trigger any access checks.
void resetUser();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do you need this? Just use the global context. It's nice that we have only one global context.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't want to completely override the original local context. What if it had some other stuff in it?

@vitlibar vitlibar Nov 9, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it's better to completely override the original context always when SQL_SECURITY is not INVOKER.
Because we want a context created for the definer, not kind of a mixture between contexts for two users. I mean all the settings should the definer's settings, and roles should be the definer's roles, and so on.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Probably we need to keep the information about the initial query in the ClientInfo of the new context though.

178
188
198
02177_MV_3 20 0 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why?

@nikitamikhaylov

Copy link
Copy Markdown
Member

@pufit @vitlibar We need to resolve conflicts and deal with remaining tests.

# Conflicts:
#	src/Core/SettingsChangesHistory.h
#	src/Processors/Transforms/buildPushingToViewsChain.cpp
@pufit
pufit force-pushed the pufit/views-sql-security branch from e954148 to 50436ed Compare February 21, 2024 03:47
@pufit
pufit force-pushed the pufit/views-sql-security branch from 50436ed to a2a4657 Compare February 21, 2024 16:48
@nikitamikhaylov
nikitamikhaylov merged commit 5730670 into master Feb 22, 2024
@nikitamikhaylov
nikitamikhaylov deleted the pufit/views-sql-security branch February 22, 2024 16:47
@jrdi

jrdi commented Feb 23, 2024

Copy link
Copy Markdown
Contributor

This PR will make an upgrade from 24.1 a bit difficult. If you have:

  • Replica 1: Using 24.2 (master)
  • Replica 2: Using 24.1 or earlier

If you try to create a new normal/materialized view in Replica 1 ON CLUSTER, the following error will be returned:

Received exception from server (version 24.2.1):
Code: 62. DB::Exception: Received from localhost:29000. DB::Exception: There was an error on [replica-2:39000]: Code: 62. DB::Exception: Syntax error: failed at position 142 ('DEFINER'): DEFINER = default SQL SECURITY DEFINER AS SELECT a FROM definer.landing. Expected one of: storage definition, ENGINE, PARTITION BY, PRIMARY KEY, ORDER BY, SAMPLE BY, TTL, POPULATE, EMPTY, AS. (SYNTAX_ERROR) (version 24.1.3.31 (official build)). (SYNTAX_ERROR)

This is because the SQL syntax executed in Replica 2 already contains unsupported SQL SECURITY syntax. Is there a way to disable this feature? It will be useful, at least for the upgrade process. I have tried passing default_materialized_view_sql_security = 'NONE', but new syntax is still included.

Received exception from server (version 24.2.1):
Code: 62. DB::Exception: Received from localhost:29000. DB::Exception: There was an error on [replica-2:39000]: Code: 62. DB::Exception: Syntax error: failed at position 128 ('SQL'): SQL SECURITY NONE AS SELECT a FROM definer.landing. Expected one of: storage definition, ENGINE, PARTITION BY, PRIMARY KEY, ORDER BY, SAMPLE BY, TTL, POPULATE, EMPTY, AS. (SYNTAX_ERROR) (version 24.1.3.31 (official build)). (SYNTAX_ERROR)

Full reproducer:

CREATE DATABASE definer ON CLUSTER tinybird;

CREATE TABLE definer.landing ON CLUSTER tinybird (a UInt32) Engine=MergeTree() ORDER BY tuple();
CREATE TABLE definer.ds ON CLUSTER tinybird (a UInt32) Engine=MergeTree() ORDER BY tuple();

CREATE MATERIALIZED VIEW definer.mv ON CLUSTER tinybird TO definer.ds AS SELECT a FROM definer.landing;

Algunenano added a commit that referenced this pull request Feb 23, 2024
@vitlibar

Copy link
Copy Markdown
Member

Code: 62. DB::Exception: Received from localhost:29000. DB::Exception: There was an error on [replica-2:39000]: Code: 62. DB::Exception: Syntax error: failed at position 142 ('DEFINER'): DEFINER = default SQL SECURITY DEFINER AS SELECT a FROM definer.landing. Expected one of: storage definition, ENGINE, PARTITION BY, PRIMARY KEY, ORDER BY, SAMPLE BY, TTL, POPULATE, EMPTY, AS. (SYNTAX_ERROR) (version 24.1.3.31 (official build)). (SYNTAX_ERROR)

@pufit It seems we should add DEFINER and SQL SECURITY to the create query on each shard/replica separately, and not on the initiator of the ON CLUSTER query, what do you think?

}

if (!sql_security && !sql_security_p.parse(pos, sql_security, expected))
sql_security = std::make_shared<ASTSQLSecurity>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks strange that after you failed to parse SQL security you're adding a default one to the query anyway. It seems better not to do that:

if (!sql_security)
    sql_security_p.parse(pos, sql_security, expected);

enum class SQLSecurityType
{
INVOKER,
DEFINER,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A few comments would be nice.

{"parallel_replicas_prefer_local_join", true, true, "If true, and JOIN can be executed with parallel replicas algorithm, and all storages of right JOIN part are *MergeTree, local JOIN will be used instead of GLOBAL JOIN."},
{"extract_key_value_pairs_max_pairs_per_row", 0, 0, "Max number of pairs that can be produced by the `extractKeyValuePairs` function. Used as a safeguard against consuming too much memory."},
}},
{"async_insert_busy_timeout_decrease_rate", 0.2, 0.2, "The exponential growth rate at which the adaptive asynchronous insert timeout decreases"},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

async_insert_busy_timeout_decrease_rate - why is that here?

}},
{"async_insert_busy_timeout_decrease_rate", 0.2, 0.2, "The exponential growth rate at which the adaptive asynchronous insert timeout decreases"},
{"default_view_definer", "", "CURRENT_USER", "Allows to set default `DEFINER` option while creating a view"},
{"default_materialized_view_sql_security", "INVOKER", "DEFINER", "Allows to set a default value for SQL SECURITY option when creating a materialized view"},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we support default_view_definer='' and default_materialized_view_sql_security='INVOKER'?

@pufit

pufit commented Feb 23, 2024 •

Copy link
Copy Markdown
Member Author

@pufit It seems we should add DEFINER and SQL SECURITY to the create query on each shard/replica separately, and not on the initiator of the ON CLUSTER query, what do you think?

There is a problem: if we run a simple query with no SQL security specified, the default values must be resolved on the initiator side. Otherwise, we can get a different SQL security on the other replicas.

The only solution I can see is to add a special setting for the migration period (a.k.a "I know what I'm doing"), which will forcefully disable the new SQL security syntax during DDL, assuming that default values are the same for all replicas.

@nikitamikhaylov

Copy link
Copy Markdown
Member

Good approach. Let's proceed with setting which will be disabled for a few releases and then made obsolete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🎅 🎁 gift🎄 To make people wonder pr-feature Pull request with new product feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RBAC: Stored Object Access Control / DEFINER / SQL SECURITY

7 participants