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

RBAC-2 #7257

Merged
merged 12 commits into from
Dec 6, 2019
1 change: 0 additions & 1 deletion dbms/programs/server/HTTPHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include <IO/WriteBufferFromTemporaryFile.h>
#include <DataStreams/IBlockInputStream.h>
#include <Interpreters/executeQuery.h>
#include <Interpreters/Quota.h>
#include <Common/typeid_cast.h>
#include <Poco/Net/HTTPStream.h>

Expand Down
1 change: 0 additions & 1 deletion dbms/programs/server/TCPHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <DataStreams/NativeBlockInputStream.h>
#include <DataStreams/NativeBlockOutputStream.h>
#include <Interpreters/executeQuery.h>
#include <Interpreters/Quota.h>
#include <Interpreters/TablesStatus.h>
#include <Interpreters/InternalTextLogsQueue.h>
#include <Storages/StorageMemory.h>
Expand Down
52 changes: 52 additions & 0 deletions dbms/src/Access/AccessControlManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <Access/AccessControlManager.h>
#include <Access/MultipleAccessStorage.h>
#include <Access/MemoryAccessStorage.h>
#include <Access/UsersConfigAccessStorage.h>
#include <Access/QuotaContextFactory.h>


namespace DB
{
namespace
{
std::vector<std::unique_ptr<IAccessStorage>> createStorages()
{
std::vector<std::unique_ptr<IAccessStorage>> list;
list.emplace_back(std::make_unique<MemoryAccessStorage>());
list.emplace_back(std::make_unique<UsersConfigAccessStorage>());
return list;
}
}


AccessControlManager::AccessControlManager()
: MultipleAccessStorage(createStorages()),
quota_context_factory(std::make_unique<QuotaContextFactory>(*this))
{
}


AccessControlManager::~AccessControlManager()
{
}


void AccessControlManager::loadFromConfig(const Poco::Util::AbstractConfiguration & users_config)
{
auto & users_config_access_storage = dynamic_cast<UsersConfigAccessStorage &>(getStorageByIndex(1));
users_config_access_storage.loadFromConfig(users_config);
}


std::shared_ptr<QuotaContext> AccessControlManager::createQuotaContext(
const String & user_name, const Poco::Net::IPAddress & address, const String & custom_quota_key)
{
return quota_context_factory->createContext(user_name, address, custom_quota_key);
}


std::vector<QuotaUsageInfo> AccessControlManager::getQuotaUsageInfo() const
{
return quota_context_factory->getUsageInfo();
}
}
45 changes: 45 additions & 0 deletions dbms/src/Access/AccessControlManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include <Access/MultipleAccessStorage.h>
#include <Poco/AutoPtr.h>
#include <memory>


namespace Poco
{
namespace Net
{
class IPAddress;
}
namespace Util
{
class AbstractConfiguration;
}
}

namespace DB
{
class QuotaContext;
class QuotaContextFactory;
struct QuotaUsageInfo;


/// Manages access control entities.
class AccessControlManager : public MultipleAccessStorage
{
public:
AccessControlManager();
~AccessControlManager();

void loadFromConfig(const Poco::Util::AbstractConfiguration & users_config);

std::shared_ptr<QuotaContext>
createQuotaContext(const String & user_name, const Poco::Net::IPAddress & address, const String & custom_quota_key);

std::vector<QuotaUsageInfo> getQuotaUsageInfo() const;

private:
std::unique_ptr<QuotaContextFactory> quota_context_factory;
};

}
19 changes: 19 additions & 0 deletions dbms/src/Access/IAccessEntity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <Access/IAccessEntity.h>
#include <Access/Quota.h>
#include <common/demangle.h>


namespace DB
{
String IAccessEntity::getTypeName(std::type_index type)
{
if (type == typeid(Quota))
return "Quota";
return demangle(type.name());
}

bool IAccessEntity::equal(const IAccessEntity & other) const
{
return (full_name == other.full_name) && (getType() == other.getType());
}
}
49 changes: 49 additions & 0 deletions dbms/src/Access/IAccessEntity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include <Core/Types.h>
#include <Common/typeid_cast.h>
#include <memory>
#include <typeindex>


namespace DB
{
/// Access entity is a set of data which have a name and a type. Access entity control something related to the access control.
/// Entities can be stored to a file or another storage, see IAccessStorage.
struct IAccessEntity
{
IAccessEntity() = default;
IAccessEntity(const IAccessEntity &) = default;
virtual ~IAccessEntity() = default;
virtual std::shared_ptr<IAccessEntity> clone() const = 0;

std::type_index getType() const { return typeid(*this); }
static String getTypeName(std::type_index type);
const String getTypeName() const { return getTypeName(getType()); }

template <typename EntityType>
bool isTypeOf() const { return isTypeOf(typeid(EntityType)); }
bool isTypeOf(std::type_index type) const { return type == getType(); }

virtual void setName(const String & name_) { full_name = name_; }
virtual String getName() const { return full_name; }
String getFullName() const { return full_name; }

friend bool operator ==(const IAccessEntity & lhs, const IAccessEntity & rhs) { return lhs.equal(rhs); }
friend bool operator !=(const IAccessEntity & lhs, const IAccessEntity & rhs) { return !(lhs == rhs); }

protected:
String full_name;

virtual bool equal(const IAccessEntity & other) const;

/// Helper function to define clone() in the derived classes.
template <typename EntityType>
std::shared_ptr<IAccessEntity> cloneImpl() const
{
return std::make_shared<EntityType>(typeid_cast<const EntityType &>(*this));
}
};

using AccessEntityPtr = std::shared_ptr<const IAccessEntity>;
}