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 task to create default user on database #2502

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/argilla/server/contexts/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from passlib.context import CryptContext
from sqlalchemy.orm import Session

CRYPT_CONTEXT = CryptContext(schemes=["bcrypt"], deprecated="auto")
_CRYPT_CONTEXT = CryptContext(schemes=["bcrypt"], deprecated="auto")


def get_workspace_user_by_workspace_id_and_user_id(db: Session, workspace_id: UUID, user_id: UUID):
Expand Down Expand Up @@ -101,7 +101,7 @@ def create_user(db: Session, user_create: UserCreate):
last_name=user_create.last_name,
username=user_create.username,
role=user_create.role,
password_hash=CRYPT_CONTEXT.hash(user_create.password),
password_hash=hash_password(user_create.password),
)

db.add(user)
Expand All @@ -121,9 +121,17 @@ def delete_user(db: Session, user: User):
def authenticate_user(db: Session, username: str, password: str):
user = get_user_by_username(db, username)

if user and CRYPT_CONTEXT.verify(password, user.password_hash):
if user and verify_password(password, user.password_hash):
return user
elif user:
return
else:
CRYPT_CONTEXT.dummy_verify()
_CRYPT_CONTEXT.dummy_verify()


def hash_password(password: str):
return _CRYPT_CONTEXT.hash(password)


def verify_password(password: str, password_hash: str):
return _CRYPT_CONTEXT.verify(password, password_hash)
2 changes: 1 addition & 1 deletion src/argilla/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def configure_database(app: FastAPI):
get_db_wrapper = contextlib.contextmanager(get_db)

def _user_has_default_credentials(user: User):
return user.api_key == DEFAULT_API_KEY or accounts.CRYPT_CONTEXT.verify(DEFAULT_PASSWORD, user.password_hash)
return user.api_key == DEFAULT_API_KEY or accounts.verify_password(DEFAULT_PASSWORD, user.password_hash)

def _log_default_user_warning():
_LOGGER.warning(
Expand Down
13 changes: 13 additions & 0 deletions src/argilla/tasks/users/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2021-present, the Recognai S.L. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
47 changes: 47 additions & 0 deletions src/argilla/tasks/users/create_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2021-present, the Recognai S.L. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import click

from argilla._constants import DEFAULT_API_KEY, DEFAULT_PASSWORD, DEFAULT_USERNAME
from argilla.server.contexts import accounts
from argilla.server.database import SessionLocal
from argilla.server.models import User, UserRole, Workspace


@click.command()
@click.option("-q", "--quiet", is_flag=True, default=False, help="Run without output.")
def create_default(quiet: bool):
"""Creates a user with default credentials on database suitable to start experimenting with argilla."""
with SessionLocal() as session, session.begin():
session.add(
User(
first_name="",
username=DEFAULT_USERNAME,
role=UserRole.admin,
api_key=DEFAULT_API_KEY,
password_hash=accounts.hash_password(DEFAULT_PASSWORD),
workspaces=[Workspace(name=DEFAULT_USERNAME)],
)
)

if not quiet:
click.echo("User with default credentials succesfully created:")
click.echo(f"• username: {DEFAULT_USERNAME!r}")
click.echo(f"• password: {DEFAULT_PASSWORD!r}")
click.echo(f"• api_key: {DEFAULT_API_KEY!r}")


if __name__ == "__main__":
create_default()
13 changes: 13 additions & 0 deletions tests/tasks/users/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2021-present, the Recognai S.L. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
42 changes: 42 additions & 0 deletions tests/tasks/users/test_create_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2021-present, the Recognai S.L. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from argilla._constants import DEFAULT_API_KEY, DEFAULT_PASSWORD, DEFAULT_USERNAME
from argilla.server.contexts import accounts
from argilla.server.models import User, UserRole
from argilla.tasks.users.create_default import create_default
from click.testing import CliRunner
from sqlalchemy.orm import Session


def test_create_default(db: Session):
result = CliRunner().invoke(create_default)

assert result.exit_code == 0
assert result.output != ""
assert db.query(User).count() == 1

default_user = db.query(User).filter_by(username=DEFAULT_USERNAME).first()
assert default_user
assert default_user.role == UserRole.admin
assert default_user.api_key == DEFAULT_API_KEY
assert accounts.verify_password(DEFAULT_PASSWORD, default_user.password_hash)
assert [ws.name for ws in default_user.workspaces] == [DEFAULT_USERNAME]


def test_create_default_quiet(db: Session):
result = CliRunner().invoke(create_default, ["--quiet"])

assert result.exit_code == 0
assert result.output == ""