This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: huabing zhao <[email protected]>
- Loading branch information
1 parent
e1d1883
commit dea5e25
Showing
5 changed files
with
246 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
// Copyright 2024 Tetrate | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build integration | ||
// +build integration | ||
|
||
package authz | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
envoy "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3" | ||
"github.com/lestrrat-go/jwx/jwt" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc/codes" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/rest" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
|
||
oidcv1 "github.com/tetrateio/authservice-go/config/gen/go/v1/oidc" | ||
"github.com/tetrateio/authservice-go/internal/oidc" | ||
) | ||
|
||
var ( | ||
oidcConfigWithSecretRef = &oidcv1.OIDCConfig{ | ||
IdToken: &oidcv1.TokenConfig{ | ||
Header: "Authorization", | ||
Preamble: "Bearer", | ||
}, | ||
AccessToken: &oidcv1.TokenConfig{ | ||
Header: "X-Access-Token", | ||
Preamble: "Bearer", | ||
}, | ||
TokenUri: "http://idp-test-server/token", | ||
AuthorizationUri: "http://idp-test-server/auth", | ||
CallbackUri: "https://localhost:443/callback", | ||
ClientId: "test-client-id", | ||
Scopes: []string{"openid", "email"}, | ||
} | ||
) | ||
|
||
func TestOIDCProcessWithKubernetesSecret(t *testing.T) { | ||
// start kube test env | ||
testEnv, conf, err := startEnv() | ||
require.NoError(t, err) | ||
|
||
// set inClusterConfig for the client | ||
inClusterConfig = conf | ||
|
||
// create secrets for testing | ||
c, err := client.New(conf, client.Options{}) | ||
require.NoError(t, err) | ||
err = c.Create(context.Background(), &v1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "test-client-secret", | ||
}, | ||
Data: map[string][]byte{ | ||
clientSecretKey: []byte("test-client-secret"), | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
err = c.Create(context.Background(), &v1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "invalid-test-client-secret", | ||
}, | ||
Data: map[string][]byte{ | ||
clientSecretKey + "-invalid": []byte("test-client-secret"), | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
// stop kube test env | ||
defer func() { | ||
require.NoError(t, testEnv.Stop()) | ||
}() | ||
|
||
jwkPriv, jwkPub := newKeyPair(t) | ||
bytes, err := json.Marshal(newKeySet(jwkPub)) | ||
require.NoError(t, err) | ||
oidcConfigWithSecretRef.JwksConfig = &oidcv1.OIDCConfig_Jwks{ | ||
Jwks: string(bytes), | ||
} | ||
|
||
clock := oidc.Clock{} | ||
sessions := &mockSessionStoreFactory{store: oidc.NewMemoryStore(&clock, time.Hour, time.Hour)} | ||
store := sessions.Get(oidcConfigWithSecretRef) | ||
h, err := NewOIDCHandler(oidcConfigWithSecretRef, oidc.NewJWKSProvider(), sessions, clock, oidc.NewStaticGenerator(newSessionID, newNonce, newState)) | ||
require.NoError(t, err) | ||
|
||
ctx := context.Background() | ||
|
||
callbackTests := []struct { | ||
name string | ||
setup func(config *oidcv1.OIDCConfig) | ||
expectedStatusCodes int32 | ||
}{ | ||
{ | ||
name: "with a valid k8s secret", | ||
setup: func(config *oidcv1.OIDCConfig) { | ||
config.ClientSecretConfig = &oidcv1.OIDCConfig_ClientSecretRef{ | ||
ClientSecretRef: &oidcv1.OIDCConfig_SecretReference{ | ||
Namespace: "default", | ||
Name: "test-client-secret", | ||
}, | ||
} | ||
}, | ||
expectedStatusCodes: int32(codes.Unauthenticated), | ||
}, | ||
{ | ||
name: "with a invalid k8s secret", | ||
setup: func(config *oidcv1.OIDCConfig) { | ||
config.ClientSecretConfig = &oidcv1.OIDCConfig_ClientSecretRef{ | ||
ClientSecretRef: &oidcv1.OIDCConfig_SecretReference{ | ||
Namespace: "default", | ||
Name: "invalid-test-client-secret", | ||
}, | ||
} | ||
}, | ||
expectedStatusCodes: int32(codes.Internal), | ||
}, | ||
{ | ||
name: "with a non-existing k8s secret", | ||
setup: func(config *oidcv1.OIDCConfig) { | ||
config.ClientSecretConfig = &oidcv1.OIDCConfig_ClientSecretRef{ | ||
ClientSecretRef: &oidcv1.OIDCConfig_SecretReference{ | ||
Namespace: "default", | ||
Name: "non-existing-test-client-secret", | ||
}, | ||
} | ||
}, | ||
expectedStatusCodes: int32(codes.Internal), | ||
}, | ||
} | ||
|
||
idpServer := newServer() | ||
h.(*oidcHandler).httpClient = idpServer.newHTTPClient() | ||
|
||
for _, tt := range callbackTests { | ||
t.Run("request matches callback: "+tt.name, func(t *testing.T) { | ||
idpServer.Start() | ||
t.Cleanup(func() { | ||
idpServer.Stop() | ||
require.NoError(t, store.RemoveSession(ctx, sessionID)) | ||
}) | ||
|
||
idpServer.tokensResponse = &idpTokensResponse{ | ||
IDToken: newJWT(t, jwkPriv, jwt.NewBuilder().Audience([]string{"test-client-id"}).Claim("nonce", newNonce)), | ||
AccessToken: "access-token", | ||
TokenType: "Bearer", | ||
} | ||
idpServer.statusCode = http.StatusOK | ||
|
||
// Set the authorization state in the store, so it can be found by the handler | ||
require.NoError(t, store.SetAuthorizationState(ctx, sessionID, validAuthState)) | ||
|
||
resp := &envoy.CheckResponse{} | ||
tt.setup(oidcConfigWithSecretRef) | ||
err = h.Process(ctx, callbackRequest, resp) | ||
require.NoError(t, err) | ||
|
||
// just check the status code here because this test is only used to test client secrets in k8s secret | ||
require.Equal(t, tt.expectedStatusCodes, resp.GetStatus().GetCode()) | ||
}) | ||
} | ||
} | ||
|
||
func startEnv() (*envtest.Environment, *rest.Config, error) { | ||
log.SetLogger(zap.New(zap.WriteTo(os.Stderr), zap.UseDevMode(true))) | ||
env := &envtest.Environment{} | ||
cfg, err := env.Start() | ||
if err != nil { | ||
return env, nil, err | ||
} | ||
return env, cfg, nil | ||
} |