From 992a8565664246a543bb534a87acc8a532013cf1 Mon Sep 17 00:00:00 2001 From: Anton Kosyakov Date: Thu, 16 Sep 2021 08:24:00 +0000 Subject: [PATCH] fix #5699: fix parisng of tasks env vars in go --- components/gitpod-protocol/go/gitpod-service.go | 16 ++++++++-------- components/gitpod-protocol/src/protocol.ts | 2 +- components/supervisor/pkg/supervisor/config.go | 16 ++++++++-------- components/supervisor/pkg/supervisor/tasks.go | 11 ++++++++++- .../supervisor/pkg/supervisor/tasks_test.go | 16 ++++++++++++++++ 5 files changed, 43 insertions(+), 18 deletions(-) diff --git a/components/gitpod-protocol/go/gitpod-service.go b/components/gitpod-protocol/go/gitpod-service.go index cbca601fb6b9b3..c5a827de840b7f 100644 --- a/components/gitpod-protocol/go/gitpod-service.go +++ b/components/gitpod-protocol/go/gitpod-service.go @@ -1827,14 +1827,14 @@ type ResolvePluginsParams struct { // TaskConfig is the TaskConfig message type type TaskConfig struct { - Before string `json:"before,omitempty"` - Command string `json:"command,omitempty"` - Env map[string]string `json:"env,omitempty"` - Init string `json:"init,omitempty"` - Name string `json:"name,omitempty"` - OpenIn string `json:"openIn,omitempty"` - OpenMode string `json:"openMode,omitempty"` - Prebuild string `json:"prebuild,omitempty"` + Before string `json:"before,omitempty"` + Command string `json:"command,omitempty"` + Env map[string]interface{} `json:"env,omitempty"` + Init string `json:"init,omitempty"` + Name string `json:"name,omitempty"` + OpenIn string `json:"openIn,omitempty"` + OpenMode string `json:"openMode,omitempty"` + Prebuild string `json:"prebuild,omitempty"` } // VSCodeConfig is the VSCodeConfig message type diff --git a/components/gitpod-protocol/src/protocol.ts b/components/gitpod-protocol/src/protocol.ts index 1259732cbf070c..7579e21f37e444 100644 --- a/components/gitpod-protocol/src/protocol.ts +++ b/components/gitpod-protocol/src/protocol.ts @@ -705,7 +705,7 @@ export interface TaskConfig { init?: string; prebuild?: string; command?: string; - env?: { [env: string]: string }; + env?: { [env: string]: any }; openIn?: 'bottom' | 'main' | 'left' | 'right'; openMode?: 'split-top' | 'split-left' | 'split-right' | 'split-bottom' | 'tab-before' | 'tab-after'; } diff --git a/components/supervisor/pkg/supervisor/config.go b/components/supervisor/pkg/supervisor/config.go index a9ccf82d9ef24a..8e51ca2f7a3342 100644 --- a/components/supervisor/pkg/supervisor/config.go +++ b/components/supervisor/pkg/supervisor/config.go @@ -224,14 +224,14 @@ type WorkspaceGitpodToken struct { // TaskConfig defines gitpod task shape type TaskConfig struct { - Name *string `json:"name,omitempty"` - Before *string `json:"before,omitempty"` - Init *string `json:"init,omitempty"` - Prebuild *string `json:"prebuild,omitempty"` - Command *string `json:"command,omitempty"` - Env *map[string]string `json:"env,omitempty"` - OpenIn *string `json:"openIn,omitempty"` - OpenMode *string `json:"openMode,omitempty"` + Name *string `json:"name,omitempty"` + Before *string `json:"before,omitempty"` + Init *string `json:"init,omitempty"` + Prebuild *string `json:"prebuild,omitempty"` + Command *string `json:"command,omitempty"` + Env *map[string]interface{} `json:"env,omitempty"` + OpenIn *string `json:"openIn,omitempty"` + OpenMode *string `json:"openMode,omitempty"` } // Validate validates this configuration diff --git a/components/supervisor/pkg/supervisor/tasks.go b/components/supervisor/pkg/supervisor/tasks.go index ccabefdf979a88..c1958fb1bbc616 100644 --- a/components/supervisor/pkg/supervisor/tasks.go +++ b/components/supervisor/pkg/supervisor/tasks.go @@ -7,6 +7,7 @@ package supervisor import ( "bufio" "context" + "encoding/json" "fmt" "io" "os" @@ -239,7 +240,15 @@ func (tm *tasksManager) Run(ctx context.Context, wg *sync.WaitGroup, successChan taskLog.Info("starting a task terminal...") openRequest := &api.OpenTerminalRequest{} if t.config.Env != nil { - openRequest.Env = *t.config.Env + openRequest.Env = make(map[string]string, len(*t.config.Env)) + for key, value := range *t.config.Env { + v, err := json.Marshal(value) + if err != nil { + taskLog.WithError(err).WithField("key", key).Error("cannot marshal env var") + } else { + openRequest.Env[key] = string(v) + } + } } var readTimeout time.Duration if !tm.config.isHeadless() { diff --git a/components/supervisor/pkg/supervisor/tasks_test.go b/components/supervisor/pkg/supervisor/tasks_test.go index 66da42177b642d..6abe7e6145bdd7 100644 --- a/components/supervisor/pkg/supervisor/tasks_test.go +++ b/components/supervisor/pkg/supervisor/tasks_test.go @@ -24,6 +24,11 @@ import ( var skipCommand = "echo \"skip\"" var failCommand = "exit 1" +var testEnv = &map[string]interface{}{ + "object": map[string]interface{}{"baz": 3}, +} +var testEnvCommand = `test $object == "{\"baz\":3}"` + func TestTaskManager(t *testing.T) { log.Log.Logger.SetLevel(logrus.FatalLevel) tests := []struct { @@ -99,6 +104,17 @@ func TestTaskManager(t *testing.T) { Success: false, }, }, + { + Desc: "env var parsing", + Headless: true, + Source: csapi.WorkspaceInitFromOther, + GitpodTasks: &[]TaskConfig{{Init: &testEnvCommand, Env: testEnv}}, + + ExpectedReporter: testHeadlessTaskProgressReporter{ + Done: true, + Success: true, + }, + }, } for _, test := range tests { t.Run(test.Desc, func(t *testing.T) {