Skip to content

Commit 9f40607

Browse files
authored
Merge pull request kubernetes#71209 from dczombera/add_impersonation_test
Add an impersonation test case to the audit E2E test
2 parents f4487a0 + f9df691 commit 9f40607

File tree

2 files changed

+67
-22
lines changed

2 files changed

+67
-22
lines changed

test/e2e/auth/audit.go

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ var _ = SIGDescribe("Advanced Audit", func() {
7979
anonymousClient, err := clientset.NewForConfig(config)
8080
framework.ExpectNoError(err)
8181

82+
By("Creating a kubernetes client that impersonates an authorized user")
83+
config, err = framework.LoadConfig()
84+
framework.ExpectNoError(err)
85+
config.Impersonate = restclient.ImpersonationConfig{
86+
UserName: "superman",
87+
Groups: []string{"system:masters"},
88+
}
89+
impersonatedClient, err := clientset.NewForConfig(config)
90+
framework.ExpectNoError(err)
91+
8292
testCases := []struct {
8393
action func()
8494
events []utils.AuditEvent
@@ -668,6 +678,30 @@ var _ = SIGDescribe("Advanced Audit", func() {
668678
},
669679
},
670680
},
681+
// List pods as impersonated user.
682+
{
683+
func() {
684+
_, err = impersonatedClient.CoreV1().Pods(namespace).List(metav1.ListOptions{})
685+
framework.ExpectNoError(err, "failed to list pods")
686+
},
687+
[]utils.AuditEvent{
688+
{
689+
Level: auditinternal.LevelRequest,
690+
Stage: auditinternal.StageResponseComplete,
691+
RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/pods", namespace),
692+
Verb: "list",
693+
Code: 200,
694+
User: auditTestUser,
695+
ImpersonatedUser: "superman",
696+
ImpersonatedGroups: "system:masters",
697+
Resource: "pods",
698+
Namespace: namespace,
699+
RequestObject: false,
700+
ResponseObject: false,
701+
AuthorizeDecision: "allow",
702+
},
703+
},
704+
},
671705
}
672706

673707
// test authorizer annotations, RBAC is required.
@@ -684,17 +718,19 @@ var _ = SIGDescribe("Advanced Audit", func() {
684718
},
685719
[]utils.AuditEvent{
686720
{
687-
Level: auditinternal.LevelRequest,
688-
Stage: auditinternal.StageResponseComplete,
689-
RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/pods/another-audit-pod", namespace),
690-
Verb: "get",
691-
Code: 403,
692-
User: auditTestUser,
693-
Resource: "pods",
694-
Namespace: namespace,
695-
RequestObject: false,
696-
ResponseObject: false,
697-
AuthorizeDecision: "forbid",
721+
Level: auditinternal.LevelRequest,
722+
Stage: auditinternal.StageResponseComplete,
723+
RequestURI: fmt.Sprintf("/api/v1/namespaces/%s/pods/another-audit-pod", namespace),
724+
Verb: "get",
725+
Code: 403,
726+
User: auditTestUser,
727+
ImpersonatedUser: "system:anonymous",
728+
ImpersonatedGroups: "system:unauthenticated",
729+
Resource: "pods",
730+
Namespace: namespace,
731+
RequestObject: false,
732+
ResponseObject: false,
733+
AuthorizeDecision: "forbid",
698734
},
699735
},
700736
},

test/utils/audit.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"bufio"
2121
"fmt"
2222
"io"
23+
"sort"
24+
"strings"
2325

2426
"k8s.io/apimachinery/pkg/runtime"
2527
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -28,17 +30,19 @@ import (
2830
)
2931

3032
type AuditEvent struct {
31-
Level auditinternal.Level
32-
Stage auditinternal.Stage
33-
RequestURI string
34-
Verb string
35-
Code int32
36-
User string
37-
Resource string
38-
Namespace string
39-
RequestObject bool
40-
ResponseObject bool
41-
AuthorizeDecision string
33+
Level auditinternal.Level
34+
Stage auditinternal.Stage
35+
RequestURI string
36+
Verb string
37+
Code int32
38+
User string
39+
ImpersonatedUser string
40+
ImpersonatedGroups string
41+
Resource string
42+
Namespace string
43+
RequestObject bool
44+
ResponseObject bool
45+
AuthorizeDecision string
4246
}
4347

4448
// Search the audit log for the expected audit lines.
@@ -101,6 +105,11 @@ func parseAuditLine(line string, version schema.GroupVersion) (AuditEvent, error
101105
if e.RequestObject != nil {
102106
event.RequestObject = true
103107
}
108+
if e.ImpersonatedUser != nil {
109+
event.ImpersonatedUser = e.ImpersonatedUser.Username
110+
sort.Strings(e.ImpersonatedUser.Groups)
111+
event.ImpersonatedGroups = strings.Join(e.ImpersonatedUser.Groups, ",")
112+
}
104113
event.AuthorizeDecision = e.Annotations["authorization.k8s.io/decision"]
105114
return event, nil
106115
}

0 commit comments

Comments
 (0)