From 359981ca267128a4441761eff85f15a9cf0f64e3 Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Wed, 31 Jul 2024 16:49:40 +0200 Subject: [PATCH] Update golangci lint and clean up code (#586) * Update golangci-lint in CI * Remove deprecated linters * Disable fatcontext * Remove copied function, because CGO is no longer used in the SDK --- .github/workflows/test.yaml | 2 +- .golangci.yaml | 13 ++----------- network/proxy.go | 5 +++-- network/utils.go | 21 --------------------- network/utils_test.go | 26 -------------------------- 5 files changed, 6 insertions(+), 61 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 5c53e4bc..687db0b0 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -69,7 +69,7 @@ jobs: - name: Lint code with golangci-lint 🚨 uses: golangci/golangci-lint-action@v4 with: - version: "v1.57" + version: "v1.59.1" skip-pkg-cache: true install-mode: "goinstall" diff --git a/.golangci.yaml b/.golangci.yaml index b5522d27..28438ba8 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -11,25 +11,16 @@ linters: - nlreturn - testpackage - paralleltest - - exhaustivestruct - exhaustruct - gocognit - gochecknoinits - gocyclo - - maligned - funlen - maintidx - musttag - - nosnakecase - - goerr113 - - ifshort - - deadcode - - interfacer - - scopelint - - structcheck - - varcheck - - golint + - err113 - testifylint + - fatcontext linters-settings: depguard: rules: diff --git a/network/proxy.go b/network/proxy.go index 2e4af39b..06689a6d 100644 --- a/network/proxy.go +++ b/network/proxy.go @@ -10,6 +10,7 @@ import ( "time" sdkAct "github.com/gatewayd-io/gatewayd-plugin-sdk/act" + "github.com/gatewayd-io/gatewayd-plugin-sdk/databases/postgres" v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1" "github.com/gatewayd-io/gatewayd/act" "github.com/gatewayd-io/gatewayd/config" @@ -315,7 +316,7 @@ func (pr *Proxy) PassThroughToServer(conn *ConnWrapper, stack *Stack) *gerr.Gate // Check if the client sent a SSL request and the server supports SSL. //nolint:nestif - if conn.IsTLSEnabled() && IsPostgresSSLRequest(request) { + if conn.IsTLSEnabled() && postgres.IsPostgresSSLRequest(request) { // Perform TLS handshake. if err := conn.UpgradeToTLS(func(net.Conn) { // Acknowledge the SSL request: @@ -361,7 +362,7 @@ func (pr *Proxy) PassThroughToServer(conn *ConnWrapper, stack *Stack) *gerr.Gate // This return causes the client to start sending // StartupMessage over the TLS connection. return nil - } else if !conn.IsTLSEnabled() && IsPostgresSSLRequest(request) { + } else if !conn.IsTLSEnabled() && postgres.IsPostgresSSLRequest(request) { // Client sent a SSL request, but the server does not support SSL. pr.Logger.Warn().Fields( diff --git a/network/utils.go b/network/utils.go index 29824766..0d7e0bab 100644 --- a/network/utils.go +++ b/network/utils.go @@ -2,7 +2,6 @@ package network import ( "crypto/sha256" - "encoding/binary" "encoding/hex" "fmt" "net" @@ -122,23 +121,3 @@ func RemoteAddr(conn net.Conn) string { } return "" } - -// IsPostgresSSLRequest returns true if the message is a SSL request. -// This is copied from gatewayd-plugin-sdk to avoid the dependency on CGO. -// -//nolint:gomnd -func IsPostgresSSLRequest(data []byte) bool { - if len(data) < 8 { - return false - } - - if binary.BigEndian.Uint32(data[0:4]) != 8 { - return false - } - - if binary.BigEndian.Uint32(data[4:8]) != 80877103 { - return false - } - - return true -} diff --git a/network/utils_test.go b/network/utils_test.go index 9a13324e..1238cbea 100644 --- a/network/utils_test.go +++ b/network/utils_test.go @@ -45,26 +45,6 @@ func TestResolve(t *testing.T) { assert.Equal(t, "127.0.0.1:53", address) } -// TestIsPostgresSSLRequest tests the IsPostgresSSLRequest function. -// It checks the entire SSL request including the length. -func TestIsPostgresSSLRequest(t *testing.T) { - // Test a valid SSL request. - sslRequest := []byte{0x00, 0x00, 0x00, 0x8, 0x04, 0xd2, 0x16, 0x2f} - assert.True(t, IsPostgresSSLRequest(sslRequest)) - - // Test an invalid SSL request. - invalidSSLRequest := []byte{0x00, 0x00, 0x00, 0x9, 0x04, 0xd2, 0x16, 0x2e} - assert.False(t, IsPostgresSSLRequest(invalidSSLRequest)) - - // Test an invalid SSL request. - invalidSSLRequest = []byte{0x04, 0xd2, 0x16} - assert.False(t, IsPostgresSSLRequest(invalidSSLRequest)) - - // Test an invalid SSL request. - invalidSSLRequest = []byte{0x00, 0x00, 0x00, 0x00, 0x04, 0xd2, 0x16, 0x2f, 0x00} - assert.False(t, IsPostgresSSLRequest(invalidSSLRequest)) -} - var seedValues = []int{1000, 10000, 100000, 1000000, 10000000} func BenchmarkGetID(b *testing.B) { @@ -192,9 +172,3 @@ func BenchmarkExtractFieldValue(b *testing.B) { ) } } - -func BenchmarkIsPostgresSSLRequest(b *testing.B) { - for i := 0; i < b.N; i++ { - IsPostgresSSLRequest([]byte{0x00, 0x00, 0x00, 0x8, 0x04, 0xd2, 0x16, 0x2f}) - } -}