Skip to content

Commit b4f902f

Browse files
authored
Merge pull request kubernetes#129897 from vinayakankugoyal/testfix
Fix kubelet_authz_test.go
2 parents 295ed0a + 81f0981 commit b4f902f

File tree

1 file changed

+54
-26
lines changed

1 file changed

+54
-26
lines changed

test/e2e_node/kubelet_authz_test.go renamed to test/e2e/node/kubelet_authz.go

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package e2enode
17+
package node
1818

1919
import (
2020
"context"
21-
"crypto/tls"
2221
"fmt"
23-
"net/http"
2422

2523
"github.com/onsi/ginkgo/v2"
2624
"github.com/onsi/gomega"
27-
authenticationv1 "k8s.io/api/authentication/v1"
2825
authorizationv1 "k8s.io/api/authorization/v1"
2926
v1 "k8s.io/api/core/v1"
3027
rbacv1 "k8s.io/api/rbac/v1"
@@ -34,32 +31,40 @@ import (
3431
"k8s.io/kubernetes/test/e2e/feature"
3532
"k8s.io/kubernetes/test/e2e/framework"
3633
e2eauth "k8s.io/kubernetes/test/e2e/framework/auth"
34+
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
35+
e2eoutput "k8s.io/kubernetes/test/e2e/framework/pod/output"
36+
admissionapi "k8s.io/pod-security-admission/api"
3737
)
3838

39-
var _ = SIGDescribe("Kubelet Authz", feature.KubeletFineGrainedAuthz, func() {
39+
var _ = SIGDescribe(feature.KubeletFineGrainedAuthz, func() {
4040
f := framework.NewDefaultFramework("kubelet-authz-test")
41+
f.NamespacePodSecurityLevel = admissionapi.LevelBaseline
42+
4143
ginkgo.Context("when calling kubelet API", func() {
4244
ginkgo.It("check /healthz enpoint is accessible via nodes/healthz RBAC", func(ctx context.Context) {
4345
sc := runKubeletAuthzTest(ctx, f, "healthz", "healthz")
44-
gomega.Expect(sc).To(gomega.Equal(http.StatusOK))
46+
gomega.Expect(sc).To(gomega.Equal("200"))
4547
})
4648
ginkgo.It("check /healthz enpoint is accessible via nodes/proxy RBAC", func(ctx context.Context) {
4749
sc := runKubeletAuthzTest(ctx, f, "healthz", "proxy")
48-
gomega.Expect(sc).To(gomega.Equal(http.StatusOK))
50+
gomega.Expect(sc).To(gomega.Equal("200"))
4951
})
5052
ginkgo.It("check /healthz enpoint is not accessible via nodes/configz RBAC", func(ctx context.Context) {
5153
sc := runKubeletAuthzTest(ctx, f, "healthz", "configz")
52-
gomega.Expect(sc).To(gomega.Equal(http.StatusUnauthorized))
54+
gomega.Expect(sc).To(gomega.Equal("403"))
5355
})
5456
})
5557
})
5658

57-
func runKubeletAuthzTest(ctx context.Context, f *framework.Framework, endpoint, authzSubresource string) int {
59+
func runKubeletAuthzTest(ctx context.Context, f *framework.Framework, endpoint, authzSubresource string) string {
5860
ns := f.Namespace.Name
5961
saName := authzSubresource
6062
crName := authzSubresource
6163
verb := "get"
6264
resource := "nodes"
65+
66+
ginkgo.By(fmt.Sprintf("Creating Service Account %s/%s", ns, saName))
67+
6368
_, err := f.ClientSet.CoreV1().ServiceAccounts(ns).Create(ctx, &v1.ServiceAccount{
6469
ObjectMeta: metav1.ObjectMeta{
6570
Name: saName,
@@ -68,12 +73,15 @@ func runKubeletAuthzTest(ctx context.Context, f *framework.Framework, endpoint,
6873
}, metav1.CreateOptions{})
6974
framework.ExpectNoError(err)
7075

76+
ginkgo.By(fmt.Sprintf("Creating ClusterRole %s with for %s/%s", crName, resource, authzSubresource))
77+
7178
_, err = f.ClientSet.RbacV1().ClusterRoles().Create(ctx, &rbacv1.ClusterRole{
7279
ObjectMeta: metav1.ObjectMeta{
7380
Name: crName,
7481
},
7582
Rules: []rbacv1.PolicyRule{
7683
{
84+
APIGroups: []string{""},
7785
Verbs: []string{verb},
7886
Resources: []string{resource + "/" + authzSubresource},
7987
},
@@ -87,9 +95,13 @@ func runKubeletAuthzTest(ctx context.Context, f *framework.Framework, endpoint,
8795
Name: saName,
8896
}
8997

98+
ginkgo.By(fmt.Sprintf("Creating ClusterRoleBinding with ClusterRole %s with subject %s/%s", crName, ns, saName))
99+
90100
err = e2eauth.BindClusterRole(ctx, f.ClientSet.RbacV1(), crName, ns, subject)
91101
framework.ExpectNoError(err)
92102

103+
ginkgo.By("Waiting for Authorization Update.")
104+
93105
err = e2eauth.WaitForAuthzUpdate(ctx, f.ClientSet.AuthorizationV1(),
94106
serviceaccount.MakeUsername(ns, saName),
95107
&authorizationv1.ResourceAttributes{
@@ -102,25 +114,41 @@ func runKubeletAuthzTest(ctx context.Context, f *framework.Framework, endpoint,
102114
)
103115
framework.ExpectNoError(err)
104116

105-
tr, err := f.ClientSet.CoreV1().ServiceAccounts(ns).CreateToken(ctx, saName, &authenticationv1.TokenRequest{}, metav1.CreateOptions{})
106-
framework.ExpectNoError(err)
117+
pod := e2epod.NewAgnhostPod(ns, fmt.Sprintf("agnhost-pod-%s", authzSubresource), nil, nil, nil)
118+
pod.Spec.ServiceAccountName = saName
119+
pod.Spec.Containers[0].Env = []v1.EnvVar{
120+
{
121+
Name: "NODE_IP",
122+
ValueFrom: &v1.EnvVarSource{
123+
FieldRef: &v1.ObjectFieldSelector{
124+
FieldPath: "status.hostIP",
125+
},
126+
},
127+
},
128+
}
107129

108-
resp, err := healthCheck(fmt.Sprintf("https://127.0.0.1:%d/%s", ports.KubeletPort, endpoint), tr.Status.Token)
109-
framework.ExpectNoError(err)
110-
return resp.StatusCode
111-
}
130+
ginkgo.By(fmt.Sprintf("Creating Pod %s in namespace %s with serviceaccount %s", pod.Name, pod.Namespace, pod.Spec.ServiceAccountName))
112131

113-
func healthCheck(url, token string) (*http.Response, error) {
114-
insecureTransport := http.DefaultTransport.(*http.Transport).Clone()
115-
insecureTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
116-
insecureHTTPClient := &http.Client{
117-
Transport: insecureTransport,
118-
}
132+
_ = e2epod.NewPodClient(f).CreateSync(ctx, pod)
133+
134+
ginkgo.By("Running command in Pod")
119135

120-
req, err := http.NewRequest(http.MethodGet, url, nil)
121-
if err != nil {
122-
return nil, err
136+
var hostWarpStart, hostWarpEnd string
137+
// IPv6 host must be wrapped within [] if you specify a port.
138+
if framework.TestContext.ClusterIsIPv6() {
139+
hostWarpStart = "["
140+
hostWarpEnd = "]"
123141
}
124-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
125-
return insecureHTTPClient.Do(req)
142+
143+
result := e2eoutput.RunHostCmdOrDie(ns,
144+
pod.Name,
145+
fmt.Sprintf("curl -XGET -sIk -o /dev/null -w '%s' --header \"Authorization: Bearer `%s`\" https://%s$NODE_IP%s:%d/%s",
146+
"%{http_code}",
147+
"cat /var/run/secrets/kubernetes.io/serviceaccount/token",
148+
hostWarpStart,
149+
hostWarpEnd,
150+
ports.KubeletPort,
151+
endpoint))
152+
153+
return result
126154
}

0 commit comments

Comments
 (0)