Skip to content

Commit

Permalink
Update golangci lint and clean up code (gatewayd-io#586)
Browse files Browse the repository at this point in the history
* Update golangci-lint in CI
* Remove deprecated linters
* Disable fatcontext
* Remove copied function, because CGO is no longer used in the SDK
  • Loading branch information
mostafa authored Jul 31, 2024
1 parent 6951811 commit 359981c
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
13 changes: 2 additions & 11 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions network/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down
21 changes: 0 additions & 21 deletions network/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package network

import (
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"fmt"
"net"
Expand Down Expand Up @@ -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
}
26 changes: 0 additions & 26 deletions network/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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})
}
}

0 comments on commit 359981c

Please sign in to comment.