Skip to content

Commit fb05437

Browse files
committed
Update obfuscation
1 parent 2f2a912 commit fb05437

File tree

8 files changed

+78
-44
lines changed

8 files changed

+78
-44
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ You can also combine your CLI arguments and the wizard. All arguments you pass f
2828

2929
> **WARNING:** Some passwords or secrets are automatically removed, but this no guarantee, so be careful what you share!
3030
31-
The `--hide` flag can be used multiple times to hide sensitive data, it supports regular expressions.
31+
The `--hide` flag can be used multiple times to hide sensitive data.
32+
As these obfuscators are based on regex, you must add a regex pattern that meets your requirements.
3233

33-
`# support-collector --hide "Secret:.*" --hide "Password:.*"`
34+
`# support-collector --hide "Secret:\s*(.*)"`
35+
36+
This will replace:
37+
* Values like `Secret: DummyValue`
3438

3539
In addition, files and folders that follow a specific pattern are not collected. This affects all files that correspond to the following filters:
3640
`.*`, `*~`, `*.key`, `*.csr`, `*.crt`, and `*.pem`

internal/collection/collection.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ func (c *Collection) AddFileYAML(fileName string, data interface{}) {
142142
_ = c.AddFileToOutput(file)
143143
}
144144

145+
// AddFileJSON will add raw file data without obfuscation
145146
func (c *Collection) AddFileJSON(fileName string, data []byte) {
146147
var jsonData interface{}
147148

@@ -158,7 +159,7 @@ func (c *Collection) AddFileJSON(fileName string, data []byte) {
158159
file := NewFile(fileName)
159160
file.Data = prettyJSON
160161

161-
_ = c.AddFileToOutput(file)
162+
_ = c.AddFileToOutputRaw(file)
162163
}
163164

164165
func (c *Collection) AddFiles(prefix, source string) {
@@ -250,16 +251,19 @@ func (c *Collection) AddJournalLog(fileName, service string) {
250251
c.AddCommandOutput(fileName, "journalctl", "-u", service, "-S", c.JournalLoggingInterval)
251252
}
252253

254+
// RegisterObfuscator adds the given Obfuscator to the Obfuscators of Collection
253255
func (c *Collection) RegisterObfuscator(o *obfuscate.Obfuscator) {
254256
c.Obfuscators = append(c.Obfuscators, o)
255257
}
256258

259+
// RegisterObfuscators adds the given list of Obfuscator to the Obfuscators of Collection
257260
func (c *Collection) RegisterObfuscators(list ...*obfuscate.Obfuscator) {
258261
for _, o := range list {
259262
c.RegisterObfuscator(o)
260263
}
261264
}
262265

266+
// ClearObfuscators clears the list of Obfuscators in the Collection
263267
func (c *Collection) ClearObfuscators() {
264268
c.Obfuscators = c.Obfuscators[:0]
265269
}
@@ -272,14 +276,17 @@ func (c *Collection) callObfuscators(kind obfuscate.Kind, name string, data []by
272276

273277
for _, o := range c.Obfuscators {
274278
if o.IsAccepting(kind, name) {
275-
count, out, err = o.Process(data)
279+
count, out, err = o.Process(data, name)
276280
if err != nil {
277281
return
278282
}
279283

280-
if count > 0 {
281-
c.Log.Debugf("Obfuscation replaced %d token in %s", count, name)
282-
}
284+
data = out
285+
}
286+
287+
if count > 0 {
288+
c.Log.Debugf("ReplacePattern '%s' replaced %d token in %s", o.ReplacePattern, count, name)
289+
count = 0
283290
}
284291
}
285292

internal/obfuscate/obfuscate.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,24 @@ const (
2929

3030
// Obfuscator provides the basic functionality of an obfuscation engine.
3131
//
32-
// Kind filters the variant of resource we want to work on, while Affecting defines a list of regexp.Regexp, to match
32+
// Kind filters the variant of resource we want to work on, while ShouldAffect defines a list of regexp.Regexp, to match
3333
// against for the file names, or command.
3434
//
35-
// Replacements will be iterated, so all matches or matched groups will be replaced.
35+
// Replacement will be iterated, so all matches or matched groups will be replaced.
3636
type Obfuscator struct {
3737
Kind
38-
Affecting []*regexp.Regexp
39-
Replacements []*regexp.Regexp
40-
Files uint
41-
Replaced uint
38+
ShouldAffect []*regexp.Regexp
39+
ReplacePattern *regexp.Regexp
40+
ObfuscatedFiles []string
41+
Replaced uint
4242
}
4343

4444
// New returns a basic Obfuscator with provided regexp.Regexp.
4545
func New(kind Kind, affects, replace *regexp.Regexp) *Obfuscator {
4646
return &Obfuscator{
47-
Kind: kind,
48-
Affecting: []*regexp.Regexp{affects},
49-
Replacements: []*regexp.Regexp{replace},
47+
Kind: kind,
48+
ShouldAffect: []*regexp.Regexp{affects},
49+
ReplacePattern: replace,
5050
}
5151
}
5252

@@ -77,27 +77,30 @@ func NewAny(replace string) *Obfuscator {
7777
return o
7878
}
7979

80-
// WithAffecting adds a new element to the list.
80+
// WithAffecting adds a new pattern to the list where the Obfuscator will be applied
8181
func (o *Obfuscator) WithAffecting(a *regexp.Regexp) *Obfuscator {
82-
o.Affecting = append(o.Affecting, a)
82+
o.ShouldAffect = append(o.ShouldAffect, a)
8383

8484
return o
8585
}
8686

87-
// WithReplacement adds a new element to the list.
87+
// WithReplacement adds a new pattern to the list.
8888
func (o *Obfuscator) WithReplacement(r *regexp.Regexp) *Obfuscator {
89-
o.Replacements = append(o.Replacements, r)
89+
o.ReplacePattern = r
9090

9191
return o
9292
}
9393

9494
// IsAccepting checks if we want to work on the resource.
95+
//
96+
// Checks if the Obfuscator.Kind is matching the given kind. If not returns false.
97+
// Checks if the Obfuscator.ShouldAffect is matching the given name.
9598
func (o Obfuscator) IsAccepting(kind Kind, name string) bool {
9699
if o.Kind != KindAny && o.Kind != kind {
97100
return false
98101
}
99102

100-
for _, re := range o.Affecting {
103+
for _, re := range o.ShouldAffect {
101104
if re.MatchString(name) {
102105
return true
103106
}
@@ -106,16 +109,18 @@ func (o Obfuscator) IsAccepting(kind Kind, name string) bool {
106109
return false
107110
}
108111

109-
// Process takes data and returns it obfuscated.
110-
func (o *Obfuscator) Process(data []byte) (uint, []byte, error) {
111-
count, out, err := o.ProcessReader(bytes.NewReader(data))
112+
// Process takes data and returns it obfuscated. Also takes name of file that is obfuscated
113+
//
114+
// Returns count of replaced values as uint, obfuscated data as []byte, and error
115+
func (o *Obfuscator) Process(data []byte, name string) (uint, []byte, error) {
116+
count, out, err := o.ProcessReader(bytes.NewReader(data), name)
112117

113118
//goland:noinspection GoNilness
114119
return count, out.Bytes(), err
115120
}
116121

117122
// ProcessReader takes an io.Reader and returns a new one obfuscated.
118-
func (o *Obfuscator) ProcessReader(r io.Reader) (count uint, out bytes.Buffer, err error) {
123+
func (o *Obfuscator) ProcessReader(r io.Reader, name string) (count uint, out bytes.Buffer, err error) {
119124
rd := bufio.NewReader(r)
120125

121126
var (
@@ -153,7 +158,7 @@ func (o *Obfuscator) ProcessReader(r io.Reader) (count uint, out bytes.Buffer, e
153158

154159
// Replace any matches, but skip empty lines
155160
if strings.TrimSpace(line) != "" {
156-
line, c = ReplacePatterns(line, o.Replacements)
161+
line, c = ReplacePattern(line, o.ReplacePattern)
157162
}
158163

159164
// Add line ending back after replacement
@@ -168,12 +173,13 @@ func (o *Obfuscator) ProcessReader(r io.Reader) (count uint, out bytes.Buffer, e
168173
}
169174

170175
if count > 0 {
171-
o.Files++
176+
o.ObfuscatedFiles = append(o.ObfuscatedFiles, name)
172177
}
173178

174179
return count, out, err
175180
}
176181

182+
/*
177183
// ReplacePatterns replaces all the patterns matches in a line.
178184
func ReplacePatterns(line string, patterns []*regexp.Regexp) (s string, count uint) {
179185
for _, pattern := range patterns {
@@ -185,6 +191,7 @@ func ReplacePatterns(line string, patterns []*regexp.Regexp) (s string, count ui
185191
186192
return line, count
187193
}
194+
*/
188195

189196
// ReplacePattern replaces all matches in a line.
190197
func ReplacePattern(line string, pattern *regexp.Regexp) (s string, count uint) {

internal/obfuscate/obfuscate_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func ExampleObfuscator() {
2424
content := []byte(`password = "secret"`)
2525

2626
if o.IsAccepting(KindFile, "test.ini") {
27-
count, data, err := o.Process(content)
27+
count, data, err := o.Process(content, "")
2828
fmt.Println(err)
2929
fmt.Println(count)
3030
fmt.Println(string(data))
@@ -64,17 +64,15 @@ func TestObfuscator_IsAccepting(t *testing.T) {
6464

6565
func TestObfuscator_Process(t *testing.T) {
6666
o := &Obfuscator{
67-
Replacements: []*regexp.Regexp{
68-
regexp.MustCompile(`^password\s*=\s*(.*)`),
69-
},
67+
ReplacePattern: regexp.MustCompile(`^password\s*=\s*(.*)`),
7068
}
7169

72-
count, out, err := o.Process([]byte("default content\r\n"))
70+
count, out, err := o.Process([]byte("default content\r\n"), "")
7371
assert.NoError(t, err)
7472
assert.Equal(t, uint(0), count)
7573
assert.Equal(t, "default content\r\n", string(out))
7674

77-
count, out, err = o.Process([]byte(iniExample))
75+
count, out, err = o.Process([]byte(iniExample), "")
7876
assert.NoError(t, err)
7977
assert.Equal(t, uint(1), count)
8078
assert.Equal(t, iniResult, string(out))
@@ -83,15 +81,15 @@ func TestObfuscator_Process(t *testing.T) {
8381
func TestNewFile(t *testing.T) {
8482
o := NewFile(`^password\s*=\s*(.*)`, "conf")
8583
assert.Equal(t, KindFile, o.Kind)
86-
assert.Len(t, o.Affecting, 1)
87-
assert.Len(t, o.Replacements, 1)
84+
assert.Len(t, o.ShouldAffect, 1)
85+
assert.NotEmpty(t, o.ShouldAffect)
8886
}
8987

9088
func TestNewOutput(t *testing.T) {
9189
o := NewOutput(`^password\s*=\s*(.*)`, "icinga2", "daemon", "-C")
9290
assert.Equal(t, KindOutput, o.Kind)
93-
assert.Len(t, o.Affecting, 1)
94-
assert.Len(t, o.Replacements, 1)
91+
assert.Len(t, o.ShouldAffect, 1)
92+
assert.NotEmpty(t, o.ReplacePattern)
9593

9694
assert.True(t, o.IsAccepting(KindOutput, "icinga2 daemon -C"))
9795
assert.False(t, o.IsAccepting(KindOutput, "icinga2 daemon"))

internal/util/testing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func AssertObfuscation(t *testing.T, obfuscators []*obfuscate.Obfuscator,
1919
continue
2020
}
2121

22-
_, out, err := o.Process([]byte(input))
22+
_, out, err := o.Process([]byte(input), name)
2323
if err != nil {
2424
t.Errorf("error during obfuscation: %s", err)
2525
return

internal/util/util.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ func StringInSlice(a string, list []string) bool {
2020
return false
2121
}
2222

23+
// DistinctStringSlice retuns the given slice with unique values
24+
func DistinctStringSlice(arr []string) []string {
25+
seen := make(map[string]bool)
26+
var result []string
27+
28+
for _, val := range arr {
29+
if !seen[val] {
30+
result = append(result, val)
31+
seen[val] = true
32+
}
33+
}
34+
35+
return result
36+
}
37+
2338
// IsPrivilegedUser returns true when the current user is root.
2439
func IsPrivilegedUser() bool {
2540
u, err := user.Current()

main.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,19 @@ func main() {
246246
c.Log.Infof("Collection complete, took us %.3f seconds", metric.Timings["total"].Seconds())
247247

248248
// Collect obfuscation info
249-
var files, count uint
249+
var (
250+
count uint
251+
affectedFiles []string
252+
)
250253

251254
for _, o := range c.Obfuscators {
252-
files += o.Files
253-
254255
count += o.Replaced
256+
257+
affectedFiles = append(affectedFiles, o.ObfuscatedFiles...)
255258
}
256259

257-
if files > 0 {
258-
c.Log.Infof("Obfuscation replaced %d token in %d files (%d definitions)", count, files, len(c.Obfuscators))
260+
if len(affectedFiles) > 0 {
261+
c.Log.Infof("Obfuscation replaced %d token in %d files (%d definitions)", count, len(util.DistinctStringSlice(affectedFiles)), len(c.Obfuscators))
259262
}
260263

261264
// get absolute path of outputFile

modules/icinga2/collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package icinga2
22

33
import (
4+
"github.com/NETWAYS/support-collector/internal/obfuscate"
45
"github.com/NETWAYS/support-collector/internal/util"
56
"os"
67
"path/filepath"
78
"regexp"
89

910
"github.com/NETWAYS/support-collector/internal/collection"
10-
"github.com/NETWAYS/support-collector/internal/obfuscate"
1111
)
1212

1313
const ModuleName = "icinga2"

0 commit comments

Comments
 (0)