diff --git a/pkg/server/domain/service/authentication.go b/pkg/server/domain/service/authentication.go index 056d01f16..435e81925 100644 --- a/pkg/server/domain/service/authentication.go +++ b/pkg/server/domain/service/authentication.go @@ -492,8 +492,14 @@ func (d *dexHandlerImpl) login(ctx context.Context) (*apisv1.UserBase, error) { klog.Errorf("failed to get the system info %s", err.Error()) } user := &model.User{ - Email: claims.Email, - Name: strings.ToLower(claims.Sub), + Email: claims.Email, + Name: func() string { + sub := strings.ToLower(claims.Sub) + if len(sub) > datastore.PrimaryKeyMaxLength { + return sub[:datastore.PrimaryKeyMaxLength] + } + return sub + }(), DexSub: claims.Sub, Alias: claims.Name, LastLoginTime: time.Now(), diff --git a/pkg/server/infrastructure/datastore/datastore.go b/pkg/server/infrastructure/datastore/datastore.go index 0f1ec846d..4ae501eed 100644 --- a/pkg/server/infrastructure/datastore/datastore.go +++ b/pkg/server/infrastructure/datastore/datastore.go @@ -46,6 +46,9 @@ var ( ErrEntityInvalid = NewDBError(fmt.Errorf("entity is invalid")) ) +// PrimaryKeyMaxLength The primary key length should be limited when the datastore is kube-api +var PrimaryKeyMaxLength = 31 + // DBError datastore error type DBError struct { err error diff --git a/pkg/server/interfaces/api/validate.go b/pkg/server/interfaces/api/validate.go index c66bce229..a09077ce1 100644 --- a/pkg/server/interfaces/api/validate.go +++ b/pkg/server/interfaces/api/validate.go @@ -23,6 +23,7 @@ import ( "github.com/go-playground/validator/v10" "github.com/kubevela/velaux/pkg/server/domain/service" + "github.com/kubevela/velaux/pkg/server/infrastructure/datastore" ) var validate = validator.New() @@ -72,7 +73,7 @@ func ValidatePayloadType(fl validator.FieldLevel) bool { // ValidateName custom check name field func ValidateName(fl validator.FieldLevel) bool { value := fl.Field().String() - if len(value) > 31 || len(value) < 2 { + if len(value) > datastore.PrimaryKeyMaxLength || len(value) < 2 { return false } return nameRegexp.MatchString(value)