<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

/*
 * Copyright 2016 ScyllaDB
 *
 * Modified by ScyllaDB
 */

/*
 * This file is part of Scylla.
 *
 * Scylla 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.
 *
 * Scylla 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Scylla.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <vector>
#include <seastar/core/future-util.hh>

#include "list_permissions_statement.hh"
#include "auth/authorizer.hh"
#include "auth/common.hh"
#include "cql3/result_set.hh"
#include "transport/messages/result_message.hh"

cql3::statements::list_permissions_statement::list_permissions_statement(
        auth::permission_set permissions,
        std::optional<auth::resource> resource,
        std::optional<sstring> username, bool recursive)
            : _permissions(permissions)
            , _resource(std::move(resource))
            , _username(std::move(username))
            , _recursive(recursive) {
}

void cql3::statements::list_permissions_statement::validate(
        distributed<service::storage_proxy>& proxy,
        const service::client_state& state) {
    // a check to ensure the existence of the user isn't being leaked by user existence check.
    state.ensure_not_anonymous();
}

future<> cql3::statements::list_permissions_statement::check_access(const service::client_state& state) {
    //
    // TODO(jhaberku): The existence checks should be in `execute()`. `check_access` should be restricted to
    // authorization checking.
    //

    auto f = make_ready_future();
    if (_username) {
        f = state.get_auth_service()->underlying_role_manager().exists(*_username).then([this](bool exists) {
            if (!exists) {
                throw exceptions::invalid_request_exception(sprint("User %s doesn't exist", *_username));
            }
        });
    }
    return f.then([this, &state] {
        if (_resource) {
            maybe_correct_resource(*_resource, state);
            return state.ensure_exists(*_resource);
        }

        return make_ready_future<>();
    }).then([this, &state] {
        const auto& as = *state.get_auth_service();

        return auth::has_superuser(as, *state.user()).then([this, &state, &as](bool has_super) {
            if (has_super) {
                return make_ready_future<>();
            }

            if (!_username) {
                return make_exception_future<>(
                        exceptions::unauthorized_exception("You are not authorized to view everyone's permissions"));
            }

            return auth::has_role(as, *state.user(), *_username).then([this](bool has_role) {
                if (!has_role) {
                    return make_exception_future<>(
                            exceptions::unauthorized_exception(
                                    sprint("You are not authorized to view %s's permissions", *_username)));
                }

                return make_ready_future<>();
            });
        });
    });
}


future<::shared_ptr<cql_transport::messages::result_message>>
cql3::statements::list_permissions_statement::execute(
        distributed<service::storage_proxy>& proxy,
        service::query_state& state,
        const query_options& options) {
    static auto make_column = [](sstring name) {
        return ::make_shared<column_specification>(
                auth::meta::AUTH_KS,
                "permissions",
                ::make_shared<column_identifier>(std::move(name), true),
                utf8_type);
    };

    static thread_local const std::vector<::shared_ptr<column_specification>> metadata({
        make_column("role"), make_column("resource"), make_column("permission")
    });

    typedef std::optional<auth::resource> opt_resource;

    std::vector<opt_resource> resources;

    auto r = _resource;
    for (;;) {
        resources.emplace_back(r);
        if (!r || !_recursive) {
            break;
        }

        auto parent = r->parent();
        if (!parent) {
            break;
        }

        r = std::move(parent);
    }

    return map_reduce(
            resources,
            [&state, this](opt_resource r) {
                return do_with(std::move(r), [this, &state](const opt_resource& r) {
                    auto& auth_service = *state.get_client_state().get_auth_service();
                    return auth_service.underlying_authorizer().list(_permissions, r, _username, auth_service);
                });
            },
            std::vector<auth::permission_details>(),
            [](std::vector<auth::permission_details> details, std::vector<auth::permission_details> pd) {
                details.insert(details.end(), pd.begin(), pd.end());
                return details;<commit_msg>cql3: Fix error handling in LIST PERMISSIONS<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

/*
 * Copyright 2016 ScyllaDB
 *
 * Modified by ScyllaDB
 */

/*
 * This file is part of Scylla.
 *
 * Scylla 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.
 *
 * Scylla 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Scylla.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <vector>
#include <seastar/core/future-util.hh>

#include "list_permissions_statement.hh"
#include "auth/authorizer.hh"
#include "auth/common.hh"
#include "cql3/result_set.hh"
#include "transport/messages/result_message.hh"

cql3::statements::list_permissions_statement::list_permissions_statement(
        auth::permission_set permissions,
        std::optional<auth::resource> resource,
        std::optional<sstring> username, bool recursive)
            : _permissions(permissions)
            , _resource(std::move(resource))
            , _username(std::move(username))
            , _recursive(recursive) {
}

void cql3::statements::list_permissions_statement::validate(
        distributed<service::storage_proxy>& proxy,
        const service::client_state& state) {
    // a check to ensure the existence of the user isn't being leaked by user existence check.
    state.ensure_not_anonymous();
}

future<> cql3::statements::list_permissions_statement::check_access(const service::client_state& state) {
    if (_resource) {
        maybe_correct_resource(*_resource, state);
        return state.ensure_exists(*_resource);
    }

    const auto& as = *state.get_auth_service();

    return auth::has_superuser(as, *state.user()).then([this, &state, &as](bool has_super) {
        if (has_super) {
            return make_ready_future<>();
        }

        if (!_username) {
            return make_exception_future<>(
                    exceptions::unauthorized_exception("You are not authorized to view everyone's permissions"));
        }

        return auth::has_role(as, *state.user(), *_username).then([this](bool has_role) {
            if (!has_role) {
                return make_exception_future<>(
                        exceptions::unauthorized_exception(
                                sprint("You are not authorized to view %s's permissions", *_username)));
            }

            return make_ready_future<>();
        }).handle_exception_type([](const auth::nonexistant_role& e) {
            return make_exception_future<>(exceptions::invalid_request_exception(e.what()));
        });
    });
}


future<::shared_ptr<cql_transport::messages::result_message>>
cql3::statements::list_permissions_statement::execute(
        distributed<service::storage_proxy>& proxy,
        service::query_state& state,
        const query_options& options) {
    static auto make_column = [](sstring name) {
        return ::make_shared<column_specification>(
                auth::meta::AUTH_KS,
                "permissions",
                ::make_shared<column_identifier>(std::move(name), true),
                utf8_type);
    };

    static thread_local const std::vector<::shared_ptr<column_specification>> metadata({
        make_column("role"), make_column("resource"), make_column("permission")
    });

    typedef std::optional<auth::resource> opt_resource;

    std::vector<opt_resource> resources;

    auto r = _resource;
    for (;;) {
        resources.emplace_back(r);
        if (!r || !_recursive) {
            break;
        }

        auto parent = r->parent();
        if (!parent) {
            break;
        }

        r = std::move(parent);
    }

    return map_reduce(
            resources,
            [&state, this](opt_resource r) {
                return do_with(std::move(r), [this, &state](const opt_resource& r) {
                    auto& auth_service = *state.get_client_state().get_auth_service();
                    return auth_service.underlying_authorizer().list(
                            _permissions,
                            r,
                            _username,
                            auth_service).handle_exception_type([](const auth::nonexistant_role& e) {
                        return make_exception_future<std::vector<auth::permission_details>>(
                                exceptions::invalid_request_exception(e.what()));
                    });
                });
            },
            std::vector<auth::permission_details>(),
            [](std::vector<auth::permission_details> details, std::vector<auth::permission_details> pd) {
                details.insert(details.end(), pd.begin(), pd.end());
                return details;
{'fraction_non_alphanumeric': 0.10931550892630511, 'fraction_numerical': 0.0026228953380150605, 'mean_word_length': 2.7111459968602825, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2264502', 'n_tokens_mistral': 3186, 'n_tokens_neox': 3085, 'n_words': 876}