2022-06-09 20:45:21 +00:00
|
|
|
// Copyright 2022 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
// Package clientsecretrequest provides REST functionality for the CredentialRequest resource.
|
|
|
|
package clientsecretrequest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-26 17:57:45 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2022-06-09 20:45:21 +00:00
|
|
|
"fmt"
|
2022-08-26 17:57:45 +00:00
|
|
|
"io"
|
|
|
|
"strings"
|
2022-06-09 20:45:21 +00:00
|
|
|
|
2022-08-26 17:57:45 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
|
|
|
"k8s.io/apiextensions-apiserver/pkg/registry/customresource/tableconvertor"
|
2022-06-09 20:45:21 +00:00
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
2022-08-26 17:57:45 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
genericvalidation "k8s.io/apimachinery/pkg/api/validation"
|
|
|
|
"k8s.io/apimachinery/pkg/api/validation/path"
|
2022-06-15 16:38:21 +00:00
|
|
|
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
2022-06-09 20:45:21 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2022-06-15 16:38:21 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2022-08-26 17:57:45 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
|
|
|
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
|
2022-06-09 20:45:21 +00:00
|
|
|
"k8s.io/apiserver/pkg/registry/rest"
|
2022-08-26 17:57:45 +00:00
|
|
|
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
|
2022-06-09 20:45:21 +00:00
|
|
|
"k8s.io/utils/trace"
|
|
|
|
|
2022-06-13 21:28:05 +00:00
|
|
|
clientsecretapi "go.pinniped.dev/generated/latest/apis/supervisor/clientsecret"
|
2022-08-26 17:57:45 +00:00
|
|
|
configv1alpha1clientset "go.pinniped.dev/generated/latest/client/supervisor/clientset/versioned/typed/config/v1alpha1"
|
|
|
|
"go.pinniped.dev/internal/oidcclientsecretstorage"
|
2022-06-09 20:45:21 +00:00
|
|
|
)
|
|
|
|
|
2022-08-26 17:57:45 +00:00
|
|
|
// cost is a good bcrypt cost for 2022, should take about 250 ms to validate.
|
|
|
|
// This value is expected to be increased over time to match CPU improvements.
|
|
|
|
const cost = 12
|
|
|
|
|
2022-08-30 19:11:17 +00:00
|
|
|
//nolint:gochecknoglobals
|
2022-08-26 17:57:45 +00:00
|
|
|
var tableConvertor = func() rest.TableConvertor {
|
|
|
|
// sadly this is not useful at the moment because `kubectl create` does not support table output
|
|
|
|
columns := []apiextensionsv1.CustomResourceColumnDefinition{
|
|
|
|
{
|
|
|
|
Name: "Secret",
|
|
|
|
Type: "string",
|
|
|
|
Description: "", // TODO generate SwaggerDoc() method to fill this field
|
|
|
|
JSONPath: ".status.generatedSecret",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Total",
|
|
|
|
Type: "integer",
|
|
|
|
Description: "", // TODO generate SwaggerDoc() method to fill this field
|
|
|
|
JSONPath: ".status.totalClientSecrets",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tc, err := tableconvertor.New(columns) // just re-use the CRD table code so we do not have to implement the interface ourselves
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // inputs are static so this should never happen
|
|
|
|
}
|
|
|
|
return tc
|
|
|
|
}()
|
|
|
|
|
|
|
|
func NewREST(secrets corev1client.SecretInterface, clients configv1alpha1clientset.OIDCClientInterface, namespace string) *REST {
|
2022-06-15 16:38:21 +00:00
|
|
|
return &REST{
|
2022-08-26 17:57:45 +00:00
|
|
|
secretStorage: oidcclientsecretstorage.New(secrets),
|
|
|
|
clients: clients,
|
|
|
|
namespace: namespace,
|
|
|
|
rand: rand.Reader,
|
2022-06-15 16:38:21 +00:00
|
|
|
}
|
2022-06-09 20:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type REST struct {
|
2022-08-26 17:57:45 +00:00
|
|
|
secretStorage *oidcclientsecretstorage.OIDCClientSecretStorage
|
|
|
|
clients configv1alpha1clientset.OIDCClientInterface
|
|
|
|
namespace string
|
|
|
|
rand io.Reader
|
2022-06-09 20:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that our *REST implements all the optional interfaces that we expect it to implement.
|
|
|
|
var _ interface {
|
|
|
|
rest.Creater
|
|
|
|
rest.NamespaceScopedStrategy
|
|
|
|
rest.Scoper
|
|
|
|
rest.Storage
|
2022-06-15 16:38:21 +00:00
|
|
|
rest.CategoriesProvider
|
|
|
|
rest.Lister
|
|
|
|
rest.TableConvertor
|
2022-06-09 20:45:21 +00:00
|
|
|
} = (*REST)(nil)
|
|
|
|
|
|
|
|
func (*REST) New() runtime.Object {
|
2022-06-13 21:28:05 +00:00
|
|
|
return &clientsecretapi.OIDCClientSecretRequest{}
|
2022-06-09 20:45:21 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:35:35 +00:00
|
|
|
func (*REST) Destroy() {}
|
|
|
|
|
2022-06-15 16:38:21 +00:00
|
|
|
func (*REST) NewList() runtime.Object {
|
|
|
|
return &clientsecretapi.OIDCClientSecretRequestList{}
|
|
|
|
}
|
|
|
|
|
2022-08-26 17:57:45 +00:00
|
|
|
// List implements the list verb. Support the list verb to support `kubectl get pinniped`, to make sure all resources
|
|
|
|
// are in the pinniped category, and avoid kubectl errors when kubectl lists.
|
2022-06-15 16:38:21 +00:00
|
|
|
func (*REST) List(_ context.Context, _ *metainternalversion.ListOptions) (runtime.Object, error) {
|
|
|
|
return &clientsecretapi.OIDCClientSecretRequestList{
|
|
|
|
ListMeta: metav1.ListMeta{
|
|
|
|
ResourceVersion: "0", // this resource version means "from the API server cache"
|
|
|
|
},
|
|
|
|
Items: []clientsecretapi.OIDCClientSecretRequest{}, // avoid sending nil items list
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *REST) ConvertToTable(ctx context.Context, obj runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
|
2022-08-26 17:57:45 +00:00
|
|
|
return tableConvertor.ConvertToTable(ctx, obj, tableOptions)
|
2022-06-15 16:38:21 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 20:45:21 +00:00
|
|
|
func (*REST) NamespaceScoped() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*REST) Categories() []string {
|
2022-06-15 16:38:21 +00:00
|
|
|
return []string{"pinniped"}
|
2022-06-09 20:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
|
2022-08-26 17:57:45 +00:00
|
|
|
t := trace.FromContext(ctx).Nest("create",
|
|
|
|
trace.Field{Key: "kind", Value: "OIDCClientSecretRequest"},
|
|
|
|
trace.Field{Key: "metadata.name", Value: name(obj)},
|
|
|
|
)
|
2022-06-09 20:45:21 +00:00
|
|
|
defer t.Log()
|
|
|
|
|
2022-08-26 17:57:45 +00:00
|
|
|
// Validate the create request before honoring it.
|
|
|
|
req, err := r.validateRequest(ctx, obj, createValidation, options, t)
|
2022-06-09 20:45:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-26 17:57:45 +00:00
|
|
|
t.Step("validateRequest")
|
|
|
|
|
|
|
|
// Find the specified OIDCClient.
|
|
|
|
oidcClient, err := r.clients.Get(ctx, req.Name, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
if apierrors.IsNotFound(err) {
|
|
|
|
traceValidationFailure(t, fmt.Sprintf("client %q does not exist", req.Name))
|
|
|
|
errs := field.ErrorList{field.NotFound(field.NewPath("metadata", "name"), req.Name)}
|
|
|
|
return nil, apierrors.NewInvalid(kindFromContext(ctx), req.Name, errs)
|
|
|
|
}
|
|
|
|
traceFailureWithError(t, "clients.Get", err)
|
|
|
|
return nil, apierrors.NewInternalError(fmt.Errorf("getting client %q failed", req.Name))
|
|
|
|
}
|
|
|
|
t.Step("clients.Get")
|
|
|
|
|
|
|
|
// Using the OIDCClient's UID, check to see if the storage Secret for its client secrets already exists.
|
|
|
|
// Note that when it does not exist, this Get() function will not return an error, and will return nil rv and hashes.
|
|
|
|
rv, hashes, err := r.secretStorage.Get(ctx, oidcClient.UID)
|
|
|
|
if err != nil {
|
|
|
|
traceFailureWithError(t, "secretStorage.Get", err)
|
|
|
|
return nil, apierrors.NewInternalError(fmt.Errorf("getting secret for client %q failed", req.Name))
|
|
|
|
}
|
|
|
|
t.Step("secretStorage.Get")
|
2022-06-09 20:45:21 +00:00
|
|
|
|
2022-08-26 17:57:45 +00:00
|
|
|
// If requested, generate a new client secret and add it to the list.
|
|
|
|
var secret string
|
|
|
|
if req.Spec.GenerateNewSecret {
|
|
|
|
secret, err = generateSecret(r.rand)
|
|
|
|
if err != nil {
|
|
|
|
traceFailureWithError(t, "generateSecret", err)
|
|
|
|
return nil, apierrors.NewInternalError(fmt.Errorf("client secret generation failed"))
|
|
|
|
}
|
|
|
|
t.Step("generateSecret")
|
|
|
|
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(secret), cost)
|
|
|
|
if err != nil {
|
|
|
|
traceFailureWithError(t, "bcrypt.GenerateFromPassword", err)
|
|
|
|
return nil, apierrors.NewInternalError(fmt.Errorf("hash generation failed"))
|
|
|
|
}
|
|
|
|
t.Step("bcrypt.GenerateFromPassword")
|
|
|
|
|
|
|
|
hashes = append([]string{string(hash)}, hashes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If requested, remove all client secrets except for the most recent one.
|
|
|
|
needsRevoke := req.Spec.RevokeOldSecrets && len(hashes) > 0
|
|
|
|
if needsRevoke {
|
|
|
|
hashes = []string{hashes[0]}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If anything was requested to change...
|
|
|
|
if req.Spec.GenerateNewSecret || needsRevoke {
|
|
|
|
// Each bcrypt comparison is expensive, and we do not want a large list to cause wasted CPU.
|
|
|
|
if len(hashes) > 5 {
|
|
|
|
return nil, apierrors.NewRequestEntityTooLargeError(
|
|
|
|
fmt.Sprintf("OIDCClient %s has too many secrets, spec.revokeOldSecrets must be true", oidcClient.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create or update the storage Secret for client secrets.
|
|
|
|
if err := r.secretStorage.Set(ctx, rv, oidcClient.Name, oidcClient.UID, hashes); err != nil {
|
|
|
|
if apierrors.IsAlreadyExists(err) || apierrors.IsConflict(err) {
|
|
|
|
return nil, apierrors.NewConflict(qualifiedResourceFromContext(ctx), req.Name,
|
|
|
|
fmt.Errorf("multiple concurrent secret generation requests for same client"))
|
|
|
|
}
|
|
|
|
|
|
|
|
traceFailureWithError(t, "secretStorage.Set", err)
|
|
|
|
return nil, apierrors.NewInternalError(fmt.Errorf("setting client secret failed"))
|
|
|
|
}
|
|
|
|
t.Step("secretStorage.Set")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the new secret in plaintext, if one was generated, along with the total number of secrets.
|
2022-06-13 21:28:05 +00:00
|
|
|
return &clientsecretapi.OIDCClientSecretRequest{
|
|
|
|
Status: clientsecretapi.OIDCClientSecretRequestStatus{
|
2022-08-26 17:57:45 +00:00
|
|
|
GeneratedSecret: secret,
|
|
|
|
TotalClientSecrets: len(hashes),
|
2022-06-09 20:45:21 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 17:57:45 +00:00
|
|
|
func (r *REST) validateRequest(
|
|
|
|
ctx context.Context,
|
|
|
|
obj runtime.Object,
|
|
|
|
createValidation rest.ValidateObjectFunc,
|
|
|
|
options *metav1.CreateOptions,
|
|
|
|
t *trace.Trace,
|
|
|
|
) (*clientsecretapi.OIDCClientSecretRequest, error) {
|
2022-06-13 21:28:05 +00:00
|
|
|
clientSecretRequest, ok := obj.(*clientsecretapi.OIDCClientSecretRequest)
|
2022-06-09 20:45:21 +00:00
|
|
|
if !ok {
|
|
|
|
traceValidationFailure(t, "not an OIDCClientSecretRequest")
|
|
|
|
return nil, apierrors.NewBadRequest(fmt.Sprintf("not an OIDCClientSecretRequest: %#v", obj))
|
|
|
|
}
|
|
|
|
|
2022-08-26 17:57:45 +00:00
|
|
|
// Ensure namespace on the object is correct, or error if a conflicting namespace was set in the object.
|
|
|
|
requestNamespace, ok := genericapirequest.NamespaceFrom(ctx)
|
|
|
|
if !ok {
|
|
|
|
return nil, apierrors.NewInternalError(fmt.Errorf("no namespace information found in request context"))
|
|
|
|
}
|
|
|
|
if err := rest.EnsureObjectNamespaceMatchesRequestNamespace(requestNamespace, clientSecretRequest); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Making client secrets outside the supervisor's namespace does not make sense.
|
|
|
|
if requestNamespace != r.namespace {
|
|
|
|
msg := fmt.Sprintf("namespace must be %s on OIDCClientSecretRequest, was %s", r.namespace, requestNamespace)
|
|
|
|
traceValidationFailure(t, msg)
|
|
|
|
return nil, apierrors.NewBadRequest(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errs := genericvalidation.ValidateObjectMetaAccessor(
|
|
|
|
clientSecretRequest,
|
|
|
|
true,
|
|
|
|
func(name string, prefix bool) []string {
|
|
|
|
if prefix {
|
|
|
|
return []string{"generateName is not supported"}
|
|
|
|
}
|
|
|
|
var errs []string
|
|
|
|
if name == "client.oauth.pinniped.dev-" {
|
|
|
|
errs = append(errs, `must not equal 'client.oauth.pinniped.dev-'`)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(name, "client.oauth.pinniped.dev-") {
|
|
|
|
errs = append(errs, `must start with 'client.oauth.pinniped.dev-'`)
|
|
|
|
}
|
|
|
|
return append(errs, path.IsValidPathSegmentName(name)...)
|
|
|
|
},
|
|
|
|
field.NewPath("metadata"),
|
|
|
|
); len(errs) > 0 {
|
|
|
|
return nil, apierrors.NewInvalid(kindFromContext(ctx), clientSecretRequest.Name, errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// just a sanity check, not sure how to honor a dry run on a virtual API
|
|
|
|
if options != nil {
|
|
|
|
if len(options.DryRun) != 0 {
|
|
|
|
traceValidationFailure(t, "dryRun not supported")
|
|
|
|
errs := field.ErrorList{field.NotSupported(field.NewPath("dryRun"), options.DryRun, nil)}
|
|
|
|
return nil, apierrors.NewInvalid(kindFromContext(ctx), clientSecretRequest.Name, errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if createValidation != nil {
|
|
|
|
if err := createValidation(ctx, obj.DeepCopyObject()); err != nil {
|
|
|
|
traceFailureWithError(t, "validation webhook", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 20:45:21 +00:00
|
|
|
return clientSecretRequest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func traceValidationFailure(t *trace.Trace, msg string) {
|
|
|
|
t.Step("failure",
|
|
|
|
trace.Field{Key: "failureType", Value: "request validation"},
|
|
|
|
trace.Field{Key: "msg", Value: msg},
|
|
|
|
)
|
|
|
|
}
|
2022-08-26 17:57:45 +00:00
|
|
|
|
|
|
|
func traceFailureWithError(t *trace.Trace, failureType string, err error) {
|
|
|
|
t.Step("failure",
|
|
|
|
trace.Field{Key: "failureType", Value: failureType},
|
|
|
|
trace.Field{Key: "msg", Value: err.Error()},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateSecret(rand io.Reader) (string, error) {
|
|
|
|
var buf [32]byte
|
|
|
|
if _, err := io.ReadFull(rand, buf[:]); err != nil {
|
|
|
|
return "", fmt.Errorf("could not generate client secret: %w", err)
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(buf[:]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func name(obj runtime.Object) string {
|
|
|
|
accessor, err := meta.Accessor(obj)
|
|
|
|
if err != nil {
|
|
|
|
return "<unknown>"
|
|
|
|
}
|
|
|
|
return accessor.GetName()
|
|
|
|
}
|
|
|
|
|
|
|
|
func qualifiedResourceFromContext(ctx context.Context) schema.GroupResource {
|
|
|
|
if info, ok := genericapirequest.RequestInfoFrom(ctx); ok {
|
|
|
|
return schema.GroupResource{Group: info.APIGroup, Resource: info.Resource}
|
|
|
|
}
|
|
|
|
// this should never happen in practice
|
|
|
|
return clientsecretapi.Resource("oidcclientsecretrequests")
|
|
|
|
}
|
|
|
|
|
|
|
|
func kindFromContext(ctx context.Context) schema.GroupKind {
|
|
|
|
if info, ok := genericapirequest.RequestInfoFrom(ctx); ok {
|
|
|
|
return schema.GroupKind{Group: info.APIGroup, Kind: "OIDCClientSecretRequest"}
|
|
|
|
}
|
|
|
|
// this should never happen in practice
|
|
|
|
return clientsecretapi.Kind("OIDCClientSecretRequest")
|
|
|
|
}
|