diff --git a/cmd/kubectl-testkube/commands/testworkflows/run.go b/cmd/kubectl-testkube/commands/testworkflows/run.go index cd29c1496e..d769014225 100644 --- a/cmd/kubectl-testkube/commands/testworkflows/run.go +++ b/cmd/kubectl-testkube/commands/testworkflows/run.go @@ -15,6 +15,7 @@ import ( "github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common/render" "github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/tests" "github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/testworkflows/renderer" + testkubecfg "github.com/kubeshop/testkube/cmd/kubectl-testkube/config" "github.com/kubeshop/testkube/cmd/testworkflow-init/instructions" apiclientv1 "github.com/kubeshop/testkube/pkg/api/v1/client" "github.com/kubeshop/testkube/pkg/api/v1/testkube" @@ -71,13 +72,18 @@ func NewRunTestWorkflowCmd() *cobra.Command { runContext = "" interfaceType = testkube.CICD_TestWorkflowRunningContextInterfaceType } + + cfg, err := testkubecfg.Load() + ui.ExitOnError("loading config file", err) + ui.NL() + execution, err := client.ExecuteTestWorkflow(name, testkube.TestWorkflowExecutionRequest{ Name: executionName, Config: config, DisableWebhooks: disableWebhooks, Tags: tags, // Pro edition only (tcl protected code) - RunningContext: tclcmd.GetRunningContext(runContext, "", "", interfaceType), + RunningContext: tclcmd.GetRunningContext(runContext, cfg.CloudContext.ApiKey, interfaceType), }, ) if err != nil { diff --git a/pkg/tcl/testworkflowstcl/cmd/cmd.go b/pkg/tcl/testworkflowstcl/cmd/cmd.go index ca610a8b77..de0c075760 100644 --- a/pkg/tcl/testworkflowstcl/cmd/cmd.go +++ b/pkg/tcl/testworkflowstcl/cmd/cmd.go @@ -9,11 +9,30 @@ package cmd import ( + "encoding/base64" + "encoding/json" + "fmt" + "strings" + "github.com/kubeshop/testkube/internal/common" "github.com/kubeshop/testkube/pkg/api/v1/testkube" ) -func GetRunningContext(runContext, username, email string, interfaceType testkube.TestWorkflowRunningContextInterfaceType) *testkube.TestWorkflowRunningContext { +func GetRunningContext(runContext, token string, interfaceType testkube.TestWorkflowRunningContextInterfaceType) *testkube.TestWorkflowRunningContext { + var username, email string + if token != "" { + payload, err := getJWTPayload(token) + if err == nil { + if value, ok := payload["userame"]; ok { + username = fmt.Sprint(value) + } + + if value, ok := payload["email"]; ok { + email = fmt.Sprint(value) + } + } + } + return &testkube.TestWorkflowRunningContext{ Interface_: &testkube.TestWorkflowRunningContextInterface{ Name: runContext, @@ -26,3 +45,32 @@ func GetRunningContext(runContext, username, email string, interfaceType testkub }, } } + +func base64Decode(encoded string) (string, error) { + decoded, err := base64.RawURLEncoding.DecodeString(encoded) + if err != nil { + return "", err + } + return string(decoded), nil +} + +func getJWTPayload(token string) (map[string]interface{}, error) { + parts := strings.Split(token, ".") + if len(parts) != 3 { + return nil, fmt.Errorf("invalid token format") + } + + // Decode the payload + payloadJSON, err := base64Decode(parts[1]) + if err != nil { + return nil, fmt.Errorf("failed to decode payload: %v", err) + } + + // Unmarshal the payload into maps + var payload map[string]interface{} + if err := json.Unmarshal([]byte(payloadJSON), &payload); err != nil { + return nil, fmt.Errorf("failed to parse payload JSON: %v", err) + } + + return payload, nil +}