Skip to content

Commit

Permalink
Added base64 encoding to send executionData from subscriber to GQL
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Chaudhary <shubham.chaudhary@harness.io>
  • Loading branch information
ispeakc0de committed Jan 13, 2023
1 parent 4a7fc43 commit dc5dd94
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
8 changes: 5 additions & 3 deletions litmus-portal/cluster-agents/subscriber/pkg/events/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package events
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"regexp"
"strconv"
Expand All @@ -12,7 +13,6 @@ import (
v1alpha13 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
wfclientset "github.com/argoproj/argo-workflows/v3/pkg/client/clientset/versioned"
v1alpha12 "github.com/litmuschaos/chaos-operator/pkg/client/clientset/versioned/typed/litmuschaos/v1alpha1"
"github.com/litmuschaos/litmus/litmus-portal/cluster-agents/subscriber/pkg/graphql"
"github.com/litmuschaos/litmus/litmus-portal/cluster-agents/subscriber/pkg/k8s"
"github.com/litmuschaos/litmus/litmus-portal/cluster-agents/subscriber/pkg/types"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -161,11 +161,13 @@ func GenerateWorkflowPayload(cid, accessKey, version, completed string, wfEvent

wfEvent.ExecutedBy = URLDecodeBase64(wfEvent.ExecutedBy)

processed, err := graphql.MarshalGQLData(wfEvent)
data, err := json.Marshal(wfEvent)
if err != nil {
return nil, err
}
mutation := `{ workflowID: \"` + wfEvent.WorkflowID + `\", workflowRunID: \"` + wfEvent.UID + `\", completed: ` + completed + `, workflowName:\"` + wfEvent.Name + `\", clusterID: ` + clusterID + `, executedBy:\"` + wfEvent.ExecutedBy + `\", executionData:\"` + processed[1:len(processed)-1] + `\"}`

executionData := base64.StdEncoding.EncodeToString(data)
mutation := `{ workflowID: \"` + wfEvent.WorkflowID + `\", workflowRunID: \"` + wfEvent.UID + `\", completed: ` + completed + `, workflowName:\"` + wfEvent.Name + `\", clusterID: ` + clusterID + `, executedBy:\"` + wfEvent.ExecutedBy + `\", executionData:\"` + executionData + `\"}`
var payload = []byte(`{"query":"mutation { chaosWorkflowRun(request:` + mutation + ` )}"}`)
return payload, nil
}
Expand Down
29 changes: 23 additions & 6 deletions litmus-portal/graphql-server/pkg/chaos-workflow/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package handler

import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"log"
"strconv"
"strings"
"time"

"github.com/sirupsen/logrus"

"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/config"

"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/authorization"
Expand Down Expand Up @@ -699,18 +702,32 @@ func ListWorkflows(request model.ListWorkflowsRequest) (*model.ListWorkflowsResp

// ChaosWorkflowRun Updates or Inserts a new Workflow Run into the DB
func ChaosWorkflowRun(request model.WorkflowRunRequest, r store.StateData) (string, error) {
var (
executionData types.ExecutionData
exeData []byte
)

cluster, err := cluster.VerifyCluster(*request.ClusterID)
if err != nil {
log.Println("ERROR", err)
return "", err
}

// Parse and store execution data
var executionData types.ExecutionData
err = json.Unmarshal([]byte(request.ExecutionData), &executionData)
if err != nil {
log.Println("Can not parse Execution Data of workflow run with id: ", request.WorkflowRunID)
return "", err
if request.ExecutionData != "" {
exeData, err = base64.StdEncoding.DecodeString(request.ExecutionData)
if err != nil {
logrus.Warn("Failed to decode execution data: ", err)

//Required for backward compatibility of subscribers
//which are not sending execution data in base64 encoded format
exeData = []byte(request.ExecutionData)
}
err = json.Unmarshal(exeData, &executionData)
if err != nil {
logrus.Error("Failed to unmarshal execution data: ", err)
return "", err
}
}

var workflowRunMetrics types.WorkflowRunMetrics
Expand All @@ -735,7 +752,7 @@ func ChaosWorkflowRun(request model.WorkflowRunRequest, r store.StateData) (stri
ExperimentsStopped: &workflowRunMetrics.ExperimentsStopped,
ExperimentsNA: &workflowRunMetrics.ExperimentsNA,
TotalExperiments: &workflowRunMetrics.TotalExperiments,
ExecutionData: request.ExecutionData,
ExecutionData: string(exeData),
Completed: request.Completed,
IsRemoved: &isRemoved,
ExecutedBy: request.ExecutedBy,
Expand Down

0 comments on commit dc5dd94

Please sign in to comment.