Skip to content

Commit 09b97a3

Browse files
anjannathopenshift-merge-robot
authored andcommitted
move func RemoveCRCHostEntriesFromKnownHosts() to pkg/crc/ssh package
we need to call this function from atleast two places this'd allow us to do that
1 parent 0959fb6 commit 09b97a3

File tree

2 files changed

+59
-59
lines changed

2 files changed

+59
-59
lines changed

pkg/crc/machine/delete.go

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package machine
22

33
import (
4-
"bufio"
5-
"fmt"
64
"os"
7-
"path/filepath"
8-
"strings"
95

10-
"github.com/crc-org/crc/pkg/crc/constants"
116
"github.com/crc-org/crc/pkg/crc/logging"
127
"github.com/crc-org/crc/pkg/crc/podman"
8+
"github.com/crc-org/crc/pkg/crc/ssh"
139
"github.com/pkg/errors"
1410
)
1511

@@ -44,58 +40,5 @@ func (client *client) Delete() error {
4440
logging.Warnf("Failed to remove crc contexts from kubeconfig: %v", err)
4541
}
4642
}
47-
return removeCRCHostEntriesFromKnownHosts()
48-
}
49-
50-
func removeCRCHostEntriesFromKnownHosts() error {
51-
knownHostsPath := filepath.Join(constants.GetHomeDir(), ".ssh", "known_hosts")
52-
if _, err := os.Stat(knownHostsPath); err != nil {
53-
return nil
54-
}
55-
f, err := os.Open(knownHostsPath)
56-
if err != nil {
57-
return fmt.Errorf("Unable to open user's 'known_hosts' file: %w", err)
58-
}
59-
defer f.Close()
60-
61-
tempHostsFile, err := os.CreateTemp(filepath.Join(constants.GetHomeDir(), ".ssh"), "crc")
62-
if err != nil {
63-
return fmt.Errorf("Unable to create temp file: %w", err)
64-
}
65-
defer func() {
66-
tempHostsFile.Close()
67-
os.Remove(tempHostsFile.Name())
68-
}()
69-
70-
if err := tempHostsFile.Chmod(0600); err != nil {
71-
return fmt.Errorf("Error trying to change permissions for temp file: %w", err)
72-
}
73-
74-
var foundCRCEntries bool
75-
scanner := bufio.NewScanner(f)
76-
writer := bufio.NewWriter(tempHostsFile)
77-
for scanner.Scan() {
78-
if strings.Contains(scanner.Text(), "[127.0.0.1]:2222") || strings.Contains(scanner.Text(), "192.168.130.11") {
79-
foundCRCEntries = true
80-
continue
81-
}
82-
if _, err := writer.WriteString(fmt.Sprintf("%s\n", scanner.Text())); err != nil {
83-
return fmt.Errorf("Error while writing hostsfile content to temp file: %w", err)
84-
}
85-
}
86-
87-
if err := writer.Flush(); err != nil {
88-
return fmt.Errorf("Error while flushing buffered content to temp file: %w", err)
89-
}
90-
91-
if foundCRCEntries {
92-
if err := f.Close(); err != nil {
93-
return fmt.Errorf("Error closing known_hosts file: %w", err)
94-
}
95-
if err := tempHostsFile.Close(); err != nil {
96-
return fmt.Errorf("Error closing temp file: %w", err)
97-
}
98-
return os.Rename(tempHostsFile.Name(), knownHostsPath)
99-
}
100-
return nil
43+
return ssh.RemoveCRCHostEntriesFromKnownHosts()
10144
}

pkg/crc/ssh/keys.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package ssh
22

33
import (
4+
"bufio"
45
"crypto/ecdsa"
56
"crypto/elliptic"
67
"crypto/rand"
78
"crypto/x509"
89
"errors"
910
"fmt"
1011
"os"
12+
"path/filepath"
13+
"strings"
1114

15+
"github.com/crc-org/crc/pkg/crc/constants"
1216
gossh "golang.org/x/crypto/ssh"
1317
)
1418

@@ -69,3 +73,56 @@ func GenerateSSHKey(path string) error {
6973

7074
return nil
7175
}
76+
77+
func RemoveCRCHostEntriesFromKnownHosts() error {
78+
knownHostsPath := filepath.Join(constants.GetHomeDir(), ".ssh", "known_hosts")
79+
if _, err := os.Stat(knownHostsPath); err != nil {
80+
return nil
81+
}
82+
f, err := os.Open(knownHostsPath)
83+
if err != nil {
84+
return fmt.Errorf("Unable to open user's 'known_hosts' file: %w", err)
85+
}
86+
defer f.Close()
87+
88+
tempHostsFile, err := os.CreateTemp(filepath.Join(constants.GetHomeDir(), ".ssh"), "crc")
89+
if err != nil {
90+
return fmt.Errorf("Unable to create temp file: %w", err)
91+
}
92+
defer func() {
93+
tempHostsFile.Close()
94+
os.Remove(tempHostsFile.Name())
95+
}()
96+
97+
if err := tempHostsFile.Chmod(0600); err != nil {
98+
return fmt.Errorf("Error trying to change permissions for temp file: %w", err)
99+
}
100+
101+
var foundCRCEntries bool
102+
scanner := bufio.NewScanner(f)
103+
writer := bufio.NewWriter(tempHostsFile)
104+
for scanner.Scan() {
105+
if strings.Contains(scanner.Text(), "[127.0.0.1]:2222") || strings.Contains(scanner.Text(), "192.168.130.11") {
106+
foundCRCEntries = true
107+
continue
108+
}
109+
if _, err := writer.WriteString(fmt.Sprintf("%s\n", scanner.Text())); err != nil {
110+
return fmt.Errorf("Error while writing hostsfile content to temp file: %w", err)
111+
}
112+
}
113+
114+
if err := writer.Flush(); err != nil {
115+
return fmt.Errorf("Error while flushing buffered content to temp file: %w", err)
116+
}
117+
118+
if foundCRCEntries {
119+
if err := f.Close(); err != nil {
120+
return fmt.Errorf("Error closing known_hosts file: %w", err)
121+
}
122+
if err := tempHostsFile.Close(); err != nil {
123+
return fmt.Errorf("Error closing temp file: %w", err)
124+
}
125+
return os.Rename(tempHostsFile.Name(), knownHostsPath)
126+
}
127+
return nil
128+
}

0 commit comments

Comments
 (0)