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

refactor: Change the default api key to argilla.apikey #2254

Merged
merged 3 commits into from Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/_source/getting_started/installation/user_management.md
Expand Up @@ -53,10 +53,15 @@ By default, if you don't configure a `users.yml` file, your Argilla instance is

- username: `argilla`
- password: `1234`
- api_key: `rubrix.apikey`
- api_key: `argilla.apikey`

for security reasons we recommend changing at least the password and API key.

:::{note}
To connect to an old Argilla server using client `>=1.3.0`, you should specify the default user API key `rubrix.apikey`.
Otherwise, connections will fail with an Unauthorized server error.
:::

### Override default API key

To override the default API key you can set the following environment variable before launching the server:
Expand Down
5 changes: 4 additions & 1 deletion src/argilla/_constants.py
Expand Up @@ -18,8 +18,11 @@

API_KEY_HEADER_NAME = "X-Argilla-Api-Key"
WORKSPACE_HEADER_NAME = "X-Argilla-Workspace"
DEFAULT_API_KEY = "rubrix.apikey" # Keep the same api key for now
DEFAULT_API_KEY = "argilla.apikey" # Keep the same api key for now

# TODO: This constant will be drop out with issue
# https://github.com/argilla-io/argilla/issues/2251 fix
_OLD_DEFAULT_API_KEY = "rubrix.apikey"
_OLD_API_KEY_HEADER_NAME = "X-Rubrix-Api-Key"
_OLD_WORKSPACE_HEADER_NAME = "X-Rubrix-Workspace"

Expand Down
2 changes: 1 addition & 1 deletion src/argilla/client/api.py
Expand Up @@ -81,7 +81,7 @@ def init(
api_url: Address of the REST API. If `None` (default) and the env variable ``ARGILLA_API_URL`` is not set,
it will default to `http://localhost:6900`.
api_key: Authentification key for the REST API. If `None` (default) and the env variable ``ARGILLA_API_KEY``
is not set, it will default to `rubrix.apikey`.
is not set, it will default to `argilla.apikey`.
workspace: The workspace to which records will be logged/loaded. If `None` (default) and the
env variable ``ARGILLA_WORKSPACE`` is not set, it will default to the private user workspace.
timeout: Wait `timeout` seconds for the connection to timeout. Default: 60.
Expand Down
7 changes: 7 additions & 0 deletions src/argilla/server/security/auth_provider/local/users/dao.py
Expand Up @@ -17,6 +17,7 @@

import yaml

from argilla._constants import _OLD_DEFAULT_API_KEY, DEFAULT_API_KEY
from argilla.server.security.auth_provider.local.settings import settings

from .model import UserInDB
Expand Down Expand Up @@ -55,6 +56,12 @@ def get_user(self, user_name: str) -> Optional[UserInDB]:

async def get_user_by_api_key(self, api_key: str) -> Optional[UserInDB]:
"""Find a user for a given api key"""

# TODO: This piece of code will be drop out with issue
# https://github.com/argilla-io/argilla/issues/2251 fix
if api_key == _OLD_DEFAULT_API_KEY and _DEFAULT_USER.api_key == DEFAULT_API_KEY:
return _DEFAULT_USER

for user in self.__users__.values():
if api_key == user.api_key:
return user
Expand Down
12 changes: 12 additions & 0 deletions tests/client/test_init.py
Expand Up @@ -14,6 +14,7 @@

from argilla import TextClassificationRecord
from argilla.client import api
from argilla.client.api import active_api


def test_resource_leaking_with_several_init(mocked_client):
Expand Down Expand Up @@ -47,3 +48,14 @@ def test_init_with_extra_headers(mocked_client):
assert (
active_api.http_client.headers[key] == value
), f"{key}:{value} not in client headers"


def test_init(mocked_client):
the_api = active_api()
user = the_api.http_client.get("/api/me")
assert user["username"] == "argilla"

api.init(api_key="rubrix.apikey")
the_api = active_api()
user = the_api.http_client.get("/api/me")
assert user["username"] == "argilla"