2016-09-21 14:29:42 +00:00
|
|
|
package transit
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2016-09-21 14:29:42 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2016-09-21 14:29:42 +00:00
|
|
|
)
|
|
|
|
|
2019-07-25 01:22:23 +00:00
|
|
|
const maxBytes = 128 * 1024
|
|
|
|
|
2016-09-21 14:29:42 +00:00
|
|
|
func (b *backend) pathRandom() *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "random" + framework.OptionalParamRegex("urlbytes"),
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"urlbytes": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "The number of bytes to generate (POST URL parameter)",
|
|
|
|
},
|
|
|
|
|
|
|
|
"bytes": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeInt,
|
|
|
|
Default: 32,
|
|
|
|
Description: "The number of bytes to generate (POST body parameter). Defaults to 32 (256 bits).",
|
|
|
|
},
|
|
|
|
|
|
|
|
"format": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Default: "base64",
|
|
|
|
Description: `Encoding format to use. Can be "hex" or "base64". Defaults to "base64".`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.UpdateOperation: b.pathRandomWrite,
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathRandomHelpSyn,
|
|
|
|
HelpDescription: pathRandomHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathRandomWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2016-09-21 14:29:42 +00:00
|
|
|
bytes := 0
|
|
|
|
var err error
|
|
|
|
strBytes := d.Get("urlbytes").(string)
|
|
|
|
if strBytes != "" {
|
|
|
|
bytes, err = strconv.Atoi(strBytes)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("error parsing url-set byte count: %s", err)), nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bytes = d.Get("bytes").(int)
|
|
|
|
}
|
|
|
|
format := d.Get("format").(string)
|
|
|
|
|
|
|
|
if bytes < 1 {
|
|
|
|
return logical.ErrorResponse(`"bytes" cannot be less than 1`), nil
|
|
|
|
}
|
|
|
|
|
2019-07-25 01:22:23 +00:00
|
|
|
if bytes > maxBytes {
|
2021-02-12 03:51:12 +00:00
|
|
|
return logical.ErrorResponse(`"bytes" should be less than %d`, maxBytes), nil
|
2019-07-25 01:22:23 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 14:29:42 +00:00
|
|
|
switch format {
|
|
|
|
case "hex":
|
|
|
|
case "base64":
|
|
|
|
default:
|
2021-02-12 03:51:12 +00:00
|
|
|
return logical.ErrorResponse("unsupported encoding format %q; must be \"hex\" or \"base64\"", format), nil
|
2016-09-21 14:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
randBytes, err := uuid.GenerateRandomBytes(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var retStr string
|
|
|
|
switch format {
|
|
|
|
case "hex":
|
|
|
|
retStr = hex.EncodeToString(randBytes)
|
|
|
|
case "base64":
|
|
|
|
retStr = base64.StdEncoding.EncodeToString(randBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the response
|
|
|
|
resp := &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"random_bytes": retStr,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathRandomHelpSyn = `Generate random bytes`
|
|
|
|
|
|
|
|
const pathRandomHelpDesc = `
|
|
|
|
This function can be used to generate high-entropy random bytes.
|
|
|
|
`
|