Skip to content

Commit

Permalink
Add sdk security example (#1722)
Browse files Browse the repository at this point in the history
* better logging

* Add sdk test as example of security search

* fix lint errors
  • Loading branch information
lferran authored Jan 12, 2024
1 parent 5483d32 commit 6e3d7d3
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
4 changes: 3 additions & 1 deletion nucliadb/nucliadb/ingest/partitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def assign_partitions(settings: Settings):
try:
settings.replica_number = int(sts_values[-1])
except Exception:
logger.error("Could not extract replica number from HOSTNAME")
logger.error(
f"Could not extract replica number from hostname: {hostname}"
)
pass

if settings.replica_number == -1:
Expand Down
85 changes: 85 additions & 0 deletions nucliadb_sdk/nucliadb_sdk/tests/test_security.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright (C) 2021 Bosutech XXI S.L.
#
# nucliadb is offered under the AGPL v3.0 and as commercial software.
# For commercial licensing, contact us at [email protected].
#
# AGPL:
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import nucliadb_sdk
from nucliadb_models.resource import KnowledgeBoxObj
from nucliadb_models.security import RequestSecurity, ResourceSecurity

CLASSIFIED_INFO = [
"""El presidente del Gobierno, Pedro Sánchez, ha anunciado este viernes \
que el Consejo de Ministros aprobará el próximo martes la declaración del \
estado de alarma para todo el territorio nacional, que se prolongará durante \
seis meses, hasta el 9 de mayo de 2021, para hacer frente a la segunda ola de \
la pandemia de coronavirus.""",
"""
The president of the united states of America, Donald Trump, has announced \
that he will not accept the results of the elections, and that he will \
continue to be the president of the united states of America.
""",
]

PUBLIC_NEWS = [
"La temperatura hoy en Sevilla será de 20 grados centígrados como máxima y 10 grados centígrados como mínima.",
"El Guggenheim de Bilbao ha sido galardonado con el premio Príncipe de Asturias de las Artes.",
]


def test_security_search(sdk: nucliadb_sdk.NucliaDB, kb: KnowledgeBoxObj):
legal_group = "legal"
sales_group = "sales"
# Create some classified resources that only legal should have access to
for i, text in enumerate(CLASSIFIED_INFO):
sdk.create_resource(
kbid=kb.uuid,
title=f"Classified {i}",
summary=text,
security=ResourceSecurity(access_groups=[legal_group]),
)

# Create some public news resources
for i, text in enumerate(PUBLIC_NEWS):
sdk.create_resource(
kbid=kb.uuid, title=f"News {i}", summary=text, security=None
)

# Only legal group has access to any classified info
results = sdk.find(
kbid=kb.uuid, query="Classified", security=RequestSecurity(groups=[sales_group])
)
assert len(results.resources) == 0

results = sdk.find(
kbid=kb.uuid, query="Classified", security=RequestSecurity(groups=[legal_group])
)
assert len(results.resources) == 2

# Public news are accessible to everyone
results = sdk.find(
kbid=kb.uuid, query="News", security=RequestSecurity(groups=[sales_group])
)
assert len(results.resources) == 2

results = sdk.find(
kbid=kb.uuid, query="News", security=RequestSecurity(groups=[legal_group])
)
assert len(results.resources) == 2

# Querying without security should return all resources
results = sdk.find(kbid=kb.uuid, query="Classified OR News")
assert len(results.resources) == 4

0 comments on commit 6e3d7d3

Please sign in to comment.