|
| 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