Skip to content

Commit b65a588

Browse files
committed
Make authentication work for /admin/eak
1 parent d669f3c commit b65a588

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

authority/admin/api/eak.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
"github.com/smallstep/certificates/authority/admin"
88
)
99

10-
// CreateExternalAccountKeyRequest is the type for GET /admin/eak requests
10+
// CreateExternalAccountKeyRequest is the type for POST /admin/eak requests
1111
type CreateExternalAccountKeyRequest struct {
1212
Name string `json:"name"`
1313
}
1414

15-
// CreateExternalAccountKeyResponse is the type for GET /admin/eak responses
15+
// CreateExternalAccountKeyResponse is the type for POST /admin/eak responses
1616
type CreateExternalAccountKeyResponse struct {
1717
KeyID string `json:"keyID"`
1818
Name string `json:"name"`
@@ -21,17 +21,17 @@ type CreateExternalAccountKeyResponse struct {
2121

2222
// CreateExternalAccountKey creates a new External Account Binding key
2323
func (h *Handler) CreateExternalAccountKey(w http.ResponseWriter, r *http.Request) {
24-
var eakRequest = new(CreateExternalAccountKeyRequest)
25-
if err := api.ReadJSON(r.Body, eakRequest); err != nil { // TODO: rewrite into protobuf json (likely)
24+
var body CreateExternalAccountKeyRequest
25+
if err := api.ReadJSON(r.Body, &body); err != nil { // TODO: rewrite into protobuf json (likely)
2626
api.WriteError(w, err)
2727
return
2828
}
2929

3030
// TODO: Validate input
3131

32-
eak, err := h.db.CreateExternalAccountKey(r.Context(), eakRequest.Name)
32+
eak, err := h.db.CreateExternalAccountKey(r.Context(), body.Name)
3333
if err != nil {
34-
api.WriteError(w, admin.WrapErrorISE(err, "error creating external account key %s", eakRequest.Name))
34+
api.WriteError(w, admin.WrapErrorISE(err, "error creating external account key %s", body.Name))
3535
return
3636
}
3737

authority/admin/api/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ func (h *Handler) Route(r api.Router) {
4040
r.MethodFunc("DELETE", "/admins/{id}", authnz(h.DeleteAdmin))
4141

4242
// External Account Binding Keys
43-
r.MethodFunc("POST", "/eak", h.CreateExternalAccountKey) // TODO: authnz
43+
r.MethodFunc("POST", "/eak", authnz(h.CreateExternalAccountKey))
4444
}

ca/adminClient.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/pkg/errors"
15+
"github.com/smallstep/certificates/api"
1516
"github.com/smallstep/certificates/authority/admin"
1617
adminAPI "github.com/smallstep/certificates/authority/admin/api"
1718
"github.com/smallstep/certificates/authority/provisioner"
@@ -558,6 +559,43 @@ retry:
558559
return nil
559560
}
560561

562+
// CreateExternalAccountKey performs the POST /admin/eak request to the CA.
563+
func (c *AdminClient) CreateExternalAccountKey(eakRequest *adminAPI.CreateExternalAccountKeyRequest) (*adminAPI.CreateExternalAccountKeyResponse, error) {
564+
var retried bool
565+
//body, err := protojson.Marshal(req)
566+
body, err := json.Marshal(eakRequest)
567+
if err != nil {
568+
return nil, errs.Wrap(http.StatusInternalServerError, err, "error marshaling request")
569+
}
570+
u := c.endpoint.ResolveReference(&url.URL{Path: path.Join(adminURLPrefix, "eak")})
571+
tok, err := c.generateAdminToken(u.Path)
572+
if err != nil {
573+
return nil, errors.Wrapf(err, "error generating admin token")
574+
}
575+
req, err := http.NewRequest("POST", u.String(), bytes.NewReader(body))
576+
if err != nil {
577+
return nil, errors.Wrapf(err, "create POST %s request failed", u)
578+
}
579+
req.Header.Add("Authorization", tok)
580+
retry:
581+
resp, err := c.client.Do(req)
582+
if err != nil {
583+
return nil, errors.Wrapf(err, "client POST %s failed", u)
584+
}
585+
if resp.StatusCode >= 400 {
586+
if !retried && c.retryOnError(resp) {
587+
retried = true
588+
goto retry
589+
}
590+
return nil, readAdminError(resp.Body)
591+
}
592+
var eakResp = new(adminAPI.CreateExternalAccountKeyResponse)
593+
if err := api.ReadJSON(resp.Body, &eakResp); err != nil {
594+
return nil, errors.Wrapf(err, "error reading %s", u)
595+
}
596+
return eakResp, nil
597+
}
598+
561599
func readAdminError(r io.ReadCloser) error {
562600
defer r.Close()
563601
adminErr := new(admin.Error)

0 commit comments

Comments
 (0)