2023-06-01 19:54:45 +00:00
|
|
|
// Copyright 2020-2023 the Pinniped contributors. All Rights Reserved.
|
2020-12-14 20:53:12 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"gopkg.in/square/go-jose.v2"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
2021-02-16 19:00:08 +00:00
|
|
|
configv1alpha1 "go.pinniped.dev/generated/latest/apis/supervisor/config/v1alpha1"
|
2021-06-22 15:23:19 +00:00
|
|
|
"go.pinniped.dev/test/testlib"
|
2020-12-14 20:53:12 +00:00
|
|
|
)
|
|
|
|
|
2021-08-25 19:34:33 +00:00
|
|
|
// safe to run in parallel with serial tests since it only interacts with a test local federation domain, see main_test.go.
|
|
|
|
func TestSupervisorSecrets_Parallel(t *testing.T) {
|
2021-06-22 15:23:19 +00:00
|
|
|
env := testlib.IntegrationEnv(t)
|
|
|
|
kubeClient := testlib.NewKubernetesClientset(t)
|
|
|
|
supervisorClient := testlib.NewSupervisorClientset(t)
|
2020-12-14 20:53:12 +00:00
|
|
|
|
2023-06-01 19:54:45 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Minute)
|
2020-12-14 20:53:12 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-12-17 21:49:53 +00:00
|
|
|
// Create our FederationDomain under test.
|
2021-06-22 15:23:19 +00:00
|
|
|
federationDomain := testlib.CreateTestFederationDomain(ctx, t, "", "", "")
|
2020-12-14 20:53:12 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2020-12-17 21:49:53 +00:00
|
|
|
secretName func(federationDomain *configv1alpha1.FederationDomain) string
|
2020-12-14 20:53:12 +00:00
|
|
|
ensureValid func(t *testing.T, secret *corev1.Secret)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "csrf cookie signing key",
|
2020-12-17 21:49:53 +00:00
|
|
|
secretName: func(federationDomain *configv1alpha1.FederationDomain) string {
|
2020-12-14 20:53:12 +00:00
|
|
|
return env.SupervisorAppName + "-key"
|
|
|
|
},
|
2020-12-17 23:30:26 +00:00
|
|
|
ensureValid: ensureValidSymmetricSecretOfTypeFunc("secrets.pinniped.dev/supervisor-csrf-signing-key"),
|
2020-12-14 20:53:12 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "jwks",
|
2020-12-17 21:49:53 +00:00
|
|
|
secretName: func(federationDomain *configv1alpha1.FederationDomain) string {
|
|
|
|
return federationDomain.Status.Secrets.JWKS.Name
|
2020-12-14 20:53:12 +00:00
|
|
|
},
|
|
|
|
ensureValid: ensureValidJWKS,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hmac signing secret",
|
2020-12-17 21:49:53 +00:00
|
|
|
secretName: func(federationDomain *configv1alpha1.FederationDomain) string {
|
|
|
|
return federationDomain.Status.Secrets.TokenSigningKey.Name
|
2020-12-14 20:53:12 +00:00
|
|
|
},
|
2020-12-18 01:07:38 +00:00
|
|
|
ensureValid: ensureValidSymmetricSecretOfTypeFunc("secrets.pinniped.dev/federation-domain-token-signing-key"),
|
2020-12-14 20:53:12 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "state signature secret",
|
2020-12-17 21:49:53 +00:00
|
|
|
secretName: func(federationDomain *configv1alpha1.FederationDomain) string {
|
|
|
|
return federationDomain.Status.Secrets.StateSigningKey.Name
|
2020-12-14 20:53:12 +00:00
|
|
|
},
|
2020-12-18 01:07:38 +00:00
|
|
|
ensureValid: ensureValidSymmetricSecretOfTypeFunc("secrets.pinniped.dev/federation-domain-state-signing-key"),
|
2020-12-14 20:53:12 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "state encryption secret",
|
2020-12-17 21:49:53 +00:00
|
|
|
secretName: func(federationDomain *configv1alpha1.FederationDomain) string {
|
|
|
|
return federationDomain.Status.Secrets.StateEncryptionKey.Name
|
2020-12-14 20:53:12 +00:00
|
|
|
},
|
2020-12-18 01:07:38 +00:00
|
|
|
ensureValid: ensureValidSymmetricSecretOfTypeFunc("secrets.pinniped.dev/federation-domain-state-encryption-key"),
|
2020-12-14 20:53:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2020-12-17 21:49:53 +00:00
|
|
|
// Ensure a secret is created with the FederationDomain's JWKS.
|
|
|
|
var updatedFederationDomain *configv1alpha1.FederationDomain
|
2021-06-22 15:23:19 +00:00
|
|
|
testlib.RequireEventually(t, func(requireEventually *require.Assertions) {
|
2021-06-16 22:51:23 +00:00
|
|
|
resp, err := supervisorClient.
|
2020-12-14 20:53:12 +00:00
|
|
|
ConfigV1alpha1().
|
2020-12-16 22:27:09 +00:00
|
|
|
FederationDomains(env.SupervisorNamespace).
|
2020-12-17 21:49:53 +00:00
|
|
|
Get(ctx, federationDomain.Name, metav1.GetOptions{})
|
2021-06-16 22:51:23 +00:00
|
|
|
requireEventually.NoError(err)
|
|
|
|
requireEventually.NotEmpty(test.secretName(resp))
|
|
|
|
updatedFederationDomain = resp
|
2023-06-01 19:54:45 +00:00
|
|
|
}, time.Minute, time.Millisecond*500)
|
2020-12-14 20:53:12 +00:00
|
|
|
|
|
|
|
// Ensure the secret actually exists.
|
|
|
|
secret, err := kubeClient.
|
|
|
|
CoreV1().
|
|
|
|
Secrets(env.SupervisorNamespace).
|
2020-12-17 21:49:53 +00:00
|
|
|
Get(ctx, test.secretName(updatedFederationDomain), metav1.GetOptions{})
|
2020-12-14 20:53:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Ensure that the secret was labelled.
|
|
|
|
for k, v := range env.SupervisorCustomLabels {
|
|
|
|
require.Equalf(t, v, secret.Labels[k], "expected secret to have label `%s: %s`", k, v)
|
|
|
|
}
|
|
|
|
require.Equal(t, env.SupervisorAppName, secret.Labels["app"])
|
|
|
|
|
|
|
|
// Ensure that the secret is valid.
|
|
|
|
test.ensureValid(t, secret)
|
|
|
|
|
|
|
|
// Ensure upon deleting the secret, it is eventually brought back.
|
|
|
|
err = kubeClient.
|
|
|
|
CoreV1().
|
|
|
|
Secrets(env.SupervisorNamespace).
|
2020-12-17 21:49:53 +00:00
|
|
|
Delete(ctx, test.secretName(updatedFederationDomain), metav1.DeleteOptions{})
|
2020-12-14 20:53:12 +00:00
|
|
|
require.NoError(t, err)
|
2021-06-22 15:23:19 +00:00
|
|
|
testlib.RequireEventually(t, func(requireEventually *require.Assertions) {
|
2021-06-16 22:51:23 +00:00
|
|
|
var err error
|
2020-12-14 20:53:12 +00:00
|
|
|
secret, err = kubeClient.
|
|
|
|
CoreV1().
|
|
|
|
Secrets(env.SupervisorNamespace).
|
2020-12-17 21:49:53 +00:00
|
|
|
Get(ctx, test.secretName(updatedFederationDomain), metav1.GetOptions{})
|
2021-06-16 22:51:23 +00:00
|
|
|
requireEventually.NoError(err)
|
2023-06-01 19:54:45 +00:00
|
|
|
}, time.Minute, time.Millisecond*500)
|
2020-12-14 20:53:12 +00:00
|
|
|
|
|
|
|
// Ensure that the new secret is valid.
|
|
|
|
test.ensureValid(t, secret)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-17 21:49:53 +00:00
|
|
|
// Upon deleting the FederationDomain, the secret is deleted (we test this behavior in our uninstall tests).
|
2020-12-14 20:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ensureValidJWKS(t *testing.T, secret *corev1.Secret) {
|
|
|
|
t.Helper()
|
|
|
|
|
2020-12-17 22:48:49 +00:00
|
|
|
// Ensure the secret has the right type.
|
2020-12-17 23:43:20 +00:00
|
|
|
require.Equal(t, corev1.SecretType("secrets.pinniped.dev/federation-domain-jwks"), secret.Type)
|
2020-12-17 22:48:49 +00:00
|
|
|
|
2020-12-14 20:53:12 +00:00
|
|
|
// Ensure the secret has an active key.
|
|
|
|
jwkData, ok := secret.Data["activeJWK"]
|
|
|
|
require.True(t, ok, "secret is missing active jwk")
|
|
|
|
|
|
|
|
// Ensure the secret's active key is valid.
|
|
|
|
var activeJWK jose.JSONWebKey
|
|
|
|
require.NoError(t, json.Unmarshal(jwkData, &activeJWK))
|
|
|
|
require.True(t, activeJWK.Valid(), "active jwk is invalid")
|
|
|
|
require.False(t, activeJWK.IsPublic(), "active jwk is public")
|
|
|
|
|
|
|
|
// Ensure the secret has a JWKS.
|
|
|
|
jwksData, ok := secret.Data["jwks"]
|
|
|
|
require.True(t, ok, "secret is missing jwks")
|
|
|
|
|
|
|
|
// Ensure the secret's JWKS is valid, public, and contains the singing key.
|
|
|
|
var jwks jose.JSONWebKeySet
|
|
|
|
require.NoError(t, json.Unmarshal(jwksData, &jwks))
|
|
|
|
foundActiveJWK := false
|
|
|
|
for _, jwk := range jwks.Keys {
|
|
|
|
require.Truef(t, jwk.Valid(), "jwk %s is invalid", jwk.KeyID)
|
|
|
|
require.Truef(t, jwk.IsPublic(), "jwk %s is not public", jwk.KeyID)
|
|
|
|
if jwk.KeyID == activeJWK.KeyID {
|
|
|
|
foundActiveJWK = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.True(t, foundActiveJWK, "could not find active JWK in JWKS: %s", jwks)
|
|
|
|
}
|
|
|
|
|
2020-12-17 23:30:26 +00:00
|
|
|
func ensureValidSymmetricSecretOfTypeFunc(secretTypeValue string) func(*testing.T, *corev1.Secret) {
|
|
|
|
return func(t *testing.T, secret *corev1.Secret) {
|
|
|
|
t.Helper()
|
|
|
|
require.Equal(t, corev1.SecretType(secretTypeValue), secret.Type)
|
|
|
|
key, ok := secret.Data["key"]
|
|
|
|
require.Truef(t, ok, "secret data does not contain 'key': %s", secret.Data)
|
|
|
|
require.Equal(t, 32, len(key))
|
|
|
|
}
|
2020-12-14 20:53:12 +00:00
|
|
|
}
|