mirror of
https://github.com/vmware-tanzu/pinniped.git
synced 2026-01-03 11:45:45 +00:00
First draft of a GitHubIdentityProvider CRD
This commit is contained in:
@@ -248,6 +248,20 @@ func TestGetAPIResourceList(t *testing.T) { //nolint:gocyclo // each t.Run is pr
|
||||
Kind: "ActiveDirectoryIdentityProvider",
|
||||
Verbs: []string{"get", "patch", "update"},
|
||||
},
|
||||
{
|
||||
Name: "githubidentityproviders",
|
||||
SingularName: "githubidentityprovider",
|
||||
Namespaced: true,
|
||||
Kind: "GitHubIdentityProvider",
|
||||
Verbs: []string{"delete", "deletecollection", "get", "list", "patch", "create", "update", "watch"},
|
||||
Categories: []string{"pinniped", "pinniped-idp", "pinniped-idps"},
|
||||
},
|
||||
{
|
||||
Name: "githubidentityproviders/status",
|
||||
Namespaced: true,
|
||||
Kind: "GitHubIdentityProvider",
|
||||
Verbs: []string{"get", "patch", "update"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -438,7 +452,7 @@ func TestGetAPIResourceList(t *testing.T) { //nolint:gocyclo // each t.Run is pr
|
||||
}
|
||||
|
||||
// manually update this value whenever you add additional fields to an API resource and then run the generator
|
||||
totalExpectedAPIFields := 261
|
||||
totalExpectedAPIFields := 287
|
||||
|
||||
// Because we are parsing text from `kubectl explain` and because the format of that text can change
|
||||
// over time, make a rudimentary assertion that this test exercised the whole tree of all fields of all
|
||||
@@ -579,6 +593,13 @@ func TestCRDAdditionalPrinterColumns_Parallel(t *testing.T) {
|
||||
{Name: "Age", Type: "date", JSONPath: ".metadata.creationTimestamp"},
|
||||
},
|
||||
},
|
||||
addSuffix("githubidentityproviders.idp.supervisor"): {
|
||||
"v1alpha1": []apiextensionsv1.CustomResourceColumnDefinition{
|
||||
{Name: "Host", Type: "string", JSONPath: ".spec.githubAPI.host"},
|
||||
{Name: "Status", Type: "string", JSONPath: ".status.phase"},
|
||||
{Name: "Age", Type: "date", JSONPath: ".metadata.creationTimestamp"},
|
||||
},
|
||||
},
|
||||
addSuffix("oidcclients.config.supervisor"): {
|
||||
"v1alpha1": []apiextensionsv1.CustomResourceColumnDefinition{
|
||||
{Name: "Privileged Scopes", Type: "string", JSONPath: `.spec.allowedScopes[?(@ == "pinniped:request-audience")]`},
|
||||
@@ -589,8 +610,20 @@ func TestCRDAdditionalPrinterColumns_Parallel(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
actualPinnipedCRDCount := 0
|
||||
expectedPinnipedCRDCount := 8 // the current number of CRDs that we ship as part of Pinniped
|
||||
// the current CRDs that we ship as part of Pinniped
|
||||
expectedPinnipedCRDNames := []string{
|
||||
"activedirectoryidentityproviders.idp.supervisor." + env.APIGroupSuffix,
|
||||
"credentialissuers.config.concierge." + env.APIGroupSuffix,
|
||||
"federationdomains.config.supervisor." + env.APIGroupSuffix,
|
||||
"githubidentityproviders.idp.supervisor." + env.APIGroupSuffix,
|
||||
"jwtauthenticators.authentication.concierge." + env.APIGroupSuffix,
|
||||
"ldapidentityproviders.idp.supervisor." + env.APIGroupSuffix,
|
||||
"oidcclients.config.supervisor." + env.APIGroupSuffix,
|
||||
"oidcidentityproviders.idp.supervisor." + env.APIGroupSuffix,
|
||||
"webhookauthenticators.authentication.concierge." + env.APIGroupSuffix,
|
||||
}
|
||||
|
||||
actualPinnipedCRDNames := make([]string, 0)
|
||||
|
||||
for _, crd := range crdList.Items {
|
||||
if !strings.Contains(crd.Spec.Group, env.APIGroupSuffix) {
|
||||
@@ -598,7 +631,7 @@ func TestCRDAdditionalPrinterColumns_Parallel(t *testing.T) {
|
||||
}
|
||||
|
||||
// Found a Pinniped CRD, so let's check it for AdditionalPrinterColumns.
|
||||
actualPinnipedCRDCount++
|
||||
actualPinnipedCRDNames = append(actualPinnipedCRDNames, crd.Name)
|
||||
|
||||
for _, version := range crd.Spec.Versions {
|
||||
expectedColumns, ok := expectedColumnsPerCRDVersion[crd.Name][version.Name]
|
||||
@@ -612,7 +645,7 @@ func TestCRDAdditionalPrinterColumns_Parallel(t *testing.T) {
|
||||
}
|
||||
|
||||
// Make sure that the logic of this test did not accidentally skip a CRD that it should have interrogated.
|
||||
require.Equal(t, expectedPinnipedCRDCount, actualPinnipedCRDCount,
|
||||
require.ElementsMatch(t, expectedPinnipedCRDNames, actualPinnipedCRDNames,
|
||||
"did not find expected number of Pinniped CRDs to check for additionalPrinterColumns")
|
||||
}
|
||||
|
||||
|
||||
307
test/integration/supervisor_github_idp_test.go
Normal file
307
test/integration/supervisor_github_idp_test.go
Normal file
@@ -0,0 +1,307 @@
|
||||
// Copyright 2024 the Pinniped contributors. All Rights Reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/utils/ptr"
|
||||
|
||||
idpv1alpha1 "go.pinniped.dev/generated/latest/apis/supervisor/idp/v1alpha1"
|
||||
"go.pinniped.dev/internal/testutil"
|
||||
|
||||
"go.pinniped.dev/test/testlib"
|
||||
)
|
||||
|
||||
func TestGitHubIDPStaticValidationOnCreate_Parallel(t *testing.T) {
|
||||
adminClient := testlib.NewKubernetesClientset(t)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
||||
t.Cleanup(cancel)
|
||||
|
||||
namespaceClient := adminClient.CoreV1().Namespaces()
|
||||
skipCELTests := !testutil.KubeServerMinorVersionAtLeastInclusive(t, adminClient.Discovery(), 26)
|
||||
|
||||
ns, err := namespaceClient.Create(ctx, &corev1.Namespace{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
GenerateName: "test-github-idp-",
|
||||
},
|
||||
}, metav1.CreateOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, namespaceClient.Delete(ctx, ns.Name, metav1.DeleteOptions{}))
|
||||
})
|
||||
|
||||
gitHubIDPClient := testlib.NewSupervisorClientset(t).IDPV1alpha1().GitHubIdentityProviders(ns.Name)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
inputSpec idpv1alpha1.GitHubIdentityProviderSpec
|
||||
expectedSpec idpv1alpha1.GitHubIdentityProviderSpec
|
||||
usesCELValidation bool
|
||||
expectedErr string
|
||||
}{
|
||||
{
|
||||
name: "all fields set",
|
||||
inputSpec: idpv1alpha1.GitHubIdentityProviderSpec{
|
||||
GitHubAPI: idpv1alpha1.GitHubAPIConfig{
|
||||
Host: ptr.To("some-host.example.com"),
|
||||
TLS: &idpv1alpha1.TLSSpec{
|
||||
CertificateAuthorityData: func() string {
|
||||
return base64.StdEncoding.EncodeToString([]byte("-----BEGIN CERTIFICATE-----\ndata goes here"))
|
||||
}(),
|
||||
},
|
||||
},
|
||||
AllowAuthentication: idpv1alpha1.GitHubAllowAuthenticationSpec{
|
||||
Organizations: idpv1alpha1.GitHubOrganizationsSpec{
|
||||
Allowed: []string{
|
||||
"org1",
|
||||
"that-other-org",
|
||||
},
|
||||
Policy: ptr.To(idpv1alpha1.GitHubAllowedAuthOrganizationsPolicyOnlyUsersFromAllowedOrganizations),
|
||||
},
|
||||
},
|
||||
Claims: idpv1alpha1.GitHubClaims{
|
||||
Username: ptr.To(idpv1alpha1.GitHubUsernameLoginAndID),
|
||||
Groups: ptr.To(idpv1alpha1.GitHubUseTeamSlugForGroupName),
|
||||
},
|
||||
Client: idpv1alpha1.GitHubClientSpec{
|
||||
SecretName: "any-name-goes-here",
|
||||
},
|
||||
},
|
||||
expectedSpec: idpv1alpha1.GitHubIdentityProviderSpec{
|
||||
GitHubAPI: idpv1alpha1.GitHubAPIConfig{
|
||||
Host: ptr.To("some-host.example.com"),
|
||||
TLS: &idpv1alpha1.TLSSpec{
|
||||
CertificateAuthorityData: "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCmRhdGEgZ29lcyBoZXJl",
|
||||
},
|
||||
},
|
||||
AllowAuthentication: idpv1alpha1.GitHubAllowAuthenticationSpec{
|
||||
Organizations: idpv1alpha1.GitHubOrganizationsSpec{
|
||||
Allowed: []string{
|
||||
"org1",
|
||||
"that-other-org",
|
||||
},
|
||||
Policy: ptr.To(idpv1alpha1.GitHubAllowedAuthOrganizationsPolicyOnlyUsersFromAllowedOrganizations),
|
||||
},
|
||||
},
|
||||
Claims: idpv1alpha1.GitHubClaims{
|
||||
Username: ptr.To(idpv1alpha1.GitHubUsernameLoginAndID),
|
||||
Groups: ptr.To(idpv1alpha1.GitHubUseTeamSlugForGroupName),
|
||||
},
|
||||
Client: idpv1alpha1.GitHubClientSpec{
|
||||
SecretName: "any-name-goes-here",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "minimum fields set - inherit defaults",
|
||||
inputSpec: idpv1alpha1.GitHubIdentityProviderSpec{
|
||||
AllowAuthentication: idpv1alpha1.GitHubAllowAuthenticationSpec{
|
||||
Organizations: idpv1alpha1.GitHubOrganizationsSpec{
|
||||
Policy: ptr.To(idpv1alpha1.GitHubAllowedAuthOrganizationsPolicyAllGitHubUsers),
|
||||
},
|
||||
},
|
||||
Client: idpv1alpha1.GitHubClientSpec{
|
||||
SecretName: "name-of-a-secret",
|
||||
},
|
||||
},
|
||||
expectedSpec: idpv1alpha1.GitHubIdentityProviderSpec{
|
||||
GitHubAPI: idpv1alpha1.GitHubAPIConfig{
|
||||
Host: ptr.To("github.com"),
|
||||
},
|
||||
AllowAuthentication: idpv1alpha1.GitHubAllowAuthenticationSpec{
|
||||
Organizations: idpv1alpha1.GitHubOrganizationsSpec{
|
||||
Policy: ptr.To(idpv1alpha1.GitHubAllowedAuthOrganizationsPolicyAllGitHubUsers),
|
||||
},
|
||||
},
|
||||
Claims: idpv1alpha1.GitHubClaims{
|
||||
Username: ptr.To(idpv1alpha1.GitHubUsernameLoginAndID),
|
||||
Groups: ptr.To(idpv1alpha1.GitHubUseTeamSlugForGroupName),
|
||||
},
|
||||
Client: idpv1alpha1.GitHubClientSpec{
|
||||
SecretName: "name-of-a-secret",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: fmt.Sprintf(
|
||||
"cannot set AllowedOrganizationsPolicy=%s and set AllowedOrganizations",
|
||||
string(idpv1alpha1.GitHubAllowedAuthOrganizationsPolicyAllGitHubUsers)),
|
||||
inputSpec: idpv1alpha1.GitHubIdentityProviderSpec{
|
||||
AllowAuthentication: idpv1alpha1.GitHubAllowAuthenticationSpec{
|
||||
Organizations: idpv1alpha1.GitHubOrganizationsSpec{
|
||||
Allowed: []string{
|
||||
"some-org",
|
||||
},
|
||||
Policy: ptr.To(idpv1alpha1.GitHubAllowedAuthOrganizationsPolicyAllGitHubUsers),
|
||||
},
|
||||
},
|
||||
Client: idpv1alpha1.GitHubClientSpec{
|
||||
SecretName: "name-of-a-secret",
|
||||
},
|
||||
},
|
||||
usesCELValidation: true,
|
||||
expectedErr: "spec.allowAuthentication.organizations.policy must be 'OnlyUsersFromAllowedOrganizations' when spec.allowAuthentication.organizations.allowed has organizations listed",
|
||||
},
|
||||
{
|
||||
name: fmt.Sprintf("spec.allowAuthentication.organizations.policy must be '%s' when spec.allowAuthentication.organizations.allowed is empty (nil)", string(idpv1alpha1.GitHubAllowedAuthOrganizationsPolicyAllGitHubUsers)),
|
||||
inputSpec: idpv1alpha1.GitHubIdentityProviderSpec{
|
||||
AllowAuthentication: idpv1alpha1.GitHubAllowAuthenticationSpec{
|
||||
Organizations: idpv1alpha1.GitHubOrganizationsSpec{
|
||||
Policy: ptr.To(idpv1alpha1.GitHubAllowedAuthOrganizationsPolicyOnlyUsersFromAllowedOrganizations),
|
||||
},
|
||||
},
|
||||
Client: idpv1alpha1.GitHubClientSpec{
|
||||
SecretName: "name-of-a-secret",
|
||||
},
|
||||
},
|
||||
usesCELValidation: true,
|
||||
expectedErr: "spec.allowAuthentication.organizations.policy must be 'AllGitHubUsers' when spec.allowAuthentication.organizations.allowed is empty",
|
||||
},
|
||||
{
|
||||
name: fmt.Sprintf("spec.allowAuthentication.organizations.policy must be '%s' when spec.allowAuthentication.organizations.allowed is empty", string(idpv1alpha1.GitHubAllowedAuthOrganizationsPolicyAllGitHubUsers)),
|
||||
inputSpec: idpv1alpha1.GitHubIdentityProviderSpec{
|
||||
AllowAuthentication: idpv1alpha1.GitHubAllowAuthenticationSpec{
|
||||
Organizations: idpv1alpha1.GitHubOrganizationsSpec{
|
||||
Allowed: []string{},
|
||||
Policy: ptr.To(idpv1alpha1.GitHubAllowedAuthOrganizationsPolicyOnlyUsersFromAllowedOrganizations),
|
||||
},
|
||||
},
|
||||
Client: idpv1alpha1.GitHubClientSpec{
|
||||
SecretName: "name-of-a-secret",
|
||||
},
|
||||
},
|
||||
usesCELValidation: true,
|
||||
expectedErr: "spec.allowAuthentication.organizations.policy must be 'AllGitHubUsers' when spec.allowAuthentication.organizations.allowed is empty",
|
||||
},
|
||||
{
|
||||
name: "spec.client.secretName in body should be at least 1 chars long",
|
||||
inputSpec: idpv1alpha1.GitHubIdentityProviderSpec{},
|
||||
expectedErr: "spec.client.secretName in body should be at least 1 chars long",
|
||||
},
|
||||
{
|
||||
name: "spec.githubAPI.host in body should be at least 1 chars long",
|
||||
inputSpec: idpv1alpha1.GitHubIdentityProviderSpec{
|
||||
GitHubAPI: idpv1alpha1.GitHubAPIConfig{
|
||||
Host: ptr.To(""),
|
||||
},
|
||||
AllowAuthentication: idpv1alpha1.GitHubAllowAuthenticationSpec{
|
||||
Organizations: idpv1alpha1.GitHubOrganizationsSpec{
|
||||
Policy: ptr.To(idpv1alpha1.GitHubAllowedAuthOrganizationsPolicyAllGitHubUsers),
|
||||
},
|
||||
},
|
||||
Client: idpv1alpha1.GitHubClientSpec{
|
||||
SecretName: "name-of-a-secret",
|
||||
},
|
||||
},
|
||||
expectedErr: "spec.githubAPI.host in body should be at least 1 chars long",
|
||||
},
|
||||
{
|
||||
name: "duplicates not permitted in spec.allowAuthentication.organizations.allowed",
|
||||
inputSpec: idpv1alpha1.GitHubIdentityProviderSpec{
|
||||
AllowAuthentication: idpv1alpha1.GitHubAllowAuthenticationSpec{
|
||||
Organizations: idpv1alpha1.GitHubOrganizationsSpec{
|
||||
Allowed: []string{
|
||||
"org1",
|
||||
"org1",
|
||||
},
|
||||
Policy: ptr.To(idpv1alpha1.GitHubAllowedAuthOrganizationsPolicyOnlyUsersFromAllowedOrganizations),
|
||||
},
|
||||
},
|
||||
Client: idpv1alpha1.GitHubClientSpec{
|
||||
SecretName: "name-of-a-secret",
|
||||
},
|
||||
},
|
||||
expectedErr: `spec.allowAuthentication.organizations.allowed[1]: Duplicate value: "org1"`,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if tt.usesCELValidation && skipCELTests {
|
||||
t.Skip("CEL is not available for current K8s version")
|
||||
}
|
||||
|
||||
input := &idpv1alpha1.GitHubIdentityProvider{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
GenerateName: "integration-test-",
|
||||
},
|
||||
Spec: tt.inputSpec,
|
||||
}
|
||||
|
||||
outputGitHubIDP, err := gitHubIDPClient.Create(ctx, input, metav1.CreateOptions{})
|
||||
if tt.expectedErr == "" {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tt.expectedSpec, outputGitHubIDP.Spec)
|
||||
} else {
|
||||
require.ErrorContains(t, err, tt.expectedErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGitHubIDPTooManyOrganizationsStaticValidationOnCreate_Parallel(t *testing.T) {
|
||||
adminClient := testlib.NewKubernetesClientset(t)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
||||
t.Cleanup(cancel)
|
||||
|
||||
namespaceClient := adminClient.CoreV1().Namespaces()
|
||||
|
||||
ns, err := namespaceClient.Create(ctx, &corev1.Namespace{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
GenerateName: "test-github-idp-",
|
||||
},
|
||||
}, metav1.CreateOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, namespaceClient.Delete(ctx, ns.Name, metav1.DeleteOptions{}))
|
||||
})
|
||||
|
||||
gitHubIDPClient := testlib.NewSupervisorClientset(t).IDPV1alpha1().GitHubIdentityProviders(ns.Name)
|
||||
|
||||
input := &idpv1alpha1.GitHubIdentityProvider{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
GenerateName: "integration-test-",
|
||||
},
|
||||
Spec: idpv1alpha1.GitHubIdentityProviderSpec{
|
||||
AllowAuthentication: idpv1alpha1.GitHubAllowAuthenticationSpec{
|
||||
Organizations: idpv1alpha1.GitHubOrganizationsSpec{
|
||||
Allowed: func() []string {
|
||||
var orgs []string
|
||||
for i := 0; i < 100; i++ {
|
||||
orgs = append(orgs, fmt.Sprintf("org-%d", i))
|
||||
}
|
||||
return orgs
|
||||
}(),
|
||||
Policy: ptr.To(idpv1alpha1.GitHubAllowedAuthOrganizationsPolicyOnlyUsersFromAllowedOrganizations),
|
||||
},
|
||||
},
|
||||
Client: idpv1alpha1.GitHubClientSpec{
|
||||
SecretName: "name-of-a-secret",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, err = gitHubIDPClient.Create(ctx, input, metav1.CreateOptions{})
|
||||
|
||||
expectedErr := "spec.allowAuthentication.organizations.allowed: Invalid value: 100: spec.allowAuthentication.organizations.allowed in body should have at most 64 items"
|
||||
if testutil.KubeServerMinorVersionAtLeastInclusive(t, adminClient.Discovery(), 24) {
|
||||
expectedErr = "spec.allowAuthentication.organizations.allowed: Too many: 100: must have at most 64 items"
|
||||
}
|
||||
|
||||
require.ErrorContains(t, err, expectedErr)
|
||||
}
|
||||
Reference in New Issue
Block a user