Skip to content

Commit

Permalink
refactor: implement changes from #58
Browse files Browse the repository at this point in the history
Co-authored-by: Maxim Baz <[email protected]>
  • Loading branch information
Pablito2020 and maximbaz committed Nov 21, 2024
1 parent f6d4982 commit 4e697eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 28 deletions.
6 changes: 3 additions & 3 deletions detector/gpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (

// WatchGPG watches for hints that YubiKey is maybe waiting for a touch on a GPG request
func WatchGPG(filesToWatch []string, requestGPGCheck chan bool) {
events := make(chan notify.EventInfo, len(filesToWatch))
events := make(chan notify.EventInfo)

initWatcher := func() {
for _, file := range filesToWatch {
if err := notify.Watch(file, events, notify.InOpen); err != nil {
if err := notify.Watch(file, events, notify.InOpen, notify.InDeleteSelf, notify.InMoveSelf); err != nil {
log.Errorf("Failed to watch file '%s': %v\n", file, err)
return;
}
log.Debug("GPG watcher is watching '%s'...\n", file)
log.Debugf("GPG watcher is watching '%s'...\n", file)
}
}

Expand Down
35 changes: 10 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bufio"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -80,14 +79,14 @@ func main() {
} else if ctx.SetProtocol(gpgme.ProtocolAssuan) != nil {
log.Debugf("Cannot initialize Assuan IPC: %v. Disabling GPG and SSH watchers.", err)
} else {
var gpgPubringPath = path.Join(gpgme.GetDirInfo("homedir"), "private-keys-v1.d")
if _, err := os.Stat(gpgPubringPath); os.IsNotExist(err) {
fmt.Printf("Directory '%s' does not exist (you have no private keys).\n", gpgPubringPath)
var gpgPrivateKeysDirPath = path.Join(gpgme.GetDirInfo("homedir"), "private-keys-v1.d")
if _, err := os.Stat(gpgPrivateKeysDirPath); os.IsNotExist(err) {
log.Debugf("Directory '%s' does not exist (you have no private keys).\n", gpgPrivateKeysDirPath)
return
}
var searchTerm = "shadowed-private-key"
searchTerm := "shadowed-private-key"
var filesToWatch []string
filesToWatch, err := findMatchingFiles(gpgPubringPath, searchTerm)
filesToWatch, err := findShadowedPrivateKeys(gpgPrivateKeysDirPath, searchTerm)
if err != nil {
fmt.Printf("Error finding files: %v\n", err)
return
Expand All @@ -105,32 +104,18 @@ func main() {
<-wait
}

func findMatchingFiles(folderPath, term string) ([]string, error) {
func findShadowedPrivateKeys(folderPath, term string) ([]string, error) {
var result []string
err := filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
if err != nil || info.IsDir() {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
data, err := os.ReadFile(path)
if err != nil {
return err
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, term) {
result = append(result, path)
break // No need to scan further lines if we already found the string
}
}

if err := scanner.Err(); err != nil {
return err
if strings.Contains(string(data), term) {
result = append(result, path)
}
return nil
})
Expand Down

0 comments on commit 4e697eb

Please sign in to comment.