Skip to content

Commit

Permalink
Issue crc-org#3514 Remove crc instance entries from known_hosts file
Browse files Browse the repository at this point in the history
while using the podman preset in CRC the instance identity is added to the
known_hosts file and when it changes podman-remote complains about it  and
shows the following error:
```
Error: failed to connect: ssh: handshake failed: knownhosts: key mismatch
```

this will occur in crc when users upgrade podman from 4.3.1 to 4.4.1 with this patch it'll
remove the [127.0.0.1]:2222 entries from known_hosts which should work-around this issue
  • Loading branch information
anjannath committed Mar 29, 2023
1 parent a1db0ce commit 1a850e8
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions cmd/crc/cmd/delete.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cmd

import (
"bufio"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/crc-org/crc/pkg/crc/constants"
crcErrors "github.com/crc-org/crc/pkg/crc/errors"
Expand Down Expand Up @@ -62,6 +65,9 @@ func deleteMachine(client machine.Client, clearCache bool, cacheDir string, inte
}

func runDelete(writer io.Writer, client machine.Client, clearCache bool, cacheDir string, interactive, force bool, outputFormat string) error {
if err := removeCRCHostEntriesFromKnownHosts(); err != nil {
logging.Warn("Unable to clear CRC instance entries from user's known_hosts file")
}
machineDeleted, err := deleteMachine(client, clearCache, cacheDir, interactive, force)
return render(&deleteResult{
Success: err == nil,
Expand All @@ -87,3 +93,49 @@ func (s *deleteResult) prettyPrintTo(writer io.Writer) error {
}
return nil
}

// TODO: remove it after we do one more upgrade of the podman bundle from 4.4.1
func removeCRCHostEntriesFromKnownHosts() error {
knownHostsPath := filepath.Join(constants.GetHomeDir(), ".ssh", "known_hosts")
f, err := os.Open(knownHostsPath)
if err != nil {
return fmt.Errorf("Unable to open user's 'known_hosts' file: %w", err)
}
defer f.Close()

tempHostsFile, err := os.CreateTemp("", "crc")
if err != nil {
return fmt.Errorf("Unable to create temp file: %w", err)
}
defer func() {
tempHostsFile.Close()
os.Remove(tempHostsFile.Name())
}()

if err := tempHostsFile.Chmod(0600); err != nil {
return fmt.Errorf("Error trying to change permissions for temp file: %w", err)
}

var foundCRCEntries bool
scanner := bufio.NewScanner(f)
writer := bufio.NewWriter(tempHostsFile)
for scanner.Scan() {
if strings.Contains(scanner.Text(), "[127.0.0.1]:2222") {
foundCRCEntries = true
continue
}
_, err := writer.WriteString(fmt.Sprintf("%s\n", scanner.Text()))
if err != nil {
return fmt.Errorf("Error while writing hostsfile content to temp file: %w", err)
}
}

if err := writer.Flush(); err != nil {
return fmt.Errorf("Error while flushing buffered content to temp file: %w", err)
}

if foundCRCEntries {
return os.Rename(tempHostsFile.Name(), knownHostsPath)
}
return nil
}

0 comments on commit 1a850e8

Please sign in to comment.