-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy paths2i_root.go
146 lines (124 loc) · 5.34 KB
/
s2i_root.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package builds
import (
"context"
"fmt"
g "github.com/onsi/ginkgo/v2"
o "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
admissionapi "k8s.io/pod-security-admission/api"
buildv1 "github.com/openshift/api/build/v1"
exutil "github.com/openshift/origin/test/extended/util"
"github.com/openshift/origin/test/extended/util/image"
)
func Before(oc *exutil.CLI) {
exutil.PreTestDump()
}
func After(oc *exutil.CLI) {
if g.CurrentSpecReport().Failed() {
exutil.DumpPodStates(oc)
exutil.DumpConfigMapStates(oc)
exutil.DumpPodLogsStartingWith("", oc)
}
}
var _ = g.Describe("[sig-builds][Feature:Builds] s2i build with a root user image", func() {
defer g.GinkgoRecover()
oc := exutil.NewCLIWithPodSecurityLevel("s2i-build-root", admissionapi.LevelBaseline)
g.It("should create a root build and fail without a privileged SCC [apigroup:build.openshift.io]", func() {
g.Skip("TODO: figure out why we aren't properly denying this, also consider whether we still need to deny it")
Before(oc)
defer After(oc)
firstArgString := fmt.Sprintf("%s~https://github.com/sclorg/nodejs-ex", image.LocationFor("quay.io/redhat-developer/test-build-roots2i:1.2"))
err := oc.Run("new-app").Args(firstArgString, "--name", "nodejsfail").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = exutil.WaitForABuild(oc.BuildClient().BuildV1().Builds(oc.Namespace()), "nodejsfail-1", nil, nil, nil)
o.Expect(err).To(o.HaveOccurred())
build, err := oc.BuildClient().BuildV1().Builds(oc.Namespace()).Get(context.Background(), "nodejsfail-1", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(build.Status.Phase).To(o.Equal(buildv1.BuildPhaseFailed))
o.Expect(build.Status.Reason).To(o.BeEquivalentTo("PullBuilderImageFailed" /*s2istatus.ReasonPullBuilderImageFailed*/))
o.Expect(build.Status.Message).To(o.BeEquivalentTo("Failed to pull builder image." /*s2istatus.ReasonMessagePullBuilderImageFailed*/))
podname := build.Annotations[buildv1.BuildPodNameAnnotation]
pod, err := oc.KubeClient().CoreV1().Pods(oc.Namespace()).Get(context.Background(), podname, metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
containers := make([]corev1.Container, len(pod.Spec.Containers)+len(pod.Spec.InitContainers))
copy(containers, pod.Spec.Containers)
copy(containers[len(pod.Spec.Containers):], pod.Spec.InitContainers)
for _, c := range containers {
env := map[string]string{}
for _, e := range c.Env {
env[e.Name] = e.Value
}
o.Expect(env["DROP_CAPS"]).To(o.Equal("KILL,MKNOD,SETGID,SETUID"))
o.Expect(env["ALLOWED_UIDS"]).To(o.Equal("1-"))
}
})
g.It("should create a root build and pass with a privileged SCC [apigroup:build.openshift.io]", func() {
Before(oc)
defer After(oc)
g.By("adding builder account to privileged SCC")
// create a namespace local role and role binding to the privileged SCC;
// role and role binding will be deleted as part of test namespace clean up
role := &rbacv1.Role{}
role.Namespace = oc.Namespace()
role.Name = "privileged-builder-role"
role.Rules = []rbacv1.PolicyRule{
{
APIGroups: []string{
"security.openshift.io",
},
ResourceNames: []string{
"privileged",
},
Resources: []string{
"securitycontextconstraints",
},
Verbs: []string{
"use",
},
},
}
role, err := oc.AdminKubeClient().RbacV1().Roles(oc.Namespace()).Create(context.Background(), role, metav1.CreateOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
roleBinding := &rbacv1.RoleBinding{}
roleBinding.Namespace = oc.Namespace()
roleBinding.Name = "privileged-builder-rolebinding"
roleBinding.RoleRef = rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "Role",
Name: "privileged-builder-role",
}
roleBinding.Subjects = []rbacv1.Subject{
{
Kind: "ServiceAccount",
APIGroup: "",
Name: "builder",
Namespace: oc.Namespace(),
},
}
roleBinding, err = oc.AdminKubeClient().RbacV1().RoleBindings(oc.Namespace()).Create(context.Background(), roleBinding, metav1.CreateOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
firstArgString := fmt.Sprintf("%s~https://github.com/sclorg/nodejs-ex", image.LocationFor("quay.io/redhat-developer/test-build-roots2i:1.2"))
err = oc.Run("new-build").Args(firstArgString, "--name", "nodejspass").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = exutil.WaitForABuild(oc.BuildClient().BuildV1().Builds(oc.Namespace()), "nodejspass-1", nil, nil, nil)
o.Expect(err).NotTo(o.HaveOccurred())
build, err := oc.BuildClient().BuildV1().Builds(oc.Namespace()).Get(context.Background(), "nodejspass-1", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
podname := build.Annotations[buildv1.BuildPodNameAnnotation]
pod, err := oc.KubeClient().CoreV1().Pods(oc.Namespace()).Get(context.Background(), podname, metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
containers := make([]corev1.Container, len(pod.Spec.Containers)+len(pod.Spec.InitContainers))
copy(containers, pod.Spec.Containers)
copy(containers[len(pod.Spec.Containers):], pod.Spec.InitContainers)
for _, c := range containers {
env := map[string]string{}
for _, e := range c.Env {
env[e.Name] = e.Value
}
o.Expect(env).NotTo(o.HaveKey("DROP_CAPS"))
o.Expect(env).NotTo(o.HaveKey("ALLOWED_UIDS"))
}
})
})