def index_base_uri(base_uri): """Register all the datasets in a base URI.""" base_uri = dtoolcore.utils.sanitise_uri(base_uri) if not base_uri_exists(base_uri): click.secho("Base URI '{}' not registered".format(base_uri), fg="red", err=True) sys.exit(1) for dataset in iter_datasets_in_base_uri(base_uri): try: dataset_info = generate_dataset_info(dataset, base_uri) except (yaml.parser.ParserError, yaml.scanner.ScannerError) as message: click.secho("Failed to register: {} {}".format( dataset.name, dataset.uri), fg="red") click.secho("README YAML parsing issue", fg="red") click.echo(message) continue try: r = register_dataset(dataset_info) except dtool_lookup_server.ValidationError as message: click.secho("Failed to register: {} {}".format( dataset.name, dataset.uri), fg="red") click.echo(message) continue click.secho("Registered: {}".format(r), fg="green")
def give_register_permission(username, base_uri): """Give a user register permission on a base URI.""" if not base_uri_exists(base_uri): click.secho("Base URI '{}' not registered".format(base_uri), fg="red", err=True) sys.exit(1) if not user_exists(username): click.secho("User '{}' not registered".format(username), fg="red", err=True) sys.exit(1) permissions = get_permission_info(base_uri) if username in permissions["users_with_register_permissions"]: click.secho( "User '{}' already has register permissions".format(username), fg="red", err=True) sys.exit(1) permissions["users_with_register_permissions"].append(username) update_permissions(permissions)
def register(): """Register a base URI. The user needs to be admin. Returns 404 for non-admins. """ username = get_jwt_identity() data = request.get_json() base_uri = data["base_uri"] try: user = get_user_obj(username) except AuthenticationError: # Unregistered users should see 404. abort(404) # Non admin users should see 404. if not user.is_admin: abort(404) # Make it idempotent. if base_uri_exists(base_uri): return "", 201 register_base_uri(base_uri) return "", 201
def add_base_uri(base_uri): """Register a base URI in the dtool lookup server.""" base_uri = dtoolcore.utils.sanitise_uri(base_uri) if base_uri_exists(base_uri): click.secho("Base URI '{}' already registered".format(base_uri), fg="red", err=True) sys.exit(1) register_base_uri(base_uri)
def test_base_uri_regsiter_route(tmp_app_with_users): # NOQA from dtool_lookup_server.utils import base_uri_exists base_uri = "s3://snow-white-again" assert not base_uri_exists(base_uri) data = {"base_uri": base_uri} headers = dict(Authorization="Bearer " + snowwhite_token) r = tmp_app_with_users.post("/admin/base_uri/register", headers=headers, data=json.dumps(data), content_type="application/json") assert r.status_code == 201 assert base_uri_exists(base_uri) # Ensure idempotent. headers = dict(Authorization="Bearer " + snowwhite_token) r = tmp_app_with_users.post("/admin/base_uri/register", headers=headers, data=json.dumps(data), content_type="application/json") assert r.status_code == 201 assert base_uri_exists(base_uri) # Only admins allowed. However, don't give away that URL exists to # non-admins. headers = dict(Authorization="Bearer " + grumpy_token) r = tmp_app_with_users.post("/admin/base_uri/register", headers=headers, data=json.dumps(data), content_type="application/json") assert r.status_code == 404 headers = dict(Authorization="Bearer " + noone_token) r = tmp_app_with_users.post("/admin/base_uri/register", headers=headers, data=json.dumps(data), content_type="application/json") assert r.status_code == 404
def test_cli_register_base_uri(tmp_cli_runner): # NOQA from dtool_lookup_server.utils import get_base_uri_obj, base_uri_exists b_uri = "s3://snow-white" assert not base_uri_exists(b_uri) from dtool_lookup_server.cli import add_base_uri result = tmp_cli_runner.invoke(add_base_uri, [b_uri]) assert result.exit_code == 0 new_base_uri = get_base_uri_obj(b_uri) expected_content = { "base_uri": b_uri, "users_with_search_permissions": [], "users_with_register_permissions": [] } assert new_base_uri.as_dict() == expected_content
def _retrieve_uri(base_uri, uuid): """Retrieve URI(s) from database given as base URI and an UUID""" if not base_uri_exists(base_uri): raise (ValidationError( "Base URI is not registered: {}".format(base_uri))) # Query database to construct the respective URI. We cannot just # concatenate base URI and UUID since the URI may depend on the name of # the dataset which we do not have. uris = [] query_result = sql_db.session.query(Dataset, BaseURI) \ .filter(Dataset.uuid == uuid) \ .filter(BaseURI.id == Dataset.base_uri_id) \ .filter(BaseURI.base_uri == base_uri) for dataset, base_uri in query_result: return dtoolcore._generate_uri( { 'uuid': dataset.uuid, 'name': dataset.name }, base_uri.base_uri) return None