登录
首页 >  Golang >  Go问答

提供表规范时如何在添加虚拟表上进行扩展

来源:stackoverflow

时间:2024-03-14 20:06:27 369浏览 收藏

Golang不知道大家是否熟悉?今天我将给大家介绍《提供表规范时如何在添加虚拟表上进行扩展》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!

问题内容

我正在使用 osquery-go 构建一个 osquery 扩展,它为 osqueryi 提供了一个虚拟表。我的表需要在特定字段上使用 where 子句来生成结果。我的桌子的规格应该放在哪里?

正如 osquery 文档中所述,规范通常在规范源文件夹中提供。但对于扩展,我不知道该怎么做。

我使用 osquery-go 上提供的示例作为起点,它运行得很好。我还可以使用约束过滤输入,但我想收到警告,而不是缺少结果:

func mytablegenerate(ctx context.context, querycontext table.querycontext) ([]map[string]string, error) {
    if cnstlist, present := querycontext.constraints["field1"]; present {
        // if 'field1' is present in querycontext.contraints's keys
        // translate: if 'field1' is in the where clause

        for _, cnst := range cnstlist.constraints {
            if cnst.operator == table.operatorequals {
                out, err := someexternalfn(cnst.expression)
                return []map[string]string{
                    {
                        "field1": cnst.expression,
                        "field2": out,
                        "field3": err,
                    },
                }, nil
            }
        }
    }
    return nil, errors.new("query to table mytable must have a where clause on 'field1'")
}

在 osqueryi 中:

osquery> select * from mytable;
osquery> select * from mytable where field1="foo";
+--------+--------+--------+
| field1 | field2 | field3 |
+--------+--------+--------+
| foo    | foobar | foobaz |
+--------+--------+--------+

我寻求什么:

osquery> select * from file;
W0618 11:50:58.840874  7252 virtual_table.cpp:991] Table file was queried without a required column in the WHERE clause
W0618 11:50:58.841397  7252 virtual_table.cpp:1002] Please see the table documentation: https://osquery.io/schema/#file

解决方案


返回错误而不是 nil。

类似于(伪代码):

cnstList, present := queryContext.Constraints["field1"]
    if !present {
       return nil, errors.New("Missing required field1")
    }
    ...

作为一个真实的示例,请查看启动器中的一些自定义表格。例如https://github.com/kolide/launcher/blob/ca2b2a48fb7ee7a13892b3f9940d4e67ccd9d6de/pkg/osquery/table/slack_config.go#L88-L101

好了,本文到此结束,带大家了解了《提供表规范时如何在添加虚拟表上进行扩展》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>