-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sshiofs, a io/fs.FS implementaion for ssh with sudo
Signed-off-by: Artiom Diomin <[email protected]>
- Loading branch information
Showing
6 changed files
with
250 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module k8c.io/kubeone | ||
|
||
go 1.14 | ||
go 1.16 | ||
|
||
require ( | ||
github.com/BurntSushi/toml v0.3.1 | ||
|
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
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,105 @@ | ||
/* | ||
Copyright 2021 The KubeOne Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package sshiofs | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"io/fs" | ||
"strings" | ||
|
||
"k8c.io/kubeone/pkg/ssh" | ||
) | ||
|
||
type sshfile struct { | ||
conn ssh.Connection | ||
name string | ||
cursor int64 | ||
fi fs.FileInfo | ||
} | ||
|
||
func (sf *sshfile) Stat() (fs.FileInfo, error) { | ||
if sf.fi != nil { | ||
return sf.fi, nil | ||
} | ||
|
||
fi, err := newSSHFileInfo(sf.name, sf.conn) | ||
sf.fi = fi | ||
return fi, err | ||
} | ||
|
||
func (sf *sshfile) Read(p []byte) (int, error) { | ||
const cmdTpl = `sudo dd status=none iflag=count_bytes,skip_bytes skip=%d count=%d if=%q` | ||
var ( | ||
stdout bytes.Buffer | ||
stderr bytes.Buffer | ||
) | ||
|
||
cmd := fmt.Sprintf(cmdTpl, sf.cursor, len(p), sf.name) | ||
_, err := sf.conn.POpen(cmd, nil, &stdout, &stderr) | ||
if err != nil { | ||
return 0, &fs.PathError{ | ||
Op: "read", | ||
Path: sf.name, | ||
Err: fmt.Errorf("%s %w", stderr.String(), err), | ||
} | ||
} | ||
|
||
n, err := stdout.Read(p) | ||
sf.cursor += int64(n) | ||
return n, err | ||
} | ||
|
||
func (sf *sshfile) Write(p []byte) (int, error) { | ||
const cmdTpl = `sudo dd status=none oflag=seek_bytes conv=notrunc seek=%d of=%q` | ||
cmd := fmt.Sprintf(cmdTpl, sf.cursor, sf.name) | ||
|
||
var ( | ||
stdin = bytes.NewBuffer(p) | ||
stdout strings.Builder | ||
stderr strings.Builder | ||
) | ||
|
||
_, err := sf.conn.POpen(cmd, stdin, &stdout, &stderr) | ||
if err != nil { | ||
return 0, fmt.Errorf("%w: %v %v", err, stderr.String(), stdout.String()) | ||
} | ||
|
||
n, err := stdout.Write(p) | ||
sf.cursor += int64(n) | ||
return n, err | ||
} | ||
|
||
func (sf *sshfile) Seek(offset int64, whence int) (int64, error) { | ||
switch whence { | ||
case io.SeekStart: | ||
sf.cursor = offset | ||
case io.SeekCurrent: | ||
sf.cursor += offset | ||
case io.SeekEnd: | ||
return sf.cursor, fmt.Errorf("io.SeekEnd unimplemented") | ||
} | ||
|
||
return sf.cursor, nil | ||
} | ||
|
||
func (sf *sshfile) Close() error { | ||
sf.cursor = 0 | ||
sf.fi = nil | ||
return nil | ||
} |
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,136 @@ | ||
/* | ||
Copyright 2021 The KubeOne Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package sshiofs | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/fs" | ||
"strings" | ||
"time" | ||
|
||
"k8c.io/kubeone/pkg/ssh" | ||
) | ||
|
||
func New(conn ssh.Connection) fs.FS { | ||
return &sshfs{conn: conn} | ||
} | ||
|
||
type sshfs struct { | ||
conn ssh.Connection | ||
} | ||
|
||
func (sfs *sshfs) Open(name string) (fs.File, error) { | ||
var hadSlashPrefix bool | ||
if strings.HasPrefix(name, "/") { | ||
name = strings.TrimPrefix(name, "/") | ||
hadSlashPrefix = true | ||
} | ||
if !fs.ValidPath(name) { | ||
return nil, &fs.PathError{ | ||
Op: "open", | ||
Path: name, | ||
Err: fs.ErrInvalid, | ||
} | ||
} | ||
if hadSlashPrefix { | ||
name = "/" + name | ||
} | ||
|
||
return &sshfile{ | ||
conn: sfs.conn, | ||
name: name, | ||
}, nil | ||
} | ||
|
||
func (sfs *sshfs) ReadFile(name string) ([]byte, error) { | ||
var buf bytes.Buffer | ||
_, err := sfs.conn.POpen(fmt.Sprintf("sudo cat %q", name), nil, &buf, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} | ||
|
||
// TODO: implement | ||
// func (sfs *sshfs) ReadDir(name string) ([]fs.DirEntry, error) { | ||
// return nil, nil | ||
// } | ||
|
||
func newSSHFileInfo(name string, conn ssh.Connection) (fs.FileInfo, error) { | ||
const statCmd = "sudo stat --printf='%%s %%f %%Y' %q" | ||
|
||
var ( | ||
stdout strings.Builder | ||
stderr strings.Builder | ||
) | ||
|
||
cmd := fmt.Sprintf(statCmd, name) | ||
exitCode, err := conn.POpen(cmd, nil, &stdout, &stderr) | ||
if exitCode != 0 || err != nil { | ||
return nil, &fs.PathError{ | ||
Op: "open", | ||
Path: name, | ||
Err: fmt.Errorf("%s %s %w", stderr.String(), err.Error(), fs.ErrNotExist), | ||
} | ||
} | ||
|
||
var ( | ||
size int64 | ||
mode fs.FileMode | ||
modTime int64 | ||
) | ||
|
||
fia := strings.Split(stdout.String(), " ") | ||
if len(fia) != 3 { | ||
return nil, fs.ErrInvalid | ||
} | ||
|
||
if _, err = fmt.Sscanf(fia[0], "%d", &size); err != nil { | ||
return nil, err | ||
} | ||
|
||
if _, err = fmt.Sscanf(fia[1], "%x", &mode); err != nil { | ||
return nil, err | ||
} | ||
|
||
if _, err = fmt.Sscanf(fia[2], "%d", &modTime); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &fileInfo{ | ||
name: name, | ||
size: size, | ||
mode: mode, | ||
time: time.Unix(modTime, 0), | ||
}, nil | ||
} | ||
|
||
type fileInfo struct { | ||
name string | ||
size int64 | ||
mode fs.FileMode | ||
time time.Time | ||
} | ||
|
||
func (fi *fileInfo) Size() int64 { return fi.size } | ||
func (fi *fileInfo) Mode() fs.FileMode { return fi.mode } | ||
func (fi *fileInfo) ModTime() time.Time { return fi.time } | ||
func (fi *fileInfo) Name() string { return fi.name } | ||
func (fi *fileInfo) IsDir() bool { return fi.mode.IsDir() } | ||
func (*fileInfo) Sys() interface{} { return nil } |