From 128942746ed64de84f38149077d42399e0788841 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Wed, 26 Jun 2024 11:13:22 +0200 Subject: [PATCH] [core] Limit ODC error string length --- common/utils/utils.go | 13 +++++++++++++ core/integration/odc/handlers.go | 24 +++++++++++++----------- core/server.go | 4 ++-- core/serverutil.go | 13 ------------- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/common/utils/utils.go b/common/utils/utils.go index 12faa43e4..7a0435cee 100644 --- a/common/utils/utils.go +++ b/common/utils/utils.go @@ -34,6 +34,7 @@ import ( "runtime" "strings" "time" + "unicode/utf8" "github.com/AliceO2Group/Control/common/logger" "github.com/sirupsen/logrus" @@ -166,3 +167,15 @@ func ParseExtraVars(extraVars string) (extraVarsMap map[string]string, err error } return } + +func TruncateString(str string, length int) string { + if length <= 0 { + return "" + } + + if utf8.RuneCountInString(str) < length { + return str + } + + return string([]rune(str)[:length]) +} diff --git a/core/integration/odc/handlers.go b/core/integration/odc/handlers.go index 9555d0ed2..6b7260da1 100644 --- a/core/integration/odc/handlers.go +++ b/core/integration/odc/handlers.go @@ -47,6 +47,8 @@ import ( "google.golang.org/grpc/status" ) +const ODC_ERROR_MAX_LENGTH = 250 + func handleGetState(ctx context.Context, odcClient *RpcClient, envId string) (string, error) { defer utils.TimeTrackFunction(time.Now(), log.WithPrefix("odcclient")) req := &odcpb.StateRequest{ @@ -78,7 +80,7 @@ func handleGetState(ctx context.Context, odcClient *RpcClient, envId string) (st newState = rep.Reply.State if odcErr := rep.Reply.GetError(); odcErr != nil { - return newState, fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg()) + return newState, fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH)) } if replyStatus := rep.Reply.Status; replyStatus != odcpb.ReplyStatus_SUCCESS { return newState, fmt.Errorf("status %s from ODC", replyStatus.String()) @@ -188,7 +190,7 @@ func handleStart(ctx context.Context, odcClient *RpcClient, arguments map[string WithError(err). Debugf("finished call odc.SetProperties, ERROR in response payload") - err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg()) + err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH)) payload["odcResponse"] = &setPropertiesResponse payloadJson, _ = json.Marshal(payload) @@ -318,7 +320,7 @@ func handleStart(ctx context.Context, odcClient *RpcClient, arguments map[string rep.Reply.Hosts = nil if odcErr := rep.Reply.GetError(); odcErr != nil { - err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg()) + err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH)) payload["odcResponse"] = &rep payloadJson, _ = json.Marshal(payload) @@ -454,7 +456,7 @@ func handleStop(ctx context.Context, odcClient *RpcClient, arguments map[string] rep.Reply.Hosts = nil if odcErr := rep.Reply.GetError(); odcErr != nil { - err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg()) + err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH)) payload["odcResponse"] = &rep payloadJson, _ = json.Marshal(payload) @@ -640,7 +642,7 @@ func handleCleanup(ctx context.Context, odcClient *RpcClient, arguments map[stri } if odcErr := rep.GetError(); odcErr != nil { - err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg()) + err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH)) payload["odcResponse"] = &rep payloadJson, _ = json.Marshal(payload) @@ -845,7 +847,7 @@ func doReset(ctx context.Context, odcClient *RpcClient, arguments map[string]str rep.Reply.Hosts = nil if odcErr := rep.Reply.GetError(); odcErr != nil { - err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg()) + err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH)) payload["odcResponse"] = &rep payloadJson, _ = json.Marshal(payload) @@ -975,7 +977,7 @@ func doTerminate(ctx context.Context, odcClient *RpcClient, arguments map[string rep.Reply.Hosts = nil if odcErr := rep.Reply.GetError(); odcErr != nil { - err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg()) + err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH)) payload["odcResponse"] = &rep payloadJson, _ = json.Marshal(payload) @@ -1100,7 +1102,7 @@ func doShutdown(ctx context.Context, odcClient *RpcClient, arguments map[string] shutdownResponse.Hosts = nil if odcErr := shutdownResponse.GetError(); odcErr != nil { - err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg()) + err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH)) payload["odcResponse"] = &shutdownResponse payloadJson, _ = json.Marshal(payload) @@ -1283,7 +1285,7 @@ func handleRun(ctx context.Context, odcClient *RpcClient, isManualXml bool, argu runResponse.Hosts = nil if odcErr := runResponse.GetError(); odcErr != nil { - err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg()) + err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH)) } if replyStatus := runResponse.Status; replyStatus != odcpb.ReplyStatus_SUCCESS { payload["odcResponse"] = &runResponse @@ -1427,7 +1429,7 @@ func handleConfigure(ctx context.Context, odcClient *RpcClient, arguments map[st WithError(err). Debugf("finished call odc.SetProperties, ERROR in response payload") - err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg()) + err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH)) payload["odcResponse"] = &setPropertiesResponse payloadJson, _ = json.Marshal(payload) @@ -1558,7 +1560,7 @@ func handleConfigure(ctx context.Context, odcClient *RpcClient, arguments map[st configureResponse.Reply.Hosts = nil if odcErr := configureResponse.Reply.GetError(); odcErr != nil { - err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg()) + err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH)) payload["odcResponse"] = &configureResponse payloadJson, _ = json.Marshal(payload) diff --git a/core/server.go b/core/server.go index 571719108..18b144b76 100644 --- a/core/server.go +++ b/core/server.go @@ -380,7 +380,7 @@ func (m *RpcServer) NewEnvironment(cxt context.Context, request *pb.NewEnvironme id := uid.New() id, err = m.state.environments.CreateEnvironment(request.GetWorkflowTemplate(), request.GetVars(), request.GetPublic(), id, request.GetAutoTransition()) if err != nil { - st := status.Newf(codes.Internal, "cannot create new environment: %s", TruncateString(err.Error(), MAX_ERROR_LENGTH)) + st := status.Newf(codes.Internal, "cannot create new environment: %s", utils.TruncateString(err.Error(), MAX_ERROR_LENGTH)) ei := &pb.EnvironmentInfo{ Id: id.String(), CreatedWhen: time.Now().UnixMilli(), @@ -395,7 +395,7 @@ func (m *RpcServer) NewEnvironment(cxt context.Context, request *pb.NewEnvironme newEnv, err := m.state.environments.Environment(id) if err != nil { - st := status.Newf(codes.Internal, "cannot get newly created environment: %s", TruncateString(err.Error(), MAX_ERROR_LENGTH)) + st := status.Newf(codes.Internal, "cannot get newly created environment: %s", utils.TruncateString(err.Error(), MAX_ERROR_LENGTH)) ei := &pb.EnvironmentInfo{ Id: id.String(), CreatedWhen: time.Now().UnixMilli(), diff --git a/core/serverutil.go b/core/serverutil.go index 81c63829f..fd9ed49ea 100644 --- a/core/serverutil.go +++ b/core/serverutil.go @@ -26,7 +26,6 @@ package core import ( "fmt" - "unicode/utf8" "github.com/AliceO2Group/Control/core/repos" @@ -237,15 +236,3 @@ func convertVarTypeStringToEnum(varType string) pb.VarSpecMessage_Type { } return pb.VarSpecMessage_Type(msgType) } - -func TruncateString(str string, length int) string { - if length <= 0 { - return "" - } - - if utf8.RuneCountInString(str) < length { - return str - } - - return string([]rune(str)[:length]) -}