Path: blob/main/test/tests/components/ws-manager/protected_secrets_test.go
2500 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package wsmanager56import (7"context"8"fmt"9"strings"10"testing"11"time"1213"sigs.k8s.io/e2e-framework/pkg/envconf"14"sigs.k8s.io/e2e-framework/pkg/features"1516csapi "github.com/gitpod-io/gitpod/content-service/api"17agent "github.com/gitpod-io/gitpod/test/pkg/agent/workspace/api"18"github.com/gitpod-io/gitpod/test/pkg/integration"19wsmanapi "github.com/gitpod-io/gitpod/ws-manager/api"20corev1 "k8s.io/api/core/v1"21)2223const (24SECRET_NAME = "USER_SECRET"25SECRET_VALUE = "a9upr238"26)2728func TestProtectedSecrets(t *testing.T) {29f := features.New("protected_secrets").WithLabel("component", "ws-manager").Assess("can use protected secrets", func(testCtx context.Context, t *testing.T, cfg *envconf.Config) context.Context {30t.Parallel()3132ctx, cancel := context.WithTimeout(testCtx, 5*time.Minute)33defer cancel()3435api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())36t.Cleanup(func() {37api.Done(t)38})3940swr := func(req *wsmanapi.StartWorkspaceRequest) error {41req.Spec.Envvars = append(req.Spec.Envvars, &wsmanapi.EnvironmentVariable{42Name: SECRET_NAME,43Value: SECRET_VALUE,44})4546req.Spec.Initializer = &csapi.WorkspaceInitializer{47Spec: &csapi.WorkspaceInitializer_Git{48Git: &csapi.GitInitializer{49RemoteUri: "https://github.com/gitpod-io/empty",50CheckoutLocation: "empty",51Config: &csapi.GitConfig{},52},53},54}5556req.Spec.WorkspaceLocation = "empty"57return nil58}5960ws, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(swr))61if err != nil {62t.Fatalf("cannot launch a workspace: %q", err)63}6465t.Cleanup(func() {66sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)67defer scancel()6869sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())70defer sapi.Done(t)7172_, err = stopWs(true, sapi)73if err != nil {74t.Errorf("cannot stop workspace: %q", err)75}76})7778k8sClient := cfg.Client()79var wsPod corev1.Pod80if err := k8sClient.Resources().Get(context.Background(), "ws-"+ws.Req.Id, cfg.Namespace(), &wsPod); err != nil {81t.Fatal(err)82}8384assertEnvSuppliedBySecret(t, &wsPod, SECRET_NAME)8586rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(),87integration.WithInstanceID(ws.Req.Id),88integration.WithContainer("workspace"),89integration.WithWorkspacekitLift(true),90)91if err != nil {92t.Fatal(err)93}9495assertEnvAvailableInWs(t, rsa)9697integration.DeferCloser(t, closer)98defer rsa.Close()99100return testCtx101}).Feature()102103testEnv.Test(t, f)104}105106func assertEnvSuppliedBySecret(t *testing.T, wsPod *corev1.Pod, secretEnv string) {107for _, c := range wsPod.Spec.Containers {108if c.Name != "workspace" {109continue110}111112for _, env := range c.Env {113if env.Name == secretEnv {114if env.Value != "" {115t.Fatalf("environment variable has plain text value")116}117118if env.ValueFrom == nil || env.ValueFrom.SecretKeyRef == nil {119t.Fatalf("environment variable value is not supplied by secret")120}121122expectedName := fmt.Sprintf("%s-env", strings.TrimPrefix(wsPod.Name, "ws-"))123if env.ValueFrom.SecretKeyRef.Name != expectedName {124t.Fatalf("expected environment variable values are not supplied by secret %s", expectedName)125}126}127}128}129}130131func assertEnvAvailableInWs(t *testing.T, rsa *integration.RpcClient) {132var grepResp agent.ExecResponse133err := rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{134Dir: prebuildLogPath,135Command: "bash",136Args: []string{137"-c",138fmt.Sprintf("env | grep %s", SECRET_NAME),139},140}, &grepResp)141142if err != nil {143t.Fatal(err)144}145146expected := fmt.Sprintf("%s=%s", SECRET_NAME, SECRET_VALUE)147if strings.TrimSpace(grepResp.Stdout) != expected {148t.Fatalf("expected environment variable to be %s, but was %s", expected, grepResp.Stdout)149}150}151152153