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 RBAC in weaviate-cli. #102

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ Here you can see an example on how the configuration file should look like if yo
}
```

If you want to allow using different users for different actions in your cluster, you can specify the different users in the configuration file and use the `--user` option to specify which user to use for a specific action.
An example of how the configuration file should look like is the following:

```json
{
"host": "your-weaviate-host",
"auth": {
"type": "user",
"user1": "your-api-key-for-user1",
"user2": "your-api-key-for-user2"
}
}
```
It's important to note that the "type" key must be set to "user" and the users must be specified in the auth section.
When using the `weaviate-cli` command, you can specify the user to use for an action by using the `--user` option. For example:

```bash
weaviate-cli --user user1 create collection --collection movies --vectorizer transformers
weaviate-cli --user user2 get collection --collection movies
```

## Requirements

- Python 3.9+
Expand Down
15 changes: 13 additions & 2 deletions cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Optional
import click
import sys
from weaviate_cli.managers.config_manager import ConfigManager
Expand All @@ -8,6 +9,8 @@
from weaviate_cli.commands.query import query
from weaviate_cli.commands.restore import restore
from weaviate_cli.commands.cancel import cancel
from weaviate_cli.commands.add import add
from weaviate_cli.commands.revoke import revoke
from weaviate_cli import __version__


Expand All @@ -26,6 +29,12 @@ def print_version(ctx, param, value):
is_flag=False,
help="If specified cli uses the config specified with this path.",
)
@click.option(
"--user",
required=False,
type=str,
help="If specified cli uses the user specified in the config file.",
)
@click.option(
"--version",
is_flag=True,
Expand All @@ -35,10 +44,10 @@ def print_version(ctx, param, value):
help="Prints the version of the CLI.",
)
@click.pass_context
def main(ctx: click.Context, config_file):
def main(ctx: click.Context, config_file: Optional[str], user: Optional[str]):
"""Weaviate CLI tool"""
try:
ctx.obj = {"config": ConfigManager(config_file)}
ctx.obj = {"config": ConfigManager(config_file, user)}
except Exception as e:
click.echo(f"Fatal Error: {e}")
sys.exit(1)
Expand All @@ -51,6 +60,8 @@ def main(ctx: click.Context, config_file):
main.add_command(restore)
main.add_command(query)
main.add_command(cancel)
main.add_command(add)
main.add_command(revoke)

if __name__ == "__main__":
main()
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
weaviate-client>=4.0.0
#weaviate-client>=4.0.0
weaviate-client @ git+https://github.com/weaviate/[email protected]/add-support-for-rbac
click==8.1.7
twine
pytest
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ classifiers =
include_package_data = True
python_requires = >=3.9
install_requires =
weaviate-client>=4.5.0
weaviate-client @ git+https://github.com/weaviate/[email protected]/add-support-for-rbac
#weaviate-client>=4.5.0
click==8.1.7
semver>=3.0.2
numpy>=1.24.0
Expand Down
99 changes: 99 additions & 0 deletions test/unittests/test_managers/test_config_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import socket
import pytest
import weaviate
from unittest.mock import MagicMock, mock_open, patch
from weaviate_cli.managers.config_manager import ConfigManager
import json
Expand Down Expand Up @@ -40,3 +41,101 @@ def test_check_host_docker_internal_failed(mock_socket):
# Test failed connection
mock_socket.side_effect = socket.error()
assert config._ConfigManager__check_host_docker_internal() == False


def test_init_with_user():
config_data = {
"host": "localhost",
"http_port": "8080",
"grpc_port": "50051",
"auth": {"type": "user", "admin": "admin-key", "jose": "jose-key"},
}

with patch("builtins.open", mock_open(read_data=json.dumps(config_data))):
with patch("os.path.isfile") as mock_isfile:
mock_isfile.return_value = True
config = ConfigManager(config_file="test_config.json", user="jose")
assert config.user == "jose"
assert config.config == config_data


def test_get_client_with_user_auth():
config_data = {
"host": "localhost",
"http_port": "8080",
"grpc_port": "50051",
"auth": {"type": "user", "admin": "admin-key", "jose": "jose-key"},
}

with patch("builtins.open", mock_open(read_data=json.dumps(config_data))):
with patch("os.path.isfile") as mock_isfile:
mock_isfile.return_value = True
with patch("weaviate.connect_to_local") as mock_connect:
config = ConfigManager(config_file="test_config.json", user="jose")
config.get_client()

mock_connect.assert_called_once()
call_kwargs = mock_connect.call_args.kwargs
assert isinstance(
call_kwargs["auth_credentials"], weaviate.auth.AuthApiKey
)
assert call_kwargs["auth_credentials"].api_key == "jose-key"


def test_get_client_with_invalid_user():
config_data = {
"host": "localhost",
"http_port": "8080",
"grpc_port": "50051",
"auth": {"type": "user", "admin": "admin-key"},
}

with patch("builtins.open", mock_open(read_data=json.dumps(config_data))):
with patch("os.path.isfile") as mock_isfile:
mock_isfile.return_value = True
config = ConfigManager(config_file="test_config.json", user="jose")
with pytest.raises(Exception) as exc_info:
config.get_client()
assert str(exc_info.value) == "User 'jose' not found in config file"


def test_get_client_missing_user():
config_data = {
"host": "localhost",
"http_port": "8080",
"grpc_port": "50051",
"auth": {"type": "user", "admin": "admin-key"},
}

with patch("builtins.open", mock_open(read_data=json.dumps(config_data))):
with patch("os.path.isfile") as mock_isfile:
mock_isfile.return_value = True
config = ConfigManager(config_file="test_config.json")
with pytest.raises(Exception) as exc_info:
config.get_client()
assert (
str(exc_info.value) == "User must be specified when auth type is 'user'"
)


def test_get_client_with_api_key_auth():
config_data = {
"host": "localhost",
"http_port": "8080",
"grpc_port": "50051",
"auth": {"type": "api_key", "api_key": "test-key"},
}

with patch("builtins.open", mock_open(read_data=json.dumps(config_data))):
with patch("os.path.isfile") as mock_isfile:
mock_isfile.return_value = True
with patch("weaviate.connect_to_local") as mock_connect:
config = ConfigManager(config_file="test_config.json")
config.get_client()

mock_connect.assert_called_once()
call_kwargs = mock_connect.call_args.kwargs
assert isinstance(
call_kwargs["auth_credentials"], weaviate.auth.AuthApiKey
)
assert call_kwargs["auth_credentials"].api_key == "test-key"
Loading
Loading