Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 866d1f7

Browse files
committed
Fix e2e tests for all passing
Signed-off-by: JoshVanL <[email protected]>
1 parent 6e22b04 commit 866d1f7

File tree

10 files changed

+57
-51
lines changed

10 files changed

+57
-51
lines changed

demo/yaml/kube-oidc-proxy.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,7 @@ rules:
135135
- "authentication.k8s.io"
136136
resources:
137137
- "userextras/scopes"
138+
- "tokenreviews"
138139
verbs:
140+
- "create"
139141
- "impersonate"

deploy/charts/kube-oidc-proxy/templates/clusterrole.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ rules:
1717
- "authentication.k8s.io"
1818
resources:
1919
- "userextras/scopes"
20+
- "tokenreviews"
2021
verbs:
22+
- "create"
2123
- "impersonate"

test/e2e/framework/helper/deploy.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ func (h *Helper) DeployProxy(ns *corev1.Namespace, issuerURL, clientID string,
125125
},
126126
{
127127
APIGroups: []string{"authentication.k8s.io"},
128-
Resources: []string{"userextras/scopes"},
129-
Verbs: []string{"impersonate"},
128+
Resources: []string{"userextras/scopes", "tokenreviews"},
129+
Verbs: []string{"impersonate", "create"},
130130
},
131131
},
132132
})
@@ -319,21 +319,28 @@ func (h *Helper) DeleteIssuer(ns string) error {
319319
return h.deleteApp(ns, IssuerName)
320320
}
321321
func (h *Helper) DeleteProxy(ns string) error {
322-
return h.deleteApp(ns, ProxyName)
322+
return h.deleteApp(ns, ProxyName, "oidc-ca")
323323
}
324324

325-
func (h *Helper) deleteApp(ns, name string) error {
325+
func (h *Helper) deleteApp(ns, name string, extraSecrets ...string) error {
326326
err := h.KubeClient.CoreV1().Pods(ns).Delete(name, nil)
327327
if err != nil && !k8sErrors.IsNotFound(err) {
328328
return err
329329
}
330330

331-
err = h.KubeClient.CoreV1().Secrets(ns).Delete(name, nil)
331+
for _, s := range append(extraSecrets, name) {
332+
err = h.KubeClient.CoreV1().Secrets(ns).Delete(s, nil)
333+
if err != nil && !k8sErrors.IsNotFound(err) {
334+
return err
335+
}
336+
}
337+
338+
err = h.KubeClient.CoreV1().Services(ns).Delete(name, nil)
332339
if err != nil && !k8sErrors.IsNotFound(err) {
333340
return err
334341
}
335342

336-
err = h.KubeClient.CoreV1().Services(ns).Delete(name, nil)
343+
err = h.KubeClient.CoreV1().ServiceAccounts(ns).Delete(name, nil)
337344
if err != nil && !k8sErrors.IsNotFound(err) {
338345
return err
339346
}

test/e2e/framework/helper/requester.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ type Requester struct {
1414

1515
func (h *Helper) NewRequester(transport http.RoundTripper, token string) *Requester {
1616
r := &Requester{
17-
token: token,
17+
token: token,
18+
transport: transport,
1819
}
1920

2021
r.client = http.DefaultClient

test/e2e/framework/helper/token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (h *Helper) NewTokenPayload(issuerURL, clientID string, exp time.Time) []by
6868
return []byte(fmt.Sprintf(`{
6969
"iss":"%s",
7070
"aud":["%s","aud-2"],
71-
"email":"foo@example.com",
71+
"email":"user@example.com",
7272
"groups":["group-1","group-2"],
7373
"exp":%d
7474
}`, issuerURL, clientID, exp.Unix()))

test/e2e/suite/cases/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cases
22

33
import (
44
_ "github.com/jetstack/kube-oidc-proxy/test/e2e/suite/cases/impersonation"
5+
_ "github.com/jetstack/kube-oidc-proxy/test/e2e/suite/cases/passthrough"
56
_ "github.com/jetstack/kube-oidc-proxy/test/e2e/suite/cases/rbac"
67
_ "github.com/jetstack/kube-oidc-proxy/test/e2e/suite/cases/token"
78
)

test/e2e/suite/cases/impersonation/impersonation.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package impersonation
22

33
import (
4-
"bytes"
54
"fmt"
6-
"io/ioutil"
75
"net/http"
86

97
. "github.com/onsi/ginkgo"
108
. "github.com/onsi/gomega"
119

1210
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
1311
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"k8s.io/client-go/kubernetes"
1413
"k8s.io/client-go/rest"
1514

1615
"github.com/jetstack/kube-oidc-proxy/test/e2e/framework"
@@ -63,7 +62,7 @@ var _ = framework.CasesDescribe("Impersonation", func() {
6362
})
6463
})
6564

66-
It("should not error at proxy when impersonation is disabled impersonation is attempted on a request", func() {
65+
It("should not error at proxy when impersonation is disabled and impersonation is attempted on a request", func() {
6766
By("Enabling the disabling of impersonation")
6867
f.DeployProxyWith("--disable-impersonation")
6968

@@ -78,31 +77,25 @@ var _ = framework.CasesDescribe("Impersonation", func() {
7877
})
7978

8079
func tryImpersonationClient(f *framework.Framework, impConfig rest.ImpersonationConfig) {
80+
// build client with impersonation
8181
config := f.NewProxyRestConfig()
8282
config.Impersonate = impConfig
83-
84-
tranConfig, err := config.TransportConfig()
83+
client, err := kubernetes.NewForConfig(config)
8584
Expect(err).NotTo(HaveOccurred())
8685

87-
client := http.DefaultClient
88-
client.Transport = tranConfig.Transport
89-
90-
// send request with signed token to proxy
91-
target := fmt.Sprintf("%s/api/v1/namespaces/%s/pods",
92-
config.Host, f.Namespace.Name)
93-
94-
resp, err := client.Get(target)
95-
Expect(err).NotTo(HaveOccurred())
96-
97-
body, err := ioutil.ReadAll(resp.Body)
98-
Expect(err).NotTo(HaveOccurred())
86+
_, err = client.CoreV1().Pods(f.Namespace.Name).List(metav1.ListOptions{})
87+
kErr, ok := err.(*k8sErrors.StatusError)
88+
if !ok {
89+
Expect(err).NotTo(HaveOccurred())
90+
}
9991

100-
expRespBody := []byte("Impersonation requests are disabled when using kube-oidc-proxy\n")
92+
expRespBody := "Impersonation requests are disabled when using kube-oidc-proxy (get pods)"
93+
resp := kErr.Status().Details.Causes[0].Message
10194

10295
// check body and status code the token was rejected
103-
if resp.StatusCode != http.StatusForbidden ||
104-
!bytes.Equal(body, expRespBody) {
96+
if int(kErr.Status().Code) != http.StatusForbidden ||
97+
resp != expRespBody {
98+
Expect(fmt.Errorf("expected status code %d with body %q, got= %+v",
99+
http.StatusForbidden, expRespBody, kErr)).NotTo(HaveOccurred())
105100
}
106-
Expect(fmt.Errorf("expected status code %d with body %q, got= %d %q",
107-
http.StatusForbidden, expRespBody, resp.StatusCode, body)).NotTo(HaveOccurred())
108101
}

test/e2e/suite/cases/passthrough/passthrough.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package impersonation
22

33
import (
4-
"bytes"
54
"fmt"
65
"net/http"
76

87
. "github.com/onsi/ginkgo"
98
. "github.com/onsi/gomega"
10-
119
corev1 "k8s.io/api/core/v1"
1210
rbacv1 "k8s.io/api/rbac/v1"
11+
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
1312
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1413
"k8s.io/client-go/kubernetes"
1514

@@ -89,22 +88,27 @@ var _ = framework.CasesDescribe("Passthrough", func() {
8988
By("Using a ServiceAccount token should error by the proxy")
9089

9190
// Create requester using the ServiceAccount token
92-
proxyConfig := f.NewProxyRestConfig()
93-
requester := f.Helper().NewRequester(proxyConfig.Transport, saToken)
91+
config := f.NewProxyRestConfig()
92+
config.BearerToken = saToken
9493

95-
// Send request with signed token to proxy
96-
target := fmt.Sprintf("%s/api/v1/namespaces/%s/pods",
97-
proxyConfig.Host, f.Namespace.Name)
98-
99-
body, statusCode, err := requester.Get(target)
94+
client, err := kubernetes.NewForConfig(config)
10095
Expect(err).NotTo(HaveOccurred())
10196

97+
_, err = client.CoreV1().Pods(f.Namespace.Name).List(metav1.ListOptions{})
98+
kErr, ok := err.(*k8sErrors.StatusError)
99+
if !ok {
100+
Expect(err).NotTo(HaveOccurred())
101+
}
102+
103+
expRespBody := "Unauthorized"
104+
resp := kErr.Status().Details.Causes[0].Message
105+
102106
// Check body and status code the token was rejected
103-
if statusCode != http.StatusForbidden ||
104-
!bytes.Equal(body, []byte("Unauthorized")) {
107+
if int(kErr.Status().Code) != http.StatusUnauthorized ||
108+
resp != expRespBody {
109+
Expect(fmt.Errorf("expected status code %d with body %q, got= %d %q",
110+
http.StatusUnauthorized, expRespBody, int(kErr.Status().Code), resp)).NotTo(HaveOccurred())
105111
}
106-
Expect(fmt.Errorf("expected status code %d with body Unauthorized, got= %d %q",
107-
http.StatusForbidden, statusCode, body)).NotTo(HaveOccurred())
108112
})
109113

110114
It("should not error on a valid OIDC token nor a valid ServiceAccount token with passthrough enabled", func() {

test/e2e/suite/cases/token/token.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ func expectProxyUnauthorized(f *framework.Framework, tokenPayload []byte) {
6161
proxyConfig.Host, f.Namespace.Name)
6262

6363
body, statusCode, err := requester.Get(target)
64+
body = bytes.TrimSpace(body)
6465
Expect(err).NotTo(HaveOccurred())
6566

6667
// Check body and status code the token was rejected
67-
if statusCode != http.StatusForbidden ||
68+
if statusCode != http.StatusUnauthorized ||
6869
!bytes.Equal(body, []byte("Unauthorized")) {
70+
Expect(fmt.Errorf("expected status code %d with body Unauthorized, got= %d %q",
71+
http.StatusUnauthorized, statusCode, body)).NotTo(HaveOccurred())
6972
}
70-
Expect(fmt.Errorf("expected status code %d with body Unauthorized, got= %d %q",
71-
http.StatusForbidden, statusCode, body)).NotTo(HaveOccurred())
7273
}

test/e2e/suite/suite.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ var _ = SynchronizedBeforeSuite(func() []byte {
2727
cfg.RepoRoot = env.RootPath()
2828
cfg.Environment = env
2929

30-
cfg.KubeConfigPath = "/home/josh/.kube/kind-config-kube-oidc-proxy-e2e"
31-
cfg.Kubectl = "/home/josh/go/src/github.com/jetstack/kube-oidc-proxy/bin/kubectl"
32-
cfg.RepoRoot = "/home/josh/go/src/github.com/jetstack/kube-oidc-proxy"
33-
cfg.Environment = env
34-
3530
if err := framework.DefaultConfig.Validate(); err != nil {
3631
log.Fatalf("Invalid test config: %v", err)
3732
}

0 commit comments

Comments
 (0)