Skip to content

Commit

Permalink
refactor: automated move of errors and id from root to kit (influxdat…
Browse files Browse the repository at this point in the history
…a#21101)



Co-authored-by: Sam Arnold <[email protected]>
  • Loading branch information
danxmoran and lesam authored Mar 30, 2021
1 parent a8183d8 commit 00afd95
Show file tree
Hide file tree
Showing 555 changed files with 7,962 additions and 6,914 deletions.
37 changes: 20 additions & 17 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@ package influxdb
import (
"context"
"fmt"

"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
)

// AuthorizationKind is returned by (*Authorization).Kind().
const AuthorizationKind = "authorization"

// ErrUnableToCreateToken sanitized error message for all errors when a user cannot create a token
var ErrUnableToCreateToken = &Error{
var ErrUnableToCreateToken = &errors.Error{
Msg: "unable to create token",
Code: EInvalid,
Code: errors.EInvalid,
}

// Authorization is an authorization. 🎉
type Authorization struct {
ID ID `json:"id"`
ID platform.ID `json:"id"`
Token string `json:"token"`
Status Status `json:"status"`
Description string `json:"description"`
OrgID ID `json:"orgID"`
UserID ID `json:"userID,omitempty"`
OrgID platform.ID `json:"orgID"`
UserID platform.ID `json:"userID,omitempty"`
Permissions []Permission `json:"permissions"`
CRUDLog
}
Expand All @@ -36,9 +39,9 @@ type AuthorizationUpdate struct {
func (a *Authorization) Valid() error {
for _, p := range a.Permissions {
if p.Resource.OrgID != nil && *p.Resource.OrgID != a.OrgID {
return &Error{
return &errors.Error{
Msg: fmt.Sprintf("permission %s is not for org id %s", p, a.OrgID),
Code: EInvalid,
Code: errors.EInvalid,
}
}
}
Expand All @@ -49,8 +52,8 @@ func (a *Authorization) Valid() error {
// PermissionSet returns the set of permissions associated with the Authorization.
func (a *Authorization) PermissionSet() (PermissionSet, error) {
if !a.IsActive() {
return nil, &Error{
Code: EUnauthorized,
return nil, &errors.Error{
Code: errors.EUnauthorized,
Msg: "token is inactive",
}
}
Expand All @@ -69,15 +72,15 @@ func (a *Authorization) IsActive() bool {
}

// GetUserID returns the user id.
func (a *Authorization) GetUserID() ID {
func (a *Authorization) GetUserID() platform.ID {
return a.UserID
}

// Kind returns session and is used for auditing.
func (a *Authorization) Kind() string { return AuthorizationKind }

// Identifier returns the authorizations ID and is used for auditing.
func (a *Authorization) Identifier() ID { return a.ID }
func (a *Authorization) Identifier() platform.ID { return a.ID }

// auth service op
const (
Expand All @@ -92,7 +95,7 @@ const (
// AuthorizationService represents a service for managing authorization data.
type AuthorizationService interface {
// Returns a single authorization by ID.
FindAuthorizationByID(ctx context.Context, id ID) (*Authorization, error)
FindAuthorizationByID(ctx context.Context, id platform.ID) (*Authorization, error)

// Returns a single authorization by Token.
FindAuthorizationByToken(ctx context.Context, t string) (*Authorization, error)
Expand All @@ -105,20 +108,20 @@ type AuthorizationService interface {
CreateAuthorization(ctx context.Context, a *Authorization) error

// UpdateAuthorization updates the status and description if available.
UpdateAuthorization(ctx context.Context, id ID, upd *AuthorizationUpdate) (*Authorization, error)
UpdateAuthorization(ctx context.Context, id platform.ID, upd *AuthorizationUpdate) (*Authorization, error)

// Removes a authorization by token.
DeleteAuthorization(ctx context.Context, id ID) error
DeleteAuthorization(ctx context.Context, id platform.ID) error
}

// AuthorizationFilter represents a set of filter that restrict the returned results.
type AuthorizationFilter struct {
Token *string
ID *ID
ID *platform.ID

UserID *ID
UserID *platform.ID
User *string

OrgID *ID
OrgID *platform.ID
Org *string
}
40 changes: 20 additions & 20 deletions authorization/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,64 @@ package authorization
import (
"fmt"

"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
)

var (
// ErrInvalidAuthID is used when the Authorization's ID cannot be encoded
ErrInvalidAuthID = &influxdb.Error{
Code: influxdb.EInvalid,
ErrInvalidAuthID = &errors.Error{
Code: errors.EInvalid,
Msg: "authorization ID is invalid",
}

// ErrAuthNotFound is used when the specified auth cannot be found
ErrAuthNotFound = &influxdb.Error{
Code: influxdb.ENotFound,
ErrAuthNotFound = &errors.Error{
Code: errors.ENotFound,
Msg: "authorization not found",
}

// NotUniqueIDError occurs when attempting to create an Authorization with an ID that already belongs to another one
NotUniqueIDError = &influxdb.Error{
Code: influxdb.EConflict,
NotUniqueIDError = &errors.Error{
Code: errors.EConflict,
Msg: "ID already exists",
}

// ErrFailureGeneratingID occurs ony when the random number generator
// cannot generate an ID in MaxIDGenerationN times.
ErrFailureGeneratingID = &influxdb.Error{
Code: influxdb.EInternal,
ErrFailureGeneratingID = &errors.Error{
Code: errors.EInternal,
Msg: "unable to generate valid id",
}

// ErrTokenAlreadyExistsError is used when attempting to create an authorization
// with a token that already exists
ErrTokenAlreadyExistsError = &influxdb.Error{
Code: influxdb.EConflict,
ErrTokenAlreadyExistsError = &errors.Error{
Code: errors.EConflict,
Msg: "token already exists",
}
)

// ErrInvalidAuthIDError is used when a service was provided an invalid ID.
func ErrInvalidAuthIDError(err error) *influxdb.Error {
return &influxdb.Error{
Code: influxdb.EInvalid,
func ErrInvalidAuthIDError(err error) *errors.Error {
return &errors.Error{
Code: errors.EInvalid,
Msg: "auth id provided is invalid",
Err: err,
}
}

// ErrInternalServiceError is used when the error comes from an internal system.
func ErrInternalServiceError(err error) *influxdb.Error {
return &influxdb.Error{
Code: influxdb.EInternal,
func ErrInternalServiceError(err error) *errors.Error {
return &errors.Error{
Code: errors.EInternal,
Err: err,
}
}

// UnexpectedAuthIndexError is used when the error comes from an internal system.
func UnexpectedAuthIndexError(err error) *influxdb.Error {
return &influxdb.Error{
Code: influxdb.EInternal,
func UnexpectedAuthIndexError(err error) *errors.Error {
return &errors.Error{
Code: errors.EInternal,
Msg: fmt.Sprintf("unexpected error retrieving auth index; Err: %v", err),
}
}
8 changes: 5 additions & 3 deletions authorization/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"

"github.com/influxdata/influxdb/v2/kit/platform"

"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/pkg/httpc"
)
Expand Down Expand Up @@ -72,7 +74,7 @@ func (s *AuthorizationClientService) FindAuthorizationByToken(ctx context.Contex
}

// FindAuthorizationByID finds a single Authorization by its ID against a remote influx server.
func (s *AuthorizationClientService) FindAuthorizationByID(ctx context.Context, id influxdb.ID) (*influxdb.Authorization, error) {
func (s *AuthorizationClientService) FindAuthorizationByID(ctx context.Context, id platform.ID) (*influxdb.Authorization, error) {
var b influxdb.Authorization
err := s.Client.
Get(prefixAuthorization, id.String()).
Expand All @@ -85,7 +87,7 @@ func (s *AuthorizationClientService) FindAuthorizationByID(ctx context.Context,
}

// UpdateAuthorization updates the status and description if available.
func (s *AuthorizationClientService) UpdateAuthorization(ctx context.Context, id influxdb.ID, upd *influxdb.AuthorizationUpdate) (*influxdb.Authorization, error) {
func (s *AuthorizationClientService) UpdateAuthorization(ctx context.Context, id platform.ID, upd *influxdb.AuthorizationUpdate) (*influxdb.Authorization, error) {
var res authResponse
err := s.Client.
PatchJSON(upd, prefixAuthorization, id.String()).
Expand All @@ -99,7 +101,7 @@ func (s *AuthorizationClientService) UpdateAuthorization(ctx context.Context, id
}

// DeleteAuthorization removes a authorization by id.
func (s *AuthorizationClientService) DeleteAuthorization(ctx context.Context, id influxdb.ID) error {
func (s *AuthorizationClientService) DeleteAuthorization(ctx context.Context, id platform.ID) error {
return s.Client.
Delete(prefixAuthorization, id.String()).
Do(ctx)
Expand Down
Loading

0 comments on commit 00afd95

Please sign in to comment.