|
| 1 | +// +build e2e |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2021 The Tekton Authors |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package test |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | +) |
| 30 | + |
| 31 | +// TestHermeticTaskRun make sure that the hermetic execution mode actually drops network from a TaskRun step |
| 32 | +// it does this by first running the TaskRun normally to make sure it passes |
| 33 | +// Then, it enables hermetic mode and makes sure the same TaskRun fails because it no longer has access to a network. |
| 34 | +func TestHermeticTaskRun(t *testing.T) { |
| 35 | + ctx := context.Background() |
| 36 | + ctx, cancel := context.WithCancel(ctx) |
| 37 | + defer cancel() |
| 38 | + |
| 39 | + c, namespace := setup(ctx, t, requireAnyGate(map[string]string{"enable-api-fields": "alpha"})) |
| 40 | + t.Parallel() |
| 41 | + defer tearDown(ctx, t, c, namespace) |
| 42 | + |
| 43 | + // first, run the task run with hermetic=false to prove that it succeeds |
| 44 | + regularTaskRunName := "not-hermetic" |
| 45 | + regularTaskRun := taskRun(regularTaskRunName, namespace, "") |
| 46 | + t.Logf("Creating TaskRun %s, hermetic=false", regularTaskRunName) |
| 47 | + if _, err := c.TaskRunClient.Create(ctx, regularTaskRun, metav1.CreateOptions{}); err != nil { |
| 48 | + t.Fatalf("Failed to create TaskRun `%s`: %s", regularTaskRunName, err) |
| 49 | + } |
| 50 | + if err := WaitForTaskRunState(ctx, c, regularTaskRunName, Succeed(regularTaskRunName), "TaskRunCompleted"); err != nil { |
| 51 | + t.Fatalf("Error waiting for TaskRun %s to finish: %s", regularTaskRunName, err) |
| 52 | + } |
| 53 | + |
| 54 | + // now, run the task mode with hermetic mode |
| 55 | + // it should fail, since it shouldn't be able to access any network |
| 56 | + hermeticTaskRunName := "hermetic-should-fail" |
| 57 | + hermeticTaskRun := taskRun(hermeticTaskRunName, namespace, "hermetic") |
| 58 | + t.Logf("Creating TaskRun %s, hermetic=true", hermeticTaskRunName) |
| 59 | + if _, err := c.TaskRunClient.Create(ctx, hermeticTaskRun, metav1.CreateOptions{}); err != nil { |
| 60 | + t.Fatalf("Failed to create TaskRun `%s`: %s", regularTaskRun.Name, err) |
| 61 | + } |
| 62 | + if err := WaitForTaskRunState(ctx, c, hermeticTaskRunName, Failed(hermeticTaskRunName), "Failed"); err != nil { |
| 63 | + t.Fatalf("Error waiting for TaskRun %s to fail: %s", hermeticTaskRunName, err) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func taskRun(name, namespace, executionMode string) *v1beta1.TaskRun { |
| 68 | + return &v1beta1.TaskRun{ |
| 69 | + ObjectMeta: metav1.ObjectMeta{Name: name, |
| 70 | + Namespace: namespace, |
| 71 | + Annotations: map[string]string{ |
| 72 | + "experimental.tekton.dev/execution-mode": executionMode, |
| 73 | + }, |
| 74 | + }, |
| 75 | + Spec: v1beta1.TaskRunSpec{ |
| 76 | + Timeout: &metav1.Duration{Duration: time.Minute}, |
| 77 | + TaskSpec: &v1beta1.TaskSpec{ |
| 78 | + Steps: []v1beta1.Step{ |
| 79 | + { |
| 80 | + Container: corev1.Container{ |
| 81 | + Name: "access-network", |
| 82 | + Image: "ubuntu", |
| 83 | + }, |
| 84 | + Script: `#!/bin/bash |
| 85 | +set -ex |
| 86 | +apt-get update |
| 87 | +apt-get install -y curl`, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + } |
| 93 | +} |
0 commit comments