Guard against length-0 slice in org_config get and FindOrCreate methods

Added ErrOrganizationConfigNotFound error
Refactored FindOrCreate to use helper get method
Ensure that FindOrCreate creates a new org config if config not found
and returns any other errors

Co-authored-by: Jared Scheib <jared.scheib@gmail.com>
pull/10616/head
Alirie Gray 2018-07-10 11:28:58 -07:00
parent d59e4763ec
commit 5b58c6aa59
4 changed files with 44 additions and 41 deletions

View File

@ -41,6 +41,9 @@ func (s *OrganizationConfigStore) Get(ctx context.Context, orgID string) (*chron
func (s *OrganizationConfigStore) get(ctx context.Context, tx *bolt.Tx, orgID string, cfg *chronograf.OrganizationConfig) error { func (s *OrganizationConfigStore) get(ctx context.Context, tx *bolt.Tx, orgID string, cfg *chronograf.OrganizationConfig) error {
v := tx.Bucket(OrganizationConfigBucket).Get([]byte(orgID)) v := tx.Bucket(OrganizationConfigBucket).Get([]byte(orgID))
if len(v) == 0 {
return chronograf.ErrOrganizationConfigNotFound
}
return internal.UnmarshalOrganizationConfig(v, cfg) return internal.UnmarshalOrganizationConfig(v, cfg)
} }
@ -48,12 +51,12 @@ func (s *OrganizationConfigStore) get(ctx context.Context, tx *bolt.Tx, orgID st
func (s *OrganizationConfigStore) FindOrCreate(ctx context.Context, orgID string) (*chronograf.OrganizationConfig, error) { func (s *OrganizationConfigStore) FindOrCreate(ctx context.Context, orgID string) (*chronograf.OrganizationConfig, error) {
var cfg chronograf.OrganizationConfig var cfg chronograf.OrganizationConfig
err := s.client.db.Update(func(tx *bolt.Tx) error { err := s.client.db.Update(func(tx *bolt.Tx) error {
v := tx.Bucket(OrganizationConfigBucket).Get([]byte(orgID)) err := s.get(ctx, tx, orgID, &cfg)
if v == nil { if err == chronograf.ErrOrganizationConfigNotFound {
cfg = newOrganizationConfig(orgID) cfg = newOrganizationConfig(orgID)
return s.update(ctx, tx, &cfg) return s.update(ctx, tx, &cfg)
} }
return internal.UnmarshalOrganizationConfig(v, &cfg) return err
}) })
if err != nil { if err != nil {

View File

@ -9,36 +9,36 @@ import (
// General errors. // General errors.
const ( const (
ErrUpstreamTimeout = Error("request to backend timed out") ErrUpstreamTimeout = Error("request to backend timed out")
ErrSourceNotFound = Error("source not found") ErrSourceNotFound = Error("source not found")
ErrServerNotFound = Error("server not found") ErrServerNotFound = Error("server not found")
ErrLayoutNotFound = Error("layout not found") ErrLayoutNotFound = Error("layout not found")
ErrDashboardNotFound = Error("dashboard not found") ErrDashboardNotFound = Error("dashboard not found")
ErrUserNotFound = Error("user not found") ErrUserNotFound = Error("user not found")
ErrLayoutInvalid = Error("layout is invalid") ErrLayoutInvalid = Error("layout is invalid")
ErrDashboardInvalid = Error("dashboard is invalid") ErrDashboardInvalid = Error("dashboard is invalid")
ErrSourceInvalid = Error("source is invalid") ErrSourceInvalid = Error("source is invalid")
ErrServerInvalid = Error("server is invalid") ErrServerInvalid = Error("server is invalid")
ErrAlertNotFound = Error("alert not found") ErrAlertNotFound = Error("alert not found")
ErrAuthentication = Error("user not authenticated") ErrAuthentication = Error("user not authenticated")
ErrUninitialized = Error("client uninitialized. Call Open() method") ErrUninitialized = Error("client uninitialized. Call Open() method")
ErrInvalidAxis = Error("Unexpected axis in cell. Valid axes are 'x', 'y', and 'y2'") ErrInvalidAxis = Error("Unexpected axis in cell. Valid axes are 'x', 'y', and 'y2'")
ErrInvalidColorType = Error("Invalid color type. Valid color types are 'min', 'max', 'threshold', 'text', and 'background'") ErrInvalidColorType = Error("Invalid color type. Valid color types are 'min', 'max', 'threshold', 'text', and 'background'")
ErrInvalidColor = Error("Invalid color. Accepted color format is #RRGGBB") ErrInvalidColor = Error("Invalid color. Accepted color format is #RRGGBB")
ErrInvalidLegend = Error("Invalid legend. Both type and orientation must be set") ErrInvalidLegend = Error("Invalid legend. Both type and orientation must be set")
ErrInvalidLegendType = Error("Invalid legend type. Valid legend type is 'static'") ErrInvalidLegendType = Error("Invalid legend type. Valid legend type is 'static'")
ErrInvalidLegendOrient = Error("Invalid orientation type. Valid orientation types are 'top', 'bottom', 'right', 'left'") ErrInvalidLegendOrient = Error("Invalid orientation type. Valid orientation types are 'top', 'bottom', 'right', 'left'")
ErrUserAlreadyExists = Error("user already exists") ErrUserAlreadyExists = Error("user already exists")
ErrOrganizationNotFound = Error("organization not found") ErrOrganizationNotFound = Error("organization not found")
ErrMappingNotFound = Error("mapping not found") ErrMappingNotFound = Error("mapping not found")
ErrOrganizationAlreadyExists = Error("organization already exists") ErrOrganizationAlreadyExists = Error("organization already exists")
ErrCannotDeleteDefaultOrganization = Error("cannot delete default organization") ErrCannotDeleteDefaultOrganization = Error("cannot delete default organization")
ErrConfigNotFound = Error("cannot find configuration") ErrConfigNotFound = Error("cannot find configuration")
ErrAnnotationNotFound = Error("annotation not found") ErrAnnotationNotFound = Error("annotation not found")
ErrInvalidCellOptionsText = Error("invalid text wrapping option. Valid wrappings are 'truncate', 'wrap', and 'single line'") ErrInvalidCellOptionsText = Error("invalid text wrapping option. Valid wrappings are 'truncate', 'wrap', and 'single line'")
ErrInvalidCellOptionsSort = Error("cell options sortby cannot be empty'") ErrInvalidCellOptionsSort = Error("cell options sortby cannot be empty'")
ErrInvalidCellOptionsColumns = Error("cell options columns cannot be empty'") ErrInvalidCellOptionsColumns = Error("cell options columns cannot be empty'")
ErrOrganizationConfigFindOrCreateFailed = Error("failed to find or create organization config") ErrOrganizationConfigNotFound = Error("could not find organization config")
) )
// Error is a domain error encountered while processing chronograf requests // Error is a domain error encountered while processing chronograf requests

View File

@ -13,7 +13,7 @@ var _ chronograf.OrganizationConfigStore = &OrganizationConfigStore{}
type OrganizationConfigStore struct{} type OrganizationConfigStore struct{}
func (s *OrganizationConfigStore) FindOrCreate(context.Context, string) (*chronograf.OrganizationConfig, error) { func (s *OrganizationConfigStore) FindOrCreate(context.Context, string) (*chronograf.OrganizationConfig, error) {
return nil, chronograf.ErrOrganizationConfigFindOrCreateFailed return nil, chronograf.ErrOrganizationConfigNotFound
} }
func (s *OrganizationConfigStore) Update(context.Context, *chronograf.OrganizationConfig) error { func (s *OrganizationConfigStore) Update(context.Context, *chronograf.OrganizationConfig) error {

View File

@ -152,7 +152,7 @@ func TestOrganizationConfig(t *testing.T) {
}, },
}, nil }, nil
default: default:
return nil, chronograf.ErrOrganizationConfigFindOrCreateFailed return nil, chronograf.ErrOrganizationConfigNotFound
} }
}, },
}, },
@ -254,7 +254,7 @@ func TestLogViewerOrganizationConfig(t *testing.T) {
}, },
}, nil }, nil
default: default:
return nil, chronograf.ErrOrganizationConfigFindOrCreateFailed return nil, chronograf.ErrOrganizationConfigNotFound
} }
}, },
}, },
@ -353,7 +353,7 @@ func TestReplaceLogViewerOrganizationConfig(t *testing.T) {
}, },
}, nil }, nil
default: default:
return nil, chronograf.ErrOrganizationConfigFindOrCreateFailed return nil, chronograf.ErrOrganizationConfigNotFound
} }
}, },
UpdateF: func(ctx context.Context, target *chronograf.OrganizationConfig) error { UpdateF: func(ctx context.Context, target *chronograf.OrganizationConfig) error {
@ -445,7 +445,7 @@ func TestReplaceLogViewerOrganizationConfig(t *testing.T) {
}, },
}, nil }, nil
default: default:
return nil, chronograf.ErrOrganizationConfigFindOrCreateFailed return nil, chronograf.ErrOrganizationConfigNotFound
} }
}, },
UpdateF: func(ctx context.Context, target *chronograf.OrganizationConfig) error { UpdateF: func(ctx context.Context, target *chronograf.OrganizationConfig) error {
@ -489,7 +489,7 @@ func TestReplaceLogViewerOrganizationConfig(t *testing.T) {
}, },
}, nil }, nil
default: default:
return nil, chronograf.ErrOrganizationConfigFindOrCreateFailed return nil, chronograf.ErrOrganizationConfigNotFound
} }
}, },
UpdateF: func(ctx context.Context, target *chronograf.OrganizationConfig) error { UpdateF: func(ctx context.Context, target *chronograf.OrganizationConfig) error {
@ -554,7 +554,7 @@ func TestReplaceLogViewerOrganizationConfig(t *testing.T) {
}, },
}, nil }, nil
default: default:
return nil, chronograf.ErrOrganizationConfigFindOrCreateFailed return nil, chronograf.ErrOrganizationConfigNotFound
} }
}, },
UpdateF: func(ctx context.Context, target *chronograf.OrganizationConfig) error { UpdateF: func(ctx context.Context, target *chronograf.OrganizationConfig) error {
@ -624,7 +624,7 @@ func TestReplaceLogViewerOrganizationConfig(t *testing.T) {
}, },
}, nil }, nil
default: default:
return nil, chronograf.ErrOrganizationConfigFindOrCreateFailed return nil, chronograf.ErrOrganizationConfigNotFound
} }
}, },
UpdateF: func(ctx context.Context, target *chronograf.OrganizationConfig) error { UpdateF: func(ctx context.Context, target *chronograf.OrganizationConfig) error {