Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TT-11755] fix lint errors (staticcheck.bug.err) #6196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jeffy-mathew
Copy link
Contributor

@jeffy-mathew jeffy-mathew commented Mar 28, 2024

User description

Description

Related Issue

Motivation and Context

How This Has Been Tested

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring or add test (improvements in base code or adds test coverage to functionality)

Checklist

  • I ensured that the documentation is up to date
  • I explained why this PR updates go.mod in detail with reasoning why it's required
  • I would like a code coverage CI quality gate exception and have explained why

Type

enhancement


Changes walkthrough


PR-Agent usage:
Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

Relevant files
Enhancement
60 files
notifications.go
Replace deprecated ioutil with io package in notifications.go

apidef/notifications.go

  • Replace ioutil with io for ReadAll method.
  • Minor formatting adjustments.
  • +2/-2     
    root_test.go
    Remove unnecessary break statement in root_test.go             

    apidef/oas/root_test.go

    • Remove unnecessary break statement in a switch case.
    +0/-1     
    manager.go
    Replace deprecated ioutil with os package in manager.go   

    certs/manager.go

  • Replace ioutil with os for ReadFile method.
  • Minor formatting adjustments.
  • +4/-4     
    manager_test.go
    Use os package for file operations in manager_test.go       

    certs/manager_test.go

  • Replace ioutil with os for temporary directory creation and file
    writing.
  • +2/-3     
    test_goplugin.go
    Replace deprecated ioutil with io package in test_goplugin.go

    ci/tests/plugin-compiler/testdata/basic-plugin/test_goplugin.go

    • Replace ioutil with io for NopCloser method.
    +2/-1     
    plugin.go
    Replace deprecated ioutil with io package in plugin.go     

    ci/tests/plugin-compiler/testdata/complex-plugin/plugin/plugin.go

    • Replace ioutil with io for NopCloser method.
    +1/-1     
    bundler.go
    Use os package for file operations in bundler.go                 

    cli/bundler/bundler.go

    • Replace ioutil with os for file operations.
    +4/-5     
    bundler_test.go
    Use os package and improve error checking in bundler_test.go

    cli/bundler/bundler_test.go

  • Replace ioutil with os for file writing and temporary directory
    creation.
  • Use assert.NoError for error checking.
  • +5/-3     
    cli.go
    Replace deprecated ioutil with os package in cli.go           

    cli/cli.go

    • Replace ioutil with os for reading files.
    +1/-2     
    linter_test.go
    Use os package for file operations in linter_test.go         

    cli/linter/linter_test.go

    • Replace ioutil with os for file operations.
    +2/-4     
    config.go
    Replace deprecated ioutil with os package in config.go     

    config/config.go

  • Replace ioutil with os for file writing.
  • Minor documentation update.
  • +2/-3     
    config_test.go
    Use os package and improve error checking in config_test.go

    config/config_test.go

  • Replace ioutil with os for file operations.
  • Use assert.NoError for error checking.
  • +23/-15 
    coprocess_test.go
    Update session creation method in coprocess_test.go           

    coprocess/coprocess_test.go

  • Replace CreateSession with CreateSession and keyID return in tests.
  • +1/-1     
    coprocess_grpc_test.go
    Update session creation and replace deprecated ioutil in
    coprocess_grpc_test.go

    coprocess/grpc/coprocess_grpc_test.go

  • Replace ioutil with io for reading response body.
  • Replace CreateSession with CreateSession and keyID return in tests.
  • +5/-4     
    coprocess_python_test.go
    Update session creation method in coprocess_python_test.go

    coprocess/python/coprocess_python_test.go

  • Replace CreateSession with CreateSession and keyID return in tests.
  • +2/-2     
    analytics.go
    Use thread-safe rand instance in analytics.go                       

    gateway/analytics.go

  • Replace mathRand.Seed with new rand instance for thread safety.
  • +2/-2     
    api.go
    Replace deprecated ioutil with io and os packages in api.go

    gateway/api.go

  • Replace ioutil with io for reading and closing request body.
  • Replace ioutil with os for file writing.
  • +9/-10   
    api_definition.go
    Replace deprecated ioutil with io package in api_definition.go

    gateway/api_definition.go

    • Replace ioutil with io for reading response body.
    +2/-3     
    api_definition_test.go
    Use os package for file operations in api_definition_test.go

    gateway/api_definition_test.go

    • Replace ioutil with os for reading files.
    +2/-2     
    api_test.go
    Replace deprecated ioutil with io package in api_test.go 

    gateway/api_test.go

    • Replace ioutil with io for reading response body.
    +3/-4     
    batch_requests.go
    Replace deprecated ioutil with io package in batch_requests.go

    gateway/batch_requests.go

    • Replace ioutil with io for reading response body.
    +2/-2     
    batch_requests_test.go
    Replace deprecated ioutil with io package in batch_requests_test.go

    gateway/batch_requests_test.go

    • Replace ioutil with io for reading response body.
    +2/-2     
    cert.go
    Replace deprecated ioutil with io package in cert.go         

    gateway/cert.go

    • Replace ioutil with io for reading request body.
    +11/-2   
    cert_test.go
    Use os package for file operations in cert_test.go             

    gateway/cert_test.go

  • Replace ioutil with os for temporary directory creation and file
    writing.
  • +5/-6     
    coprocess.go
    Replace deprecated ioutil with io package in coprocess.go

    gateway/coprocess.go

    • Replace ioutil with io for reading request and response body.
    +6/-5     
    coprocess_bundle.go
    Replace deprecated ioutil with io package in coprocess_bundle.go

    gateway/coprocess_bundle.go

    • Replace ioutil with io for reading response body.
    +1/-2     
    coprocess_grpc.go
    Use secure gRPC credentials in coprocess_grpc.go                 

    gateway/coprocess_grpc.go

  • Replace grpc.WithInsecure() with
    grpc.WithTransportCredentials(insecure.NewCredentials()).
  • +2/-1     
    coprocess_id_extractor.go
    Replace deprecated ioutil with io package in coprocess_id_extractor.go

    gateway/coprocess_id_extractor.go

    • Replace ioutil with io for reading request body.
    +1/-1     
    coprocess_lua.go
    Use os package for file operations in coprocess_lua.go     

    gateway/coprocess_lua.go

    • Replace ioutil with os for reading files.
    +3/-3     
    coprocess_python.go
    Update protobuf import path in coprocess_python.go             

    gateway/coprocess_python.go

  • Replace github.com/golang/protobuf/proto with
    google.golang.org/protobuf/proto.
  • +1/-1     
    event_handler_webhooks.go
    Replace deprecated ioutil with io package in event_handler_webhooks.go

    gateway/event_handler_webhooks.go

    • Replace ioutil with io for reading response body.
    +1/-1     
    gateway_test.go
    Replace deprecated ioutil with io package in gateway_test.go

    gateway/gateway_test.go

    • Replace ioutil with io for reading response body.
    +1/-1     
    grpc_streaming_server_test.go
    Update protobuf import path in grpc_streaming_server_test.go

    gateway/grpc_streaming_server_test.go

  • Replace github.com/golang/protobuf/proto with
    google.golang.org/protobuf/proto.
  • +1/-1     
    grpc_test.go
    Update gRPC credentials and replace deprecated ioutil in grpc_test.go

    gateway/grpc_test.go

  • Replace ioutil with io for reading response body.
  • Replace grpc.WithInsecure() with
    grpc.WithTransportCredentials(insecure.NewCredentials()).
  • +7/-6     
    handler_error.go
    Replace deprecated ioutil with io package in handler_error.go

    gateway/handler_error.go

    • Replace ioutil with io for creating NopCloser.
    +1/-1     
    handler_error_test.go
    Use os package for file operations in handler_error_test.go

    gateway/handler_error_test.go

    • Replace ioutil with os for file writing.
    +1/-2     
    handler_success.go
    Replace deprecated ioutil with io package in handler_success.go

    gateway/handler_success.go

    • Replace ioutil with io for creating NopCloser.
    +1/-1     
    host_checker.go
    Use thread-safe rand instance in host_checker.go                 

    gateway/host_checker.go

  • Replace mathRand.Seed with new rand instance for thread safety.
  • +2/-3     
    middleware.go
    Replace deprecated ioutil with io package in middleware.go

    gateway/middleware.go

    • Replace ioutil with io for creating NopCloser.
    +2/-2     
    middleware_test.go
    Replace deprecated ioutil with io package in middleware_test.go

    gateway/middleware_test.go

    • Replace ioutil with io for reading response body.
    +1/-1     
    mw_basic_auth.go
    Replace deprecated ioutil with io package in mw_basic_auth.go

    gateway/mw_basic_auth.go

    • Replace ioutil with io for creating NopCloser.
    +2/-2     
    mw_go_plugin.go
    Replace deprecated ioutil with io package in mw_go_plugin.go

    gateway/mw_go_plugin.go

    • Replace ioutil with io for creating NopCloser.
    +1/-1     
    mw_js_plugin.go
    Replace deprecated ioutil with io package in mw_js_plugin.go

    gateway/mw_js_plugin.go

  • Replace ioutil with io for reading request body and creating
    NopCloser.
  • +5/-5     
    mw_js_plugin_test.go
    Replace deprecated ioutil with io package in mw_js_plugin_test.go

    gateway/mw_js_plugin_test.go

    • Replace ioutil with io for reading request body.
    +5/-5     
    mw_jwt.go
    Improve string comparison in mw_jwt.go                                     

    gateway/mw_jwt.go

    • Use strings.EqualFold for case-insensitive string comparison.
    +1/-1     
    mw_redis_cache.go
    Replace deprecated ioutil with io package in mw_redis_cache.go

    gateway/mw_redis_cache.go

    • Replace ioutil with io for reading request body.
    +1/-2     
    mw_transform.go
    Replace deprecated ioutil with io package in mw_transform.go

    gateway/mw_transform.go

    • Replace ioutil with io for reading request body.
    +1/-1     
    mw_transform_jq.go
    Replace deprecated ioutil with io package in mw_transform_jq.go

    gateway/mw_transform_jq.go

    • Replace ioutil with io for creating NopCloser.
    +1/-1     
    mw_transform_test.go
    Replace deprecated ioutil with io package in mw_transform_test.go

    gateway/mw_transform_test.go

    • Replace ioutil with io for reading request body.
    +5/-5     
    mw_url_rewrite.go
    Improve string comparison in mw_url_rewrite.go                     

    gateway/mw_url_rewrite.go

    • Use strings.EqualFold for case-insensitive string comparison.
    +1/-1     
    mw_validate_json.go
    Replace deprecated ioutil with io package in mw_validate_json.go

    gateway/mw_validate_json.go

    • Replace ioutil with io for reading request body.
    +1/-1     
    mw_virtual_endpoint.go
    Replace deprecated ioutil with io package in mw_virtual_endpoint.go

    gateway/mw_virtual_endpoint.go

  • Replace ioutil with io for reading request body and creating
    NopCloser.
  • +2/-2     
    policy.go
    Replace deprecated ioutil with io package in policy.go     

    gateway/policy.go

    • Replace ioutil with io for reading response body.
    +1/-1     
    proxy_muxer_test.go
    Replace deprecated ioutil with io package in proxy_muxer_test.go

    gateway/proxy_muxer_test.go

    • Replace ioutil with io for reading response body.
    +1/-1     
    redis_signal_handle_config.go
    Use os package for file operations in redis_signal_handle_config.go

    gateway/redis_signal_handle_config.go

    • Replace ioutil with os for file writing.
    +2/-3     
    reverse_proxy.go
    Replace deprecated ioutil and improve string comparison in
    reverse_proxy.go

    gateway/reverse_proxy.go

  • Replace ioutil with io for creating NopCloser.
  • Use strings.EqualFold for case-insensitive string comparison.
  • +5/-6     
    reverse_proxy_test.go
    Replace deprecated ioutil with io package in reverse_proxy_test.go

    gateway/reverse_proxy_test.go

    • Replace ioutil with io for creating NopCloser.
    +14/-13 
    server.go
    Replace deprecated ioutil with io package in server.go     

    gateway/server.go

    • Replace ioutil with io for discarding log output.
    +6/-6     
    service_discovery.go
    Replace deprecated ioutil with io package in service_discovery.go

    gateway/service_discovery.go

    • Replace ioutil with io for reading response body.
    +1/-1     
    testutil.go
    Use os package for file operations in testutil.go               

    gateway/testutil.go

  • Replace ioutil with os for temporary directory creation and file
    writing.
  • +7/-8     

    @jeffy-mathew jeffy-mathew requested a review from a team as a code owner March 28, 2024 16:36
    Copy link

    API Changes

    --- prev.txt	2024-03-28 16:37:10.004026636 +0000
    +++ current.txt	2024-03-28 16:37:07.176011555 +0000
    @@ -4692,7 +4692,7 @@
     
     func WriteConf(path string, conf *Config) error
     func WriteDefault(path string, conf *Config) error
    -    writeDefault will set conf to the default config and write it to disk in
    +    WriteDefault will set conf to the default config and write it to disk in
         path, if the path is non-empty.
     
     

    Copy link

    💥 CI tests failed 🙈

    git-state

    diff --git a/ci/tests/plugin-compiler/testdata/basic-plugin/test_goplugin.go b/ci/tests/plugin-compiler/testdata/basic-plugin/test_goplugin.go
    index 5e401ff..fc0091d 100644
    --- a/ci/tests/plugin-compiler/testdata/basic-plugin/test_goplugin.go
    +++ b/ci/tests/plugin-compiler/testdata/basic-plugin/test_goplugin.go
    @@ -6,7 +6,6 @@ import (
     	"encoding/base64"
     	"encoding/json"
     	"io"
    -	"io/ioutil"
     	"net/http"
     
     	"github.com/buger/jsonparser"
    diff --git a/ci/tests/plugin-compiler/testdata/complex-plugin/plugin/plugin.go b/ci/tests/plugin-compiler/testdata/complex-plugin/plugin/plugin.go
    index 12ec662..4492b65 100644
    --- a/ci/tests/plugin-compiler/testdata/complex-plugin/plugin/plugin.go
    +++ b/ci/tests/plugin-compiler/testdata/complex-plugin/plugin/plugin.go
    @@ -3,7 +3,7 @@ package plugin
     import (
     	"bytes"
     	"encoding/json"
    -	"io/ioutil"
    +	"io"
     	"net/http"
     
     	"github.com/TykTechnologies/tyk/ctx"
    diff --git a/cli/bundler/bundler_test.go b/cli/bundler/bundler_test.go
    index 8d9b2c8..da075b6 100644
    --- a/cli/bundler/bundler_test.go
    +++ b/cli/bundler/bundler_test.go
    @@ -8,9 +8,10 @@ import (
     	"strings"
     	"testing"
     
    -	"github.com/TykTechnologies/tyk/apidef"
     	"github.com/stretchr/testify/assert"
     
    +	"github.com/TykTechnologies/tyk/apidef"
    +
     	kingpin "github.com/alecthomas/kingpin/v2"
     )
     
    diff --git a/gateway/coprocess.go b/gateway/coprocess.go
    index 6cc7445..4645a9f 100644
    --- a/gateway/coprocess.go
    +++ b/gateway/coprocess.go
    @@ -19,7 +19,6 @@ import (
     
     	"errors"
     	"fmt"
    -	"io/ioutil"
     	"net/http"
     )
     
    diff --git a/gateway/coprocess_id_extractor.go b/gateway/coprocess_id_extractor.go
    index 19c43c3..96900e0 100644
    --- a/gateway/coprocess_id_extractor.go
    +++ b/gateway/coprocess_id_extractor.go
    @@ -4,7 +4,7 @@ import (
     	"crypto/md5"
     	"errors"
     	"fmt"
    -	"io/ioutil"
    +	"io"
     	"net/http"
     	"strings"
     	"time"
    diff --git a/gateway/coprocess_lua.go b/gateway/coprocess_lua.go
    index 07fa3fd..f05d799 100644
    --- a/gateway/coprocess_lua.go
    +++ b/gateway/coprocess_lua.go
    @@ -70,6 +70,7 @@ import (
     	"encoding/json"
     	"errors"
     	"io/ioutil"
    +	"os"
     	"path/filepath"
     	"unsafe"
     
    diff --git a/gateway/event_handler_webhooks.go b/gateway/event_handler_webhooks.go
    index 0cccefc..bb53128 100644
    --- a/gateway/event_handler_webhooks.go
    +++ b/gateway/event_handler_webhooks.go
    @@ -6,7 +6,7 @@ import (
     	"encoding/hex"
     	"encoding/json"
     	htmlTemplate "html/template"
    -	"io/ioutil"
    +	"io"
     	"net/http"
     	"net/url"
     	"path/filepath"
    diff --git a/gateway/gateway_test.go b/gateway/gateway_test.go
    index 5b80e1a..979dc39 100644
    --- a/gateway/gateway_test.go
    +++ b/gateway/gateway_test.go
    @@ -5,7 +5,7 @@ import (
     	"context"
     	"encoding/json"
     	"fmt"
    -	"io/ioutil"
    +	"io"
     	"net"
     	"net/http"
     	"net/http/httptest"
    diff --git a/gateway/grpc_test.go b/gateway/grpc_test.go
    index 2fa452d..64c1700 100644
    --- a/gateway/grpc_test.go
    +++ b/gateway/grpc_test.go
    @@ -16,9 +16,10 @@ import (
     	"testing"
     	"time"
     
    -	"github.com/TykTechnologies/tyk/certs"
     	"google.golang.org/grpc/credentials/insecure"
     
    +	"github.com/TykTechnologies/tyk/certs"
    +
     	"github.com/TykTechnologies/tyk/config"
     
     	"google.golang.org/grpc/metadata"
    diff --git a/gateway/handler_error.go b/gateway/handler_error.go
    index 26d092a..0b3382d 100644
    --- a/gateway/handler_error.go
    +++ b/gateway/handler_error.go
    @@ -6,7 +6,6 @@ import (
     	"errors"
     	htmlTemplate "html/template"
     	"io"
    -	"io/ioutil"
     	"net/http"
     	"strconv"
     	"strings"
    diff --git a/gateway/handler_success.go b/gateway/handler_success.go
    index ab85108..abcba2c 100644
    --- a/gateway/handler_success.go
    +++ b/gateway/handler_success.go
    @@ -4,7 +4,6 @@ import (
     	"bytes"
     	"encoding/base64"
     	"io"
    -	"io/ioutil"
     	"net/http"
     	"strconv"
     	"strings"
    diff --git a/gateway/middleware.go b/gateway/middleware.go
    index 21c7b50..16001f6 100644
    --- a/gateway/middleware.go
    +++ b/gateway/middleware.go
    @@ -7,7 +7,6 @@ import (
     	"errors"
     	"fmt"
     	"io"
    -	"io/ioutil"
     	"net/http"
     	"strconv"
     	"time"
    diff --git a/gateway/middleware_test.go b/gateway/middleware_test.go
    index 26ca8d8..9457fda 100644
    --- a/gateway/middleware_test.go
    +++ b/gateway/middleware_test.go
    @@ -3,7 +3,7 @@ package gateway
     import (
     	"encoding/json"
     	"fmt"
    -	"io/ioutil"
    +	"io"
     	"net/http"
     	"testing"
     	"time"
    diff --git a/gateway/mw_basic_auth.go b/gateway/mw_basic_auth.go
    index 781277b..576a70e 100644
    --- a/gateway/mw_basic_auth.go
    +++ b/gateway/mw_basic_auth.go
    @@ -4,7 +4,7 @@ import (
     	"bytes"
     	"encoding/base64"
     	"errors"
    -	"io/ioutil"
    +	"io"
     	"net/http"
     	"strings"
     
    diff --git a/gateway/mw_go_plugin.go b/gateway/mw_go_plugin.go
    index 46ddc0c..f00d358 100644
    --- a/gateway/mw_go_plugin.go
    +++ b/gateway/mw_go_plugin.go
    @@ -5,7 +5,7 @@ import (
     	"context"
     	"errors"
     	"fmt"
    -	"io/ioutil"
    +	"io"
     	"net/http"
     	"time"
     
    diff --git a/gateway/mw_transform.go b/gateway/mw_transform.go
    index f0e81d2..61ad61b 100644
    --- a/gateway/mw_transform.go
    +++ b/gateway/mw_transform.go
    @@ -5,7 +5,6 @@ import (
     	"encoding/json"
     	"fmt"
     	"io"
    -	"io/ioutil"
     	"net/http"
     
     	"github.com/clbanning/mxj"
    diff --git a/gateway/mw_transform_jq.go b/gateway/mw_transform_jq.go
    index 21aa23f..640eeab 100644
    --- a/gateway/mw_transform_jq.go
    +++ b/gateway/mw_transform_jq.go
    @@ -8,7 +8,6 @@ import (
     	"encoding/json"
     	"errors"
     	"io"
    -	"io/ioutil"
     	"net/http"
     
     	"github.com/sirupsen/logrus"
    diff --git a/gateway/mw_transform_test.go b/gateway/mw_transform_test.go
    index 9351b78..cc0fe05 100644
    --- a/gateway/mw_transform_test.go
    +++ b/gateway/mw_transform_test.go
    @@ -2,7 +2,7 @@ package gateway
     
     import (
     	"encoding/base64"
    -	"io/ioutil"
    +	"io"
     	"net/http"
     	"strings"
     	"testing"
    diff --git a/gateway/mw_url_rewrite.go b/gateway/mw_url_rewrite.go
    index 9b1e2dc..4080ae1 100644
    --- a/gateway/mw_url_rewrite.go
    +++ b/gateway/mw_url_rewrite.go
    @@ -2,7 +2,7 @@ package gateway
     
     import (
     	"fmt"
    -	"io/ioutil"
    +	"io"
     	"net/http"
     	"net/textproto"
     	"net/url"
    diff --git a/gateway/mw_validate_json.go b/gateway/mw_validate_json.go
    index 5ff72ff..82d041b 100644
    --- a/gateway/mw_validate_json.go
    +++ b/gateway/mw_validate_json.go
    @@ -3,7 +3,7 @@ package gateway
     import (
     	"errors"
     	"fmt"
    -	"io/ioutil"
    +	"io"
     	"net/http"
     
     	"github.com/TykTechnologies/gojsonschema"
    diff --git a/gateway/mw_virtual_endpoint.go b/gateway/mw_virtual_endpoint.go
    index 2bdde8a..f422f79 100644
    --- a/gateway/mw_virtual_endpoint.go
    +++ b/gateway/mw_virtual_endpoint.go
    @@ -7,7 +7,6 @@ import (
     	"errors"
     	"fmt"
     	"io"
    -	"io/ioutil"
     	"net/http"
     	"net/url"
     	"os"
    diff --git a/gateway/policy.go b/gateway/policy.go
    index a8e9211..9a5b8d8 100644
    --- a/gateway/policy.go
    +++ b/gateway/policy.go
    @@ -3,7 +3,7 @@ package gateway
     import (
     	"encoding/json"
     	"errors"
    -	"io/ioutil"
    +	"io"
     	"net/http"
     	"os"
     	"path/filepath"
    diff --git a/gateway/proxy_muxer_test.go b/gateway/proxy_muxer_test.go
    index 74729b5..16d70f0 100644
    --- a/gateway/proxy_muxer_test.go
    +++ b/gateway/proxy_muxer_test.go
    @@ -4,7 +4,6 @@ import (
     	"encoding/json"
     	"fmt"
     	"io"
    -	"io/ioutil"
     	"net"
     	"net/http"
     	"net/http/httptest"
    diff --git a/gateway/res_handler_jq_transform.go b/gateway/res_handler_jq_transform.go
    index 3d31b2a..69d55d8 100644
    --- a/gateway/res_handler_jq_transform.go
    +++ b/gateway/res_handler_jq_transform.go
    @@ -6,7 +6,7 @@ package gateway
     import (
     	"bytes"
     	"encoding/json"
    -	"io/ioutil"
    +	"io"
     	"net/http"
     	"strconv"
     
    diff --git a/gateway/res_handler_transform.go b/gateway/res_handler_transform.go
    index 294bbb1..11485b3 100644
    --- a/gateway/res_handler_transform.go
    +++ b/gateway/res_handler_transform.go
    @@ -6,7 +6,6 @@ import (
     	"compress/gzip"
     	"encoding/json"
     	"io"
    -	"io/ioutil"
     	"net/http"
     	"strconv"
     
    diff --git a/gateway/service_discovery.go b/gateway/service_discovery.go
    index b565aa7..16be34b 100644
    --- a/gateway/service_discovery.go
    +++ b/gateway/service_discovery.go
    @@ -1,7 +1,7 @@
     package gateway
     
     import (
    -	"io/ioutil"
    +	"io"
     	"net/http"
     	"strconv"
     	"strings"
    diff --git a/gateway/tracing_test.go b/gateway/tracing_test.go
    index c695fe8..91dfc74 100644
    --- a/gateway/tracing_test.go
    +++ b/gateway/tracing_test.go
    @@ -1,7 +1,7 @@
     package gateway
     
     import (
    -	"io/ioutil"
    +	"io"
     	"net/http"
     	"testing"
     
    diff --git a/go.mod b/go.mod
    index 1f22b54..a255b10 100644
    --- a/go.mod
    +++ b/go.mod
    @@ -35,7 +35,7 @@ require (
     	github.com/gocraft/health v0.0.0-20170925182251-8675af27fef0
     	github.com/gofrs/uuid v4.4.0+incompatible
     	github.com/golang-jwt/jwt/v4 v4.5.0
    -	github.com/golang/protobuf v1.5.3
    +	github.com/golang/protobuf v1.5.3 // indirect
     	github.com/google/uuid v1.6.0 // indirect
     	github.com/gorilla/mux v1.8.1
     	github.com/gorilla/websocket v1.5.1
    diff --git a/test/goplugins/test_goplugin.go b/test/goplugins/test_goplugin.go
    index 4d55cb1..3ef1074 100644
    --- a/test/goplugins/test_goplugin.go
    +++ b/test/goplugins/test_goplugin.go
    @@ -5,7 +5,7 @@ import (
     	"bytes"
     	"encoding/base64"
     	"encoding/json"
    -	"io/ioutil"
    +	"io"
     	"net/http"
     
     	"github.com/buger/jsonparser"

    Please look at the run or in the Checks tab.

    Copy link

    PR Description updated to latest commit (e137cbb)

    Copy link

    PR Review

    ⏱️ Estimated effort to review [1-5]

    2, because the changes are mostly about replacing deprecated or less efficient functions with their newer counterparts across multiple files. The changes are straightforward and consistent, making the review process relatively easy.

    🧪 Relevant tests

    No

    🔍 Possible issues

    Possible Bug: In the file gateway/api.go, the replacement of ioutil.ReadAll with io.ReadAll and ioutil.NopCloser with io.NopCloser is correct, but it's important to ensure that the behavior of reading the request and response bodies remains unchanged. Specifically, the code should ensure that the request body can be re-read if necessary, as this is a common pattern in middleware that inspect or modify the request.

    🔒 Security concerns

    No

    Code feedback:
    relevant filegateway/api.go
    suggestion      

    Ensure that replacing ioutil.ReadAll with io.ReadAll does not affect the ability to re-read the request body in middleware. Consider testing or adding logic to reset the request body if it needs to be read multiple times. [important]

    relevant linecontents, _ := io.ReadAll(r.Body)

    relevant filegateway/coprocess.go
    suggestion      

    After using io.ReadAll to read the request body, ensure to reset the body with r.Body = io.NopCloser(bytes.NewReader(originalBody)) if there's a need to read the body again later in the request processing pipeline. [important]

    relevant lineminiRequestObject.RawBody, err = io.ReadAll(req.Body)

    relevant filegateway/mw_transform.go
    suggestion      

    After transforming the request body, ensure the Content-Length header is updated to reflect the new body length. This prevents issues with clients or downstream services that rely on the Content-Length header to read the request body. [important]

    relevant linebody, _ := io.ReadAll(r.Body)

    relevant filegateway/reverse_proxy.go
    suggestion      

    When replacing the response body with a new reader after reading it, ensure to also update the Content-Length header if the body size changes. This is crucial for HTTP/1.1 connections where the length of the response is determined by this header. [important]

    relevant lineres.Body = io.NopCloser(&bodyBuffer)


    ✨ Review tool usage guide:

    Overview:
    The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

    The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

    • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
    /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
    
    [pr_reviewer]
    some_config1=...
    some_config2=...
    

    See the review usage page for a comprehensive guide on using this tool.

    Copy link

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Best practice
    Handle the error returned by io.ReadAll to ensure successful reading of the response body.

    Consider handling the error returned by io.ReadAll to ensure that the reading of the
    response body does not encounter any issues. Ignoring this error might lead to unnoticed
    failures in reading the response.

    apidef/notifications.go [82]

     _, err = io.ReadAll(resp.Body)
    +if err != nil {
    +    log.Error("Failed to read response body: ", err)
    +    return
    +}
     
    Check if the certificate file exists before attempting to read it with os.ReadFile.

    When using os.ReadFile to read certificate files, it's important to check if the file
    exists before attempting to read it. This can prevent runtime errors in cases where the
    file might have been deleted or moved.

    certs/manager.go [418]

    +if _, err := os.Stat(id); os.IsNotExist(err) {
    +    c.logger.Warn("Certificate file does not exist:", id)
    +    out = append(out, nil)
    +    continue
    +}
     rawCert, err = os.ReadFile(id)
     
    Use more restrictive file permissions when writing temporary files in tests.

    When writing files in tests, especially for temporary files, it's a good practice to use
    more restrictive file permissions to avoid exposing sensitive data. Consider changing the
    file permissions from 0666 to 0600.

    certs/manager_test.go [127]

    -os.WriteFile(certPath, certPem, 0666)
    +os.WriteFile(certPath, certPem, 0600)
     
    Handle errors when creating new files and writing data to the ZIP archive.

    Ensure error handling is performed after creating new files in the ZIP archive. This
    prevents potential issues where writing to the archive could fail without being noticed.

    cli/bundler/bundler.go [137]

     newManifest, err := zipWriter.Create(defaultManifestPath)
    +if err != nil {
    +    return fmt.Errorf("failed to create new manifest in ZIP: %w", err)
    +}
     _, err = newManifest.Write(manifestData)
    +if err != nil {
    +    return fmt.Errorf("failed to write manifest data to ZIP: %w", err)
    +}
     
    Use more restrictive file permissions when writing configuration files to disk.

    When writing configuration files to disk, it's a good practice to use more restrictive
    file permissions to avoid exposing sensitive configuration details. Consider changing the
    file permissions from 0644 to 0600.

    config/config.go [1216]

    -return os.WriteFile(path, bs, 0644)
    +return os.WriteFile(path, bs, 0600)
     
    Add error handling after reading the request body.

    When reading the request body with io.ReadAll, it's important to handle the error to
    prevent potential issues with empty or malformed request bodies. Add error handling after
    reading the request body.

    gateway/api.go [441]

    -contents, _ := io.ReadAll(r.Body)
    +contents, err := io.ReadAll(r.Body)
    +if err != nil {
    +    log.Error("Failed to read request body: ", err)
    +    // Handle the error appropriately, e.g., return an error response
    +    return
    +}
     
    Check for errors after reading from resp.Body.

    After reading from resp.Body, it's a good practice to check for errors to ensure the read
    operation was successful. This can help in diagnosing issues or preventing processing of
    incomplete data.

    gateway/batch_requests.go [72]

     content, err := io.ReadAll(resp.Body)
    +if err != nil {
    +    log.Error("Failed to read response body: ", err)
    +    // Handle the error appropriately
    +    return BatchReplyUnit{}
    +}
     
    Handle errors when reading the request body in certHandler.

    When reading the request body in the certHandler function, it's crucial to handle any
    potential errors. This ensures that the function can gracefully handle cases where the
    request body is not readable or when an unexpected error occurs during reading.

    gateway/cert.go [524]

     content, err := io.ReadAll(r.Body)
    +if err != nil {
    +    doJSONWrite(w, http.StatusBadRequest, apiError("Failed to read request body"))
    +    return
    +}
     
    Check response status before reading the body in HTTP responses.

    When handling HTTP responses, it's important to check the response status code before
    attempting to read the body. This ensures that the body is only read when the response
    indicates success, preventing unnecessary operations on error responses.

    gateway/api_definition.go [509]

    -body, _ := io.ReadAll(resp.Body)
    +if resp.StatusCode != http.StatusOK {
    +    // Handle non-OK status appropriately
    +    return nil, fmt.Errorf("Unexpected status code: %d", resp.StatusCode)
    +}
    +body, err := io.ReadAll(resp.Body)
    +if err != nil {
    +    // Handle error
    +    return nil, fmt.Errorf("Failed to read response body: %v", err)
    +}
     
    Handle errors returned by os.MkdirTemp and os.WriteFile.

    It's recommended to handle errors returned by os.MkdirTemp and os.WriteFile to prevent
    runtime panics or unexpected behavior in case of failures. Ignoring errors can lead to
    difficult-to-debug issues in production environments.

    gateway/cert_test.go [48-71]

    -dir, _ := os.MkdirTemp("", "certs")
    -err := os.WriteFile(certFilePath, serverCertPem, 0666)
    +dir, err := os.MkdirTemp("", "certs")
    +if err != nil {
    +    t.Fatalf("failed to create temp directory: %v", err)
    +}
    +err = os.WriteFile(certFilePath, serverCertPem, 0666)
    +if err != nil {
    +    t.Fatalf("failed to write server certificate: %v", err)
    +}
     
    Check if request body is not nil before closing it.

    Instead of directly closing the request body, consider checking if it is not nil to avoid
    a potential nil pointer dereference.

    gateway/coprocess.go [107]

    -defer req.Body.Close()
    +if req.Body != nil {
    +    defer req.Body.Close()
    +}
     
    Handle errors returned by grpc.Dial.

    When dialing a gRPC connection with grpc.Dial, it's a good practice to handle the error to
    ensure the connection was established successfully.

    gateway/grpc_test.go [577]

     conn, err := grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
    +if err != nil {
    +    t.Fatalf("failed to dial gRPC: %v", err)
    +}
     
    Reset request body after reading for form parsing.

    When parsing form data from the request, it's safer to reset the request body after
    reading, to ensure subsequent handlers can also read the request body if needed.

    gateway/middleware.go [1070-1074]

    -r.Body = io.NopCloser(io.TeeReader(r.Body, &b))
    -r.Body = io.NopCloser(&b)
    +bodyBytes, err := io.ReadAll(r.Body)
    +if err != nil {
    +    // Handle error
    +}
    +r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) // Reset body so it can be read again
    +r.ParseForm()
    +r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) // Reset body again after parsing
     
    Add error handling for bytes.NewReader.

    Consider handling the potential error returned by bytes.NewReader when initializing
    httpResponse.Body. While bytes.NewReader is unlikely to return an error with a byte slice
    (w.data) as input, explicitly checking for an error can prevent potential panics or issues
    if the implementation changes or if w.data is manipulated in unexpected ways before this
    call.

    gateway/mw_go_plugin.go [86]

    -httpResponse.Body = io.NopCloser(bytes.NewReader(w.data))
    +bodyReader, err := bytes.NewReader(w.data)
    +if err != nil {
    +    // Handle error, possibly logging it or returning an error response
    +    return nil
    +}
    +httpResponse.Body = io.NopCloser(bodyReader)
     
    Performance
    Store rand.Rand as a struct field to improve performance.

    Instead of creating a new instance of rand.Rand on each call to recordWorker, consider
    storing it as a field in the RedisAnalyticsHandler struct. This change avoids the overhead
    of creating a new random source and generator each time, which can improve performance,
    especially under high load.

    gateway/analytics.go [144]

    -rand := mathRand.New(mathRand.NewSource(time.Now().Unix()))
    +// Assume `rand` is a field in the RedisAnalyticsHandler struct, initialized once.
     
    Enhancement
    Use a single instance of rand source for better randomness.

    To improve the randomness and avoid potential issues with concurrent access, consider
    creating the rand source once and storing it, instead of creating a new one every time
    getStaggeredTime is called.

    gateway/host_checker.go [93]

    -rand := mathRand.New(mathRand.NewSource(time.Now().Unix()))
    +// Consider moving this to a struct where `getStaggeredTime` is a method, and initialize `rand` in the struct's constructor.
    +var randSource = mathRand.New(mathRand.NewSource(time.Now().UnixNano()))
    +// Then in `getStaggeredTime`:
    +dur := randSource.Intn(max-min) + min
     

    ✨ Improve tool usage guide:

    Overview:
    The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

    • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
    /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
    
    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    

    See the improve usage page for a comprehensive guide on using this tool.

    @@ -415,7 +415,7 @@
    // fallback to file
    if err != nil {
    // Try read from file
    rawCert, err = ioutil.ReadFile(id)
    rawCert, err = os.ReadFile(id)

    Check failure

    Code scanning / CodeQL

    Uncontrolled data used in path expression High

    This path depends on a
    user-provided value
    .
    @@ -939,7 +938,7 @@
    return apiError("Marshalling failed"), http.StatusInternalServerError
    }

    if err := ioutil.WriteFile(polFilePath, asByte, 0644); err != nil {
    if err := os.WriteFile(polFilePath, asByte, 0644); err != nil {

    Check failure

    Code scanning / CodeQL

    Uncontrolled data used in path expression High

    This path depends on a
    user-provided value
    .
    @@ -1266,7 +1265,7 @@
    return errors.New("marshalling failed"), http.StatusInternalServerError
    }

    if err := ioutil.WriteFile(defFilePath, asByte, 0644); err != nil {
    if err := os.WriteFile(defFilePath, asByte, 0644); err != nil {

    Check failure

    Code scanning / CodeQL

    Uncontrolled data used in path expression High

    This path depends on a
    user-provided value
    .
    This path depends on a
    user-provided value
    .
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    None yet

    1 participant