2018-10-05 21:48:36 +00:00
|
|
|
package ca
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"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-06-18 21:44:19 +00:00
|
|
|
"github.com/smallstep/cli/config"
|
2019-02-06 03:32:01 +00:00
|
|
|
"github.com/smallstep/cli/crypto/x509util"
|
2018-10-05 21:48:36 +00:00
|
|
|
"gopkg.in/square/go-jose.v2/jwt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ClientOption is the type of options passed to the Client constructor.
|
|
|
|
type ClientOption func(o *clientOptions) error
|
|
|
|
|
|
|
|
type clientOptions struct {
|
|
|
|
transport http.RoundTripper
|
|
|
|
rootSHA256 string
|
|
|
|
rootFilename string
|
2019-06-18 02:01:04 +00:00
|
|
|
rootBundle []byte
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *clientOptions) apply(opts []ClientOption) (err error) {
|
|
|
|
for _, fn := range opts {
|
|
|
|
if err = fn(o); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
return tr, nil
|
|
|
|
}
|
2018-10-05 21:48:36 +00:00
|
|
|
return nil, errors.New("a transport, a root cert, or a root sha256 must be used")
|
|
|
|
}
|
|
|
|
return tr, nil
|
|
|
|
}
|
|
|
|
|
2019-06-18 02:01:04 +00:00
|
|
|
// WithTransport adds a custom transport to the Client. It will fail if a
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
func getTransportFromFile(filename string) (http.RoundTripper, error) {
|
|
|
|
data, err := ioutil.ReadFile(filename)
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
type ProvisionerOption func(o *provisionerOptions) error
|
|
|
|
|
|
|
|
type provisionerOptions struct {
|
|
|
|
cursor string
|
|
|
|
limit int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *provisionerOptions) apply(opts []ProvisionerOption) (err error) {
|
|
|
|
for _, fn := range opts {
|
|
|
|
if err = fn(o); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *provisionerOptions) rawQuery() string {
|
|
|
|
v := url.Values{}
|
|
|
|
if len(o.cursor) > 0 {
|
|
|
|
v.Set("cursor", o.cursor)
|
|
|
|
}
|
|
|
|
if o.limit > 0 {
|
|
|
|
v.Set("limit", strconv.Itoa(o.limit))
|
|
|
|
}
|
|
|
|
return v.Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithProvisionerCursor will request the provisioners starting with the given cursor.
|
|
|
|
func WithProvisionerCursor(cursor string) ProvisionerOption {
|
|
|
|
return func(o *provisionerOptions) error {
|
|
|
|
o.cursor = cursor
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithProvisionerLimit will request the given number of provisioners.
|
|
|
|
func WithProvisionerLimit(limit int) ProvisionerOption {
|
|
|
|
return func(o *provisionerOptions) error {
|
|
|
|
o.limit = limit
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// Client implements an HTTP client for the CA server.
|
|
|
|
type Client struct {
|
2019-01-12 03:13:06 +00:00
|
|
|
client *http.Client
|
|
|
|
endpoint *url.URL
|
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{
|
|
|
|
client: &http.Client{
|
|
|
|
Transport: tr,
|
|
|
|
},
|
|
|
|
endpoint: u,
|
|
|
|
}, 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) {
|
|
|
|
c.client.Transport = tr
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// Health performs the health request to the CA and returns the
|
|
|
|
// api.HealthResponse struct.
|
|
|
|
func (c *Client) Health() (*api.HealthResponse, error) {
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/health"})
|
|
|
|
resp, err := c.client.Get(u.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client GET %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var health api.HealthResponse
|
|
|
|
if err := readJSON(resp.Body, &health); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &health, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Root performs the root request to the CA with the given 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) Root(sha256Sum string) (*api.RootResponse, error) {
|
|
|
|
sha256Sum = strings.ToLower(strings.Replace(sha256Sum, "-", "", -1))
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/root/" + sha256Sum})
|
|
|
|
resp, err := getInsecureClient().Get(u.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client GET %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var root api.RootResponse
|
|
|
|
if err := readJSON(resp.Body, &root); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
// verify the sha256
|
|
|
|
sum := sha256.Sum256(root.RootPEM.Raw)
|
|
|
|
if sha256Sum != strings.ToLower(hex.EncodeToString(sum[:])) {
|
|
|
|
return nil, errors.New("root certificate SHA256 fingerprint do not match")
|
|
|
|
}
|
|
|
|
return &root, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign performs the sign request to the CA and returns the api.SignResponse
|
|
|
|
// struct.
|
|
|
|
func (c *Client) Sign(req *api.SignRequest) (*api.SignResponse, error) {
|
|
|
|
body, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error marshaling request")
|
|
|
|
}
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/sign"})
|
|
|
|
resp, err := c.client.Post(u.String(), "application/json", bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client POST %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var sign api.SignResponse
|
|
|
|
if err := readJSON(resp.Body, &sign); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
// Add tls.ConnectionState:
|
|
|
|
// We'll extract the root certificate from the verified chains
|
|
|
|
sign.TLS = resp.TLS
|
|
|
|
return &sign, nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 01:46:43 +00:00
|
|
|
// SignSSH performs the SSH certificate sign request to the CA and returns the
|
|
|
|
// api.SignSSHResponse struct.
|
|
|
|
func (c *Client) SignSSH(req *api.SignSSHRequest) (*api.SignSSHResponse, error) {
|
|
|
|
body, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error marshaling request")
|
|
|
|
}
|
2019-09-26 20:22:07 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/ssh/sign"})
|
2019-07-24 01:46:43 +00:00
|
|
|
resp, err := c.client.Post(u.String(), "application/json", bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client POST %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var sign api.SignSSHResponse
|
|
|
|
if err := readJSON(resp.Body, &sign); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &sign, nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// Renew performs the renew request to the CA and returns the api.SignResponse
|
|
|
|
// struct.
|
|
|
|
func (c *Client) Renew(tr http.RoundTripper) (*api.SignResponse, error) {
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/renew"})
|
|
|
|
client := &http.Client{Transport: tr}
|
|
|
|
resp, err := client.Post(u.String(), "application/json", http.NoBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client POST %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var sign api.SignResponse
|
|
|
|
if err := readJSON(resp.Body, &sign); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &sign, nil
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
// Revoke performs the revoke request to the CA and returns the api.RevokeResponse
|
|
|
|
// struct.
|
|
|
|
func (c *Client) Revoke(req *api.RevokeRequest, tr http.RoundTripper) (*api.RevokeResponse, error) {
|
|
|
|
body, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error marshaling request")
|
|
|
|
}
|
|
|
|
|
|
|
|
var client *http.Client
|
|
|
|
if tr != nil {
|
|
|
|
client = &http.Client{Transport: tr}
|
|
|
|
} else {
|
|
|
|
client = c.client
|
|
|
|
}
|
|
|
|
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/revoke"})
|
|
|
|
resp, err := client.Post(u.String(), "application/json", bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client POST %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-10-09 21:54:29 +00:00
|
|
|
// Provisioners performs the provisioners request to the CA 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) {
|
|
|
|
o := new(provisionerOptions)
|
|
|
|
if err := o.apply(opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{
|
|
|
|
Path: "/provisioners",
|
|
|
|
RawQuery: o.rawQuery(),
|
|
|
|
})
|
2018-10-09 21:54:29 +00:00
|
|
|
resp, err := c.client.Get(u.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client GET %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProvisionerKey performs the request to the CA to get the encrypted key for
|
|
|
|
// the given provisioner kid and returns the api.ProvisionerKeyResponse struct
|
|
|
|
// with the encrypted key.
|
|
|
|
func (c *Client) ProvisionerKey(kid string) (*api.ProvisionerKeyResponse, error) {
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/provisioners/" + kid + "/encrypted-key"})
|
|
|
|
resp, err := c.client.Get(u.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client GET %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-01-08 01:48:56 +00:00
|
|
|
// Roots performs the get roots request to the CA and returns the
|
|
|
|
// api.RootsResponse struct.
|
2019-01-12 03:08:08 +00:00
|
|
|
func (c *Client) Roots() (*api.RootsResponse, error) {
|
2019-01-08 01:48:56 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/roots"})
|
2019-01-12 03:08:08 +00:00
|
|
|
resp, err := c.client.Get(u.String())
|
2019-01-08 01:48:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client GET %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-05 01:51:32 +00:00
|
|
|
// Federation performs the get federation request to the CA and returns the
|
|
|
|
// api.FederationResponse struct.
|
2019-01-12 03:08:08 +00:00
|
|
|
func (c *Client) Federation() (*api.FederationResponse, error) {
|
2019-01-05 01:51:32 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/federation"})
|
2019-01-12 03:08:08 +00:00
|
|
|
resp, err := c.client.Get(u.String())
|
2019-01-05 01:51:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client GET %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:09:41 +00:00
|
|
|
// SSHKeys performs the get /ssh/keys request to the CA and returns the
|
2019-09-26 20:22:07 +00:00
|
|
|
// api.SSHKeysResponse struct.
|
|
|
|
func (c *Client) SSHKeys() (*api.SSHKeysResponse, error) {
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/ssh/keys"})
|
|
|
|
resp, err := c.client.Get(u.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client GET %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var keys api.SSHKeysResponse
|
|
|
|
if err := readJSON(resp.Body, &keys); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &keys, nil
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:09:41 +00:00
|
|
|
// SSHFederation performs the get /ssh/federation request to the CA and returns
|
|
|
|
// the api.SSHKeysResponse struct.
|
|
|
|
func (c *Client) SSHFederation() (*api.SSHKeysResponse, error) {
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/ssh/federation"})
|
|
|
|
resp, err := c.client.Get(u.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client GET %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var keys api.SSHKeysResponse
|
|
|
|
if err := readJSON(resp.Body, &keys); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &keys, nil
|
|
|
|
}
|
|
|
|
|
2019-10-04 02:03:38 +00:00
|
|
|
// SSHConfig performs the POST request to the CA to get the ssh configuration
|
|
|
|
// templates.
|
|
|
|
func (c *Client) SSHConfig(req *api.SSHConfigRequest) (*api.SSHConfigResponse, error) {
|
|
|
|
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"})
|
|
|
|
resp, err := c.client.Post(u.String(), "application/json", bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client POST %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return nil, readError(resp.Body)
|
|
|
|
}
|
|
|
|
var config api.SSHConfigResponse
|
|
|
|
if err := readJSON(resp.Body, &config); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
2019-06-18 02:01:04 +00:00
|
|
|
// RootFingerprint 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) RootFingerprint() (string, error) {
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/health"})
|
|
|
|
resp, err := c.client.Get(u.String())
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "client GET %s failed", u)
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-08-23 19:09:16 +00:00
|
|
|
dnsNames, ips, emails := 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,
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInsecureClient() *http.Client {
|
|
|
|
return &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-06-18 21:44:19 +00:00
|
|
|
return filepath.Join(config.StepPath(), "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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readError(r io.ReadCloser) error {
|
|
|
|
defer r.Close()
|
|
|
|
apiErr := new(api.Error)
|
|
|
|
if err := json.NewDecoder(r).Decode(apiErr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return apiErr
|
|
|
|
}
|