2018-10-05 21:48:36 +00:00
|
|
|
package ca
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-12-21 11:56:56 +00:00
|
|
|
"context"
|
2018-10-05 21:48:36 +00:00
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2021-05-03 19:48:20 +00:00
|
|
|
"encoding/pem"
|
2022-11-10 22:49:16 +00:00
|
|
|
"fmt"
|
2018-10-05 21:48:36 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-06-18 02:01:04 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-10-26 18:35:15 +00:00
|
|
|
"strconv"
|
2018-10-05 21:48:36 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2018-11-01 04:36:01 +00:00
|
|
|
"github.com/smallstep/certificates/api"
|
2019-01-30 23:36:42 +00:00
|
|
|
"github.com/smallstep/certificates/authority"
|
2019-11-05 02:07:52 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2019-12-12 04:23:44 +00:00
|
|
|
"github.com/smallstep/certificates/ca/identity"
|
2019-12-16 07:54:25 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
2021-08-10 07:38:30 +00:00
|
|
|
"go.step.sm/cli-utils/step"
|
2021-05-03 19:48:20 +00:00
|
|
|
"go.step.sm/crypto/jose"
|
2020-08-14 22:33:50 +00:00
|
|
|
"go.step.sm/crypto/keyutil"
|
|
|
|
"go.step.sm/crypto/pemutil"
|
2020-08-12 00:59:33 +00:00
|
|
|
"go.step.sm/crypto/x509util"
|
2019-11-19 01:07:23 +00:00
|
|
|
"golang.org/x/net/http2"
|
2021-05-03 19:48:20 +00:00
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
"google.golang.org/protobuf/proto"
|
2018-10-05 21:48:36 +00:00
|
|
|
"gopkg.in/square/go-jose.v2/jwt"
|
|
|
|
)
|
|
|
|
|
2019-12-12 04:23:44 +00:00
|
|
|
// DisableIdentity is a global variable to disable the identity.
|
|
|
|
var DisableIdentity = false
|
|
|
|
|
2019-11-28 01:30:06 +00:00
|
|
|
// UserAgent will set the User-Agent header in the client requests.
|
|
|
|
var UserAgent = "step-http-client/1.0"
|
|
|
|
|
|
|
|
type uaClient struct {
|
|
|
|
Client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func newClient(transport http.RoundTripper) *uaClient {
|
|
|
|
return &uaClient{
|
|
|
|
Client: &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 19:43:48 +00:00
|
|
|
//nolint:gosec // used in bootstrap protocol
|
2019-11-28 01:30:06 +00:00
|
|
|
func newInsecureClient() *uaClient {
|
|
|
|
return &uaClient{
|
|
|
|
Client: &http.Client{
|
2021-04-21 23:05:27 +00:00
|
|
|
Transport: getDefaultTransport(&tls.Config{InsecureSkipVerify: true}),
|
2019-11-28 01:30:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *uaClient) GetTransport() http.RoundTripper {
|
|
|
|
return c.Client.Transport
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *uaClient) SetTransport(tr http.RoundTripper) {
|
|
|
|
c.Client.Transport = tr
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
func (c *uaClient) Get(u string) (*http.Response, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.GetWithContext(context.Background(), u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *uaClient) GetWithContext(ctx context.Context, u string) (*http.Response, error) {
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", u, http.NoBody)
|
2019-11-28 01:30:06 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, errors.Wrapf(err, "create GET %s request failed", u)
|
2019-11-28 01:30:06 +00:00
|
|
|
}
|
|
|
|
req.Header.Set("User-Agent", UserAgent)
|
|
|
|
return c.Client.Do(req)
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
func (c *uaClient) Post(u, contentType string, body io.Reader) (*http.Response, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.PostWithContext(context.Background(), u, contentType, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *uaClient) PostWithContext(ctx context.Context, u, contentType string, body io.Reader) (*http.Response, error) {
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", u, body)
|
2019-11-28 01:30:06 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, errors.Wrapf(err, "create POST %s request failed", u)
|
2019-11-28 01:30:06 +00:00
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
req.Header.Set("User-Agent", UserAgent)
|
|
|
|
return c.Client.Do(req)
|
|
|
|
}
|
|
|
|
|
2021-05-12 07:03:40 +00:00
|
|
|
func (c *uaClient) Do(req *http.Request) (*http.Response, error) {
|
|
|
|
req.Header.Set("User-Agent", UserAgent)
|
|
|
|
return c.Client.Do(req)
|
|
|
|
}
|
|
|
|
|
2019-11-20 19:50:46 +00:00
|
|
|
// RetryFunc defines the method used to retry a request. If it returns true, the
|
|
|
|
// request will be retried once.
|
|
|
|
type RetryFunc func(code int) bool
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// ClientOption is the type of options passed to the Client constructor.
|
|
|
|
type ClientOption func(o *clientOptions) error
|
|
|
|
|
|
|
|
type clientOptions struct {
|
2021-04-08 01:57:48 +00:00
|
|
|
transport http.RoundTripper
|
|
|
|
rootSHA256 string
|
|
|
|
rootFilename string
|
|
|
|
rootBundle []byte
|
|
|
|
certificate tls.Certificate
|
|
|
|
getClientCertificate func(*tls.CertificateRequestInfo) (*tls.Certificate, error)
|
|
|
|
retryFunc RetryFunc
|
2021-05-03 19:48:20 +00:00
|
|
|
x5cJWK *jose.JSONWebKey
|
|
|
|
x5cCertFile string
|
|
|
|
x5cCertStrs []string
|
|
|
|
x5cCert *x509.Certificate
|
|
|
|
x5cSubject string
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *clientOptions) apply(opts []ClientOption) (err error) {
|
2022-08-23 19:43:48 +00:00
|
|
|
o.applyDefaultIdentity()
|
2018-10-05 21:48:36 +00:00
|
|
|
for _, fn := range opts {
|
|
|
|
if err = fn(o); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-19 01:07:23 +00:00
|
|
|
// applyDefaultIdentity sets the options for the default identity if the
|
|
|
|
// identity file is present. The identity is enabled by default.
|
2022-08-23 19:43:48 +00:00
|
|
|
func (o *clientOptions) applyDefaultIdentity() {
|
2019-11-27 02:48:28 +00:00
|
|
|
if DisableIdentity {
|
2022-08-23 19:43:48 +00:00
|
|
|
return
|
2019-11-27 02:48:28 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 00:26:53 +00:00
|
|
|
// Do not load an identity if something fails
|
2019-12-12 04:23:44 +00:00
|
|
|
i, err := identity.LoadDefaultIdentity()
|
2019-11-19 01:07:23 +00:00
|
|
|
if err != nil {
|
2022-08-23 19:43:48 +00:00
|
|
|
return
|
2019-11-19 01:07:23 +00:00
|
|
|
}
|
2019-12-12 04:23:44 +00:00
|
|
|
if err := i.Validate(); err != nil {
|
2022-08-23 19:43:48 +00:00
|
|
|
return
|
2019-11-19 01:07:23 +00:00
|
|
|
}
|
2019-12-12 04:23:44 +00:00
|
|
|
crt, err := i.TLSCertificate()
|
2019-11-19 01:07:23 +00:00
|
|
|
if err != nil {
|
2022-08-23 19:43:48 +00:00
|
|
|
return
|
2019-11-19 01:07:23 +00:00
|
|
|
}
|
2019-12-12 04:23:44 +00:00
|
|
|
o.certificate = crt
|
2021-04-08 01:57:48 +00:00
|
|
|
o.getClientCertificate = i.GetClientCertificateFunc()
|
2019-11-19 01:07:23 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// checkTransport checks if other ways to set up a transport have been provided.
|
|
|
|
// If they have it returns an error.
|
|
|
|
func (o *clientOptions) checkTransport() error {
|
2019-06-18 02:01:04 +00:00
|
|
|
if o.transport != nil || o.rootFilename != "" || o.rootSHA256 != "" || o.rootBundle != nil {
|
2018-10-05 21:48:36 +00:00
|
|
|
return errors.New("multiple transport methods have been configured")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getTransport returns the transport configured in the clientOptions.
|
|
|
|
func (o *clientOptions) getTransport(endpoint string) (tr http.RoundTripper, err error) {
|
|
|
|
if o.transport != nil {
|
|
|
|
tr = o.transport
|
|
|
|
}
|
|
|
|
if o.rootFilename != "" {
|
|
|
|
if tr, err = getTransportFromFile(o.rootFilename); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if o.rootSHA256 != "" {
|
|
|
|
if tr, err = getTransportFromSHA256(endpoint, o.rootSHA256); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2019-06-18 02:01:04 +00:00
|
|
|
if o.rootBundle != nil {
|
|
|
|
if tr, err = getTransportFromCABundle(o.rootBundle); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// As the last option attempt to load the default root ca
|
2018-10-05 21:48:36 +00:00
|
|
|
if tr == nil {
|
2019-06-18 02:01:04 +00:00
|
|
|
rootFile := getRootCAPath()
|
|
|
|
if _, err := os.Stat(rootFile); err == nil {
|
|
|
|
if tr, err = getTransportFromFile(rootFile); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 01:07:23 +00:00
|
|
|
if tr == nil {
|
|
|
|
return nil, errors.New("a transport, a root cert, or a root sha256 must be used")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add client certificate if available
|
|
|
|
if o.certificate.Certificate != nil {
|
|
|
|
switch tr := tr.(type) {
|
|
|
|
case *http.Transport:
|
2019-11-27 02:48:28 +00:00
|
|
|
if tr.TLSClientConfig == nil {
|
2022-08-19 00:46:20 +00:00
|
|
|
tr.TLSClientConfig = &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
2019-11-27 02:48:28 +00:00
|
|
|
}
|
2019-11-19 01:07:23 +00:00
|
|
|
if len(tr.TLSClientConfig.Certificates) == 0 && tr.TLSClientConfig.GetClientCertificate == nil {
|
|
|
|
tr.TLSClientConfig.Certificates = []tls.Certificate{o.certificate}
|
2021-04-08 01:57:48 +00:00
|
|
|
tr.TLSClientConfig.GetClientCertificate = o.getClientCertificate
|
2019-11-19 01:07:23 +00:00
|
|
|
}
|
|
|
|
case *http2.Transport:
|
2019-11-27 02:48:28 +00:00
|
|
|
if tr.TLSClientConfig == nil {
|
2022-08-19 00:46:20 +00:00
|
|
|
tr.TLSClientConfig = &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
2019-11-27 02:48:28 +00:00
|
|
|
}
|
2019-11-19 01:07:23 +00:00
|
|
|
if len(tr.TLSClientConfig.Certificates) == 0 && tr.TLSClientConfig.GetClientCertificate == nil {
|
|
|
|
tr.TLSClientConfig.Certificates = []tls.Certificate{o.certificate}
|
2021-04-08 01:57:48 +00:00
|
|
|
tr.TLSClientConfig.GetClientCertificate = o.getClientCertificate
|
2019-11-19 01:07:23 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unsupported transport type %T", tr)
|
|
|
|
}
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
2019-11-19 01:07:23 +00:00
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
return tr, nil
|
|
|
|
}
|
|
|
|
|
2021-10-29 06:58:18 +00:00
|
|
|
// WithTransport adds a custom transport to the Client. It will fail if a
|
2019-06-18 02:01:04 +00:00
|
|
|
// previous option to create the transport has been configured.
|
2018-10-05 21:48:36 +00:00
|
|
|
func WithTransport(tr http.RoundTripper) ClientOption {
|
|
|
|
return func(o *clientOptions) error {
|
|
|
|
if err := o.checkTransport(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.transport = tr
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 06:58:18 +00:00
|
|
|
// WithInsecure adds a insecure transport that bypasses TLS verification.
|
|
|
|
func WithInsecure() ClientOption {
|
|
|
|
return func(o *clientOptions) error {
|
|
|
|
o.transport = &http.Transport{
|
2022-08-19 00:46:20 +00:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
TLSClientConfig: &tls.Config{
|
2022-08-23 19:43:48 +00:00
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
//nolint:gosec // insecure option
|
2022-08-19 00:46:20 +00:00
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
2021-10-29 06:58:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 02:01:04 +00:00
|
|
|
// WithRootFile will create the transport using the given root certificate. It
|
|
|
|
// will fail if a previous option to create the transport has been configured.
|
2018-10-05 21:48:36 +00:00
|
|
|
func WithRootFile(filename string) ClientOption {
|
|
|
|
return func(o *clientOptions) error {
|
|
|
|
if err := o.checkTransport(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.rootFilename = filename
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 02:01:04 +00:00
|
|
|
// WithRootSHA256 will create the transport using an insecure client to retrieve
|
|
|
|
// the root certificate using its fingerprint. It will fail if a previous option
|
|
|
|
// to create the transport has been configured.
|
2018-10-05 21:48:36 +00:00
|
|
|
func WithRootSHA256(sum string) ClientOption {
|
|
|
|
return func(o *clientOptions) error {
|
|
|
|
if err := o.checkTransport(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.rootSHA256 = sum
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 02:01:04 +00:00
|
|
|
// WithCABundle will create the transport using the given root certificates. It
|
|
|
|
// will fail if a previous option to create the transport has been configured.
|
|
|
|
func WithCABundle(bundle []byte) ClientOption {
|
|
|
|
return func(o *clientOptions) error {
|
|
|
|
if err := o.checkTransport(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.rootBundle = bundle
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 01:07:23 +00:00
|
|
|
// WithCertificate will set the given certificate as the TLS client certificate
|
|
|
|
// in the client.
|
2021-05-03 19:48:20 +00:00
|
|
|
func WithCertificate(cert tls.Certificate) ClientOption {
|
2019-11-19 01:07:23 +00:00
|
|
|
return func(o *clientOptions) error {
|
2021-05-03 19:48:20 +00:00
|
|
|
o.certificate = cert
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithAdminX5C will set the given file as the X5C certificate for use
|
|
|
|
// by the client.
|
|
|
|
func WithAdminX5C(certs []*x509.Certificate, key interface{}, passwordFile string) ClientOption {
|
|
|
|
return func(o *clientOptions) error {
|
|
|
|
// Get private key from given key file
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
opts []jose.Option
|
|
|
|
)
|
2021-10-08 18:59:57 +00:00
|
|
|
if passwordFile != "" {
|
2021-05-03 19:48:20 +00:00
|
|
|
opts = append(opts, jose.WithPasswordFile(passwordFile))
|
|
|
|
}
|
|
|
|
blk, err := pemutil.Serialize(key)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error serializing private key")
|
|
|
|
}
|
|
|
|
o.x5cJWK, err = jose.ParseKey(pem.EncodeToMemory(blk), opts...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.x5cCertStrs, err = jose.ValidateX5C(certs, o.x5cJWK.Key)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error validating x5c certificate chain and key for use in x5c header")
|
|
|
|
}
|
|
|
|
|
|
|
|
o.x5cCert = certs[0]
|
2022-04-08 01:14:43 +00:00
|
|
|
switch leaf := certs[0]; {
|
|
|
|
case leaf.Subject.CommonName != "":
|
|
|
|
o.x5cSubject = leaf.Subject.CommonName
|
|
|
|
case len(leaf.DNSNames) > 0:
|
|
|
|
o.x5cSubject = leaf.DNSNames[0]
|
|
|
|
case len(leaf.EmailAddresses) > 0:
|
|
|
|
o.x5cSubject = leaf.EmailAddresses[0]
|
2021-05-03 19:48:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 01:07:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 19:50:46 +00:00
|
|
|
// WithRetryFunc defines a method used to retry a request.
|
|
|
|
func WithRetryFunc(fn RetryFunc) ClientOption {
|
|
|
|
return func(o *clientOptions) error {
|
|
|
|
o.retryFunc = fn
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
func getTransportFromFile(filename string) (http.RoundTripper, error) {
|
2021-11-12 23:46:34 +00:00
|
|
|
data, err := os.ReadFile(filename)
|
2018-10-05 21:48:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", filename)
|
|
|
|
}
|
|
|
|
pool := x509.NewCertPool()
|
2019-02-04 18:29:52 +00:00
|
|
|
if !pool.AppendCertsFromPEM(data) {
|
|
|
|
return nil, errors.Errorf("error parsing %s: no certificates found", filename)
|
|
|
|
}
|
2018-10-05 21:48:36 +00:00
|
|
|
return getDefaultTransport(&tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
PreferServerCipherSuites: true,
|
|
|
|
RootCAs: pool,
|
2021-04-21 23:05:27 +00:00
|
|
|
}), nil
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getTransportFromSHA256(endpoint, sum string) (http.RoundTripper, error) {
|
2018-10-24 19:42:37 +00:00
|
|
|
u, err := parseEndpoint(endpoint)
|
2018-10-05 21:48:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-24 19:42:37 +00:00
|
|
|
client := &Client{endpoint: u}
|
2018-10-05 21:48:36 +00:00
|
|
|
root, err := client.Root(sum)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
pool.AddCert(root.RootPEM.Certificate)
|
|
|
|
return getDefaultTransport(&tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
PreferServerCipherSuites: true,
|
|
|
|
RootCAs: pool,
|
2021-04-21 23:05:27 +00:00
|
|
|
}), nil
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 02:01:04 +00:00
|
|
|
func getTransportFromCABundle(bundle []byte) (http.RoundTripper, error) {
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
if !pool.AppendCertsFromPEM(bundle) {
|
|
|
|
return nil, errors.New("error parsing ca bundle: no certificates found")
|
|
|
|
}
|
|
|
|
return getDefaultTransport(&tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
PreferServerCipherSuites: true,
|
|
|
|
RootCAs: pool,
|
2021-04-21 23:05:27 +00:00
|
|
|
}), nil
|
2019-06-18 02:01:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 02:29:45 +00:00
|
|
|
// parseEndpoint parses and validates the given endpoint. It supports general
|
|
|
|
// URLs like https://ca.smallstep.com[:port][/path], and incomplete URLs like
|
|
|
|
// ca.smallstep.com[:port][/path].
|
2018-10-24 19:42:37 +00:00
|
|
|
func parseEndpoint(endpoint string) (*url.URL, error) {
|
|
|
|
u, err := url.Parse(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error parsing endpoint '%s'", endpoint)
|
|
|
|
}
|
2018-11-27 02:29:45 +00:00
|
|
|
|
|
|
|
// URLs are generally parsed as:
|
|
|
|
// [scheme:][//[userinfo@]host][/]path[?query][#fragment]
|
|
|
|
// But URLs that do not start with a slash after the scheme are interpreted as
|
|
|
|
// scheme:opaque[?query][#fragment]
|
|
|
|
if u.Opaque == "" {
|
|
|
|
if u.Scheme == "" {
|
|
|
|
u.Scheme = "https"
|
|
|
|
}
|
|
|
|
if u.Host == "" {
|
|
|
|
// endpoint looks like ca.smallstep.com or ca.smallstep.com/1.0/sign
|
|
|
|
if u.Path != "" {
|
|
|
|
parts := strings.SplitN(u.Path, "/", 2)
|
|
|
|
u.Host = parts[0]
|
|
|
|
if len(parts) == 2 {
|
|
|
|
u.Path = parts[1]
|
|
|
|
} else {
|
|
|
|
u.Path = ""
|
|
|
|
}
|
|
|
|
return parseEndpoint(u.String())
|
|
|
|
}
|
|
|
|
return nil, errors.Errorf("error parsing endpoint: url '%s' is not valid", endpoint)
|
|
|
|
}
|
|
|
|
return u, nil
|
2018-10-24 19:42:37 +00:00
|
|
|
}
|
2018-11-27 02:29:45 +00:00
|
|
|
// scheme:opaque[?query][#fragment]
|
|
|
|
// endpoint looks like ca.smallstep.com:443 or ca.smallstep.com:443/1.0/sign
|
|
|
|
return parseEndpoint("https://" + endpoint)
|
2018-10-24 19:42:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 18:35:15 +00:00
|
|
|
// ProvisionerOption is the type of options passed to the Provisioner method.
|
2022-04-25 17:23:07 +00:00
|
|
|
type ProvisionerOption func(o *ProvisionerOptions) error
|
2018-10-26 18:35:15 +00:00
|
|
|
|
2022-04-25 17:23:07 +00:00
|
|
|
// ProvisionerOptions stores options for the provisioner CRUD API.
|
|
|
|
type ProvisionerOptions struct {
|
|
|
|
Cursor string
|
|
|
|
Limit int
|
|
|
|
ID string
|
|
|
|
Name string
|
2018-10-26 18:35:15 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 16:43:32 +00:00
|
|
|
// Apply caches provisioner options on a struct for later use.
|
2022-04-25 17:23:07 +00:00
|
|
|
func (o *ProvisionerOptions) Apply(opts []ProvisionerOption) (err error) {
|
2018-10-26 18:35:15 +00:00
|
|
|
for _, fn := range opts {
|
|
|
|
if err = fn(o); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-25 17:23:07 +00:00
|
|
|
func (o *ProvisionerOptions) rawQuery() string {
|
2018-10-26 18:35:15 +00:00
|
|
|
v := url.Values{}
|
2022-05-18 16:43:32 +00:00
|
|
|
if o.Cursor != "" {
|
2022-04-25 17:23:07 +00:00
|
|
|
v.Set("cursor", o.Cursor)
|
2018-10-26 18:35:15 +00:00
|
|
|
}
|
2022-04-25 17:23:07 +00:00
|
|
|
if o.Limit > 0 {
|
|
|
|
v.Set("limit", strconv.Itoa(o.Limit))
|
2018-10-26 18:35:15 +00:00
|
|
|
}
|
2022-05-18 16:43:32 +00:00
|
|
|
if o.ID != "" {
|
2022-04-25 17:23:07 +00:00
|
|
|
v.Set("id", o.ID)
|
2021-05-03 19:48:20 +00:00
|
|
|
}
|
2022-05-18 16:43:32 +00:00
|
|
|
if o.Name != "" {
|
2022-04-25 17:23:07 +00:00
|
|
|
v.Set("name", o.Name)
|
2021-05-03 19:48:20 +00:00
|
|
|
}
|
2018-10-26 18:35:15 +00:00
|
|
|
return v.Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithProvisionerCursor will request the provisioners starting with the given cursor.
|
|
|
|
func WithProvisionerCursor(cursor string) ProvisionerOption {
|
2022-04-25 17:23:07 +00:00
|
|
|
return func(o *ProvisionerOptions) error {
|
|
|
|
o.Cursor = cursor
|
2018-10-26 18:35:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithProvisionerLimit will request the given number of provisioners.
|
|
|
|
func WithProvisionerLimit(limit int) ProvisionerOption {
|
2022-04-25 17:23:07 +00:00
|
|
|
return func(o *ProvisionerOptions) error {
|
|
|
|
o.Limit = limit
|
2018-10-26 18:35:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:48:20 +00:00
|
|
|
// WithProvisionerID will request the given provisioner.
|
|
|
|
func WithProvisionerID(id string) ProvisionerOption {
|
2022-04-25 17:23:07 +00:00
|
|
|
return func(o *ProvisionerOptions) error {
|
|
|
|
o.ID = id
|
2021-05-03 19:48:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithProvisionerName will request the given provisioner.
|
|
|
|
func WithProvisionerName(name string) ProvisionerOption {
|
2022-04-25 17:23:07 +00:00
|
|
|
return func(o *ProvisionerOptions) error {
|
|
|
|
o.Name = name
|
2021-05-03 19:48:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// Client implements an HTTP client for the CA server.
|
|
|
|
type Client struct {
|
2019-11-28 01:30:06 +00:00
|
|
|
client *uaClient
|
2019-11-20 19:50:46 +00:00
|
|
|
endpoint *url.URL
|
|
|
|
retryFunc RetryFunc
|
|
|
|
opts []ClientOption
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a new Client with the given endpoint and options.
|
|
|
|
func NewClient(endpoint string, opts ...ClientOption) (*Client, error) {
|
2018-10-24 19:42:37 +00:00
|
|
|
u, err := parseEndpoint(endpoint)
|
2018-10-05 21:48:36 +00:00
|
|
|
if err != nil {
|
2018-10-24 19:42:37 +00:00
|
|
|
return nil, err
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
// Retrieve transport from options.
|
|
|
|
o := new(clientOptions)
|
|
|
|
if err := o.apply(opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tr, err := o.getTransport(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
2019-11-28 01:30:06 +00:00
|
|
|
client: newClient(tr),
|
2019-11-20 19:50:46 +00:00
|
|
|
endpoint: u,
|
|
|
|
retryFunc: o.retryFunc,
|
|
|
|
opts: opts,
|
2018-10-05 21:48:36 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-11-20 19:50:46 +00:00
|
|
|
func (c *Client) retryOnError(r *http.Response) bool {
|
|
|
|
if c.retryFunc != nil {
|
|
|
|
if c.retryFunc(r.StatusCode) {
|
|
|
|
o := new(clientOptions)
|
|
|
|
if err := o.apply(c.opts); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tr, err := o.getTransport(c.endpoint.String())
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
r.Body.Close()
|
2019-11-28 01:30:06 +00:00
|
|
|
c.client.SetTransport(tr)
|
2019-11-20 19:50:46 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-11 23:22:53 +00:00
|
|
|
// GetCaURL returns the configured CA url.
|
2022-03-11 23:13:39 +00:00
|
|
|
func (c *Client) GetCaURL() string {
|
|
|
|
return c.endpoint.String()
|
|
|
|
}
|
|
|
|
|
2019-11-21 00:03:31 +00:00
|
|
|
// GetRootCAs returns the RootCAs certificate pool from the configured
|
|
|
|
// transport.
|
|
|
|
func (c *Client) GetRootCAs() *x509.CertPool {
|
2019-11-28 01:30:06 +00:00
|
|
|
switch t := c.client.GetTransport().(type) {
|
2019-11-21 00:03:31 +00:00
|
|
|
case *http.Transport:
|
|
|
|
if t.TLSClientConfig != nil {
|
|
|
|
return t.TLSClientConfig.RootCAs
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case *http2.Transport:
|
|
|
|
if t.TLSClientConfig != nil {
|
|
|
|
return t.TLSClientConfig.RootCAs
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 02:37:12 +00:00
|
|
|
// SetTransport updates the transport of the internal HTTP client.
|
|
|
|
func (c *Client) SetTransport(tr http.RoundTripper) {
|
2019-11-28 01:30:06 +00:00
|
|
|
c.client.SetTransport(tr)
|
2019-04-04 02:37:12 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// Version performs the version request to the CA with an empty context and returns the
|
2019-11-21 01:15:48 +00:00
|
|
|
// api.VersionResponse struct.
|
|
|
|
func (c *Client) Version() (*api.VersionResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.VersionWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
// VersionWithContext performs the version request to the CA with the provided context
|
|
|
|
// and returns the api.VersionResponse struct.
|
|
|
|
func (c *Client) VersionWithContext(ctx context.Context) (*api.VersionResponse, error) {
|
2019-11-21 01:15:48 +00:00
|
|
|
var retried bool
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/version"})
|
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.GetWithContext(ctx, u.String())
|
2019-11-21 01:15:48 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2019-11-21 01:15:48 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-21 01:15:48 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var version api.VersionResponse
|
|
|
|
if err := readJSON(resp.Body, &version); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrapf(http.StatusInternalServerError, err, "client.Version; error reading %s", u)
|
2019-11-21 01:15:48 +00:00
|
|
|
}
|
|
|
|
return &version, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// Health performs the health request to the CA with an empty context
|
|
|
|
// and returns the api.HealthResponse struct.
|
2018-10-05 21:48:36 +00:00
|
|
|
func (c *Client) Health() (*api.HealthResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.HealthWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
// HealthWithContext performs the health request to the CA with the provided context
|
|
|
|
// and returns the api.HealthResponse struct.
|
|
|
|
func (c *Client) HealthWithContext(ctx context.Context) (*api.HealthResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2018-10-05 21:48:36 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/health"})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.GetWithContext(ctx, u.String())
|
2018-10-05 21:48:36 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2018-10-05 21:48:36 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var health api.HealthResponse
|
|
|
|
if err := readJSON(resp.Body, &health); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrapf(http.StatusInternalServerError, err, "client.Health; error reading %s", u)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
return &health, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// Root performs the root request to the CA with an empty context and the provided
|
|
|
|
// SHA256 and returns the api.RootResponse struct. It uses an insecure client, but
|
|
|
|
// it checks the resulting root certificate with the given SHA256, returning an error
|
|
|
|
// if they do not match.
|
2018-10-05 21:48:36 +00:00
|
|
|
func (c *Client) Root(sha256Sum string) (*api.RootResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.RootWithContext(context.Background(), sha256Sum)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RootWithContext performs the root request to the CA with an empty context and the provided
|
|
|
|
// SHA256 and returns the api.RootResponse struct. It uses an insecure client, but
|
|
|
|
// it checks the resulting root certificate with the given SHA256, returning an error
|
|
|
|
// if they do not match.
|
|
|
|
func (c *Client) RootWithContext(ctx context.Context, sha256Sum string) (*api.RootResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2021-10-08 18:59:57 +00:00
|
|
|
sha256Sum = strings.ToLower(strings.ReplaceAll(sha256Sum, "-", ""))
|
2018-10-05 21:48:36 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/root/" + sha256Sum})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := newInsecureClient().GetWithContext(ctx, u.String())
|
2018-10-05 21:48:36 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2018-10-05 21:48:36 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var root api.RootResponse
|
|
|
|
if err := readJSON(resp.Body, &root); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrapf(http.StatusInternalServerError, err, "client.Root; error reading %s", u)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
// verify the sha256
|
|
|
|
sum := sha256.Sum256(root.RootPEM.Raw)
|
2021-10-08 18:59:57 +00:00
|
|
|
if !strings.EqualFold(sha256Sum, strings.ToLower(hex.EncodeToString(sum[:]))) {
|
2021-11-18 23:12:44 +00:00
|
|
|
return nil, errs.BadRequest("root certificate fingerprint does not match")
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
return &root, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// Sign performs the sign request to the CA with an empty context and returns
|
|
|
|
// the api.SignResponse struct.
|
2018-10-05 21:48:36 +00:00
|
|
|
func (c *Client) Sign(req *api.SignRequest) (*api.SignResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.SignWithContext(context.Background(), req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignWithContext performs the sign request to the CA with the provided context
|
|
|
|
// and returns the api.SignResponse struct.
|
|
|
|
func (c *Client) SignWithContext(ctx context.Context, req *api.SignRequest) (*api.SignResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2018-10-05 21:48:36 +00:00
|
|
|
body, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "client.Sign; error marshaling request")
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/sign"})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.PostWithContext(ctx, u.String(), "application/json", bytes.NewReader(body))
|
2018-10-05 21:48:36 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2018-10-05 21:48:36 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var sign api.SignResponse
|
|
|
|
if err := readJSON(resp.Body, &sign); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrapf(http.StatusInternalServerError, err, "client.Sign; error reading %s", u)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
// Add tls.ConnectionState:
|
|
|
|
// We'll extract the root certificate from the verified chains
|
|
|
|
sign.TLS = resp.TLS
|
|
|
|
return &sign, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// Renew performs the renew request to the CA with an empty context and
|
|
|
|
// returns the api.SignResponse struct.
|
2018-10-05 21:48:36 +00:00
|
|
|
func (c *Client) Renew(tr http.RoundTripper) (*api.SignResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.RenewWithContext(context.Background(), tr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenewWithContext performs the renew request to the CA with the provided context
|
|
|
|
// and returns the api.SignResponse struct.
|
|
|
|
func (c *Client) RenewWithContext(ctx context.Context, tr http.RoundTripper) (*api.SignResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2018-10-05 21:48:36 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/renew"})
|
|
|
|
client := &http.Client{Transport: tr}
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", u.String(), http.NoBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := client.Do(req)
|
2018-10-05 21:48:36 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2018-10-05 21:48:36 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var sign api.SignResponse
|
|
|
|
if err := readJSON(resp.Body, &sign); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrapf(http.StatusInternalServerError, err, "client.Renew; error reading %s", u)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
return &sign, nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 04:21:01 +00:00
|
|
|
// RenewWithToken performs the renew request to the CA with the given
|
2022-12-21 11:56:56 +00:00
|
|
|
// authorization token and and empty context and returns the api.SignResponse struct.
|
|
|
|
// This method is generally used to renew an expired certificate.
|
2022-03-11 04:21:01 +00:00
|
|
|
func (c *Client) RenewWithToken(token string) (*api.SignResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.RenewWithTokenAndContext(context.Background(), token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenewWithTokenAndContext performs the renew request to the CA with the given
|
|
|
|
// authorization token and context and returns the api.SignResponse struct.
|
|
|
|
// This method is generally used to renew an expired certificate.
|
|
|
|
func (c *Client) RenewWithTokenAndContext(ctx context.Context, token string) (*api.SignResponse, error) {
|
2022-03-11 04:21:01 +00:00
|
|
|
var retried bool
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/renew"})
|
2022-12-21 11:56:56 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", u.String(), http.NoBody)
|
2022-03-11 04:21:01 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, errors.Wrapf(err, "create POST %s request failed", u)
|
2022-03-11 04:21:01 +00:00
|
|
|
}
|
|
|
|
req.Header.Add("Authorization", "Bearer "+token)
|
|
|
|
retry:
|
|
|
|
resp, err := c.client.Do(req)
|
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2022-03-11 04:21:01 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2022-03-11 04:21:01 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var sign api.SignResponse
|
|
|
|
if err := readJSON(resp.Body, &sign); err != nil {
|
|
|
|
return nil, errs.Wrapf(http.StatusInternalServerError, err, "client.RenewWithToken; error reading %s", u)
|
|
|
|
}
|
|
|
|
return &sign, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// Rekey performs the rekey request to the CA with an empty context and
|
|
|
|
// returns the api.SignResponse struct.
|
2021-04-29 23:06:45 +00:00
|
|
|
func (c *Client) Rekey(req *api.RekeyRequest, tr http.RoundTripper) (*api.SignResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.RekeyWithContext(context.Background(), req, tr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RekeyWithContext performs the rekey request to the CA with the provided context
|
|
|
|
// and returns the api.SignResponse struct.
|
|
|
|
func (c *Client) RekeyWithContext(ctx context.Context, req *api.RekeyRequest, tr http.RoundTripper) (*api.SignResponse, error) {
|
2021-04-29 23:06:45 +00:00
|
|
|
var retried bool
|
|
|
|
body, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error marshaling request")
|
|
|
|
}
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/rekey"})
|
|
|
|
client := &http.Client{Transport: tr}
|
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
httpReq, err := http.NewRequestWithContext(ctx, "POST", u.String(), bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := client.Do(httpReq)
|
2021-04-29 23:06:45 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2021-04-29 23:06:45 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2021-04-29 23:06:45 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var sign api.SignResponse
|
|
|
|
if err := readJSON(resp.Body, &sign); err != nil {
|
|
|
|
return nil, errs.Wrapf(http.StatusInternalServerError, err, "client.Rekey; error reading %s", u)
|
|
|
|
}
|
|
|
|
return &sign, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// Revoke performs the revoke request to the CA with an empty context and returns
|
|
|
|
// the api.RevokeResponse struct.
|
2019-03-05 08:07:13 +00:00
|
|
|
func (c *Client) Revoke(req *api.RevokeRequest, tr http.RoundTripper) (*api.RevokeResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.RevokeWithContext(context.Background(), req, tr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RevokeWithContext performs the revoke request to the CA with the provided context and
|
|
|
|
// returns the api.RevokeResponse struct.
|
|
|
|
func (c *Client) RevokeWithContext(ctx context.Context, req *api.RevokeRequest, tr http.RoundTripper) (*api.RevokeResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2019-03-05 08:07:13 +00:00
|
|
|
body, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error marshaling request")
|
|
|
|
}
|
2019-11-28 01:30:06 +00:00
|
|
|
var client *uaClient
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2019-03-05 08:07:13 +00:00
|
|
|
if tr != nil {
|
2019-11-28 01:30:06 +00:00
|
|
|
client = newClient(tr)
|
2019-03-05 08:07:13 +00:00
|
|
|
} else {
|
|
|
|
client = c.client
|
|
|
|
}
|
|
|
|
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/revoke"})
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := client.PostWithContext(ctx, u.String(), "application/json", bytes.NewReader(body))
|
2019-03-05 08:07:13 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2019-03-05 08:07:13 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var revoke api.RevokeResponse
|
|
|
|
if err := readJSON(resp.Body, &revoke); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &revoke, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// Provisioners performs the provisioners request to the CA with an empty context
|
|
|
|
// and returns the api.ProvisionersResponse struct with a map of provisioners.
|
2018-10-26 18:35:15 +00:00
|
|
|
//
|
|
|
|
// ProvisionerOption WithProvisionerCursor and WithProvisionLimit can be used to
|
|
|
|
// paginate the provisioners.
|
|
|
|
func (c *Client) Provisioners(opts ...ProvisionerOption) (*api.ProvisionersResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.ProvisionersWithContext(context.Background(), opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProvisionersWithContext performs the provisioners request to the CA with the provided context
|
|
|
|
// and returns the api.ProvisionersResponse struct with a map of provisioners.
|
|
|
|
//
|
|
|
|
// ProvisionerOption WithProvisionerCursor and WithProvisionLimit can be used to
|
|
|
|
// paginate the provisioners.
|
|
|
|
func (c *Client) ProvisionersWithContext(ctx context.Context, opts ...ProvisionerOption) (*api.ProvisionersResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2022-04-25 17:23:07 +00:00
|
|
|
o := new(ProvisionerOptions)
|
|
|
|
if err := o.Apply(opts); err != nil {
|
2018-10-26 18:35:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{
|
|
|
|
Path: "/provisioners",
|
|
|
|
RawQuery: o.rawQuery(),
|
|
|
|
})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.GetWithContext(ctx, u.String())
|
2018-10-09 21:54:29 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2018-10-09 21:54:29 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2018-10-09 21:54:29 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var provisioners api.ProvisionersResponse
|
|
|
|
if err := readJSON(resp.Body, &provisioners); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &provisioners, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// ProvisionerKey performs the request to the CA with an empty context to get
|
|
|
|
// the encrypted key for the given provisioner kid and returns the api.ProvisionerKeyResponse
|
|
|
|
// struct with the encrypted key.
|
2018-10-09 21:54:29 +00:00
|
|
|
func (c *Client) ProvisionerKey(kid string) (*api.ProvisionerKeyResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.ProvisionerKeyWithContext(context.Background(), kid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProvisionerKeyWithContext performs the request to the CA with the provided context to get
|
|
|
|
// the encrypted key for the given provisioner kid and returns the api.ProvisionerKeyResponse
|
|
|
|
// struct with the encrypted key.
|
|
|
|
func (c *Client) ProvisionerKeyWithContext(ctx context.Context, kid string) (*api.ProvisionerKeyResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2018-10-09 21:54:29 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/provisioners/" + kid + "/encrypted-key"})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.GetWithContext(ctx, u.String())
|
2018-10-09 21:54:29 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2018-10-09 21:54:29 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2018-10-09 21:54:29 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var key api.ProvisionerKeyResponse
|
|
|
|
if err := readJSON(resp.Body, &key); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &key, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// Roots performs the get roots request to the CA with an empty context
|
|
|
|
// and returns the api.RootsResponse struct.
|
2019-01-12 03:08:08 +00:00
|
|
|
func (c *Client) Roots() (*api.RootsResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.RootsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
// RootsWithContext performs the get roots request to the CA with the provided context
|
|
|
|
// and returns the api.RootsResponse struct.
|
|
|
|
func (c *Client) RootsWithContext(ctx context.Context) (*api.RootsResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2019-01-08 01:48:56 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/roots"})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.GetWithContext(ctx, u.String())
|
2019-01-08 01:48:56 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2019-01-08 01:48:56 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2019-01-08 01:48:56 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
2019-01-15 01:35:38 +00:00
|
|
|
var roots api.RootsResponse
|
|
|
|
if err := readJSON(resp.Body, &roots); err != nil {
|
2019-01-08 01:48:56 +00:00
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
2019-01-15 01:35:38 +00:00
|
|
|
return &roots, nil
|
2019-01-08 01:48:56 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// Federation performs the get federation request to the CA with an empty context
|
|
|
|
// and returns the api.FederationResponse struct.
|
2019-01-12 03:08:08 +00:00
|
|
|
func (c *Client) Federation() (*api.FederationResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.FederationWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
// FederationWithContext performs the get federation request to the CA with the provided context
|
|
|
|
// and returns the api.FederationResponse struct.
|
|
|
|
func (c *Client) FederationWithContext(ctx context.Context) (*api.FederationResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2019-01-05 01:51:32 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/federation"})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.GetWithContext(ctx, u.String())
|
2019-01-05 01:51:32 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2019-01-05 01:51:32 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2019-01-05 01:51:32 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var federation api.FederationResponse
|
|
|
|
if err := readJSON(resp.Body, &federation); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &federation, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// SSHSign performs the POST /ssh/sign request to the CA with an empty context
|
|
|
|
// and returns the api.SSHSignResponse struct.
|
2019-10-09 01:35:28 +00:00
|
|
|
func (c *Client) SSHSign(req *api.SSHSignRequest) (*api.SSHSignResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.SSHSignWithContext(context.Background(), req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHSignWithContext performs the POST /ssh/sign request to the CA with the provided context
|
|
|
|
// and returns the api.SSHSignResponse struct.
|
|
|
|
func (c *Client) SSHSignWithContext(ctx context.Context, req *api.SSHSignRequest) (*api.SSHSignResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2019-10-09 01:35:28 +00:00
|
|
|
body, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error marshaling request")
|
|
|
|
}
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/ssh/sign"})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.PostWithContext(ctx, u.String(), "application/json", bytes.NewReader(body))
|
2019-10-09 01:35:28 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2019-10-09 01:35:28 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2019-10-09 01:35:28 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var sign api.SSHSignResponse
|
|
|
|
if err := readJSON(resp.Body, &sign); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &sign, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// SSHRenew performs the POST /ssh/renew request to the CA with an empty context
|
|
|
|
// and returns the api.SSHRenewResponse struct.
|
2019-10-28 18:50:43 +00:00
|
|
|
func (c *Client) SSHRenew(req *api.SSHRenewRequest) (*api.SSHRenewResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.SSHRenewWithContext(context.Background(), req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHRenewWithContext performs the POST /ssh/renew request to the CA with the provided context
|
|
|
|
// and returns the api.SSHRenewResponse struct.
|
|
|
|
func (c *Client) SSHRenewWithContext(ctx context.Context, req *api.SSHRenewRequest) (*api.SSHRenewResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2019-10-28 18:50:43 +00:00
|
|
|
body, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error marshaling request")
|
|
|
|
}
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/ssh/renew"})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.PostWithContext(ctx, u.String(), "application/json", bytes.NewReader(body))
|
2019-10-28 18:50:43 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2019-10-28 18:50:43 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var renew api.SSHRenewResponse
|
|
|
|
if err := readJSON(resp.Body, &renew); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &renew, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// SSHRekey performs the POST /ssh/rekey request to the CA with an empty context
|
|
|
|
// and returns the api.SSHRekeyResponse struct.
|
2019-10-28 18:50:43 +00:00
|
|
|
func (c *Client) SSHRekey(req *api.SSHRekeyRequest) (*api.SSHRekeyResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.SSHRekeyWithContext(context.Background(), req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHRekeyWithContext performs the POST /ssh/rekey request to the CA with the provided context
|
|
|
|
// and returns the api.SSHRekeyResponse struct.
|
|
|
|
func (c *Client) SSHRekeyWithContext(ctx context.Context, req *api.SSHRekeyRequest) (*api.SSHRekeyResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2019-10-28 18:50:43 +00:00
|
|
|
body, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error marshaling request")
|
|
|
|
}
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/ssh/rekey"})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.PostWithContext(ctx, u.String(), "application/json", bytes.NewReader(body))
|
2019-10-28 18:50:43 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2019-10-28 18:50:43 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var rekey api.SSHRekeyResponse
|
|
|
|
if err := readJSON(resp.Body, &rekey); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &rekey, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// SSHRevoke performs the POST /ssh/revoke request to the CA with an empty context
|
|
|
|
// and returns the api.SSHRevokeResponse struct.
|
2019-10-28 18:50:43 +00:00
|
|
|
func (c *Client) SSHRevoke(req *api.SSHRevokeRequest) (*api.SSHRevokeResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.SSHRevokeWithContext(context.Background(), req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHRevokeWithContext performs the POST /ssh/revoke request to the CA with the provided context
|
|
|
|
// and returns the api.SSHRevokeResponse struct.
|
|
|
|
func (c *Client) SSHRevokeWithContext(ctx context.Context, req *api.SSHRevokeRequest) (*api.SSHRevokeResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2019-10-28 18:50:43 +00:00
|
|
|
body, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error marshaling request")
|
|
|
|
}
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/ssh/revoke"})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.PostWithContext(ctx, u.String(), "application/json", bytes.NewReader(body))
|
2019-10-28 18:50:43 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2019-10-28 18:50:43 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var revoke api.SSHRevokeResponse
|
|
|
|
if err := readJSON(resp.Body, &revoke); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &revoke, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// SSHRoots performs the GET /ssh/roots request to the CA with an empty context
|
|
|
|
// and returns the api.SSHRootsResponse struct.
|
2019-10-09 01:35:28 +00:00
|
|
|
func (c *Client) SSHRoots() (*api.SSHRootsResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.SSHRootsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHRootsWithContext performs the GET /ssh/roots request to the CA with the provided context
|
|
|
|
// and returns the api.SSHRootsResponse struct.
|
|
|
|
func (c *Client) SSHRootsWithContext(ctx context.Context) (*api.SSHRootsResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2019-10-09 01:35:28 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/ssh/roots"})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.GetWithContext(ctx, u.String())
|
2019-09-26 20:22:07 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2019-09-26 20:22:07 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2019-09-26 20:22:07 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
2019-10-09 01:35:28 +00:00
|
|
|
var keys api.SSHRootsResponse
|
2019-09-26 20:22:07 +00:00
|
|
|
if err := readJSON(resp.Body, &keys); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &keys, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// SSHFederation performs the get /ssh/federation request to the CA with an empty context
|
|
|
|
// and returns the api.SSHRootsResponse struct.
|
2019-10-09 01:35:28 +00:00
|
|
|
func (c *Client) SSHFederation() (*api.SSHRootsResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.SSHFederationWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHFederationWithContext performs the get /ssh/federation request to the CA with the provided context
|
|
|
|
// and returns the api.SSHRootsResponse struct.
|
|
|
|
func (c *Client) SSHFederationWithContext(ctx context.Context) (*api.SSHRootsResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2019-10-09 01:09:41 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/ssh/federation"})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.GetWithContext(ctx, u.String())
|
2019-10-09 01:09:41 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2019-10-09 01:09:41 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2019-10-09 01:09:41 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
2019-10-09 01:35:28 +00:00
|
|
|
var keys api.SSHRootsResponse
|
2019-10-09 01:09:41 +00:00
|
|
|
if err := readJSON(resp.Body, &keys); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &keys, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// SSHConfig performs the POST /ssh/config request to the CA with an empty context
|
|
|
|
// to get the ssh configuration templates.
|
2019-10-04 02:03:38 +00:00
|
|
|
func (c *Client) SSHConfig(req *api.SSHConfigRequest) (*api.SSHConfigResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.SSHConfigWithContext(context.Background(), req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHConfigWithContext performs the POST /ssh/config request to the CA with the provided context
|
|
|
|
// to get the ssh configuration templates.
|
|
|
|
func (c *Client) SSHConfigWithContext(ctx context.Context, req *api.SSHConfigRequest) (*api.SSHConfigResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2019-10-04 02:03:38 +00:00
|
|
|
body, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error marshaling request")
|
|
|
|
}
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/ssh/config"})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.PostWithContext(ctx, u.String(), "application/json", bytes.NewReader(body))
|
2019-10-04 02:03:38 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2019-10-04 02:03:38 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2019-10-04 02:03:38 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
2021-10-08 18:59:57 +00:00
|
|
|
var cfg api.SSHConfigResponse
|
|
|
|
if err := readJSON(resp.Body, &cfg); err != nil {
|
2019-10-04 02:03:38 +00:00
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
2021-10-08 18:59:57 +00:00
|
|
|
return &cfg, nil
|
2019-10-04 02:03:38 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// SSHCheckHost performs the POST /ssh/check-host request to the CA with an empty context,
|
|
|
|
// the principal and a token and returns the api.SSHCheckPrincipalResponse.
|
2021-10-08 18:59:57 +00:00
|
|
|
func (c *Client) SSHCheckHost(principal, token string) (*api.SSHCheckPrincipalResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.SSHCheckHostWithContext(context.Background(), principal, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHCheckHostWithContext performs the POST /ssh/check-host request to the CA with the provided context,
|
|
|
|
// principal and token and returns the api.SSHCheckPrincipalResponse.
|
|
|
|
func (c *Client) SSHCheckHostWithContext(ctx context.Context, principal, token string) (*api.SSHCheckPrincipalResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2019-10-10 20:08:57 +00:00
|
|
|
body, err := json.Marshal(&api.SSHCheckPrincipalRequest{
|
|
|
|
Type: provisioner.SSHHostCert,
|
|
|
|
Principal: principal,
|
2019-12-10 07:14:56 +00:00
|
|
|
Token: token,
|
2019-10-10 20:08:57 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2019-12-17 23:53:37 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "error marshaling request",
|
|
|
|
errs.WithMessage("Failed to marshal the check-host request"))
|
2019-10-10 20:08:57 +00:00
|
|
|
}
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/ssh/check-host"})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.PostWithContext(ctx, u.String(), "application/json", bytes.NewReader(body))
|
2019-10-10 20:08:57 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2019-10-10 20:08:57 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2021-11-19 02:17:36 +00:00
|
|
|
return nil, readError(resp.Body)
|
2019-10-10 20:08:57 +00:00
|
|
|
}
|
|
|
|
var check api.SSHCheckPrincipalResponse
|
|
|
|
if err := readJSON(resp.Body, &check); err != nil {
|
2022-09-21 05:12:08 +00:00
|
|
|
return nil, errs.Wrapf(http.StatusInternalServerError, err, "error reading %s response",
|
|
|
|
[]any{u, errs.WithMessage("Failed to parse response from /ssh/check-host endpoint")}...)
|
2019-10-10 20:08:57 +00:00
|
|
|
}
|
|
|
|
return &check, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// SSHGetHosts performs the GET /ssh/get-hosts request to the CA with an empty context.
|
2019-10-27 00:40:26 +00:00
|
|
|
func (c *Client) SSHGetHosts() (*api.SSHGetHostsResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.SSHGetHostsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHGetHostsWithContext performs the GET /ssh/get-hosts request to the CA with the provided context.
|
|
|
|
func (c *Client) SSHGetHostsWithContext(ctx context.Context) (*api.SSHGetHostsResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2019-11-27 22:27:23 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/ssh/hosts"})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.GetWithContext(ctx, u.String())
|
2019-10-25 20:47:49 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2019-10-25 20:47:49 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2019-10-25 20:47:49 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var hosts api.SSHGetHostsResponse
|
|
|
|
if err := readJSON(resp.Body, &hosts); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &hosts, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 11:56:56 +00:00
|
|
|
// SSHBastion performs the POST /ssh/bastion request to the CA with an empty context.
|
2019-11-15 04:32:38 +00:00
|
|
|
func (c *Client) SSHBastion(req *api.SSHBastionRequest) (*api.SSHBastionResponse, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.SSHBastionWithContext(context.Background(), req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHBastionWithContext performs the POST /ssh/bastion request to the CA with the provided context.
|
|
|
|
func (c *Client) SSHBastionWithContext(ctx context.Context, req *api.SSHBastionRequest) (*api.SSHBastionResponse, error) {
|
2019-11-20 19:50:46 +00:00
|
|
|
var retried bool
|
2019-11-15 04:32:38 +00:00
|
|
|
body, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errors.Wrap(err, "client.SSHBastion; error marshaling request")
|
2019-11-15 04:32:38 +00:00
|
|
|
}
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/ssh/bastion"})
|
2019-11-20 19:50:46 +00:00
|
|
|
retry:
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.PostWithContext(ctx, u.String(), "application/json", bytes.NewReader(body))
|
2019-11-15 04:32:38 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return nil, clientError(err)
|
2019-11-15 04:32:38 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2022-12-21 15:02:26 +00:00
|
|
|
if !retried && c.retryOnError(resp) { //nolint:contextcheck // deeply nested context; retry using the same context
|
2019-11-20 19:50:46 +00:00
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2019-11-15 04:32:38 +00:00
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var bastion api.SSHBastionResponse
|
|
|
|
if err := readJSON(resp.Body, &bastion); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errors.Wrapf(err, "client.SSHBastion; error reading %s", u)
|
2019-11-15 04:32:38 +00:00
|
|
|
}
|
|
|
|
return &bastion, nil
|
|
|
|
}
|
|
|
|
|
2019-06-18 02:01:04 +00:00
|
|
|
// RootFingerprint is a helper method that returns the current root fingerprint.
|
2022-12-21 11:56:56 +00:00
|
|
|
// It does an health connection and gets the fingerprint from the TLS verified chains.
|
2019-06-18 02:01:04 +00:00
|
|
|
func (c *Client) RootFingerprint() (string, error) {
|
2022-12-21 11:56:56 +00:00
|
|
|
return c.RootFingerprintWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
// RootFingerprintWithContext is a helper method that returns the current root fingerprint.
|
|
|
|
// It does an health connection and gets the fingerprint from the TLS verified chains.
|
|
|
|
func (c *Client) RootFingerprintWithContext(ctx context.Context) (string, error) {
|
2019-06-18 02:01:04 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/health"})
|
2022-12-21 11:56:56 +00:00
|
|
|
resp, err := c.client.GetWithContext(ctx, u.String())
|
2019-06-18 02:01:04 +00:00
|
|
|
if err != nil {
|
2022-11-10 22:49:16 +00:00
|
|
|
return "", clientError(err)
|
2019-06-18 02:01:04 +00:00
|
|
|
}
|
2022-08-23 19:43:48 +00:00
|
|
|
defer resp.Body.Close()
|
2019-06-18 02:01:04 +00:00
|
|
|
if resp.TLS == nil || len(resp.TLS.VerifiedChains) == 0 {
|
|
|
|
return "", errors.New("missing verified chains")
|
|
|
|
}
|
|
|
|
lastChain := resp.TLS.VerifiedChains[len(resp.TLS.VerifiedChains)-1]
|
|
|
|
if len(lastChain) == 0 {
|
|
|
|
return "", errors.New("missing verified chains")
|
|
|
|
}
|
|
|
|
return x509util.Fingerprint(lastChain[len(lastChain)-1]), nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// CreateSignRequest is a helper function that given an x509 OTT returns a
|
|
|
|
// simple but secure sign request as well as the private key used.
|
|
|
|
func CreateSignRequest(ott string) (*api.SignRequest, crypto.PrivateKey, error) {
|
|
|
|
token, err := jwt.ParseSigned(ott)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "error parsing ott")
|
|
|
|
}
|
2019-01-30 23:36:42 +00:00
|
|
|
var claims authority.Claims
|
2018-10-05 21:48:36 +00:00
|
|
|
if err := token.UnsafeClaimsWithoutVerification(&claims); err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "error parsing ott")
|
|
|
|
}
|
|
|
|
|
|
|
|
pk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "error generating key")
|
|
|
|
}
|
|
|
|
|
2020-06-23 18:10:45 +00:00
|
|
|
dnsNames, ips, emails, uris := x509util.SplitSANs(claims.SANs)
|
2019-03-07 01:01:12 +00:00
|
|
|
if claims.Email != "" {
|
|
|
|
emails = append(emails, claims.Email)
|
|
|
|
}
|
2019-01-30 23:36:42 +00:00
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
template := &x509.CertificateRequest{
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: claims.Subject,
|
|
|
|
},
|
|
|
|
SignatureAlgorithm: x509.ECDSAWithSHA256,
|
2019-01-30 23:36:42 +00:00
|
|
|
DNSNames: dnsNames,
|
|
|
|
IPAddresses: ips,
|
2019-03-07 01:01:12 +00:00
|
|
|
EmailAddresses: emails,
|
2020-06-23 18:10:45 +00:00
|
|
|
URIs: uris,
|
2018-10-25 02:49:16 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
csr, err := x509.CreateCertificateRequest(rand.Reader, template, pk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "error creating certificate request")
|
|
|
|
}
|
|
|
|
cr, err := x509.ParseCertificateRequest(csr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "error parsing certificate request")
|
|
|
|
}
|
|
|
|
if err := cr.CheckSignature(); err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "error signing certificate request")
|
|
|
|
}
|
|
|
|
return &api.SignRequest{
|
|
|
|
CsrPEM: api.CertificateRequest{CertificateRequest: cr},
|
|
|
|
OTT: ott,
|
|
|
|
}, pk, nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 03:11:54 +00:00
|
|
|
// CreateCertificateRequest creates a new CSR with the given common name and
|
|
|
|
// SANs. If no san is provided the commonName will set also a SAN.
|
|
|
|
func CreateCertificateRequest(commonName string, sans ...string) (*api.CertificateRequest, crypto.PrivateKey, error) {
|
2020-08-14 22:33:50 +00:00
|
|
|
key, err := keyutil.GenerateDefaultKey()
|
2019-11-21 03:11:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return createCertificateRequest(commonName, sans, key)
|
|
|
|
}
|
|
|
|
|
2019-12-12 04:23:44 +00:00
|
|
|
// CreateIdentityRequest returns a new CSR to create the identity. If an
|
|
|
|
// identity was already present it reuses the private key.
|
|
|
|
func CreateIdentityRequest(commonName string, sans ...string) (*api.CertificateRequest, crypto.PrivateKey, error) {
|
|
|
|
var identityKey crypto.PrivateKey
|
|
|
|
if i, err := identity.LoadDefaultIdentity(); err == nil && i.Key != "" {
|
|
|
|
if k, err := pemutil.Read(i.Key); err == nil {
|
|
|
|
identityKey = k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if identityKey == nil {
|
|
|
|
return CreateCertificateRequest(commonName, sans...)
|
|
|
|
}
|
|
|
|
return createCertificateRequest(commonName, sans, identityKey)
|
|
|
|
}
|
|
|
|
|
2019-12-12 21:16:17 +00:00
|
|
|
// LoadDefaultIdentity is a wrapper for identity.LoadDefaultIdentity.
|
|
|
|
func LoadDefaultIdentity() (*identity.Identity, error) {
|
|
|
|
return identity.LoadDefaultIdentity()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteDefaultIdentity is a wrapper for identity.WriteDefaultIdentity.
|
|
|
|
func WriteDefaultIdentity(certChain []api.Certificate, key crypto.PrivateKey) error {
|
|
|
|
return identity.WriteDefaultIdentity(certChain, key)
|
|
|
|
}
|
|
|
|
|
2019-11-21 03:11:54 +00:00
|
|
|
func createCertificateRequest(commonName string, sans []string, key crypto.PrivateKey) (*api.CertificateRequest, crypto.PrivateKey, error) {
|
|
|
|
if len(sans) == 0 {
|
|
|
|
sans = []string{commonName}
|
|
|
|
}
|
2020-06-23 18:10:45 +00:00
|
|
|
dnsNames, ips, emails, uris := x509util.SplitSANs(sans)
|
2019-11-21 03:11:54 +00:00
|
|
|
template := &x509.CertificateRequest{
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: commonName,
|
|
|
|
},
|
|
|
|
DNSNames: dnsNames,
|
|
|
|
IPAddresses: ips,
|
|
|
|
EmailAddresses: emails,
|
2020-06-23 18:10:45 +00:00
|
|
|
URIs: uris,
|
2019-11-21 03:11:54 +00:00
|
|
|
}
|
|
|
|
csr, err := x509.CreateCertificateRequest(rand.Reader, template, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
cr, err := x509.ParseCertificateRequest(csr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := cr.CheckSignature(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.CertificateRequest{CertificateRequest: cr}, key, nil
|
|
|
|
}
|
|
|
|
|
2019-06-18 02:01:04 +00:00
|
|
|
// getRootCAPath returns the path where the root CA is stored based on the
|
|
|
|
// STEPPATH environment variable.
|
|
|
|
func getRootCAPath() string {
|
2021-08-10 07:38:30 +00:00
|
|
|
return filepath.Join(step.Path(), "certs", "root_ca.crt")
|
2019-06-18 02:01:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
func readJSON(r io.ReadCloser, v interface{}) error {
|
|
|
|
defer r.Close()
|
|
|
|
return json.NewDecoder(r).Decode(v)
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:48:20 +00:00
|
|
|
func readProtoJSON(r io.ReadCloser, m proto.Message) error {
|
|
|
|
defer r.Close()
|
2021-11-12 23:46:34 +00:00
|
|
|
data, err := io.ReadAll(r)
|
2021-05-03 19:48:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return protojson.Unmarshal(data, m)
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
func readError(r io.ReadCloser) error {
|
|
|
|
defer r.Close()
|
2019-12-16 07:54:25 +00:00
|
|
|
apiErr := new(errs.Error)
|
2018-10-05 21:48:36 +00:00
|
|
|
if err := json.NewDecoder(r).Decode(apiErr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return apiErr
|
|
|
|
}
|
2022-11-10 22:49:16 +00:00
|
|
|
|
|
|
|
func clientError(err error) error {
|
|
|
|
var uerr *url.Error
|
|
|
|
if errors.As(err, &uerr) {
|
|
|
|
return fmt.Errorf("client %s %s failed: %w",
|
|
|
|
strings.ToUpper(uerr.Op), uerr.URL, uerr.Err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("client request failed: %w", err)
|
|
|
|
}
|