Skip to content

Commit 8cb7b78

Browse files
Clarify talismanrc method notes and names
Authored-by: Owen Nelson <[email protected]>
1 parent 06b32c0 commit 8cb7b78

File tree

8 files changed

+37
-36
lines changed

8 files changed

+37
-36
lines changed

cmd/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (r *runner) Run(tRC *talismanrc.TalismanRC, promptContext prompt.PromptCont
3434
ie := helpers.BuildIgnoreEvaluator(r.mode, tRC, repo)
3535

3636
setCustomSeverities(tRC)
37-
additionsToScan := tRC.FilterAdditions(r.additions)
37+
additionsToScan := tRC.RemoveScopedFiles(r.additions)
3838

3939
detector.DefaultChain(tRC, ie).Test(additionsToScan, tRC, r.results)
4040
r.printReport(promptContext)

cmd/scanner_cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (s *ScannerCmd) Run() int {
3131
fmt.Printf("\n\n")
3232
utility.CreateArt("Running Scan..")
3333

34-
additionsToScan := s.tRC.FilterAdditions(s.additions)
34+
additionsToScan := s.tRC.RemoveScopedFiles(s.additions)
3535

3636
detector.DefaultChain(s.tRC, s.ignoreEvaluator).Test(additionsToScan, s.tRC, s.results)
3737
reportsPath, err := report.GenerateReport(s.results, s.reportDirectory)

detector/filecontent/filecontent_detector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (fc *FileContentDetector) Test(comparator helpers.IgnoreEvaluator, currentA
119119
data := []byte(content)
120120
addition.Data = data
121121
}
122-
addition.Data = []byte(talismanRC.FilterAllowedPatternsFromAddition(addition))
122+
addition.Data = []byte(talismanRC.RemoveAllowedPatterns(addition))
123123
for _, ct := range contentTypes {
124124
contents <- content{
125125
name: addition.Name,

detector/pattern/pattern_detector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (detector PatternDetector) Test(comparator helpers.IgnoreEvaluator, current
5050
ignoredFilePaths <- addition.Path
5151
return
5252
}
53-
detections := detector.secretsPattern.check(ignoreConfig.FilterAllowedPatternsFromAddition(addition), ignoreConfig.Threshold)
53+
detections := detector.secretsPattern.check(ignoreConfig.RemoveAllowedPatterns(addition), ignoreConfig.Threshold)
5454
matches <- match{name: addition.Name, path: addition.Path, detections: detections, commits: addition.Commits}
5555
}(addition)
5656
}

talismanrc/rc_file.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@ var (
2020
currentRCFileName = DefaultRCFileName
2121
)
2222

23+
// Load creates a TalismanRC struct based on a .talismanrc file, if present
2324
func Load() (*TalismanRC, error) {
2425
fileContents, err := afero.ReadFile(fs, currentRCFileName)
2526
if err != nil {
2627
// File does not exist or is not readable, proceed as if there is no .talismanrc
2728
fileContents = []byte{}
2829
}
29-
return newPersistedRC(fileContents)
30+
return talismanRCFromYaml(fileContents)
3031
}
3132

32-
func newPersistedRC(fileContents []byte) (*TalismanRC, error) {
33+
func talismanRCFromYaml(fileContents []byte) (*TalismanRC, error) {
3334
talismanRCFromFile := TalismanRC{}
3435
err := yaml.Unmarshal(fileContents, &talismanRCFromFile)
3536
if err != nil {

talismanrc/rc_file_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fileignoreconfig:
5959
custom_patterns:
6060
- 'pwd_[a-z]{8,20}'`)
6161

62-
rc, err := newPersistedRC(fileContents)
62+
rc, err := talismanRCFromYaml(fileContents)
6363
assert.Nil(t, err, "Should successfully unmarshal valid yaml")
6464
assert.Equal(t, 1, len(rc.FileIgnoreConfig))
6565
assert.Equal(t, 1, len(rc.CustomPatterns))
@@ -75,7 +75,7 @@ fileignoreconfig:
7575
- filename: testfile_3.yml
7676
checksum: file3_checksum`)
7777

78-
rc, _ := newPersistedRC(fileContent)
78+
rc, _ := talismanRCFromYaml(fileContent)
7979
assert.Equal(t, 3, len(rc.FileIgnoreConfig))
8080

8181
assert.Equal(t, rc.FileIgnoreConfig[0].GetFileName(), "testfile_1.yml")
@@ -88,7 +88,7 @@ fileignoreconfig:
8888

8989
t.Run("Should read severity level", func(t *testing.T) {
9090
talismanRCContents := []byte("threshold: high")
91-
persistedTalismanrc, _ := newPersistedRC(talismanRCContents)
91+
persistedTalismanrc, _ := talismanRCFromYaml(talismanRCContents)
9292
assert.Equal(t, persistedTalismanrc.Threshold, severity.High)
9393
})
9494

@@ -98,15 +98,15 @@ custom_severities:
9898
- detector: Base64Content
9999
severity: low
100100
`)
101-
talismanRC, _ := newPersistedRC(talismanRCContents)
101+
talismanRC, _ := talismanRCFromYaml(talismanRCContents)
102102
assert.Equal(t, talismanRC.CustomSeverities, []CustomSeverityConfig{{Detector: "Base64Content", Severity: severity.Low}})
103103
})
104104
}
105105

106106
func TestShouldIgnoreUnformattedFiles(t *testing.T) {
107107
for _, s := range []string{"#", "#monkey", "# this monkey likes bananas "} {
108108
fileContents := []byte(s)
109-
talismanRC, err := newPersistedRC(fileContents)
109+
talismanRC, err := talismanRCFromYaml(fileContents)
110110
assert.Nil(t, err, "Should successfully unmarshal commented yaml")
111111
assert.Equal(t, &TalismanRC{Version: "1.0"}, talismanRC, "Expected commented line '%s' to result in an empty TalismanRC")
112112
}

talismanrc/talismanrc.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,16 @@ type TalismanRC struct {
2323
Version string `yaml:"version"`
2424
}
2525

26-
// SuggestRCFor returns the talismanRC file content corresponding to input ignore configs
26+
// SuggestRCFor returns a string representation of a .talismanrc for the specified FileIgnoreConfigs
2727
func SuggestRCFor(configs []FileIgnoreConfig) string {
2828
tRC := TalismanRC{FileIgnoreConfig: configs, Version: DefaultRCVersion}
2929
result, _ := yaml.Marshal(tRC)
3030

3131
return string(result)
3232
}
3333

34-
// Accept answers true if the Addition.Path is configured to be checked by the detectors
35-
func (tRC *TalismanRC) Accept(addition gitrepo.Addition, detectorName string) bool {
36-
return !tRC.Deny(addition, detectorName)
37-
}
38-
39-
// FilterAdditions removes scope files from additions
40-
func (tRC *TalismanRC) FilterAdditions(additions []gitrepo.Addition) []gitrepo.Addition {
34+
// RemoveScopedFiles removes scope files from additions
35+
func (tRC *TalismanRC) RemoveScopedFiles(additions []gitrepo.Addition) []gitrepo.Addition {
4136
var applicableScopeFileNames []string
4237
if tRC.ScopeConfig != nil {
4338
for _, scope := range tRC.ScopeConfig {
@@ -62,6 +57,7 @@ func (tRC *TalismanRC) FilterAdditions(additions []gitrepo.Addition) []gitrepo.A
6257
return result
6358
}
6459

60+
// AddIgnores inserts the specified FileIgnoreConfigs to an existing .talismanrc file, or creates one if it doesn't exist.
6561
func (tRC *TalismanRC) AddIgnores(entriesToAdd []FileIgnoreConfig) {
6662
if len(entriesToAdd) > 0 {
6763
logr.Debugf("Adding entries: %v", entriesToAdd)
@@ -112,18 +108,8 @@ func combineFileIgnores(exsiting, incoming []FileIgnoreConfig) []FileIgnoreConfi
112108
return result
113109
}
114110

115-
// Deny answers true if the Addition.Path is configured to be ignored and not checked by the detectors
116-
func (tRC *TalismanRC) Deny(addition gitrepo.Addition, detectorName string) bool {
117-
for _, pattern := range tRC.effectiveRules(detectorName) {
118-
if addition.Matches(pattern) {
119-
return true
120-
}
121-
}
122-
return false
123-
}
124-
125-
// Strip git addition
126-
func (tRC *TalismanRC) FilterAllowedPatternsFromAddition(addition gitrepo.Addition) string {
111+
// RemoveAllowedPatterns removes globally- and per-file allowed patterns from an Addition
112+
func (tRC *TalismanRC) RemoveAllowedPatterns(addition gitrepo.Addition) string {
127113
additionPathAsString := string(addition.Path)
128114
// Processing global allowed patterns
129115
for _, pattern := range tRC.AllowedPatterns {
@@ -141,13 +127,27 @@ func (tRC *TalismanRC) FilterAllowedPatternsFromAddition(addition gitrepo.Additi
141127
return string(addition.Data)
142128
}
143129

130+
// Deny answers true if the Addition should NOT be checked by the specified detector
131+
func (tRC *TalismanRC) Deny(addition gitrepo.Addition, detectorName string) bool {
132+
for _, pattern := range tRC.effectiveRules(detectorName) {
133+
if addition.Matches(pattern) {
134+
return true
135+
}
136+
}
137+
return false
138+
}
139+
140+
// Accept answers true if the Addition should be checked by the specified detector
141+
func (tRC *TalismanRC) Accept(addition gitrepo.Addition, detectorName string) bool {
142+
return !tRC.Deny(addition, detectorName)
143+
}
144+
144145
func (tRC *TalismanRC) effectiveRules(detectorName string) []string {
145146
var result []string
146147
for _, ignore := range tRC.FileIgnoreConfig {
147148
if ignore.isEffective(detectorName) {
148149
result = append(result, ignore.GetFileName())
149150
}
150151
}
151-
152152
return result
153153
}

talismanrc/talismanrc_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestShouldFilterAllowedPatternsFromAddition(t *testing.T) {
4444
gitRepoAddition1 := testAdditionWithData("file1", []byte(fileContent))
4545
talismanrc := &TalismanRC{AllowedPatterns: []*Pattern{{regexp.MustCompile(hex)}}}
4646

47-
fileContentFiltered := talismanrc.FilterAllowedPatternsFromAddition(gitRepoAddition1)
47+
fileContentFiltered := talismanrc.RemoveAllowedPatterns(gitRepoAddition1)
4848

4949
assert.Equal(t, fileContentFiltered, "Prefix content")
5050
}
@@ -56,8 +56,8 @@ func TestShouldFilterAllowedPatternsFromAdditionBasedOnFileConfig(t *testing.T)
5656
gitRepoAddition2 := testAdditionWithData("file2", []byte(fileContent))
5757
talismanrc := createTalismanRCWithFileIgnores("file1", "somedetector", []string{hexContent})
5858

59-
fileContentFiltered1 := talismanrc.FilterAllowedPatternsFromAddition(gitRepoAddition1)
60-
fileContentFiltered2 := talismanrc.FilterAllowedPatternsFromAddition(gitRepoAddition2)
59+
fileContentFiltered1 := talismanrc.RemoveAllowedPatterns(gitRepoAddition1)
60+
fileContentFiltered2 := talismanrc.RemoveAllowedPatterns(gitRepoAddition2)
6161

6262
assert.Equal(t, fileContentFiltered1, "Prefix content")
6363
assert.Equal(t, fileContentFiltered2, fileContent)
@@ -111,7 +111,7 @@ func TestIgnoreAdditionsByScope(t *testing.T) {
111111
for scopeName, additions := range testTable {
112112
t.Run(fmt.Sprintf("should ignore files for %s scope", scopeName), func(t *testing.T) {
113113
talismanRCConfig := createTalismanRCWithScopeIgnores([]string{scopeName})
114-
filteredAdditions := talismanRCConfig.FilterAdditions(additions)
114+
filteredAdditions := talismanRCConfig.RemoveScopedFiles(additions)
115115
for _, addition := range additions {
116116
assert.NotContains(t, filteredAdditions, addition, "Expected %s to be ignored", addition.Name)
117117
}

0 commit comments

Comments
 (0)