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

Add golangci config file and fix linter issues #108

Merged
merged 13 commits into from
Sep 19, 2023
61 changes: 61 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
run:
tests: false
skip-dirs:
- "mocks"
issues:
exclude-rules:
- path: internal/events/test_helper.go
text: "unused"
linters-settings:
golint: {}
gocritic:
enabled-checks: []
disabled-checks:
- regexpMust
goheader:
values:
regexp:
COMPANY: .*
template: |-
Copyright © {{ YEAR }} {{ COMPANY }}

SPDX-License-Identifier: Apache-2.0

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
linters:
disable-all: false
disable:
- structcheck
enable:
- depguard
- dogsled
- errcheck
- goconst
- gocritic
- gocyclo
- gofmt
- goheader
- goimports
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- unused
15 changes: 7 additions & 8 deletions cmd/fabconnect.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright 2021 Kaleido
// Copyright © 2023 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//
// http://www.apache.org/licenses/LICENSE-2.0

//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -22,16 +22,14 @@ import (
"net/http"
"strings"

"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"

"github.com/hyperledger/firefly-fabconnect/internal/conf"
"github.com/hyperledger/firefly-fabconnect/internal/rest"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
prefixed "github.com/x-cray/logrus-prefixed-formatter"

_ "net/http/pprof"
)

func initLogging(debugLevel int) {
Expand Down Expand Up @@ -67,7 +65,7 @@ var rootConfig = &cmdConfig{}

func newRootCmd() (*cobra.Command, *conf.RESTGatewayConf) {
restGatewayConf := &conf.RESTGatewayConf{}
var restGateway *rest.RESTGateway
var restGateway *rest.Gateway

rootCmd := &cobra.Command{
Use: "fabconnect",
Expand Down Expand Up @@ -96,6 +94,7 @@ func newRootCmd() (*cobra.Command, *conf.RESTGatewayConf) {

if rootConfig.DebugPort > 0 {
go func() {
// #nosec
log.Debugf("Debug HTTP endpoint listening on localhost:%d: %s", rootConfig.DebugPort, http.ListenAndServe(fmt.Sprintf("localhost:%d", rootConfig.DebugPort), nil))
}()
}
Expand Down Expand Up @@ -142,7 +141,7 @@ func initConfig() {
}
}

func startServer(restGatewayConf *conf.RESTGatewayConf, restGateway *rest.RESTGateway) error {
func startServer(restGatewayConf *conf.RESTGatewayConf, restGateway *rest.Gateway) error {

if rootConfig.PrintYAML {
a, err := marshalToYAML(rootConfig)
Expand Down
26 changes: 13 additions & 13 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright 2021 Kaleido
// Copyright © 2023 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//
// http://www.apache.org/licenses/LICENSE-2.0

//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -77,8 +77,8 @@ func GetAccessToken(ctx context.Context) string {
return ""
}

// AuthRPC authorize an RPC call
func AuthRPC(ctx context.Context, method string, args ...interface{}) error {
// RPC authorize an RPC call
func RPC(ctx context.Context, method string, args ...interface{}) error {
if securityModule != nil && !IsSystemContext(ctx) {
authCtx := GetAuthContext(ctx)
if authCtx == nil {
Expand All @@ -89,8 +89,8 @@ func AuthRPC(ctx context.Context, method string, args ...interface{}) error {
return nil
}

// AuthRPCSubscribe authorize a subscribe RPC call
func AuthRPCSubscribe(ctx context.Context, namespace string, channel interface{}, args ...interface{}) error {
// RPCSubscribe authorize a subscribe RPC call
func RPCSubscribe(ctx context.Context, namespace string, channel interface{}, args ...interface{}) error {
if securityModule != nil && !IsSystemContext(ctx) {
authCtx := GetAuthContext(ctx)
if authCtx == nil {
Expand All @@ -101,8 +101,8 @@ func AuthRPCSubscribe(ctx context.Context, namespace string, channel interface{}
return nil
}

// AuthEventStreams authorize the whole of event streams
func AuthEventStreams(ctx context.Context) error {
// EventStreams authorize the whole of event streams
func EventStreams(ctx context.Context) error {
if securityModule != nil && !IsSystemContext(ctx) {
authCtx := GetAuthContext(ctx)
if authCtx == nil {
Expand All @@ -113,8 +113,8 @@ func AuthEventStreams(ctx context.Context) error {
return nil
}

// AuthListAsyncReplies authorize the listing or searching of all replies
func AuthListAsyncReplies(ctx context.Context) error {
// ListAsyncReplies authorize the listing or searching of all replies
func ListAsyncReplies(ctx context.Context) error {
if securityModule != nil && !IsSystemContext(ctx) {
authCtx := GetAuthContext(ctx)
if authCtx == nil {
Expand All @@ -125,8 +125,8 @@ func AuthListAsyncReplies(ctx context.Context) error {
return nil
}

// AuthReadAsyncReplyByUUID authorize the query of an invidual reply by UUID
func AuthReadAsyncReplyByUUID(ctx context.Context) error {
// ReadAsyncReplyByUUID authorize the query of an invidual reply by UUID
func ReadAsyncReplyByUUID(ctx context.Context) error {
if securityModule != nil && !IsSystemContext(ctx) {
authCtx := GetAuthContext(ctx)
if authCtx == nil {
Expand Down
44 changes: 22 additions & 22 deletions internal/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ func TestAccessToken(t *testing.T) {
func TestAuthRPC(t *testing.T) {
assert := assert.New(t)

assert.NoError(AuthRPC(context.Background(), "anything"))
assert.NoError(RPC(context.Background(), "anything"))

RegisterSecurityModule(&authtest.TestSecurityModule{})

assert.EqualError(AuthRPC(context.Background(), "anything"), "No auth context")
assert.EqualError(RPC(context.Background(), "anything"), "No auth context")

assert.NoError(AuthRPC(NewSystemAuthContext(), "anything"))
assert.NoError(RPC(NewSystemAuthContext(), "anything"))

ctx, _ := WithAuthContext(context.Background(), "testat")
assert.NoError(AuthRPC(ctx, "testrpc"))
assert.EqualError(AuthRPC(ctx, "anything"), "badness")
assert.NoError(RPC(ctx, "testrpc"))
assert.EqualError(RPC(ctx, "anything"), "badness")

RegisterSecurityModule(nil)

Expand All @@ -80,17 +80,17 @@ func TestAuthRPC(t *testing.T) {
func TestAuthRPCSubscribe(t *testing.T) {
assert := assert.New(t)

assert.NoError(AuthRPCSubscribe(context.Background(), "anything", nil))
assert.NoError(RPCSubscribe(context.Background(), "anything", nil))

RegisterSecurityModule(&authtest.TestSecurityModule{})

assert.EqualError(AuthRPCSubscribe(context.Background(), "anything", nil), "No auth context")
assert.EqualError(RPCSubscribe(context.Background(), "anything", nil), "No auth context")

assert.NoError(AuthRPCSubscribe(NewSystemAuthContext(), "anything", nil))
assert.NoError(RPCSubscribe(NewSystemAuthContext(), "anything", nil))

ctx, _ := WithAuthContext(context.Background(), "testat")
assert.NoError(AuthRPCSubscribe(ctx, "testns", nil))
assert.EqualError(AuthRPCSubscribe(ctx, "anything", nil), "badness")
assert.NoError(RPCSubscribe(ctx, "testns", nil))
assert.EqualError(RPCSubscribe(ctx, "anything", nil), "badness")

RegisterSecurityModule(nil)

Expand All @@ -99,16 +99,16 @@ func TestAuthRPCSubscribe(t *testing.T) {
func TestAuthEventStreams(t *testing.T) {
assert := assert.New(t)

assert.NoError(AuthEventStreams(context.Background()))
assert.NoError(EventStreams(context.Background()))

RegisterSecurityModule(&authtest.TestSecurityModule{})

assert.EqualError(AuthEventStreams(context.Background()), "No auth context")
assert.EqualError(EventStreams(context.Background()), "No auth context")

assert.NoError(AuthEventStreams(NewSystemAuthContext()))
assert.NoError(EventStreams(NewSystemAuthContext()))

ctx, _ := WithAuthContext(context.Background(), "testat")
assert.NoError(AuthEventStreams(ctx))
assert.NoError(EventStreams(ctx))

RegisterSecurityModule(nil)

Expand All @@ -117,16 +117,16 @@ func TestAuthEventStreams(t *testing.T) {
func TestAuthListAsyncReplies(t *testing.T) {
assert := assert.New(t)

assert.NoError(AuthListAsyncReplies(context.Background()))
assert.NoError(ListAsyncReplies(context.Background()))

RegisterSecurityModule(&authtest.TestSecurityModule{})

assert.EqualError(AuthListAsyncReplies(context.Background()), "No auth context")
assert.EqualError(ListAsyncReplies(context.Background()), "No auth context")

assert.NoError(AuthListAsyncReplies(NewSystemAuthContext()))
assert.NoError(ListAsyncReplies(NewSystemAuthContext()))

ctx, _ := WithAuthContext(context.Background(), "testat")
assert.NoError(AuthListAsyncReplies(ctx))
assert.NoError(ListAsyncReplies(ctx))

RegisterSecurityModule(nil)

Expand All @@ -135,16 +135,16 @@ func TestAuthListAsyncReplies(t *testing.T) {
func TestAuthReadAsyncReplyByUUID(t *testing.T) {
assert := assert.New(t)

assert.NoError(AuthReadAsyncReplyByUUID(context.Background()))
assert.NoError(ReadAsyncReplyByUUID(context.Background()))

RegisterSecurityModule(&authtest.TestSecurityModule{})

assert.EqualError(AuthReadAsyncReplyByUUID(context.Background()), "No auth context")
assert.EqualError(ReadAsyncReplyByUUID(context.Background()), "No auth context")

assert.NoError(AuthReadAsyncReplyByUUID(NewSystemAuthContext()))
assert.NoError(ReadAsyncReplyByUUID(NewSystemAuthContext()))

ctx, _ := WithAuthContext(context.Background(), "testat")
assert.NoError(AuthReadAsyncReplyByUUID(ctx))
assert.NoError(ReadAsyncReplyByUUID(ctx))

RegisterSecurityModule(nil)

Expand Down
11 changes: 8 additions & 3 deletions internal/auth/authtest/testsm.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright 2021 Kaleido
// Copyright © 2023 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//
// http://www.apache.org/licenses/LICENSE-2.0

//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -38,6 +38,7 @@ func (sm *TestSecurityModule) AuthRPC(authCtx interface{}, method string, args .
if method == "testrpc" {
return nil
}
default:
}
return fmt.Errorf("badness")
}
Expand All @@ -49,6 +50,7 @@ func (sm *TestSecurityModule) AuthRPCSubscribe(authCtx interface{}, namespace st
if namespace == "testns" {
return nil
}
default:
}
return fmt.Errorf("badness")
}
Expand All @@ -58,6 +60,7 @@ func (sm *TestSecurityModule) AuthEventStreams(authCtx interface{}) error {
switch authCtx.(type) {
case string:
return nil
default:
}
return fmt.Errorf("badness")
}
Expand All @@ -67,6 +70,7 @@ func (sm *TestSecurityModule) AuthListAsyncReplies(authCtx interface{}) error {
switch authCtx.(type) {
case string:
return nil
default:
}
return fmt.Errorf("badness")
}
Expand All @@ -76,6 +80,7 @@ func (sm *TestSecurityModule) AuthReadAsyncReplyByUUID(authCtx interface{}) erro
switch authCtx.(type) {
case string:
return nil
default:
}
return fmt.Errorf("badness")
}
8 changes: 4 additions & 4 deletions internal/errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright 2021 Kaleido
// Copyright © 2023 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//
// http://www.apache.org/licenses/LICENSE-2.0

//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -158,7 +158,7 @@ const (
// LevelDBFailedRetriveOriginalKey problem retrieving entry - original key
LevelDBFailedRetriveOriginalKey = "Failed to retrieve the entry for the original key: %s. %s"
// LevelDBFailedRetriveGeneratedID problem retrieving entry - generated ID
LevelDBFailedRetriveGeneratedID = "Failed to retrieve the entry for the generated ID: %s. %s"
LevelDBFailedRetriveGeneratedID = "failed to retrieve the entry for the generated ID: %s. %s"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious why this one was lowercased but not the others?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's because I am getting this:

internal/rest/receipt/leveldb.go:227:15: ST1005: error strings should not be capitalized (stylecheck)
                return nil, fmt.Errorf(errors.LevelDBFailedRetriveGeneratedID, requestID, err)


// KVStoreDBLoad failed to init DB
KVStoreDBLoad = "Failed to open DB at %s: %s"
Expand Down
Loading