2021-05-03 19:48:20 +00:00
|
|
|
package ca
|
|
|
|
|
|
|
|
import (
|
2021-05-12 07:03:40 +00:00
|
|
|
"bytes"
|
2021-05-03 19:48:20 +00:00
|
|
|
"crypto/x509"
|
2021-05-12 07:03:40 +00:00
|
|
|
"encoding/json"
|
2021-05-18 04:07:25 +00:00
|
|
|
"io"
|
2021-05-03 19:48:20 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
2021-05-18 23:50:54 +00:00
|
|
|
"strconv"
|
2021-05-03 19:48:20 +00:00
|
|
|
"time"
|
2021-05-03 19:48:20 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-05-03 19:48:20 +00:00
|
|
|
"github.com/smallstep/certificates/authority/admin"
|
|
|
|
adminAPI "github.com/smallstep/certificates/authority/admin/api"
|
2021-05-26 21:55:31 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2021-05-12 07:03:40 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
2021-05-03 19:48:20 +00:00
|
|
|
"go.step.sm/cli-utils/token"
|
|
|
|
"go.step.sm/cli-utils/token/provision"
|
|
|
|
"go.step.sm/crypto/jose"
|
|
|
|
"go.step.sm/crypto/randutil"
|
|
|
|
"go.step.sm/linkedca"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
2021-05-03 19:48:20 +00:00
|
|
|
)
|
|
|
|
|
2021-05-26 04:13:01 +00:00
|
|
|
var adminURLPrefix = "admin"
|
|
|
|
|
2021-05-20 23:02:20 +00:00
|
|
|
// AdminClient implements an HTTP client for the CA server.
|
|
|
|
type AdminClient struct {
|
2021-05-03 19:48:20 +00:00
|
|
|
client *uaClient
|
|
|
|
endpoint *url.URL
|
|
|
|
retryFunc RetryFunc
|
|
|
|
opts []ClientOption
|
|
|
|
x5cJWK *jose.JSONWebKey
|
|
|
|
x5cCertFile string
|
|
|
|
x5cCertStrs []string
|
|
|
|
x5cCert *x509.Certificate
|
|
|
|
x5cIssuer string
|
|
|
|
x5cSubject string
|
2021-05-03 19:48:20 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 23:02:20 +00:00
|
|
|
// NewAdminClient creates a new AdminClient with the given endpoint and options.
|
|
|
|
func NewAdminClient(endpoint string, opts ...ClientOption) (*AdminClient, error) {
|
2021-05-03 19:48:20 +00:00
|
|
|
u, err := parseEndpoint(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-05-20 23:02:20 +00:00
|
|
|
return &AdminClient{
|
2021-05-03 19:48:20 +00:00
|
|
|
client: newClient(tr),
|
|
|
|
endpoint: u,
|
|
|
|
retryFunc: o.retryFunc,
|
|
|
|
opts: opts,
|
|
|
|
x5cJWK: o.x5cJWK,
|
|
|
|
x5cCertFile: o.x5cCertFile,
|
|
|
|
x5cCertStrs: o.x5cCertStrs,
|
|
|
|
x5cCert: o.x5cCert,
|
|
|
|
x5cIssuer: o.x5cIssuer,
|
|
|
|
x5cSubject: o.x5cSubject,
|
2021-05-03 19:48:20 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
func (c *AdminClient) generateAdminToken(urlPath string) (string, error) {
|
2021-05-03 19:48:20 +00:00
|
|
|
// A random jwt id will be used to identify duplicated tokens
|
|
|
|
jwtID, err := randutil.Hex(64) // 256 bits
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
tokOptions := []token.Options{
|
|
|
|
token.WithJWTID(jwtID),
|
|
|
|
token.WithKid(c.x5cJWK.KeyID),
|
|
|
|
token.WithIssuer(c.x5cIssuer),
|
2021-10-08 18:59:57 +00:00
|
|
|
token.WithAudience(urlPath),
|
2021-05-03 19:48:20 +00:00
|
|
|
token.WithValidity(now, now.Add(token.DefaultValidity)),
|
|
|
|
token.WithX5CCerts(c.x5cCertStrs),
|
|
|
|
}
|
|
|
|
|
|
|
|
tok, err := provision.New(c.x5cSubject, tokOptions...)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tok.SignedString(c.x5cJWK.Algorithm, c.x5cJWK.Key)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-20 23:02:20 +00:00
|
|
|
func (c *AdminClient) retryOnError(r *http.Response) bool {
|
2021-05-03 19:48:20 +00:00
|
|
|
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()
|
|
|
|
c.client.SetTransport(tr)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-20 23:02:20 +00:00
|
|
|
// GetAdmin performs the GET /admin/admin/{id} request to the CA.
|
2021-05-26 04:13:01 +00:00
|
|
|
func (c *AdminClient) GetAdmin(id string) (*linkedca.Admin, error) {
|
2021-05-03 19:48:20 +00:00
|
|
|
var retried bool
|
2021-05-26 04:13:01 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: path.Join(adminURLPrefix, "admins", id)})
|
2021-05-03 19:48:20 +00:00
|
|
|
retry:
|
|
|
|
resp, err := c.client.Get(u.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client GET %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
if !retried && c.retryOnError(resp) {
|
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2021-05-20 23:02:20 +00:00
|
|
|
return nil, readAdminError(resp.Body)
|
2021-05-03 19:48:20 +00:00
|
|
|
}
|
2021-05-26 04:13:01 +00:00
|
|
|
var adm = new(linkedca.Admin)
|
2021-07-07 00:14:13 +00:00
|
|
|
if err := readProtoJSON(resp.Body, adm); err != nil {
|
2021-05-03 19:48:20 +00:00
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return adm, nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 21:55:31 +00:00
|
|
|
// AdminOption is the type of options passed to the Admin method.
|
2021-05-18 23:50:54 +00:00
|
|
|
type AdminOption func(o *adminOptions) error
|
|
|
|
|
|
|
|
type adminOptions struct {
|
|
|
|
cursor string
|
|
|
|
limit int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *adminOptions) apply(opts []AdminOption) (err error) {
|
|
|
|
for _, fn := range opts {
|
|
|
|
if err = fn(o); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *adminOptions) 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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithAdminCursor will request the admins starting with the given cursor.
|
|
|
|
func WithAdminCursor(cursor string) AdminOption {
|
|
|
|
return func(o *adminOptions) error {
|
|
|
|
o.cursor = cursor
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithAdminLimit will request the given number of admins.
|
|
|
|
func WithAdminLimit(limit int) AdminOption {
|
|
|
|
return func(o *adminOptions) error {
|
|
|
|
o.limit = limit
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 21:55:31 +00:00
|
|
|
// GetAdminsPaginate returns a page from the the GET /admin/admins request to the CA.
|
2021-05-03 19:48:20 +00:00
|
|
|
func (c *AdminClient) GetAdminsPaginate(opts ...AdminOption) (*adminAPI.GetAdminsResponse, error) {
|
2021-05-18 23:50:54 +00:00
|
|
|
var retried bool
|
|
|
|
o := new(adminOptions)
|
|
|
|
if err := o.apply(opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{
|
2021-05-20 20:01:58 +00:00
|
|
|
Path: "/admin/admins",
|
2021-05-18 23:50:54 +00:00
|
|
|
RawQuery: o.rawQuery(),
|
|
|
|
})
|
2021-05-03 19:48:20 +00:00
|
|
|
tok, err := c.generateAdminToken(u.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error generating admin token")
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "create GET %s request failed", u)
|
|
|
|
}
|
|
|
|
req.Header.Add("Authorization", tok)
|
2021-05-18 23:50:54 +00:00
|
|
|
retry:
|
2021-05-03 19:48:20 +00:00
|
|
|
resp, err := c.client.Do(req)
|
2021-05-18 23:50:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client GET %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
if !retried && c.retryOnError(resp) {
|
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2021-05-20 23:02:20 +00:00
|
|
|
return nil, readAdminError(resp.Body)
|
2021-05-18 23:50:54 +00:00
|
|
|
}
|
2021-05-03 19:48:20 +00:00
|
|
|
var body = new(adminAPI.GetAdminsResponse)
|
2021-05-18 23:50:54 +00:00
|
|
|
if err := readJSON(resp.Body, body); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 21:55:31 +00:00
|
|
|
// GetAdmins returns all admins from the GET /admin/admins request to the CA.
|
|
|
|
func (c *AdminClient) GetAdmins(opts ...AdminOption) ([]*linkedca.Admin, error) {
|
|
|
|
var (
|
|
|
|
cursor = ""
|
|
|
|
admins = []*linkedca.Admin{}
|
|
|
|
)
|
|
|
|
for {
|
|
|
|
resp, err := c.GetAdminsPaginate(WithAdminCursor(cursor), WithAdminLimit(100))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
admins = append(admins, resp.Admins...)
|
|
|
|
if resp.NextCursor == "" {
|
|
|
|
return admins, nil
|
|
|
|
}
|
|
|
|
cursor = resp.NextCursor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 23:02:20 +00:00
|
|
|
// CreateAdmin performs the POST /admin/admins request to the CA.
|
2021-05-03 19:48:20 +00:00
|
|
|
func (c *AdminClient) CreateAdmin(createAdminRequest *adminAPI.CreateAdminRequest) (*linkedca.Admin, error) {
|
2021-05-12 07:03:40 +00:00
|
|
|
var retried bool
|
2021-05-03 19:48:20 +00:00
|
|
|
body, err := json.Marshal(createAdminRequest)
|
2021-05-12 07:03:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "error marshaling request")
|
|
|
|
}
|
2021-05-20 23:02:20 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: "/admin/admins"})
|
2021-05-03 19:48:20 +00:00
|
|
|
tok, err := c.generateAdminToken(u.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error generating admin token")
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", u.String(), bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "create GET %s request failed", u)
|
|
|
|
}
|
|
|
|
req.Header.Add("Authorization", tok)
|
2021-05-12 07:03:40 +00:00
|
|
|
retry:
|
2021-05-03 19:48:20 +00:00
|
|
|
resp, err := c.client.Do(req)
|
2021-05-12 07:03:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client POST %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
if !retried && c.retryOnError(resp) {
|
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2021-05-20 23:02:20 +00:00
|
|
|
return nil, readAdminError(resp.Body)
|
2021-05-12 07:03:40 +00:00
|
|
|
}
|
2021-05-26 04:13:01 +00:00
|
|
|
var adm = new(linkedca.Admin)
|
2021-07-07 00:14:13 +00:00
|
|
|
if err := readProtoJSON(resp.Body, adm); err != nil {
|
2021-05-12 07:03:40 +00:00
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return adm, nil
|
|
|
|
}
|
|
|
|
|
2021-05-20 23:02:20 +00:00
|
|
|
// RemoveAdmin performs the DELETE /admin/admins/{id} request to the CA.
|
|
|
|
func (c *AdminClient) RemoveAdmin(id string) error {
|
2021-05-12 07:03:40 +00:00
|
|
|
var retried bool
|
2021-05-26 04:13:01 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: path.Join(adminURLPrefix, "admins", id)})
|
2021-05-03 19:48:20 +00:00
|
|
|
tok, err := c.generateAdminToken(u.Path)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error generating admin token")
|
|
|
|
}
|
2021-05-12 07:03:40 +00:00
|
|
|
req, err := http.NewRequest("DELETE", u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "create DELETE %s request failed", u)
|
|
|
|
}
|
2021-05-03 19:48:20 +00:00
|
|
|
req.Header.Add("Authorization", tok)
|
2021-05-12 07:03:40 +00:00
|
|
|
retry:
|
|
|
|
resp, err := c.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "client DELETE %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
if !retried && c.retryOnError(resp) {
|
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2021-05-20 23:02:20 +00:00
|
|
|
return readAdminError(resp.Body)
|
2021-05-12 07:03:40 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-20 23:02:20 +00:00
|
|
|
// UpdateAdmin performs the PUT /admin/admins/{id} request to the CA.
|
2021-05-03 19:48:20 +00:00
|
|
|
func (c *AdminClient) UpdateAdmin(id string, uar *adminAPI.UpdateAdminRequest) (*linkedca.Admin, error) {
|
2021-05-12 07:03:40 +00:00
|
|
|
var retried bool
|
|
|
|
body, err := json.Marshal(uar)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "error marshaling request")
|
|
|
|
}
|
2021-05-26 04:13:01 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: path.Join(adminURLPrefix, "admins", id)})
|
2021-05-03 19:48:20 +00:00
|
|
|
tok, err := c.generateAdminToken(u.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error generating admin token")
|
|
|
|
}
|
2021-05-18 04:07:25 +00:00
|
|
|
req, err := http.NewRequest("PATCH", u.String(), bytes.NewReader(body))
|
2021-05-12 07:03:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "create PUT %s request failed", u)
|
|
|
|
}
|
2021-05-03 19:48:20 +00:00
|
|
|
req.Header.Add("Authorization", tok)
|
2021-05-12 07:03:40 +00:00
|
|
|
retry:
|
|
|
|
resp, err := c.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client PUT %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
if !retried && c.retryOnError(resp) {
|
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2021-05-20 23:02:20 +00:00
|
|
|
return nil, readAdminError(resp.Body)
|
2021-05-12 07:03:40 +00:00
|
|
|
}
|
2021-05-26 04:13:01 +00:00
|
|
|
var adm = new(linkedca.Admin)
|
2021-07-07 00:14:13 +00:00
|
|
|
if err := readProtoJSON(resp.Body, adm); err != nil {
|
2021-05-12 07:03:40 +00:00
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return adm, nil
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:48:20 +00:00
|
|
|
// GetProvisioner performs the GET /admin/provisioners/{name} request to the CA.
|
|
|
|
func (c *AdminClient) GetProvisioner(opts ...ProvisionerOption) (*linkedca.Provisioner, error) {
|
2021-05-18 04:07:25 +00:00
|
|
|
var retried bool
|
2021-05-03 19:48:20 +00:00
|
|
|
o := new(provisionerOptions)
|
|
|
|
if err := o.apply(opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var u *url.URL
|
2021-10-08 18:59:57 +00:00
|
|
|
switch {
|
|
|
|
case len(o.id) > 0:
|
2021-05-03 19:48:20 +00:00
|
|
|
u = c.endpoint.ResolveReference(&url.URL{
|
|
|
|
Path: "/admin/provisioners/id",
|
|
|
|
RawQuery: o.rawQuery(),
|
|
|
|
})
|
2021-10-08 18:59:57 +00:00
|
|
|
case len(o.name) > 0:
|
2021-05-03 19:48:20 +00:00
|
|
|
u = c.endpoint.ResolveReference(&url.URL{Path: path.Join(adminURLPrefix, "provisioners", o.name)})
|
2021-10-08 18:59:57 +00:00
|
|
|
default:
|
2021-05-03 19:48:20 +00:00
|
|
|
return nil, errors.New("must set either name or id in method options")
|
|
|
|
}
|
|
|
|
tok, err := c.generateAdminToken(u.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error generating admin token")
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "create PUT %s request failed", u)
|
|
|
|
}
|
|
|
|
req.Header.Add("Authorization", tok)
|
2021-05-18 04:07:25 +00:00
|
|
|
retry:
|
2021-05-03 19:48:20 +00:00
|
|
|
resp, err := c.client.Do(req)
|
2021-05-18 04:07:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client GET %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
if !retried && c.retryOnError(resp) {
|
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2021-05-20 23:02:20 +00:00
|
|
|
return nil, readAdminError(resp.Body)
|
2021-05-18 04:07:25 +00:00
|
|
|
}
|
2021-05-26 04:13:01 +00:00
|
|
|
var prov = new(linkedca.Provisioner)
|
2021-05-03 19:48:20 +00:00
|
|
|
if err := readProtoJSON(resp.Body, prov); err != nil {
|
2021-05-18 04:07:25 +00:00
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return prov, nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 21:55:31 +00:00
|
|
|
// GetProvisionersPaginate performs the GET /admin/provisioners request to the CA.
|
2021-05-03 19:48:20 +00:00
|
|
|
func (c *AdminClient) GetProvisionersPaginate(opts ...ProvisionerOption) (*adminAPI.GetProvisionersResponse, error) {
|
2021-05-11 22:25:37 +00:00
|
|
|
var retried bool
|
2021-05-26 21:55:31 +00:00
|
|
|
o := new(provisionerOptions)
|
|
|
|
if err := o.apply(opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{
|
|
|
|
Path: "/admin/provisioners",
|
|
|
|
RawQuery: o.rawQuery(),
|
|
|
|
})
|
2021-05-03 19:48:20 +00:00
|
|
|
tok, err := c.generateAdminToken(u.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error generating admin token")
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "create PUT %s request failed", u)
|
|
|
|
}
|
|
|
|
req.Header.Add("Authorization", tok)
|
2021-05-11 22:25:37 +00:00
|
|
|
retry:
|
2021-05-03 19:48:20 +00:00
|
|
|
resp, err := c.client.Do(req)
|
2021-05-11 22:25:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client GET %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
if !retried && c.retryOnError(resp) {
|
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2021-05-20 23:02:20 +00:00
|
|
|
return nil, readAdminError(resp.Body)
|
2021-05-11 22:25:37 +00:00
|
|
|
}
|
2021-05-03 19:48:20 +00:00
|
|
|
var body = new(adminAPI.GetProvisionersResponse)
|
2021-05-26 21:55:31 +00:00
|
|
|
if err := readJSON(resp.Body, body); err != nil {
|
2021-05-11 22:25:37 +00:00
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
2021-05-26 21:55:31 +00:00
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProvisioners returns all admins from the GET /admin/admins request to the CA.
|
|
|
|
func (c *AdminClient) GetProvisioners(opts ...AdminOption) (provisioner.List, error) {
|
|
|
|
var (
|
|
|
|
cursor = ""
|
|
|
|
provs = provisioner.List{}
|
|
|
|
)
|
|
|
|
for {
|
|
|
|
resp, err := c.GetProvisionersPaginate(WithProvisionerCursor(cursor), WithProvisionerLimit(100))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
provs = append(provs, resp.Provisioners...)
|
|
|
|
if resp.NextCursor == "" {
|
|
|
|
return provs, nil
|
|
|
|
}
|
|
|
|
cursor = resp.NextCursor
|
|
|
|
}
|
2021-05-11 22:25:37 +00:00
|
|
|
}
|
2021-05-18 04:07:25 +00:00
|
|
|
|
2021-05-20 23:02:20 +00:00
|
|
|
// RemoveProvisioner performs the DELETE /admin/provisioners/{name} request to the CA.
|
2021-05-03 19:48:20 +00:00
|
|
|
func (c *AdminClient) RemoveProvisioner(opts ...ProvisionerOption) error {
|
|
|
|
var (
|
|
|
|
u *url.URL
|
|
|
|
retried bool
|
|
|
|
)
|
|
|
|
|
|
|
|
o := new(provisionerOptions)
|
|
|
|
if err := o.apply(opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
switch {
|
|
|
|
case len(o.id) > 0:
|
2021-05-03 19:48:20 +00:00
|
|
|
u = c.endpoint.ResolveReference(&url.URL{
|
|
|
|
Path: path.Join(adminURLPrefix, "provisioners/id"),
|
|
|
|
RawQuery: o.rawQuery(),
|
|
|
|
})
|
2021-10-08 18:59:57 +00:00
|
|
|
case len(o.name) > 0:
|
2021-05-03 19:48:20 +00:00
|
|
|
u = c.endpoint.ResolveReference(&url.URL{Path: path.Join(adminURLPrefix, "provisioners", o.name)})
|
2021-10-08 18:59:57 +00:00
|
|
|
default:
|
2021-05-03 19:48:20 +00:00
|
|
|
return errors.New("must set either name or id in method options")
|
|
|
|
}
|
|
|
|
tok, err := c.generateAdminToken(u.Path)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error generating admin token")
|
|
|
|
}
|
2021-05-18 04:07:25 +00:00
|
|
|
req, err := http.NewRequest("DELETE", u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "create DELETE %s request failed", u)
|
|
|
|
}
|
2021-05-03 19:48:20 +00:00
|
|
|
req.Header.Add("Authorization", tok)
|
2021-05-18 04:07:25 +00:00
|
|
|
retry:
|
|
|
|
resp, err := c.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "client DELETE %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
if !retried && c.retryOnError(resp) {
|
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2021-05-20 23:02:20 +00:00
|
|
|
return readAdminError(resp.Body)
|
2021-05-18 04:07:25 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-20 23:02:20 +00:00
|
|
|
// CreateProvisioner performs the POST /admin/provisioners request to the CA.
|
2021-05-26 04:13:01 +00:00
|
|
|
func (c *AdminClient) CreateProvisioner(prov *linkedca.Provisioner) (*linkedca.Provisioner, error) {
|
2021-05-18 04:07:25 +00:00
|
|
|
var retried bool
|
2021-05-03 19:48:20 +00:00
|
|
|
body, err := protojson.Marshal(prov)
|
2021-05-18 04:07:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "error marshaling request")
|
|
|
|
}
|
2021-05-03 19:48:20 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: path.Join(adminURLPrefix, "provisioners")})
|
|
|
|
tok, err := c.generateAdminToken(u.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error generating admin token")
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", u.String(), bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "create POST %s request failed", u)
|
|
|
|
}
|
|
|
|
req.Header.Add("Authorization", tok)
|
2021-05-18 04:07:25 +00:00
|
|
|
retry:
|
2021-05-03 19:48:20 +00:00
|
|
|
resp, err := c.client.Do(req)
|
2021-05-18 04:07:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client POST %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
if !retried && c.retryOnError(resp) {
|
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2021-05-20 23:02:20 +00:00
|
|
|
return nil, readAdminError(resp.Body)
|
2021-05-18 04:07:25 +00:00
|
|
|
}
|
2021-05-26 04:13:01 +00:00
|
|
|
var nuProv = new(linkedca.Provisioner)
|
2021-05-03 19:48:20 +00:00
|
|
|
if err := readProtoJSON(resp.Body, nuProv); err != nil {
|
2021-05-18 04:07:25 +00:00
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
2021-05-21 20:31:41 +00:00
|
|
|
return nuProv, nil
|
2021-05-18 04:07:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 19:48:20 +00:00
|
|
|
// UpdateProvisioner performs the PUT /admin/provisioners/{name} request to the CA.
|
|
|
|
func (c *AdminClient) UpdateProvisioner(name string, prov *linkedca.Provisioner) error {
|
2021-05-18 04:07:25 +00:00
|
|
|
var retried bool
|
2021-05-03 19:48:20 +00:00
|
|
|
body, err := protojson.Marshal(prov)
|
2021-05-18 04:07:25 +00:00
|
|
|
if err != nil {
|
2021-05-03 19:48:20 +00:00
|
|
|
return errs.Wrap(http.StatusInternalServerError, err, "error marshaling request")
|
|
|
|
}
|
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: path.Join(adminURLPrefix, "provisioners", name)})
|
|
|
|
tok, err := c.generateAdminToken(u.Path)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error generating admin token")
|
2021-05-18 04:07:25 +00:00
|
|
|
}
|
|
|
|
req, err := http.NewRequest("PUT", u.String(), bytes.NewReader(body))
|
|
|
|
if err != nil {
|
2021-05-03 19:48:20 +00:00
|
|
|
return errors.Wrapf(err, "create PUT %s request failed", u)
|
2021-05-18 04:07:25 +00:00
|
|
|
}
|
2021-05-03 19:48:20 +00:00
|
|
|
req.Header.Add("Authorization", tok)
|
2021-05-18 04:07:25 +00:00
|
|
|
retry:
|
|
|
|
resp, err := c.client.Do(req)
|
|
|
|
if err != nil {
|
2021-05-03 19:48:20 +00:00
|
|
|
return errors.Wrapf(err, "client PUT %s failed", u)
|
2021-05-18 04:07:25 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
if !retried && c.retryOnError(resp) {
|
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
2021-05-03 19:48:20 +00:00
|
|
|
return readAdminError(resp.Body)
|
2021-05-18 04:07:25 +00:00
|
|
|
}
|
2021-05-03 19:48:20 +00:00
|
|
|
return nil
|
2021-05-18 04:07:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 13:16:11 +00:00
|
|
|
// GetExternalAccountKeysPaginate returns a page from the the GET /admin/acme/eab request to the CA.
|
2021-10-11 21:34:23 +00:00
|
|
|
func (c *AdminClient) GetExternalAccountKeysPaginate(provisionerName, reference string, opts ...AdminOption) (*adminAPI.GetExternalAccountKeysResponse, error) {
|
2021-07-23 13:16:11 +00:00
|
|
|
var retried bool
|
|
|
|
o := new(adminOptions)
|
|
|
|
if err := o.apply(opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-17 15:08:02 +00:00
|
|
|
p := path.Join(adminURLPrefix, "acme/eab", provisionerName)
|
|
|
|
if reference != "" {
|
|
|
|
p = path.Join(p, "/", reference)
|
|
|
|
}
|
2021-07-23 13:16:11 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{
|
2021-09-17 15:08:02 +00:00
|
|
|
Path: p,
|
2021-07-23 13:16:11 +00:00
|
|
|
RawQuery: o.rawQuery(),
|
|
|
|
})
|
|
|
|
tok, err := c.generateAdminToken(u.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error generating admin token")
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "create GET %s request failed", u)
|
|
|
|
}
|
|
|
|
req.Header.Add("Authorization", tok)
|
|
|
|
retry:
|
|
|
|
resp, err := c.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client GET %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
if !retried && c.retryOnError(resp) {
|
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
return nil, readAdminError(resp.Body)
|
|
|
|
}
|
2021-08-27 14:58:04 +00:00
|
|
|
var body = new(adminAPI.GetExternalAccountKeysResponse)
|
|
|
|
if err := readJSON(resp.Body, body); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
|
|
|
return body, nil
|
2021-07-23 13:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateExternalAccountKey performs the POST /admin/acme/eab request to the CA.
|
2021-10-08 12:29:44 +00:00
|
|
|
func (c *AdminClient) CreateExternalAccountKey(provisionerName string, eakRequest *adminAPI.CreateExternalAccountKeyRequest) (*linkedca.EABKey, error) {
|
2021-07-22 20:43:21 +00:00
|
|
|
var retried bool
|
|
|
|
body, err := json.Marshal(eakRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "error marshaling request")
|
|
|
|
}
|
2021-10-08 12:29:44 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: path.Join(adminURLPrefix, "acme/eab/", provisionerName)})
|
2021-07-22 20:43:21 +00:00
|
|
|
tok, err := c.generateAdminToken(u.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error generating admin token")
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", u.String(), bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "create POST %s request failed", u)
|
|
|
|
}
|
|
|
|
req.Header.Add("Authorization", tok)
|
|
|
|
retry:
|
|
|
|
resp, err := c.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "client POST %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
if !retried && c.retryOnError(resp) {
|
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
return nil, readAdminError(resp.Body)
|
|
|
|
}
|
2021-08-27 10:39:37 +00:00
|
|
|
var eabKey = new(linkedca.EABKey)
|
|
|
|
if err := readProtoJSON(resp.Body, eabKey); err != nil {
|
2021-07-22 20:43:21 +00:00
|
|
|
return nil, errors.Wrapf(err, "error reading %s", u)
|
|
|
|
}
|
2021-08-27 10:39:37 +00:00
|
|
|
return eabKey, nil
|
2021-07-22 20:43:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 15:48:09 +00:00
|
|
|
// RemoveExternalAccountKey performs the DELETE /admin/acme/eab/{prov}/{key_id} request to the CA.
|
2021-10-11 21:34:23 +00:00
|
|
|
func (c *AdminClient) RemoveExternalAccountKey(provisionerName, keyID string) error {
|
2021-08-27 12:10:00 +00:00
|
|
|
var retried bool
|
2021-09-17 15:48:09 +00:00
|
|
|
u := c.endpoint.ResolveReference(&url.URL{Path: path.Join(adminURLPrefix, "acme/eab", provisionerName, "/", keyID)})
|
2021-08-27 12:10:00 +00:00
|
|
|
tok, err := c.generateAdminToken(u.Path)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error generating admin token")
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("DELETE", u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "create DELETE %s request failed", u)
|
|
|
|
}
|
|
|
|
req.Header.Add("Authorization", tok)
|
|
|
|
retry:
|
|
|
|
resp, err := c.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "client DELETE %s failed", u)
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
if !retried && c.retryOnError(resp) {
|
|
|
|
retried = true
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
return readAdminError(resp.Body)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-23 13:16:11 +00:00
|
|
|
// GetExternalAccountKeys returns all ACME EAB Keys from the GET /admin/acme/eab request to the CA.
|
2021-10-11 21:34:23 +00:00
|
|
|
func (c *AdminClient) GetExternalAccountKeys(provisionerName, reference string, opts ...AdminOption) ([]*linkedca.EABKey, error) {
|
2021-07-23 13:16:11 +00:00
|
|
|
var (
|
|
|
|
cursor = ""
|
2021-08-27 10:39:37 +00:00
|
|
|
eaks = []*linkedca.EABKey{}
|
2021-07-23 13:16:11 +00:00
|
|
|
)
|
|
|
|
for {
|
2021-09-17 15:08:02 +00:00
|
|
|
resp, err := c.GetExternalAccountKeysPaginate(provisionerName, reference, WithAdminCursor(cursor), WithAdminLimit(100))
|
2021-07-23 13:16:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
eaks = append(eaks, resp.EAKs...)
|
|
|
|
if resp.NextCursor == "" {
|
|
|
|
return eaks, nil
|
|
|
|
}
|
|
|
|
cursor = resp.NextCursor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 23:02:20 +00:00
|
|
|
func readAdminError(r io.ReadCloser) error {
|
2021-07-23 13:16:11 +00:00
|
|
|
// TODO: not all errors can be read (i.e. 404); seems to be a bigger issue
|
2021-05-18 04:07:25 +00:00
|
|
|
defer r.Close()
|
2021-05-03 19:48:20 +00:00
|
|
|
adminErr := new(admin.Error)
|
|
|
|
if err := json.NewDecoder(r).Decode(adminErr); err != nil {
|
2021-05-18 04:07:25 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-05-03 19:48:20 +00:00
|
|
|
return errors.New(adminErr.Message)
|
2021-05-18 04:07:25 +00:00
|
|
|
}
|