Skip to content

Commit 1dba869

Browse files
committed
Use LinkedCA.EABKey type in ACME EAB API
1 parent f31ca4f commit 1dba869

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

acme/api/account.go

-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ func (h *Handler) validateExternalAccountBinding(ctx context.Context, nar *NewAc
265265
return nil, acme.WrapErrorISE(err, "error parsing externalAccountBinding jws")
266266
}
267267

268-
// TODO: verify supported algorithms against the incoming alg (and corresponding settings)?
269268
// TODO: implement strategy pattern to allow for different ways of verification (i.e. webhook call) based on configuration
270269

271270
keyID := eabJWS.Signatures[0].Protected.KeyID

authority/admin/api/acme.go

+21-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/smallstep/certificates/api"
77
"github.com/smallstep/certificates/authority/admin"
8+
"go.step.sm/linkedca"
89
)
910

1011
// CreateExternalAccountKeyRequest is the type for POST /admin/acme/eab requests
@@ -13,44 +14,50 @@ type CreateExternalAccountKeyRequest struct {
1314
Name string `json:"name"`
1415
}
1516

16-
// CreateExternalAccountKeyResponse is the type for POST /admin/acme/eab responses
17-
type CreateExternalAccountKeyResponse struct {
18-
ProvisionerName string `json:"provisioner"`
19-
KeyID string `json:"keyID"`
20-
Name string `json:"name"`
21-
Key []byte `json:"key"`
17+
// Validate validates a new-admin request body.
18+
func (r *CreateExternalAccountKeyRequest) Validate() error {
19+
if r.ProvisionerName == "" {
20+
return admin.NewError(admin.ErrorBadRequestType, "provisioner name cannot be empty")
21+
}
22+
if r.Name == "" {
23+
return admin.NewError(admin.ErrorBadRequestType, "name / reference cannot be empty")
24+
}
25+
return nil
2226
}
2327

2428
// GetExternalAccountKeysResponse is the type for GET /admin/acme/eab responses
2529
type GetExternalAccountKeysResponse struct {
26-
EAKs []*CreateExternalAccountKeyResponse `json:"eaks"`
27-
NextCursor string `json:"nextCursor"`
30+
EAKs []*linkedca.EABKey `json:"eaks"`
31+
NextCursor string `json:"nextCursor"`
2832
}
2933

3034
// CreateExternalAccountKey creates a new External Account Binding key
3135
func (h *Handler) CreateExternalAccountKey(w http.ResponseWriter, r *http.Request) {
3236
var body CreateExternalAccountKeyRequest
3337
if err := api.ReadJSON(r.Body, &body); err != nil { // TODO: rewrite into protobuf json (likely)
34-
api.WriteError(w, err)
38+
api.WriteError(w, admin.WrapError(admin.ErrorBadRequestType, err, "error reading request body"))
3539
return
3640
}
3741

38-
// TODO: Validate input
42+
if err := body.Validate(); err != nil {
43+
api.WriteError(w, err)
44+
return
45+
}
3946

4047
eak, err := h.acmeDB.CreateExternalAccountKey(r.Context(), body.ProvisionerName, body.Name)
4148
if err != nil {
4249
api.WriteError(w, admin.WrapErrorISE(err, "error creating external account key %s", body.Name))
4350
return
4451
}
4552

46-
eakResponse := CreateExternalAccountKeyResponse{
53+
response := &linkedca.EABKey{
54+
EabKid: eak.ID,
55+
EabHmacKey: eak.KeyBytes,
4756
ProvisionerName: eak.ProvisionerName,
48-
KeyID: eak.ID,
4957
Name: eak.Name,
50-
Key: eak.KeyBytes,
5158
}
5259

53-
api.JSONStatus(w, eakResponse, http.StatusCreated) // TODO: rewrite into protobuf json (likely)
60+
api.ProtoJSONStatus(w, response, http.StatusCreated)
5461
}
5562

5663
// GetExternalAccountKeys returns a segment of ACME EAB Keys.

ca/adminClient.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"time"
1313

1414
"github.com/pkg/errors"
15-
"github.com/smallstep/certificates/api"
1615
"github.com/smallstep/certificates/authority/admin"
1716
adminAPI "github.com/smallstep/certificates/authority/admin/api"
1817
"github.com/smallstep/certificates/authority/provisioner"
@@ -600,7 +599,7 @@ retry:
600599
}
601600

602601
// CreateExternalAccountKey performs the POST /admin/acme/eab request to the CA.
603-
func (c *AdminClient) CreateExternalAccountKey(eakRequest *adminAPI.CreateExternalAccountKeyRequest) (*adminAPI.CreateExternalAccountKeyResponse, error) {
602+
func (c *AdminClient) CreateExternalAccountKey(eakRequest *adminAPI.CreateExternalAccountKeyRequest) (*linkedca.EABKey, error) {
604603
var retried bool
605604
body, err := json.Marshal(eakRequest)
606605
if err != nil {
@@ -628,18 +627,18 @@ retry:
628627
}
629628
return nil, readAdminError(resp.Body)
630629
}
631-
var eakResp = new(adminAPI.CreateExternalAccountKeyResponse)
632-
if err := api.ReadJSON(resp.Body, &eakResp); err != nil {
630+
var eabKey = new(linkedca.EABKey)
631+
if err := readProtoJSON(resp.Body, eabKey); err != nil {
633632
return nil, errors.Wrapf(err, "error reading %s", u)
634633
}
635-
return eakResp, nil
634+
return eabKey, nil
636635
}
637636

638637
// GetExternalAccountKeys returns all ACME EAB Keys from the GET /admin/acme/eab request to the CA.
639-
func (c *AdminClient) GetExternalAccountKeys(opts ...AdminOption) ([]*adminAPI.CreateExternalAccountKeyResponse, error) {
638+
func (c *AdminClient) GetExternalAccountKeys(opts ...AdminOption) ([]*linkedca.EABKey, error) {
640639
var (
641640
cursor = ""
642-
eaks = []*adminAPI.CreateExternalAccountKeyResponse{}
641+
eaks = []*linkedca.EABKey{}
643642
)
644643
for {
645644
resp, err := c.GetExternalAccountKeysPaginate(WithAdminCursor(cursor), WithAdminLimit(100))

0 commit comments

Comments
 (0)