-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial ACME new-nonce API implementation * Return proper HTTP status codes for ACME new-nonce API handler
- Loading branch information
1 parent
2834ac2
commit ddb5db1
Showing
7 changed files
with
190 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package pki | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/hashicorp/vault/sdk/framework" | ||
"github.com/hashicorp/vault/sdk/logical" | ||
) | ||
|
||
func pathAcmeRootNonce(b *backend) *framework.Path { | ||
return patternAcmeNonce(b, "acme/new-nonce", false /* requireRole */, false /* requireIssuer */) | ||
} | ||
|
||
func pathAcmeRoleNonce(b *backend) *framework.Path { | ||
return patternAcmeNonce(b, "roles/"+framework.GenericNameRegex("role")+"/acme/new-nonce", | ||
true /* requireRole */, false /* requireIssuer */) | ||
} | ||
|
||
func pathAcmeIssuerNonce(b *backend) *framework.Path { | ||
return patternAcmeNonce(b, "issuer/"+framework.GenericNameRegex(issuerRefParam)+"/acme/new-nonce", | ||
false /* requireRole */, true /* requireIssuer */) | ||
} | ||
|
||
func pathAcmeIssuerAndRoleNonce(b *backend) *framework.Path { | ||
return patternAcmeNonce(b, | ||
"issuer/"+framework.GenericNameRegex(issuerRefParam)+"/roles/"+framework.GenericNameRegex( | ||
"role")+"/acme/new-nonce", | ||
true /* requireRole */, true /* requireIssuer */) | ||
} | ||
|
||
func patternAcmeNonce(b *backend, pattern string, requireRole, requireIssuer bool) *framework.Path { | ||
fields := map[string]*framework.FieldSchema{} | ||
if requireRole { | ||
fields["role"] = &framework.FieldSchema{ | ||
Type: framework.TypeString, | ||
Description: `The desired role for the acme request`, | ||
Required: true, | ||
} | ||
} | ||
if requireIssuer { | ||
fields[issuerRefParam] = &framework.FieldSchema{ | ||
Type: framework.TypeString, | ||
Description: `Reference to an existing issuer name or issuer id`, | ||
Required: true, | ||
} | ||
} | ||
return &framework.Path{ | ||
Pattern: pattern, | ||
Fields: fields, | ||
Operations: map[logical.Operation]framework.OperationHandler{ | ||
logical.HeaderOperation: &framework.PathOperation{ | ||
Callback: b.acmeWrapper(b.acmeNonceHandler), | ||
ForwardPerformanceSecondary: false, | ||
ForwardPerformanceStandby: true, | ||
}, | ||
logical.ReadOperation: &framework.PathOperation{ | ||
Callback: b.acmeWrapper(b.acmeNonceHandler), | ||
ForwardPerformanceSecondary: false, | ||
ForwardPerformanceStandby: true, | ||
}, | ||
}, | ||
|
||
HelpSynopsis: pathAcmeDirectoryHelpSync, | ||
HelpDescription: pathAcmeDirectoryHelpDesc, | ||
} | ||
} | ||
|
||
func (b *backend) acmeNonceHandler(ctx acmeContext, r *logical.Request, _ *framework.FieldData) (*logical.Response, error) { | ||
nonce, _, err := b.acmeState.GetNonce() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Header operations return 200, GET return 204. | ||
httpStatus := http.StatusOK | ||
if r.Operation == logical.ReadOperation { | ||
httpStatus = http.StatusNoContent | ||
} | ||
|
||
return &logical.Response{ | ||
Headers: map[string][]string{ | ||
"Cache-Control": {"no-store"}, | ||
"Replay-Nonce": {nonce}, | ||
"Link": genAcmeLinkHeader(ctx), | ||
}, | ||
Data: map[string]interface{}{ | ||
logical.HTTPStatusCode: httpStatus, | ||
}, | ||
}, nil | ||
} | ||
|
||
func genAcmeLinkHeader(ctx acmeContext) []string { | ||
path := fmt.Sprintf("<%s>;rel=\"index\"", ctx.baseUrl.JoinPath("/acme/directory").String()) | ||
return []string{path} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters