Skip to content

Commit

Permalink
[core] Limit ODC error string length
Browse files Browse the repository at this point in the history
  • Loading branch information
teo committed Jun 27, 2024
1 parent 7189e9f commit 1e83c51
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
13 changes: 13 additions & 0 deletions common/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"runtime"
"strings"
"time"
"unicode/utf8"

"github.com/AliceO2Group/Control/common/logger"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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])
}
24 changes: 13 additions & 11 deletions core/integration/odc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions core/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
13 changes: 0 additions & 13 deletions core/serverutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ package core

import (
"fmt"
"unicode/utf8"

"github.com/AliceO2Group/Control/core/repos"

Expand Down Expand Up @@ -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])
}

0 comments on commit 1e83c51

Please sign in to comment.