Skip to content

Add basic security functions to druidapi#14009

Closed
paul-rogers wants to merge 4 commits intoapache:masterfrom
paul-rogers:230331-py-auth
Closed

Add basic security functions to druidapi#14009
paul-rogers wants to merge 4 commits intoapache:masterfrom
paul-rogers:230331-py-auth

Conversation

@paul-rogers
Copy link
Copy Markdown
Contributor

This PR adds a complete set of Basic security functions to the Python druidapi. These functions are handy for setting up security, inspecting the security setup, and learning the nuances of the basic security system. They would make a fine foundation for Basic security tutorial notebook. If we did such a notebook:

  • Emphasize that users are defined twice: once in the authorizer, again in the authenticator.
  • The many config settings that have to be done just right.
  • The complexities of SQL security: sometimes one needs multiple permissions.

Since the Druid console doesn't provide tools to set up basic security, doing it via Python is a handy way to get started until a user defines a more production-grade integration with an external system.

Example:

# Define a coordinator-specific client, using the admin user
coord = druidapi.jupyter_client('http://localhost:8081', auth=('admin', 'pwd'))
# Create a basic auth client for your authenticator and authorizer:
ac = coord.basic_security('myAuthorizer', 'myAuthenticator')

# Get information
# List users
ac.users()
# List roles
ac.users()
# List roles for a user
ac.authorization_user('alice')
# List permissions for a role
ac.role_permissions('aliceRole')

# Create user
ac.add_user('fred', 'pwd')
# Create role
ac.add_role('myRole')
# Grant permissions to a role
perms = [ac.resource_action(consts.DATASOURCE_RESOURCE, 'foo', consts.READ_ACTION)]
ac.set_role_permissions('myRole', perms)
# Assign a role to a user
ac.assign_role_to_user('myRole', 'fred')

# "Log in" as the new user
fred = druidapi.jupyter_client('http://localhost:8888', auth=('fred', 'pwd'))
# Perform operations as the user.
fred.sql.sql('SELECT * FROM foo LIMIT 10')

# Drop user
ac.drop_user('fred')

Release note

See the description.


This PR has:

  • been self-reviewed.
  • added documentation for new or modified features or behaviors.
  • a release note entry in the PR description.
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • been tested in a test Druid cluster.

Comment thread docs/operations/security-overview.md Outdated
Comment thread docs/operations/security-overview.md Outdated
Comment thread docs/operations/security-overview.md Outdated
Comment thread docs/operations/security-overview.md Outdated
Comment thread examples/quickstart/jupyter-notebooks/druidapi/druidapi/basic_auth.py Outdated
Comment thread examples/quickstart/jupyter-notebooks/druidapi/druidapi/druid.py Outdated
Comment thread examples/quickstart/jupyter-notebooks/druidapi/druidapi/druid.py Outdated
Comment thread examples/quickstart/jupyter-notebooks/druidapi/druidapi/druid.py Outdated
Comment thread examples/quickstart/jupyter-notebooks/druidapi/druidapi/basic_auth.py Outdated
Comment thread examples/quickstart/jupyter-notebooks/druidapi/druidapi/rest.py Outdated
@paul-rogers paul-rogers mentioned this pull request Apr 4, 2023
8 tasks
@paul-rogers
Copy link
Copy Markdown
Contributor Author

@vtlim, thanks again for acting as my spell-checker and proof-reader!

@paul-rogers
Copy link
Copy Markdown
Contributor Author

The build is clean except for two issues:

  • The "slow" Kafka tests which repeatedly fail in many PRs.
  • The code scanning task which has started failing due to a missing config file.

If we ignore the above two issues, which are independent of the code in this PR, the build is clean.

Copy link
Copy Markdown
Contributor

@abhishekrb19 abhishekrb19 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 adding this @paul-rogers! The change overall looks good. I left some comments.

```
An example configuration:

```text
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: if you switch this from text -> properties, you get some nice syntax highlighting

Suggested change
```text
```properties

```
> Note: Druid treats the resource name as a regular expression (regex). You can use a specific datasource name or regex to grant permissions for multiple datasources at a time.
]
```
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I suspect there's a missing backtick in this diff that's causing markdown to render the following section inside a codeblock - please see screenshot:
CleanShot 2023-04-07 at 09 19 12@2x


class BasicAuthClient:
'''
Manage Basic security. The Druid session must be logged in with the super
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: "Basic" -> "basic"

Returns both the authenticator and authorizer status.
'''
return {
"authenticator": self.authorization_status(),
Copy link
Copy Markdown
Contributor

@abhishekrb19 abhishekrb19 Apr 7, 2023

Choose a reason for hiding this comment

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

Same as below - I think the status assignment for authenticator and authorizer is flipped.

Returns the list of authenticator and authorizer users.
'''
return {
"authenticator": self.authorization_users(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this be:

        return {
            "authenticator": self.authentication_users(),
            "authorizer": self. authorization_users()
        }

basic security enabled, you must specify the admin user and password:

```
coord = druidapi.jupyter_client('http://localhost:8081', auth=('admin', 'admin-pwd'))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Neat!

vtlim added a commit that referenced this pull request Apr 11, 2023
Co-authored-by: Victoria Lim <vtlim@users.noreply.github.com>
Co-authored-by: Paul Rogers <progers@apache.org>
@abhishekrb19
Copy link
Copy Markdown
Contributor

This change was included in #14055

@vtlim vtlim closed this Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants