2018-11-03 01:54:49 +00:00
|
|
|
package ca
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-12-15 19:24:46 +00:00
|
|
|
"crypto"
|
2019-01-17 22:48:33 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
2018-11-03 01:54:49 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-12-15 19:24:46 +00:00
|
|
|
"github.com/smallstep/certificates/api"
|
2020-08-24 21:44:11 +00:00
|
|
|
"go.step.sm/crypto/jose"
|
2018-11-03 01:54:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type tokenClaims struct {
|
|
|
|
SHA string `json:"sha"`
|
|
|
|
jose.Claims
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bootstrap is a helper function that initializes a client with the
|
|
|
|
// configuration in the bootstrap token.
|
|
|
|
func Bootstrap(token string) (*Client, error) {
|
2020-08-24 21:44:11 +00:00
|
|
|
tok, err := jose.ParseSigned(token)
|
2018-11-03 01:54:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error parsing token")
|
|
|
|
}
|
|
|
|
var claims tokenClaims
|
|
|
|
if err := tok.UnsafeClaimsWithoutVerification(&claims); err != nil {
|
2018-11-05 20:22:10 +00:00
|
|
|
return nil, errors.Wrap(err, "error parsing token")
|
2018-11-03 01:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate bootstrap token
|
|
|
|
switch {
|
2021-10-08 18:59:57 +00:00
|
|
|
case claims.SHA == "":
|
2018-11-03 01:54:49 +00:00
|
|
|
return nil, errors.New("invalid bootstrap token: sha claim is not present")
|
|
|
|
case !strings.HasPrefix(strings.ToLower(claims.Audience[0]), "http"):
|
|
|
|
return nil, errors.New("invalid bootstrap token: aud claim is not a url")
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewClient(claims.Audience[0], WithRootSHA256(claims.SHA))
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:48:20 +00:00
|
|
|
// BootstrapClient is a helper function that using the given bootstrap token
|
|
|
|
// return an http.Client configured with a Transport prepared to do TLS
|
|
|
|
// connections using the client certificate returned by the certificate
|
|
|
|
// authority. By default the server will kick off a routine that will renew the
|
2018-11-07 01:39:00 +00:00
|
|
|
// certificate after 2/3rd of the certificate's lifetime has expired.
|
2018-11-03 01:54:49 +00:00
|
|
|
//
|
|
|
|
// Usage:
|
2018-11-07 01:39:00 +00:00
|
|
|
//
|
2022-06-15 18:28:59 +00:00
|
|
|
// // Default example with certificate rotation.
|
|
|
|
// client, err := ca.BootstrapClient(ctx.Background(), token)
|
|
|
|
//
|
|
|
|
// // Example canceling automatic certificate rotation.
|
|
|
|
// ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
// defer cancel()
|
|
|
|
// client, err := ca.BootstrapClient(ctx, token)
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// resp, err := client.Get("https://internal.smallstep.com")
|
2021-05-03 19:48:20 +00:00
|
|
|
func BootstrapClient(ctx context.Context, token string, options ...TLSOption) (*http.Client, error) {
|
2022-12-21 15:02:26 +00:00
|
|
|
b, err := createBootstrap(token) //nolint:contextcheck // deeply nested context; temporary
|
2018-11-08 00:07:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-15 20:16:21 +00:00
|
|
|
// Make sure the tlsConfig has all supported roots on RootCAs.
|
2021-12-14 22:42:38 +00:00
|
|
|
//
|
|
|
|
// The roots request is only supported if identity certificates are not
|
|
|
|
// required. In all cases the current root is also added after applying all
|
|
|
|
// options too.
|
2021-12-15 19:24:46 +00:00
|
|
|
if !b.RequireClientAuth {
|
2021-12-14 22:42:38 +00:00
|
|
|
options = append(options, AddRootsToRootCAs())
|
|
|
|
}
|
2019-01-08 02:55:40 +00:00
|
|
|
|
2021-12-15 19:24:46 +00:00
|
|
|
transport, err := b.Client.Transport(ctx, b.SignResponse, b.PrivateKey, options...)
|
2018-11-08 00:07:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:48:20 +00:00
|
|
|
return &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}, nil
|
2018-11-08 00:07:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 19:48:20 +00:00
|
|
|
// BootstrapServer is a helper function that using the given token returns the
|
|
|
|
// given http.Server configured with a TLS certificate signed by the Certificate
|
|
|
|
// Authority. By default the server will kick off a routine that will renew the
|
2018-11-07 01:39:00 +00:00
|
|
|
// certificate after 2/3rd of the certificate's lifetime has expired.
|
2018-11-03 01:54:49 +00:00
|
|
|
//
|
2021-05-03 19:48:20 +00:00
|
|
|
// Without any extra option the server will be configured for mTLS, it will
|
|
|
|
// require and verify clients certificates, but options can be used to drop this
|
|
|
|
// requirement, the most common will be only verify the certs if given with
|
|
|
|
// ca.VerifyClientCertIfGiven(), or add extra CAs with
|
|
|
|
// ca.AddClientCA(*x509.Certificate).
|
|
|
|
//
|
2018-11-03 01:54:49 +00:00
|
|
|
// Usage:
|
2018-11-07 01:39:00 +00:00
|
|
|
//
|
2022-06-15 18:28:59 +00:00
|
|
|
// // Default example with certificate rotation.
|
|
|
|
// srv, err := ca.BootstrapServer(context.Background(), token, &http.Server{
|
|
|
|
// Addr: ":443",
|
|
|
|
// Handler: handler,
|
|
|
|
// })
|
|
|
|
//
|
|
|
|
// // Example canceling automatic certificate rotation.
|
|
|
|
// ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
// defer cancel()
|
|
|
|
// srv, err := ca.BootstrapServer(ctx, token, &http.Server{
|
|
|
|
// Addr: ":443",
|
|
|
|
// Handler: handler,
|
|
|
|
// })
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// srv.ListenAndServeTLS("", "")
|
2021-05-03 19:48:20 +00:00
|
|
|
func BootstrapServer(ctx context.Context, token string, base *http.Server, options ...TLSOption) (*http.Server, error) {
|
|
|
|
if base.TLSConfig != nil {
|
|
|
|
return nil, errors.New("server TLSConfig is already set")
|
|
|
|
}
|
|
|
|
|
2022-12-21 15:02:26 +00:00
|
|
|
b, err := createBootstrap(token) //nolint:contextcheck // deeply nested context; temporary
|
2018-11-03 01:54:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-15 20:16:21 +00:00
|
|
|
// Make sure the tlsConfig has all supported roots on RootCAs.
|
2021-12-14 22:42:38 +00:00
|
|
|
//
|
|
|
|
// The roots request is only supported if identity certificates are not
|
|
|
|
// required. In all cases the current root is also added after applying all
|
|
|
|
// options too.
|
2021-12-15 19:24:46 +00:00
|
|
|
if !b.RequireClientAuth {
|
2021-12-14 22:42:38 +00:00
|
|
|
options = append(options, AddRootsToCAs())
|
|
|
|
}
|
2019-01-08 02:55:40 +00:00
|
|
|
|
2021-12-15 19:24:46 +00:00
|
|
|
tlsConfig, err := b.Client.GetServerTLSConfig(ctx, b.SignResponse, b.PrivateKey, options...)
|
2018-11-03 01:54:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:48:20 +00:00
|
|
|
base.TLSConfig = tlsConfig
|
|
|
|
return base, nil
|
2018-11-03 01:54:49 +00:00
|
|
|
}
|
2019-01-17 22:48:33 +00:00
|
|
|
|
|
|
|
// BootstrapListener is a helper function that using the given token returns a
|
|
|
|
// TLS listener which accepts connections from an inner listener and wraps each
|
|
|
|
// connection with Server.
|
|
|
|
//
|
|
|
|
// Without any extra option the server will be configured for mTLS, it will
|
|
|
|
// require and verify clients certificates, but options can be used to drop this
|
|
|
|
// requirement, the most common will be only verify the certs if given with
|
|
|
|
// ca.VerifyClientCertIfGiven(), or add extra CAs with
|
|
|
|
// ca.AddClientCA(*x509.Certificate).
|
|
|
|
//
|
|
|
|
// Usage:
|
2022-06-15 18:28:59 +00:00
|
|
|
//
|
|
|
|
// inner, err := net.Listen("tcp", ":443")
|
|
|
|
// if err != nil {
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
// ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
// defer cancel()
|
|
|
|
// lis, err := ca.BootstrapListener(ctx, token, inner)
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// srv := grpc.NewServer()
|
|
|
|
// ... // register services
|
|
|
|
// srv.Serve(lis)
|
2019-01-17 22:48:33 +00:00
|
|
|
func BootstrapListener(ctx context.Context, token string, inner net.Listener, options ...TLSOption) (net.Listener, error) {
|
2022-12-21 15:02:26 +00:00
|
|
|
b, err := createBootstrap(token) //nolint:contextcheck // deeply nested context; temporary
|
2019-01-17 22:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-15 20:16:21 +00:00
|
|
|
// Make sure the tlsConfig has all supported roots on RootCAs.
|
2021-12-15 19:24:46 +00:00
|
|
|
//
|
|
|
|
// The roots request is only supported if identity certificates are not
|
|
|
|
// required. In all cases the current root is also added after applying all
|
|
|
|
// options too.
|
|
|
|
if !b.RequireClientAuth {
|
|
|
|
options = append(options, AddRootsToCAs())
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig, err := b.Client.GetServerTLSConfig(ctx, b.SignResponse, b.PrivateKey, options...)
|
2021-12-14 22:42:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:24:46 +00:00
|
|
|
return tls.NewListener(inner, tlsConfig), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type bootstrap struct {
|
|
|
|
Client *Client
|
|
|
|
RequireClientAuth bool
|
|
|
|
SignResponse *api.SignResponse
|
|
|
|
PrivateKey crypto.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func createBootstrap(token string) (*bootstrap, error) {
|
|
|
|
client, err := Bootstrap(token)
|
2019-01-17 22:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:24:46 +00:00
|
|
|
version, err := client.Version()
|
2019-01-17 22:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:24:46 +00:00
|
|
|
req, pk, err := CreateSignRequest(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-12-14 22:42:38 +00:00
|
|
|
}
|
2019-01-17 22:48:33 +00:00
|
|
|
|
2021-12-15 19:24:46 +00:00
|
|
|
sign, err := client.Sign(req)
|
2019-01-17 22:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:24:46 +00:00
|
|
|
return &bootstrap{
|
|
|
|
Client: client,
|
|
|
|
RequireClientAuth: version.RequireClientAuthentication,
|
|
|
|
SignResponse: sign,
|
|
|
|
PrivateKey: pk,
|
|
|
|
}, nil
|
2019-01-17 22:48:33 +00:00
|
|
|
}
|