[libcalamares] Introduce redaction-of-names class for logging

- redacted names are stable inside of one run of Calamares
- random, private displays of a given string for a context

SEE #1593
This commit is contained in:
Adriaan de Groot 2021-11-16 14:47:13 +01:00
parent 7b3c4db8f0
commit 152b3c333b
2 changed files with 49 additions and 0 deletions

View File

@ -20,6 +20,8 @@
#include <QDir>
#include <QFileInfo>
#include <QMutex>
#include <QRandomGenerator>
#include <QTextStream>
#include <QTime>
#include <QVariant>
@ -252,4 +254,34 @@ operator<<( QDebug& s, const RedactedCommand& l )
return s;
}
/** @brief Returns a stable-but-private hash of @p context and @p s
*
* Identical strings with the same context will be hashed the same,
* so that they can be logged and still recognized as the-same.
*/
static uint insertRedactedName( const QString& context, const QString& s )
{
static uint salt = QRandomGenerator::global()->generate(); // Just once
uint val = qHash(context, salt);
return qHash(s, val);
}
RedactedName::RedactedName( const QString& context, const QString& s )
: m_id( insertRedactedName(context, s) ),
m_context(context)
{
}
RedactedName::RedactedName(const char *context, const QString& s )
: RedactedName( QString::fromLatin1(context), s )
{
}
QDebug&
operator<< ( QDebug& s, const RedactedName& n )
{
return s << NoQuote << n.m_context << '$' << n.m_id << Quote;
}
} // namespace Logger

View File

@ -226,6 +226,23 @@ struct RedactedCommand
QDebug& operator<<( QDebug& s, const RedactedCommand& l );
/** @brief When logging "private" identifiers, keep them consistent but private
*
* Send a string to a logger in such a way that each time it is logged,
* it logs the same way, but without revealing the actual contents.
* This can be applied to user names, UUIDs, etc.
*/
struct RedactedName
{
RedactedName( const char* context, const QString& s );
RedactedName( const QString& context, const QString& s );
const uint m_id;
const QString m_context;
};
QDebug& operator<<( QDebug& s, const RedactedName& n );
/**
* @brief Formatted logging of a pointer
*