Merge remote-tracking branch 'origin/topic/jsiwek/gh-591-set-enum-config'

* origin/topic/jsiwek/gh-591-set-enum-config:
  GH-591: allow Config::set_value() to use empty/unspecified table/sets
  GH-591: fix reading set[enum] values from input files
This commit is contained in:
Johanna Amann 2019-10-07 10:42:15 -07:00
commit 09f4053dbc
9 changed files with 202 additions and 3 deletions

View file

@ -1,4 +1,10 @@
3.1.0-dev.167 | 2019-10-07 10:42:15 -0700
* GH-591: allow Config::set_value() to use empty/unspecified table/sets (Jon Siwek, Corelight)
* GH-591: fix reading set[enum] values from input files (Jon Siwek, Corelight)
3.1.0-dev.164 | 2019-10-02 11:04:13 -0700
* Fix build with external CAF but bundled Broker (Dominik Charousset)

View file

@ -1 +1 @@
3.1.0-dev.164
3.1.0-dev.167

View file

@ -2536,8 +2536,31 @@ Val* Manager::ValueToVal(const Stream* i, const Value* val, bool& have_error) co
if ( stag == TYPE_VOID )
TypeTag stag = val->val.set_val.vals[0]->type;
set_index = new TypeList(base_type(stag)->Ref());
set_index->Append(base_type(stag)->Ref());
BroType* index_type;
if ( stag == TYPE_ENUM )
{
// Enums are not a base-type, so need to look it up.
const auto& sv = val->val.set_val.vals[0]->val.string_val;
std::string enum_name(sv.data, sv.length);
auto enum_id = global_scope()->Lookup(enum_name);
if ( ! enum_id )
{
Warning(i, "Value '%s' for stream '%s' is not a valid enum.",
enum_name.data(), i->name.c_str());
have_error = true;
return nullptr;
}
index_type = enum_id->Type()->AsEnumType();
}
else
index_type = base_type_no_ref(stag);
set_index = new TypeList(index_type);
set_index->Append(index_type->Ref());
}
SetType* s = new SetType(set_index, 0);

View file

@ -97,6 +97,18 @@ function Option::set%(ID: string, val: any, location: string &default=""%): bool
if ( ! same_type(i->Type(), val->Type()) )
{
if ( i->Type()->Tag() == TYPE_TABLE &&
val->Type()->Tag() == TYPE_TABLE &&
val->Type()->AsTableType()->IsUnspecifiedTable() )
{
// Just coerce an empty/unspecified table to the right type.
auto tv = new TableVal(i->Type()->AsTableType(),
i->ID_Val()->AsTableVal()->Attrs());
auto rval = call_option_handlers_and_set_value(ID, i, tv, location);
Unref(tv);
return val_mgr->GetBool(rval);
}
builtin_error(fmt("Incompatible type for set of ID '%s': got '%s', need '%s'",
ID->CheckString(), type_name(val->Type()->Tag()), type_name(i->Type()->Tag())));
return val_mgr->GetBool(0);

View file

@ -0,0 +1,41 @@
{
RED
}
{
}
{
BLUE
}
{
BLUE,
GREEN,
RED
}
{
}
---
[RED]
[]
[BLUE]
[RED, GREEN, BLUE]
[]
---
{
[RED] = red
}
{
}
{
[BLUE] = blue
}
{
[BLUE] = blue,
[GREEN] = green,
[RED] = red
}
{
}

View file

@ -0,0 +1,12 @@
DPD::ignore_violations, {
}
---
{
Analyzer::ANALYZER_SYSLOG
}
---
DPD::ignore_violations, {
Analyzer::ANALYZER_SYSLOG
}
---

View file

@ -0,0 +1,10 @@
#separator \x09
#set_separator ,
#empty_field (empty)
#unset_field -
#path config
#open 2019-10-03-04-02-02
#fields ts id old_value new_value location
#types time string string string string
1570075321.966826 DPD::ignore_violations (empty) Analyzer::ANALYZER_SYSLOG -
#close 2019-10-03-04-02-02

View file

@ -0,0 +1,49 @@
# @TEST-EXEC: zeek -b %INPUT >out
# @TEST-EXEC: btest-diff out
@load base/frameworks/config
@load base/frameworks/dpd
type Color: enum { RED, GREEN, BLUE };
option my_set: set[Color] = set(RED);
option my_vector: vector of Color = vector(RED);
option my_table: table[Color] of string = table([RED] = "red");
event zeek_init()
{
print my_set;
Config::set_value("my_set", set());
print my_set;
Config::set_value("my_set", set(BLUE));
print my_set;
Config::set_value("my_set", set(RED, GREEN, BLUE));
print my_set;
Config::set_value("my_set", set());
print my_set;
print "---";
print my_vector;
Config::set_value("my_vector", vector());
print my_vector;
Config::set_value("my_vector", vector(BLUE));
print my_vector;
Config::set_value("my_vector", vector(RED, GREEN, BLUE));
print my_vector;
Config::set_value("my_vector", vector());
print my_vector;
print "---";
print my_table;
Config::set_value("my_table", table());
print my_table;
Config::set_value("my_table", table([BLUE] = "blue"));
print my_table;
Config::set_value("my_table", table([RED] = "red", [GREEN] = "green", [BLUE] = "blue"));
print my_table;
Config::set_value("my_table", table());
print my_table;
}

View file

@ -0,0 +1,46 @@
# @TEST-EXEC: btest-bg-run zeek zeek -b %INPUT
# @TEST-EXEC: btest-bg-wait 10
# @TEST-EXEC: btest-diff zeek/.stdout
# @TEST-EXEC: btest-diff zeek/config.log
@TEST-START-FILE configfile4
DPD::ignore_violations Analyzer::ANALYZER_SYSLOG
@TEST-END-FILE
@load base/frameworks/config
@load base/frameworks/dpd
redef exit_only_after_terminate = T;
redef InputConfig::empty_field = "EMPTY";
redef InputConfig::set_separator = "\t";
type Idx: record {
option_name: string;
};
type Val: record {
option_val: string;
};
global currconfig: table[string] of string = table();
event InputConfig::new_value(name: string, source: string, id: string, value: any)
{
print id, lookup_ID(id);
print "---";
print value;
print "---";
Config::set_value(id, value);
print id, lookup_ID(id);
print "---";
}
event Input::end_of_data(name: string, source:string)
{
terminate();
}
event zeek_init()
{
Input::add_table([$reader=Input::READER_CONFIG, $source="../configfile4", $name="configuration", $idx=Idx, $val=Val, $destination=currconfig, $want_record=F]);
}