Skip to content

Commit 3acbedd

Browse files
Add a helper function to build ACME API patterns (#20180)
- Add a helper function that can accept the final API path along with the pattern function for an ACME api definition and generate the various flavors for the given API
1 parent 3e022a3 commit 3acbedd

File tree

8 files changed

+57
-191
lines changed

8 files changed

+57
-191
lines changed

builtin/logical/pki/acme_wrappers.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,27 @@ func (b *backend) acmeAccountRequiredWrapper(op acmeAccountRequiredOperation) fr
153153
})
154154
}
155155

156+
// A helper function that will build up the various path patterns we want for ACME APIs.
157+
func buildAcmeFrameworkPaths(b *backend, patternFunc func(b *backend, pattern string) *framework.Path, acmeApi string) []*framework.Path {
158+
var patterns []*framework.Path
159+
for _, baseUrl := range []string{
160+
"acme",
161+
"roles/" + framework.GenericNameRegex("role") + "/acme",
162+
"issuer/" + framework.GenericNameRegex(issuerRefParam) + "/acme",
163+
"issuer/" + framework.GenericNameRegex(issuerRefParam) + "/roles/" + framework.GenericNameRegex("role") + "/acme",
164+
} {
165+
166+
if !strings.HasPrefix(acmeApi, "/") {
167+
acmeApi = "/" + acmeApi
168+
}
169+
170+
path := patternFunc(b, baseUrl+acmeApi)
171+
patterns = append(patterns, path)
172+
}
173+
174+
return patterns
175+
}
176+
156177
func getAcmeBaseUrl(sc *storageContext, path string) (*url.URL, error) {
157178
cfg, err := sc.getClusterConfig()
158179
if err != nil {

builtin/logical/pki/backend.go

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -216,43 +216,7 @@ func Backend(conf *logical.BackendConfig) *backend {
216216
pathResignCrls(&b),
217217
pathSignRevocationList(&b),
218218

219-
// ACME APIs
220-
pathAcmeRootDirectory(&b),
221-
pathAcmeRoleDirectory(&b),
222-
pathAcmeIssuerDirectory(&b),
223-
pathAcmeIssuerAndRoleDirectory(&b),
224-
pathAcmeRootNonce(&b),
225-
pathAcmeRoleNonce(&b),
226-
pathAcmeIssuerNonce(&b),
227-
pathAcmeIssuerAndRoleNonce(&b),
228-
pathAcmeRootNewAccount(&b),
229-
pathAcmeRoleNewAccount(&b),
230-
pathAcmeIssuerNewAccount(&b),
231-
pathAcmeIssuerAndRoleNewAccount(&b),
232-
pathAcmeRootUpdateAccount(&b),
233-
pathAcmeRoleUpdateAccount(&b),
234-
pathAcmeIssuerUpdateAccount(&b),
235-
pathAcmeIssuerAndRoleUpdateAccount(&b),
236-
pathAcmeRootAuthorization(&b),
237-
pathAcmeRoleAuthorization(&b),
238-
pathAcmeIssuerAuthorization(&b),
239-
pathAcmeIssuerAndRoleAuthorization(&b),
240-
pathAcmeRootChallenge(&b),
241-
pathAcmeRoleChallenge(&b),
242-
pathAcmeIssuerChallenge(&b),
243-
pathAcmeIssuerAndRoleChallenge(&b),
244-
pathAcmeRootNewOrder(&b),
245-
pathAcmeRoleNewOrder(&b),
246-
pathAcmeIssuerNewOrder(&b),
247-
pathAcmeIssuerAndRoleNewOrder(&b),
248-
pathAcmeRootListOrders(&b),
249-
pathAcmeRoleListOrders(&b),
250-
pathAcmeIssuerListOrders(&b),
251-
pathAcmeIssuerAndRoleListOrders(&b),
252-
pathAcmeRootGetOrder(&b),
253-
pathAcmeRoleGetOrder(&b),
254-
pathAcmeIssuerGetOrder(&b),
255-
pathAcmeIssuerAndRoleGetOrder(&b),
219+
// ACME APIs see below
256220
},
257221

258222
Secrets: []*framework.Secret{
@@ -265,6 +229,22 @@ func Backend(conf *logical.BackendConfig) *backend {
265229
PeriodicFunc: b.periodicFunc,
266230
}
267231

232+
// Add ACME paths to backend
233+
var acmePaths []*framework.Path
234+
acmePaths = append(acmePaths, pathAcmeDirectory(&b)...)
235+
acmePaths = append(acmePaths, pathAcmeNonce(&b)...)
236+
acmePaths = append(acmePaths, pathAcmeNewAccount(&b)...)
237+
acmePaths = append(acmePaths, pathAcmeUpdateAccount(&b)...)
238+
acmePaths = append(acmePaths, pathAcmeGetOrder(&b)...)
239+
acmePaths = append(acmePaths, pathAcmeListOrders(&b)...)
240+
acmePaths = append(acmePaths, pathAcmeNewOrder(&b)...)
241+
acmePaths = append(acmePaths, pathAcmeChallenge(&b)...)
242+
acmePaths = append(acmePaths, pathAcmeAuthorization(&b)...)
243+
244+
for _, acmePath := range acmePaths {
245+
b.Backend.Paths = append(b.Backend.Paths, acmePath)
246+
}
247+
268248
// Add specific un-auth'd paths for ACME APIs
269249
for _, acmePrefix := range []string{"", "issuer/+/", "roles/+/", "issuer/+/roles/+/"} {
270250
b.PathsSpecial.Unauthenticated = append(b.PathsSpecial.Unauthenticated, acmePrefix+"acme/directory")

builtin/logical/pki/path_acme_authorizations.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,8 @@ import (
1010
"github.com/hashicorp/vault/sdk/logical"
1111
)
1212

13-
func pathAcmeRootAuthorization(b *backend) *framework.Path {
14-
return patternAcmeAuthorization(b, "acme/authorization/"+framework.MatchAllRegex("auth_id"))
15-
}
16-
17-
func pathAcmeRoleAuthorization(b *backend) *framework.Path {
18-
return patternAcmeAuthorization(b, "roles/"+framework.GenericNameRegex("role")+"/acme/authorization/"+framework.MatchAllRegex("auth_id"))
19-
}
20-
21-
func pathAcmeIssuerAuthorization(b *backend) *framework.Path {
22-
return patternAcmeAuthorization(b, "issuer/"+framework.GenericNameRegex(issuerRefParam)+"/acme/authorization/"+framework.MatchAllRegex("auth_id"))
23-
}
24-
25-
func pathAcmeIssuerAndRoleAuthorization(b *backend) *framework.Path {
26-
return patternAcmeAuthorization(b,
27-
"issuer/"+framework.GenericNameRegex(issuerRefParam)+
28-
"/roles/"+framework.GenericNameRegex("role")+"/acme/authorization/"+framework.MatchAllRegex("auth_id"))
13+
func pathAcmeAuthorization(b *backend) []*framework.Path {
14+
return buildAcmeFrameworkPaths(b, patternAcmeAuthorization, "/authorization/"+framework.MatchAllRegex("auth_id"))
2915
}
3016

3117
func addFieldsForACMEAuthorization(fields map[string]*framework.FieldSchema) map[string]*framework.FieldSchema {

builtin/logical/pki/path_acme_challenges.go

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,9 @@ import (
1010
"github.com/hashicorp/vault/sdk/logical"
1111
)
1212

13-
func pathAcmeRootChallenge(b *backend) *framework.Path {
14-
return patternAcmeChallenge(b,
15-
"acme/challenge/"+framework.MatchAllRegex("auth_id")+"/"+
16-
framework.MatchAllRegex("challenge_type"))
17-
}
18-
19-
func pathAcmeRoleChallenge(b *backend) *framework.Path {
20-
return patternAcmeChallenge(b,
21-
"roles/"+framework.GenericNameRegex("role")+"/acme/challenge/"+
22-
framework.MatchAllRegex("auth_id")+"/"+
23-
framework.MatchAllRegex("challenge_type"))
24-
}
25-
26-
func pathAcmeIssuerChallenge(b *backend) *framework.Path {
27-
return patternAcmeChallenge(b,
28-
"issuer/"+framework.GenericNameRegex(issuerRefParam)+"/acme/challenge/"+
29-
framework.MatchAllRegex("auth_id")+"/"+
30-
framework.MatchAllRegex("challenge_type"))
31-
}
32-
33-
func pathAcmeIssuerAndRoleChallenge(b *backend) *framework.Path {
34-
return patternAcmeChallenge(b,
35-
"issuer/"+framework.GenericNameRegex(issuerRefParam)+
36-
"/roles/"+framework.GenericNameRegex("role")+"/acme/challenge/"+
37-
framework.MatchAllRegex("auth_id")+"/"+
38-
framework.MatchAllRegex("challenge_type"))
13+
func pathAcmeChallenge(b *backend) []*framework.Path {
14+
return buildAcmeFrameworkPaths(b, patternAcmeChallenge,
15+
"/challenge/"+framework.MatchAllRegex("auth_id")+"/"+framework.MatchAllRegex("challenge_type"))
3916
}
4017

4118
func addFieldsForACMEChallenge(fields map[string]*framework.FieldSchema) map[string]*framework.FieldSchema {

builtin/logical/pki/path_acme_directory.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,8 @@ const (
1717
pathAcmeDirectoryHelpDesc = `Provide an ACME directory response that contains URLS for various ACME operations.`
1818
)
1919

20-
func pathAcmeRootDirectory(b *backend) *framework.Path {
21-
return patternAcmeDirectory(b, "acme/directory")
22-
}
23-
24-
func pathAcmeRoleDirectory(b *backend) *framework.Path {
25-
return patternAcmeDirectory(b, "roles/"+framework.GenericNameRegex("role")+"/acme/directory")
26-
}
27-
28-
func pathAcmeIssuerDirectory(b *backend) *framework.Path {
29-
return patternAcmeDirectory(b, "issuer/"+framework.GenericNameRegex(issuerRefParam)+"/acme/directory")
30-
}
31-
32-
func pathAcmeIssuerAndRoleDirectory(b *backend) *framework.Path {
33-
return patternAcmeDirectory(b,
34-
"issuer/"+framework.GenericNameRegex(issuerRefParam)+
35-
"/roles/"+framework.GenericNameRegex("role")+"/acme/directory")
20+
func pathAcmeDirectory(b *backend) []*framework.Path {
21+
return buildAcmeFrameworkPaths(b, patternAcmeDirectory, "/directory")
3622
}
3723

3824
func patternAcmeDirectory(b *backend, pattern string) *framework.Path {

builtin/logical/pki/path_acme_new_account.go

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,12 @@ func uuidNameRegex(name string) string {
1818
return fmt.Sprintf("(?P<%s>[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}?)", name)
1919
}
2020

21-
func pathAcmeRootNewAccount(b *backend) *framework.Path {
22-
return patternAcmeNewAccount(b, "acme/new-account")
21+
func pathAcmeNewAccount(b *backend) []*framework.Path {
22+
return buildAcmeFrameworkPaths(b, patternAcmeNewAccount, "/new-account")
2323
}
2424

25-
func pathAcmeRoleNewAccount(b *backend) *framework.Path {
26-
return patternAcmeNewAccount(b, "roles/"+framework.GenericNameRegex("role")+"/acme/new-account")
27-
}
28-
29-
func pathAcmeIssuerNewAccount(b *backend) *framework.Path {
30-
return patternAcmeNewAccount(b, "issuer/"+framework.GenericNameRegex(issuerRefParam)+"/acme/new-account")
31-
}
32-
33-
func pathAcmeIssuerAndRoleNewAccount(b *backend) *framework.Path {
34-
return patternAcmeNewAccount(b,
35-
"issuer/"+framework.GenericNameRegex(issuerRefParam)+
36-
"/roles/"+framework.GenericNameRegex("role")+"/acme/new-account")
37-
}
38-
39-
func pathAcmeRootUpdateAccount(b *backend) *framework.Path {
40-
return patternAcmeNewAccount(b, "acme/account/"+uuidNameRegex("kid"))
41-
}
42-
43-
func pathAcmeRoleUpdateAccount(b *backend) *framework.Path {
44-
return patternAcmeNewAccount(b, "roles/"+framework.GenericNameRegex("role")+"/acme/account/"+uuidNameRegex("kid"))
45-
}
46-
47-
func pathAcmeIssuerUpdateAccount(b *backend) *framework.Path {
48-
return patternAcmeNewAccount(b, "issuer/"+framework.GenericNameRegex(issuerRefParam)+"/acme/account/"+uuidNameRegex("kid"))
49-
}
50-
51-
func pathAcmeIssuerAndRoleUpdateAccount(b *backend) *framework.Path {
52-
return patternAcmeNewAccount(b,
53-
"issuer/"+framework.GenericNameRegex(issuerRefParam)+
54-
"/roles/"+framework.GenericNameRegex("role")+"/acme/account/"+uuidNameRegex("kid"))
25+
func pathAcmeUpdateAccount(b *backend) []*framework.Path {
26+
return buildAcmeFrameworkPaths(b, patternAcmeNewAccount, "/account/"+uuidNameRegex("kid"))
5527
}
5628

5729
func addFieldsForACMEPath(fields map[string]*framework.FieldSchema, pattern string) map[string]*framework.FieldSchema {

builtin/logical/pki/path_acme_nonce.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,8 @@ import (
1111
"github.com/hashicorp/vault/sdk/logical"
1212
)
1313

14-
func pathAcmeRootNonce(b *backend) *framework.Path {
15-
return patternAcmeNonce(b, "acme/new-nonce")
16-
}
17-
18-
func pathAcmeRoleNonce(b *backend) *framework.Path {
19-
return patternAcmeNonce(b, "roles/"+framework.GenericNameRegex("role")+"/acme/new-nonce")
20-
}
21-
22-
func pathAcmeIssuerNonce(b *backend) *framework.Path {
23-
return patternAcmeNonce(b, "issuer/"+framework.GenericNameRegex(issuerRefParam)+"/acme/new-nonce")
24-
}
25-
26-
func pathAcmeIssuerAndRoleNonce(b *backend) *framework.Path {
27-
return patternAcmeNonce(b,
28-
"issuer/"+framework.GenericNameRegex(issuerRefParam)+
29-
"/roles/"+framework.GenericNameRegex("role")+"/acme/new-nonce")
14+
func pathAcmeNonce(b *backend) []*framework.Path {
15+
return buildAcmeFrameworkPaths(b, patternAcmeNonce, "/new-nonce")
3016
}
3117

3218
func patternAcmeNonce(b *backend, pattern string) *framework.Path {

builtin/logical/pki/path_acme_order.go

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,16 @@ import (
1414
"golang.org/x/net/idna"
1515
)
1616

17-
func pathAcmeRootListOrders(b *backend) *framework.Path {
18-
return patternAcmeListOrders(b, "acme/orders")
17+
func pathAcmeListOrders(b *backend) []*framework.Path {
18+
return buildAcmeFrameworkPaths(b, patternAcmeListOrders, "/orders")
1919
}
2020

21-
func pathAcmeRoleListOrders(b *backend) *framework.Path {
22-
return patternAcmeListOrders(b, "roles/"+framework.GenericNameRegex("role")+"/acme/orders")
21+
func pathAcmeGetOrder(b *backend) []*framework.Path {
22+
return buildAcmeFrameworkPaths(b, patternAcmeGetOrder, "/order/"+uuidNameRegex("order_id"))
2323
}
2424

25-
func pathAcmeIssuerListOrders(b *backend) *framework.Path {
26-
return patternAcmeListOrders(b, "issuer/"+framework.GenericNameRegex(issuerRefParam)+"/acme/orders")
27-
}
28-
29-
func pathAcmeIssuerAndRoleListOrders(b *backend) *framework.Path {
30-
return patternAcmeListOrders(b,
31-
"issuer/"+framework.GenericNameRegex(issuerRefParam)+
32-
"/roles/"+framework.GenericNameRegex("role")+"/acme/orders")
33-
}
34-
35-
func pathAcmeRootGetOrder(b *backend) *framework.Path {
36-
return patternAcmeGetOrder(b, "acme/order/"+uuidNameRegex("order_id"))
37-
}
38-
39-
func pathAcmeRoleGetOrder(b *backend) *framework.Path {
40-
return patternAcmeGetOrder(b, "roles/"+framework.GenericNameRegex("role")+"/acme/order/"+uuidNameRegex("order_id"))
41-
}
42-
43-
func pathAcmeIssuerGetOrder(b *backend) *framework.Path {
44-
return patternAcmeGetOrder(b, "issuer/"+framework.GenericNameRegex(issuerRefParam)+"/acme/order/"+uuidNameRegex("order_id"))
45-
}
46-
47-
func pathAcmeIssuerAndRoleGetOrder(b *backend) *framework.Path {
48-
return patternAcmeGetOrder(b,
49-
"issuer/"+framework.GenericNameRegex(issuerRefParam)+
50-
"/roles/"+framework.GenericNameRegex("role")+"/acme/order/"+uuidNameRegex("order_id"))
51-
}
52-
53-
func pathAcmeRootNewOrder(b *backend) *framework.Path {
54-
return patternAcmeNewOrder(b, "acme/new-order")
55-
}
56-
57-
func pathAcmeRoleNewOrder(b *backend) *framework.Path {
58-
return patternAcmeNewOrder(b, "roles/"+framework.GenericNameRegex("role")+"/acme/new-order")
59-
}
60-
61-
func pathAcmeIssuerNewOrder(b *backend) *framework.Path {
62-
return patternAcmeNewOrder(b, "issuer/"+framework.GenericNameRegex(issuerRefParam)+"/acme/new-order")
63-
}
64-
65-
func pathAcmeIssuerAndRoleNewOrder(b *backend) *framework.Path {
66-
return patternAcmeNewOrder(b,
67-
"issuer/"+framework.GenericNameRegex(issuerRefParam)+
68-
"/roles/"+framework.GenericNameRegex("role")+"/acme/new-order")
25+
func pathAcmeNewOrder(b *backend) []*framework.Path {
26+
return buildAcmeFrameworkPaths(b, patternAcmeNewOrder, "/new-order")
6927
}
7028

7129
func patternAcmeNewOrder(b *backend, pattern string) *framework.Path {

0 commit comments

Comments
 (0)