2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-03-19 13:39:25 +00:00
|
|
|
package framework
|
|
|
|
|
2015-03-19 13:59:01 +00:00
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-03-19 13:59:01 +00:00
|
|
|
"time"
|
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-03-19 13:59:01 +00:00
|
|
|
)
|
|
|
|
|
2015-03-19 13:39:25 +00:00
|
|
|
// Secret is a type of secret that can be returned from a backend.
|
|
|
|
type Secret struct {
|
|
|
|
// Type is the name of this secret type. This is used to setup the
|
|
|
|
// vault ID and to look up the proper secret structure when revocation/
|
|
|
|
// renewal happens. Once this is set this should not be changed.
|
2015-03-19 13:59:01 +00:00
|
|
|
//
|
|
|
|
// The format of this must match (case insensitive): ^a-Z0-9_$
|
2015-03-19 13:39:25 +00:00
|
|
|
Type string
|
|
|
|
|
|
|
|
// Fields is the mapping of data fields and schema that comprise
|
|
|
|
// the structure of this secret.
|
|
|
|
Fields map[string]*FieldSchema
|
2015-03-19 13:59:01 +00:00
|
|
|
|
2016-02-01 00:33:16 +00:00
|
|
|
// DefaultDuration is the default value for the duration of the lease for
|
|
|
|
// this secret. This can be manually overwritten with the result of
|
|
|
|
// Response().
|
2015-04-14 00:05:32 +00:00
|
|
|
//
|
2016-02-01 00:33:16 +00:00
|
|
|
// If these aren't set, Vault core will set a default lease period which
|
|
|
|
// may come from a mount tuning.
|
|
|
|
DefaultDuration time.Duration
|
2015-03-19 14:07:45 +00:00
|
|
|
|
2015-03-21 15:20:30 +00:00
|
|
|
// Renew is the callback called to renew this secret. If Renew is
|
2015-04-13 22:18:27 +00:00
|
|
|
// not specified then renewable is set to false in the secret.
|
|
|
|
// See lease.go for helpers for this value.
|
|
|
|
Renew OperationFunc
|
2015-03-21 15:20:30 +00:00
|
|
|
|
|
|
|
// Revoke is the callback called to revoke this secret. This is required.
|
2015-03-19 14:07:45 +00:00
|
|
|
Revoke OperationFunc
|
2015-03-19 13:59:01 +00:00
|
|
|
}
|
|
|
|
|
2015-03-21 15:20:30 +00:00
|
|
|
func (s *Secret) Renewable() bool {
|
2015-04-13 22:18:27 +00:00
|
|
|
return s.Renew != nil
|
2015-03-21 15:20:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 16:59:48 +00:00
|
|
|
func (s *Secret) Response(
|
2022-09-08 00:31:20 +00:00
|
|
|
data, internal map[string]interface{},
|
|
|
|
) *logical.Response {
|
2015-03-20 16:59:48 +00:00
|
|
|
internalData := make(map[string]interface{})
|
|
|
|
for k, v := range internal {
|
|
|
|
internalData[k] = v
|
2015-03-19 13:59:01 +00:00
|
|
|
}
|
2015-03-20 16:59:48 +00:00
|
|
|
internalData["secret_type"] = s.Type
|
2015-03-19 13:59:01 +00:00
|
|
|
|
|
|
|
return &logical.Response{
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2016-02-01 00:33:16 +00:00
|
|
|
TTL: s.DefaultDuration,
|
|
|
|
Renewable: s.Renewable(),
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
|
|
|
InternalData: internalData,
|
2015-03-19 13:59:01 +00:00
|
|
|
},
|
2015-03-19 22:11:42 +00:00
|
|
|
|
2015-03-19 13:59:01 +00:00
|
|
|
Data: data,
|
2015-03-19 22:11:42 +00:00
|
|
|
}
|
2015-03-19 13:39:25 +00:00
|
|
|
}
|
2015-03-21 15:20:30 +00:00
|
|
|
|
|
|
|
// HandleRenew is the request handler for renewing this secret.
|
2018-01-08 18:31:38 +00:00
|
|
|
func (s *Secret) HandleRenew(ctx context.Context, req *logical.Request) (*logical.Response, error) {
|
2015-03-21 15:20:30 +00:00
|
|
|
if !s.Renewable() {
|
|
|
|
return nil, logical.ErrUnsupportedOperation
|
|
|
|
}
|
|
|
|
|
|
|
|
data := &FieldData{
|
|
|
|
Raw: req.Data,
|
|
|
|
Schema: s.Fields,
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
return s.Renew(ctx, req, data)
|
2015-03-21 15:20:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 21:57:39 +00:00
|
|
|
// HandleRevoke is the request handler for revoking this secret.
|
2018-01-08 18:31:38 +00:00
|
|
|
func (s *Secret) HandleRevoke(ctx context.Context, req *logical.Request) (*logical.Response, error) {
|
2015-03-21 15:20:30 +00:00
|
|
|
data := &FieldData{
|
|
|
|
Raw: req.Data,
|
|
|
|
Schema: s.Fields,
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Revoke != nil {
|
2018-01-08 18:31:38 +00:00
|
|
|
return s.Revoke(ctx, req, data)
|
2015-03-21 15:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, logical.ErrUnsupportedOperation
|
|
|
|
}
|