@@ -2,6 +2,8 @@ package api
2
2
3
3
import (
4
4
"context"
5
+ "errors"
6
+ "fmt"
5
7
"net/http"
6
8
7
9
"github.com/go-chi/chi"
@@ -20,6 +22,9 @@ type CreateExternalAccountKeyRequest struct {
20
22
21
23
// Validate validates a new ACME EAB Key request body.
22
24
func (r * CreateExternalAccountKeyRequest ) Validate () error {
25
+ if len (r .Reference ) > 256 { // an arbitrary, but sensible (IMO), limit
26
+ return fmt .Errorf ("reference length %d exceeds the maximum (256)" , len (r .Reference ))
27
+ }
23
28
return nil
24
29
}
25
30
@@ -85,7 +90,7 @@ func (h *Handler) CreateExternalAccountKey(w http.ResponseWriter, r *http.Reques
85
90
}
86
91
87
92
if err := body .Validate (); err != nil {
88
- api .WriteError (w , err )
93
+ api .WriteError (w , admin . WrapError ( admin . ErrorBadRequestType , err , "error validating request body" ) )
89
94
return
90
95
}
91
96
@@ -97,9 +102,9 @@ func (h *Handler) CreateExternalAccountKey(w http.ResponseWriter, r *http.Reques
97
102
k , err := h .acmeDB .GetExternalAccountKeyByReference (r .Context (), prov , reference )
98
103
// retrieving an EAB key from DB results in an error if it doesn't exist, which is what we're looking for,
99
104
// but other errors can also happen. Return early if that happens; continuing if it was acme.ErrNotFound.
100
- shouldWriteError := err != nil && acme .ErrNotFound != err
105
+ shouldWriteError := err != nil && ! errors . Is ( err , acme .ErrNotFound )
101
106
if shouldWriteError {
102
- api .WriteError (w , err )
107
+ api .WriteError (w , admin . WrapErrorISE ( err , "could not lookup external account key by reference" ) )
103
108
return
104
109
}
105
110
// if a key was found, return HTTP 409 conflict
@@ -114,7 +119,11 @@ func (h *Handler) CreateExternalAccountKey(w http.ResponseWriter, r *http.Reques
114
119
115
120
eak , err := h .acmeDB .CreateExternalAccountKey (r .Context (), prov , reference )
116
121
if err != nil {
117
- api .WriteError (w , admin .WrapErrorISE (err , "error creating ACME EAB key for provisioner %s and reference %s" , prov , reference ))
122
+ msg := fmt .Sprintf ("error creating ACME EAB key for provisioner '%s'" , prov )
123
+ if reference != "" {
124
+ msg += fmt .Sprintf (" and reference '%s'" , reference )
125
+ }
126
+ api .WriteError (w , admin .WrapErrorISE (err , msg ))
118
127
return
119
128
}
120
129
@@ -134,7 +143,7 @@ func (h *Handler) DeleteExternalAccountKey(w http.ResponseWriter, r *http.Reques
134
143
keyID := chi .URLParam (r , "id" )
135
144
136
145
if err := h .acmeDB .DeleteExternalAccountKey (r .Context (), prov , keyID ); err != nil {
137
- api .WriteError (w , admin .WrapErrorISE (err , "error deleting ACME EAB Key %s " , keyID ))
146
+ api .WriteError (w , admin .WrapErrorISE (err , "error deleting ACME EAB Key '%s' " , keyID ))
138
147
return
139
148
}
140
149
@@ -165,14 +174,16 @@ func (h *Handler) GetExternalAccountKeys(w http.ResponseWriter, r *http.Request)
165
174
if reference != "" {
166
175
key , err = h .acmeDB .GetExternalAccountKeyByReference (r .Context (), prov , reference )
167
176
if err != nil {
168
- api .WriteError (w , admin .WrapErrorISE (err , "error getting external account key with reference %s " , reference ))
177
+ api .WriteError (w , admin .WrapErrorISE (err , "error retrieving external account key with reference '%s' " , reference ))
169
178
return
170
179
}
171
- keys = []* acme.ExternalAccountKey {key }
180
+ if key != nil {
181
+ keys = []* acme.ExternalAccountKey {key }
182
+ }
172
183
} else {
173
184
keys , nextCursor , err = h .acmeDB .GetExternalAccountKeys (r .Context (), prov , cursor , limit )
174
185
if err != nil {
175
- api .WriteError (w , admin .WrapErrorISE (err , "error getting external account keys" ))
186
+ api .WriteError (w , admin .WrapErrorISE (err , "error retrieving external account keys" ))
176
187
return
177
188
}
178
189
}
0 commit comments