2017-12-14 17:51:50 +00:00
|
|
|
package transit
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2019-12-11 15:32:22 +00:00
|
|
|
"errors"
|
|
|
|
"strings"
|
2018-01-08 18:31:38 +00:00
|
|
|
|
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"
|
2017-12-14 17:51:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *backend) pathRestore() *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "restore" + framework.OptionalParamRegex("name"),
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"backup": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Backed up key data to be restored. This should be the output from the 'backup/' endpoint.",
|
|
|
|
},
|
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "If set, this will be the name of the restored key.",
|
|
|
|
},
|
2018-09-25 20:20:59 +00:00
|
|
|
"force": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeBool,
|
|
|
|
Description: "If set and a key by the given name exists, force the restore operation and override the key.",
|
|
|
|
Default: false,
|
|
|
|
},
|
2017-12-14 17:51:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.UpdateOperation: b.pathRestoreUpdate,
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathRestoreHelpSyn,
|
|
|
|
HelpDescription: pathRestoreHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathRestoreUpdate(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2017-12-14 17:51:50 +00:00
|
|
|
backupB64 := d.Get("backup").(string)
|
2018-09-25 20:20:59 +00:00
|
|
|
force := d.Get("force").(bool)
|
2017-12-14 17:51:50 +00:00
|
|
|
if backupB64 == "" {
|
|
|
|
return logical.ErrorResponse("'backup' must be supplied"), nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:16:09 +00:00
|
|
|
// If a name is given, make sure it does not contain any slashes. The Transit
|
|
|
|
// secret engine does not allow sub-paths in key names
|
2019-12-11 15:32:22 +00:00
|
|
|
keyName := d.Get("name").(string)
|
2019-12-11 16:16:09 +00:00
|
|
|
if strings.Contains(keyName, "/") {
|
|
|
|
return nil, ErrInvalidKeyName
|
2019-12-11 15:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, b.lm.RestorePolicy(ctx, req.Storage, keyName, backupB64, force)
|
2017-12-14 17:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const pathRestoreHelpSyn = `Restore the named key`
|
|
|
|
const pathRestoreHelpDesc = `This path is used to restore the named key.`
|
2019-12-11 15:32:22 +00:00
|
|
|
|
|
|
|
var ErrInvalidKeyName = errors.New("key names cannot be paths")
|