2021-01-07 22:58:09 +00:00
|
|
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
2020-12-08 01:39:51 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
// Package authenticator contains helper code for dealing with *Authenticator CRDs.
|
|
|
|
package authenticator
|
|
|
|
|
|
|
|
import (
|
2021-04-28 17:49:42 +00:00
|
|
|
"crypto/x509"
|
2020-12-08 01:39:51 +00:00
|
|
|
"encoding/base64"
|
2021-04-28 17:49:42 +00:00
|
|
|
"fmt"
|
2020-12-08 01:39:51 +00:00
|
|
|
|
2021-10-20 11:59:24 +00:00
|
|
|
"k8s.io/client-go/util/cert"
|
|
|
|
|
2021-02-16 19:00:08 +00:00
|
|
|
auth1alpha1 "go.pinniped.dev/generated/latest/apis/concierge/authentication/v1alpha1"
|
2020-12-08 01:39:51 +00:00
|
|
|
)
|
|
|
|
|
2020-12-08 20:36:27 +00:00
|
|
|
// Closer is a type that can be closed idempotently.
|
2020-12-08 16:08:53 +00:00
|
|
|
//
|
|
|
|
// This type is slightly different from io.Closer, because io.Closer can return an error and is not
|
|
|
|
// necessarily idempotent.
|
|
|
|
type Closer interface {
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
2020-12-08 01:39:51 +00:00
|
|
|
// CABundle returns a PEM-encoded CA bundle from the provided spec. If the provided spec is nil, a
|
|
|
|
// nil CA bundle will be returned. If the provided spec contains a CA bundle that is not properly
|
|
|
|
// encoded, an error will be returned.
|
2021-10-20 11:59:24 +00:00
|
|
|
func CABundle(spec *auth1alpha1.TLSSpec) (*x509.CertPool, []byte, error) {
|
2021-04-28 17:49:42 +00:00
|
|
|
if spec == nil || len(spec.CertificateAuthorityData) == 0 {
|
2021-10-20 11:59:24 +00:00
|
|
|
return nil, nil, nil
|
2020-12-08 01:39:51 +00:00
|
|
|
}
|
2021-04-28 17:49:42 +00:00
|
|
|
|
|
|
|
pem, err := base64.StdEncoding.DecodeString(spec.CertificateAuthorityData)
|
|
|
|
if err != nil {
|
2021-10-20 11:59:24 +00:00
|
|
|
return nil, nil, err
|
2021-04-28 17:49:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 11:59:24 +00:00
|
|
|
rootCAs, err := cert.NewPoolFromBytes(pem)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("certificateAuthorityData is not valid PEM: %w", err)
|
2021-04-28 17:49:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 11:59:24 +00:00
|
|
|
return rootCAs, pem, nil
|
2020-12-08 01:39:51 +00:00
|
|
|
}
|