-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib/ssh: use UserKnownHostFile from configuration in NewClientInterac…
…tive Previously, the ssh Client always use InsecureIgnoreHostKey in HostKeyCallback. This may post security issue, like man-in-the-middle attack, since we did not check the server host key with one of key that known by client from UserKnownHostFile (for example ~/.ssh/known_hosts). This changes use the SSH section UserKnownHostFile from configuration (default to ~/.ssh/known_hosts) to check if the server host key is valid. The NewClientInteractive will return an error, "key is unknown", if host key not exist in UserKnownHostFile or "key is mismatch" if host key not match with one registered in UserKnownHostFile.
- Loading branch information
Showing
4 changed files
with
163 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2023, Shulhan <[email protected]>. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package ssh | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/shuLhan/share/lib/ssh/config" | ||
) | ||
|
||
// TestNewClient_KeyError test SSH to server with host key does not exist in | ||
// known_hosts database. | ||
func TestNewClient_KeyError_notExist(t *testing.T) { | ||
t.Skip(`Require active SSH server`) | ||
|
||
var ( | ||
section = config.NewSection(`localhost`) | ||
|
||
wd string | ||
pathFile string | ||
err error | ||
) | ||
|
||
wd, err = os.Getwd() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = section.Set(config.KeyUser, `ms`) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
err = section.Set(config.KeyHostname, `localhost`) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
pathFile = filepath.Join(wd, `testdata/localhost/known_hosts_empty`) | ||
err = section.Set(config.KeyUserKnownHostsFile, pathFile) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
pathFile = filepath.Join(wd, `testdata/localhost/client.key`) | ||
err = section.Set(config.KeyIdentityFile, pathFile) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, err = NewClientInteractive(section) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-----BEGIN OPENSSH PRIVATE KEY----- | ||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW | ||
QyNTUxOQAAACA0CnQ54UzXoKW709LIYSxFLiVf+ibVbxsB8sCmGQBDiQAAAJDOW1pGzlta | ||
RgAAAAtzc2gtZWQyNTUxOQAAACA0CnQ54UzXoKW709LIYSxFLiVf+ibVbxsB8sCmGQBDiQ | ||
AAAEDkCGqgWIckW9eebw+fGj6m4cGrzc+qUSPxBjFAsPDHxjQKdDnhTNegpbvT0shhLEUu | ||
JV/6JtVvGwHywKYZAEOJAAAAC2F3d2FuQGxvY2FsAQI= | ||
-----END OPENSSH PRIVATE KEY----- |
Empty file.