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

Add support for role query parameters #328

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

pulpdrew
Copy link

Summary

This PR adds support for specifying roles via request query parameters. Closes #269.

Checklist

Delete items not relevant to your PR:

  • Unit and integration tests covering the common scenarios were added
  • A human-readable description of the changes was provided to include in CHANGELOG
  • For significant changes, documentation in https://github.com/ClickHouse/clickhouse-docs was updated with further explanations or tutorials

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Contributor

@slvrtrn slvrtrn left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! Tests are good, too. There is one thing that we should fix before merging (the example); the rest are mostly nitpicks

console.log(await rows1.json())

await client.close()
})()
Copy link
Contributor

Choose a reason for hiding this comment

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

We aim to have runnable examples; I suggest using the roles bootstrap from tests here.

It is also a good idea to:

  • demonstrate expected failures with CH error codes (printing it in the console will suffice so that we can see that 497 ACCESS_DENIED, just like in some other examples, like abort_request.ts, cancel_query.ts, and probably some others)
  • print the output rows like in all other examples (the result set should be consumed, as it holds an unreleased keep-alive socket, waiting for streaming to start; again, a misuse in an example - ResultSets should not be "dangling" in the code)

Copy link
Author

Choose a reason for hiding this comment

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

Updated the examples so that they run successfully, consume output rows, and print any error codes.

Example output:
Screenshot 2024-09-28 at 10 09 35 AM

// with a role defined in the client configuration, all queries will use the specified role
await client.command({
query: `SELECT * FROM SECURED_TABLE`,
})
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be a query call; now it is a misuse in an example, we want to avoid that

Copy link
Author

Choose a reason for hiding this comment

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

Updated all SELECT queries to use .query instead of .command

@@ -67,6 +67,7 @@ If something is missing, or you found a mistake in one of these examples, please
- [default_format_setting.ts](default_format_setting.ts) - sending queries using `exec` method without a `FORMAT` clause; the default format will be set from the client settings.
- [session_id_and_temporary_tables.ts](session_id_and_temporary_tables.ts) - creating a temporary table, which requires a session_id to be passed to the server.
- [session_level_commands.ts](session_level_commands.ts) - using SET commands, memorized for the specific session_id.
- [role.ts](role.ts) - using one more more roles, without explicit `USE` commands or session IDs
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- [role.ts](role.ts) - using one more more roles, without explicit `USE` commands or session IDs
- [role.ts](role.ts) - using one or more roles without explicit `USE` commands or session IDs

@@ -0,0 +1,23 @@
import { createClient } from '@clickhouse/client' // or '@clickhouse/client-web'

void (async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -78,6 +78,7 @@ describe('toSearchParams', () => {
qaz: 'qux',
},
session_id: 'my-session-id',
role: ['my-role-1', 'my-role-2'],
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a test with a single role (as a string).

Comment on lines 83 to 90
if (role && typeof role === 'string') {
params.set('role', role)
} else if (role && Array.isArray(role)) {
for (const r of role) {
params.append('role', r)
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: maybe just a single undefined check will suffice?

Suggested change
if (role && typeof role === 'string') {
params.set('role', role)
} else if (role && Array.isArray(role)) {
for (const r of role) {
params.append('role', r)
}
}
if (role) {
if (typeof role === 'string') {
params.set('role', role)
} else if (Array.isArray(role)) {
for (const r of role) {
params.append('role', r)
}
}
}

@slvrtrn
Copy link
Contributor

slvrtrn commented Sep 26, 2024

The CLA also needs to be signed before we merge it.

@pulpdrew
Copy link
Author

The CLA also needs to be signed before we merge it.

Will do - I am working with my company to make sure I am not violating any of their open source policies by signing, but will sign as soon as I have approval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add role support via query parameters
3 participants