Skip to content

Commit

Permalink
handle comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrato committed Jul 24, 2024
1 parent c1f306c commit b5c9cb8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
17 changes: 6 additions & 11 deletions lib/secretsscanner/authorizedkeys/authorized_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ type WatcherConfig struct {
// Returns [ErrUnsupportedPlatform] if the operating system is not supported.
func NewWatcher(ctx context.Context, config WatcherConfig) (*Watcher, error) {

switch getOS(config) {
case constants.LinuxOS:
default:
if getOS(config) != constants.LinuxOS {
return nil, trace.Wrap(ErrUnsupportedPlatform)
}

Expand Down Expand Up @@ -258,7 +256,9 @@ func (w *Watcher) fetchAndReportAuthorizedKeys(
}

hostKeys, err := w.parseAuthorizedKeysFile(u, authorizedKeysPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
} else if err != nil {
w.logger.Warn("Failed to parse authorized_keys file", "error", err)
continue
}
Expand All @@ -273,10 +273,7 @@ func (w *Watcher) fetchAndReportAuthorizedKeys(
const maxKeysPerReport = 500
for i := 0; i < len(keys); i += maxKeysPerReport {
start := i
end := i + maxKeysPerReport
if end > len(keys) {
end = len(keys)
}
end := min(i+maxKeysPerReport, len(keys))
if err := stream.Send(
&accessgraphsecretsv1pb.ReportAuthorizedKeysRequest{
Keys: keys[start:end],
Expand Down Expand Up @@ -338,9 +335,7 @@ func userList(ctx context.Context, log *slog.Logger, filePath string) ([]user.Us

func (w *Watcher) parseAuthorizedKeysFile(u user.User, authorizedKeysPath string) ([]*accessgraphsecretsv1pb.AuthorizedKey, error) {
file, err := os.Open(authorizedKeysPath)
if errors.Is(err, os.ErrNotExist) {
return nil, nil
} else if err != nil {
if err != nil {
return nil, trace.Wrap(err)
}
defer func() {
Expand Down
24 changes: 16 additions & 8 deletions lib/secretsscanner/authorizedkeys/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,23 @@ func supervisorRunner(parentCtx context.Context, cfg supervisorRunnerConfig) err
mu sync.Mutex
)

getIsRunning := func() bool {
mu.Lock()
defer mu.Unlock()
return isRunning
}

setIsRunning := func(s bool) {
mu.Lock()
defer mu.Unlock()
isRunning = s
}

runRoutine := func(ctx context.Context, cancel context.CancelCauseFunc) {
defer func() {
wg.Done()
cancel(errShutdown)
mu.Lock()
isRunning = false
mu.Unlock()
setIsRunning(false)
}()
if err := cfg.runner(ctx); err != nil && !errors.Is(err, errShutdown) {
cfg.logger.WarnContext(ctx, "Runner failed", "error", err)
Expand All @@ -75,14 +85,12 @@ func supervisorRunner(parentCtx context.Context, cfg supervisorRunnerConfig) err
switch enabled, err := cfg.checkIfMonitorEnabled(parentCtx); {
case err != nil:
cfg.logger.WarnContext(parentCtx, "Failed to check if authorized keys report is enabled", "error", err)
case enabled && !isRunning:
case enabled && !getIsRunning():
runCtx, runCtxCancel = context.WithCancelCause(parentCtx)
mu.Lock()
isRunning = true
mu.Unlock()
setIsRunning(true)
wg.Add(1)
go runRoutine(runCtx, runCtxCancel)
case !enabled && isRunning:
case !enabled && getIsRunning():
runCtxCancel(errShutdown)
// Wait for the runner to stop before checking if the monitor is enabled again.
wg.Wait()
Expand Down
2 changes: 1 addition & 1 deletion lib/secretsscanner/authorizedkeys/supervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestSupervisorRunner(t *testing.T) {
mu.Lock()
defer mu.Unlock()
return !running
}, 1000*time.Millisecond, 10*time.Millisecond, "expected runner to re-stop, but it did not")
}, 100*time.Millisecond, 10*time.Millisecond, "expected runner to re-stop, but it did not")

// Cancel the context to stop the supervisor
cancel()
Expand Down

0 comments on commit b5c9cb8

Please sign in to comment.