From 4ecc60f9f4fef3307af4a0c3a71e4388e2da08a8 Mon Sep 17 00:00:00 2001 From: Mustafa Arslan Date: Wed, 14 Aug 2024 22:02:41 +0300 Subject: [PATCH] linter fixes --- bindings/sftp/sftp.go | 17 ++++++++++------- bindings/sftp/sftp_test.go | 9 +++++---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/bindings/sftp/sftp.go b/bindings/sftp/sftp.go index 3539c62863..960294a44a 100644 --- a/bindings/sftp/sftp.go +++ b/bindings/sftp/sftp.go @@ -3,6 +3,7 @@ package sftp import ( "context" "encoding/json" + "errors" "fmt" "io" "reflect" @@ -65,6 +66,7 @@ func (sftp *Sftp) Init(_ context.Context, metadata bindings.Metadata) error { var hostKeyCallback ssh.HostKeyCallback if m.InsecureIgnoreHostKey { + //nolint:gosec hostKeyCallback = ssh.InsecureIgnoreHostKey() } else if len(m.KnownHostsFile) > 0 { hostKeyCallback, err = knownhosts.New(m.KnownHostsFile) @@ -72,7 +74,8 @@ func (sftp *Sftp) Init(_ context.Context, metadata bindings.Metadata) error { return fmt.Errorf("sftp binding error: read known host file error: %w", err) } } else if len(m.HostPublicKey) > 0 { - hostPublicKey, _, _, _, err := ssh.ParseAuthorizedKey(m.HostPublicKey) + var hostPublicKey ssh.PublicKey + hostPublicKey, _, _, _, err = ssh.ParseAuthorizedKey(m.HostPublicKey) if err != nil { return fmt.Errorf("sftp binding error: parse host public key error: %w", err) } @@ -81,7 +84,7 @@ func (sftp *Sftp) Init(_ context.Context, metadata bindings.Metadata) error { } if hostKeyCallback == nil { - return fmt.Errorf("sftp binding error: no host validation method provided") + return errors.New("sftp binding error: no host validation method provided") } if len(m.PrivateKey) > 0 { @@ -206,13 +209,13 @@ func (sftp *Sftp) list(_ context.Context, req *bindings.InvokeRequest) (*binding return nil, fmt.Errorf("sftp binding error: error read dir %s: %w", path, err) } - var resp []listResponse + resp := make([]listResponse, len(files)) - for _, file := range files { - resp = append(resp, listResponse{ + for i, file := range files { + resp[i] = listResponse{ FileName: file.Name(), IsDirectory: file.IsDir(), - }) + } } jsonResponse, err := json.Marshal(resp) @@ -297,7 +300,7 @@ func (metadata sftpMetadata) getPath(requestMetadata map[string]string) (path st } if path == "" { - err = fmt.Errorf("required metadata rootPath or fileName missing") + err = errors.New("required metadata rootPath or fileName missing") } return diff --git a/bindings/sftp/sftp_test.go b/bindings/sftp/sftp_test.go index 41bd7a020f..69f7e5bb82 100644 --- a/bindings/sftp/sftp_test.go +++ b/bindings/sftp/sftp_test.go @@ -25,8 +25,8 @@ func TestParseMeta(t *testing.T) { sftp := Sftp{} meta, err := sftp.parseMetadata(m) - privateKeyBytes := []uint8([]byte{0x63, 0x48, 0x4a, 0x70, 0x64, 0x6d, 0x46, 0x30, 0x5a, 0x55, 0x74, 0x6c, 0x65, 0x51, 0x3d, 0x3d}) - hostPublicKeyBytes := []uint8([]byte{0x61, 0x47, 0x39, 0x7a, 0x64, 0x46, 0x42, 0x31, 0x59, 0x6d, 0x78, 0x70, 0x59, 0x30, 0x74, 0x6c, 0x65, 0x51, 0x3d, 0x3d}) + privateKeyBytes := []byte{0x63, 0x48, 0x4a, 0x70, 0x64, 0x6d, 0x46, 0x30, 0x5a, 0x55, 0x74, 0x6c, 0x65, 0x51, 0x3d, 0x3d} + hostPublicKeyBytes := []byte{0x61, 0x47, 0x39, 0x7a, 0x64, 0x46, 0x42, 0x31, 0x59, 0x6d, 0x78, 0x70, 0x59, 0x30, 0x74, 0x6c, 0x65, 0x51, 0x3d, 0x3d} require.NoError(t, err) assert.Equal(t, "path", meta.RootPath) @@ -39,6 +39,7 @@ func TestParseMeta(t *testing.T) { assert.True(t, meta.InsecureIgnoreHostKey) }) } + func TestMergeWithRequestMetadata(t *testing.T) { t.Run("Has merged metadata", func(t *testing.T) { m := bindings.Metadata{} @@ -69,8 +70,8 @@ func TestMergeWithRequestMetadata(t *testing.T) { mergedMeta, err := meta.mergeWithRequestMetadata(&request) - privateKeyBytes := []uint8([]byte{0x63, 0x48, 0x4a, 0x70, 0x64, 0x6d, 0x46, 0x30, 0x5a, 0x55, 0x74, 0x6c, 0x65, 0x51, 0x3d, 0x3d}) - hostPublicKeyBytes := []uint8([]byte{0x61, 0x47, 0x39, 0x7a, 0x64, 0x46, 0x42, 0x31, 0x59, 0x6d, 0x78, 0x70, 0x59, 0x30, 0x74, 0x6c, 0x65, 0x51, 0x3d, 0x3d}) + privateKeyBytes := []byte{0x63, 0x48, 0x4a, 0x70, 0x64, 0x6d, 0x46, 0x30, 0x5a, 0x55, 0x74, 0x6c, 0x65, 0x51, 0x3d, 0x3d} + hostPublicKeyBytes := []byte{0x61, 0x47, 0x39, 0x7a, 0x64, 0x46, 0x42, 0x31, 0x59, 0x6d, 0x78, 0x70, 0x59, 0x30, 0x74, 0x6c, 0x65, 0x51, 0x3d, 0x3d} require.NoError(t, err) assert.Equal(t, "changedpath", mergedMeta.RootPath)