Skip to content

Commit

Permalink
Add support for additional collations in column definitions (osquery#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Micah-Kolide authored Dec 18, 2023
1 parent a184da4 commit 1243c73
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
6 changes: 4 additions & 2 deletions osquery/core/sql/column.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ enum class ColumnOptions {
/// This column should be hidden from '*'' selects.
HIDDEN = 16,

// This sets the collating sequence to NOCASE
COLLATENOCASE = 32,
// Set the collation sequence for this column.
COLLATEBINARY = 32,
COLLATENOCASE = 64,
COLLATERTRIM = 128,
};

/// Treat column options as a set of flags.
Expand Down
6 changes: 5 additions & 1 deletion osquery/core/tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,12 @@ std::string columnDefinition(const TableColumns& columns, bool is_extension) {
if (options & ColumnOptions::HIDDEN) {
statement += " HIDDEN";
}
if (options & ColumnOptions::COLLATENOCASE) {
if (options & ColumnOptions::COLLATEBINARY) {
statement += " COLLATE BINARY";
} else if (options & ColumnOptions::COLLATENOCASE) {
statement += " COLLATE NOCASE";
} else if (options & ColumnOptions::COLLATERTRIM) {
statement += " COLLATE RTRIM";
}
if (i < columns.size() - 1) {
statement += ", ";
Expand Down
2 changes: 1 addition & 1 deletion specs/windows/registry.table
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
table_name("registry")
description("All of the Windows registry hives.")
schema([
Column("key", TEXT, "Name of the key to search for", additional=True, collate_nocase=True),
Column("key", TEXT, "Name of the key to search for", additional=True, collate="nocase"),
Column("path", TEXT, "Full path to the value", index=True),
Column("name", TEXT, "Name of the registry value entry"),
Column("type", TEXT, "Type of the registry value, or 'subkey' if item is a subkey"),
Expand Down
22 changes: 20 additions & 2 deletions tools/codegen/gentable.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,17 @@ def __repr__(self):
"required": "REQUIRED",
"optimized": "OPTIMIZED",
"hidden": "HIDDEN",
"collate_nocase": "COLLATENOCASE",
"collate": "COLLATE",
}

# Available collation sequences to set on column definitions.
# This should mimic the collation sequences in ColumnOptions.
COLLATIONS = [
"BINARY",
"NOCASE",
"RTRIM",
]

# Column options that render tables uncacheable.
NON_CACHEABLE = [
"REQUIRED",
Expand Down Expand Up @@ -215,7 +223,17 @@ def generate(self, path, template="default"):
for option in column.options:
# Only allow explicitly-defined options.
if option in COLUMN_OPTIONS:
column_options.append("ColumnOptions::" + COLUMN_OPTIONS[option])
if option == "collate":
collation = str(column.options["collate"]).upper()
if collation in COLLATIONS:
column_options.append("ColumnOptions::" + COLUMN_OPTIONS[option] + collation)
else:
print(lightred(
"Table %s column %s contains an unknown collation sequence: %s" % (
self.table_name, column.name, column.options["collate"])))
exit(1)
else:
column_options.append("ColumnOptions::" + COLUMN_OPTIONS[option])
all_options.append(COLUMN_OPTIONS[option])
else:
print(yellow(
Expand Down

0 comments on commit 1243c73

Please sign in to comment.