Skip to content

Commit

Permalink
[TT-10451] Remove init functions from gateway, enforce linting (TykTe…
Browse files Browse the repository at this point in the history
…chnologies#5761)

https://tyktech.atlassian.net/browse/TT-10451

Removes all init funcs except `/log`, we exclude that one from linting.

Unwanted behaviour fixes:

- test package init() leaked into prod code, enabling pprof server start
and a resource monitor
- gateway defaultTykErrors reset errors and duplicated code from two
init functions
- init oauth2 errors function updated to use declared consts

To resolve the first, an `test.InitTestMain` function is added and
invoked from gateway.InitTestMain

To verify:

```
golangci-lint run --no-config --disable-all -E gochecknoinits -v ./...
```

---------

Co-authored-by: Tit Petric <tit@tyk.io>
Rate limit · GitHub

Access has been restricted

You have triggered a rate limit.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

titpetric and Tit Petric authored Nov 9, 2023
1 parent 72f28b1 commit 357563c
Showing 19 changed files with 118 additions and 150 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -75,6 +75,7 @@ linters:
- ireturn
- goerr113
enable:
- gochecknoinits
- thelper
- errcheck
- errorlint
2 changes: 1 addition & 1 deletion .taskfiles/deps/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ tasks:
status:
- type golangci-lint
cmds:
- go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.45.0
- go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2

faillint:
internal: true
105 changes: 52 additions & 53 deletions apidef/oas/validator.go
Original file line number Diff line number Diff line change
@@ -27,9 +27,11 @@ const (
)

var (
log = logger.Get()
log = logger.Get()

schemaOnce sync.Once

oasJSONSchemas map[string][]byte
mu sync.Mutex
errorFormatter = func(errs []error) string {
var result strings.Builder
for i, err := range errs {
@@ -45,65 +47,63 @@ var (
defaultVersion string
)

func init() {
if err := loadOASSchema(); err != nil {
log.WithError(err).Error("loadOASSchema failed!")
return
}

setDefaultVersion()
}

func loadOASSchema() error {
mu.Lock()
defer mu.Unlock()
load := func() error {
xTykAPIGwSchema, err := schemaDir.ReadFile(fmt.Sprintf("schema/%s.json", ExtensionTykAPIGateway))
if err != nil {
return fmt.Errorf("%s loading failed: %w", ExtensionTykAPIGateway, err)
}

xTykAPIGwSchema, err := schemaDir.ReadFile(fmt.Sprintf("schema/%s.json", ExtensionTykAPIGateway))
if err != nil {
return fmt.Errorf("%s loading failed: %w", ExtensionTykAPIGateway, err)
}
xTykAPIGwSchemaWithoutDefs := jsonparser.Delete(xTykAPIGwSchema, keyDefinitions)
oasJSONSchemas = make(map[string][]byte)
members, err := schemaDir.ReadDir("schema")
for _, member := range members {
if member.IsDir() {
continue
}

xTykAPIGwSchemaWithoutDefs := jsonparser.Delete(xTykAPIGwSchema, keyDefinitions)
oasJSONSchemas = make(map[string][]byte)
members, err := schemaDir.ReadDir("schema")
for _, member := range members {
if member.IsDir() {
continue
}
fileName := member.Name()
if !strings.HasSuffix(fileName, ".json") {
continue
}

fileName := member.Name()
if !strings.HasSuffix(fileName, ".json") {
continue
}
if strings.HasSuffix(fileName, fmt.Sprintf("%s.json", ExtensionTykAPIGateway)) {
continue
}

if strings.HasSuffix(fileName, fmt.Sprintf("%s.json", ExtensionTykAPIGateway)) {
continue
}
var data []byte
data, err = schemaDir.ReadFile(filepath.Join("schema/", fileName))
if err != nil {
return err
}

var data []byte
data, err = schemaDir.ReadFile(filepath.Join("schema/", fileName))
if err != nil {
return err
}
data, err = jsonparser.Set(data, xTykAPIGwSchemaWithoutDefs, keyProperties, ExtensionTykAPIGateway)
if err != nil {
return err
}

data, err = jsonparser.Set(data, xTykAPIGwSchemaWithoutDefs, keyProperties, ExtensionTykAPIGateway)
if err != nil {
return err
}
err = jsonparser.ObjectEach(xTykAPIGwSchema, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
data, err = jsonparser.Set(data, value, keyDefinitions, string(key))
return err
}, keyDefinitions)
if err != nil {
return err
}

err = jsonparser.ObjectEach(xTykAPIGwSchema, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
data, err = jsonparser.Set(data, value, keyDefinitions, string(key))
return err
}, keyDefinitions)
if err != nil {
return err
oasVersion := strings.TrimSuffix(fileName, ".json")
oasJSONSchemas[oasVersion] = data
}

oasVersion := strings.TrimSuffix(fileName, ".json")
oasJSONSchemas[oasVersion] = data
setDefaultVersion()

return nil
}

return nil
var err error
schemaOnce.Do(func() {
err = load()
})
return err
}

// ValidateOASObject validates an OAS document against a particular OAS version.
@@ -137,8 +137,9 @@ func ValidateOASObject(documentBody []byte, oasVersion string) error {

// GetOASSchema returns an oas schema for a particular version.
func GetOASSchema(version string) ([]byte, error) {
mu.Lock()
defer mu.Unlock()
if err := loadOASSchema(); err != nil {
return nil, fmt.Errorf("loadOASSchema failed: %w", err)
}

if version == "" {
return oasJSONSchemas[defaultVersion], nil
@@ -171,8 +172,6 @@ func findDefaultVersion(rawVersions []string) string {
}

func setDefaultVersion() {
mu.Lock()
defer mu.Unlock()
var versions []string
for k := range oasJSONSchemas {
versions = append(versions, k)
6 changes: 1 addition & 5 deletions cli/bundler/bundler.go
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ const (
)

var (
bundler *Bundler
bundler *Bundler = &Bundler{}

errNoHooks = errors.New("No hooks defined")
errNoDriver = errors.New("No driver specified")
@@ -49,10 +49,6 @@ type Bundler struct {
manifestPath *string
}

func init() {
bundler = &Bundler{}
}

// Bundle is the entrypoint function for this subcommand.
func (b *Bundler) Bundle(ctx *kingpin.ParseContext) error {
return nil
5 changes: 4 additions & 1 deletion cli/bundler/bundler_test.go
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ var (
}
)

func init() {
func TestMain(m *testing.M) {
testApp = kingpin.New("tyk-cli", "")
AddTo(testApp)

@@ -39,6 +39,8 @@ func init() {
bundler.bundlePath = &bundlePath
manifestPath := defaultManifestPath
bundler.manifestPath = &manifestPath

os.Exit(m.Run())
}

func writeManifestFile(t testing.TB, manifest interface{}, filename string) *string {
@@ -69,6 +71,7 @@ func TestCommands(t *testing.T) {
t.Fatalf("Command not found")
}
}

func TestBuild(t *testing.T) {
defer os.Remove(defaultManifestPath)

7 changes: 2 additions & 5 deletions cli/importer/importer.go
Original file line number Diff line number Diff line change
@@ -23,7 +23,8 @@ const (
)

var (
imp *Importer
imp *Importer = &Importer{}

errUnknownMode = errors.New("Unknown mode")
)

@@ -42,10 +43,6 @@ type Importer struct {
asVersion *string
}

func init() {
imp = &Importer{}
}

// AddTo initializes an importer object.
func AddTo(app *kingpin.Application) {
cmd := app.Command(cmdName, cmdDesc)
6 changes: 3 additions & 3 deletions coprocess/python/coprocess_id_extractor_python_test.go
Original file line number Diff line number Diff line change
@@ -169,7 +169,7 @@ func TestValueExtractorHeaderSource(t *testing.T) {
ts := gateway.StartTest(nil, gateway.TestConfig{
CoprocessConfig: config.CoProcessConfig{
EnableCoProcess: true,
PythonPathPrefix: pkgPath,
PythonPathPrefix: pkgPath(),
},
})
defer ts.Close()
@@ -195,7 +195,7 @@ func TestValueExtractorHeaderSource(t *testing.T) {
ts := gateway.StartTest(nil, gateway.TestConfig{
CoprocessConfig: config.CoProcessConfig{
EnableCoProcess: true,
PythonPathPrefix: pkgPath,
PythonPathPrefix: pkgPath(),
},
})
defer ts.Close()
@@ -223,7 +223,7 @@ func TestValueExtractorHeaderSource(t *testing.T) {
ts := gateway.StartTest(nil, gateway.TestConfig{
CoprocessConfig: config.CoProcessConfig{
EnableCoProcess: true,
PythonPathPrefix: pkgPath,
PythonPathPrefix: pkgPath(),
},
})
defer ts.Close()
8 changes: 3 additions & 5 deletions coprocess/python/coprocess_python_test.go
Original file line number Diff line number Diff line change
@@ -19,11 +19,9 @@ import (
"github.com/TykTechnologies/tyk/user"
)

var pkgPath string

func init() {
func pkgPath() string {
_, filename, _, _ := runtime.Caller(0)
pkgPath = filepath.Dir(filename) + "./../../"
return filepath.Dir(filename) + "./../../"
}

var pythonBundleWithAuthCheck = func(token1, token2 string) map[string]string {
@@ -224,7 +222,7 @@ func setupGateway() *gateway.Test {
ts := gateway.StartTest(nil, gateway.TestConfig{
CoprocessConfig: config.CoProcessConfig{
EnableCoProcess: true,
PythonPathPrefix: pkgPath,
PythonPathPrefix: pkgPath(),
}})

return ts
4 changes: 3 additions & 1 deletion dlpython/main_test.go
Original file line number Diff line number Diff line change
@@ -8,11 +8,13 @@ import (

var testVersion = "3.5"

func init() {
func TestMain(m *testing.M) {
if versionOverride := os.Getenv("PYTHON_VERSION"); versionOverride != "" {
testVersion = versionOverride
}
fmt.Printf("Using Python %s for tests\n", testVersion)

os.Exit(m.Run())
}

func TestFindPythonConfig(t *testing.T) {
8 changes: 3 additions & 5 deletions gateway/coprocess_bundle_test.go
Original file line number Diff line number Diff line change
@@ -22,11 +22,9 @@ var (
testBundlesPath = filepath.Join(testMiddlewarePath, "bundles")
)

var pkgPath string

func init() {
func pkgPath() string {
_, filename, _, _ := runtime.Caller(0)
pkgPath = filepath.Dir(filename) + "./.."
return filepath.Dir(filename) + "./.."
}

var grpcBundleWithAuthCheck = map[string]string{
@@ -233,7 +231,7 @@ func TestResponseOverride(t *testing.T) {
ts := StartTest(nil, TestConfig{
CoprocessConfig: config.CoProcessConfig{
EnableCoProcess: true,
PythonPathPrefix: pkgPath,
PythonPathPrefix: pkgPath(),
PythonVersion: pythonVersion,
}})
defer ts.Close()
45 changes: 2 additions & 43 deletions gateway/handler_error.go
Original file line number Diff line number Diff line change
@@ -47,50 +47,9 @@ func errorAndStatusCode(errType string) (error, int) {

func defaultTykErrors() {
TykErrors = make(map[string]config.TykError)
TykErrors[ErrAuthAuthorizationFieldMissing] = config.TykError{
Message: MsgAuthFieldMissing,
Code: http.StatusUnauthorized,
}

TykErrors[ErrAuthKeyNotFound] = config.TykError{
Message: MsgApiAccessDisallowed,
Code: http.StatusForbidden,
}

TykErrors[ErrAuthCertNotFound] = config.TykError{
Message: MsgApiAccessDisallowed,
Code: http.StatusForbidden,
}

TykErrors[ErrAuthCertExpired] = config.TykError{
Message: MsgCertificateExpired,
Code: http.StatusForbidden,
}

TykErrors[ErrAuthKeyIsInvalid] = config.TykError{
Message: MsgApiAccessDisallowed,
Code: http.StatusForbidden,
}

TykErrors[ErrOAuthAuthorizationFieldMissing] = config.TykError{
Message: MsgAuthFieldMissing,
Code: http.StatusBadRequest,
}

TykErrors[ErrOAuthAuthorizationFieldMalformed] = config.TykError{
Message: MsgBearerMailformed,
Code: http.StatusBadRequest,
}

TykErrors[ErrOAuthKeyNotFound] = config.TykError{
Message: MsgKeyNotAuthorized,
Code: http.StatusForbidden,
}

TykErrors[ErrOAuthClientDeleted] = config.TykError{
Message: MsgOauthClientRevoked,
Code: http.StatusForbidden,
}
initAuthKeyErrors()
initOauth2KeyExistsErrors()
}

func overrideTykErrors(gw *Gateway) {
2 changes: 1 addition & 1 deletion gateway/mw_auth_key.go
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ const (
MsgInvalidKey = "Attempted access with invalid key."
)

func init() {
func initAuthKeyErrors() {
TykErrors[ErrAuthAuthorizationFieldMissing] = config.TykError{
Message: MsgAuthFieldMissing,
Code: http.StatusUnauthorized,
Rate limit · GitHub

Access has been restricted

You have triggered a rate limit.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

0 comments on commit 357563c

Please sign in to comment.