2021-03-10 18:30:06 +00:00
|
|
|
// Copyright 2021 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package issuer
|
|
|
|
|
|
|
|
import (
|
2021-03-15 16:24:07 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2021-03-10 18:30:06 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/util/errors"
|
|
|
|
|
|
|
|
"go.pinniped.dev/internal/constable"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultCertIssuerErr = constable.Error("failed to issue cert")
|
|
|
|
|
2021-03-13 00:09:16 +00:00
|
|
|
type ClientCertIssuer interface {
|
2021-03-15 16:24:07 +00:00
|
|
|
Name() string
|
2021-03-13 00:09:16 +00:00
|
|
|
IssueClientCertPEM(username string, groups []string, ttl time.Duration) (certPEM, keyPEM []byte, err error)
|
2021-03-10 18:30:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 00:09:16 +00:00
|
|
|
var _ ClientCertIssuer = ClientCertIssuers{}
|
2021-03-10 18:30:06 +00:00
|
|
|
|
2021-03-13 00:09:16 +00:00
|
|
|
type ClientCertIssuers []ClientCertIssuer
|
2021-03-10 18:30:06 +00:00
|
|
|
|
2021-03-15 16:24:07 +00:00
|
|
|
func (c ClientCertIssuers) Name() string {
|
|
|
|
if len(c) == 0 {
|
|
|
|
return "empty-client-cert-issuers"
|
|
|
|
}
|
|
|
|
|
|
|
|
names := make([]string, 0, len(c))
|
|
|
|
for _, issuer := range c {
|
|
|
|
names = append(names, issuer.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(names, ",")
|
|
|
|
}
|
|
|
|
|
2021-03-13 00:09:16 +00:00
|
|
|
func (c ClientCertIssuers) IssueClientCertPEM(username string, groups []string, ttl time.Duration) ([]byte, []byte, error) {
|
2021-03-10 18:30:06 +00:00
|
|
|
var errs []error
|
|
|
|
|
|
|
|
for _, issuer := range c {
|
2021-03-13 00:09:16 +00:00
|
|
|
certPEM, keyPEM, err := issuer.IssueClientCertPEM(username, groups, ttl)
|
2021-03-10 18:30:06 +00:00
|
|
|
if err != nil {
|
2021-03-15 16:24:07 +00:00
|
|
|
errs = append(errs, fmt.Errorf("%s failed to issue client cert: %w", issuer.Name(), err))
|
2021-03-10 18:30:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return certPEM, keyPEM, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := errors.NewAggregate(errs); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil, defaultCertIssuerErr
|
|
|
|
}
|