scanSSHDirectory scans the SSH directory for private key files
(sshDir string)
| 177 | |
| 178 | // scanSSHDirectory scans the SSH directory for private key files |
| 179 | func scanSSHDirectory(sshDir string) ([]*SSHKeyInfo, error) { |
| 180 | // Check if directory exists |
| 181 | if _, err := os.Stat(sshDir); os.IsNotExist(err) { |
| 182 | return nil, fmt.Errorf("SSH directory not found: %s", sshDir) |
| 183 | } |
| 184 | |
| 185 | // Standard SSH private key patterns |
| 186 | keyPatterns := []string{ |
| 187 | "id_rsa", |
| 188 | "id_dsa", |
| 189 | "id_ecdsa", |
| 190 | "id_ed25519", |
| 191 | "id_ecdsa_sk", |
| 192 | "id_ed25519_sk", |
| 193 | } |
| 194 | |
| 195 | var keys []*SSHKeyInfo |
| 196 | |
| 197 | // Read directory entries |
| 198 | entries, err := os.ReadDir(sshDir) |
| 199 | if err != nil { |
| 200 | return nil, fmt.Errorf("failed to read SSH directory: %w", err) |
| 201 | } |
| 202 | |
| 203 | // Scan for keys |
| 204 | for _, entry := range entries { |
| 205 | if entry.IsDir() { |
| 206 | continue |
| 207 | } |
| 208 | |
| 209 | filename := entry.Name() |
| 210 | |
| 211 | // Skip known non-key files |
| 212 | if strings.HasSuffix(filename, ".pub") || |
| 213 | strings.HasSuffix(filename, ".ppk") || |
| 214 | filename == "config" || |
| 215 | filename == "known_hosts" || |
| 216 | filename == "authorized_keys" { |
| 217 | continue |
| 218 | } |
| 219 | |
| 220 | // Check if it matches standard patterns OR has no extension |
| 221 | isStandardKey := false |
| 222 | for _, pattern := range keyPatterns { |
| 223 | if strings.HasPrefix(filename, pattern) { |
| 224 | isStandardKey = true |
| 225 | break |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // Also check files without extensions (potential custom keys) |
| 230 | if !isStandardKey && !strings.Contains(filename, ".") { |
| 231 | isStandardKey = true |
| 232 | } |
| 233 | |
| 234 | if !isStandardKey { |
| 235 | continue |
| 236 | } |
no test coverage detected