Skip to content

Commit

Permalink
linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arslanmusta committed Aug 14, 2024
1 parent a0c4015 commit 4ecc60f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
17 changes: 10 additions & 7 deletions bindings/sftp/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sftp
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
Expand Down Expand Up @@ -65,14 +66,16 @@ 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)
if err != nil {
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)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions bindings/sftp/sftp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4ecc60f

Please sign in to comment.