2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-07-31 16:08:07 +00:00
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-07-31 16:08:07 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
|
2020-08-26 02:02:27 +00:00
|
|
|
crdpinnipedv1alpha1 "github.com/suzerain-io/pinniped/generated/1.19/apis/crdpinniped/v1alpha1"
|
2020-08-20 17:54:15 +00:00
|
|
|
"github.com/suzerain-io/pinniped/test/library"
|
2020-07-31 16:08:07 +00:00
|
|
|
)
|
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
func TestCredentialIssuerConfig(t *testing.T) {
|
2020-08-07 01:44:14 +00:00
|
|
|
library.SkipUnlessIntegration(t)
|
2020-08-25 01:07:34 +00:00
|
|
|
namespaceName := library.GetEnv(t, "PINNIPED_NAMESPACE")
|
2020-07-31 16:08:07 +00:00
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
config := library.NewClientConfig(t)
|
2020-08-20 17:54:15 +00:00
|
|
|
client := library.NewPinnipedClientset(t)
|
2020-07-31 16:08:07 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
t.Run("test successful CredentialIssuerConfig", func(t *testing.T) {
|
|
|
|
actualConfigList, err := client.
|
|
|
|
CrdV1alpha1().
|
|
|
|
CredentialIssuerConfigs(namespaceName).
|
|
|
|
List(ctx, metav1.ListOptions{})
|
|
|
|
require.NoError(t, err)
|
2020-09-15 17:04:46 +00:00
|
|
|
for _, config := range actualConfigList.Items {
|
|
|
|
t.Logf("found CredentialIssuerConfig: %+v", config)
|
|
|
|
}
|
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
require.Len(t, actualConfigList.Items, 1)
|
2020-07-31 16:08:07 +00:00
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
actualStatusKubeConfigInfo := actualConfigList.Items[0].Status.KubeConfigInfo
|
2020-07-31 16:08:07 +00:00
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
// Verify the cluster strategy status based on what's expected of the test cluster's ability to share signing keys.
|
|
|
|
actualStatusStrategies := actualConfigList.Items[0].Status.Strategies
|
|
|
|
require.Len(t, actualStatusStrategies, 1)
|
|
|
|
actualStatusStrategy := actualStatusStrategies[0]
|
|
|
|
require.Equal(t, crdpinnipedv1alpha1.KubeClusterSigningCertificateStrategyType, actualStatusStrategy.Type)
|
2020-07-31 16:08:07 +00:00
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
if library.ClusterHasCapability(t, library.ClusterSigningKeyIsAvailable) {
|
|
|
|
require.Equal(t, crdpinnipedv1alpha1.SuccessStrategyStatus, actualStatusStrategy.Status)
|
|
|
|
require.Equal(t, crdpinnipedv1alpha1.FetchedKeyStrategyReason, actualStatusStrategy.Reason)
|
|
|
|
require.Equal(t, "Key was fetched successfully", actualStatusStrategy.Message)
|
2020-08-28 00:18:48 +00:00
|
|
|
// Verify the published kube config info.
|
|
|
|
require.Equal(t, expectedStatusKubeConfigInfo(config), actualStatusKubeConfigInfo)
|
2020-08-25 01:07:34 +00:00
|
|
|
} else {
|
|
|
|
require.Equal(t, crdpinnipedv1alpha1.ErrorStrategyStatus, actualStatusStrategy.Status)
|
|
|
|
require.Equal(t, crdpinnipedv1alpha1.CouldNotFetchKeyStrategyReason, actualStatusStrategy.Reason)
|
2020-08-28 00:50:46 +00:00
|
|
|
require.Contains(t, actualStatusStrategy.Message, "did not find kube-controller-manager pod")
|
2020-08-28 00:18:48 +00:00
|
|
|
// For now, don't verify the kube config info because its not available on GKE. We'll need to address
|
|
|
|
// this somehow once we starting supporting those cluster types.
|
|
|
|
// Require `nil` to remind us to address this later for other types of clusters where it is available.
|
|
|
|
require.Nil(t, actualStatusKubeConfigInfo)
|
2020-08-25 01:07:34 +00:00
|
|
|
}
|
2020-07-31 16:08:07 +00:00
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
require.WithinDuration(t, time.Now(), actualStatusStrategy.LastUpdateTime.Local(), 10*time.Minute)
|
|
|
|
})
|
2020-07-31 16:08:07 +00:00
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
t.Run("reconciling CredentialIssuerConfig", func(t *testing.T) {
|
|
|
|
library.SkipUnlessClusterHasCapability(t, library.ClusterSigningKeyIsAvailable)
|
2020-07-31 21:35:20 +00:00
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
existingConfig, err := client.
|
2020-08-20 17:54:15 +00:00
|
|
|
CrdV1alpha1().
|
2020-08-21 16:55:44 +00:00
|
|
|
CredentialIssuerConfigs(namespaceName).
|
2020-08-20 17:54:15 +00:00
|
|
|
Get(ctx, "pinniped-config", metav1.GetOptions{})
|
2020-08-25 01:07:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, existingConfig.Status.Strategies, 1)
|
|
|
|
initialStrategy := existingConfig.Status.Strategies[0]
|
|
|
|
|
|
|
|
// Mutate the existing object. Don't delete it because that would mess up its `Status.Strategies` array,
|
|
|
|
// since the reconciling controller is not currently responsible for that field.
|
2020-08-26 01:22:53 +00:00
|
|
|
updatedServerValue := "https://junk"
|
|
|
|
existingConfig.Status.KubeConfigInfo.Server = updatedServerValue
|
2020-08-25 01:07:34 +00:00
|
|
|
updatedConfig, err := client.
|
|
|
|
CrdV1alpha1().
|
|
|
|
CredentialIssuerConfigs(namespaceName).
|
|
|
|
Update(ctx, existingConfig, metav1.UpdateOptions{})
|
|
|
|
require.NoError(t, err)
|
2020-08-26 01:22:53 +00:00
|
|
|
require.Equal(t, updatedServerValue, updatedConfig.Status.KubeConfigInfo.Server)
|
2020-08-25 01:07:34 +00:00
|
|
|
|
2020-08-26 01:22:53 +00:00
|
|
|
// Expect that the object's mutated field is set back to what matches its source of truth by the controller.
|
2020-08-25 01:07:34 +00:00
|
|
|
var actualCredentialIssuerConfig *crdpinnipedv1alpha1.CredentialIssuerConfig
|
2020-08-26 01:22:53 +00:00
|
|
|
var configChangesServerField = func() bool {
|
2020-08-25 01:07:34 +00:00
|
|
|
actualCredentialIssuerConfig, err = client.
|
|
|
|
CrdV1alpha1().
|
|
|
|
CredentialIssuerConfigs(namespaceName).
|
|
|
|
Get(ctx, "pinniped-config", metav1.GetOptions{})
|
2020-08-26 01:22:53 +00:00
|
|
|
return err == nil && actualCredentialIssuerConfig.Status.KubeConfigInfo.Server != updatedServerValue
|
2020-07-31 16:08:07 +00:00
|
|
|
}
|
2020-08-26 01:22:53 +00:00
|
|
|
assert.Eventually(t, configChangesServerField, 10*time.Second, 100*time.Millisecond)
|
2020-08-25 01:07:34 +00:00
|
|
|
require.NoError(t, err) // prints out the error and stops the test in case of failure
|
|
|
|
actualStatusKubeConfigInfo := actualCredentialIssuerConfig.Status.KubeConfigInfo
|
|
|
|
require.Equal(t, expectedStatusKubeConfigInfo(config), actualStatusKubeConfigInfo)
|
|
|
|
|
|
|
|
// The strategies should not have changed during reconciliation.
|
|
|
|
require.Len(t, actualCredentialIssuerConfig.Status.Strategies, 1)
|
|
|
|
require.Equal(t, initialStrategy, actualCredentialIssuerConfig.Status.Strategies[0])
|
|
|
|
})
|
2020-07-31 16:08:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
func expectedStatusKubeConfigInfo(config *rest.Config) *crdpinnipedv1alpha1.CredentialIssuerConfigKubeConfigInfo {
|
|
|
|
return &crdpinnipedv1alpha1.CredentialIssuerConfigKubeConfigInfo{
|
|
|
|
Server: config.Host,
|
|
|
|
CertificateAuthorityData: base64.StdEncoding.EncodeToString(config.TLSClientConfig.CAData),
|
2020-07-31 16:08:07 +00:00
|
|
|
}
|
|
|
|
}
|