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

refactor: normalize error handling #32

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ func ApplyPermissionAgent(logger *logrus.Logger, conf *foodme.Configuration, htt
}

// Establish sql handler
agent := foodme.NewPermissionAgent(conf, httpClient)
if agent == nil {
agent, err := foodme.NewPermissionAgent(conf, httpClient)
if err != nil {
logger.WithFields(logrus.Fields{"component": "api"}).Errorf("[%p] Failed to create permission agent", r)
HandleErrorResponse(logger, w, http.StatusInternalServerError, "Failed to create permission agent")
HandleErrorResponse(logger, w, http.StatusInternalServerError, "Failed to create permission agent: "+err.Error())
return
}

Expand Down
2 changes: 1 addition & 1 deletion api/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func TestApplyPermissionAgent(t *testing.T) {
r = &http.Request{Body: &MockBody{Body: "{\"username\":\"test\", \"sql\":\"select * from pets\"}"}}
handler(w, r)
assert.DeepEqual(t, w.headers.headers, []int{500})
assert.DeepEqual(t, w.buffer.buffer, []byte("{\"detail\":\"Failed to create permission agent\"}\n"))
assert.DeepEqual(t, w.buffer.buffer, []byte("{\"detail\":\"Failed to create permission agent: unknown permission agent type: \"}\n"))

// Missing SQL handler
conf.PermissionAgentEnabled = true
Expand Down
4 changes: 2 additions & 2 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/sirupsen/logrus"
)

func Cleaner(logger *logrus.Logger, period int) {
func StartCleaner(logger *logrus.Logger, period int) {
logger.WithFields(logrus.Fields{"component": "cleaner"}).Infof("Starting the Cleaner")
t := time.NewTicker(time.Duration(period) * time.Second)
go func() {
Expand All @@ -28,7 +28,7 @@ func Cleaner(logger *logrus.Logger, period int) {
func Start(logger *logrus.Logger, conf *foodme.Configuration) {
logger.WithFields(logrus.Fields{"component": "api"}).Infof("Starting the API")

go Cleaner(logger, conf.ApiGarbageCollectionPeriod)
StartCleaner(logger, conf.ApiGarbageCollectionPeriod)
server := http.NewServeMux()
httpClient := &http.Client{}
server.HandleFunc("POST /connection", CreateNewConnection(logger, conf.ApiUsernameLifetime))
Expand Down
7 changes: 3 additions & 4 deletions internal/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ func GetHandler(conf *Configuration, logger *logrus.Logger, httpClient IHttpClie
}

var sqlHandler ISQLHandler
var err error
if conf.PermissionAgentEnabled {
pAgent := NewPermissionAgent(conf, httpClient)
if pAgent == nil {
return nil, fmt.Errorf("unsupported permission agent")
pAgent, err := NewPermissionAgent(conf, httpClient)
if err != nil {
return nil, fmt.Errorf("failed to create permission agent: %w", err)
}
sqlHandler, err = NewSQLHandler(conf.DestinationDatabaseType, logger, pAgent)
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions internal/permission_agent.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package foodme

import "fmt"

type SelectFilters struct {
WhereFilters []string `json:"whereFilters"`
JoinFilters []*JoinFilter `json:"joinFilters"`
Expand All @@ -10,7 +12,7 @@ type JoinFilter struct {
Conditions string `json:"conditions"`
}

func NewPermissionAgent(conf *Configuration, httpClient IHttpClient) IPermissionAgent {
func NewPermissionAgent(conf *Configuration, httpClient IHttpClient) (IPermissionAgent, error) {
switch conf.PermissionAgentType {
case "opa":
return NewOPASQL(
Expand All @@ -21,10 +23,10 @@ func NewPermissionAgent(conf *Configuration, httpClient IHttpClient) IPermission
conf.PermissionAgentOPADeleteQuery,
conf.PermissionAgentOPAStringEscapeCharacter,
httpClient,
)
), nil
case "http":
return NewHTTPPermissionAgent(conf.PermissionAgentHTTPDDLEndpoint, conf.PermissionAgentHTTPSelectEndpoint, httpClient)
return NewHTTPPermissionAgent(conf.PermissionAgentHTTPDDLEndpoint, conf.PermissionAgentHTTPSelectEndpoint, httpClient), nil
default:
return nil
return nil, fmt.Errorf("unknown permission agent type: %s", conf.PermissionAgentType)
}
}
10 changes: 6 additions & 4 deletions internal/permission_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import (
func TestNewPermissionAgent(t *testing.T) {
conf, err := NewConfiguration([]string{"--destination-database-type", "postgres", "--destination-host", "localhost", "--destination-port", "5432"})
assert.NilError(t, err)
agent := NewPermissionAgent(conf, nil)
assert.Assert(t, agent == nil)
_, err = NewPermissionAgent(conf, nil)
assert.Error(t, err, "unknown permission agent type: ")

conf, err = NewConfiguration([]string{"--destination-database-type", "postgres", "--destination-host", "localhost", "--destination-port", "5432", "--permission-agent-type", "opa"})
assert.NilError(t, err)
agent = NewPermissionAgent(conf, nil)
agent, err := NewPermissionAgent(conf, nil)
assert.NilError(t, err)
assert.Assert(t, agent != nil)

conf, err = NewConfiguration([]string{"--destination-database-type", "postgres", "--destination-host", "localhost", "--destination-port", "5432", "--permission-agent-type", "http"})
assert.NilError(t, err)
agent = NewPermissionAgent(conf, nil)
agent, err = NewPermissionAgent(conf, nil)
assert.NilError(t, err)
assert.Assert(t, agent != nil)
}
2 changes: 1 addition & 1 deletion internal/postgres_sql_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type PostgresSQLHandler struct {
userInfo map[string]interface{}
}

func NewPostgresSQLHandler(logger *logrus.Logger, pAgent IPermissionAgent) ISQLHandler {
func NewPostgresSQLHandler(logger *logrus.Logger, pAgent IPermissionAgent) *PostgresSQLHandler {
return &PostgresSQLHandler{Logger: logger, PermissionAgent: pAgent, withTables: make(map[string]string)}
}

Expand Down