Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code scanning alert no. 9: Use of insecure HostKeyCallback implementation #5

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"time"

"golang.org/x/crypto/ssh"
"io/ioutil"

Check failure on line 11 in pkg/ssh/ssh.go

View workflow job for this annotation

GitHub Actions / Build, Test, and Lint (1.22.x, ubuntu-latest)

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package [io] or package [os], and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)

Check failure on line 11 in pkg/ssh/ssh.go

View workflow job for this annotation

GitHub Actions / Build, Test, and Lint (stable, ubuntu-latest)

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package [io] or package [os], and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
)

// NewSSHClientWithKey creates a new ssh.Client using a private key
Expand All @@ -17,10 +18,19 @@
return nil, fmt.Errorf("failed to parse private key: %v", err)
}

publicKeyBytes, err := ioutil.ReadFile("allowed_hostkey.pub")
if err != nil {
return nil, fmt.Errorf("failed to read allowed host key: %v", err)
}
allowedHostKey, err := ssh.ParsePublicKey(publicKeyBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse allowed host key: %v", err)
}

config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
HostKeyCallback: ssh.FixedHostKey(allowedHostKey),
Timeout: 10 * time.Second,
}

Expand All @@ -36,10 +46,19 @@

// NewSSHClientWithPassword creates a new ssh.Client using a password
func NewSSHClientWithPassword(host string, port string, user string, password string) (*ssh.Client, error) {
publicKeyBytes, err := ioutil.ReadFile("allowed_hostkey.pub")
if err != nil {
return nil, fmt.Errorf("failed to read allowed host key: %v", err)
}
allowedHostKey, err := ssh.ParsePublicKey(publicKeyBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse allowed host key: %v", err)
}

config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{ssh.Password(password)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
HostKeyCallback: ssh.FixedHostKey(allowedHostKey),
Timeout: 10 * time.Second,
}

Expand Down
Loading