2023-08-27 22:59:02 +00:00
|
|
|
// Copyright 2022-2023 the Pinniped contributors. All Rights Reserved.
|
2022-06-17 16:56:53 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package oidcclientwatcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-07-06 17:34:24 +00:00
|
|
|
"strings"
|
2022-06-17 16:56:53 +00:00
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/api/equality"
|
|
|
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
corev1informers "k8s.io/client-go/informers/core/v1"
|
|
|
|
|
|
|
|
"go.pinniped.dev/generated/latest/apis/supervisor/config/v1alpha1"
|
Create username scope, required for clients to get username in ID token
- For backwards compatibility with older Pinniped CLIs, the pinniped-cli
client does not need to request the username or groups scopes for them
to be granted. For dynamic clients, the usual OAuth2 rules apply:
the client must be allowed to request the scopes according to its
configuration, and the client must actually request the scopes in the
authorization request.
- If the username scope was not granted, then there will be no username
in the ID token, and the cluster-scoped token exchange will fail since
there would be no username in the resulting cluster-scoped ID token.
- The OIDC well-known discovery endpoint lists the username and groups
scopes in the scopes_supported list, and lists the username and groups
claims in the claims_supported list.
- Add username and groups scopes to the default list of scopes
put into kubeconfig files by "pinniped get kubeconfig" CLI command,
and the default list of scopes used by "pinniped login oidc" when
no list of scopes is specified in the kubeconfig file
- The warning header about group memberships changing during upstream
refresh will only be sent to the pinniped-cli client, since it is
only intended for kubectl and it could leak the username to the
client (which may not have the username scope granted) through the
warning message text.
- Add the user's username to the session storage as a new field, so that
during upstream refresh we can compare the original username from the
initial authorization to the refreshed username, even in the case when
the username scope was not granted (and therefore the username is not
stored in the ID token claims of the session storage)
- Bump the Supervisor session storage format version from 2 to 3
due to the username field being added to the session struct
- Extract commonly used string constants related to OIDC flows to api
package.
- Change some import names to make them consistent:
- Always import github.com/coreos/go-oidc/v3/oidc as "coreosoidc"
- Always import go.pinniped.dev/generated/latest/apis/supervisor/oidc
as "oidcapi"
- Always import go.pinniped.dev/internal/oidc as "oidc"
2022-08-08 23:29:22 +00:00
|
|
|
oidcapi "go.pinniped.dev/generated/latest/apis/supervisor/oidc"
|
2022-06-17 16:56:53 +00:00
|
|
|
pinnipedclientset "go.pinniped.dev/generated/latest/client/supervisor/clientset/versioned"
|
|
|
|
configInformers "go.pinniped.dev/generated/latest/client/supervisor/informers/externalversions/config/v1alpha1"
|
|
|
|
pinnipedcontroller "go.pinniped.dev/internal/controller"
|
|
|
|
"go.pinniped.dev/internal/controller/conditionsutil"
|
|
|
|
"go.pinniped.dev/internal/controllerlib"
|
2023-06-22 22:12:33 +00:00
|
|
|
"go.pinniped.dev/internal/federationdomain/oidcclientvalidator"
|
2022-06-17 16:56:53 +00:00
|
|
|
"go.pinniped.dev/internal/oidcclientsecretstorage"
|
|
|
|
"go.pinniped.dev/internal/plog"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-07-06 17:34:24 +00:00
|
|
|
secretTypeToObserve = "storage.pinniped.dev/oidc-client-secret" //nolint:gosec // this is not a credential
|
Create username scope, required for clients to get username in ID token
- For backwards compatibility with older Pinniped CLIs, the pinniped-cli
client does not need to request the username or groups scopes for them
to be granted. For dynamic clients, the usual OAuth2 rules apply:
the client must be allowed to request the scopes according to its
configuration, and the client must actually request the scopes in the
authorization request.
- If the username scope was not granted, then there will be no username
in the ID token, and the cluster-scoped token exchange will fail since
there would be no username in the resulting cluster-scoped ID token.
- The OIDC well-known discovery endpoint lists the username and groups
scopes in the scopes_supported list, and lists the username and groups
claims in the claims_supported list.
- Add username and groups scopes to the default list of scopes
put into kubeconfig files by "pinniped get kubeconfig" CLI command,
and the default list of scopes used by "pinniped login oidc" when
no list of scopes is specified in the kubeconfig file
- The warning header about group memberships changing during upstream
refresh will only be sent to the pinniped-cli client, since it is
only intended for kubectl and it could leak the username to the
client (which may not have the username scope granted) through the
warning message text.
- Add the user's username to the session storage as a new field, so that
during upstream refresh we can compare the original username from the
initial authorization to the refreshed username, even in the case when
the username scope was not granted (and therefore the username is not
stored in the ID token claims of the session storage)
- Bump the Supervisor session storage format version from 2 to 3
due to the username field being added to the session struct
- Extract commonly used string constants related to OIDC flows to api
package.
- Change some import names to make them consistent:
- Always import github.com/coreos/go-oidc/v3/oidc as "coreosoidc"
- Always import go.pinniped.dev/generated/latest/apis/supervisor/oidc
as "oidcapi"
- Always import go.pinniped.dev/internal/oidc as "oidc"
2022-08-08 23:29:22 +00:00
|
|
|
oidcClientPrefixToObserve = oidcapi.ClientIDRequiredOIDCClientPrefix
|
2022-06-17 16:56:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type oidcClientWatcherController struct {
|
|
|
|
pinnipedClient pinnipedclientset.Interface
|
|
|
|
oidcClientInformer configInformers.OIDCClientInformer
|
|
|
|
secretInformer corev1informers.SecretInformer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewOIDCClientWatcherController returns a controllerlib.Controller that watches OIDCClients and updates
|
|
|
|
// their status with validation errors.
|
|
|
|
func NewOIDCClientWatcherController(
|
|
|
|
pinnipedClient pinnipedclientset.Interface,
|
|
|
|
secretInformer corev1informers.SecretInformer,
|
|
|
|
oidcClientInformer configInformers.OIDCClientInformer,
|
|
|
|
withInformer pinnipedcontroller.WithInformerOptionFunc,
|
|
|
|
) controllerlib.Controller {
|
|
|
|
return controllerlib.New(
|
|
|
|
controllerlib.Config{
|
|
|
|
Name: "OIDCClientWatcherController",
|
|
|
|
Syncer: &oidcClientWatcherController{
|
|
|
|
pinnipedClient: pinnipedClient,
|
|
|
|
secretInformer: secretInformer,
|
|
|
|
oidcClientInformer: oidcClientInformer,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// We want to be notified when an OIDCClient's corresponding secret gets updated or deleted.
|
|
|
|
withInformer(
|
|
|
|
secretInformer,
|
|
|
|
pinnipedcontroller.MatchAnySecretOfTypeFilter(secretTypeToObserve, pinnipedcontroller.SingletonQueue()),
|
|
|
|
controllerlib.InformerOption{},
|
|
|
|
),
|
|
|
|
// We want to be notified when anything happens to an OIDCClient.
|
|
|
|
withInformer(
|
|
|
|
oidcClientInformer,
|
2022-07-06 17:34:24 +00:00
|
|
|
pinnipedcontroller.SimpleFilterWithSingletonQueue(func(obj metav1.Object) bool {
|
|
|
|
return strings.HasPrefix(obj.GetName(), oidcClientPrefixToObserve)
|
|
|
|
}),
|
2022-06-17 16:56:53 +00:00
|
|
|
controllerlib.InformerOption{},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync implements controllerlib.Syncer.
|
|
|
|
func (c *oidcClientWatcherController) Sync(ctx controllerlib.Context) error {
|
|
|
|
// Sync could be called on either a Secret or an OIDCClient, so to keep it simple, revalidate
|
|
|
|
// all OIDCClients whenever anything changes.
|
|
|
|
oidcClients, err := c.oidcClientInformer.Lister().List(labels.Everything())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to list OIDCClients: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're only going to use storage to call GetName(), which happens to not need the constructor params.
|
|
|
|
// This is because we can read the Secrets from the informer cache here, instead of doing live reads.
|
2022-08-26 17:57:45 +00:00
|
|
|
storage := oidcclientsecretstorage.New(nil)
|
2022-06-17 16:56:53 +00:00
|
|
|
|
|
|
|
for _, oidcClient := range oidcClients {
|
2022-07-06 17:34:24 +00:00
|
|
|
// Skip the OIDCClients that we are not trying to observe.
|
|
|
|
if !strings.HasPrefix(oidcClient.Name, oidcClientPrefixToObserve) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-17 16:56:53 +00:00
|
|
|
correspondingSecretName := storage.GetName(oidcClient.UID)
|
|
|
|
|
|
|
|
secret, err := c.secretInformer.Lister().Secrets(oidcClient.Namespace).Get(correspondingSecretName)
|
|
|
|
if err != nil {
|
|
|
|
if !k8serrors.IsNotFound(err) {
|
|
|
|
// Anything other than a NotFound error is unexpected when reading from an informer.
|
|
|
|
return fmt.Errorf("failed to get %s/%s secret: %w", oidcClient.Namespace, correspondingSecretName, err)
|
|
|
|
}
|
|
|
|
// Got a NotFound error, so continue. The Secret just doesn't exist yet, which is okay.
|
|
|
|
plog.DebugErr(
|
|
|
|
"OIDCClientWatcherController error getting storage Secret for OIDCClient's client secrets", err,
|
|
|
|
"oidcClientName", oidcClient.Name,
|
|
|
|
"oidcClientNamespace", oidcClient.Namespace,
|
|
|
|
"secretName", correspondingSecretName,
|
|
|
|
)
|
|
|
|
secret = nil
|
|
|
|
}
|
|
|
|
|
2022-07-20 20:55:56 +00:00
|
|
|
_, conditions, clientSecrets := oidcclientvalidator.Validate(oidcClient, secret, oidcclientvalidator.DefaultMinBcryptCost)
|
2022-06-17 16:56:53 +00:00
|
|
|
|
2022-07-14 16:51:11 +00:00
|
|
|
if err := c.updateStatus(ctx.Context, oidcClient, conditions, len(clientSecrets)); err != nil {
|
2022-06-17 16:56:53 +00:00
|
|
|
return fmt.Errorf("cannot update OIDCClient '%s/%s': %w", oidcClient.Namespace, oidcClient.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plog.Debug(
|
|
|
|
"OIDCClientWatcherController Sync updated an OIDCClient",
|
|
|
|
"oidcClientName", oidcClient.Name,
|
|
|
|
"oidcClientNamespace", oidcClient.Namespace,
|
|
|
|
"conditionsCount", len(conditions),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-06 17:34:24 +00:00
|
|
|
func (c *oidcClientWatcherController) updateStatus(
|
|
|
|
ctx context.Context,
|
|
|
|
upstream *v1alpha1.OIDCClient,
|
2023-08-27 22:59:02 +00:00
|
|
|
conditions []*metav1.Condition,
|
2022-07-06 17:34:24 +00:00
|
|
|
totalClientSecrets int,
|
|
|
|
) error {
|
2022-06-17 16:56:53 +00:00
|
|
|
updated := upstream.DeepCopy()
|
|
|
|
|
|
|
|
hadErrorCondition := conditionsutil.MergeConfigConditions(conditions, upstream.Generation, &updated.Status.Conditions, plog.New())
|
|
|
|
|
|
|
|
updated.Status.Phase = v1alpha1.PhaseReady
|
|
|
|
if hadErrorCondition {
|
|
|
|
updated.Status.Phase = v1alpha1.PhaseError
|
|
|
|
}
|
|
|
|
|
2022-07-14 16:30:03 +00:00
|
|
|
updated.Status.TotalClientSecrets = int32(totalClientSecrets)
|
2022-07-06 17:34:24 +00:00
|
|
|
|
2022-06-17 16:56:53 +00:00
|
|
|
if equality.Semantic.DeepEqual(upstream, updated) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := c.pinnipedClient.
|
|
|
|
ConfigV1alpha1().
|
|
|
|
OIDCClients(upstream.Namespace).
|
|
|
|
UpdateStatus(ctx, updated, metav1.UpdateOptions{})
|
|
|
|
return err
|
|
|
|
}
|