no-src / gofs

A cross-platform real-time file synchronization tool out of the box based on Golang
Apache License 2.0
460 stars 40 forks source link

fix: invalid ssh key file #246

Closed capric98 closed 1 year ago

capric98 commented 1 year ago

Problem: gofs will always throw an error of "invalid ssh key file" when using ssh_key authentication, the problem is from: https://github.com/no-src/gofs/blob/e14bbcb6290010524f68bcd99dc7e74907bb1351/driver/sftp/sftp.go#L109-L120 where keyFileName is not "known_hosts".

Refer to the example from golang.org/x/crypto/ssh, we could use ssh.InsecureIgnoreHostKey() as HostKeyCallback function temporarily, because seems like ssh.ParseKnownHosts(keyData) has some other issue, and the host may not be in known_hosts file already.

Instead, the private key file could be parsed by ssh.ParsePrivateKey() to be a ssh.Signer and then ssh.PublicKeys() will convert it to ssh.AuthMethod.

codecov[bot] commented 1 year ago

Codecov Report

Merging #246 (185707a) into main (e14bbcb) will not change coverage. Report is 1 commits behind head on main. The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main      #246   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           55        55           
  Lines         1686      1690    +4     
=========================================
+ Hits          1686      1690    +4     
Files Changed Coverage Δ
conf/config.go 100.00% <ø> (ø)
flag/flag.go 100.00% <ø> (ø)
core/vfs.go 100.00% <100.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

mstmdev commented 1 year ago

I remove the -ssh_key flag and add some parameters to -source and -dest flags: ssh_user, ssh_pass, ssh_key, ssh_key_pass, ssh_host_key.

Use the above parameters to control the sftp authentication, for example:

$ gofs -source="sftp://127.0.0.1:22?remote_path=/gofs_sftp_server&ssh_user=sftp_user&ssh_pass=sftp_pwd&ssh_key=./id_rsa&ssh_key_pass=123456&ssh_host_key=/root/.ssh/known_hosts" -dest="./dest" -sync_once
mstmdev commented 1 year ago

Please help review the code. @capric98

mstmdev commented 1 year ago

Great idea, a warning log will be printed when the host key is empty. Please review it again. @capric98

mstmdev commented 1 year ago

Merged.