2023-03-29 16:29:19 +00:00
|
|
|
package pki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
pathAcmeDirectoryHelpSync = `Read the proper URLs for various ACME operations`
|
|
|
|
pathAcmeDirectoryHelpDesc = `Provide an ACME directory response that contains URLS for various ACME operations.`
|
|
|
|
)
|
|
|
|
|
|
|
|
func pathAcmeRootDirectory(b *backend) *framework.Path {
|
2023-03-29 19:06:09 +00:00
|
|
|
return patternAcmeDirectory(b, "acme/directory")
|
2023-03-29 16:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func pathAcmeRoleDirectory(b *backend) *framework.Path {
|
2023-03-29 19:06:09 +00:00
|
|
|
return patternAcmeDirectory(b, "roles/"+framework.GenericNameRegex("role")+"/acme/directory")
|
2023-03-29 16:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func pathAcmeIssuerDirectory(b *backend) *framework.Path {
|
2023-03-29 19:06:09 +00:00
|
|
|
return patternAcmeDirectory(b, "issuer/"+framework.GenericNameRegex(issuerRefParam)+"/acme/directory")
|
2023-03-29 16:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func pathAcmeIssuerAndRoleDirectory(b *backend) *framework.Path {
|
|
|
|
return patternAcmeDirectory(b,
|
2023-03-29 19:06:09 +00:00
|
|
|
"issuer/"+framework.GenericNameRegex(issuerRefParam)+
|
|
|
|
"/roles/"+framework.GenericNameRegex("role")+"/acme/directory")
|
2023-03-29 16:29:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 19:06:09 +00:00
|
|
|
func patternAcmeDirectory(b *backend, pattern string) *framework.Path {
|
2023-03-29 16:29:19 +00:00
|
|
|
fields := map[string]*framework.FieldSchema{}
|
2023-03-29 19:06:09 +00:00
|
|
|
addFieldsForACMEPath(fields, pattern)
|
|
|
|
|
2023-03-29 16:29:19 +00:00
|
|
|
return &framework.Path{
|
|
|
|
Pattern: pattern,
|
|
|
|
Fields: fields,
|
|
|
|
Operations: map[logical.Operation]framework.OperationHandler{
|
|
|
|
logical.ReadOperation: &framework.PathOperation{
|
|
|
|
Callback: b.acmeWrapper(b.acmeDirectoryHandler),
|
|
|
|
ForwardPerformanceSecondary: false,
|
|
|
|
ForwardPerformanceStandby: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathAcmeDirectoryHelpSync,
|
|
|
|
HelpDescription: pathAcmeDirectoryHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type acmeOperation func(acmeCtx acmeContext, r *logical.Request, _ *framework.FieldData) (*logical.Response, error)
|
|
|
|
|
|
|
|
type acmeContext struct {
|
|
|
|
baseUrl *url.URL
|
|
|
|
sc *storageContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) acmeWrapper(op acmeOperation) framework.OperationFunc {
|
|
|
|
return acmeErrorWrapper(func(ctx context.Context, r *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
sc := b.makeStorageContext(ctx, r.Storage)
|
|
|
|
|
|
|
|
if false {
|
|
|
|
// TODO sclark: Check if ACME is enable here
|
2023-03-29 21:08:31 +00:00
|
|
|
return nil, fmt.Errorf("ACME is disabled in configuration: %w", ErrServerInternal)
|
2023-03-29 16:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
baseUrl, err := getAcmeBaseUrl(sc, r.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
acmeCtx := acmeContext{
|
|
|
|
baseUrl: baseUrl,
|
|
|
|
sc: sc,
|
|
|
|
}
|
|
|
|
|
|
|
|
return op(acmeCtx, r, data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAcmeBaseUrl(sc *storageContext, path string) (*url.URL, error) {
|
|
|
|
cfg, err := sc.getClusterConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed loading cluster config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Path == "" {
|
2023-03-29 21:08:31 +00:00
|
|
|
return nil, fmt.Errorf("ACME feature requires local cluster path configuration to be set: %w", ErrServerInternal)
|
2023-03-29 16:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
baseUrl, err := url.Parse(cfg.Path)
|
|
|
|
if err != nil {
|
2023-03-29 21:08:31 +00:00
|
|
|
return nil, fmt.Errorf("ACME feature a proper URL configured in local cluster path: %w", ErrServerInternal)
|
2023-03-29 16:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
directoryPrefix := ""
|
|
|
|
lastIndex := strings.LastIndex(path, "/acme/")
|
|
|
|
if lastIndex != -1 {
|
|
|
|
directoryPrefix = path[0:lastIndex]
|
|
|
|
}
|
|
|
|
|
2023-04-03 13:38:20 +00:00
|
|
|
return baseUrl.JoinPath(directoryPrefix, "/acme/"), nil
|
2023-03-29 16:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func acmeErrorWrapper(op framework.OperationFunc) framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, r *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
resp, err := op(ctx, r, data)
|
|
|
|
if err != nil {
|
2023-03-29 21:08:31 +00:00
|
|
|
return TranslateError(err)
|
2023-03-29 16:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) acmeDirectoryHandler(acmeCtx acmeContext, r *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
|
|
|
|
rawBody, err := json.Marshal(map[string]interface{}{
|
2023-04-03 13:38:20 +00:00
|
|
|
"newNonce": acmeCtx.baseUrl.JoinPath("new-nonce").String(),
|
|
|
|
"newAccount": acmeCtx.baseUrl.JoinPath("new-account").String(),
|
|
|
|
"newOrder": acmeCtx.baseUrl.JoinPath("new-order").String(),
|
|
|
|
"revokeCert": acmeCtx.baseUrl.JoinPath("revoke-cert").String(),
|
|
|
|
"keyChange": acmeCtx.baseUrl.JoinPath("key-change").String(),
|
2023-03-29 16:29:19 +00:00
|
|
|
"meta": map[string]interface{}{
|
|
|
|
"externalAccountRequired": false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed encoding response: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
logical.HTTPContentType: "application/json",
|
|
|
|
logical.HTTPStatusCode: http.StatusOK,
|
|
|
|
logical.HTTPRawBody: rawBody,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|