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.
Quick fix for deployment updates (#1975)
* Quick fix for deploymeny updates * Fix test * Move stringset function to pkg/stringset Co-authored-by: Nick Angelou <[email protected]>
- Loading branch information
Showing
4 changed files
with
236 additions
and
3 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
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,65 @@ | ||
package stringset | ||
|
||
/** | ||
* 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/>. | ||
*/ | ||
|
||
// New creates a new set of unique string values | ||
func New(values ...string) []string { | ||
if values == nil { | ||
return nil | ||
} | ||
return Append(make([]string, 0, len(values)), values...) | ||
} | ||
|
||
// Dedup de-duplicates a set of values in-place | ||
func Dedup(values []string) []string { | ||
if values == nil { | ||
return nil | ||
} | ||
return Append(values[:0], values...) | ||
} | ||
|
||
// Concat concatenates all parts into a single set of distinct values | ||
func Concat(parts ...[]string) []string { | ||
// Collect the max required size | ||
size := 0 | ||
for _, part := range parts { | ||
size += len(part) | ||
} | ||
// Allocate a big enough slice for all values | ||
union := make([]string, 0, size) | ||
// Add all parts omitting duplicate values | ||
for _, part := range parts { | ||
union = Append(union, part...) | ||
} | ||
return union | ||
} | ||
|
||
// Append appends src to dst skipping duplicate values | ||
func Append(dst []string, values ...string) []string { | ||
loopValues: | ||
for _, value := range values { | ||
for _, duplicate := range dst { | ||
if duplicate == value { | ||
continue loopValues | ||
} | ||
} | ||
dst = append(dst, value) | ||
} | ||
return dst | ||
} |
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,152 @@ | ||
package stringset | ||
|
||
/** | ||
* 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 ( | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConcat(t *testing.T) { | ||
type testCase struct { | ||
Args [][]string | ||
Expect []string | ||
} | ||
for i, tc := range []testCase{ | ||
{ | ||
Args: [][]string{ | ||
{"foo", "bar"}, | ||
{"foo", "baz"}, | ||
}, | ||
Expect: []string{"foo", "bar", "baz"}, | ||
}, | ||
{ | ||
Args: [][]string{ | ||
{"foo", "bar"}, | ||
}, | ||
Expect: []string{"foo", "bar"}, | ||
}, | ||
{ | ||
Args: [][]string{ | ||
{"foo", "foo"}, | ||
}, | ||
Expect: []string{"foo"}, | ||
}, | ||
{ | ||
Args: [][]string{ | ||
{}, | ||
nil, | ||
}, | ||
Expect: []string{}, | ||
}, | ||
{ | ||
Args: [][]string{ | ||
{"foo", "bar"}, | ||
{"foo", "baz"}, | ||
{"bar", "baz", "qux"}, | ||
}, | ||
Expect: []string{"foo", "bar", "baz", "qux"}, | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(strconv.Itoa(i), func(t *testing.T) { | ||
actual := Concat(tc.Args...) | ||
assert.Equal(t, tc.Expect, actual) | ||
}) | ||
} | ||
} | ||
|
||
func TestNew(t *testing.T) { | ||
type testCase struct { | ||
Args []string | ||
Expect []string | ||
} | ||
for i, tc := range []testCase{ | ||
{ | ||
Args: []string{"foo", "bar", "baz"}, | ||
Expect: []string{"foo", "bar", "baz"}, | ||
}, | ||
{ | ||
Args: []string{"foo", "bar", "baz", "foo"}, | ||
Expect: []string{"foo", "bar", "baz"}, | ||
}, | ||
{ | ||
Args: nil, | ||
Expect: nil, | ||
}, | ||
{ | ||
Args: []string{}, | ||
Expect: []string{}, | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(strconv.Itoa(i), func(t *testing.T) { | ||
actual := New(tc.Args...) | ||
assert.Equal(t, tc.Expect, actual) | ||
|
||
if len(tc.Args) > 0 { | ||
// Ensure the result is a new slice | ||
for i := range tc.Args { | ||
tc.Args[i] += "foo" | ||
} | ||
assert.NotEqual(t, actual, tc.Args) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDedup(t *testing.T) { | ||
type testCase struct { | ||
Args []string | ||
Expect []string | ||
} | ||
for i, tc := range []testCase{ | ||
{ | ||
Args: []string{"foo", "bar", "baz"}, | ||
Expect: []string{"foo", "bar", "baz"}, | ||
}, | ||
{ | ||
Args: []string{"foo", "bar", "baz", "foo"}, | ||
Expect: []string{"foo", "bar", "baz"}, | ||
}, | ||
{ | ||
Args: nil, | ||
Expect: nil, | ||
}, | ||
{ | ||
Args: []string{}, | ||
Expect: []string{}, | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(strconv.Itoa(i), func(t *testing.T) { | ||
actual := Dedup(tc.Args) | ||
assert.Equal(t, tc.Expect, actual) | ||
if len(tc.Args) > 0 { | ||
// Ensure the result is the same slice | ||
for i := range tc.Args { | ||
tc.Args[i] += "foo" | ||
} | ||
assert.Equal(t, actual, tc.Args[:len(actual)]) | ||
} | ||
}) | ||
} | ||
} |