-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
289 additions
and
17 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
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,30 @@ | ||
package pkg | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
|
||
"github.com/spectralops/teller/pkg/core" | ||
) | ||
|
||
type Redactor struct { | ||
Entries []core.EnvEntry | ||
} | ||
|
||
func NewRedactor(entries []core.EnvEntry) *Redactor { | ||
return &Redactor{ | ||
Entries: entries, | ||
} | ||
} | ||
|
||
func (r *Redactor) Redact(s string) string { | ||
redacted := s | ||
entries := append([]core.EnvEntry(nil), r.Entries...) | ||
|
||
sort.Sort(core.EntriesByValueSize(entries)) | ||
for _, ent := range entries { | ||
redacted = strings.ReplaceAll(redacted, ent.Value, ent.RedactWith) | ||
} | ||
|
||
return redacted | ||
} |
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,112 @@ | ||
package pkg | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/alecthomas/assert" | ||
"github.com/spectralops/teller/pkg/core" | ||
) | ||
|
||
func TestRedactorOverlap(t *testing.T) { | ||
|
||
// in this case we dont want '123' to appear in the clear after all redactions are made. | ||
// it can happen if the smaller secret get replaced first because both | ||
// secrets overlap. we need to ensure the wider secrets always get | ||
// replaced first. | ||
|
||
entries := []core.EnvEntry{ | ||
{ | ||
Provider: "test", | ||
ResolvedPath: "/some/path", | ||
Key: "OTHER_KEY", | ||
Value: "hello", | ||
RedactWith: "**OTHER_KEY**", | ||
}, | ||
{ | ||
Provider: "test", | ||
ResolvedPath: "/some/path", | ||
Key: "SOME_KEY", | ||
Value: "hello123", | ||
RedactWith: "**SOME_KEY**", | ||
}, | ||
} | ||
redactor := Redactor{Entries: entries} | ||
s := ` | ||
func Foobar(){ | ||
secret := "hello" | ||
callService(secret, "hello123") | ||
// hello, hello123 | ||
} | ||
` | ||
sr := ` | ||
func Foobar(){ | ||
secret := "**OTHER_KEY**" | ||
callService(secret, "**SOME_KEY**") | ||
// **OTHER_KEY**, **SOME_KEY** | ||
} | ||
` | ||
|
||
assert.Equal(t, redactor.Redact(s), sr) | ||
} | ||
func TestRedactorMultiple(t *testing.T) { | ||
|
||
entries := []core.EnvEntry{ | ||
{ | ||
Provider: "test", | ||
ResolvedPath: "/some/path", | ||
Key: "SOME_KEY", | ||
Value: "shazam", | ||
RedactWith: "**SOME_KEY**", | ||
}, | ||
{ | ||
Provider: "test", | ||
ResolvedPath: "/some/path", | ||
Key: "OTHER_KEY", | ||
Value: "loot", | ||
RedactWith: "**OTHER_KEY**", | ||
}, | ||
} | ||
redactor := Redactor{Entries: entries} | ||
s := ` | ||
func Foobar(){ | ||
secret := "loot" | ||
callService(secret, "shazam") | ||
} | ||
` | ||
sr := ` | ||
func Foobar(){ | ||
secret := "**OTHER_KEY**" | ||
callService(secret, "**SOME_KEY**") | ||
} | ||
` | ||
|
||
assert.Equal(t, redactor.Redact(s), sr) | ||
} | ||
|
||
func TestRedactor(t *testing.T) { | ||
|
||
entries := []core.EnvEntry{ | ||
{ | ||
Provider: "test", | ||
ResolvedPath: "/some/path", | ||
Key: "SOME_KEY", | ||
Value: "shazam", | ||
RedactWith: "**NOPE**", | ||
}, | ||
} | ||
redactor := Redactor{Entries: entries} | ||
s := ` | ||
func Foobar(){ | ||
secret := "shazam" | ||
callService(secret, "shazam") | ||
} | ||
` | ||
sr := ` | ||
func Foobar(){ | ||
secret := "**NOPE**" | ||
callService(secret, "**NOPE**") | ||
} | ||
` | ||
|
||
assert.Equal(t, redactor.Redact(s), sr) | ||
} |
Oops, something went wrong.