Browse Source

Fix SSH key loading to read .pub file directly

Problem: When loading existing ED25519 key, ParseRawPrivateKey failed
with "key is not ED25519" error during type assertion

Solution: Simplified GenerateOrLoadSSHKey to read .pub file directly
instead of parsing private key and extracting public key

This eliminates complex PEM parsing and makes the function more reliable

Tested: SSH tunnel successfully connects to device via stub server
- Device registers with SSH public key
- Server allocates port 50000 for SSH tunnel
- Tunnel establishes: 192.168.5.2:50000 -> device:22
- Connection works: ssh -J user@stub -p 50000 root@localhost

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
root 1 month ago
parent
commit
07dc293a3a
1 changed files with 5 additions and 29 deletions
  1. 5 29
      cmd/beacon-daemon/client.go

+ 5 - 29
cmd/beacon-daemon/client.go

@@ -271,37 +271,14 @@ func (c *APIClient) UpdateWiFiCredentials(ssid, psk string) error {
 // Returns OpenSSH public key format
 func GenerateOrLoadSSHKey(keyPath string) (string, error) {
 	// Check if key already exists
+	pubKeyPath := keyPath + ".pub"
 	if _, err := os.Stat(keyPath); err == nil {
-		// Load existing key
-		privKeyBytes, err := os.ReadFile(keyPath)
+		// Key exists - read public key file
+		pubKeyBytes, err := os.ReadFile(pubKeyPath)
 		if err != nil {
-			return "", fmt.Errorf("failed to read existing key: %w", err)
-		}
-
-		block, _ := pem.Decode(privKeyBytes)
-		if block == nil {
-			return "", fmt.Errorf("failed to decode PEM block")
+			return "", fmt.Errorf("failed to read public key: %w", err)
 		}
-
-		// Parse ED25519 private key
-		privKey, err := ssh.ParseRawPrivateKey(privKeyBytes)
-		if err != nil {
-			return "", fmt.Errorf("failed to parse private key: %w", err)
-		}
-
-		ed25519Key, ok := privKey.(ed25519.PrivateKey)
-		if !ok {
-			return "", fmt.Errorf("key is not ED25519")
-		}
-
-		// Extract public key
-		pubKey := ed25519Key.Public().(ed25519.PublicKey)
-		sshPubKey, err := ssh.NewPublicKey(pubKey)
-		if err != nil {
-			return "", fmt.Errorf("failed to create SSH public key: %w", err)
-		}
-
-		return string(ssh.MarshalAuthorizedKey(sshPubKey)), nil
+		return string(pubKeyBytes), nil
 	}
 
 	// Generate new ED25519 key pair
@@ -336,7 +313,6 @@ func GenerateOrLoadSSHKey(keyPath string) (string, error) {
 	pubKeyStr := string(ssh.MarshalAuthorizedKey(sshPubKey))
 
 	// Save public key
-	pubKeyPath := keyPath + ".pub"
 	if err := os.WriteFile(pubKeyPath, []byte(pubKeyStr), 0644); err != nil {
 		return "", fmt.Errorf("failed to write public key: %w", err)
 	}