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

ApiListener::UpdateObjectAuthority(): distribute auth. by object's host #10161

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
27 changes: 24 additions & 3 deletions lib/remote/apilistener-authority.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void ApiListener::UpdateObjectAuthority()

std::vector<Endpoint::Ptr> endpoints;
Endpoint::Ptr my_endpoint;
int hostChildrenInheritObjectAuthority = 0;

if (my_zone) {
my_endpoint = Endpoint::GetLocalEndpoint();
Expand All @@ -51,6 +52,12 @@ void ApiListener::UpdateObjectAuthority()
return a->GetName() < b->GetName();
}
);

for (auto& endpoint : endpoints) {
if (endpoint == my_endpoint || endpoint->GetCapabilities() & (uint_fast64_t)ApiCapabilities::HostChildrenInheritObjectAuthority) {
++hostChildrenInheritObjectAuthority;
}
}
}

for (const Type::Ptr& type : Type::GetAllTypes()) {
Expand All @@ -65,10 +72,24 @@ void ApiListener::UpdateObjectAuthority()

bool authority;

if (!my_zone)
if (my_zone) {
auto name (object->GetName());

// If all endpoints know this algorithm, we can use it.
if (hostChildrenInheritObjectAuthority == endpoints.size()) {
auto exclamation (name.FindFirstOf('!'));

// Pin child objects of hosts (HOST!...) to the same endpoint as the host.
// This reduces cross-object action latency withing the same host.
if (exclamation != String::NPos) {
name = name.SubStr(0, exclamation);
}
}

authority = endpoints[Utility::SDBM(name) % endpoints.size()] == my_endpoint;
} else {
authority = true;
else
authority = endpoints[Utility::SDBM(object->GetName()) % endpoints.size()] == my_endpoint;
}

#ifdef I2_DEBUG
// //Enable on demand, causes heavy logging on each run.
Expand Down
25 changes: 21 additions & 4 deletions lib/remote/apilistener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,9 @@ static const auto l_AppVersionInt (([]() -> unsigned long {
})());

static const auto l_MyCapabilities (
(uint_fast64_t)ApiCapabilities::ExecuteArbitraryCommand | (uint_fast64_t)ApiCapabilities::IfwApiCheckCommand
(uint_fast64_t)ApiCapabilities::ExecuteArbitraryCommand
| (uint_fast64_t)ApiCapabilities::IfwApiCheckCommand
| (uint_fast64_t)ApiCapabilities::HostChildrenInheritObjectAuthority
);

/**
Expand Down Expand Up @@ -852,11 +854,22 @@ void ApiListener::NewClientHandlerInternal(
JsonRpcConnection::Ptr aclient = new JsonRpcConnection(identity, verify_ok, client, role);

if (endpoint) {
endpoint->AddClient(aclient);
const auto reuniteCluster ([this, endpoint](const JsonRpcConnection::Ptr& aclient) {
endpoint->AddClient(aclient);

Utility::QueueAsyncCallback([this, aclient, endpoint]() {
SyncClient(aclient, endpoint, true);
Utility::QueueAsyncCallback([this, aclient, endpoint] {
SyncClient(aclient, endpoint, true);
});
});

if (endpoint->GetZone() == Zone::GetLocalZone()) {
// Nodes in the same zone, which do teamwork, must know each other's capabilities in advance.
// Hence, we delay everything else until they're told us via an icinga::Hello message.
// Even v2.12 nodes send this message at the beginning, so we're safe.
aclient->OnNextHello.connect(reuniteCluster);
} else {
reuniteCluster(aclient);
}
} else if (!AddAnonymousClient(aclient)) {
Log(LogNotice, "ApiListener")
<< "Ignoring anonymous JSON-RPC connection " << conninfo
Expand Down Expand Up @@ -1825,6 +1838,10 @@ Value ApiListener::HelloAPIHandler(const MessageOrigin::Ptr& origin, const Dicti
}
}
}

decltype(client->OnNextHello) listeners;
std::swap(client->OnNextHello, listeners);
listeners(client);
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/remote/apilistener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ enum class ApiCapabilities : uint_fast64_t
{
ExecuteArbitraryCommand = 1u << 0u,
IfwApiCheckCommand = 1u << 1u,
HostChildrenInheritObjectAuthority = 1u << 2u,
};

/**
Expand Down
4 changes: 4 additions & 0 deletions lib/remote/jsonrpcconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <boost/asio/io_context.hpp>
#include <boost/asio/io_context_strand.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/signals2.hpp>

namespace icinga
{
Expand Down Expand Up @@ -59,6 +60,9 @@ class JsonRpcConnection final : public Object
void SendMessage(const Dictionary::Ptr& request);
void SendRawMessage(const String& request);

// On remote icinga::Hello, this signal is emitted and all handlers are removed from it, atomically.
boost::signals2::signal<void(const Ptr&)> OnNextHello;

static Value HeartbeatAPIHandler(const intrusive_ptr<MessageOrigin>& origin, const Dictionary::Ptr& params);

static double GetWorkQueueRate();
Expand Down
Loading