Skip to content

Commit b3ef405

Browse files
[ICD] Finish ICDM server refactor (project-chip#30703)
* clean up icdm server * rename variables * move CheckAdmin to anonymous namespace
1 parent c8bea78 commit b3ef405

File tree

2 files changed

+75
-56
lines changed

2 files changed

+75
-56
lines changed

src/app/clusters/icd-management-server/icd-management-server.cpp

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,30 @@ class IcdManagementFabricDelegate : public FabricTable::Delegate
188188
IcdManagementFabricDelegate gFabricDelegate;
189189
IcdManagementAttributeAccess gAttribute;
190190

191+
/**
192+
* @brief Function checks if the client as admin permissions to the cluster in the commandPath
193+
*
194+
* @param[out] isClientAdmin True : Client has admin permissions
195+
* False : Client does not have admin permissions
196+
* If an error ocurs, isClientAdmin is not changed
197+
* @return CHIP_ERROR
198+
*/
199+
CHIP_ERROR CheckAdmin(CommandHandler * commandObj, const ConcreteCommandPath & commandPath, bool & isClientAdmin)
200+
{
201+
RequestPath requestPath{ .cluster = commandPath.mClusterId, .endpoint = commandPath.mEndpointId };
202+
CHIP_ERROR err = GetAccessControl().Check(commandObj->GetSubjectDescriptor(), requestPath, Privilege::kAdminister);
203+
if (CHIP_NO_ERROR == err)
204+
{
205+
isClientAdmin = true;
206+
}
207+
else if (CHIP_ERROR_ACCESS_DENIED == err)
208+
{
209+
isClientAdmin = false;
210+
err = CHIP_NO_ERROR;
211+
}
212+
return err;
213+
}
214+
191215
} // namespace
192216

193217
/*
@@ -198,22 +222,32 @@ PersistentStorageDelegate * ICDManagementServer::mStorage = nullptr;
198222
Crypto::SymmetricKeystore * ICDManagementServer::mSymmetricKeystore = nullptr;
199223
ICDConfigurationData * ICDManagementServer::mICDConfigurationData = nullptr;
200224

201-
Status ICDManagementServer::RegisterClient(FabricIndex fabric_index, NodeId node_id, uint64_t monitored_subject, ByteSpan key,
202-
Optional<ByteSpan> verification_key, bool isAdmin, uint32_t & icdCounter)
225+
Status ICDManagementServer::RegisterClient(CommandHandler * commandObj, const ConcreteCommandPath & commandPath,
226+
const Commands::RegisterClient::DecodableType & commandData, uint32_t & icdCounter)
203227
{
228+
FabricIndex fabricIndex = commandObj->GetAccessingFabricIndex();
229+
NodeId nodeId = commandData.checkInNodeID;
230+
uint64_t monitoredSubject = commandData.monitoredSubject;
231+
ByteSpan key = commandData.key;
232+
Optional<ByteSpan> verificationKey = commandData.verificationKey;
233+
bool isClientAdmin = false;
234+
235+
// Check if client is admin
236+
VerifyOrReturnError(CHIP_NO_ERROR == CheckAdmin(commandObj, commandPath, isClientAdmin), InteractionModel::Status::Failure);
237+
204238
bool isFirstEntryForFabric = false;
205-
ICDMonitoringTable table(*mStorage, fabric_index, mICDConfigurationData->GetClientsSupportedPerFabric(), mSymmetricKeystore);
239+
ICDMonitoringTable table(*mStorage, fabricIndex, mICDConfigurationData->GetClientsSupportedPerFabric(), mSymmetricKeystore);
206240

207241
// Get current entry, if exists
208242
ICDMonitoringEntry entry(mSymmetricKeystore);
209-
CHIP_ERROR err = table.Find(node_id, entry);
243+
CHIP_ERROR err = table.Find(nodeId, entry);
210244
if (CHIP_NO_ERROR == err)
211245
{
212246
// Existing entry: Validate Key if, and only if, the ISD does NOT have administrator permissions
213-
if (!isAdmin)
247+
if (!isClientAdmin)
214248
{
215-
VerifyOrReturnError(verification_key.HasValue(), InteractionModel::Status::Failure);
216-
VerifyOrReturnError(entry.IsKeyEquivalent(verification_key.Value()), InteractionModel::Status::Failure);
249+
VerifyOrReturnError(verificationKey.HasValue(), InteractionModel::Status::Failure);
250+
VerifyOrReturnError(entry.IsKeyEquivalent(verificationKey.Value()), InteractionModel::Status::Failure);
217251
}
218252
}
219253
else if (CHIP_ERROR_NOT_FOUND == err)
@@ -231,8 +265,8 @@ Status ICDManagementServer::RegisterClient(FabricIndex fabric_index, NodeId node
231265
}
232266

233267
// Save
234-
entry.checkInNodeID = node_id;
235-
entry.monitoredSubject = monitored_subject;
268+
entry.checkInNodeID = nodeId;
269+
entry.monitoredSubject = monitoredSubject;
236270
if (entry.keyHandleValid)
237271
{
238272
entry.DeleteKey();
@@ -262,19 +296,27 @@ Status ICDManagementServer::RegisterClient(FabricIndex fabric_index, NodeId node
262296
return InteractionModel::Status::Success;
263297
}
264298

265-
Status ICDManagementServer::UnregisterClient(FabricIndex fabric_index, NodeId node_id, Optional<ByteSpan> verificationKey,
266-
bool isAdmin)
299+
Status ICDManagementServer::UnregisterClient(CommandHandler * commandObj, const ConcreteCommandPath & commandPath,
300+
const Commands::UnregisterClient::DecodableType & commandData)
267301
{
268-
ICDMonitoringTable table(*mStorage, fabric_index, mICDConfigurationData->GetClientsSupportedPerFabric(), mSymmetricKeystore);
302+
FabricIndex fabricIndex = commandObj->GetAccessingFabricIndex();
303+
NodeId nodeId = commandData.checkInNodeID;
304+
Optional<ByteSpan> verificationKey = commandData.verificationKey;
305+
bool isClientAdmin = false;
306+
307+
// Check if client is admin
308+
VerifyOrReturnError(CHIP_NO_ERROR == CheckAdmin(commandObj, commandPath, isClientAdmin), InteractionModel::Status::Failure);
309+
310+
ICDMonitoringTable table(*mStorage, fabricIndex, mICDConfigurationData->GetClientsSupportedPerFabric(), mSymmetricKeystore);
269311

270312
// Get current entry, if exists
271313
ICDMonitoringEntry entry(mSymmetricKeystore);
272-
CHIP_ERROR err = table.Find(node_id, entry);
314+
CHIP_ERROR err = table.Find(nodeId, entry);
273315
VerifyOrReturnError(CHIP_ERROR_NOT_FOUND != err, InteractionModel::Status::NotFound);
274316
VerifyOrReturnError(CHIP_NO_ERROR == err, InteractionModel::Status::Failure);
275317

276318
// Existing entry: Validate Key if, and only if, the ISD has NOT administrator permissions
277-
if (!isAdmin)
319+
if (!isClientAdmin)
278320
{
279321
VerifyOrReturnError(verificationKey.HasValue(), InteractionModel::Status::Failure);
280322
VerifyOrReturnError(entry.IsKeyEquivalent(verificationKey.Value()), InteractionModel::Status::Failure);
@@ -291,7 +333,7 @@ Status ICDManagementServer::UnregisterClient(FabricIndex fabric_index, NodeId no
291333
return InteractionModel::Status::Success;
292334
}
293335

294-
Status ICDManagementServer::StayActiveRequest(FabricIndex fabric_index)
336+
Status ICDManagementServer::StayActiveRequest(FabricIndex fabricIndex)
295337
{
296338
// TODO: Implementent stay awake logic for end device
297339
// https://github.com/project-chip/connectedhomeip/issues/24259
@@ -304,22 +346,6 @@ void ICDManagementServer::TriggerICDMTableUpdatedEvent()
304346
ICDNotifier::GetInstance().BroadcastICDManagementEvent(ICDListener::ICDManagementEvents::kTableUpdated);
305347
}
306348

307-
CHIP_ERROR ICDManagementServer::CheckAdmin(CommandHandler * commandObj, const ConcreteCommandPath & commandPath, bool & isAdmin)
308-
{
309-
RequestPath requestPath{ .cluster = commandPath.mClusterId, .endpoint = commandPath.mEndpointId };
310-
CHIP_ERROR err = GetAccessControl().Check(commandObj->GetSubjectDescriptor(), requestPath, Privilege::kAdminister);
311-
if (CHIP_NO_ERROR == err)
312-
{
313-
isAdmin = true;
314-
}
315-
else if (CHIP_ERROR_ACCESS_DENIED == err)
316-
{
317-
isAdmin = false;
318-
err = CHIP_NO_ERROR;
319-
}
320-
return err;
321-
}
322-
323349
void ICDManagementServer::Init(PersistentStorageDelegate & storage, Crypto::SymmetricKeystore * symmetricKeystore,
324350
ICDConfigurationData & icdConfigurationData)
325351
{
@@ -339,17 +365,10 @@ void ICDManagementServer::Init(PersistentStorageDelegate & storage, Crypto::Symm
339365
bool emberAfIcdManagementClusterRegisterClientCallback(CommandHandler * commandObj, const ConcreteCommandPath & commandPath,
340366
const Commands::RegisterClient::DecodableType & commandData)
341367
{
342-
InteractionModel::Status status = InteractionModel::Status::Failure;
343-
bool isAdmin = false;
344-
uint32_t icdCounter = 0;
345-
ICDManagementServer server;
368+
uint32_t icdCounter = 0;
346369

347-
if (CHIP_NO_ERROR == server.CheckAdmin(commandObj, commandPath, isAdmin))
348-
{
349-
status =
350-
server.RegisterClient(commandObj->GetAccessingFabricIndex(), commandData.checkInNodeID, commandData.monitoredSubject,
351-
commandData.key, commandData.verificationKey, isAdmin, icdCounter);
352-
}
370+
ICDManagementServer server;
371+
InteractionModel::Status status = server.RegisterClient(commandObj, commandPath, commandData, icdCounter);
353372

354373
if (InteractionModel::Status::Success == status)
355374
{
@@ -371,15 +390,8 @@ bool emberAfIcdManagementClusterRegisterClientCallback(CommandHandler * commandO
371390
bool emberAfIcdManagementClusterUnregisterClientCallback(CommandHandler * commandObj, const ConcreteCommandPath & commandPath,
372391
const Commands::UnregisterClient::DecodableType & commandData)
373392
{
374-
InteractionModel::Status status = InteractionModel::Status::Failure;
375-
bool isAdmin = false;
376393
ICDManagementServer server;
377-
378-
if (CHIP_NO_ERROR == server.CheckAdmin(commandObj, commandPath, isAdmin))
379-
{
380-
status = server.UnregisterClient(commandObj->GetAccessingFabricIndex(), commandData.checkInNodeID,
381-
commandData.verificationKey, isAdmin);
382-
}
394+
InteractionModel::Status status = server.UnregisterClient(commandObj, commandPath, commandData);
383395

384396
commandObj->AddStatus(commandPath, status);
385397
return true;

src/app/clusters/icd-management-server/icd-management-server.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#pragma once
1919

20+
#include <app-common/zap-generated/cluster-objects.h>
2021
#include <app/CommandHandler.h>
2122
#include <app/ConcreteAttributePath.h>
2223
#include <app/icd/ICDConfigurationData.h>
@@ -37,16 +38,22 @@ class ICDManagementServer
3738

3839
static void Init(chip::PersistentStorageDelegate & storage, chip::Crypto::SymmetricKeystore * symmetricKeystore,
3940
chip::ICDConfigurationData & ICDConfigurationData);
40-
Status RegisterClient(chip::FabricIndex fabric_index, chip::NodeId node_id, uint64_t monitored_subject, chip::ByteSpan key,
41-
chip::Optional<chip::ByteSpan> verification_key, bool is_admin, uint32_t & icdCounter);
4241

43-
Status UnregisterClient(chip::FabricIndex fabric_index, chip::NodeId node_id, chip::Optional<chip::ByteSpan> verificationKey,
44-
bool is_admin);
42+
/**
43+
* @brief Function that executes the business logic of the RegisterClient Command
44+
*
45+
* @param[out] icdCounter If function succeeds, icdCounter will have the current value of the ICDCounter stored in the
46+
* ICDConfigurationData If function fails, icdCounter will be unchanged
47+
* @return Status
48+
*/
49+
Status RegisterClient(chip::app::CommandHandler * commandObj, const chip::app::ConcreteCommandPath & commandPath,
50+
const chip::app::Clusters::IcdManagement::Commands::RegisterClient::DecodableType & commandData,
51+
uint32_t & icdCounter);
4552

46-
Status StayActiveRequest(chip::FabricIndex fabric_index);
53+
Status UnregisterClient(chip::app::CommandHandler * commandObj, const chip::app::ConcreteCommandPath & commandPath,
54+
const chip::app::Clusters::IcdManagement::Commands::UnregisterClient::DecodableType & commandData);
4755

48-
CHIP_ERROR CheckAdmin(chip::app::CommandHandler * commandObj, const chip::app::ConcreteCommandPath & commandPath,
49-
bool & is_admin);
56+
Status StayActiveRequest(chip::FabricIndex fabricIndex);
5057

5158
private:
5259
/**

0 commit comments

Comments
 (0)