Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions api/proto/teleport/legacy/types/events/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,7 @@ enum SFTPAction {
RENAME = 16;
READLINK = 17;
SYMLINK = 18;
LINK = 19;
}

// SFTP is emitted when file operations have occurred between server and client
Expand Down
37 changes: 20 additions & 17 deletions api/types/events/events.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ replace (
github.com/julienschmidt/httprouter => github.com/gravitational/httprouter v1.3.1-0.20220408074523-c876c5e705a5
github.com/keys-pub/go-libfido2 => github.com/gravitational/go-libfido2 v1.5.3-0.20230202181331-c71192ef1c8a
github.com/microsoft/go-mssqldb => github.com/gravitational/go-mssqldb v0.11.1-0.20230331180905-0f76f1751cd3
github.com/pkg/sftp => github.com/gravitational/sftp v1.13.6-0.20230328150159-dfe4e0d94419
github.com/sirupsen/logrus => github.com/gravitational/logrus v1.4.4-0.20210817004754-047e20245621
github.com/vulcand/predicate => github.com/gravitational/predicate v1.3.0
// Use our internal crypto fork, to work around the issue with OpenSSH <= 7.6 mentioned here: https://github.com/golang/go/issues/53391
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,6 @@ github.com/gravitational/redis/v9 v9.0.0-teleport.3 h1:Eg/j3jiNUZ558KDXOqzF682EF
github.com/gravitational/redis/v9 v9.0.0-teleport.3/go.mod h1:8et+z03j0l8N+DvsVnclzjf3Dl/pFHgRk+2Ct1qw66A=
github.com/gravitational/roundtrip v1.0.2 h1:eOCY0NEKKaB0ksJmvhO6lPMFz1pIIef+vyPBTBROQ5c=
github.com/gravitational/roundtrip v1.0.2/go.mod h1:fuI1booM2hLRA/B/m5MRAPOU6mBZNYcNycono2UuTw0=
github.com/gravitational/sftp v1.13.6-0.20230328150159-dfe4e0d94419 h1:520SBVjHrTsfTPs7Udp+aXJV4WXOlKexKoIyiN0BJys=
github.com/gravitational/sftp v1.13.6-0.20230328150159-dfe4e0d94419/go.mod h1:wHDZ0IZX6JcBYRK1TH9bcVq8G7TLpVHYIGJRFnmPfxg=
github.com/gravitational/trace v1.2.1 h1:Iaf43aqbKV5H8bdiRs1qByjEHgAfADJ0lt0JwRyu+q8=
github.com/gravitational/trace v1.2.1/go.mod h1:n0ijrq6psJY0sOI/NzLp+xdd8xl79jjwzVOFHDY6+kQ=
github.com/gravitational/ttlmap v0.0.0-20171116003245-91fd36b9004c h1:C2iWDiod8vQ3YnOiCdMP9qYeg2UifQ8KSk36r0NswSE=
Expand Down Expand Up @@ -1043,6 +1041,8 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
github.com/pkg/sftp v1.13.5 h1:a3RLUqkyjYRtBTZJZ1VRrKbN3zhuPLlUc3sphVz81go=
github.com/pkg/sftp v1.13.5/go.mod h1:wHDZ0IZX6JcBYRK1TH9bcVq8G7TLpVHYIGJRFnmPfxg=
github.com/pkg/xattr v0.4.9 h1:5883YPCtkSd8LFbs13nXplj9g9tlrwoJRjgpgMu1/fE=
github.com/pkg/xattr v0.4.9/go.mod h1:di8WF84zAKk8jzR1UBTEWh9AUlIZZ7M/JNt8e9B6ktU=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
77 changes: 75 additions & 2 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7313,9 +7313,12 @@ func testSFTP(t *testing.T, suite *integrationTestSuite) {
require.NoError(t, testFile.Close())
})

_, err = testFile.WriteString("This is test data.")
contents := []byte("This is test data.")
_, err = testFile.Write(contents)
require.NoError(t, err)
require.NoError(t, testFile.Sync())
_, err = testFile.Seek(0, io.SeekStart)
require.NoError(t, err)

// Test stat'ing a file.
t.Run("stat", func(t *testing.T) {
Expand All @@ -7341,6 +7344,12 @@ func testSFTP(t *testing.T, suite *integrationTestSuite) {

_, err = io.Copy(downloadFile, remoteDownloadFile)
require.NoError(t, err)

_, err = downloadFile.Seek(0, io.SeekStart)
require.NoError(t, err)
data, err := io.ReadAll(downloadFile)
require.NoError(t, err)
require.Equal(t, contents, data)
})

// Test uploading a file.
Expand All @@ -7354,11 +7363,75 @@ func testSFTP(t *testing.T, suite *integrationTestSuite) {

_, err = io.Copy(remoteUploadFile, testFile)
require.NoError(t, err)

_, err = remoteUploadFile.Seek(0, io.SeekStart)
require.NoError(t, err)
data, err := io.ReadAll(remoteUploadFile)
require.NoError(t, err)
require.Equal(t, contents, data)
})

// Test changing file permissions.
t.Run("chmod", func(t *testing.T) {
err = sftpClient.Chmod(testFilePath, 0o777)
err := sftpClient.Chmod(testFilePath, 0o777)
require.NoError(t, err)

fi, err := os.Stat(testFilePath)
require.NoError(t, err)
require.Equal(t, fs.FileMode(0o777), fi.Mode().Perm())
})

// Test operations on a directory.
t.Run("mkdir", func(t *testing.T) {
dirPath := filepath.Join(tempDir, "dir")
require.NoError(t, sftpClient.Mkdir(dirPath))

err := sftpClient.Chmod(dirPath, 0o777)
require.NoError(t, err)

fi, err := os.Stat(dirPath)
require.NoError(t, err)
require.Equal(t, fs.FileMode(0o777), fi.Mode().Perm())

f, err := sftpClient.Create(filepath.Join(dirPath, "file"))
require.NoError(t, err)
require.NoError(t, f.Close())

fileInfos, err := sftpClient.ReadDir(dirPath)
require.NoError(t, err)
require.Len(t, fileInfos, 1)
require.Equal(t, "file", fileInfos[0].Name())
})

// Test renaming a file.
t.Run("rename", func(t *testing.T) {
path := filepath.Join(tempDir, "to-be-renamed")
f, err := sftpClient.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())

newPath := path + "-done"
err = sftpClient.Rename(path, newPath)
require.NoError(t, err)

_, err = sftpClient.Stat(path)
require.ErrorIs(t, err, os.ErrNotExist)
_, err = sftpClient.Stat(newPath)
require.NoError(t, err)
})

// Test removing a file.
t.Run("remove", func(t *testing.T) {
path := filepath.Join(tempDir, "to-be-removed")
f, err := sftpClient.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())

err = sftpClient.Remove(path)
require.NoError(t, err)

_, err = sftpClient.Stat(path)
require.ErrorIs(t, err, os.ErrNotExist)
})

// Ensure SFTP audit events are present.
Expand Down
9 changes: 9 additions & 0 deletions lib/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,3 +871,12 @@ const (
// AzureInviteTokenName is the name of the default token to use
// when templating the script to be executed.
const AzureInviteTokenName = "azure-discovery-token"

const (
// FilePermissions are safe default permissions to use when
// creating files.
FilePermissions = 0o644
// DirectoryPermissions are safe default permissions to use when
// creating directories.
DirectoryPermissions = 0o755
)
2 changes: 2 additions & 0 deletions lib/events/codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ const (
SFTPReadlinkFailureCode = "TS017E"
SFTPSymlinkCode = "TS018I"
SFTPSymlinkFailureCode = "TS018E"
SFTPLinkCode = "TS019I"
SFTPLinkFailureCode = "TS019E"

// SessionCommandCode is a session command code.
SessionCommandCode = "T4000I"
Expand Down
10 changes: 6 additions & 4 deletions lib/sshutils/sftp/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"time"

"github.com/gravitational/trace"

"github.com/gravitational/teleport/lib/defaults"
)

// localFS provides API for accessing the files on
Expand Down Expand Up @@ -81,25 +83,25 @@ func (l localFS) Open(ctx context.Context, path string) (fs.File, error) {
return &fileWrapper{file: f}, nil
}

func (l localFS) Create(ctx context.Context, path string, mode os.FileMode) (io.WriteCloser, error) {
func (l localFS) Create(ctx context.Context, path string) (io.WriteCloser, error) {
if err := ctx.Err(); err != nil {
return nil, err
}

f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, defaults.FilePermissions)
if err != nil {
return nil, trace.Wrap(err)
}

return f, nil
}

func (l localFS) Mkdir(ctx context.Context, path string, mode os.FileMode) error {
func (l localFS) Mkdir(ctx context.Context, path string) error {
if err := ctx.Err(); err != nil {
return err
}

err := os.MkdirAll(path, mode)
err := os.MkdirAll(path, defaults.DirectoryPermissions)
if err != nil && !os.IsExist(err) {
return trace.ConvertSystemError(err)
}
Expand Down
8 changes: 4 additions & 4 deletions lib/sshutils/sftp/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,25 @@ func (r *remoteFS) Open(ctx context.Context, path string) (fs.File, error) {
return f, nil
}

func (r *remoteFS) Create(ctx context.Context, path string, mode os.FileMode) (io.WriteCloser, error) {
func (r *remoteFS) Create(ctx context.Context, path string) (io.WriteCloser, error) {
if err := ctx.Err(); err != nil {
return nil, err
}

f, err := r.c.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
f, err := r.c.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
if err != nil {
return nil, trace.Wrap(err)
}

return f, nil
}

func (r *remoteFS) Mkdir(ctx context.Context, path string, mode os.FileMode) error {
func (r *remoteFS) Mkdir(ctx context.Context, path string) error {
if err := ctx.Err(); err != nil {
return err
}

err := r.c.MkdirAll(path, mode)
err := r.c.MkdirAll(path)
if err != nil {
return trace.Wrap(err)
}
Expand Down
Loading