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

Commit 9dc9e08

Browse files
committed
Adds e2e tests for extra impersonation headers
Signed-off-by: JoshVanL <[email protected]>
1 parent 8eeda28 commit 9dc9e08

File tree

22 files changed

+490
-52
lines changed

22 files changed

+490
-52
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright Jetstack Ltd. See LICENSE for details.
2+
FROM alpine:3.10
3+
4+
LABEL description="A fake API server that will respond to requests with the same body and headers."
5+
6+
RUN apk --no-cache --update add ca-certificates
7+
8+
COPY ./bin/fake-apiserver /usr/bin/fake-apiserver
9+
10+
CMD ["/usr/bin/fake-apiserver"]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright Jetstack Ltd. See LICENSE for details.
2+
package main
3+
4+
import (
5+
"fmt"
6+
"os"
7+
8+
"github.com/spf13/cobra"
9+
10+
"github.com/jetstack/kube-oidc-proxy/pkg/util"
11+
"github.com/jetstack/kube-oidc-proxy/test/e2e/framework/fake-apiserver/cmd/options"
12+
"github.com/jetstack/kube-oidc-proxy/test/e2e/framework/fake-apiserver/pkg/server"
13+
)
14+
15+
func main() {
16+
opts := new(options.Options)
17+
stopCh := util.SignalHandler()
18+
19+
cmd := &cobra.Command{
20+
Use: "fake-apiserver",
21+
Short: "A fake apiserver that will respond to requests with the same body and headers",
22+
RunE: func(cmd *cobra.Command, args []string) error {
23+
server, err := server.New(opts.KeyFile, opts.CertFile, stopCh)
24+
if err != nil {
25+
return err
26+
}
27+
28+
compCh, err := server.Run(opts.BindAddress, opts.ListenPort)
29+
if err != nil {
30+
return err
31+
}
32+
33+
<-compCh
34+
35+
return nil
36+
},
37+
}
38+
39+
opts.AddFlags(cmd)
40+
41+
if err := cmd.Execute(); err != nil {
42+
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
43+
os.Exit(1)
44+
}
45+
46+
os.Exit(0)
47+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright Jetstack Ltd. See LICENSE for details.
2+
package options
3+
4+
import (
5+
"github.com/spf13/cobra"
6+
)
7+
8+
type Options struct {
9+
BindAddress string
10+
ListenPort string
11+
12+
KeyFile string
13+
CertFile string
14+
}
15+
16+
func (o *Options) AddFlags(cmd *cobra.Command) {
17+
cmd.PersistentFlags().StringVar(&o.BindAddress, "bind-address",
18+
"0.0.0.0", "Bound Address to listen and serve on.")
19+
20+
cmd.PersistentFlags().StringVar(&o.ListenPort, "secure-port",
21+
"6443", "Port to serve HTTPS.")
22+
o.must(cmd.MarkPersistentFlagRequired("secure-port"))
23+
24+
cmd.PersistentFlags().StringVar(&o.KeyFile, "tls-private-key-file",
25+
"/etc/apiserver/key.pem", "File location to key for serving.")
26+
o.must(cmd.MarkPersistentFlagRequired("tls-private-key-file"))
27+
28+
cmd.PersistentFlags().StringVar(&o.CertFile, "tls-cert-file",
29+
"/etc/apiserver/key.pem", "File location to certificate for serving.")
30+
o.must(cmd.MarkPersistentFlagRequired("tls-cert-file"))
31+
}
32+
33+
func (o *Options) must(err error) {
34+
if err != nil {
35+
panic(err)
36+
}
37+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright Jetstack Ltd. See LICENSE for details.
2+
package server
3+
4+
import (
5+
"encoding/pem"
6+
"fmt"
7+
"io"
8+
"io/ioutil"
9+
"net"
10+
"net/http"
11+
12+
log "github.com/sirupsen/logrus"
13+
)
14+
15+
type Server struct {
16+
keyFile, certFile string
17+
18+
stopCh <-chan struct{}
19+
}
20+
21+
func New(keyFile, certFile string, stopCh <-chan struct{}) (*Server, error) {
22+
b, err := ioutil.ReadFile(keyFile)
23+
if err != nil {
24+
return nil, err
25+
}
26+
27+
block, _ := pem.Decode(b)
28+
if block == nil {
29+
return nil,
30+
fmt.Errorf("failed to parse PEM block containing the key: %q", keyFile)
31+
}
32+
33+
return &Server{
34+
keyFile: keyFile,
35+
certFile: certFile,
36+
stopCh: stopCh,
37+
}, nil
38+
}
39+
40+
func (s *Server) Run(bindAddress, listenPort string) (<-chan struct{}, error) {
41+
serveAddr := fmt.Sprintf("%s:%s", bindAddress, listenPort)
42+
43+
l, err := net.Listen("tcp", serveAddr)
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
go func() {
49+
<-s.stopCh
50+
if l != nil {
51+
l.Close()
52+
}
53+
}()
54+
55+
compCh := make(chan struct{})
56+
go func() {
57+
defer close(compCh)
58+
59+
err := http.ServeTLS(l, s, s.certFile, s.keyFile)
60+
if err != nil {
61+
log.Errorf("stopped serving TLS (%s): %s", serveAddr, err)
62+
}
63+
}()
64+
65+
log.Infof("fake API server listening and serving on %s", serveAddr)
66+
67+
return compCh, nil
68+
}
69+
70+
func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
71+
log.Infof("fake API server received url %s", r.URL)
72+
73+
for k, vs := range r.Header {
74+
for _, v := range vs {
75+
rw.Header().Add(k, v)
76+
}
77+
}
78+
79+
if _, err := io.Copy(rw, r.Body); err != nil {
80+
log.Errorf("failed to copy request body to response: %s", err)
81+
}
82+
83+
rw.WriteHeader(http.StatusOK)
84+
}

test/e2e/framework/framework.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package framework
33

44
import (
55
"fmt"
6+
"net/url"
67

78
. "github.com/onsi/ginkgo"
89
. "github.com/onsi/gomega"
@@ -14,7 +15,7 @@ import (
1415

1516
"github.com/jetstack/kube-oidc-proxy/test/e2e/framework/config"
1617
"github.com/jetstack/kube-oidc-proxy/test/e2e/framework/helper"
17-
"github.com/jetstack/kube-oidc-proxy/test/e2e/util"
18+
"github.com/jetstack/kube-oidc-proxy/test/util"
1819
)
1920

2021
var DefaultConfig = &config.Config{}
@@ -35,7 +36,7 @@ type Framework struct {
3536
helper *helper.Helper
3637

3738
issuerKeyBundle, proxyKeyBundle *util.KeyBundle
38-
issuerURL, proxyURL string
39+
issuerURL, proxyURL *url.URL
3940
}
4041

4142
func NewDefaultFramework(baseName string) *Framework {
@@ -81,7 +82,7 @@ func (f *Framework) BeforeEach() {
8182

8283
By("Deploying kube-oidc-proxy")
8384
proxyKeyBundle, proxyURL, err := f.helper.DeployProxy(f.Namespace,
84-
issuerURL, clientID, issuerKeyBundle)
85+
issuerURL, clientID, issuerKeyBundle, nil)
8586
Expect(err).NotTo(HaveOccurred())
8687

8788
f.issuerURL, f.proxyURL = issuerURL, proxyURL
@@ -110,14 +111,14 @@ func (f *Framework) AfterEach() {
110111
Expect(err).NotTo(HaveOccurred())
111112
}
112113

113-
func (f *Framework) DeployProxyWith(extraArgs ...string) {
114+
func (f *Framework) DeployProxyWith(extraVolumes []corev1.Volume, extraArgs ...string) {
114115
By("Deleting kube-oidc-proxy deployment")
115116
err := f.Helper().DeleteProxy(f.Namespace.Name)
116117
Expect(err).NotTo(HaveOccurred())
117118

118119
By(fmt.Sprintf("Deploying kube-oidc-proxy with extra args %s", extraArgs))
119120
f.proxyKeyBundle, f.proxyURL, err = f.helper.DeployProxy(f.Namespace, f.issuerURL,
120-
clientID, f.issuerKeyBundle, extraArgs...)
121+
clientID, f.issuerKeyBundle, extraVolumes, extraArgs...)
121122
Expect(err).NotTo(HaveOccurred())
122123
}
123124

@@ -133,11 +134,11 @@ func (f *Framework) ProxyKeyBundle() *util.KeyBundle {
133134
return f.proxyKeyBundle
134135
}
135136

136-
func (f *Framework) IssuerURL() string {
137+
func (f *Framework) IssuerURL() *url.URL {
137138
return f.issuerURL
138139
}
139140

140-
func (f *Framework) ProxyURL() string {
141+
func (f *Framework) ProxyURL() *url.URL {
141142
return f.proxyURL
142143
}
143144

0 commit comments

Comments
 (0)