[cli] add "dataset active|pending -ns" support (#11518)

This CLI command argument "-ns" prints out the dataset fields and
redact the sensitive values, including the network key and PSKc
fields.
This commit is contained in:
Tony Zhou
2025-06-03 15:20:49 +00:00
committed by GitHub
parent ea3a3da0ee
commit 5050bec030
4 changed files with 108 additions and 26 deletions
+57 -23
View File
@@ -578,28 +578,29 @@ exit:
return error;
}
otError Dataset::Print(otOperationalDatasetTlvs &aDatasetTlvs)
otError Dataset::Print(otOperationalDatasetTlvs &aDatasetTlvs, bool aNonsensitiveOnly)
{
struct ComponentTitle
{
const char *mTitle; // Title to output.
const char *mName; // To use with `LookupMapper()`.
const char *mTitle; // Title to output.
const char *mName; // To use with `LookupMapper()`.
bool mIsSensitive; // Whether the field is sensitive.
};
static const ComponentTitle kTitles[] = {
{"Pending Timestamp", "pendingtimestamp"},
{"Active Timestamp", "activetimestamp"},
{"Channel", "channel"},
{"Wake-up Channel", "wakeupchannel"},
{"Channel Mask", "channelmask"},
{"Delay", "delay"},
{"Ext PAN ID", "extpanid"},
{"Mesh Local Prefix", "meshlocalprefix"},
{"Network Key", "networkkey"},
{"Network Name", "networkname"},
{"PAN ID", "panid"},
{"PSKc", "pskc"},
{"Security Policy", "securitypolicy"},
{"Pending Timestamp", "pendingtimestamp", false},
{"Active Timestamp", "activetimestamp", false},
{"Channel", "channel", false},
{"Wake-up Channel", "wakeupchannel", false},
{"Channel Mask", "channelmask", false},
{"Delay", "delay", false},
{"Ext PAN ID", "extpanid", false},
{"Mesh Local Prefix", "meshlocalprefix", false},
{"Network Key", "networkkey", true},
{"Network Name", "networkname", false},
{"PAN ID", "panid", false},
{"PSKc", "pskc", true},
{"Security Policy", "securitypolicy", false},
};
otError error;
@@ -609,12 +610,21 @@ otError Dataset::Print(otOperationalDatasetTlvs &aDatasetTlvs)
for (const ComponentTitle &title : kTitles)
{
const ComponentMapper *mapper = LookupMapper(title.mName);
const ComponentMapper *mapper;
mapper = LookupMapper(title.mName);
if (dataset.mComponents.*mapper->mIsPresentPtr)
{
OutputFormat("%s: ", title.mTitle);
(this->*mapper->mOutput)(dataset);
if (aNonsensitiveOnly && title.mIsSensitive)
{
OutputLine("[Redacted]");
}
else
{
(this->*mapper->mOutput)(dataset);
}
}
}
@@ -688,8 +698,24 @@ exit:
* 0e08000000000001000000030000103506000...3023d82c841eff0e68db86f35740c030000ff
* Done
* @endcode
* @cparam dataset active [-x]
* The optional `-x` argument prints the Active Operational %Dataset values as hex-encoded TLVs.
* @code
* dataset active -ns
* Active Timestamp: 1
* Channel: 13
* Channel Mask: 0x07fff800
* Ext PAN ID: d63e8e3e495ebbc3
* Mesh Local Prefix: fd3d:b50b:f96d:722d::/64
* Network Key: [Redacted]
* Network Name: OpenThread-8f28
* PAN ID: 0x8f28
* PSKc: [Redacted]
* Security Policy: 0, onrcb
* Done
* @endcode
* @cparam dataset active [-x|-ns]
* * The optional `-x` argument prints the Active Operational Dataset values as hex-encoded TLVs.
* * The optional `-ns` argument prints the Active Operational Dataset values and redact the sensitive values, including
* the network key and PSKc fields.
* @par api_copy
* #otDatasetGetActive
* @par
@@ -704,12 +730,16 @@ template <> otError Dataset::Process<Cmd("active")>(Arg aArgs[])
if (aArgs[0].IsEmpty())
{
error = Print(dataset);
error = Print(dataset, /* aNonsensitiveOnly */ false);
}
else if (aArgs[0] == "-x")
{
OutputBytesLine(dataset.mTlvs, dataset.mLength);
}
else if (aArgs[0] == "-ns")
{
error = Print(dataset, /* aNonsensitiveOnly */ true);
}
else
{
error = OT_ERROR_INVALID_ARGS;
@@ -728,12 +758,16 @@ template <> otError Dataset::Process<Cmd("pending")>(Arg aArgs[])
if (aArgs[0].IsEmpty())
{
error = Print(datasetTlvs);
error = Print(datasetTlvs, /* aNonsensitiveOnly */ false);
}
else if (aArgs[0] == "-x")
{
OutputBytesLine(datasetTlvs.mTlvs, datasetTlvs.mLength);
}
else if (aArgs[0] == "-ns")
{
error = Print(datasetTlvs, /* aNonsensitiveOnly */ true);
}
else
{
error = OT_ERROR_INVALID_ARGS;
@@ -1283,7 +1317,7 @@ otError Dataset::Process(Arg aArgs[])
if (aArgs[0].IsEmpty())
{
ExitNow(error = Print(sDatasetTlvs));
ExitNow(error = Print(sDatasetTlvs, /* aNonsensitiveOnly */ false));
}
/**