mirror of
https://github.com/dsx-ai-factory/nvke.git
synced 2026-12-07 04:35:58 +08:00
* feat(deploy)!: move the greenfield overlays and the release namespace to nvke-controller This moves them, and with them the persisted identifiers a site carries: the Helm release and its namespace, the database, and the Temporal namespace and task queue. In the three greenfield stacks: nameOverride, fullnameOverride, namespaceOverride, serviceAccount.name, serviceMonitor.name, appDb.dbName, the Temporal namespace and queue, the field managers and the managed-by values, tracing.serviceName, and the objects the stacks render around them -- the Namespace manifest, the Postgres CREATE DATABASE, the SPIRE admin ID and ClusterSPIFFEID namespace selectors, the auth-dev ConfigMap's namespace, the CAPD RBAC ClusterRole and its subject, and the spire-client probe URL. Consumers that move with it: controller/Tiltfile (release name, namespace, SPIRE admin path and the string-matched resource names), deploy/check-site.sh, the e2e site helpers and their collect script, cli/internal/bootstrap/controllerdeploy (including the NodePort Service nvke-controller-external), cli/internal/deploypath's admin path, management-engine/hack/nvke-controller.sh, and the CI lane scripts. The discovery paths search the new name first and keep the old one: nvkectl's authConfigNamespaces, nvke-token.sh, nvke-auth-demo.sh and dev-env-spire.sh now try nvke-controller, then nke-controller, then cloud-workflow-engine. The reference health dashboard matches all three workload names, and Tilt's preflight refuses to take over a cluster that still runs the nke-controller release rather than failing later. The published image keeps its name per #1554 decision 2, as do the nke-controller-config ConfigMap and the nke-controller-chart release artifact; hack/name-check-allowlist.txt now carries one entry per reason rather than one blanket token. Refs #1861, #1554, #1496, #1818 Signed-off-by: Terry Howe <thowe@nvidia.com> * fix: address review feedback Name nke-controller as the intermediate namespace in the authentication docs, matching authConfigNamespaces, and restore the legacy node-agent audience in the exchange mock's test. Signed-off-by: Terry Howe <thowe@nvidia.com> * fix: address review feedback Signed-off-by: Terry Howe <thowe@nvidia.com> * fix(e2e): point the nico bootstrap lane at the nvke-controller namespace The nico lane still asserted the tenant credentials Secret landed in nke-controller, so both e2e (nico) jobs failed after the rename. Signed-off-by: Terry Howe <thowe@nvidia.com> * fix(deploy): carry the nvke-controller rename into the rebased files The rebase onto main replayed the rename over two files that gained nke-controller references after the branch was cut: the management-engine dev overlay, which succeeds the hack script this branch had edited and was removed in #1918, and an API-authentication example added in #1935. Refs #1861 Signed-off-by: Terry Howe <thowe@nvidia.com> * fix: address review feedback Continue namespace discovery past a Forbidden lookup in nvke-token.sh and nvke-auth-demo.sh. --ignore-not-found suppresses absence, not denial, so a kubeconfig scoped to nke-controller alone died on the nvke-controller probe before reaching the namespace it can read -- the migration case this branch adds. Keep the first denial, try the remaining candidates, and fail with it only when none was readable, since falling back to cloud-workflow-engine needs a confirmed absence. Refs #1861 Signed-off-by: Terry Howe <thowe@nvidia.com> --------- Signed-off-by: Terry Howe <thowe@nvidia.com>
685 lines
22 KiB
Go
685 lines
22 KiB
Go
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package auth
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
authnv1 "k8s.io/api/authentication/v1"
|
|
corev1 "k8s.io/api/core/v1"
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
"k8s.io/client-go/kubernetes/fake"
|
|
"k8s.io/client-go/rest"
|
|
k8stesting "k8s.io/client-go/testing"
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
|
)
|
|
|
|
// The controller namespaces authConfigNamespaces searches, by name, so a
|
|
// reordering or a typo there fails here rather than on a site.
|
|
const (
|
|
greenfieldNamespace = "nvke-controller"
|
|
previousNamespace = "nke-controller"
|
|
legacyNamespace = "cloud-workflow-engine"
|
|
)
|
|
|
|
func TestTokenMintsForConfiguredAudience(t *testing.T) {
|
|
const configuredAudience = "https://nke.example.internal"
|
|
|
|
client := fake.NewClientset(&corev1.ConfigMap{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: authConfigMaps[0],
|
|
Namespace: legacyNamespace,
|
|
},
|
|
Data: map[string]string{
|
|
authConfigKey: "audiences:\n - " + configuredAudience + "\n",
|
|
},
|
|
})
|
|
client.PrependReactor("create", "serviceaccounts", func(action k8stesting.Action) (bool, runtime.Object, error) {
|
|
create, ok := action.(k8stesting.CreateActionImpl)
|
|
if !ok || create.Subresource != "token" {
|
|
return false, nil, nil
|
|
}
|
|
if create.Namespace != tokenNamespace || create.Name != tokenServiceAccount {
|
|
return true, nil, fmt.Errorf("minted for %s/%s", create.Namespace, create.Name)
|
|
}
|
|
request, ok := create.Object.(*authnv1.TokenRequest)
|
|
if !ok {
|
|
return true, nil, fmt.Errorf("request type is %T", create.Object)
|
|
}
|
|
if len(request.Spec.Audiences) != 1 || request.Spec.Audiences[0] != configuredAudience {
|
|
return true, nil, fmt.Errorf("audiences are %v", request.Spec.Audiences)
|
|
}
|
|
if request.Spec.ExpirationSeconds == nil || *request.Spec.ExpirationSeconds != int64(tokenLifetime.Seconds()) {
|
|
return true, nil, fmt.Errorf("expirationSeconds is %v", request.Spec.ExpirationSeconds)
|
|
}
|
|
return true, &authnv1.TokenRequest{
|
|
Status: authnv1.TokenRequestStatus{Token: "minted-token"},
|
|
}, nil
|
|
})
|
|
|
|
token, err := (&TokenSource{client: client}).Mint(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("Mint: %v", err)
|
|
}
|
|
if token != "minted-token" {
|
|
t.Fatalf("token = %q, want minted-token", token)
|
|
}
|
|
}
|
|
|
|
func TestTokenUsesCachedToken(t *testing.T) {
|
|
const endpoint = "https://nke.example.internal"
|
|
cache := &tokenCache{tokens: map[string]cachedToken{
|
|
endpoint: {
|
|
Value: "cached-token",
|
|
ExpiresAt: time.Now().Add(tokenRenewBefore + time.Minute),
|
|
},
|
|
}}
|
|
client := fake.NewClientset()
|
|
source := &TokenSource{
|
|
client: client,
|
|
endpoint: endpoint,
|
|
cache: cache,
|
|
}
|
|
|
|
token, err := source.Mint(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("Mint: %v", err)
|
|
}
|
|
if token != "cached-token" {
|
|
t.Fatalf("token = %q, want cached-token", token)
|
|
}
|
|
if len(client.Actions()) != 0 {
|
|
t.Fatalf("cached token made Kubernetes API calls: %v", client.Actions())
|
|
}
|
|
}
|
|
|
|
func TestTokenRenewsAndCachesNearExpiryToken(t *testing.T) {
|
|
const endpoint = "https://nke.example.internal"
|
|
mintedExpiry := time.Now().Add(tokenLifetime)
|
|
client := fake.NewClientset(&corev1.ConfigMap{
|
|
ObjectMeta: metav1.ObjectMeta{Name: authConfigMaps[0], Namespace: legacyNamespace},
|
|
Data: map[string]string{authConfigKey: "audiences:\n - " + endpoint + "\n"},
|
|
})
|
|
client.PrependReactor("create", "serviceaccounts", func(_ k8stesting.Action) (bool, runtime.Object, error) {
|
|
return true, &authnv1.TokenRequest{
|
|
Status: authnv1.TokenRequestStatus{
|
|
Token: "minted-token",
|
|
ExpirationTimestamp: metav1.NewTime(mintedExpiry),
|
|
},
|
|
}, nil
|
|
})
|
|
cache := &tokenCache{tokens: map[string]cachedToken{
|
|
endpoint: {
|
|
Value: "old-token",
|
|
ExpiresAt: time.Now().Add(tokenRenewBefore),
|
|
},
|
|
}}
|
|
source := &TokenSource{client: client, endpoint: endpoint, cache: cache}
|
|
|
|
token, err := source.Mint(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("Mint: %v", err)
|
|
}
|
|
if token != "minted-token" {
|
|
t.Fatalf("token = %q, want minted-token", token)
|
|
}
|
|
stored := cache.tokens[endpoint]
|
|
if stored.Value != "minted-token" || !stored.ExpiresAt.Equal(mintedExpiry) {
|
|
t.Fatalf("cached token = %#v, want minted token expiring at %v", stored, mintedExpiry)
|
|
}
|
|
}
|
|
|
|
func TestTokenReusesMintedTokenInMemory(t *testing.T) {
|
|
const configuredAudience = "https://nke.example.internal"
|
|
client := fake.NewClientset(&corev1.ConfigMap{
|
|
ObjectMeta: metav1.ObjectMeta{Name: authConfigMaps[0], Namespace: legacyNamespace},
|
|
Data: map[string]string{authConfigKey: "audiences:\n - " + configuredAudience + "\n"},
|
|
})
|
|
mints := 0
|
|
client.PrependReactor("create", "serviceaccounts", func(_ k8stesting.Action) (bool, runtime.Object, error) {
|
|
mints++
|
|
return true, &authnv1.TokenRequest{
|
|
Status: authnv1.TokenRequestStatus{
|
|
Token: "minted-token",
|
|
ExpirationTimestamp: metav1.NewTime(time.Now().Add(tokenLifetime)),
|
|
},
|
|
}, nil
|
|
})
|
|
source := &TokenSource{
|
|
client: client,
|
|
cache: &tokenCache{tokens: make(map[string]cachedToken)},
|
|
}
|
|
|
|
for range 2 {
|
|
token, err := source.Mint(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("Mint: %v", err)
|
|
}
|
|
if token != "minted-token" {
|
|
t.Fatalf("token = %q, want minted-token", token)
|
|
}
|
|
}
|
|
if mints != 1 {
|
|
t.Fatalf("mint calls = %d, want 1", mints)
|
|
}
|
|
}
|
|
|
|
func TestTokenReturnsNoCredentialWhenAuthenticationIsDisabled(t *testing.T) {
|
|
client := fake.NewClientset()
|
|
|
|
token, err := (&TokenSource{client: client}).Mint(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("Mint: %v", err)
|
|
}
|
|
if token != "" {
|
|
t.Fatalf("token = %q, want empty", token)
|
|
}
|
|
for _, action := range client.Actions() {
|
|
if action.GetVerb() == "create" && action.GetResource().Resource == "serviceaccounts" {
|
|
t.Fatal("Token tried to mint a credential without an auth ConfigMap")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTokenRequiresConfiguredAudience(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
configMap *corev1.ConfigMap
|
|
}{
|
|
{
|
|
name: "missing auth config key",
|
|
configMap: &corev1.ConfigMap{
|
|
ObjectMeta: metav1.ObjectMeta{Name: authConfigMaps[0], Namespace: legacyNamespace},
|
|
},
|
|
},
|
|
{
|
|
name: "empty audience list",
|
|
configMap: &corev1.ConfigMap{
|
|
ObjectMeta: metav1.ObjectMeta{Name: authConfigMaps[0], Namespace: legacyNamespace},
|
|
Data: map[string]string{authConfigKey: "audiences: []\n"},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
client := fake.NewClientset()
|
|
if tt.configMap != nil {
|
|
if _, err := client.CoreV1().ConfigMaps(legacyNamespace).
|
|
Create(t.Context(), tt.configMap, metav1.CreateOptions{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
if _, err := (&TokenSource{client: client}).Mint(t.Context()); err == nil {
|
|
t.Fatal("Mint returned no error")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAudienceSearchesBothControllerNamespaces(t *testing.T) {
|
|
authConfig := func(namespace, audience string) *corev1.ConfigMap {
|
|
return &corev1.ConfigMap{
|
|
ObjectMeta: metav1.ObjectMeta{Name: authConfigMaps[0], Namespace: namespace},
|
|
Data: map[string]string{authConfigKey: "audiences:\n - " + audience + "\n"},
|
|
}
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
objects []runtime.Object
|
|
wantAudience string
|
|
wantEnabled bool
|
|
}{
|
|
{
|
|
name: "greenfield site",
|
|
objects: []runtime.Object{authConfig(greenfieldNamespace, "https://greenfield.example")},
|
|
wantAudience: "https://greenfield.example",
|
|
wantEnabled: true,
|
|
},
|
|
{
|
|
name: "site installed before the nvke rename",
|
|
objects: []runtime.Object{authConfig(previousNamespace, "https://previous.example")},
|
|
wantAudience: "https://previous.example",
|
|
wantEnabled: true,
|
|
},
|
|
{
|
|
name: "legacy site",
|
|
objects: []runtime.Object{authConfig(legacyNamespace, "https://legacy.example")},
|
|
wantAudience: "https://legacy.example",
|
|
wantEnabled: true,
|
|
},
|
|
{
|
|
name: "every name present uses the greenfield name",
|
|
objects: []runtime.Object{
|
|
authConfig(legacyNamespace, "https://legacy.example"),
|
|
authConfig(previousNamespace, "https://previous.example"),
|
|
authConfig(greenfieldNamespace, "https://greenfield.example"),
|
|
},
|
|
wantAudience: "https://greenfield.example",
|
|
wantEnabled: true,
|
|
},
|
|
{
|
|
name: "neither name leaves authentication off",
|
|
wantEnabled: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
client := fake.NewClientset(tt.objects...)
|
|
audience, enabled, err := (&TokenSource{client: client}).audience(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("audience: %v", err)
|
|
}
|
|
if audience != tt.wantAudience || enabled != tt.wantEnabled {
|
|
t.Fatalf("audience = %q, enabled = %t; want %q, %t", audience, enabled, tt.wantAudience, tt.wantEnabled)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The chart's default ConfigMap name moved to nvke-auth-config; a site that
|
|
// pinned apiAuth.configMap.name keeps nke-auth-config. The CLI reads either,
|
|
// so one nvkectl works against both.
|
|
func TestAudienceSearchesBothConfigMapNames(t *testing.T) {
|
|
authConfig := func(name, audience string) *corev1.ConfigMap {
|
|
return &corev1.ConfigMap{
|
|
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: greenfieldNamespace},
|
|
Data: map[string]string{authConfigKey: "audiences:\n - " + audience + "\n"},
|
|
}
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
objects []runtime.Object
|
|
wantAudience string
|
|
wantEnabled bool
|
|
}{
|
|
{
|
|
name: "greenfield name",
|
|
objects: []runtime.Object{authConfig("nvke-auth-config", "https://greenfield.example")},
|
|
wantAudience: "https://greenfield.example",
|
|
wantEnabled: true,
|
|
},
|
|
{
|
|
name: "pinned legacy name",
|
|
objects: []runtime.Object{authConfig("nke-auth-config", "https://legacy.example")},
|
|
wantAudience: "https://legacy.example",
|
|
wantEnabled: true,
|
|
},
|
|
{
|
|
name: "both names present uses the greenfield name",
|
|
objects: []runtime.Object{
|
|
authConfig("nke-auth-config", "https://legacy.example"),
|
|
authConfig("nvke-auth-config", "https://greenfield.example"),
|
|
},
|
|
wantAudience: "https://greenfield.example",
|
|
wantEnabled: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
client := fake.NewClientset(tt.objects...)
|
|
audience, enabled, err := (&TokenSource{client: client}).audience(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("audience: %v", err)
|
|
}
|
|
if audience != tt.wantAudience || enabled != tt.wantEnabled {
|
|
t.Fatalf("audience = %q, enabled = %t; want %q, %t", audience, enabled, tt.wantAudience, tt.wantEnabled)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Only NotFound moves on to the next namespace. Any other failure is reported,
|
|
// so a denied read cannot turn into an unauthenticated request.
|
|
func TestAudienceReportsNonNotFoundErrors(t *testing.T) {
|
|
client := fake.NewClientset(&corev1.ConfigMap{
|
|
ObjectMeta: metav1.ObjectMeta{Name: authConfigMaps[0], Namespace: legacyNamespace},
|
|
Data: map[string]string{authConfigKey: "audiences:\n - https://legacy.example\n"},
|
|
})
|
|
client.PrependReactor("get", "configmaps", func(action k8stesting.Action) (bool, runtime.Object, error) {
|
|
if action.GetNamespace() == greenfieldNamespace {
|
|
return true, nil, fmt.Errorf("forbidden")
|
|
}
|
|
return false, nil, nil
|
|
})
|
|
|
|
if _, _, err := (&TokenSource{client: client}).audience(t.Context()); err == nil {
|
|
t.Fatal("audience returned no error for a failed read of the greenfield namespace")
|
|
}
|
|
}
|
|
|
|
// forbid returns a reactor that denies get on exactly the named ConfigMaps, the
|
|
// way RBAC scoped to resourceNames does, and passes everything else through.
|
|
func forbid(names ...string) k8stesting.ReactionFunc {
|
|
denied := make(map[string]bool, len(names))
|
|
for _, n := range names {
|
|
denied[n] = true
|
|
}
|
|
return func(action k8stesting.Action) (bool, runtime.Object, error) {
|
|
get, ok := action.(k8stesting.GetActionImpl)
|
|
if !ok || !denied[get.Name] {
|
|
return false, nil, nil
|
|
}
|
|
return true, nil, apierrors.NewForbidden(
|
|
schema.GroupResource{Resource: "configmaps"}, get.Name, fmt.Errorf("not permitted"),
|
|
)
|
|
}
|
|
}
|
|
|
|
// A site pinned to the legacy name may grant get on that name alone. The denial
|
|
// of the greenfield name is then expected, and must not stop the search before
|
|
// the name the kubeconfig can actually read.
|
|
func TestAudienceFallsBackWhenTheGreenfieldNameIsForbidden(t *testing.T) {
|
|
client := fake.NewClientset(&corev1.ConfigMap{
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nke-auth-config", Namespace: greenfieldNamespace},
|
|
Data: map[string]string{authConfigKey: "audiences:\n - https://pinned.example\n"},
|
|
})
|
|
client.PrependReactor("get", "configmaps", forbid("nvke-auth-config"))
|
|
|
|
audience, enabled, err := (&TokenSource{client: client}).audience(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("audience: %v", err)
|
|
}
|
|
if audience != "https://pinned.example" || !enabled {
|
|
t.Fatalf("audience = %q, enabled = %t; want https://pinned.example, true", audience, enabled)
|
|
}
|
|
}
|
|
|
|
// The other half of that: when nothing is readable, the denial is the answer.
|
|
// Reporting "authentication is off" would send a bare request that the
|
|
// controller rejects, turning an RBAC problem into a confusing 401.
|
|
func TestAudienceReportsForbiddenWhenNothingIsReadable(t *testing.T) {
|
|
client := fake.NewClientset()
|
|
client.PrependReactor("get", "configmaps", forbid("nvke-auth-config", "nke-auth-config"))
|
|
|
|
_, enabled, err := (&TokenSource{client: client}).audience(t.Context())
|
|
if err == nil {
|
|
t.Fatal("audience treated a denied read as authentication being off")
|
|
}
|
|
if enabled {
|
|
t.Fatal("audience reported authentication as enabled without a ConfigMap")
|
|
}
|
|
if !apierrors.IsForbidden(err) {
|
|
t.Fatalf("audience lost the Forbidden cause: %v", err)
|
|
}
|
|
}
|
|
|
|
// A chart-rendered configuration reaches the controller as apiAuth in its
|
|
// settings ConfigMap, so that is read first. Off or absent there leaves the
|
|
// authentication ConfigMap to decide, as a controller reading AUTH_CONFIG_PATH
|
|
// or one older than apiAuth needs.
|
|
func TestAudienceReadsAPIAuthFromControllerConfig(t *testing.T) {
|
|
settings := func(name, config string) *corev1.ConfigMap {
|
|
return &corev1.ConfigMap{
|
|
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: greenfieldNamespace},
|
|
Data: map[string]string{controllerConfigKey: config},
|
|
}
|
|
}
|
|
authConfig := &corev1.ConfigMap{
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nvke-auth-config", Namespace: greenfieldNamespace},
|
|
Data: map[string]string{authConfigKey: "audiences:\n - https://file.example\n"},
|
|
}
|
|
const on = "apiAuth:\n enabled: true\n audiences: [\"https://inline.example\"]\n"
|
|
const off = "apiAuth:\n enabled: false\n audiences: []\n"
|
|
|
|
tests := []struct {
|
|
name string
|
|
objects []runtime.Object
|
|
forbidden []string
|
|
wantAudience string
|
|
wantEnabled bool
|
|
wantErr string
|
|
}{
|
|
{
|
|
name: "apiAuth wins over the authentication ConfigMap",
|
|
objects: []runtime.Object{settings("nvke-controller-config", on), authConfig},
|
|
wantAudience: "https://inline.example",
|
|
wantEnabled: true,
|
|
},
|
|
{
|
|
name: "a pinned settings name is read too",
|
|
objects: []runtime.Object{settings("nke-controller-config", on)},
|
|
wantAudience: "https://inline.example",
|
|
wantEnabled: true,
|
|
},
|
|
{
|
|
name: "apiAuth off falls back to the authentication ConfigMap",
|
|
objects: []runtime.Object{settings("nvke-controller-config", off), authConfig},
|
|
wantAudience: "https://file.example",
|
|
wantEnabled: true,
|
|
},
|
|
{
|
|
name: "a controller older than apiAuth falls back too",
|
|
objects: []runtime.Object{settings("nvke-controller-config", "metricsListenAddress: :9090\n"), authConfig},
|
|
wantAudience: "https://file.example",
|
|
wantEnabled: true,
|
|
},
|
|
{
|
|
name: "apiAuth off and no authentication ConfigMap is off",
|
|
objects: []runtime.Object{settings("nvke-controller-config", off)},
|
|
},
|
|
{
|
|
name: "an unreadable settings ConfigMap still falls back",
|
|
objects: []runtime.Object{settings("nvke-controller-config", on), authConfig},
|
|
forbidden: []string{"nvke-controller-config", "nke-controller-config"},
|
|
wantAudience: "https://file.example",
|
|
wantEnabled: true,
|
|
},
|
|
{
|
|
name: "apiAuth off and an unreadable authentication ConfigMap is the denial",
|
|
objects: []runtime.Object{settings("nvke-controller-config", off)},
|
|
forbidden: []string{"nvke-auth-config", "nke-auth-config"},
|
|
wantErr: "forbidden",
|
|
},
|
|
{
|
|
name: "apiAuth on without audiences is an error",
|
|
objects: []runtime.Object{settings("nvke-controller-config", "apiAuth:\n enabled: true\n")},
|
|
wantErr: "enables apiAuth but sets no audiences",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
client := fake.NewClientset(tt.objects...)
|
|
if len(tt.forbidden) > 0 {
|
|
client.PrependReactor("get", "configmaps", forbid(tt.forbidden...))
|
|
}
|
|
audience, enabled, err := (&TokenSource{client: client}).audience(t.Context())
|
|
if tt.wantErr != "" {
|
|
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
|
t.Fatalf("audience error = %v, want one containing %q", err, tt.wantErr)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("audience: %v", err)
|
|
}
|
|
if audience != tt.wantAudience || enabled != tt.wantEnabled {
|
|
t.Fatalf("audience = %q, enabled = %t; want %q, %t", audience, enabled, tt.wantAudience, tt.wantEnabled)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUsesOIDCLogin(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
exec *clientcmdapi.ExecConfig
|
|
want bool
|
|
}{
|
|
{name: "nil"},
|
|
{name: "teleport", exec: &clientcmdapi.ExecConfig{Command: "tsh", Args: []string{"kube", "credentials"}}},
|
|
{name: "eks", exec: &clientcmdapi.ExecConfig{Command: "aws", Args: []string{"eks", "get-token"}}},
|
|
{name: "gke", exec: &clientcmdapi.ExecConfig{Command: "gke-gcloud-auth-plugin"}},
|
|
{
|
|
name: "kubectl oidc-login",
|
|
exec: &clientcmdapi.ExecConfig{Command: "kubectl", Args: []string{"oidc-login", "get-token"}},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "kubectl-oidc_login",
|
|
exec: &clientcmdapi.ExecConfig{Command: "/usr/local/bin/kubectl-oidc_login", Args: []string{"get-token"}},
|
|
want: true,
|
|
},
|
|
{name: "name substring", exec: &clientcmdapi.ExecConfig{Command: "not-oidc-login"}},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := usesOIDCLogin(tt.exec); got != tt.want {
|
|
t.Fatalf("usesOIDCLogin() = %t, want %t", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUseExecToken(t *testing.T) {
|
|
oidc := &clientcmdapi.ExecConfig{Command: "kubectl", Args: []string{"oidc-login", "get-token"}}
|
|
tests := []struct {
|
|
name string
|
|
cfg *rest.Config
|
|
want bool
|
|
}{
|
|
{name: "oidc only", cfg: &rest.Config{ExecProvider: oidc}, want: true},
|
|
{name: "oidc plus token", cfg: &rest.Config{ExecProvider: oidc, BearerToken: "static"}},
|
|
{name: "oidc plus basic", cfg: &rest.Config{ExecProvider: oidc, Username: "u", Password: "p"}},
|
|
{name: "oidc plus cert", cfg: &rest.Config{
|
|
ExecProvider: oidc,
|
|
TLSClientConfig: rest.TLSClientConfig{CertData: []byte("c"), KeyData: []byte("k")},
|
|
}},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := useExecToken(tt.cfg); got != tt.want {
|
|
t.Fatalf("useExecToken() = %t, want %t", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMintUsesOIDCLoginToken(t *testing.T) {
|
|
const want = "exec-plugin-token"
|
|
source := newExecSource(t, execSourceOpts{
|
|
args: []string{"oidc-login", "get-token"},
|
|
credential: `{"kind":"ExecCredential","apiVersion":"client.authentication.k8s.io/v1","status":{"token":"` + want + `"}}`,
|
|
})
|
|
if source.restConfig == nil {
|
|
t.Fatal("expected oidc-login token source")
|
|
}
|
|
source.client = fake.NewClientset()
|
|
|
|
token, err := source.Mint(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("Mint: %v", err)
|
|
}
|
|
if token != want {
|
|
t.Fatalf("token = %q, want %q", token, want)
|
|
}
|
|
if actions := source.client.(*fake.Clientset).Actions(); len(actions) != 0 {
|
|
t.Fatalf("oidc-login mint made Kubernetes API calls: %v", actions)
|
|
}
|
|
}
|
|
|
|
func TestMintUsesTokenRequestForExecBlockers(t *testing.T) {
|
|
const staticToken = "static-k8s-token"
|
|
const execToken = "exec-plugin-token"
|
|
tests := []struct {
|
|
name string
|
|
opts execSourceOpts
|
|
}{
|
|
{
|
|
name: "teleport client cert",
|
|
opts: execSourceOpts{
|
|
credential: `{"kind":"ExecCredential","apiVersion":"client.authentication.k8s.io/v1","status":{"clientCertificateData":"dummy-cert","clientKeyData":"dummy-key"}}`,
|
|
},
|
|
},
|
|
{
|
|
name: "static token plus oidc-login",
|
|
opts: execSourceOpts{
|
|
args: []string{"oidc-login", "get-token"},
|
|
credential: `{"kind":"ExecCredential","apiVersion":"client.authentication.k8s.io/v1","status":{"token":"` + execToken + `"}}`,
|
|
token: staticToken,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
source := newExecSource(t, tt.opts)
|
|
if source.restConfig != nil {
|
|
t.Fatal("expected TokenRequest path")
|
|
}
|
|
source.client = fake.NewClientset()
|
|
|
|
token, err := source.Mint(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("Mint: %v", err)
|
|
}
|
|
if token != "" {
|
|
t.Fatalf("token = %q, want empty (auth ConfigMap absent)", token)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type execSourceOpts struct {
|
|
args []string
|
|
credential string
|
|
token string
|
|
}
|
|
|
|
func newExecSource(t *testing.T, opts execSourceOpts) *TokenSource {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
command := filepath.Join(dir, "exec-helper.sh")
|
|
if err := os.WriteFile(command, []byte("#!/bin/sh\nprintf '%s' '"+opts.credential+"'\n"), 0o755); err != nil {
|
|
t.Fatalf("write helper: %v", err)
|
|
}
|
|
|
|
cfg := clientcmdapi.NewConfig()
|
|
cfg.Clusters["site"] = &clientcmdapi.Cluster{
|
|
Server: "https://127.0.0.1:6443",
|
|
InsecureSkipTLSVerify: true,
|
|
}
|
|
cfg.AuthInfos["user"] = &clientcmdapi.AuthInfo{
|
|
Token: opts.token,
|
|
Exec: &clientcmdapi.ExecConfig{
|
|
APIVersion: "client.authentication.k8s.io/v1",
|
|
Command: command,
|
|
Args: opts.args,
|
|
InteractiveMode: clientcmdapi.NeverExecInteractiveMode,
|
|
},
|
|
}
|
|
cfg.Contexts["user"] = &clientcmdapi.Context{Cluster: "site", AuthInfo: "user"}
|
|
cfg.CurrentContext = "user"
|
|
kcPath := filepath.Join(dir, "kubeconfig")
|
|
data, err := clientcmd.Write(*cfg)
|
|
if err != nil {
|
|
t.Fatalf("Write: %v", err)
|
|
}
|
|
if err := os.WriteFile(kcPath, data, 0o600); err != nil {
|
|
t.Fatalf("write kubeconfig: %v", err)
|
|
}
|
|
|
|
source, err := NewTokenSource(kcPath, "https://controller.example")
|
|
if err != nil {
|
|
t.Fatalf("NewTokenSource: %v", err)
|
|
}
|
|
return source
|
|
}
|