diff --git a/cmd/crc/cmd/delete.go b/cmd/crc/cmd/delete.go index 126017cdac..dff81eaa0b 100644 --- a/cmd/crc/cmd/delete.go +++ b/cmd/crc/cmd/delete.go @@ -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" @@ -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, @@ -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 +}