Skip to content

Commit

Permalink
feat: move error codes into another package
Browse files Browse the repository at this point in the history
  • Loading branch information
1995parham committed Nov 24, 2024
1 parent 58085d0 commit 92557b5
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 90 deletions.
55 changes: 13 additions & 42 deletions internal/authenticator/errors.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,21 @@
package authenticator

import (
"errors"
"fmt"

"github.com/snapp-incubator/soteria/pkg/acl"
)
import "github.com/snapp-incubator/soteria/internal/error"

var (
ErrInvalidSigningMethod = errors.New("signing method does not match with authenticator signing method")
ErrIssNotFound = errors.New("could not found iss in token claims")
ErrSubNotFound = errors.New("could not found sub in token claims")
ErrInvalidClaims = errors.New("invalid claims")
ErrInvalidIP = errors.New("IP is not valid")
ErrInvalidAccessType = errors.New("requested access type is invalid")
ErrDecodeHashID = errors.New("could not decode hash id")
ErrInvalidSecret = errors.New("invalid secret")
ErrIncorrectPassword = errors.New("username or password is wrong")
ErrInvalidSigningMethod = error.ErrInvalidSigningMethod
ErrIssNotFound = error.ErrIssNotFound
ErrSubNotFound = error.ErrSubNotFound
ErrInvalidClaims = error.ErrInvalidClaims
ErrInvalidIP = error.ErrInvalidIP
ErrInvalidAccessType = error.ErrInvalidAccessType
ErrDecodeHashID = error.ErrDecodeHashID
ErrInvalidSecret = error.ErrInvalidSecret
ErrIncorrectPassword = error.ErrIncorrectPassword
)

type TopicNotAllowedError struct {
Issuer string
Sub string
AccessType acl.AccessType
Topic string
TopicType string
}

func (err TopicNotAllowedError) Error() string {
return fmt.Sprintf("issuer %s with sub %s is not allowed to %s on topic %s (%s)",
err.Issuer, err.Sub, err.AccessType, err.Topic, err.TopicType,
)
}

type KeyNotFoundError struct {
Issuer string
}

func (err KeyNotFoundError) Error() string {
return fmt.Sprintf("cannot find issuer %s key", err.Issuer)
}
type TopicNotAllowedError = error.TopicNotAllowedError

type InvalidTopicError struct {
Topic string
}
type KeyNotFoundError = error.KeyNotFoundError

func (err InvalidTopicError) Error() string {
return fmt.Sprintf("provided topic %s is not valid", err.Topic)
}
type InvalidTopicError = error.InvalidTopicError
50 changes: 50 additions & 0 deletions internal/error/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package error

Check failure on line 1 in internal/error/error.go

View workflow job for this annotation

GitHub Actions / lint

package name error has same name as predeclared identifier (predeclared)

import (
"errors"
"fmt"

"github.com/snapp-incubator/soteria/pkg/acl"
)

var (
ErrInvalidSigningMethod = errors.New("signing method does not match with authenticator signing method")
ErrIssNotFound = errors.New("could not found iss in token claims")
ErrSubNotFound = errors.New("could not found sub in token claims")
ErrInvalidClaims = errors.New("invalid claims")
ErrInvalidIP = errors.New("IP is not valid")
ErrInvalidAccessType = errors.New("requested access type is invalid")
ErrDecodeHashID = errors.New("could not decode hash id")
ErrInvalidSecret = errors.New("invalid secret")
ErrIncorrectPassword = errors.New("username or password is wrong")
)

type TopicNotAllowedError struct {
Issuer string
Sub string
AccessType acl.AccessType
Topic string
TopicType string
}

func (err TopicNotAllowedError) Error() string {
return fmt.Sprintf("issuer %s with sub %s is not allowed to %s on topic %s (%s)",
err.Issuer, err.Sub, err.AccessType, err.Topic, err.TopicType,
)
}

type KeyNotFoundError struct {
Issuer string
}

func (err KeyNotFoundError) Error() string {
return fmt.Sprintf("cannot find issuer %s key", err.Issuer)
}

type InvalidTopicError struct {
Topic string
}

func (err InvalidTopicError) Error() string {
return fmt.Sprintf("provided topic %s is not valid", err.Topic)
}
46 changes: 23 additions & 23 deletions internal/metric/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strconv"

"github.com/prometheus/client_golang/prometheus"
"github.com/snapp-incubator/soteria/internal/authenticator"
serror "github.com/snapp-incubator/soteria/internal/error"
)

type AutoAuthenticatorMetrics struct {
Expand Down Expand Up @@ -86,28 +86,28 @@ func (m *APIMetrics) AuthSuccess(company, source string) {
func (m *APIMetrics) AuthFailed(company, source string, err error) {
var (
status string
topicNotAllowedErrorTarget *authenticator.TopicNotAllowedError
keyNotFoundErrorTarget *authenticator.KeyNotFoundError
topicNotAllowedErrorTarget *serror.TopicNotAllowedError
keyNotFoundErrorTarget *serror.KeyNotFoundError
)

switch {
case errors.Is(err, authenticator.ErrInvalidSigningMethod):
case errors.Is(err, serror.ErrInvalidSigningMethod):
status = "err_invalid_signing_method"
case errors.Is(err, authenticator.ErrIssNotFound):
case errors.Is(err, serror.ErrIssNotFound):
status = "err_iss_not_found"
case errors.Is(err, authenticator.ErrSubNotFound):
case errors.Is(err, serror.ErrSubNotFound):
status = "err_sub_not_found"
case errors.Is(err, authenticator.ErrInvalidClaims):
case errors.Is(err, serror.ErrInvalidClaims):
status = "err_invalid_claims"
case errors.Is(err, authenticator.ErrInvalidIP):
case errors.Is(err, serror.ErrInvalidIP):
status = "err_invalid_ip"
case errors.Is(err, authenticator.ErrInvalidAccessType):
case errors.Is(err, serror.ErrInvalidAccessType):
status = "err_invalid_access_type"
case errors.Is(err, authenticator.ErrDecodeHashID):
case errors.Is(err, serror.ErrDecodeHashID):
status = "err_decode_hash_id"
case errors.Is(err, authenticator.ErrInvalidSecret):
case errors.Is(err, serror.ErrInvalidSecret):
status = "err_invalid_secret"
case errors.Is(err, authenticator.ErrIncorrectPassword):
case errors.Is(err, serror.ErrIncorrectPassword):
status = "err_incorrect_password"
case errors.As(err, &topicNotAllowedErrorTarget):
status = "topic_not_allowed_error"
Expand All @@ -128,28 +128,28 @@ func (m *APIMetrics) ACLSuccess(company string) {
func (m *APIMetrics) ACLFailed(company string, err error) {
var (
status string
topicNotAllowedErrorTarget *authenticator.TopicNotAllowedError
keyNotFoundErrorTarget *authenticator.KeyNotFoundError
topicNotAllowedErrorTarget *serror.TopicNotAllowedError
keyNotFoundErrorTarget *serror.KeyNotFoundError
)

switch {
case errors.Is(err, authenticator.ErrInvalidSigningMethod):
case errors.Is(err, serror.ErrInvalidSigningMethod):
status = "err_invalid_signing_method"
case errors.Is(err, authenticator.ErrIssNotFound):
case errors.Is(err, serror.ErrIssNotFound):
status = "err_iss_not_found"
case errors.Is(err, authenticator.ErrSubNotFound):
case errors.Is(err, serror.ErrSubNotFound):
status = "err_sub_not_found"
case errors.Is(err, authenticator.ErrInvalidClaims):
case errors.Is(err, serror.ErrInvalidClaims):
status = "err_invalid_claims"
case errors.Is(err, authenticator.ErrInvalidIP):
case errors.Is(err, serror.ErrInvalidIP):
status = "err_invalid_ip"
case errors.Is(err, authenticator.ErrInvalidAccessType):
case errors.Is(err, serror.ErrInvalidAccessType):
status = "err_invalid_access_type"
case errors.Is(err, authenticator.ErrDecodeHashID):
case errors.Is(err, serror.ErrDecodeHashID):
status = "err_decode_hash_id"
case errors.Is(err, authenticator.ErrInvalidSecret):
case errors.Is(err, serror.ErrInvalidSecret):
status = "err_invalid_secret"
case errors.Is(err, authenticator.ErrIncorrectPassword):
case errors.Is(err, serror.ErrIncorrectPassword):
status = "err_incorrect_password"
case errors.As(err, &topicNotAllowedErrorTarget):
status = "topic_not_allowed_error"
Expand Down
50 changes: 25 additions & 25 deletions internal/metric/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"testing"

"github.com/snapp-incubator/soteria/internal/authenticator"
serror "github.com/snapp-incubator/soteria/internal/error"
"github.com/snapp-incubator/soteria/internal/metric"
)

Expand All @@ -14,44 +14,44 @@ func TestAuthIncrement(t *testing.T) {
m := metric.NewAPIMetrics()

m.AuthSuccess("snapp", "-")
m.AuthFailed("snapp", "-", authenticator.ErrInvalidSigningMethod)
m.AuthFailed("snapp", "-", authenticator.ErrIssNotFound)
m.AuthFailed("snapp", "-", authenticator.ErrSubNotFound)
m.AuthFailed("snapp", "-", authenticator.ErrInvalidClaims)
m.AuthFailed("snapp", "-", authenticator.ErrInvalidIP)

m.AuthFailed("snapp", "-", authenticator.ErrInvalidAccessType)
m.AuthFailed("snapp", "-", authenticator.ErrDecodeHashID)
m.AuthFailed("snapp", "-", authenticator.ErrInvalidSecret)
m.AuthFailed("snapp", "-", authenticator.ErrIncorrectPassword)
m.AuthFailed("snapp", "-", &authenticator.TopicNotAllowedError{
m.AuthFailed("snapp", "-", serror.ErrInvalidSigningMethod)
m.AuthFailed("snapp", "-", serror.ErrIssNotFound)
m.AuthFailed("snapp", "-", serror.ErrSubNotFound)
m.AuthFailed("snapp", "-", serror.ErrInvalidClaims)
m.AuthFailed("snapp", "-", serror.ErrInvalidIP)

m.AuthFailed("snapp", "-", serror.ErrInvalidAccessType)
m.AuthFailed("snapp", "-", serror.ErrDecodeHashID)
m.AuthFailed("snapp", "-", serror.ErrInvalidSecret)
m.AuthFailed("snapp", "-", serror.ErrIncorrectPassword)
m.AuthFailed("snapp", "-", &serror.TopicNotAllowedError{
Issuer: "issuer",
Sub: "subject",
AccessType: "1",
Topic: "topic",
TopicType: "pub",
})
m.AuthFailed("snapp", "-", &authenticator.KeyNotFoundError{Issuer: "iss"})
m.AuthFailed("snapp", "-", &serror.KeyNotFoundError{Issuer: "iss"})
m.AuthFailed("snapp", "-", errors.ErrUnsupported)

m.ACLSuccess("snapp")
m.ACLFailed("snapp", authenticator.ErrInvalidSigningMethod)
m.ACLFailed("snapp", authenticator.ErrIssNotFound)
m.ACLFailed("snapp", authenticator.ErrSubNotFound)
m.ACLFailed("snapp", authenticator.ErrInvalidClaims)
m.ACLFailed("snapp", authenticator.ErrInvalidIP)

m.ACLFailed("snapp", authenticator.ErrInvalidAccessType)
m.ACLFailed("snapp", authenticator.ErrDecodeHashID)
m.ACLFailed("snapp", authenticator.ErrInvalidSecret)
m.ACLFailed("snapp", authenticator.ErrIncorrectPassword)
m.ACLFailed("snapp", &authenticator.TopicNotAllowedError{
m.ACLFailed("snapp", serror.ErrInvalidSigningMethod)
m.ACLFailed("snapp", serror.ErrIssNotFound)
m.ACLFailed("snapp", serror.ErrSubNotFound)
m.ACLFailed("snapp", serror.ErrInvalidClaims)
m.ACLFailed("snapp", serror.ErrInvalidIP)

m.ACLFailed("snapp", serror.ErrInvalidAccessType)
m.ACLFailed("snapp", serror.ErrDecodeHashID)
m.ACLFailed("snapp", serror.ErrInvalidSecret)
m.ACLFailed("snapp", serror.ErrIncorrectPassword)
m.ACLFailed("snapp", &serror.TopicNotAllowedError{
Issuer: "issuer",
Sub: "subject",
AccessType: "1",
Topic: "topic",
TopicType: "pub",
})
m.ACLFailed("snapp", &authenticator.KeyNotFoundError{Issuer: "iss"})
m.ACLFailed("snapp", &serror.KeyNotFoundError{Issuer: "iss"})
m.ACLFailed("snapp", errors.ErrUnsupported)
}

0 comments on commit 92557b5

Please sign in to comment.