Skip to content

Commit 1e83c51

Browse files
committed
[core] Limit ODC error string length
1 parent 7189e9f commit 1e83c51

4 files changed

Lines changed: 28 additions & 26 deletions

File tree

common/utils/utils.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"runtime"
3535
"strings"
3636
"time"
37+
"unicode/utf8"
3738

3839
"github.com/AliceO2Group/Control/common/logger"
3940
"github.com/sirupsen/logrus"
@@ -166,3 +167,15 @@ func ParseExtraVars(extraVars string) (extraVarsMap map[string]string, err error
166167
}
167168
return
168169
}
170+
171+
func TruncateString(str string, length int) string {
172+
if length <= 0 {
173+
return ""
174+
}
175+
176+
if utf8.RuneCountInString(str) < length {
177+
return str
178+
}
179+
180+
return string([]rune(str)[:length])
181+
}

core/integration/odc/handlers.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ import (
4747
"google.golang.org/grpc/status"
4848
)
4949

50+
const ODC_ERROR_MAX_LENGTH = 250
51+
5052
func handleGetState(ctx context.Context, odcClient *RpcClient, envId string) (string, error) {
5153
defer utils.TimeTrackFunction(time.Now(), log.WithPrefix("odcclient"))
5254
req := &odcpb.StateRequest{
@@ -78,7 +80,7 @@ func handleGetState(ctx context.Context, odcClient *RpcClient, envId string) (st
7880
newState = rep.Reply.State
7981

8082
if odcErr := rep.Reply.GetError(); odcErr != nil {
81-
return newState, fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg())
83+
return newState, fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH))
8284
}
8385
if replyStatus := rep.Reply.Status; replyStatus != odcpb.ReplyStatus_SUCCESS {
8486
return newState, fmt.Errorf("status %s from ODC", replyStatus.String())
@@ -188,7 +190,7 @@ func handleStart(ctx context.Context, odcClient *RpcClient, arguments map[string
188190
WithError(err).
189191
Debugf("finished call odc.SetProperties, ERROR in response payload")
190192

191-
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg())
193+
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH))
192194

193195
payload["odcResponse"] = &setPropertiesResponse
194196
payloadJson, _ = json.Marshal(payload)
@@ -318,7 +320,7 @@ func handleStart(ctx context.Context, odcClient *RpcClient, arguments map[string
318320
rep.Reply.Hosts = nil
319321

320322
if odcErr := rep.Reply.GetError(); odcErr != nil {
321-
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg())
323+
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH))
322324

323325
payload["odcResponse"] = &rep
324326
payloadJson, _ = json.Marshal(payload)
@@ -454,7 +456,7 @@ func handleStop(ctx context.Context, odcClient *RpcClient, arguments map[string]
454456
rep.Reply.Hosts = nil
455457

456458
if odcErr := rep.Reply.GetError(); odcErr != nil {
457-
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg())
459+
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH))
458460

459461
payload["odcResponse"] = &rep
460462
payloadJson, _ = json.Marshal(payload)
@@ -640,7 +642,7 @@ func handleCleanup(ctx context.Context, odcClient *RpcClient, arguments map[stri
640642
}
641643

642644
if odcErr := rep.GetError(); odcErr != nil {
643-
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg())
645+
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH))
644646

645647
payload["odcResponse"] = &rep
646648
payloadJson, _ = json.Marshal(payload)
@@ -845,7 +847,7 @@ func doReset(ctx context.Context, odcClient *RpcClient, arguments map[string]str
845847
rep.Reply.Hosts = nil
846848

847849
if odcErr := rep.Reply.GetError(); odcErr != nil {
848-
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg())
850+
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH))
849851

850852
payload["odcResponse"] = &rep
851853
payloadJson, _ = json.Marshal(payload)
@@ -975,7 +977,7 @@ func doTerminate(ctx context.Context, odcClient *RpcClient, arguments map[string
975977
rep.Reply.Hosts = nil
976978

977979
if odcErr := rep.Reply.GetError(); odcErr != nil {
978-
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg())
980+
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH))
979981

980982
payload["odcResponse"] = &rep
981983
payloadJson, _ = json.Marshal(payload)
@@ -1100,7 +1102,7 @@ func doShutdown(ctx context.Context, odcClient *RpcClient, arguments map[string]
11001102
shutdownResponse.Hosts = nil
11011103

11021104
if odcErr := shutdownResponse.GetError(); odcErr != nil {
1103-
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg())
1105+
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH))
11041106

11051107
payload["odcResponse"] = &shutdownResponse
11061108
payloadJson, _ = json.Marshal(payload)
@@ -1283,7 +1285,7 @@ func handleRun(ctx context.Context, odcClient *RpcClient, isManualXml bool, argu
12831285
runResponse.Hosts = nil
12841286

12851287
if odcErr := runResponse.GetError(); odcErr != nil {
1286-
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg())
1288+
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH))
12871289
}
12881290
if replyStatus := runResponse.Status; replyStatus != odcpb.ReplyStatus_SUCCESS {
12891291
payload["odcResponse"] = &runResponse
@@ -1427,7 +1429,7 @@ func handleConfigure(ctx context.Context, odcClient *RpcClient, arguments map[st
14271429
WithError(err).
14281430
Debugf("finished call odc.SetProperties, ERROR in response payload")
14291431

1430-
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg())
1432+
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH))
14311433

14321434
payload["odcResponse"] = &setPropertiesResponse
14331435
payloadJson, _ = json.Marshal(payload)
@@ -1558,7 +1560,7 @@ func handleConfigure(ctx context.Context, odcClient *RpcClient, arguments map[st
15581560
configureResponse.Reply.Hosts = nil
15591561

15601562
if odcErr := configureResponse.Reply.GetError(); odcErr != nil {
1561-
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), odcErr.GetMsg())
1563+
err = fmt.Errorf("code %d from ODC: %s", odcErr.GetCode(), utils.TruncateString(odcErr.GetMsg(), ODC_ERROR_MAX_LENGTH))
15621564

15631565
payload["odcResponse"] = &configureResponse
15641566
payloadJson, _ = json.Marshal(payload)

core/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func (m *RpcServer) NewEnvironment(cxt context.Context, request *pb.NewEnvironme
380380
id := uid.New()
381381
id, err = m.state.environments.CreateEnvironment(request.GetWorkflowTemplate(), request.GetVars(), request.GetPublic(), id, request.GetAutoTransition())
382382
if err != nil {
383-
st := status.Newf(codes.Internal, "cannot create new environment: %s", TruncateString(err.Error(), MAX_ERROR_LENGTH))
383+
st := status.Newf(codes.Internal, "cannot create new environment: %s", utils.TruncateString(err.Error(), MAX_ERROR_LENGTH))
384384
ei := &pb.EnvironmentInfo{
385385
Id: id.String(),
386386
CreatedWhen: time.Now().UnixMilli(),
@@ -395,7 +395,7 @@ func (m *RpcServer) NewEnvironment(cxt context.Context, request *pb.NewEnvironme
395395

396396
newEnv, err := m.state.environments.Environment(id)
397397
if err != nil {
398-
st := status.Newf(codes.Internal, "cannot get newly created environment: %s", TruncateString(err.Error(), MAX_ERROR_LENGTH))
398+
st := status.Newf(codes.Internal, "cannot get newly created environment: %s", utils.TruncateString(err.Error(), MAX_ERROR_LENGTH))
399399
ei := &pb.EnvironmentInfo{
400400
Id: id.String(),
401401
CreatedWhen: time.Now().UnixMilli(),

core/serverutil.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ package core
2626

2727
import (
2828
"fmt"
29-
"unicode/utf8"
3029

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

@@ -237,15 +236,3 @@ func convertVarTypeStringToEnum(varType string) pb.VarSpecMessage_Type {
237236
}
238237
return pb.VarSpecMessage_Type(msgType)
239238
}
240-
241-
func TruncateString(str string, length int) string {
242-
if length <= 0 {
243-
return ""
244-
}
245-
246-
if utf8.RuneCountInString(str) < length {
247-
return str
248-
}
249-
250-
return string([]rune(str)[:length])
251-
}

0 commit comments

Comments
 (0)