|
| 1 | +package backup |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +// common location of keys - more can be added here |
| 10 | +var StandardKeyLocations = []string{ |
| 11 | + "~/.ssh/", |
| 12 | + "~/.gnupg/", |
| 13 | +} |
| 14 | + |
| 15 | +// any saved keylcoation |
| 16 | +type KeyLocation struct { |
| 17 | + Path string |
| 18 | + |
| 19 | + Type string // "ssh", "gpg", "custom" strongs can be stored |
| 20 | + |
| 21 | + Files []string |
| 22 | + |
| 23 | + IsDirectory bool |
| 24 | +} |
| 25 | + |
| 26 | +// searches for keys in standard locations |
| 27 | +func searchStandardLocations() ([]KeyLocation, error) { |
| 28 | + var locations []KeyLocation |
| 29 | + homeDir, err := os.UserHomeDir() |
| 30 | + if err != nil { |
| 31 | + return nil, err |
| 32 | + } |
| 33 | + |
| 34 | + for _, location := range StandardKeyLocations { |
| 35 | + // reeplace ~operator with actual home directory |
| 36 | + fullPath := strings.Replace(location, "~", homeDir, 1) |
| 37 | + |
| 38 | + if _, err := os.Stat(fullPath); os.IsNotExist(err) { |
| 39 | + continue // skip on dir invalid |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + keyType := determineKeyType(fullPath) |
| 44 | + files, err := discoverKeyFiles(fullPath) |
| 45 | + if err != nil { |
| 46 | + continue |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + if len(files) > 0 { |
| 51 | + locations = append(locations, KeyLocation{ |
| 52 | + |
| 53 | + Path: fullPath, |
| 54 | + Type: keyType, |
| 55 | + Files: files, |
| 56 | + IsDirectory: true, |
| 57 | + }) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return locations, nil |
| 62 | +} |
| 63 | + |
| 64 | +// determineKeyType identifies the type of keys based on directory path |
| 65 | +func determineKeyType(path string) string { |
| 66 | + if strings.Contains(path, ".ssh") { |
| 67 | + return "ssh" |
| 68 | + } |
| 69 | + if strings.Contains(path, ".gnupg") { |
| 70 | + return "gpg" |
| 71 | + } |
| 72 | + return "custom" |
| 73 | +} |
| 74 | + |
| 75 | +// discoverKeyFiles finds all key files in a directory |
| 76 | +func discoverKeyFiles(dirPath string) ([]string, error) { |
| 77 | + var keyFiles []string |
| 78 | + |
| 79 | + err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error { |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + if !info.IsDir() && isKeyFile(path, info) { |
| 85 | + keyFiles = append(keyFiles, path) |
| 86 | + } |
| 87 | + |
| 88 | + return nil |
| 89 | + }) |
| 90 | + |
| 91 | + return keyFiles, err |
| 92 | +} |
| 93 | + |
| 94 | +// isKeyFile determines if a file is likely a key file |
| 95 | +func isKeyFile(path string, info os.FileInfo) bool { |
| 96 | + name := info.Name() |
| 97 | + |
| 98 | + // SSH key patterns |
| 99 | + sshPatterns := []string{ |
| 100 | + "id_rsa", "id_dsa", "id_ecdsa", "id_ed25519", |
| 101 | + "authorized_keys", "known_hosts", "config", |
| 102 | + } |
| 103 | + |
| 104 | + // GPG key patterns |
| 105 | + gpgPatterns := []string{ |
| 106 | + "pubring.gpg", "secring.gpg", "trustdb.gpg", |
| 107 | + "gpg.conf", "gpg-agent.conf", |
| 108 | + } |
| 109 | + |
| 110 | + // check SSH |
| 111 | + for _, pattern := range sshPatterns { |
| 112 | + if strings.Contains(name, pattern) { |
| 113 | + return true |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // cgeck GPG |
| 118 | + for _, pattern := range gpgPatterns { |
| 119 | + if strings.Contains(name, pattern) { |
| 120 | + return true |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // check for private key extensions - more can be added |
| 125 | + if strings.HasSuffix(name, ".pub") || |
| 126 | + strings.HasSuffix(name, ".pem") || |
| 127 | + strings.HasSuffix(name, ".key") { |
| 128 | + return true |
| 129 | + } |
| 130 | + |
| 131 | + return false |
| 132 | +} |
0 commit comments