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

Commit dea5e25

Browse files
committed
add test
Signed-off-by: huabing zhao <[email protected]>
1 parent e1d1883 commit dea5e25

File tree

5 files changed

+246
-7
lines changed

5 files changed

+246
-7
lines changed

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ BUILD_OPTS ?=
1818
TEST_OPTS ?=
1919
TEST_PKGS ?= $(shell go list ./... | grep -v /e2e)
2020
OUTDIR ?= bin
21+
ENVTEST ?= sigs.k8s.io/controller-runtime/tools/setup-envtest
2122

2223
include env.mk # Load common variables
2324
-include .makerc # Pick up any local overrides.
@@ -93,8 +94,11 @@ COVERAGE_OPTS ?=
9394
.PHONY: coverage
9495
coverage: ## Creates coverage report for all projects
9596
@echo "Running test coverage"
97+
@go get $(ENVTEST)
98+
@go run $(ENVTEST) use
9699
@mkdir -p $(OUTDIR)/$@
97100
@go test $(COVERAGE_OPTS) \
101+
--tags=integration \
98102
-timeout 30s \
99103
-coverprofile $(OUTDIR)/$@/coverage.out \
100104
-covermode atomic \

go.mod

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
google.golang.org/protobuf v1.32.0
1919
k8s.io/api v0.29.0
2020
k8s.io/apimachinery v0.29.0
21+
k8s.io/client-go v0.29.0
2122
sigs.k8s.io/controller-runtime v0.17.2
2223
)
2324

@@ -33,6 +34,7 @@ require (
3334
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
3435
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
3536
github.com/go-logr/logr v1.4.1 // indirect
37+
github.com/go-logr/zapr v1.3.0 // indirect
3638
github.com/go-openapi/jsonpointer v0.19.6 // indirect
3739
github.com/go-openapi/jsonreference v0.20.2 // indirect
3840
github.com/go-openapi/swag v0.22.3 // indirect
@@ -60,6 +62,8 @@ require (
6062
github.com/rogpeppe/go-internal v1.12.0 // indirect
6163
github.com/spf13/pflag v1.0.5 // indirect
6264
github.com/yuin/gopher-lua v1.1.0 // indirect
65+
go.uber.org/multierr v1.11.0 // indirect
66+
go.uber.org/zap v1.26.0 // indirect
6367
golang.org/x/crypto v0.18.0 // indirect
6468
golang.org/x/oauth2 v0.16.0 // indirect
6569
golang.org/x/sys v0.16.0 // indirect
@@ -70,7 +74,7 @@ require (
7074
gopkg.in/inf.v0 v0.9.1 // indirect
7175
gopkg.in/yaml.v2 v2.4.0 // indirect
7276
gopkg.in/yaml.v3 v3.0.1 // indirect
73-
k8s.io/client-go v0.29.0 // indirect
77+
k8s.io/apiextensions-apiserver v0.29.0 // indirect
7478
k8s.io/klog/v2 v2.110.1 // indirect
7579
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
7680
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect

go.sum

+22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZp
77
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
88
github.com/alicebob/miniredis/v2 v2.31.1 h1:7XAt0uUg3DtwEKW5ZAGa+K7FZV2DdKQo5K/6TTnfX8Y=
99
github.com/alicebob/miniredis/v2 v2.31.1/go.mod h1:UB/T2Uztp7MlFSDakaX1sTXUv5CASoprx0wulRT6HBg=
10+
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
11+
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
1012
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
1113
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
1214
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
@@ -37,6 +39,8 @@ github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH
3739
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
3840
github.com/evanphx/json-patch/v5 v5.8.0 h1:lRj6N9Nci7MvzrXuX6HFzU8XjmhPiXPlsKEy1u0KQro=
3941
github.com/evanphx/json-patch/v5 v5.8.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
42+
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
43+
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
4044
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
4145
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
4246
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
@@ -54,6 +58,7 @@ github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
5458
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
5559
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
5660
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
61+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
5762
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
5863
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
5964
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
@@ -104,6 +109,9 @@ github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczG
104109
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
105110
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
106111
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
112+
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
113+
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
114+
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k=
107115
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
108116
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
109117
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -119,6 +127,14 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
119127
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
120128
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
121129
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
130+
github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk=
131+
github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA=
132+
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
133+
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
134+
github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM=
135+
github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY=
136+
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
137+
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
122138
github.com/redis/go-redis/v9 v9.4.0 h1:Yzoz33UZw9I/mFhx4MNrB6Fk+XHO1VukNcCa1+lwyKk=
123139
github.com/redis/go-redis/v9 v9.4.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
124140
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
@@ -146,6 +162,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
146162
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
147163
github.com/yuin/gopher-lua v1.1.0 h1:BojcDhfyDWgU2f2TOzYK/g5p2gxMrku8oupLDqlnSqE=
148164
github.com/yuin/gopher-lua v1.1.0/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
165+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
166+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
149167
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
150168
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
151169
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
@@ -222,6 +240,8 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
222240
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
223241
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
224242
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
243+
gomodules.xyz/jsonpatch/v2 v2.4.0 h1:Ci3iUJyx9UeRx7CeFN8ARgGbkESwJK+KB9lLcWxY/Zw=
244+
gomodules.xyz/jsonpatch/v2 v2.4.0/go.mod h1:AH3dM2RI6uoBZxn3LVrfvJ3E0/9dG4cSrbuBJT4moAY=
225245
google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM=
226246
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
227247
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe h1:bQnxqljG/wqi4NTXu2+DJ3n7APcEA882QZ1JvhQAq9o=
@@ -251,6 +271,8 @@ k8s.io/apimachinery v0.29.0 h1:+ACVktwyicPz0oc6MTMLwa2Pw3ouLAfAon1wPLtG48o=
251271
k8s.io/apimachinery v0.29.0/go.mod h1:eVBxQ/cwiJxH58eK/jd/vAk4mrxmVlnpBH5J2GbMeis=
252272
k8s.io/client-go v0.29.0 h1:KmlDtFcrdUzOYrBhXHgKw5ycWzc3ryPX5mQe0SkG3y8=
253273
k8s.io/client-go v0.29.0/go.mod h1:yLkXH4HKMAywcrD82KMSmfYg2DlE8mepPR4JGSo5n38=
274+
k8s.io/component-base v0.29.0 h1:T7rjd5wvLnPBV1vC4zWd/iWRbV8Mdxs+nGaoaFzGw3s=
275+
k8s.io/component-base v0.29.0/go.mod h1:sADonFTQ9Zc9yFLghpDpmNXEdHyQmFIGbiuZbqAXQ1M=
254276
k8s.io/klog/v2 v2.110.1 h1:U/Af64HJf7FcwMcXyKm2RPM22WZzyR7OSpYj5tg3cL0=
255277
k8s.io/klog/v2 v2.110.1/go.mod h1:YGtd1984u+GgbuZ7e08/yBuAfKLSO0+uR1Fhi6ExXjo=
256278
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 h1:aVUu9fTY98ivBPKR9Y5w/AuzbMm96cd3YHRTU83I780=

internal/authz/oidc.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"google.golang.org/grpc/codes"
3737
v1 "k8s.io/api/core/v1"
3838
"k8s.io/apimachinery/pkg/types"
39+
"k8s.io/client-go/rest"
3940
"sigs.k8s.io/controller-runtime/pkg/client"
4041
"sigs.k8s.io/controller-runtime/pkg/client/config"
4142

@@ -346,7 +347,7 @@ func (o *oidcHandler) retrieveTokens(ctx context.Context, log telemetry.Logger,
346347
"redirect_uri": []string{o.config.GetCallbackUri()},
347348
}
348349

349-
clientSecret, err := getClientSecret(ctx, log, o.config)
350+
clientSecret, err := getClientSecret(ctx, o.config)
350351
if err != nil {
351352
log.Error("error getting client secret", err)
352353
setDenyResponse(resp, newDenyResponse(), codes.Internal)
@@ -811,11 +812,19 @@ func loadWellKnownConfig(client *http.Client, cfg *oidcv1.OIDCConfig) error {
811812
const clientSecretKey = "client-secret"
812813
const defaultNamespace = "default"
813814

815+
// inClusterConfig is used to inject the kubeconfig for testing purposes.
816+
var inClusterConfig *rest.Config
817+
814818
// getClientSecret retrieves the client secret from the given OIDCConfig.
815819
// The client secret can be a literal or a reference to a Kubernetes secret.
816820
// If the client secret is a reference to a Kubernetes secret, it retrieves the secret from the Kubernetes API.
817-
func getClientSecret(ctx context.Context, log telemetry.Logger, c *oidcv1.OIDCConfig) (string, error) {
821+
func getClientSecret(ctx context.Context, c *oidcv1.OIDCConfig) (string, error) {
818822
if c.GetClientSecretRef() != nil {
823+
var (
824+
cfg *rest.Config
825+
err error
826+
)
827+
819828
namespace := c.GetClientSecretRef().Namespace
820829
if namespace == "" {
821830
namespace = defaultNamespace
@@ -825,11 +834,14 @@ func getClientSecret(ctx context.Context, log telemetry.Logger, c *oidcv1.OIDCCo
825834
Name: c.GetClientSecretRef().Name,
826835
}
827836

828-
log.Info("get client secret from Kubernetes secret", "secret", secretName.String())
829-
cfg, err := config.GetConfig()
830-
if err != nil {
831-
return "", fmt.Errorf("error getting rest config: %w", err)
837+
cfg = inClusterConfig
838+
if cfg == nil {
839+
cfg, err = config.GetConfig()
840+
if err != nil {
841+
return "", fmt.Errorf("error getting kube config: %w", err)
842+
}
832843
}
844+
833845
cl, err := client.New(cfg, client.Options{})
834846
if err != nil {
835847
return "", fmt.Errorf("errot creating kube client: %w", err)
+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
// Copyright 2024 Tetrate
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build integration
16+
// +build integration
17+
18+
package authz
19+
20+
import (
21+
"context"
22+
"encoding/json"
23+
"net/http"
24+
"os"
25+
"testing"
26+
"time"
27+
28+
envoy "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3"
29+
"github.com/lestrrat-go/jwx/jwt"
30+
"github.com/stretchr/testify/require"
31+
"google.golang.org/grpc/codes"
32+
v1 "k8s.io/api/core/v1"
33+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+
"k8s.io/client-go/rest"
35+
"sigs.k8s.io/controller-runtime/pkg/client"
36+
"sigs.k8s.io/controller-runtime/pkg/envtest"
37+
"sigs.k8s.io/controller-runtime/pkg/log"
38+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
39+
40+
oidcv1 "github.com/tetrateio/authservice-go/config/gen/go/v1/oidc"
41+
"github.com/tetrateio/authservice-go/internal/oidc"
42+
)
43+
44+
var (
45+
oidcConfigWithSecretRef = &oidcv1.OIDCConfig{
46+
IdToken: &oidcv1.TokenConfig{
47+
Header: "Authorization",
48+
Preamble: "Bearer",
49+
},
50+
AccessToken: &oidcv1.TokenConfig{
51+
Header: "X-Access-Token",
52+
Preamble: "Bearer",
53+
},
54+
TokenUri: "http://idp-test-server/token",
55+
AuthorizationUri: "http://idp-test-server/auth",
56+
CallbackUri: "https://localhost:443/callback",
57+
ClientId: "test-client-id",
58+
Scopes: []string{"openid", "email"},
59+
}
60+
)
61+
62+
func TestOIDCProcessWithKubernetesSecret(t *testing.T) {
63+
// start kube test env
64+
testEnv, conf, err := startEnv()
65+
require.NoError(t, err)
66+
67+
// set inClusterConfig for the client
68+
inClusterConfig = conf
69+
70+
// create secrets for testing
71+
c, err := client.New(conf, client.Options{})
72+
require.NoError(t, err)
73+
err = c.Create(context.Background(), &v1.Secret{
74+
ObjectMeta: metav1.ObjectMeta{
75+
Namespace: "default",
76+
Name: "test-client-secret",
77+
},
78+
Data: map[string][]byte{
79+
clientSecretKey: []byte("test-client-secret"),
80+
},
81+
})
82+
require.NoError(t, err)
83+
err = c.Create(context.Background(), &v1.Secret{
84+
ObjectMeta: metav1.ObjectMeta{
85+
Namespace: "default",
86+
Name: "invalid-test-client-secret",
87+
},
88+
Data: map[string][]byte{
89+
clientSecretKey + "-invalid": []byte("test-client-secret"),
90+
},
91+
})
92+
require.NoError(t, err)
93+
94+
// stop kube test env
95+
defer func() {
96+
require.NoError(t, testEnv.Stop())
97+
}()
98+
99+
jwkPriv, jwkPub := newKeyPair(t)
100+
bytes, err := json.Marshal(newKeySet(jwkPub))
101+
require.NoError(t, err)
102+
oidcConfigWithSecretRef.JwksConfig = &oidcv1.OIDCConfig_Jwks{
103+
Jwks: string(bytes),
104+
}
105+
106+
clock := oidc.Clock{}
107+
sessions := &mockSessionStoreFactory{store: oidc.NewMemoryStore(&clock, time.Hour, time.Hour)}
108+
store := sessions.Get(oidcConfigWithSecretRef)
109+
h, err := NewOIDCHandler(oidcConfigWithSecretRef, oidc.NewJWKSProvider(), sessions, clock, oidc.NewStaticGenerator(newSessionID, newNonce, newState))
110+
require.NoError(t, err)
111+
112+
ctx := context.Background()
113+
114+
callbackTests := []struct {
115+
name string
116+
setup func(config *oidcv1.OIDCConfig)
117+
expectedStatusCodes int32
118+
}{
119+
{
120+
name: "with a valid k8s secret",
121+
setup: func(config *oidcv1.OIDCConfig) {
122+
config.ClientSecretConfig = &oidcv1.OIDCConfig_ClientSecretRef{
123+
ClientSecretRef: &oidcv1.OIDCConfig_SecretReference{
124+
Namespace: "default",
125+
Name: "test-client-secret",
126+
},
127+
}
128+
},
129+
expectedStatusCodes: int32(codes.Unauthenticated),
130+
},
131+
{
132+
name: "with a invalid k8s secret",
133+
setup: func(config *oidcv1.OIDCConfig) {
134+
config.ClientSecretConfig = &oidcv1.OIDCConfig_ClientSecretRef{
135+
ClientSecretRef: &oidcv1.OIDCConfig_SecretReference{
136+
Namespace: "default",
137+
Name: "invalid-test-client-secret",
138+
},
139+
}
140+
},
141+
expectedStatusCodes: int32(codes.Internal),
142+
},
143+
{
144+
name: "with a non-existing k8s secret",
145+
setup: func(config *oidcv1.OIDCConfig) {
146+
config.ClientSecretConfig = &oidcv1.OIDCConfig_ClientSecretRef{
147+
ClientSecretRef: &oidcv1.OIDCConfig_SecretReference{
148+
Namespace: "default",
149+
Name: "non-existing-test-client-secret",
150+
},
151+
}
152+
},
153+
expectedStatusCodes: int32(codes.Internal),
154+
},
155+
}
156+
157+
idpServer := newServer()
158+
h.(*oidcHandler).httpClient = idpServer.newHTTPClient()
159+
160+
for _, tt := range callbackTests {
161+
t.Run("request matches callback: "+tt.name, func(t *testing.T) {
162+
idpServer.Start()
163+
t.Cleanup(func() {
164+
idpServer.Stop()
165+
require.NoError(t, store.RemoveSession(ctx, sessionID))
166+
})
167+
168+
idpServer.tokensResponse = &idpTokensResponse{
169+
IDToken: newJWT(t, jwkPriv, jwt.NewBuilder().Audience([]string{"test-client-id"}).Claim("nonce", newNonce)),
170+
AccessToken: "access-token",
171+
TokenType: "Bearer",
172+
}
173+
idpServer.statusCode = http.StatusOK
174+
175+
// Set the authorization state in the store, so it can be found by the handler
176+
require.NoError(t, store.SetAuthorizationState(ctx, sessionID, validAuthState))
177+
178+
resp := &envoy.CheckResponse{}
179+
tt.setup(oidcConfigWithSecretRef)
180+
err = h.Process(ctx, callbackRequest, resp)
181+
require.NoError(t, err)
182+
183+
// just check the status code here because this test is only used to test client secrets in k8s secret
184+
require.Equal(t, tt.expectedStatusCodes, resp.GetStatus().GetCode())
185+
})
186+
}
187+
}
188+
189+
func startEnv() (*envtest.Environment, *rest.Config, error) {
190+
log.SetLogger(zap.New(zap.WriteTo(os.Stderr), zap.UseDevMode(true)))
191+
env := &envtest.Environment{}
192+
cfg, err := env.Start()
193+
if err != nil {
194+
return env, nil, err
195+
}
196+
return env, cfg, nil
197+
}

0 commit comments

Comments
 (0)