-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprivileged_test.go
112 lines (93 loc) · 3.32 KB
/
privileged_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package vizzini_test
import (
"time"
"code.cloudfoundry.org/bbs/models"
. "code.cloudfoundry.org/vizzini/matchers"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Privileged", func() {
var task *models.TaskDefinition
var runUser string
var containerPrivileged bool
BeforeEach(func() {
if timeout == DefaultEventuallyTimeout {
SetDefaultEventuallyTimeout(time.Second * 15)
}
})
JustBeforeEach(func() {
task = Task()
task.Privileged = containerPrivileged
task.Action = models.WrapAction(&models.RunAction{
Path: "bash",
Args: []string{"-c", "id > /tmp/bar; echo h > /proc/sysrq-trigger ; echo have_real_root=$? >> /tmp/bar"},
User: runUser,
})
Expect(bbsClient.DesireTask(logger, traceID, guid, domain, task)).To(Succeed())
Eventually(TaskGetter(logger, guid)).Should(HaveTaskState(models.Task_Completed))
})
AfterEach(func() {
defer SetDefaultEventuallyTimeout(timeout)
Expect(bbsClient.ResolvingTask(logger, traceID, guid)).To(Succeed())
Expect(bbsClient.DeleteTask(logger, traceID, guid)).To(Succeed())
})
Context("with a privileged container", func() {
BeforeEach(func() {
if !config.EnablePrivilegedContainerTests {
Skip("privileged container tests are disabled")
}
containerPrivileged = true
})
Context("when running a privileged action", func() {
BeforeEach(func() {
runUser = "root"
})
It("should run as root", func() {
completedTask, err := bbsClient.TaskByGuid(logger, traceID, guid)
Expect(err).NotTo(HaveOccurred())
Expect(completedTask.Result).To(ContainSubstring("uid=0(root)"), "If this fails, then your executor may not be configured to allow privileged actions")
Expect(completedTask.Result).To(MatchRegexp(`groups=.*0\(root\)`))
Expect(completedTask.Result).To(ContainSubstring("have_real_root=0"))
})
})
Context("when running a non-privileged action", func() {
BeforeEach(func() {
runUser = "vcap"
})
It("should run as non-root", func() {
completedTask, err := bbsClient.TaskByGuid(logger, traceID, guid)
Expect(err).NotTo(HaveOccurred())
Expect(completedTask.Result).To(MatchRegexp(`uid=\d{4,5}\(vcap\)`))
Expect(completedTask.Result).To(MatchRegexp(`groups=\d{4,5}\(vcap\)`))
Expect(completedTask.Result).To(ContainSubstring("have_real_root=1"))
})
})
})
Context("with an unprivileged container", func() {
BeforeEach(func() {
containerPrivileged = false
})
Context("when running a privileged action", func() {
BeforeEach(func() {
runUser = "root"
})
It("should run as namespaced root", func() {
completedTask, err := bbsClient.TaskByGuid(logger, traceID, guid)
Expect(err).NotTo(HaveOccurred())
Expect(completedTask.Result).To(ContainSubstring("uid=0(root)"), "If this fails, then your executor may not be configured to allow privileged actions")
Expect(completedTask.Result).To(ContainSubstring("have_real_root=1"))
})
})
Context("when running a non-privileged action", func() {
BeforeEach(func() {
runUser = "vcap"
})
It("should run as non-root", func() {
completedTask, err := bbsClient.TaskByGuid(logger, traceID, guid)
Expect(err).NotTo(HaveOccurred())
Expect(completedTask.Result).To(MatchRegexp(`uid=\d{4,5}\(vcap\)`))
Expect(completedTask.Result).To(ContainSubstring("have_real_root=1"))
})
})
})
})