Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v15] [scankeys] support context cancellation to interrupt keys scan #47657

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/secretsscanner/scanner/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func (s *Scanner) findPrivateKeys(ctx context.Context, root, deviceID string, pr
logger := s.log.With("root", root)

err := filepath.WalkDir(root, func(path string, info fs.DirEntry, err error) error {
// check if the context is done before processing the file.
if ctx.Err() != nil {
return ctx.Err()
}
if err != nil {
logger.DebugContext(ctx, "error walking directory", "path", path, "error", err)
return fs.SkipDir
Expand Down
17 changes: 14 additions & 3 deletions tool/tsh/common/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ func (c *scanKeysCommand) run(cf *CLIConf) error {
return trace.Wrap(err, "device not enrolled")
}

fmt.Printf("Device trust credentials found.\nScanning %s.\n", strings.Join(c.dirs, ", "))
dirs := splitCommaSeparatedSlice(c.dirs)
fmt.Printf("Device trust credentials found.\nScanning %s.\n", strings.Join(dirs, ", "))

scanner, err := secretsscanner.New(secretsscanner.Config{
Dirs: c.dirs,
SkipPaths: c.skipPaths,
Dirs: dirs,
SkipPaths: splitCommaSeparatedSlice(c.skipPaths),
Log: slog.Default(),
})
if err != nil {
Expand Down Expand Up @@ -168,3 +169,13 @@ func collectPrivateKeys(privateKeys []secretsscanner.SSHPrivateKey) []*accessgra
}
return keys
}

func splitCommaSeparatedSlice(s []string) []string {
var result []string
for _, entry := range s {
for _, split := range strings.Split(entry, ",") {
result = append(result, strings.TrimSpace(split))
}
}
return result
}
69 changes: 69 additions & 0 deletions tool/tsh/common/scan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Teleport
* Copyright (C) 2024 Gravitational, 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 <http://www.gnu.org/licenses/>.
*/

package common

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_splitCommaSeparatedSlice(t *testing.T) {

tests := []struct {
name string
args []string
want []string
}{
{
name: "empty",
},
{
name: "single dir",
args: []string{"dir1"},
want: []string{"dir1"},
},
{
name: "multi dir",
args: []string{"dir1", "dir2"},
want: []string{"dir1", "dir2"},
},
{
name: "multi dir comma separated",
args: []string{"dir1,dir2"},
want: []string{"dir1", "dir2"},
},
{
name: "multi dir comma separated spaces",
args: []string{"dir1, dir2"},
want: []string{"dir1", "dir2"},
},
{
name: "multi dir comma separated spaces",
args: []string{"dir1, dir2", "dir3"},
want: []string{"dir1", "dir2", "dir3"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := splitCommaSeparatedSlice(tt.args)
require.Equal(t, tt.want, got)
})
}
}
Loading