Skip to content

Commit

Permalink
[SecurityAgent] TokenDispatcher now uses RPC::Communicator instead of…
Browse files Browse the repository at this point in the history
… IPCServer - allowing multiple connections
  • Loading branch information
Lukasz Iwan authored and Santhosh Ramani committed Jun 16, 2021
1 parent a690181 commit 6c37065
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 70 deletions.
65 changes: 29 additions & 36 deletions SecurityAgent/SecurityAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,11 @@ namespace Plugin {
const string _callsign;
};

void SecurityAgent::TokenDispatcher::Tokenize::Procedure(Core::IPCChannel& source, Core::ProxyType<Core::IIPC>& data) {
Core::ProxyType<IPC::SecurityAgent::TokenData> message = Core::proxy_cast<IPC::SecurityAgent::TokenData>(data);

ASSERT (message.IsValid() == true);

if (message.IsValid() == true) {
string token;
if (_parent->CreateToken(message->Parameters().Length(), message->Parameters().Value(), token) == Core::ERROR_NONE) {
message->Response().Set(static_cast<uint16_t>(token.length()), reinterpret_cast<const uint8_t*>(token.c_str()));
source.ReportResponse(data);
}
else {
TRACE(Trace::Fatal, ("Could not create a security token."));
}
}
}

SecurityAgent::SecurityAgent() : _dispatcher(nullptr)
SecurityAgent::SecurityAgent()
: _secretKey()
, _acl()
, _dispatcher(nullptr)
, _engine()
{
RegisterAll();

Expand All @@ -97,6 +84,8 @@ namespace Plugin {
_skipURL = static_cast<uint8_t>(service->WebPrefix().length());
Core::File aclFile(service->PersistentPath() + config.ACL.Value(), true);

PluginHost::ISubSystem* subSystem = service->SubSystems();

if (aclFile.Exists() == false) {
aclFile = service->DataPath() + config.ACL.Value();
}
Expand All @@ -114,29 +103,36 @@ namespace Plugin {
}
}

PluginHost::ISubSystem* subSystem = service->SubSystems();

ASSERT(_dispatcher == nullptr);
ASSERT(subSystem != nullptr);

if (subSystem != nullptr) {
Core::Sink<SecurityCallsign> information(service->Callsign());

if (subSystem->IsActive(PluginHost::ISubSystem::SECURITY) != false) {
SYSLOG(Logging::Startup, (_T("Security is not defined as External !!")));
}
string connector = config.Connector.Value();

subSystem->Set(PluginHost::ISubSystem::SECURITY, &information);
subSystem->Release();
if (connector.empty() == true) {
connector = service->VolatilePath() + _T("token");
}
_engine = Core::ProxyType<RPC::InvokeServer>::Create(&Core::IWorkerPool::Instance());
_dispatcher.reset(new TokenDispatcher(Core::NodeId(connector.c_str()), service->ProxyStubPath(), this, _engine));

ASSERT(_dispatcher == nullptr);
if (_dispatcher != nullptr) {

string connector = config.Connector.Value();
if (_dispatcher->IsListening() == false) {
_dispatcher.reset(nullptr);
_engine.Release();
} else {
if (subSystem != nullptr) {
Core::SystemInfo::SetEnvironment(_T("SECURITYAGENT_PATH"), config.Connector.Value(), true);
Core::Sink<SecurityCallsign> information(service->Callsign());

if (connector.empty() == true) {
connector = service->VolatilePath() + _T("token");
if (subSystem->IsActive(PluginHost::ISubSystem::SECURITY) != false) {
SYSLOG(Logging::Startup, (_T("Security is not defined as External !!")));
}

subSystem->Set(PluginHost::ISubSystem::SECURITY, &information);
subSystem->Release();
}
}
}
_dispatcher = new TokenDispatcher(Core::NodeId(connector.c_str()), this);

// On success return empty, to indicate there is no error text.
return _T("");
Expand All @@ -148,9 +144,6 @@ namespace Plugin {

ASSERT(subSystem != nullptr);

delete _dispatcher;
_dispatcher = nullptr;

if (subSystem != nullptr) {
subSystem->Set(PluginHost::ISubSystem::NOT_SECURITY, nullptr);
subSystem->Release();
Expand Down
71 changes: 37 additions & 34 deletions SecurityAgent/SecurityAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,52 +33,54 @@ namespace Plugin {
public PluginHost::JSONRPC,
public PluginHost::IWeb {
private:
class TokenDispatcher {
private:
class TokenDispatcher : public RPC::Communicator {
public:
TokenDispatcher() = delete;
TokenDispatcher(const TokenDispatcher&) = delete;
TokenDispatcher& operator=(const TokenDispatcher&) = delete;

private:
class Tokenize : public Core::IIPCServer {
private:
Tokenize(const Tokenize&) = delete;
Tokenize& operator=(const Tokenize&) = delete;

public:
Tokenize(PluginHost::IAuthenticate* parent) : _parent(parent)
{
TokenDispatcher(
const Core::NodeId& source,
const std::string& proxyStubPath,
PluginHost::IAuthenticate* parentInterface,
const Core::ProxyType<RPC::InvokeServer>& engine
)
: RPC::Communicator(source, proxyStubPath, Core::ProxyType<Core::IIPCServer>(engine))
, _parentInterface(parentInterface)
{
if(_parentInterface != nullptr){
_parentInterface->AddRef();
}
virtual ~Tokenize()
{
engine->Announcements(Announcement());
Open(Core::infinite);
}
~TokenDispatcher() override
{
if(_parentInterface != nullptr){
_parentInterface->Release();
}

public:
void Procedure(Core::IPCChannel& source, Core::ProxyType<Core::IIPC>& data) override;

private:
PluginHost::IAuthenticate* _parent;
};
Close(Core::infinite);
}

public:
TokenDispatcher(const Core::NodeId& endPoint, PluginHost::IAuthenticate* officer)
: _channel(endPoint, 1024)
private:
void* Aquire(const string&, const uint32_t interfaceId, const uint32_t versionId) override
{
Core::SystemInfo::SetEnvironment(_T("SECURITYAGENT_PATH"), endPoint.QualifiedName().c_str());
void* result = nullptr;

_channel.CreateFactory<IPC::SecurityAgent::TokenData>(1);
_channel.Register(IPC::SecurityAgent::TokenData::Id(), Core::ProxyType<Core::IIPCServer>(Core::ProxyType<Tokenize>::Create(officer)));
if (((versionId == 1) || (versionId == static_cast<uint32_t>(~0))) && ((interfaceId == PluginHost::IAuthenticate::ID) || (interfaceId == Core::IUnknown::ID))) {

_parentInterface->AddRef();

_channel.Open(0);
}
~TokenDispatcher()
{
_channel.Close(Core::infinite);
_channel.Unregister(IPC::SecurityAgent::TokenData::Id());
_channel.DestroyFactory<IPC::SecurityAgent::TokenData>();

TRACE(Trace::Information, ("SecurityAgent interface(IAuthenticate) aquired => %p", this));
result = _parentInterface;
}
return (result);
}

private:
Core::IPCChannelClientType<Core::Void, true, true> _channel;
PluginHost::IAuthenticate* _parentInterface;
};

class Config : public Core::JSON::Container {
Expand Down Expand Up @@ -162,7 +164,8 @@ namespace Plugin {
uint8_t _secretKey[Crypto::SHA256::Length];
AccessControlList _acl;
uint8_t _skipURL;
TokenDispatcher* _dispatcher;
std::unique_ptr<TokenDispatcher> _dispatcher;
Core::ProxyType<RPC::InvokeServer> _engine;
};

} // namespace Plugin
Expand Down

0 comments on commit 6c37065

Please sign in to comment.