2020-07-23 15:05:21 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 VMware, Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-14 14:01:38 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2020-07-24 15:40:08 +00:00
|
|
|
"net/http"
|
2020-07-23 15:05:21 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-24 22:57:29 +00:00
|
|
|
rbacv1 "k8s.io/api/rbac/v1"
|
2020-07-23 15:05:21 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-08-03 22:29:55 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2020-07-23 15:05:21 +00:00
|
|
|
|
2020-08-05 20:58:03 +00:00
|
|
|
"github.com/suzerain-io/placeholder-name/kubernetes/1.19/api/apis/placeholder/v1alpha1"
|
2020-07-23 15:05:21 +00:00
|
|
|
"github.com/suzerain-io/placeholder-name/test/library"
|
|
|
|
)
|
|
|
|
|
2020-08-14 14:11:14 +00:00
|
|
|
func makeRequest(t *testing.T, spec v1alpha1.CredentialRequestSpec) (*v1alpha1.CredentialRequest, error) {
|
2020-07-23 15:05:21 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2020-08-12 21:29:46 +00:00
|
|
|
client := library.NewAnonymousPlaceholderNameClientset(t)
|
2020-07-23 15:05:21 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-08-14 14:11:14 +00:00
|
|
|
return client.PlaceholderV1alpha1().CredentialRequests().Create(ctx, &v1alpha1.CredentialRequest{
|
2020-07-23 15:05:21 +00:00
|
|
|
TypeMeta: metav1.TypeMeta{},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{},
|
|
|
|
Spec: spec,
|
|
|
|
}, metav1.CreateOptions{})
|
|
|
|
}
|
|
|
|
|
2020-08-03 22:29:55 +00:00
|
|
|
func addTestClusterRoleBinding(ctx context.Context, t *testing.T, adminClient kubernetes.Interface, binding *rbacv1.ClusterRoleBinding) {
|
|
|
|
_, err := adminClient.RbacV1().ClusterRoleBindings().Get(ctx, binding.Name, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
// "404 not found" errors are acceptable, but others would be unexpected
|
|
|
|
statusError, isStatus := err.(*errors.StatusError)
|
|
|
|
require.True(t, isStatus)
|
|
|
|
require.Equal(t, http.StatusNotFound, int(statusError.Status().Code))
|
|
|
|
|
|
|
|
_, err = adminClient.RbacV1().ClusterRoleBindings().Create(ctx, binding, metav1.CreateOptions{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
err = adminClient.RbacV1().ClusterRoleBindings().Delete(ctx, binding.Name, metav1.DeleteOptions{})
|
|
|
|
require.NoError(t, err, "Test failed to clean up after itself")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-14 14:11:14 +00:00
|
|
|
func TestSuccessfulCredentialRequest(t *testing.T) {
|
2020-08-07 01:44:14 +00:00
|
|
|
library.SkipUnlessIntegration(t)
|
2020-08-03 14:17:11 +00:00
|
|
|
tmcClusterToken := library.Getenv(t, "PLACEHOLDER_NAME_TMC_CLUSTER_TOKEN")
|
2020-07-23 15:05:21 +00:00
|
|
|
|
2020-08-14 14:11:14 +00:00
|
|
|
response, err := makeRequest(t, v1alpha1.CredentialRequestSpec{
|
|
|
|
Type: v1alpha1.TokenCredentialType,
|
|
|
|
Token: &v1alpha1.CredentialRequestTokenCredential{Value: tmcClusterToken},
|
2020-07-23 15:05:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
2020-07-24 22:57:29 +00:00
|
|
|
|
|
|
|
// Note: If this assertion fails then your TMC token might have expired. Get a fresh one and try again.
|
2020-07-23 23:01:55 +00:00
|
|
|
require.Empty(t, response.Status.Message)
|
2020-07-23 15:05:21 +00:00
|
|
|
|
|
|
|
require.Empty(t, response.Spec)
|
2020-07-23 23:01:55 +00:00
|
|
|
require.NotNil(t, response.Status.Credential)
|
2020-07-24 15:40:08 +00:00
|
|
|
require.Empty(t, response.Status.Credential.Token)
|
|
|
|
require.NotEmpty(t, response.Status.Credential.ClientCertificateData)
|
|
|
|
require.NotEmpty(t, response.Status.Credential.ClientKeyData)
|
2020-07-30 21:41:12 +00:00
|
|
|
require.NotNil(t, response.Status.Credential.ExpirationTimestamp)
|
2020-08-11 20:09:31 +00:00
|
|
|
require.InDelta(t, time.Until(response.Status.Credential.ExpirationTimestamp.Time), 1*time.Hour, float64(3*time.Minute))
|
2020-07-24 15:40:08 +00:00
|
|
|
|
2020-07-24 22:57:29 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-08-03 22:29:55 +00:00
|
|
|
// Create a client using the admin kubeconfig.
|
2020-07-24 22:57:29 +00:00
|
|
|
adminClient := library.NewClientset(t)
|
|
|
|
|
2020-08-14 14:11:14 +00:00
|
|
|
// Create a client using the certificate from the CredentialRequest.
|
|
|
|
clientWithCertFromCredentialRequest := library.NewClientsetWithCertAndKey(
|
2020-08-03 22:29:55 +00:00
|
|
|
t,
|
2020-08-12 21:29:46 +00:00
|
|
|
response.Status.Credential.ClientCertificateData,
|
|
|
|
response.Status.Credential.ClientKeyData,
|
2020-08-03 22:29:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
t.Run("access as user", func(t *testing.T) {
|
|
|
|
addTestClusterRoleBinding(ctx, t, adminClient, &rbacv1.ClusterRoleBinding{
|
2020-07-24 22:57:29 +00:00
|
|
|
TypeMeta: metav1.TypeMeta{},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2020-08-03 22:29:55 +00:00
|
|
|
Name: "integration-test-user-readonly-role-binding",
|
2020-07-24 22:57:29 +00:00
|
|
|
},
|
|
|
|
Subjects: []rbacv1.Subject{{
|
|
|
|
Kind: rbacv1.UserKind,
|
|
|
|
APIGroup: rbacv1.GroupName,
|
2020-08-14 14:01:38 +00:00
|
|
|
Name: getCommonName(t, response.Status.Credential.ClientCertificateData),
|
2020-07-24 22:57:29 +00:00
|
|
|
}},
|
|
|
|
RoleRef: rbacv1.RoleRef{
|
|
|
|
Kind: "ClusterRole",
|
|
|
|
APIGroup: rbacv1.GroupName,
|
|
|
|
Name: "view",
|
|
|
|
},
|
2020-08-03 22:29:55 +00:00
|
|
|
})
|
2020-07-24 22:57:29 +00:00
|
|
|
|
2020-08-03 22:29:55 +00:00
|
|
|
// Use the client which is authenticated as the TMC user to list namespaces
|
2020-08-14 14:11:14 +00:00
|
|
|
listNamespaceResponse, err := clientWithCertFromCredentialRequest.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
|
2020-08-03 22:29:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, listNamespaceResponse.Items)
|
|
|
|
})
|
2020-07-24 22:57:29 +00:00
|
|
|
|
2020-08-03 22:29:55 +00:00
|
|
|
t.Run("access as group", func(t *testing.T) {
|
|
|
|
addTestClusterRoleBinding(ctx, t, adminClient, &rbacv1.ClusterRoleBinding{
|
|
|
|
TypeMeta: metav1.TypeMeta{},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "integration-test-group-readonly-role-binding",
|
|
|
|
},
|
|
|
|
Subjects: []rbacv1.Subject{{
|
|
|
|
Kind: rbacv1.GroupKind,
|
|
|
|
APIGroup: rbacv1.GroupName,
|
|
|
|
Name: "tmc:member",
|
|
|
|
}},
|
|
|
|
RoleRef: rbacv1.RoleRef{
|
|
|
|
Kind: "ClusterRole",
|
|
|
|
APIGroup: rbacv1.GroupName,
|
|
|
|
Name: "view",
|
|
|
|
},
|
|
|
|
})
|
2020-07-24 15:40:08 +00:00
|
|
|
|
2020-08-03 22:29:55 +00:00
|
|
|
// Use the client which is authenticated as the TMC group to list namespaces
|
2020-08-14 14:11:14 +00:00
|
|
|
listNamespaceResponse, err := clientWithCertFromCredentialRequest.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
|
2020-08-03 22:29:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, listNamespaceResponse.Items)
|
|
|
|
})
|
2020-07-23 23:01:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 14:11:14 +00:00
|
|
|
func TestFailedCredentialRequestWhenTheRequestIsValidButTheTokenDoesNotAuthenticateTheUser(t *testing.T) {
|
2020-08-07 01:44:14 +00:00
|
|
|
library.SkipUnlessIntegration(t)
|
2020-08-14 14:11:14 +00:00
|
|
|
response, err := makeRequest(t, v1alpha1.CredentialRequestSpec{
|
|
|
|
Type: v1alpha1.TokenCredentialType,
|
|
|
|
Token: &v1alpha1.CredentialRequestTokenCredential{Value: "not a good token"},
|
2020-07-23 23:01:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Empty(t, response.Spec)
|
|
|
|
require.Nil(t, response.Status.Credential)
|
2020-08-14 13:18:31 +00:00
|
|
|
require.Equal(t, stringPtr("authentication failed"), response.Status.Message)
|
2020-07-23 15:05:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 14:11:14 +00:00
|
|
|
func TestCredentialRequest_ShouldFailWhenRequestDoesNotIncludeToken(t *testing.T) {
|
2020-08-07 01:44:14 +00:00
|
|
|
library.SkipUnlessIntegration(t)
|
2020-08-14 14:11:14 +00:00
|
|
|
response, err := makeRequest(t, v1alpha1.CredentialRequestSpec{
|
|
|
|
Type: v1alpha1.TokenCredentialType,
|
2020-07-23 23:01:55 +00:00
|
|
|
Token: nil,
|
|
|
|
})
|
2020-07-23 15:05:21 +00:00
|
|
|
|
|
|
|
require.Error(t, err)
|
|
|
|
statusError, isStatus := err.(*errors.StatusError)
|
|
|
|
require.True(t, isStatus)
|
|
|
|
|
|
|
|
require.Equal(t, 1, len(statusError.ErrStatus.Details.Causes))
|
|
|
|
cause := statusError.ErrStatus.Details.Causes[0]
|
|
|
|
require.Equal(t, metav1.CauseType("FieldValueRequired"), cause.Type)
|
|
|
|
require.Equal(t, "Required value: token must be supplied", cause.Message)
|
|
|
|
require.Equal(t, "spec.token.value", cause.Field)
|
2020-07-24 15:40:08 +00:00
|
|
|
|
|
|
|
require.Empty(t, response.Spec)
|
|
|
|
require.Nil(t, response.Status.Credential)
|
2020-07-23 15:05:21 +00:00
|
|
|
}
|
2020-08-14 13:18:31 +00:00
|
|
|
|
|
|
|
func stringPtr(s string) *string {
|
|
|
|
return &s
|
|
|
|
}
|
2020-08-14 14:01:38 +00:00
|
|
|
|
|
|
|
func getCommonName(t *testing.T, certPEM string) string {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
pemBlock, _ := pem.Decode([]byte(certPEM))
|
|
|
|
cert, err := x509.ParseCertificate(pemBlock.Bytes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return cert.Subject.CommonName
|
|
|
|
}
|