forked from gane5hvarma/panther
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add jsoniter extension to force
omitempty
on all fields (#1204)
* Add jsoniter extension to force omitempty on all fields
- Loading branch information
Showing
4 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
internal/log_analysis/log_processor/pantherlog/omitempty/omitempty.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package omitempty | ||
|
||
/** | ||
* Panther is a Cloud-Native SIEM for the Modern Security Team. | ||
* Copyright (C) 2020 Panther Labs Inc | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/fatih/structtag" | ||
jsoniter "github.com/json-iterator/go" | ||
"github.com/modern-go/reflect2" | ||
) | ||
|
||
// New injects omitempty option to all fields | ||
func New(key string) jsoniter.Extension { | ||
if key == "" { | ||
key = "json" | ||
} | ||
return &omitemptyExt{ | ||
key: key, | ||
} | ||
} | ||
|
||
type omitemptyExt struct { | ||
jsoniter.DummyExtension | ||
key string | ||
} | ||
|
||
func (ext *omitemptyExt) UpdateStructDescriptor(desc *jsoniter.StructDescriptor) { | ||
for _, binding := range desc.Fields { | ||
field := binding.Field | ||
// Assert that the struct descriptor does not contain anonymous fields (jsoniter omits them) | ||
if field.Anonymous() { | ||
panic("Anonymous field in struct descriptor") | ||
} | ||
tag := injectOmitempty(field.Tag(), ext.key) | ||
binding.Field = InjectTag(field, tag) | ||
} | ||
} | ||
|
||
func InjectTag(field reflect2.StructField, tag reflect.StructTag) reflect2.StructField { | ||
return &fieldExt{ | ||
StructField: field, | ||
tag: tag, | ||
} | ||
} | ||
|
||
type fieldExt struct { | ||
reflect2.StructField | ||
tag reflect.StructTag | ||
} | ||
|
||
func (ext *fieldExt) Tag() reflect.StructTag { | ||
if ext.tag != "" { | ||
return ext.tag | ||
} | ||
return ext.StructField.Tag() | ||
} | ||
|
||
func injectOmitempty(original reflect.StructTag, key string) reflect.StructTag { | ||
tags, err := structtag.Parse(string(original)) | ||
if err != nil { | ||
return original | ||
} | ||
tag, err := tags.Get(key) | ||
if err != nil { | ||
tag := structtag.Tag{ | ||
Key: key, | ||
Options: []string{"omitempty"}, | ||
} | ||
_ = tags.Set(&tag) | ||
return reflect.StructTag(tags.String()) | ||
} | ||
// Assert jsoniter omits fields witg `-` name | ||
if tag.Name == "-" { | ||
panic("JSON-omittted field in struct descriptor") | ||
} | ||
tags.AddOptions(key, "omitempty") | ||
return reflect.StructTag(tags.String()) | ||
} |
70 changes: 70 additions & 0 deletions
70
internal/log_analysis/log_processor/pantherlog/omitempty/omitempty_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package omitempty | ||
|
||
/** | ||
* Panther is a Cloud-Native SIEM for the Modern Security Team. | ||
* Copyright (C) 2020 Panther Labs Inc | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import ( | ||
"testing" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
api := jsoniter.Config{}.Froze() | ||
api.RegisterExtension(New("json")) | ||
|
||
type A struct { | ||
Foo string `json:"foo"` | ||
Bar string `json:",omitempty"` | ||
Baz string | ||
Nested *A `json:"nested"` | ||
} | ||
type B struct { | ||
Qux string `json:"qux"` | ||
A | ||
Ignore string `json:"-"` | ||
} | ||
{ | ||
out, err := api.MarshalToString(&A{ | ||
Nested: &A{}, | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, `{"nested":{}}`, out) | ||
} | ||
{ | ||
out, err := api.MarshalToString(&A{}) | ||
require.NoError(t, err) | ||
require.Equal(t, `{}`, out) | ||
} | ||
{ | ||
out, err := api.MarshalToString(&B{}) | ||
require.NoError(t, err) | ||
require.Equal(t, `{}`, out) | ||
} | ||
{ | ||
out, err := api.MarshalToString(&B{ | ||
Qux: "qux", | ||
A: A{ | ||
Foo: "foo", | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, `{"qux":"qux","foo":"foo"}`, out) | ||
} | ||
} |