2020-07-31 16:08:07 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 VMware, Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
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)
|
|
|
|
require.Len(t, actualConfigList.Items, 1)
|
2020-07-31 16:08:07 +00:00
|
|
|
|
2020-08-25 01:07:34 +00:00
|
|
|
// Verify the published kube config info.
|
|
|
|
actualStatusKubeConfigInfo := actualConfigList.Items[0].Status.KubeConfigInfo
|
|
|
|
require.Equal(t, expectedStatusKubeConfigInfo(config), actualStatusKubeConfigInfo)
|
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)
|
|
|
|
} else {
|
|
|
|
require.Equal(t, crdpinnipedv1alpha1.ErrorStrategyStatus, actualStatusStrategy.Status)
|
|
|
|
require.Equal(t, crdpinnipedv1alpha1.CouldNotFetchKeyStrategyReason, actualStatusStrategy.Reason)
|
|
|
|
require.Contains(t, actualStatusStrategy.Message, "some part of the error message")
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|