Skip to content

Commit cdc8d06

Browse files
svishwanath-twTalisman Test User
authored andcommitted
#314 Fix issue with talismanRC
1 parent 94c7e3a commit cdc8d06

File tree

5 files changed

+75
-23
lines changed

5 files changed

+75
-23
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ dist/
2020
*.mprof
2121
.envrc
2222
talisman_html_report
23+
.vscode
24+
.idea

cmd/talisman.go

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,42 @@ var options struct {
5151

5252
func init() {
5353
log.SetOutput(os.Stderr)
54-
flag.BoolVarP(&options.debug, "debug", "d", false, "enable debug mode (warning: very verbose)")
55-
flag.StringVarP(&options.loglevel, "loglevel", "l", "error", "enable debug mode (warning: very verbose)")
56-
flag.BoolVarP(&showVersion, "version", "v", false, "show current version of talisman")
57-
flag.StringVarP(&options.pattern, "pattern", "p", "", "pattern (glob-like) of files to scan (ignores githooks)")
58-
flag.StringVarP(&options.githook, "githook", "g", PrePush, "either pre-push or pre-commit")
59-
flag.BoolVarP(&options.scan, "scan", "s", false, "scanner scans the git commit history for potential secrets")
60-
flag.BoolVar(&options.ignoreHistory, "ignoreHistory", false, "scanner scans all files on current head, will not scan through git commit history")
61-
flag.StringVarP(&options.checksum, "checksum", "c", "", "checksum calculator calculates checksum and suggests .talismanrc format")
62-
flag.StringVarP(&options.reportDirectory, "reportDirectory", "r", "talisman_report", "directory where the scan reports will be stored")
63-
flag.BoolVarP(&options.scanWithHtml, "scanWithHtml", "w", false, "generate html report (**Make sure you have installed talisman_html_report to use this, as mentioned in Readme**)")
64-
flag.BoolVarP(&interactive, "interactive", "i", false, "interactively update talismanrc (only makes sense with -g/--githook)")
65-
flag.BoolVarP(&options.shouldProfile, "profile", "", false, "interactively update talismanrc (only makes sense with -g/--githook)")
54+
flag.BoolVarP(&options.debug,
55+
"debug", "d", false,
56+
"enable debug mode (warning: very verbose)")
57+
flag.StringVarP(&options.loglevel,
58+
"loglevel", "l", "error",
59+
"enable debug mode (warning: very verbose)")
60+
flag.BoolVarP(&showVersion,
61+
"version", "v", false,
62+
"show current version of talisman")
63+
flag.StringVarP(&options.pattern,
64+
"pattern", "p", "",
65+
"pattern (glob-like) of files to scan (ignores githooks)")
66+
flag.StringVarP(&options.githook,
67+
"githook", "g", PrePush,
68+
"either pre-push or pre-commit")
69+
flag.BoolVarP(&options.scan,
70+
"scan", "s", false,
71+
"scanner scans the git commit history for potential secrets")
72+
flag.BoolVar(&options.ignoreHistory,
73+
"ignoreHistory", false,
74+
"scanner scans all files on current head, will not scan through git commit history")
75+
flag.StringVarP(&options.checksum,
76+
"checksum", "c", "",
77+
"checksum calculator calculates checksum and suggests .talismanrc format")
78+
flag.StringVarP(&options.reportDirectory,
79+
"reportDirectory", "r", "talisman_report",
80+
"directory where the scan reports will be stored")
81+
flag.BoolVarP(&options.scanWithHtml,
82+
"scanWithHtml", "w", false,
83+
"generate html report (**Make sure you have installed talisman_html_report to use this, as mentioned in Readme**)")
84+
flag.BoolVarP(&interactive,
85+
"interactive", "i", false,
86+
"interactively update talismanrc (only makes sense with -g/--githook)")
87+
flag.BoolVarP(&options.shouldProfile,
88+
"profile", "f", false,
89+
"profile cpu usage of talisman")
6690

6791
}
6892

@@ -151,7 +175,7 @@ func run(promptContext prompt.PromptContext) (returnCode int) {
151175
}
152176
bytes, _ := json.Marshal(options)
153177
fields := make(map[string]interface{})
154-
_ = json.Unmarshal(bytes, fields)
178+
_ = json.Unmarshal(bytes, &fields)
155179
log.WithFields(fields).Debug("Execution environment")
156180

157181
if options.checksum != "" {

git_testing/git_testing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"path/filepath"
99
"strings"
1010

11-
"github.com/sirupsen/logrus"
1211
lorem "github.com/drhodes/golorem"
12+
"github.com/sirupsen/logrus"
1313
)
1414

1515
var Logger *logrus.Entry

talismanrc/talismanrc.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package talismanrc
22

33
import (
4-
"log"
54
"os"
65
"regexp"
76
"sort"
@@ -52,6 +51,7 @@ type persistedRC struct {
5251
Version string `default:"1.0" yaml:"version"`
5352
}
5453

54+
//SuggestRCFor returns the talismanRC file content corresponding to input ignore configs
5555
func SuggestRCFor(configs []IgnoreConfig) string {
5656
fileIgnoreConfigs := []FileIgnoreConfig{}
5757
for _, config := range configs {
@@ -126,18 +126,18 @@ func (tRC *persistedRC) AddIgnores(mode Mode, entriesToAdd []IgnoreConfig) {
126126
ignoreEntries, _ := yaml.Marshal(&talismanRCConfig)
127127
file, err := fs.OpenFile(currentRCFileName, os.O_CREATE|os.O_WRONLY, 0644)
128128
if err != nil {
129-
log.Printf("error opening %s: %s", currentRCFileName, err)
129+
logr.Errorf("error opening %s: %s", currentRCFileName, err)
130130
}
131131
defer func() {
132132
err := file.Close()
133133
if err != nil {
134-
log.Printf("error closing %s: %s", currentRCFileName, err)
134+
logr.Errorf("error closing %s: %s", currentRCFileName, err)
135135
}
136136
}()
137137
logr.Debugf("Writing talismanrc: %v", string(ignoreEntries))
138138
_, err = file.WriteString(string(ignoreEntries))
139139
if err != nil {
140-
log.Printf("error writing to %s: %s", currentRCFileName, err)
140+
logr.Errorf("error writing to %s: %s", currentRCFileName, err)
141141
}
142142
}
143143
}
@@ -247,8 +247,8 @@ func fromPersistedRC(configFromTalismanRCFile *persistedRC, mode Mode) *Talisman
247247
tRC.AllowedPatterns[i] = regexp.MustCompile(p)
248248
}
249249
tRC.IgnoreConfigs = make([]IgnoreConfig, len(configFromTalismanRCFile.FileIgnoreConfig))
250-
for i, v := range configFromTalismanRCFile.FileIgnoreConfig {
251-
tRC.IgnoreConfigs[i] = IgnoreConfig(&v)
250+
for i := range configFromTalismanRCFile.FileIgnoreConfig {
251+
tRC.IgnoreConfigs[i] = &configFromTalismanRCFile.FileIgnoreConfig[i]
252252
}
253253
}
254254

@@ -265,8 +265,8 @@ func fromPersistedRC(configFromTalismanRCFile *persistedRC, mode Mode) *Talisman
265265
tRC.AllowedPatterns[i] = regexp.MustCompile(p)
266266
}
267267
tRC.IgnoreConfigs = make([]IgnoreConfig, len(scanconfigFromTalismanRCFile.FileIgnoreConfig))
268-
for i, v := range scanconfigFromTalismanRCFile.FileIgnoreConfig {
269-
tRC.IgnoreConfigs[i] = IgnoreConfig(&v)
268+
for i := range scanconfigFromTalismanRCFile.FileIgnoreConfig {
269+
tRC.IgnoreConfigs[i] = &scanconfigFromTalismanRCFile.FileIgnoreConfig[i]
270270
}
271271
}
272272
tRC.base = configFromTalismanRCFile
@@ -275,5 +275,6 @@ func fromPersistedRC(configFromTalismanRCFile *persistedRC, mode Mode) *Talisman
275275

276276
func For(mode Mode) *TalismanRC {
277277
configFromTalismanRCFile := ConfigFromFile()
278-
return fromPersistedRC(configFromTalismanRCFile, mode)
278+
talismanRC := fromPersistedRC(configFromTalismanRCFile, mode)
279+
return talismanRC
279280
}

talismanrc/talismanrc_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,28 @@ version: ""
301301
assert.Equal(t, expectedRC, str)
302302
})
303303
}
304+
305+
func TestFor(t *testing.T) {
306+
var repoFileReader = func(string) ([]byte, error) {
307+
return []byte(`fileignoreconfig:
308+
- filename: testfile_1.yml
309+
checksum: file1_checksum
310+
- filename: testfile_2.yml
311+
checksum: file2_checksum
312+
- filename: testfile_3.yml
313+
checksum: file3_checksum`), nil
314+
}
315+
t.Run("talismanrc.For(mode) should read multiple entries in rc file correctly", func(t *testing.T) {
316+
setRepoFileReader(repoFileReader)
317+
rc := For(HookMode)
318+
assert.Equal(t, 3, len(rc.IgnoreConfigs))
319+
320+
assert.Equal(t, rc.IgnoreConfigs[0].GetFileName(), "testfile_1.yml")
321+
assert.True(t, rc.IgnoreConfigs[0].ChecksumMatches("file1_checksum"))
322+
assert.Equal(t, rc.IgnoreConfigs[1].GetFileName(), "testfile_2.yml")
323+
assert.True(t, rc.IgnoreConfigs[1].ChecksumMatches("file2_checksum"))
324+
assert.Equal(t, rc.IgnoreConfigs[2].GetFileName(), "testfile_3.yml")
325+
assert.True(t, rc.IgnoreConfigs[2].ChecksumMatches("file3_checksum"))
326+
327+
})
328+
}

0 commit comments

Comments
 (0)