Skip to content

Commit

Permalink
Merge pull request #15 from carverauto/updates/linter_fixes_02
Browse files Browse the repository at this point in the history
linter updates
  • Loading branch information
mfreeman451 authored Oct 6, 2024
2 parents 1a821ae + bd0d599 commit e7e8cf7
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/event-ingest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
// Set up gRPC connection to API
grpcServerAddress := app.Config.Get("GRPC_SERVER_ADDRESS")

conn, err := grpc.Dial(grpcServerAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(grpcServerAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("Failed to connect to gRPC server: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/eventrunner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
// Initialize the Gofr app
app := gofr.New()

subjects := strings.Split(",", os.Getenv("NATS_SUBJECTS"))
subjects := strings.Split(app.Config.Get("NATS_SUBJECTS"), ",")

natsClient := nats.New(&nats.Config{
Server: os.Getenv("PUBSUB_BROKER"),
Expand Down
13 changes: 10 additions & 3 deletions cmd/eventrunner/migrations/20241003051401_create_events_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
// Table creation statement for Cassandra
// Table creation statement for Cassandra.
createTableCassandra = `CREATE TABLE IF NOT EXISTS events (
id TEXT,
source TEXT,
Expand All @@ -23,12 +23,14 @@ const (
) WITH CLUSTERING ORDER BY (time DESC);`

// Example batch insert into the events table
//nolint:lll // This is a batch operation
addCassandraRecords = `BEGIN BATCH
INSERT INTO events (id, source, type, subject, time, data_contenttype, data, specversion) VALUES ('1', 'sourceA', 'typeA', 'subjectA', toTimestamp(now()), 'application/json', '{ "key": "value" }', '1.0');
INSERT INTO events (id, source, type, subject, time, data_contenttype, data, specversion) VALUES ('2', 'sourceB', 'typeB', 'subjectB', toTimestamp(now()), 'application/xml', '<key>value</key>', '1.0');
APPLY BATCH;`

// Template query for inserting data into events
//nolint:lll // This is a template query
eventDataCassandra = `INSERT INTO events (id, source, type, subject, time, data_contenttype, data, specversion) VALUES (?, ?, ?, ?, ?, ?, ?, ?);`
)

Expand All @@ -54,11 +56,16 @@ func createTableEventsCassandra() migration.Migrate {
now := time.Now()

// Add records to the batch
if err := d.Cassandra.BatchQuery(batchName, eventDataCassandra, "3", "sourceC", "typeC", "subjectC", now, "application/json", "{ \"key\": \"valueC\" }", "1.0"); err != nil {
if err := d.Cassandra.
BatchQuery(batchName, eventDataCassandra,
"3", "sourceC", "typeC", "subjectC", now,
"application/json", "{ \"key\": \"valueC\" }", "1.0"); err != nil {
return err
}

if err := d.Cassandra.BatchQuery(batchName, eventDataCassandra, "4", "sourceD", "typeD", "subjectD", now, "application/xml", "<key>valueD</key>", "1.0"); err != nil {
if err := d.Cassandra.
BatchQuery(batchName, eventDataCassandra,
"4", "sourceD", "typeD", "subjectD", now, "application/xml", "<key>valueD</key>", "1.0"); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/api/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
gofrhttp "gofr.dev/pkg/gofr/http"
)

// MockRequest is a mock implementation of gofr.Request
// MockRequest is a mock implementation of gofr.Request.
type MockRequest struct {
gofrhttp.Request
ctrl *gomock.Controller
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/middleware/http_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func CombineMiddleware(middlewares ...interface{}) gofr.Handler {
}
}

// Adapt converts a handler func and middlewares into a gofr.Handler
// Adapt converts a handler func and middlewares into a gofr.Handler.
func Adapt(h interface{}, middlewares ...handlers.Middleware) gofr.Handler {
return func(c *gofr.Context) (interface{}, error) {
var handler handlers.Handler
Expand Down
12 changes: 7 additions & 5 deletions pkg/api/middleware/jwt_oidc_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,36 @@ package middleware

import (
"context"

"github.com/coreos/go-oidc/v3/oidc"
)

// OIDCVerifier is a wrapper around oidc.IDTokenVerifier
// OIDCVerifier is a wrapper around oidc.IDTokenVerifier.
type OIDCVerifier struct {
verifier *oidc.IDTokenVerifier
}

// NewOIDCVerifier returns an instance of OIDCVerifier
// NewOIDCVerifier returns an instance of OIDCVerifier.
func NewOIDCVerifier(verifier *oidc.IDTokenVerifier) *OIDCVerifier {
return &OIDCVerifier{verifier: verifier}
}

// Verify method to conform to the IDTokenVerifier interface
// Verify method to conform to the IDTokenVerifier interface.
func (o *OIDCVerifier) Verify(ctx context.Context, rawToken string) (Token, error) {
idToken, err := o.verifier.Verify(ctx, rawToken)
if err != nil {
return nil, err
}

return &OIDCToken{idToken: idToken}, nil
}

// OIDCToken is a wrapper around oidc.IDToken to implement the Token interface
// OIDCToken is a wrapper around oidc.IDToken to implement the Token interface.
type OIDCToken struct {
idToken *oidc.IDToken
}

// Claims wraps the oidc.IDToken.Claims method to conform to the Token interface
// Claims wraps the oidc.IDToken.Claims method to conform to the Token interface.
func (o *OIDCToken) Claims(v interface{}) error {
return o.idToken.Claims(v)
}
14 changes: 8 additions & 6 deletions pkg/api/middleware/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ func (r *MockRequest) Bind(i interface{}) error {
return json.Unmarshal(r.body, i)
}

func (r *MockRequest) HostName() string {
func (*MockRequest) HostName() string {
return "localhost"
}

func (r *MockRequest) Header() http.Header {
return r.header
}

const authorizationKey contextKey = "Authorization"

func TestJWTMiddleware_Validate(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand All @@ -81,7 +83,7 @@ func TestJWTMiddleware_Validate(t *testing.T) {
{
name: "Valid Token",
setupRequest: func(req *MockRequest) {
req.ctx = context.WithValue(context.Background(), "Authorization", "Bearer valid_token")
req.ctx = context.WithValue(context.Background(), authorizationKey, "Bearer valid_token")
},
setupMocks: func() {
mockVerifier.EXPECT().Verify(gomock.Any(), "valid_token").Return(mockToken, nil).Times(1)
Expand All @@ -102,15 +104,15 @@ func TestJWTMiddleware_Validate(t *testing.T) {
},
{
name: "Missing Authorization Header",
setupRequest: func(req *MockRequest) {}, // No Authorization set in context
setupRequest: func(*MockRequest) {}, // No Authorization set in context
setupMocks: func() {},
expectedResult: nil,
expectedError: eventingest.NewAuthError("Missing or invalid authorization header"),
},
{
name: "Invalid Authorization Header",
setupRequest: func(req *MockRequest) {
req.ctx = context.WithValue(context.Background(), "Authorization", "InvalidHeader")
req.ctx = context.WithValue(context.Background(), authorizationKey, "InvalidHeader")
},
setupMocks: func() {},
expectedResult: nil,
Expand All @@ -119,7 +121,7 @@ func TestJWTMiddleware_Validate(t *testing.T) {
{
name: "Invalid Token",
setupRequest: func(req *MockRequest) {
req.ctx = context.WithValue(context.Background(), "Authorization", "Bearer invalid_token")
req.ctx = context.WithValue(context.Background(), authorizationKey, "Bearer invalid_token")
},
setupMocks: func() {
mockVerifier.EXPECT().Verify(gomock.Any(), "invalid_token").Return(nil, eventingest.NewAuthError("Invalid token")).Times(1)
Expand Down Expand Up @@ -150,7 +152,7 @@ func TestJWTMiddleware_Validate(t *testing.T) {

tt.setupMocks()

handler := jwtMiddleware.Validate(func(cc customctx.Context) (interface{}, error) {
handler := jwtMiddleware.Validate(func(customctx.Context) (interface{}, error) {
return "success", nil
})

Expand Down
8 changes: 5 additions & 3 deletions pkg/api/middleware/rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package middleware
import (
"testing"

"github.com/stretchr/testify/require"

"github.com/carverauto/eventrunner/pkg/api/handlers"
customctx "github.com/carverauto/eventrunner/pkg/context"
"github.com/carverauto/eventrunner/pkg/eventingest"
Expand Down Expand Up @@ -75,17 +77,17 @@ func TestAuthenticateAPIKey(t *testing.T) {
mockContext := customctx.NewMockContext(ctrl)
tt.setupMocks(mockContext)

nextHandler := handlers.HandlerFunc(func(c *gofr.Context) (interface{}, error) {
nextHandler := handlers.HandlerFunc(func(*gofr.Context) (interface{}, error) {
return "success", nil
})

result, err := authenticateAPIKey(mockContext, nextHandler)

if tt.expectedError != nil {
assert.Error(t, err)
require.Error(t, err)
assert.Equal(t, tt.expectedError, err)
} else {
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, tt.expectedResult, result)
}
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewCustomContext(c *gofr.Context) *CustomContext {
}
}

// Adapter is a test helper that implements customctx.Context
// Adapter is a test helper that implements customctx.Context.
type Adapter struct {
MockContext *MockContext
GofrContext *gofr.Context
Expand Down
3 changes: 2 additions & 1 deletion pkg/eventingest/grpc_forwarder_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package eventingest

import (
"github.com/stretchr/testify/require"
"testing"

"github.com/stretchr/testify/require"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
Expand Down

0 comments on commit e7e8cf7

Please sign in to comment.